MIRROR: javascript for 馃悳's, a tiny runtime with big ambitions
1import { test, testThrows, summary } from './helpers.js';
2import zlib from 'node:zlib';
3
4console.log('node:zlib Tests\n');
5
6test('zlib is object', typeof zlib, 'object');
7test('zlib toStringTag', Object.prototype.toString.call(zlib), '[object zlib]');
8
9test('constants exists', typeof zlib.constants, 'object');
10test('Z_OK is 0', zlib.constants.Z_OK, 0);
11test('Z_NO_FLUSH is 0', zlib.constants.Z_NO_FLUSH, 0);
12test('Z_FINISH is 4', zlib.constants.Z_FINISH, 4);
13test('Z_DEFAULT_COMPRESSION is -1', zlib.constants.Z_DEFAULT_COMPRESSION, -1);
14test('Z_BEST_SPEED is 1', zlib.constants.Z_BEST_SPEED, 1);
15test('Z_BEST_COMPRESSION is 9', zlib.constants.Z_BEST_COMPRESSION, 9);
16
17test('createGzip is function', typeof zlib.createGzip, 'function');
18test('createGunzip is function', typeof zlib.createGunzip, 'function');
19test('createDeflate is function', typeof zlib.createDeflate, 'function');
20test('createInflate is function', typeof zlib.createInflate, 'function');
21test('createDeflateRaw is function', typeof zlib.createDeflateRaw, 'function');
22test('createInflateRaw is function', typeof zlib.createInflateRaw, 'function');
23test('createUnzip is function', typeof zlib.createUnzip, 'function');
24test('createBrotliCompress is function', typeof zlib.createBrotliCompress, 'function');
25test('createBrotliDecompress is function', typeof zlib.createBrotliDecompress, 'function');
26
27test('gzipSync is function', typeof zlib.gzipSync, 'function');
28test('gunzipSync is function', typeof zlib.gunzipSync, 'function');
29test('deflateSync is function', typeof zlib.deflateSync, 'function');
30test('inflateSync is function', typeof zlib.inflateSync, 'function');
31test('deflateRawSync is function', typeof zlib.deflateRawSync, 'function');
32test('inflateRawSync is function', typeof zlib.inflateRawSync, 'function');
33test('unzipSync is function', typeof zlib.unzipSync, 'function');
34test('brotliCompressSync is function', typeof zlib.brotliCompressSync, 'function');
35test('brotliDecompressSync is function', typeof zlib.brotliDecompressSync, 'function');
36
37test('gzip is function', typeof zlib.gzip, 'function');
38test('gunzip is function', typeof zlib.gunzip, 'function');
39test('deflate is function', typeof zlib.deflate, 'function');
40test('inflate is function', typeof zlib.inflate, 'function');
41test('crc32 is function', typeof zlib.crc32, 'function');
42
43const gz = zlib.createGzip();
44test('createGzip returns object', typeof gz, 'object');
45test('createGzip has write', typeof gz.write, 'function');
46test('createGzip has end', typeof gz.end, 'function');
47test('createGzip has on', typeof gz.on, 'function');
48test('createGzip has pipe', typeof gz.pipe, 'function');
49test('createGzip has destroy', typeof gz.destroy, 'function');
50test('createGzip readable=true', gz.readable, true);
51test('createGzip writable=true', gz.writable, true);
52test('createGzip bytesWritten starts 0', gz.bytesWritten, 0);
53
54const gun = zlib.createGunzip();
55test('createGunzip has write', typeof gun.write, 'function');
56test('createGunzip readable=true', gun.readable, true);
57
58const inf = zlib.createInflate();
59test('createInflate has write', typeof inf.write, 'function');
60
61const br = zlib.createBrotliDecompress();
62test('createBrotliDecompress returns object', typeof br, 'object');
63test('createBrotliDecompress has write', typeof br.write, 'function');
64
65const input = Buffer.from('Hello, zlib world!');
66
67const gzipped = zlib.gzipSync(input);
68test('gzipSync returns Buffer', gzipped instanceof Uint8Array, true);
69test('gzipSync has gzip magic bytes', gzipped[0], 0x1f);
70test('gzipSync magic byte 2', gzipped[1], 0x8b);
71test('gzipSync result is smaller for repetitive input', gzipped.length > 0, true);
72
73const gunzipped = zlib.gunzipSync(gzipped);
74test('gunzipSync roundtrip length', gunzipped.length, input.length);
75test('gunzipSync roundtrip value', Buffer.from(gunzipped).toString(), 'Hello, zlib world!');
76
77const deflated = zlib.deflateSync(input);
78test('deflateSync returns Buffer', deflated instanceof Uint8Array, true);
79test('deflateSync result non-empty', deflated.length > 0, true);
80
81const inflated = zlib.inflateSync(deflated);
82test('inflateSync roundtrip', Buffer.from(inflated).toString(), 'Hello, zlib world!');
83
84const deflatedRaw = zlib.deflateRawSync(input);
85test('deflateRawSync result non-empty', deflatedRaw.length > 0, true);
86
87const inflatedRaw = zlib.inflateRawSync(deflatedRaw);
88test('inflateRawSync roundtrip', Buffer.from(inflatedRaw).toString(), 'Hello, zlib world!');
89
90const brotliCompressed = zlib.brotliCompressSync(input);
91test('brotliCompressSync result non-empty', brotliCompressed.length > 0, true);
92
93const brotliDecompressed = zlib.brotliDecompressSync(brotliCompressed);
94test('brotliDecompressSync roundtrip', Buffer.from(brotliDecompressed).toString(), 'Hello, zlib world!');
95
96const unzipped = zlib.unzipSync(gzipped);
97test('unzipSync gzip roundtrip', Buffer.from(unzipped).toString(), 'Hello, zlib world!');
98
99const fromString = zlib.gzipSync('hello string');
100test('gzipSync accepts string', fromString instanceof Uint8Array, true);
101const fromStringBack = zlib.gunzipSync(fromString);
102test('gunzipSync string roundtrip', Buffer.from(fromStringBack).toString(), 'hello string');
103
104const repetitive = Buffer.from('a'.repeat(1000));
105const compressedRep = zlib.gzipSync(repetitive);
106test('gzip compresses repetitive data', compressedRep.length < repetitive.length, true);
107
108const crc0 = zlib.crc32('hello');
109test('crc32 returns number', typeof crc0, 'number');
110test('crc32 hello is consistent', crc0, zlib.crc32('hello'));
111test('crc32 chaining', zlib.crc32('world', zlib.crc32('hello')) !== zlib.crc32('hello'), true);
112test('crc32 different inputs differ', zlib.crc32('hello') !== zlib.crc32('world'), true);
113
114function testAsyncGzip() {
115 return new Promise((resolve) => {
116 zlib.gzip(input, (err, result) => {
117 test('async gzip no error', err, null);
118 test('async gzip returns Buffer', result instanceof Uint8Array, true);
119 test('async gzip magic byte', result[0], 0x1f);
120
121 zlib.gunzip(result, (err2, result2) => {
122 test('async gunzip no error', err2, null);
123 test('async gunzip roundtrip', Buffer.from(result2).toString(), 'Hello, zlib world!');
124 resolve();
125 });
126 });
127 });
128}
129
130function testAsyncDeflate() {
131 return new Promise((resolve) => {
132 zlib.deflate(input, (err, result) => {
133 test('async deflate no error', err, null);
134 test('async deflate result non-empty', result.length > 0, true);
135
136 zlib.inflate(result, (err2, result2) => {
137 test('async inflate no error', err2, null);
138 test('async inflate roundtrip', Buffer.from(result2).toString(), 'Hello, zlib world!');
139 resolve();
140 });
141 });
142 });
143}
144
145function testAsyncBrotli() {
146 return new Promise((resolve) => {
147 zlib.brotliCompress(input, (err, result) => {
148 test('async brotliCompress no error', err, null);
149 test('async brotliCompress result non-empty', result.length > 0, true);
150
151 zlib.brotliDecompress(result, (err2, result2) => {
152 test('async brotliDecompress no error', err2, null);
153 test('async brotliDecompress roundtrip', Buffer.from(result2).toString(), 'Hello, zlib world!');
154 resolve();
155 });
156 });
157 });
158}
159
160function testTransformStream() {
161 return new Promise((resolve) => {
162 const chunks = [];
163 const stream = zlib.createGunzip();
164
165 stream.on('data', (chunk) => chunks.push(chunk));
166 stream.on('end', () => {
167 let total = 0;
168 for (const c of chunks) total += c.length;
169 const out = new Uint8Array(total);
170 let off = 0;
171 for (const c of chunks) { out.set(c, off); off += c.length; }
172 test('transform stream roundtrip', Buffer.from(out).toString(), 'Hello, zlib world!');
173 resolve();
174 });
175
176 const compressed = zlib.gzipSync(input);
177 stream.write(compressed);
178 stream.end();
179 });
180}
181
182function testBytesWritten() {
183 const stream = zlib.createDeflate();
184 const data = Buffer.from('track bytes written');
185 stream.write(data);
186 test('bytesWritten after write', stream.bytesWritten, data.length);
187}
188
189testBytesWritten();
190
191await testAsyncGzip();
192await testAsyncDeflate();
193await testAsyncBrotli();
194await testTransformStream();
195
196summary();