Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose zlib constants directly under zlib module #2823

Merged
merged 1 commit into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
223 changes: 223 additions & 0 deletions src/node/zlib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,119 @@ import { constants, codes } from 'node-internal:internal_zlib_constants';
import { default as compatFlags } from 'workerd:compatibility-flags';

const { nodeJsZlib } = compatFlags;
const {
Z_NO_FLUSH,
Z_PARTIAL_FLUSH,
Z_SYNC_FLUSH,
Z_FULL_FLUSH,
Z_FINISH,
Z_BLOCK,

Z_OK,
Z_STREAM_END,
Z_NEED_DICT,
Z_ERRNO,
Z_STREAM_ERROR,
Z_DATA_ERROR,
Z_MEM_ERROR,
Z_BUF_ERROR,
Z_VERSION_ERROR,

Z_NO_COMPRESSION,
Z_BEST_SPEED,
Z_BEST_COMPRESSION,
Z_DEFAULT_COMPRESSION,
Z_FILTERED,
Z_HUFFMAN_ONLY,
Z_RLE,
Z_FIXED,
Z_DEFAULT_STRATEGY,
ZLIB_VERNUM,

DEFLATE,
INFLATE,
GZIP,
GUNZIP,
DEFLATERAW,
INFLATERAW,
UNZIP,
BROTLI_DECODE,
BROTLI_ENCODE,

Z_MIN_WINDOWBITS,
Z_MAX_WINDOWBITS,
Z_DEFAULT_WINDOWBITS,
Z_MIN_CHUNK,
Z_MAX_CHUNK,
Z_DEFAULT_CHUNK,
Z_MIN_MEMLEVEL,
Z_MAX_MEMLEVEL,
Z_DEFAULT_MEMLEVEL,
Z_MIN_LEVEL,
Z_MAX_LEVEL,
Z_DEFAULT_LEVEL,

BROTLI_OPERATION_PROCESS,
BROTLI_OPERATION_FLUSH,
BROTLI_OPERATION_FINISH,
BROTLI_OPERATION_EMIT_METADATA,
BROTLI_PARAM_MODE,
BROTLI_MODE_GENERIC,
BROTLI_MODE_TEXT,
BROTLI_MODE_FONT,
BROTLI_DEFAULT_MODE,
BROTLI_PARAM_QUALITY,
BROTLI_MIN_QUALITY,
BROTLI_MAX_QUALITY,
BROTLI_DEFAULT_QUALITY,
BROTLI_PARAM_LGWIN,
BROTLI_MIN_WINDOW_BITS,
BROTLI_MAX_WINDOW_BITS,
BROTLI_LARGE_MAX_WINDOW_BITS,
BROTLI_DEFAULT_WINDOW,
BROTLI_PARAM_LGBLOCK,
BROTLI_MIN_INPUT_BLOCK_BITS,
BROTLI_MAX_INPUT_BLOCK_BITS,
BROTLI_PARAM_DISABLE_LITERAL_CONTEXT_MODELING,
BROTLI_PARAM_LARGE_WINDOW,
BROTLI_PARAM_NPOSTFIX,
BROTLI_PARAM_NDIRECT,
BROTLI_DECODER_RESULT_ERROR,
BROTLI_DECODER_RESULT_SUCCESS,
BROTLI_DECODER_RESULT_NEEDS_MORE_INPUT,
BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT,
BROTLI_DECODER_PARAM_DISABLE_RING_BUFFER_REALLOCATION,
BROTLI_DECODER_PARAM_LARGE_WINDOW,
BROTLI_DECODER_NO_ERROR,
BROTLI_DECODER_SUCCESS,
BROTLI_DECODER_NEEDS_MORE_INPUT,
BROTLI_DECODER_NEEDS_MORE_OUTPUT,
BROTLI_DECODER_ERROR_FORMAT_EXUBERANT_NIBBLE,
BROTLI_DECODER_ERROR_FORMAT_RESERVED,
BROTLI_DECODER_ERROR_FORMAT_EXUBERANT_META_NIBBLE,
BROTLI_DECODER_ERROR_FORMAT_SIMPLE_HUFFMAN_ALPHABET,
BROTLI_DECODER_ERROR_FORMAT_SIMPLE_HUFFMAN_SAME,
BROTLI_DECODER_ERROR_FORMAT_CL_SPACE,
BROTLI_DECODER_ERROR_FORMAT_HUFFMAN_SPACE,
BROTLI_DECODER_ERROR_FORMAT_CONTEXT_MAP_REPEAT,
BROTLI_DECODER_ERROR_FORMAT_BLOCK_LENGTH_1,
BROTLI_DECODER_ERROR_FORMAT_BLOCK_LENGTH_2,
BROTLI_DECODER_ERROR_FORMAT_TRANSFORM,
BROTLI_DECODER_ERROR_FORMAT_DICTIONARY,
BROTLI_DECODER_ERROR_FORMAT_WINDOW_BITS,
BROTLI_DECODER_ERROR_FORMAT_PADDING_1,
BROTLI_DECODER_ERROR_FORMAT_PADDING_2,
BROTLI_DECODER_ERROR_FORMAT_DISTANCE,
BROTLI_DECODER_ERROR_DICTIONARY_NOT_SET,
BROTLI_DECODER_ERROR_INVALID_ARGUMENTS,
BROTLI_DECODER_ERROR_ALLOC_CONTEXT_MODES,
BROTLI_DECODER_ERROR_ALLOC_TREE_GROUPS,
BROTLI_DECODER_ERROR_ALLOC_CONTEXT_MAP,
BROTLI_DECODER_ERROR_ALLOC_RING_BUFFER_1,
BROTLI_DECODER_ERROR_ALLOC_RING_BUFFER_2,
BROTLI_DECODER_ERROR_ALLOC_BLOCK_TYPE_TREES,
BROTLI_DECODER_ERROR_UNREACHABLE,
} = constants;

function protectMethod(method: unknown): unknown {
if (!nodeJsZlib) {
Expand Down Expand Up @@ -100,12 +213,122 @@ export {
brotliDecompressSync,
brotliCompress,
brotliCompressSync,

// NodeJS also exports all constants directly under zlib, but this is deprecated
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jasnell afaik, there isn't any other alternative way to expose this, but any idea?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well... you could move all of the constants here into a separate internal/zlib-constants import as named exports only... then do

import * as zlibConstants from 'node-internal:zlib-constants';

export const constants = { ...zlibConstants };
export * from 'node-internal:zlib-constants';

But might not be worth the extra hassle. I'd just go with this for now.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JavaScript is a weird language.

Z_NO_FLUSH,
Z_PARTIAL_FLUSH,
Z_SYNC_FLUSH,
Z_FULL_FLUSH,
Z_FINISH,
Z_BLOCK,
Z_OK,
Z_STREAM_END,
Z_NEED_DICT,
Z_ERRNO,
Z_STREAM_ERROR,
Z_DATA_ERROR,
Z_MEM_ERROR,
Z_BUF_ERROR,
Z_VERSION_ERROR,
Z_NO_COMPRESSION,
Z_BEST_SPEED,
Z_BEST_COMPRESSION,
Z_DEFAULT_COMPRESSION,
Z_FILTERED,
Z_HUFFMAN_ONLY,
Z_RLE,
Z_FIXED,
Z_DEFAULT_STRATEGY,
ZLIB_VERNUM,
DEFLATE,
INFLATE,
GZIP,
GUNZIP,
DEFLATERAW,
INFLATERAW,
UNZIP,
BROTLI_DECODE,
BROTLI_ENCODE,
Z_MIN_WINDOWBITS,
Z_MAX_WINDOWBITS,
Z_DEFAULT_WINDOWBITS,
Z_MIN_CHUNK,
Z_MAX_CHUNK,
Z_DEFAULT_CHUNK,
Z_MIN_MEMLEVEL,
Z_MAX_MEMLEVEL,
Z_DEFAULT_MEMLEVEL,
Z_MIN_LEVEL,
Z_MAX_LEVEL,
Z_DEFAULT_LEVEL,
BROTLI_OPERATION_PROCESS,
BROTLI_OPERATION_FLUSH,
BROTLI_OPERATION_FINISH,
BROTLI_OPERATION_EMIT_METADATA,
BROTLI_PARAM_MODE,
BROTLI_MODE_GENERIC,
BROTLI_MODE_TEXT,
BROTLI_MODE_FONT,
BROTLI_DEFAULT_MODE,
BROTLI_PARAM_QUALITY,
BROTLI_MIN_QUALITY,
BROTLI_MAX_QUALITY,
BROTLI_DEFAULT_QUALITY,
BROTLI_PARAM_LGWIN,
BROTLI_MIN_WINDOW_BITS,
BROTLI_MAX_WINDOW_BITS,
BROTLI_LARGE_MAX_WINDOW_BITS,
BROTLI_DEFAULT_WINDOW,
BROTLI_PARAM_LGBLOCK,
BROTLI_MIN_INPUT_BLOCK_BITS,
BROTLI_MAX_INPUT_BLOCK_BITS,
BROTLI_PARAM_DISABLE_LITERAL_CONTEXT_MODELING,
BROTLI_PARAM_LARGE_WINDOW,
BROTLI_PARAM_NPOSTFIX,
BROTLI_PARAM_NDIRECT,
BROTLI_DECODER_RESULT_ERROR,
BROTLI_DECODER_RESULT_SUCCESS,
BROTLI_DECODER_RESULT_NEEDS_MORE_INPUT,
BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT,
BROTLI_DECODER_PARAM_DISABLE_RING_BUFFER_REALLOCATION,
BROTLI_DECODER_PARAM_LARGE_WINDOW,
BROTLI_DECODER_NO_ERROR,
BROTLI_DECODER_SUCCESS,
BROTLI_DECODER_NEEDS_MORE_INPUT,
BROTLI_DECODER_NEEDS_MORE_OUTPUT,
BROTLI_DECODER_ERROR_FORMAT_EXUBERANT_NIBBLE,
BROTLI_DECODER_ERROR_FORMAT_RESERVED,
BROTLI_DECODER_ERROR_FORMAT_EXUBERANT_META_NIBBLE,
BROTLI_DECODER_ERROR_FORMAT_SIMPLE_HUFFMAN_ALPHABET,
BROTLI_DECODER_ERROR_FORMAT_SIMPLE_HUFFMAN_SAME,
BROTLI_DECODER_ERROR_FORMAT_CL_SPACE,
BROTLI_DECODER_ERROR_FORMAT_HUFFMAN_SPACE,
BROTLI_DECODER_ERROR_FORMAT_CONTEXT_MAP_REPEAT,
BROTLI_DECODER_ERROR_FORMAT_BLOCK_LENGTH_1,
BROTLI_DECODER_ERROR_FORMAT_BLOCK_LENGTH_2,
BROTLI_DECODER_ERROR_FORMAT_TRANSFORM,
BROTLI_DECODER_ERROR_FORMAT_DICTIONARY,
BROTLI_DECODER_ERROR_FORMAT_WINDOW_BITS,
BROTLI_DECODER_ERROR_FORMAT_PADDING_1,
BROTLI_DECODER_ERROR_FORMAT_PADDING_2,
BROTLI_DECODER_ERROR_FORMAT_DISTANCE,
BROTLI_DECODER_ERROR_DICTIONARY_NOT_SET,
BROTLI_DECODER_ERROR_INVALID_ARGUMENTS,
BROTLI_DECODER_ERROR_ALLOC_CONTEXT_MODES,
BROTLI_DECODER_ERROR_ALLOC_TREE_GROUPS,
BROTLI_DECODER_ERROR_ALLOC_CONTEXT_MAP,
BROTLI_DECODER_ERROR_ALLOC_RING_BUFFER_1,
BROTLI_DECODER_ERROR_ALLOC_RING_BUFFER_2,
BROTLI_DECODER_ERROR_ALLOC_BLOCK_TYPE_TREES,
BROTLI_DECODER_ERROR_UNREACHABLE,
};

export default {
crc32,
codes,
constants,
// NodeJS also exports all constants directly under zlib, but this is deprecated
...constants,

// Classes
Gzip,
Expand Down
9 changes: 9 additions & 0 deletions src/workerd/api/node/tests/zlib-nodejs-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,15 @@ export const constantsTest = {
},
};

// This test makes sure constants can be accessed from the zlib module directly
// This is supported by NodeJS, but is deprecated.
export const constantsDirectlyUnderZlibTest = {
test() {
assert.deepStrictEqual(zlib.Z_NEED_DICT, 2);
assert.deepStrictEqual(zlib.Z_OK, 0);
},
};

// Tests are taken from:
// https://github.com/nodejs/node/blob/561bc87c7607208f0d3db6dcd9231efeb48cfe2f/test/parallel/test-zlib-zero-windowBits.js
export const testZeroWindowBits = {
Expand Down