Skip to content

Commit 5f0b594

Browse files
authored
Merge pull request #26 from DawidMyslak/fix/zlib-constants-fallbacks
fix: add explicit zlib constant fallbacks for esbuild-stripped values
2 parents 5b0201d + 63b5344 commit 5f0b594

3 files changed

Lines changed: 94 additions & 3 deletions

File tree

packages/core/isolate-runtime/src/inject/require-setup.ts

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -300,15 +300,43 @@
300300
// constants object. Node.js zlib.constants bundles all Z_ values plus
301301
// DEFLATE (1), INFLATE (2), GZIP (3), DEFLATERAW (4), INFLATERAW (5),
302302
// UNZIP (6), GUNZIP (7). Packages like ssh2 destructure constants.
303+
var zlibConstants = typeof result.constants === 'object' && result.constants !== null
304+
? result.constants
305+
: {};
303306
if (typeof result.constants !== 'object' || result.constants === null) {
304-
var zlibConstants = {};
305307
var constKeys = Object.keys(result);
306308
for (var ci = 0; ci < constKeys.length; ci++) {
307309
var ck = constKeys[ci];
308310
if (ck.indexOf('Z_') === 0 && typeof result[ck] === 'number') {
309311
zlibConstants[ck] = result[ck];
310312
}
311313
}
314+
// Add Z_* constants that esbuild may strip from the browserify-zlib bundle.
315+
if (typeof zlibConstants.Z_NO_FLUSH !== 'number') zlibConstants.Z_NO_FLUSH = 0;
316+
if (typeof zlibConstants.Z_PARTIAL_FLUSH !== 'number') zlibConstants.Z_PARTIAL_FLUSH = 1;
317+
if (typeof zlibConstants.Z_SYNC_FLUSH !== 'number') zlibConstants.Z_SYNC_FLUSH = 2;
318+
if (typeof zlibConstants.Z_FULL_FLUSH !== 'number') zlibConstants.Z_FULL_FLUSH = 3;
319+
if (typeof zlibConstants.Z_FINISH !== 'number') zlibConstants.Z_FINISH = 4;
320+
if (typeof zlibConstants.Z_BLOCK !== 'number') zlibConstants.Z_BLOCK = 5;
321+
if (typeof zlibConstants.Z_TREES !== 'number') zlibConstants.Z_TREES = 6;
322+
if (typeof zlibConstants.Z_OK !== 'number') zlibConstants.Z_OK = 0;
323+
if (typeof zlibConstants.Z_STREAM_END !== 'number') zlibConstants.Z_STREAM_END = 1;
324+
if (typeof zlibConstants.Z_NEED_DICT !== 'number') zlibConstants.Z_NEED_DICT = 2;
325+
if (typeof zlibConstants.Z_ERRNO !== 'number') zlibConstants.Z_ERRNO = -1;
326+
if (typeof zlibConstants.Z_STREAM_ERROR !== 'number') zlibConstants.Z_STREAM_ERROR = -2;
327+
if (typeof zlibConstants.Z_DATA_ERROR !== 'number') zlibConstants.Z_DATA_ERROR = -3;
328+
if (typeof zlibConstants.Z_MEM_ERROR !== 'number') zlibConstants.Z_MEM_ERROR = -4;
329+
if (typeof zlibConstants.Z_BUF_ERROR !== 'number') zlibConstants.Z_BUF_ERROR = -5;
330+
if (typeof zlibConstants.Z_VERSION_ERROR !== 'number') zlibConstants.Z_VERSION_ERROR = -6;
331+
if (typeof zlibConstants.Z_NO_COMPRESSION !== 'number') zlibConstants.Z_NO_COMPRESSION = 0;
332+
if (typeof zlibConstants.Z_BEST_SPEED !== 'number') zlibConstants.Z_BEST_SPEED = 1;
333+
if (typeof zlibConstants.Z_BEST_COMPRESSION !== 'number') zlibConstants.Z_BEST_COMPRESSION = 9;
334+
if (typeof zlibConstants.Z_DEFAULT_COMPRESSION !== 'number') zlibConstants.Z_DEFAULT_COMPRESSION = -1;
335+
if (typeof zlibConstants.Z_FILTERED !== 'number') zlibConstants.Z_FILTERED = 1;
336+
if (typeof zlibConstants.Z_HUFFMAN_ONLY !== 'number') zlibConstants.Z_HUFFMAN_ONLY = 2;
337+
if (typeof zlibConstants.Z_RLE !== 'number') zlibConstants.Z_RLE = 3;
338+
if (typeof zlibConstants.Z_FIXED !== 'number') zlibConstants.Z_FIXED = 4;
339+
if (typeof zlibConstants.Z_DEFAULT_STRATEGY !== 'number') zlibConstants.Z_DEFAULT_STRATEGY = 0;
312340
// Add mode constants that Node.js exposes but browserify-zlib does not.
313341
if (typeof zlibConstants.DEFLATE !== 'number') zlibConstants.DEFLATE = 1;
314342
if (typeof zlibConstants.INFLATE !== 'number') zlibConstants.INFLATE = 2;
@@ -317,8 +345,8 @@
317345
if (typeof zlibConstants.INFLATERAW !== 'number') zlibConstants.INFLATERAW = 5;
318346
if (typeof zlibConstants.UNZIP !== 'number') zlibConstants.UNZIP = 6;
319347
if (typeof zlibConstants.GUNZIP !== 'number') zlibConstants.GUNZIP = 7;
320-
result.constants = zlibConstants;
321348
}
349+
result.constants = zlibConstants;
322350
return result;
323351
}
324352

packages/core/src/generated/isolate-runtime.ts

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

packages/secure-exec/tests/test-suite/node/polyfills.ts

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,69 @@ export function runNodePolyfillSuite(context: NodeSuiteContext): void {
2626
});
2727
});
2828

29+
it("zlib.constants has flush, return-code, level, and strategy constants", async () => {
30+
const runtime = await context.createRuntime();
31+
const result = await runtime.run(`
32+
const zlib = require('zlib');
33+
const c = zlib.constants;
34+
module.exports = {
35+
Z_NO_FLUSH: c.Z_NO_FLUSH,
36+
Z_PARTIAL_FLUSH: c.Z_PARTIAL_FLUSH,
37+
Z_SYNC_FLUSH: c.Z_SYNC_FLUSH,
38+
Z_FULL_FLUSH: c.Z_FULL_FLUSH,
39+
Z_FINISH: c.Z_FINISH,
40+
Z_BLOCK: c.Z_BLOCK,
41+
Z_TREES: c.Z_TREES,
42+
Z_OK: c.Z_OK,
43+
Z_STREAM_END: c.Z_STREAM_END,
44+
Z_NEED_DICT: c.Z_NEED_DICT,
45+
Z_ERRNO: c.Z_ERRNO,
46+
Z_STREAM_ERROR: c.Z_STREAM_ERROR,
47+
Z_DATA_ERROR: c.Z_DATA_ERROR,
48+
Z_MEM_ERROR: c.Z_MEM_ERROR,
49+
Z_BUF_ERROR: c.Z_BUF_ERROR,
50+
Z_VERSION_ERROR: c.Z_VERSION_ERROR,
51+
Z_NO_COMPRESSION: c.Z_NO_COMPRESSION,
52+
Z_BEST_SPEED: c.Z_BEST_SPEED,
53+
Z_BEST_COMPRESSION: c.Z_BEST_COMPRESSION,
54+
Z_DEFAULT_COMPRESSION: c.Z_DEFAULT_COMPRESSION,
55+
Z_FILTERED: c.Z_FILTERED,
56+
Z_HUFFMAN_ONLY: c.Z_HUFFMAN_ONLY,
57+
Z_RLE: c.Z_RLE,
58+
Z_FIXED: c.Z_FIXED,
59+
Z_DEFAULT_STRATEGY: c.Z_DEFAULT_STRATEGY,
60+
};
61+
`);
62+
expect(result.code).toBe(0);
63+
expect(result.exports).toEqual({
64+
Z_NO_FLUSH: 0,
65+
Z_PARTIAL_FLUSH: 1,
66+
Z_SYNC_FLUSH: 2,
67+
Z_FULL_FLUSH: 3,
68+
Z_FINISH: 4,
69+
Z_BLOCK: 5,
70+
Z_TREES: 6,
71+
Z_OK: 0,
72+
Z_STREAM_END: 1,
73+
Z_NEED_DICT: 2,
74+
Z_ERRNO: -1,
75+
Z_STREAM_ERROR: -2,
76+
Z_DATA_ERROR: -3,
77+
Z_MEM_ERROR: -4,
78+
Z_BUF_ERROR: -5,
79+
Z_VERSION_ERROR: -6,
80+
Z_NO_COMPRESSION: 0,
81+
Z_BEST_SPEED: 1,
82+
Z_BEST_COMPRESSION: 9,
83+
Z_DEFAULT_COMPRESSION: -1,
84+
Z_FILTERED: 1,
85+
Z_HUFFMAN_ONLY: 2,
86+
Z_RLE: 3,
87+
Z_FIXED: 4,
88+
Z_DEFAULT_STRATEGY: 0,
89+
});
90+
});
91+
2992
it("zlib.constants has mode constants (DEFLATE=1..GUNZIP=7)", async () => {
3093
const runtime = await context.createRuntime();
3194
const result = await runtime.run(`

0 commit comments

Comments
 (0)