<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">//! © 2015 Nathan Rugg &lt;nmrugg@gmail.com&gt; | MIT
/// See LICENSE for more details.

// jshint bitwise:true, curly:true, eqeqeq:true, forin:true, immed:true, latedef:true, newcap:true, noarg:true, noempty:true, nonew:true, onevar:true, plusplus:true, quotmark:double, undef:true, unused:strict, browser: true, node: true

/// Does the environment support web workers?  If not, let's load the worker manually (without polluting the global scope).
if (typeof Worker === "undefined" || (typeof location !== "undefined" &amp;&amp; location.protocol === "file:")) {
    /// Is this Node.js?
    if (typeof global !== "undefined" &amp;&amp; typeof require !== "undefined") {
        this.LZMA = function (lzma_path) {
            return require(lzma_path || "./lzma_worker.js").LZMA;
        };
    /// Is this a browser?
    } else if (typeof window !== "undefined" &amp;&amp; window.document) {
        (function () {
            var that = this,
                global_var,
                req = function req(path) {
                    var script_tag  = document.createElement("script");
                    script_tag.type ="text/javascript";
                    script_tag.src  = path;
                    script_tag.onload = function () {
                        /// Make sure this LZMA variable doesn't get overwritten by the worker's.
                        that.LZMA = non_worker_lzma;
                    };
                    document.getElementsByTagName("head")[0].appendChild(script_tag);
                };
            
            /// Determine the global variable (it's called "window" in browsers, "global" in Node.js).
            if (typeof window !== "undefined") {
                global_var = window;
            } else if (global) {
                global_var = global;
            }
            
            function non_worker_lzma(path) {
                var fake_lzma;
                
                req(path);
                
                fake_lzma = {
                    compress: function compress(mixed, mode, on_finish, on_progress) {
                        if (global_var.LZMA_WORKER) {
                            global_var.LZMA_WORKER.compress(mixed, mode, on_finish, on_progress);
                        } else {
                            /// Wait
                            setTimeout(function ()
                            {
                                fake_lzma.compress(mixed, mode, on_finish, on_progress);
                            }, 50);
                        }
                    },
                    decompress: function decompress(byte_arr, on_finish, on_progress) {
                        if (global_var.LZMA_WORKER) {
                            global_var.LZMA_WORKER.decompress(byte_arr, on_finish, on_progress);
                        } else {
                            /// Wait
                            setTimeout(function ()
                            {
                                fake_lzma.decompress(byte_arr, on_finish, on_progress);
                            }, 50);
                        }
                    },
                    worker: function worker () {
                        return null;
                    }
                };
                
                return fake_lzma;
            }
            
            that.LZMA = non_worker_lzma;
        }());
    } else {
        /// It doesn't seem to be either Node.js or a browser.
        console.error("Can't load the worker. Sorry.");
    }
} else {
    /// Let's use Web Workers.
    ///NOTE: The "this" keyword is the global context ("window" variable) if loaded via a &lt;script&gt; tag
    ///      or the function context if loaded as a module (e.g., in Node.js).
    this.LZMA = function (lzma_path) {
        var action_compress   = 1,
            action_decompress = 2,
            action_progress   = 3,
            
            callback_obj = {},
            lzma_worker;
            ///NOTE: Node.js needs something like "./" or "../" at the beginning.
            var xhr = new XMLHttpRequest();
            xhr.open('get',lzma_path || "./lzma_worker-min.js", true);
            xhr.onreadystatechange = function() {
                if(xhr.readyState == 4){
                    if(xhr.status == 200){
                        var worker_blob=new Blob([xhr.responseText])
                        lzma_worker = new Worker(window.URL.createObjectURL(worker_blob));
                        lzma_worker.onmessage = function onmessage(e) {
                            if (e.data.action === action_progress) {
                                if (callback_obj[e.data.cbn] &amp;&amp; typeof callback_obj[e.data.cbn].on_progress === "function") {
                                    callback_obj[e.data.cbn].on_progress(e.data.result);
                                }
                            } else {
                                if (callback_obj[e.data.cbn] &amp;&amp; typeof callback_obj[e.data.cbn].on_finish === "function") {
                                    callback_obj[e.data.cbn].on_finish(e.data.result, e.data.error);
                                    
                                    /// Since the (de)compression is complete, the callbacks are no longer needed.
                                    delete callback_obj[e.data.cbn];
                                }
                            }
                        };
                        
                        /// Very simple error handling.
                        lzma_worker.onerror = function(event) {
                            
                            console.log(11)
                            var err = new Error(event.message + " (" + event.filename + ":" + event.lineno + ")");
                            
                            for (var cbn in callback_obj) {
                                callback_obj[cbn].on_finish(null, err);
                            }
                            
                            console.error('Uncaught error in lzma_worker', err);
                        };
                    }
                }
            }
            xhr.send();
        // var script_text  = document.createElement("script");
        // script_text.src = lzma_path || "./lzma_worker-min.js";
        // console.dir(script_text)
        // script_text.onload = function() {
        //     console.log(123);
        //     var worker_blob=new Blob([script_text.textContent])
        //     lzma_worker = new Worker(window.URL.createObjectURL(worker_blob));
        //     lzma_worker.onmessage = function onmessage(e) {
        //         if (e.data.action === action_progress) {
        //             if (callback_obj[e.data.cbn] &amp;&amp; typeof callback_obj[e.data.cbn].on_progress === "function") {
        //                 callback_obj[e.data.cbn].on_progress(e.data.result);
        //             }
        //         } else {
        //             if (callback_obj[e.data.cbn] &amp;&amp; typeof callback_obj[e.data.cbn].on_finish === "function") {
        //                 callback_obj[e.data.cbn].on_finish(e.data.result, e.data.error);
                        
        //                 /// Since the (de)compression is complete, the callbacks are no longer needed.
        //                 delete callback_obj[e.data.cbn];
        //             }
        //         }
        //     };
            
        //     /// Very simple error handling.
        //     lzma_worker.onerror = function(event) {
                
        //         console.log(11)
        //         var err = new Error(event.message + " (" + event.filename + ":" + event.lineno + ")");
                
        //         for (var cbn in callback_obj) {
        //             callback_obj[cbn].on_finish(null, err);
        //         }
                
        //         console.error('Uncaught error in lzma_worker', err);
        //     };
        // }
        
        
        return (function () {
            
            function send_to_worker(action, data, mode, on_finish, on_progress) {
                var cbn;
                
                do {
                    cbn = Math.floor(Math.random() * (10000000));
                } while(typeof callback_obj[cbn] !== "undefined");
                
                callback_obj[cbn] = {
                    on_finish:   on_finish,
                    on_progress: on_progress
                };
                
                lzma_worker.postMessage({
                    action: action, /// action_compress = 1, action_decompress = 2, action_progress = 3
                    cbn:    cbn,    /// callback number
                    data:   data,
                    mode:   mode
                });
            }
            
            return {
                compress: function compress(mixed, mode, on_finish, on_progress) {
                    send_to_worker(action_compress, mixed, mode, on_finish, on_progress);
                },
                decompress: function decompress(byte_arr, on_finish, on_progress) {
                    send_to_worker(action_decompress, byte_arr, false, on_finish, on_progress);
                },
                worker: function worker() {
                    return lzma_worker;
                }
            };
        }());
    };
}
</pre></body></html>