]>
Commit | Line | Data |
---|---|---|
f52e9790 NL |
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) { | |
cfb824ed | 24 | /* eslint-disable camelcase */ |
f52e9790 NL |
25 | this.strm.input = inData; |
26 | this.strm.avail_in = this.strm.input.length; | |
27 | this.strm.next_in = 0; | |
28 | this.strm.output = this.outputBuffer; | |
29 | this.strm.avail_out = this.chunkSize; | |
30 | this.strm.next_out = 0; | |
cfb824ed | 31 | /* eslint-enable camelcase */ |
f52e9790 NL |
32 | |
33 | let lastRet = deflate(this.strm, Z_FULL_FLUSH); | |
34 | let outData = new Uint8Array(this.strm.output.buffer, 0, this.strm.next_out); | |
35 | ||
36 | if (lastRet < 0) { | |
37 | throw new Error("zlib deflate failed"); | |
38 | } | |
39 | ||
40 | if (this.strm.avail_in > 0) { | |
41 | // Read chunks until done | |
42 | ||
43 | let chunks = [outData]; | |
44 | let totalLen = outData.length; | |
45 | do { | |
cfb824ed | 46 | /* eslint-disable camelcase */ |
f52e9790 NL |
47 | this.strm.output = new Uint8Array(this.chunkSize); |
48 | this.strm.next_out = 0; | |
49 | this.strm.avail_out = this.chunkSize; | |
cfb824ed | 50 | /* eslint-enable camelcase */ |
f52e9790 NL |
51 | |
52 | lastRet = deflate(this.strm, Z_FULL_FLUSH); | |
53 | ||
54 | if (lastRet < 0) { | |
55 | throw new Error("zlib deflate failed"); | |
56 | } | |
57 | ||
58 | let chunk = new Uint8Array(this.strm.output.buffer, 0, this.strm.next_out); | |
59 | totalLen += chunk.length; | |
60 | chunks.push(chunk); | |
61 | } while (this.strm.avail_in > 0); | |
62 | ||
63 | // Combine chunks into a single data | |
64 | ||
65 | let newData = new Uint8Array(totalLen); | |
66 | let offset = 0; | |
67 | ||
68 | for (let i = 0; i < chunks.length; i++) { | |
69 | newData.set(chunks[i], offset); | |
70 | offset += chunks[i].length; | |
71 | } | |
72 | ||
73 | outData = newData; | |
74 | } | |
75 | ||
cfb824ed | 76 | /* eslint-disable camelcase */ |
f52e9790 NL |
77 | this.strm.input = null; |
78 | this.strm.avail_in = 0; | |
79 | this.strm.next_in = 0; | |
cfb824ed | 80 | /* eslint-enable camelcase */ |
f52e9790 NL |
81 | |
82 | return outData; | |
83 | } | |
84 | ||
85 | } |