]> git.proxmox.com Git - mirror_novnc.git/commitdiff
Add deflator helper class for deflating data
authorNiko Lehto <nikle@cendio.se>
Fri, 7 Feb 2020 12:23:21 +0000 (13:23 +0100)
committerNiko Lehto <nikle@cendio.se>
Mon, 17 Feb 2020 10:29:29 +0000 (11:29 +0100)
Wraps pako's deflate for easier usage.

core/deflator.js [new file with mode: 0644]
tests/test.deflator.js [new file with mode: 0644]
vendor/pako/lib/zlib/deflate.js

diff --git a/core/deflator.js b/core/deflator.js
new file mode 100644 (file)
index 0000000..ad3d0fb
--- /dev/null
@@ -0,0 +1,79 @@
+/*
+ * noVNC: HTML5 VNC client
+ * Copyright (C) 2020 The noVNC Authors
+ * Licensed under MPL 2.0 (see LICENSE.txt)
+ *
+ * See README.md for usage and integration instructions.
+ */
+
+import { deflateInit, deflate } from "../vendor/pako/lib/zlib/deflate.js";
+import { Z_FULL_FLUSH } from "../vendor/pako/lib/zlib/deflate.js";
+import ZStream from "../vendor/pako/lib/zlib/zstream.js";
+
+export default class Deflator {
+    constructor() {
+        this.strm = new ZStream();
+        this.chunkSize = 1024 * 10 * 10;
+        this.outputBuffer = new Uint8Array(this.chunkSize);
+        this.windowBits = 5;
+
+        deflateInit(this.strm, this.windowBits);
+    }
+
+    deflate(inData) {
+        this.strm.input = inData;
+        this.strm.avail_in = this.strm.input.length;
+        this.strm.next_in = 0;
+        this.strm.output = this.outputBuffer;
+        this.strm.avail_out = this.chunkSize;
+        this.strm.next_out = 0;
+
+        let lastRet = deflate(this.strm, Z_FULL_FLUSH);
+        let outData = new Uint8Array(this.strm.output.buffer, 0, this.strm.next_out);
+
+        if (lastRet < 0) {
+            throw new Error("zlib deflate failed");
+        }
+
+        if (this.strm.avail_in > 0) {
+            // Read chunks until done
+
+            let chunks = [outData];
+            let totalLen = outData.length;
+            do {
+                this.strm.output = new Uint8Array(this.chunkSize);
+                this.strm.next_out = 0;
+                this.strm.avail_out = this.chunkSize;
+
+                lastRet = deflate(this.strm, Z_FULL_FLUSH);
+
+                if (lastRet < 0) {
+                    throw new Error("zlib deflate failed");
+                }
+
+                let chunk = new Uint8Array(this.strm.output.buffer, 0, this.strm.next_out);
+                totalLen += chunk.length;
+                chunks.push(chunk);
+            } while (this.strm.avail_in > 0);
+
+            // Combine chunks into a single data
+
+            let newData = new Uint8Array(totalLen);
+            let offset = 0;
+
+            for (let i = 0; i < chunks.length; i++) {
+                newData.set(chunks[i], offset);
+                offset += chunks[i].length;
+            }
+
+            outData = newData;
+        }
+
+        this.strm.input = null;
+        this.strm.avail_in = 0;
+        this.strm.next_in = 0;
+
+        return outData;
+    }
+
+}
diff --git a/tests/test.deflator.js b/tests/test.deflator.js
new file mode 100644 (file)
index 0000000..2f2fab3
--- /dev/null
@@ -0,0 +1,80 @@
+/* eslint-disable no-console */
+const expect = chai.expect;
+
+import { inflateInit, inflate } from "../vendor/pako/lib/zlib/inflate.js";
+import ZStream from "../vendor/pako/lib/zlib/zstream.js";
+import Deflator from "../core/deflator.js";
+
+function _inflator(compText, expected) {
+    let strm = new ZStream();
+    let chunkSize = 1024 * 10 * 10;
+    strm.output = new Uint8Array(chunkSize);
+
+    inflateInit(strm, 5);
+
+    if (expected > chunkSize) {
+        chunkSize = expected;
+        strm.output = new Uint8Array(chunkSize);
+    }
+
+    strm.input = compText;
+    strm.avail_in = strm.input.length;
+    strm.next_in = 0;
+
+    strm.next_out = 0;
+    strm.avail_out = expected.length;
+
+    let ret = inflate(strm, 0);
+
+    // Check that return code is not an error
+    expect(ret).to.be.greaterThan(-1);
+
+    return new Uint8Array(strm.output.buffer, 0, strm.next_out);
+}
+
+describe('Deflate data', function () {
+
+    it('should be able to deflate messages', function () {
+        let deflator = new Deflator();
+
+        let text = "123asdf";
+        let preText = new Uint8Array(text.length);
+        for (let i = 0; i < preText.length; i++) {
+            preText[i] = text.charCodeAt(i);
+        }
+
+        let compText = deflator.deflate(preText);
+
+        let inflatedText = _inflator(compText, text.length);
+        expect(inflatedText).to.array.equal(preText);
+
+    });
+
+    it('should be able to deflate large messages', function () {
+        let deflator = new Deflator();
+
+        /* Generate a big string with random characters. Used because
+           repetition of letters might be deflated more effectively than
+           random ones. */
+        let text = "";
+        let characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
+        for (let i = 0; i < 300000; i++) {
+            text += characters.charAt(Math.floor(Math.random() * characters.length));
+        }
+
+        let preText = new Uint8Array(text.length);
+        for (let i = 0; i < preText.length; i++) {
+            preText[i] = text.charCodeAt(i);
+        }
+
+        let compText = deflator.deflate(preText);
+
+        //Check that the compressed size is expected size
+        expect(compText.length).to.be.greaterThan((1024 * 10 * 10) * 2);
+
+        let inflatedText = _inflator(compText, text.length);
+
+        expect(inflatedText).to.array.equal(preText);
+
+    });
+});
index c51915e2d948c05ad416dde0e2f43abfdfa9442b..c3a5ba49aa7443a864038312f0112664493a4a69 100644 (file)
@@ -9,51 +9,51 @@ import msg from "./messages.js";
 
 
 /* Allowed flush values; see deflate() and inflate() below for details */
-var Z_NO_FLUSH      = 0;
-var Z_PARTIAL_FLUSH = 1;
-//var Z_SYNC_FLUSH    = 2;
-var Z_FULL_FLUSH    = 3;
-var Z_FINISH        = 4;
-var Z_BLOCK         = 5;
-//var Z_TREES         = 6;
+export const Z_NO_FLUSH      = 0;
+export const Z_PARTIAL_FLUSH = 1;
+//export const Z_SYNC_FLUSH    = 2;
+export const Z_FULL_FLUSH    = 3;
+export const Z_FINISH        = 4;
+export const Z_BLOCK         = 5;
+//export const Z_TREES         = 6;
 
 
 /* Return codes for the compression/decompression functions. Negative values
  * are errors, positive values are used for special but normal events.
  */
-var Z_OK            = 0;
-var Z_STREAM_END    = 1;
-//var Z_NEED_DICT     = 2;
-//var Z_ERRNO         = -1;
-var Z_STREAM_ERROR  = -2;
-var Z_DATA_ERROR    = -3;
-//var Z_MEM_ERROR     = -4;
-var Z_BUF_ERROR     = -5;
-//var Z_VERSION_ERROR = -6;
+export const Z_OK            = 0;
+export const Z_STREAM_END    = 1;
+//export const Z_NEED_DICT     = 2;
+//export const Z_ERRNO         = -1;
+export const Z_STREAM_ERROR  = -2;
+export const Z_DATA_ERROR    = -3;
+//export const Z_MEM_ERROR     = -4;
+export const Z_BUF_ERROR     = -5;
+//export const Z_VERSION_ERROR = -6;
 
 
 /* compression levels */
-//var Z_NO_COMPRESSION      = 0;
-//var Z_BEST_SPEED          = 1;
-//var Z_BEST_COMPRESSION    = 9;
-var Z_DEFAULT_COMPRESSION = -1;
+//export const Z_NO_COMPRESSION      = 0;
+//export const Z_BEST_SPEED          = 1;
+//export const Z_BEST_COMPRESSION    = 9;
+export const Z_DEFAULT_COMPRESSION = -1;
 
 
-var Z_FILTERED            = 1;
-var Z_HUFFMAN_ONLY        = 2;
-var Z_RLE                 = 3;
-var Z_FIXED               = 4;
-var Z_DEFAULT_STRATEGY    = 0;
+export const Z_FILTERED            = 1;
+export const Z_HUFFMAN_ONLY        = 2;
+export const Z_RLE                 = 3;
+export const Z_FIXED               = 4;
+export const Z_DEFAULT_STRATEGY    = 0;
 
 /* Possible values of the data_type field (though see inflate()) */
-//var Z_BINARY              = 0;
-//var Z_TEXT                = 1;
-//var Z_ASCII               = 1; // = Z_TEXT
-var Z_UNKNOWN             = 2;
+//export const Z_BINARY              = 0;
+//export const Z_TEXT                = 1;
+//export const Z_ASCII               = 1; // = Z_TEXT
+export const Z_UNKNOWN             = 2;
 
 
 /* The deflate compression method */
-var Z_DEFLATED  = 8;
+export const Z_DEFLATED  = 8;
 
 /*============================================================================*/