-
Notifications
You must be signed in to change notification settings - Fork 21
/
brotli.spec.ts
421 lines (368 loc) · 19.2 KB
/
brotli.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
import { TextEncoder, TextDecoder } from 'text-encoding';
if (typeof global !== 'undefined' && typeof global.TextEncoder === 'undefined') {
global.TextEncoder = TextEncoder;
global.TextDecoder = TextDecoder;
}
let getRandomValues: typeof crypto.getRandomValues;
let btoa: typeof global.btoa;
let atob: typeof global.atob;
if (typeof process !== 'undefined' && process.versions.node) {
// Polyfill for web APIs not present in some node versions:
atob = global.atob || require('atob');
btoa = global.btoa || require('btoa');
// This is actually available as crypto.webcrypto in Node 15+, but not in older versions.
const { Crypto } = require('@peculiar/webcrypto');
const crypto = new Crypto();
getRandomValues = crypto.getRandomValues.bind(crypto);
} else {
getRandomValues = crypto.getRandomValues.bind(crypto);
btoa = globalThis.btoa;
atob = globalThis.atob;
}
const textEncoder = new TextEncoder();
const textDecoder = new TextDecoder();
const dataToBase64 = (data: Uint8Array | number[]) => btoa(String.fromCharCode(...data));
const base64ToData = (base64: string) => new Uint8Array(
[...atob(base64)].map(c => c.charCodeAt(0))
);
import { expect } from 'chai';
import brotliPromise, { type BrotliWasmType } from '..';
describe("Brotli-wasm", () => {
let brotli: BrotliWasmType;
beforeEach(async () => {
brotli = await brotliPromise;
});
it("can compress data", () => {
const input = textEncoder.encode("Test input data");
const result = brotli.compress(input);
expect(dataToBase64(result)).to.equal('Gw4A+KWpyubolCCjVAjmxJ4D');
});
it("can compress data with a different quality setting", () => {
const input = textEncoder.encode("Test input data");
const result = brotli.compress(input, { quality: 1 });
expect(dataToBase64(result)).to.equal('CweAVGVzdCBpbnB1dCBkYXRhAw==');
});
it("can decompress data", () => {
// Generated with: echo -n '$CONTENT' | brotli --stdout - | base64
const input = base64ToData('GxoAABypU587dC0k9ianQOgqjS32iUTcCA==');
const result = brotli.decompress(input);
expect(textDecoder.decode(result)).to.equal('Brotli brotli brotli brotli');
});
it("can compress and decompress data many times", function () {
this.timeout(10000); // Should only take 2-4 seconds, but leave some slack
const input = textEncoder.encode("Test input data");
for (let i = 0; i < 500; i++) {
const compressed = brotli.compress(input);
expect(dataToBase64(compressed)).to.equal('Gw4A+KWpyubolCCjVAjmxJ4D');
const decompressed = brotli.decompress(compressed);
expect(textDecoder.decode(decompressed)).to.equal('Test input data');
}
});
it("cleanly fails when options is something other than an object", () => {
const input = textEncoder.encode("Test input data");
expect(() =>
brotli.compress(input, "this should not be a string" as any)
).to.throw('Options is not an object');
});
it("does not fail when options contain unknown properties", () => {
const input = textEncoder.encode("Test input data");
const result = brotli.compress(input, { someRandomKey: 1, quality: 5 } as any);
expect(dataToBase64(result)).to.equal('CweAVGVzdCBpbnB1dCBkYXRhAw==');
});
it("does not fail when compressing with an illegal quality value", () => {
const input = textEncoder.encode("Test input data");
const result = brotli.compress(input, { quality: 12 });
expect(dataToBase64(result)).to.equal('Gw4A+KWpyubolCCjVAjmxJ4D');
});
it("cleanly fails when decompressing garbage", () => {
const input = textEncoder.encode("This is not brotli data, it's just a string");
expect(() =>
brotli.decompress(input)
).to.throw('Brotli decompress failed');
});
it("can compress & decompress back to the original result", () => {
const input = "Some thrilling text I urgently need to compress";
const result = textDecoder.decode(
brotli.decompress(brotli.compress(textEncoder.encode(input)))
);
expect(result).to.equal(input);
});
it("can compress & decompress back to the original result with a different quality setting", () => {
const input = "Some thrilling text I urgently need to compress";
const result = textDecoder.decode(
brotli.decompress(brotli.compress(textEncoder.encode(input), { quality: 3 }))
);
expect(result).to.equal(input);
});
it("can streamingly compress data", () => {
const input = textEncoder.encode("Test input data");
const input1 = input.slice(0, input.length / 2);
const input2 = input.slice(input.length / 2);
const stream = new brotli.CompressStream();
const result1 = stream.compress(input1, 100);
const output1 = result1.buf;
expect(result1.code).to.equal(brotli.BrotliStreamResultCode.NeedsMoreInput);
const result2 = stream.compress(input2, 100);
const output2 = result2.buf;
expect(result2.code).to.equal(brotli.BrotliStreamResultCode.NeedsMoreInput);
const result3 = stream.compress(undefined, 100);
const output3 = result3.buf;
expect(result3.code).to.equal(brotli.BrotliStreamResultCode.ResultSuccess);
expect(dataToBase64([...output1, ...output2, ...output3])).to.equal('Gw4A+KWpyubolCCjVAjmxJ4D');
});
it("can streamingly compress data with a different quality setting", () => {
const input = textEncoder.encode("Test input data");
const input1 = input.slice(0, input.length / 2);
const input2 = input.slice(input.length / 2);
const quality = 1;
const stream = new brotli.CompressStream(quality);
const result1 = stream.compress(input1, 100);
const output1 = result1.buf;
expect(result1.code).to.equal(brotli.BrotliStreamResultCode.NeedsMoreInput);
const result2 = stream.compress(input2, 100);
const output2 = result2.buf;
expect(result2.code).to.equal(brotli.BrotliStreamResultCode.NeedsMoreInput);
const result3 = stream.compress(undefined, 100);
const output3 = result3.buf;
expect(result3.code).to.equal(brotli.BrotliStreamResultCode.ResultSuccess);
// It will be different from non-streaming result.
// But it can still be decompressed back to the original string.
let output = new Uint8Array([...output1, ...output2, ...output3]);
expect(dataToBase64(brotli.decompress(output))).to.equal(dataToBase64(input));
});
it("can streamingly decompress data", () => {
// Generated with: echo -n '$CONTENT' | brotli --stdout - | base64
const input = base64ToData('GxoAABypU587dC0k9ianQOgqjS32iUTcCA==');
const input1 = input.slice(0, input.length / 2);
const input2 = input.slice(input.length / 2);
const stream = new brotli.DecompressStream();
const result1 = stream.decompress(input1, 100);
const output1 = result1.buf;
expect(result1.code).to.equal(brotli.BrotliStreamResultCode.NeedsMoreInput);
const result2 = stream.decompress(input2, 100);
const output2 = result2.buf;
expect(result2.code).to.equal(brotli.BrotliStreamResultCode.ResultSuccess);
expect(textDecoder.decode(new Uint8Array([...output1, ...output2]))).to.equal('Brotli brotli brotli brotli');
});
it("does not fail when streamingly compressing with an illegal quality value", () => {
const input = textEncoder.encode("Test input data");
const input1 = input.slice(0, input.length / 2);
const input2 = input.slice(input.length / 2);
const quality = 12;
const stream = new brotli.CompressStream(quality);
const result1 = stream.compress(input1, 100);
const output1 = result1.buf;
expect(result1.code).to.equal(brotli.BrotliStreamResultCode.NeedsMoreInput);
const result2 = stream.compress(input2, 100);
const output2 = result2.buf;
expect(result2.code).to.equal(brotli.BrotliStreamResultCode.NeedsMoreInput);
const result3 = stream.compress(undefined, 100);
const output3 = result3.buf;
expect(result3.code).to.equal(brotli.BrotliStreamResultCode.ResultSuccess);
expect(dataToBase64([...output1, ...output2, ...output3])).to.equal('Gw4A+KWpyubolCCjVAjmxJ4D');
});
it("cleanly fails when streamingly decompressing garbage", () => {
const input = textEncoder.encode("This is not brotli data, it's just a string");
const stream = new brotli.DecompressStream();
expect(() =>
stream.decompress(input, 100)
).to.throw('Brotli streaming decompress failed');
});
it("can streamingly compress & decompress back to the original result", () => {
const s = "Some thrilling text I urgently need to compress";
const encInput = textEncoder.encode(s);
const encInput1 = encInput.slice(0, encInput.length / 2);
const encInput2 = encInput.slice(encInput.length / 2);
const encStream = new brotli.CompressStream();
const encResult1 = encStream.compress(encInput1, 100);
const encOutput1 = encResult1.buf;
expect(encResult1.code).to.equal(brotli.BrotliStreamResultCode.NeedsMoreInput);
const encResult2 = encStream.compress(encInput2, 100);
const encOutput2 = encResult2.buf;
expect(encResult2.code).to.equal(brotli.BrotliStreamResultCode.NeedsMoreInput);
const encResult3 = encStream.compress(undefined, 100);
const encOutput3 = encResult3.buf;
expect(encResult3.code).to.equal(brotli.BrotliStreamResultCode.ResultSuccess);
const encOutput = new Uint8Array([...encOutput1, ...encOutput2, ...encOutput3]);
const decInput1 = encOutput.slice(0, encOutput.length / 2);
const decInput2 = encOutput.slice(encOutput.length / 2);
const decStream = new brotli.DecompressStream();
const decResult1 = decStream.decompress(decInput1, 100);
const decOutput1 = decResult1.buf;
expect(decResult1.code).to.equal(brotli.BrotliStreamResultCode.NeedsMoreInput);
const decResult2 = decStream.decompress(decInput2, 100);
const decOutput2 = decResult2.buf;
expect(decResult2.code).to.equal(brotli.BrotliStreamResultCode.ResultSuccess);
const decOutput = new Uint8Array([...decOutput1, ...decOutput2]);
expect(textDecoder.decode(decOutput)).to.equal(s);
});
it("can streamingly compress & decompress back to the original result with a different quality setting", () => {
const s = "Some thrilling text I urgently need to compress";
const encInput = textEncoder.encode(s);
const encInput1 = encInput.slice(0, encInput.length / 2);
const encInput2 = encInput.slice(encInput.length / 2);
const quality = 3;
const encStream = new brotli.CompressStream(quality);
const encResult1 = encStream.compress(encInput1, 100);
const encOutput1 = encResult1.buf;
expect(encResult1.code).to.equal(brotli.BrotliStreamResultCode.NeedsMoreInput);
const encResult2 = encStream.compress(encInput2, 100);
const encOutput2 = encResult2.buf;
expect(encResult2.code).to.equal(brotli.BrotliStreamResultCode.NeedsMoreInput);
const encResult3 = encStream.compress(undefined, 100);
const encOutput3 = encResult3.buf;
expect(encResult3.code).to.equal(brotli.BrotliStreamResultCode.ResultSuccess);
const encOutput = new Uint8Array([...encOutput1, ...encOutput2, ...encOutput3]);
const decInput1 = encOutput.slice(0, encOutput.length / 2);
const decInput2 = encOutput.slice(encOutput.length / 2);
const decStream = new brotli.DecompressStream();
const decResult1 = decStream.decompress(decInput1, 100);
const decOutput1 = decResult1.buf;
expect(decResult1.code).to.equal(brotli.BrotliStreamResultCode.NeedsMoreInput);
const decResult2 = decStream.decompress(decInput2, 100);
const decOutput2 = decResult2.buf;
expect(decResult2.code).to.equal(brotli.BrotliStreamResultCode.ResultSuccess);
const decOutput = new Uint8Array([...decOutput1, ...decOutput2]);
expect(textDecoder.decode(decOutput)).to.equal(s);
});
it("streaming compressing can handle needing more output when action is process", function () {
this.timeout(10000);
// The input should be more than about 1.6MB with enough randomness
// to make the compressor ask for more output space when the action is PROCESS
const input = generateRandomBytes(1600000);
const stream = new brotli.CompressStream();
const result1 = stream.compress(input, 1);
const output1 = result1.buf;
expect(result1.code).to.equal(brotli.BrotliStreamResultCode.NeedsMoreOutput);
const result2 = stream.compress(input.slice(result1.input_offset), 1500000);
const output2 = result2.buf;
expect(result2.code).to.equal(brotli.BrotliStreamResultCode.NeedsMoreInput);
const result3 = stream.compress(undefined, 1640000);
const output3 = result3.buf;
expect(result3.code).to.equal(brotli.BrotliStreamResultCode.ResultSuccess);
const output = new Uint8Array([...output1, ...output2, ...output3]);
expect([...brotli.decompress(output)]).to.deep.equal([...input]);
});
it("streaming compressing can handle needing more output when action is finish", () => {
const input = textEncoder.encode('Some thrilling text I urgently need to compress');
const stream = new brotli.CompressStream();
const result1 = stream.compress(input, 1);
const output1 = result1.buf;
expect(result1.code).to.equal(brotli.BrotliStreamResultCode.NeedsMoreInput);
const result2 = stream.compress(undefined, 1);
const output2 = result2.buf;
expect(result2.code).to.equal(brotli.BrotliStreamResultCode.NeedsMoreOutput);
const result3 = stream.compress(undefined, 100);
const output3 = result3.buf;
expect(result3.code).to.equal(brotli.BrotliStreamResultCode.ResultSuccess);
const output = new Uint8Array([...output1, ...output2, ...output3]);
expect(dataToBase64(brotli.decompress(output))).to.equal(dataToBase64(input));
});
it("streaming decompressing can handle needing more output", () => {
const input = base64ToData('GxoAABypU587dC0k9ianQOgqjS32iUTcCA==');
const stream = new brotli.DecompressStream();
const result1 = stream.decompress(input, 1);
const output1 = result1.buf;
expect(result1.code).to.equal(brotli.BrotliStreamResultCode.NeedsMoreOutput);
const result2 = stream.decompress(input.slice(result1.input_offset), 100);
const output2 = result2.buf;
expect(result2.code).to.equal(brotli.BrotliStreamResultCode.ResultSuccess);
expect(textDecoder.decode(new Uint8Array([...output1, ...output2]))).to.equal('Brotli brotli brotli brotli');
});
const areStreamsAvailable = !!globalThis.TransformStream;
it("can streamingly compress & decompress back to the original result with web streams", async function () {
if (!areStreamsAvailable) return this.skip();
// This is very similar to the streaming example in the README, but with reduced buffer sizes to
// try and ensure it catches any issues:
let input = "";
for (let i = 0; i < 1000; i++) {
input += `${i}: Brotli brotli brotli brotli `;
}
const inputStream = new ReadableStream({
start(controller) {
controller.enqueue(input);
controller.close();
}
});
const textEncoderStream = new TextEncoderStream();
const compressStream = new brotli.CompressStream();
const compressionStream = new TransformStream({
transform(chunk, controller) {
let resultCode;
let inputOffset = 0;
do {
const input = chunk.slice(inputOffset);
const result = compressStream.compress(input, 10);
controller.enqueue(result.buf);
resultCode = result.code;
inputOffset += result.input_offset;
} while (resultCode === brotli.BrotliStreamResultCode.NeedsMoreOutput);
if (resultCode !== brotli.BrotliStreamResultCode.NeedsMoreInput) {
controller.error(`Brotli compression failed when transforming with code ${resultCode}`);
}
},
flush(controller) {
let resultCode;
do {
const result = compressStream.compress(undefined, 10);
controller.enqueue(result.buf);
resultCode = result.code;
} while (resultCode === brotli.BrotliStreamResultCode.NeedsMoreOutput)
if (resultCode !== brotli.BrotliStreamResultCode.ResultSuccess) {
controller.error(`Brotli compression failed when flushing with code ${resultCode}`);
}
controller.terminate();
}
});
const decompressStream = new brotli.DecompressStream();
const decompressionStream = new TransformStream({
transform(chunk, controller) {
let resultCode;
let inputOffset = 0;
do {
const input = chunk.slice(inputOffset);
const result = decompressStream.decompress(input, 100);
controller.enqueue(result.buf);
resultCode = result.code;
inputOffset += result.input_offset;
} while (resultCode === brotli.BrotliStreamResultCode.NeedsMoreOutput);
if (
resultCode !== brotli.BrotliStreamResultCode.NeedsMoreInput &&
resultCode !== brotli.BrotliStreamResultCode.ResultSuccess
) {
controller.error(`Brotli decompression failed with code ${resultCode}`)
}
},
flush(controller) {
controller.terminate();
}
});
const textDecoderStream = new TextDecoderStream();
let output = '';
const outputStream = new WritableStream({
write(chunk) {
output += chunk;
}
});
await inputStream
.pipeThrough(textEncoderStream)
.pipeThrough(compressionStream)
.pipeThrough(decompressionStream)
.pipeThrough(textDecoderStream)
.pipeTo(outputStream);
expect(output).to.equal(input);
});
});
function generateRandomBytes(size: number) {
const resultArray = new Uint8Array(size);
let generatedSize = 0;
// We can only generate 65535 bytes at a time, so we loop up to size:
while (generatedSize < size) {
const sizeToGenerate = Math.min(size - generatedSize, 65535);
const stepResultArray = new Uint8Array(sizeToGenerate);
getRandomValues(stepResultArray);
resultArray.set(stepResultArray, generatedSize);
generatedSize += sizeToGenerate;
}
return resultArray;
}