From ee62eb82803e700e2cf14c1299bfdcece8623e0a Mon Sep 17 00:00:00 2001 From: Yagiz Nizipli Date: Mon, 26 Aug 2024 19:44:02 -0400 Subject: [PATCH] add zlib codes constants --- src/node/internal/internal_zlib.ts | 33 +++++++++++++++++++ src/node/zlib.ts | 4 ++- .../api/node/tests/zlib-nodejs-test.js | 22 ++++++++++++- 3 files changed, 57 insertions(+), 2 deletions(-) diff --git a/src/node/internal/internal_zlib.ts b/src/node/internal/internal_zlib.ts index 7c2836792ee3..1c3770f56aef 100644 --- a/src/node/internal/internal_zlib.ts +++ b/src/node/internal/internal_zlib.ts @@ -11,6 +11,15 @@ import { isArrayBufferView } from 'node-internal:internal_types'; import { Zlib } from 'node-internal:internal_zlib_base'; const { + CONST_Z_OK, + CONST_Z_STREAM_END, + CONST_Z_NEED_DICT, + CONST_Z_ERRNO, + CONST_Z_STREAM_ERROR, + CONST_Z_DATA_ERROR, + CONST_Z_MEM_ERROR, + CONST_Z_BUF_ERROR, + CONST_Z_VERSION_ERROR, CONST_DEFLATE, CONST_DEFLATERAW, CONST_INFLATE, @@ -41,6 +50,30 @@ Object.defineProperties( ) ); +// Translation table for return codes. +const rawCodes: Record = { + Z_OK: CONST_Z_OK, + Z_STREAM_END: CONST_Z_STREAM_END, + Z_NEED_DICT: CONST_Z_NEED_DICT, + Z_ERRNO: CONST_Z_ERRNO, + Z_STREAM_ERROR: CONST_Z_STREAM_ERROR, + Z_DATA_ERROR: CONST_Z_DATA_ERROR, + Z_MEM_ERROR: CONST_Z_MEM_ERROR, + Z_BUF_ERROR: CONST_Z_BUF_ERROR, + Z_VERSION_ERROR: CONST_Z_VERSION_ERROR, +}; + +for (const key of Object.keys(rawCodes)) { + rawCodes[rawCodes[key] as number] = key; +} + +export const codes = { + __proto__: null, + enumerable: true, + writable: false, + value: Object.freeze(rawCodes), +}; + export function crc32( data: ArrayBufferView | string, value: number = 0 diff --git a/src/node/zlib.ts b/src/node/zlib.ts index efecda82e4c8..23fea7caaaa6 100644 --- a/src/node/zlib.ts +++ b/src/node/zlib.ts @@ -1,5 +1,5 @@ import * as zlib from 'node-internal:internal_zlib'; -import { crc32, constants } from 'node-internal:internal_zlib'; +import { crc32, constants, codes } from 'node-internal:internal_zlib'; import { default as compatFlags } from 'workerd:compatibility-flags'; const { nodeJsZlib } = compatFlags; @@ -31,6 +31,7 @@ const createUnzip = protectMethod(zlib.createUnzip); export { crc32, + codes, constants, // Classes @@ -54,6 +55,7 @@ export { export default { crc32, + codes, constants, // Classes diff --git a/src/workerd/api/node/tests/zlib-nodejs-test.js b/src/workerd/api/node/tests/zlib-nodejs-test.js index fa17f2587c83..de60a24d5397 100644 --- a/src/workerd/api/node/tests/zlib-nodejs-test.js +++ b/src/workerd/api/node/tests/zlib-nodejs-test.js @@ -759,6 +759,26 @@ export const testZlibBytesRead = { }, }; +// Tests are taken from: +// https://github.com/nodejs/node/blob/3a71ccf6c473357e89be61b26739fd9139dce4db/test/parallel/test-zlib-const.js +export const zlibConst = { + test() { + strictEqual(zlib.constants.Z_OK, 0, 'Expected Z_OK to be 0'); + zlib.constants.Z_OK = 1; + strictEqual(zlib.constants.Z_OK, 0, 'Z_OK should be immutable'); + strictEqual( + zlib.codes.Z_OK, + 0, + `Expected Z_OK to be 0; got ${zlib.codes.Z_OK}` + ); + zlib.codes.Z_OK = 1; + strictEqual(zlib.codes.Z_OK, 0, 'Z_OK should be immutable'); + zlib.codes = { Z_OK: 1 }; + strictEqual(zlib.codes.Z_OK, 0, 'Z_OK should be immutable'); + assert(Object.isFrozen(zlib.codes), 'Expected zlib.codes to be frozen'); + }, +}; + // Node.js tests relevant to zlib // // - [ ] test-zlib-brotli-16GB.js @@ -810,7 +830,7 @@ export const testZlibBytesRead = { // - [ ] test-zlib-empty-buffer.js // - [ ] test-zlib-invalid-arg-value-brotli-compress.js // - [ ] test-zlib-random-byte-pipes.js -// - [ ] test-zlib-const.js +// - [x] test-zlib-const.js // - [x] test-zlib-failed-init.js // - [ ] test-zlib-invalid-input.js // - [ ] test-zlib-reset-before-write.js