]> git.proxmox.com Git - mirror_novnc.git/blob - core/inflator.js
Move error handling to Inflate class
[mirror_novnc.git] / core / inflator.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 { inflateInit, inflate, inflateReset } from "../vendor/pako/lib/zlib/inflate.js";
10 import ZStream from "../vendor/pako/lib/zlib/zstream.js";
11
12 export default class Inflate {
13 constructor() {
14 this.strm = new ZStream();
15 this.chunkSize = 1024 * 10 * 10;
16 this.strm.output = new Uint8Array(this.chunkSize);
17 this.windowBits = 5;
18
19 inflateInit(this.strm, this.windowBits);
20 }
21
22 inflate(data, expected) {
23 this.strm.input = data;
24 this.strm.avail_in = this.strm.input.length;
25 this.strm.next_in = 0;
26 this.strm.next_out = 0;
27
28 // resize our output buffer if it's too small
29 // (we could just use multiple chunks, but that would cause an extra
30 // allocation each time to flatten the chunks)
31 if (expected > this.chunkSize) {
32 this.chunkSize = expected;
33 this.strm.output = new Uint8Array(this.chunkSize);
34 }
35
36 this.strm.avail_out = this.chunkSize;
37
38 inflate(this.strm, 0); // Flush argument not used.
39
40 if (this.strm.next_out != expected) {
41 throw new Error("Incomplete zlib block");
42 }
43
44 return new Uint8Array(this.strm.output.buffer, 0, this.strm.next_out);
45 }
46
47 reset() {
48 inflateReset(this.strm);
49 }
50 }