-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_zlib_promises.js
More file actions
47 lines (37 loc) · 1.49 KB
/
test_zlib_promises.js
File metadata and controls
47 lines (37 loc) · 1.49 KB
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
import zlib from 'node:zlib';
import { promisify } from 'node:util';
const deflate = promisify(zlib.deflate);
const inflate = promisify(zlib.inflate);
const gzip = promisify(zlib.gzip);
const gunzip = promisify(zlib.gunzip);
const deflateRaw = promisify(zlib.deflateRaw);
const inflateRaw = promisify(zlib.inflateRaw);
const unzip = promisify(zlib.unzip);
const input = new Uint8Array(2000);
for (let i = 0; i < input.length; i++) {
input[i] = (i % 256);
}
console.log('--- Zlib Promisify Test ---');
const checkPass = (a, b) => {
if (a.length !== b.length) return false;
for (let i = 0; i < a.length; i++) if (a[i] !== b[i]) return false;
return true;
};
async function test() {
try {
const deflated = await deflate(input);
const inflated = await inflate(deflated);
console.log('Deflate/Inflate:', checkPass(inflated, input) ? 'PASS' : 'FAIL');
const gzipped = await gzip(input);
const gunzipped = await gunzip(gzipped);
console.log('Gzip/Gunzip:', checkPass(gunzipped, input) ? 'PASS' : 'FAIL');
const raw = await deflateRaw(input);
const rawInflated = await inflateRaw(raw);
console.log('DeflateRaw/InflateRaw:', checkPass(rawInflated, input) ? 'PASS' : 'FAIL');
const unzipped = await unzip(gzipped);
console.log('Unzip:', checkPass(unzipped, input) ? 'PASS' : 'FAIL');
} catch (e) {
console.error('Test failed with error:', e);
}
}
test();