From 6053f8fd31d6d8ce553fc0cf89189a4a8100a36b Mon Sep 17 00:00:00 2001 From: Yagiz Nizipli Date: Mon, 26 Aug 2024 19:44:02 -0400 Subject: [PATCH 1/3] add zlib codes constants --- src/node/internal/internal_zlib.ts | 28 ++++++++++++ src/node/zlib.ts | 4 +- .../api/node/tests/zlib-nodejs-test.js | 45 ++++++++++++++++++- 3 files changed, 75 insertions(+), 2 deletions(-) diff --git a/src/node/internal/internal_zlib.ts b/src/node/internal/internal_zlib.ts index 7c2836792ee..5cd56016e2e 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,25 @@ 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 = 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 efecda82e4c..23fea7caaaa 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 fa17f2587c8..d0dd9a60ab3 100644 --- a/src/workerd/api/node/tests/zlib-nodejs-test.js +++ b/src/workerd/api/node/tests/zlib-nodejs-test.js @@ -759,6 +759,49 @@ 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'); + throws(() => { + zlib.constants.Z_OK = 1; + }, /Cannot assign to read only property/); + 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}` + ); + throws(() => { + zlib.codes.Z_OK = 1; + }, /Cannot assign to read only property/); + strictEqual(zlib.codes.Z_OK, 0, 'Z_OK should be immutable'); + assert(Object.isFrozen(zlib.codes), 'Expected zlib.codes to be frozen'); + + deepStrictEqual(zlib.codes, { + '-1': 'Z_ERRNO', + '-2': 'Z_STREAM_ERROR', + '-3': 'Z_DATA_ERROR', + '-4': 'Z_MEM_ERROR', + '-5': 'Z_BUF_ERROR', + '-6': 'Z_VERSION_ERROR', + 0: 'Z_OK', + 1: 'Z_STREAM_END', + 2: 'Z_NEED_DICT', + Z_BUF_ERROR: -5, + Z_DATA_ERROR: -3, + Z_ERRNO: -1, + Z_MEM_ERROR: -4, + Z_NEED_DICT: 2, + Z_OK: 0, + Z_STREAM_END: 1, + Z_STREAM_ERROR: -2, + Z_VERSION_ERROR: -6, + }); + }, +}; + // Node.js tests relevant to zlib // // - [ ] test-zlib-brotli-16GB.js @@ -810,7 +853,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 From c3e804e1960758d68e64c885b74f677d3a332bd1 Mon Sep 17 00:00:00 2001 From: Yagiz Nizipli Date: Mon, 26 Aug 2024 20:17:11 -0400 Subject: [PATCH 2/3] add test-zlib-object-write.js test --- .../api/node/tests/zlib-nodejs-test.js | 25 ++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/src/workerd/api/node/tests/zlib-nodejs-test.js b/src/workerd/api/node/tests/zlib-nodejs-test.js index d0dd9a60ab3..f0fc6e6986d 100644 --- a/src/workerd/api/node/tests/zlib-nodejs-test.js +++ b/src/workerd/api/node/tests/zlib-nodejs-test.js @@ -802,6 +802,29 @@ export const zlibConst = { }, }; +// Tests are taken from: +// https://github.com/nodejs/node/blob/3a71ccf6c473357e89be61b26739fd9139dce4db/test/parallel/test-zlib-object-write.js + +export const zlibObjectWrite = { + async test() { + const { promise, resolve, reject } = Promise.withResolvers(); + const gunzip = new zlib.Gunzip({ objectMode: true }); + gunzip.on('error', reject); + assert.throws( + () => { + gunzip.write({}); + }, + { + name: 'TypeError', + code: 'ERR_INVALID_ARG_TYPE', + } + ); + gunzip.on('close', resolve); + gunzip.close(); + await promise; + }, +}; + // Node.js tests relevant to zlib // // - [ ] test-zlib-brotli-16GB.js @@ -837,7 +860,7 @@ export const zlibConst = { // - [x] test-zlib-bytes-read.js // - [ ] test-zlib-destroy-pipe.js // - [ ] test-zlib-from-gzip.js -// - [ ] test-zlib-object-write.js +// - [x] test-zlib-object-write.js // - [ ] test-zlib-write-after-flush.js // - [x] test-zlib-close-after-error.js // - [ ] test-zlib-dictionary-fail.js From 4ca348608160addca2e7343f1b7e05f8af672dfc Mon Sep 17 00:00:00 2001 From: Yagiz Nizipli Date: Mon, 26 Aug 2024 20:21:55 -0400 Subject: [PATCH 3/3] add test-zlib-zero-byte.js tests --- .../api/node/tests/zlib-nodejs-test.js | 37 ++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/src/workerd/api/node/tests/zlib-nodejs-test.js b/src/workerd/api/node/tests/zlib-nodejs-test.js index f0fc6e6986d..4d69171a868 100644 --- a/src/workerd/api/node/tests/zlib-nodejs-test.js +++ b/src/workerd/api/node/tests/zlib-nodejs-test.js @@ -825,6 +825,41 @@ export const zlibObjectWrite = { }, }; +// Tests are taken from: +// https://github.com/nodejs/node/blob/3a71ccf6c473357e89be61b26739fd9139dce4db/test/parallel/test-zlib-zero-byte.js +export const zlibZeroByte = { + async test() { + // TODO(soon): Add BrotliCompress once it is implemented + for (const Compressor of [zlib.Gzip]) { + const { promise, resolve, reject } = Promise.withResolvers(); + let endCalled = false; + const gz = new Compressor(); + const emptyBuffer = Buffer.alloc(0); + let received = 0; + gz.on('data', function (c) { + received += c.length; + }); + + gz.on('end', function () { + const expected = Compressor === zlib.Gzip ? 20 : 1; + assert.strictEqual( + received, + expected, + `${received}, ${expected}, ${Compressor.name}` + ); + endCalled = true; + }); + gz.on('error', reject); + gz.on('finish', resolve); + gz.write(emptyBuffer); + gz.end(); + + await promise; + assert(endCalled, 'End should have been called'); + } + }, +}; + // Node.js tests relevant to zlib // // - [ ] test-zlib-brotli-16GB.js @@ -866,7 +901,7 @@ export const zlibObjectWrite = { // - [ ] test-zlib-dictionary-fail.js // - [ ] test-zlib-from-gzip-with-trailing-garbage.js // - [ ] test-zlib-params.js -// - [ ] test-zlib-zero-byte.js +// - [x] test-zlib-zero-byte.js // - [ ] test-zlib-close-after-write.js // - [ ] test-zlib-dictionary.js // - [ ] test-zlib-from-string.js