]> git.proxmox.com Git - mirror_novnc.git/blob - core/deflator.js
ad3d0fb77107dbd5d9d357241ca96e9a302be297
[mirror_novnc.git] / core / deflator.js
1 /*
2 * noVNC: HTML5 VNC client
3 * Copyright (C) 2020 The noVNC Authors
4 * Licensed under MPL 2.0 (see LICENSE.txt)
5 *
6 * See README.md for usage and integration instructions.
7 */
8
9 import { deflateInit, deflate } from "../vendor/pako/lib/zlib/deflate.js";
10 import { Z_FULL_FLUSH } from "../vendor/pako/lib/zlib/deflate.js";
11 import ZStream from "../vendor/pako/lib/zlib/zstream.js";
12
13 export default class Deflator {
14 constructor() {
15 this.strm = new ZStream();
16 this.chunkSize = 1024 * 10 * 10;
17 this.outputBuffer = new Uint8Array(this.chunkSize);
18 this.windowBits = 5;
19
20 deflateInit(this.strm, this.windowBits);
21 }
22
23 deflate(inData) {
24 this.strm.input = inData;
25 this.strm.avail_in = this.strm.input.length;
26 this.strm.next_in = 0;
27 this.strm.output = this.outputBuffer;
28 this.strm.avail_out = this.chunkSize;
29 this.strm.next_out = 0;
30
31 let lastRet = deflate(this.strm, Z_FULL_FLUSH);
32 let outData = new Uint8Array(this.strm.output.buffer, 0, this.strm.next_out);
33
34 if (lastRet < 0) {
35 throw new Error("zlib deflate failed");
36 }
37
38 if (this.strm.avail_in > 0) {
39 // Read chunks until done
40
41 let chunks = [outData];
42 let totalLen = outData.length;
43 do {
44 this.strm.output = new Uint8Array(this.chunkSize);
45 this.strm.next_out = 0;
46 this.strm.avail_out = this.chunkSize;
47
48 lastRet = deflate(this.strm, Z_FULL_FLUSH);
49
50 if (lastRet < 0) {
51 throw new Error("zlib deflate failed");
52 }
53
54 let chunk = new Uint8Array(this.strm.output.buffer, 0, this.strm.next_out);
55 totalLen += chunk.length;
56 chunks.push(chunk);
57 } while (this.strm.avail_in > 0);
58
59 // Combine chunks into a single data
60
61 let newData = new Uint8Array(totalLen);
62 let offset = 0;
63
64 for (let i = 0; i < chunks.length; i++) {
65 newData.set(chunks[i], offset);
66 offset += chunks[i].length;
67 }
68
69 outData = newData;
70 }
71
72 this.strm.input = null;
73 this.strm.avail_in = 0;
74 this.strm.next_in = 0;
75
76 return outData;
77 }
78
79 }