适用于GZIP的Javascript(浏览器)解压缩器

时间:2018-07-16 02:48:32

标签: javascript compression

这个问题与被认为是一个好的Stackoverflow问题有点不同,但是,我想这是许多人可能需要的东西,所以这是我的情况:

我正在制作一个Web应用程序,该应用程序将主要通过手机在互联网上行链路(农场)速度很慢的地方进行访问,但仍需要与服务器保持非常动态的信息交换。

到目前为止,我正在使用websocket来减少通信开销,仅交换JSON数组上的原始数据,甚至通过此套接字接收CSS和脚本,所有这些都已缩小。

为了减少更多的数据交换,我考虑过在服务器端使用GZIP压缩,然后在客户端使用所有压缩,反之亦然。

在压缩领域中,我是一个完整的菜鸟,请问各位朋友,是否有用于文本数据压缩/解压缩的JavaScript插件?

在首次访问时不需要费时的事情(具体来说,小于1M)。以下是我未缩小的初始脚本,该脚本处理了所有通信:

let ws = new WebSocket("wss://localhost/Socket");

ws.onopen = function () {
    let
            functions = new Map(),
            loadedScripts = new Set();

    //receives a JSON array with two elements
    //the first tells what to do with the data,
    //which is an integer that is key for a mapped function
    ws.onmessage = function (event) {
        let data = JSON.parse(event.data);

        //finds the determined function on the map and passes the second
        //item of the array as argument to it
        functions.get(data[0])(data[1]);
    };

    // the function #1 is the one who discovers if the reqested script
    // and version are already loaded on are stored on the local storage
    // from a previous access, if not sends a request for the given script
    // it receives an array of "requirements"
    functions.set(1, function (array) { //require [name, token, version]
        for (let require in array) {
            loadedScripts.add(require[0]);
            if (typeof window[require[0]] !== "function" || eval(require[0]).version !== require[2]) {
                if (eval(localStorage[require[0]]) !== require[2]) {
                    ws.send(JSON.stringify([2, require[1]]));
                }
            }
       }
    });

    // the #2 function is the one who evaluates the string, which is a 
    // script, received on the websocket
    // HERE COMPRESSION WOULD BE VERY HELPFULL
    functions.set(2, function (script) { //[scriptName, script]
        eval(script[1]);
        try {
            localStorage[script[0]] = script[1];
        } catch (e) {
            if (e instanceof DOMException) {
                for (let k in localStorage) {
                    if (!loadedScripts.has(k)) {
                        localStorage.removeItem(k);
                    }
                }
                functions.get(2)(script);
            } else {
                throw e;
            }
        }
    });
};

//...

对于任何拼写错误或不正确的文字,我会深表歉意,而不是我的语言。

编辑

我实际上已经在谷歌上搜索了一些插件,但是我对这个主题的了解非常有限,这使我不太了解它。因此,我的问题有点像“压缩新手”。

0 个答案:

没有答案