Skip to content

Commit

Permalink
node:zlib implem: Expose zlib.constants
Browse files Browse the repository at this point in the history
  • Loading branch information
npaun committed Aug 8, 2024
1 parent 577d544 commit 6eb4389
Show file tree
Hide file tree
Showing 7 changed files with 433 additions and 9 deletions.
18 changes: 17 additions & 1 deletion src/node/internal/internal_zlib.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { default as zlibUtil } from 'node-internal:zlib';
import { Buffer } from 'node-internal:internal_buffer';
import { validateUint32 } from 'node-internal:validators';
import { validateUint32 } from 'node-internal:validators';
import { isArrayBufferView } from 'node-internal:internal_types';
import { ERR_INVALID_ARG_TYPE } from 'node-internal:internal_errors';

Expand All @@ -14,6 +14,22 @@ function crc32(data: ArrayBufferView | string, value: number = 0): number {
return zlibUtil.crc32(data, value);
}


const constPrefix = 'CONST_';
const constants = {};

// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
Object.defineProperties(constants, Object.fromEntries(Object.entries(Object.getPrototypeOf(zlibUtil))
.filter(([k,]) => k.startsWith(constPrefix))
.map(([k, v]) => [k.slice(constPrefix.length), {
value: v,
writable: false,
configurable: false,
enumerable: true
}])
));

export {
crc32,
constants,
}
114 changes: 114 additions & 0 deletions src/node/internal/zlib.d.ts
Original file line number Diff line number Diff line change
@@ -1 +1,115 @@
export function crc32(data: ArrayBufferView, value: number): number;

// zlib.constants (part of the API contract for node:zlib)
export const CONST_Z_NO_FLUSH: number;
export const CONST_Z_PARTIAL_FLUSH: number;
export const CONST_Z_SYNC_FLUSH: number;
export const CONST_Z_FULL_FLUSH: number;
export const CONST_Z_FINISH: number;
export const CONST_Z_BLOCK: number;

export const CONST_Z_OK: number;
export const CONST_Z_STREAM_END: number;
export const CONST_Z_NEED_DICT: number;
export const CONST_Z_ERRNO: number;
export const CONST_Z_STREAM_ERROR: number;
export const CONST_Z_DATA_ERROR: number;
export const CONST_Z_MEM_ERROR: number;
export const CONST_Z_BUF_ERROR: number;
export const CONST_Z_VERSION_ERROR: number;

export const CONST_Z_NO_COMPRESSION: number;
export const CONST_Z_BEST_SPEED: number;
export const CONST_Z_BEST_COMPRESSION: number;
export const CONST_Z_DEFAULT_COMPRESSION: number;
export const CONST_Z_FILTERED: number;
export const CONST_Z_HUFFMAN_ONLY: number;
export const CONST_Z_RLE: number;
export const CONST_Z_FIXED: number;
export const CONST_Z_DEFAULT_STRATEGY: number;
export const CONST_ZLIB_VERNUM: number;

export const CONST_DEFLATE: number;
export const CONST_INFLATE: number;
export const CONST_GZIP: number;
export const CONST_GUNZIP: number;
export const CONST_DEFLATERAW: number;
export const CONST_INFLATERAW: number;
export const CONST_UNZIP: number;
export const CONST_BROTLI_DECODE: number;
export const CONST_BROTLI_ENCODE: number;

export const CONST_Z_MIN_WINDOWBITS: number;
export const CONST_Z_MAX_WINDOWBITS: number;
export const CONST_Z_DEFAULT_WINDOWBITS: number;
export const CONST_Z_MIN_CHUNK: number;
export const CONST_Z_MAX_CHUNK: number;
export const CONST_Z_DEFAULT_CHUNK: number;
export const CONST_Z_MIN_MEMLEVEL: number;
export const CONST_Z_MAX_MEMLEVEL: number;
export const CONST_Z_DEFAULT_MEMLEVEL: number;
export const CONST_Z_MIN_LEVEL: number;
export const CONST_Z_MAX_LEVEL: number;
export const CONST_Z_DEFAULT_LEVEL: number;

export const CONST_BROTLI_OPERATION_PROCESS: number;
export const CONST_BROTLI_OPERATION_FLUSH: number;
export const CONST_BROTLI_OPERATION_FINISH: number;
export const CONST_BROTLI_OPERATION_EMIT_METADATA: number;
export const CONST_BROTLI_PARAM_MODE: number;
export const CONST_BROTLI_MODE_GENERIC: number;
export const CONST_BROTLI_MODE_TEXT: number;
export const CONST_BROTLI_MODE_FONT: number;
export const CONST_BROTLI_DEFAULT_MODE: number;
export const CONST_BROTLI_PARAM_QUALITY: number;
export const CONST_BROTLI_MIN_QUALITY: number;
export const CONST_BROTLI_MAX_QUALITY: number;
export const CONST_BROTLI_DEFAULT_QUALITY: number;
export const CONST_BROTLI_PARAM_LGWIN: number;
export const CONST_BROTLI_MIN_WINDOW_BITS: number;
export const CONST_BROTLI_MAX_WINDOW_BITS: number;
export const CONST_BROTLI_LARGE_MAX_WINDOW_BITS: number;
export const CONST_BROTLI_DEFAULT_WINDOW: number;
export const CONST_BROTLI_PARAM_LGBLOCK: number;
export const CONST_BROTLI_MIN_INPUT_BLOCK_BITS: number;
export const CONST_BROTLI_MAX_INPUT_BLOCK_BITS: number;
export const CONST_BROTLI_PARAM_DISABLE_LITERAL_CONTEXT_MODELING: number;
export const CONST_BROTLI_PARAM_SIZE_HINT: number;
export const CONST_BROTLI_PARAM_LARGE_WINDOW: number;
export const CONST_BROTLI_PARAM_NPOSTFIX: number;
export const CONST_BROTLI_PARAM_NDIRECT: number;
export const CONST_BROTLI_DECODER_RESULT_ERROR: number;
export const CONST_BROTLI_DECODER_RESULT_SUCCESS: number;
export const CONST_BROTLI_DECODER_RESULT_NEEDS_MORE_INPUT: number;
export const CONST_BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT: number;
export const CONST_BROTLI_DECODER_PARAM_DISABLE_RING_BUFFER_REALLOCATION: number;
export const CONST_BROTLI_DECODER_PARAM_LARGE_WINDOW: number;
export const CONST_BROTLI_DECODER_NO_ERROR: number;
export const CONST_BROTLI_DECODER_SUCCESS: number;
export const CONST_BROTLI_DECODER_NEEDS_MORE_INPUT: number;
export const CONST_BROTLI_DECODER_NEEDS_MORE_OUTPUT: number;
export const CONST_BROTLI_DECODER_ERROR_FORMAT_EXUBERANT_NIBBLE: number;
export const CONST_BROTLI_DECODER_ERROR_FORMAT_RESERVED: number;
export const CONST_BROTLI_DECODER_ERROR_FORMAT_EXUBERANT_META_NIBBLE: number;
export const CONST_BROTLI_DECODER_ERROR_FORMAT_SIMPLE_HUFFMAN_ALPHABET: number;
export const CONST_BROTLI_DECODER_ERROR_FORMAT_SIMPLE_HUFFMAN_SAME: number;
export const CONST_BROTLI_DECODER_ERROR_FORMAT_CL_SPACE: number;
export const CONST_BROTLI_DECODER_ERROR_FORMAT_HUFFMAN_SPACE: number;
export const CONST_BROTLI_DECODER_ERROR_FORMAT_CONTEXT_MAP_REPEAT: number;
export const CONST_BROTLI_DECODER_ERROR_FORMAT_BLOCK_LENGTH_1: number;
export const CONST_BROTLI_DECODER_ERROR_FORMAT_BLOCK_LENGTH_2: number;
export const CONST_BROTLI_DECODER_ERROR_FORMAT_TRANSFORM: number;
export const CONST_BROTLI_DECODER_ERROR_FORMAT_DICTIONARY: number;
export const CONST_BROTLI_DECODER_ERROR_FORMAT_WINDOW_BITS: number;
export const CONST_BROTLI_DECODER_ERROR_FORMAT_PADDING_1: number;
export const CONST_BROTLI_DECODER_ERROR_FORMAT_PADDING_2: number;
export const CONST_BROTLI_DECODER_ERROR_FORMAT_DISTANCE: number;
export const CONST_BROTLI_DECODER_ERROR_DICTIONARY_NOT_SET: number;
export const CONST_BROTLI_DECODER_ERROR_INVALID_ARGUMENTS: number;
export const CONST_BROTLI_DECODER_ERROR_ALLOC_CONTEXT_MODES: number;
export const CONST_BROTLI_DECODER_ERROR_ALLOC_TREE_GROUPS: number;
export const CONST_BROTLI_DECODER_ERROR_ALLOC_CONTEXT_MAP: number;
export const CONST_BROTLI_DECODER_ERROR_ALLOC_RING_BUFFER_1: number;
export const CONST_BROTLI_DECODER_ERROR_ALLOC_RING_BUFFER_2: number;
export const CONST_BROTLI_DECODER_ERROR_ALLOC_BLOCK_TYPE_TREES: number;
export const CONST_BROTLI_DECODER_ERROR_UNREACHABLE: number;
4 changes: 3 additions & 1 deletion src/node/zlib.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { crc32 } from 'node-internal:internal_zlib';
import { crc32, constants } from 'node-internal:internal_zlib';

export {
crc32,
constants,
}

export default {
crc32,
constants,
}
117 changes: 116 additions & 1 deletion src/workerd/api/node/tests/zlib-nodejs-test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import {
strictEqual,
throws,
deepStrictEqual
} from 'node:assert';
import { Buffer } from 'node:buffer';
import { crc32 } from 'node:zlib';
import { crc32, constants } from 'node:zlib';


// The following test data comes from
Expand Down Expand Up @@ -213,3 +214,117 @@ export const crc32Test = {
});
}
}

export const constantsTest = {
test() {
deepStrictEqual(JSON.stringify(constants, null, 2),`{
"Z_NO_FLUSH": 0,
"Z_PARTIAL_FLUSH": 1,
"Z_SYNC_FLUSH": 2,
"Z_FULL_FLUSH": 3,
"Z_FINISH": 4,
"Z_BLOCK": 5,
"Z_OK": 0,
"Z_STREAM_END": 1,
"Z_NEED_DICT": 2,
"Z_ERRNO": -1,
"Z_STREAM_ERROR": -2,
"Z_DATA_ERROR": -3,
"Z_MEM_ERROR": -4,
"Z_BUF_ERROR": -5,
"Z_VERSION_ERROR": -6,
"Z_NO_COMPRESSION": 0,
"Z_BEST_SPEED": 1,
"Z_BEST_COMPRESSION": 9,
"Z_DEFAULT_COMPRESSION": -1,
"Z_FILTERED": 1,
"Z_HUFFMAN_ONLY": 2,
"Z_RLE": 3,
"Z_FIXED": 4,
"Z_DEFAULT_STRATEGY": 0,
"ZLIB_VERNUM": 4880,
"DEFLATE": 1,
"INFLATE": 2,
"GZIP": 3,
"GUNZIP": 4,
"DEFLATERAW": 5,
"INFLATERAW": 6,
"UNZIP": 7,
"BROTLI_DECODE": 8,
"BROTLI_ENCODE": 9,
"Z_MIN_WINDOWBITS": 8,
"Z_MAX_WINDOWBITS": 15,
"Z_DEFAULT_WINDOWBITS": 15,
"Z_MIN_CHUNK": 64,
"Z_MAX_CHUNK": null,
"Z_DEFAULT_CHUNK": 16384,
"Z_MIN_MEMLEVEL": 1,
"Z_MAX_MEMLEVEL": 9,
"Z_DEFAULT_MEMLEVEL": 8,
"Z_MIN_LEVEL": -1,
"Z_MAX_LEVEL": 9,
"Z_DEFAULT_LEVEL": -1,
"BROTLI_OPERATION_PROCESS": 0,
"BROTLI_OPERATION_FLUSH": 1,
"BROTLI_OPERATION_FINISH": 2,
"BROTLI_OPERATION_EMIT_METADATA": 3,
"BROTLI_PARAM_MODE": 0,
"BROTLI_MODE_GENERIC": 0,
"BROTLI_MODE_TEXT": 1,
"BROTLI_MODE_FONT": 2,
"BROTLI_DEFAULT_MODE": 0,
"BROTLI_PARAM_QUALITY": 1,
"BROTLI_MIN_QUALITY": 0,
"BROTLI_MAX_QUALITY": 11,
"BROTLI_DEFAULT_QUALITY": 11,
"BROTLI_PARAM_LGWIN": 2,
"BROTLI_MIN_WINDOW_BITS": 10,
"BROTLI_MAX_WINDOW_BITS": 24,
"BROTLI_LARGE_MAX_WINDOW_BITS": 30,
"BROTLI_DEFAULT_WINDOW": 22,
"BROTLI_PARAM_LGBLOCK": 3,
"BROTLI_MIN_INPUT_BLOCK_BITS": 16,
"BROTLI_MAX_INPUT_BLOCK_BITS": 24,
"BROTLI_PARAM_DISABLE_LITERAL_CONTEXT_MODELING": 4,
"BROTLI_PARAM_SIZE_HINT": 5,
"BROTLI_PARAM_LARGE_WINDOW": 6,
"BROTLI_PARAM_NPOSTFIX": 7,
"BROTLI_PARAM_NDIRECT": 8,
"BROTLI_DECODER_RESULT_ERROR": 0,
"BROTLI_DECODER_RESULT_SUCCESS": 1,
"BROTLI_DECODER_RESULT_NEEDS_MORE_INPUT": 2,
"BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT": 3,
"BROTLI_DECODER_PARAM_DISABLE_RING_BUFFER_REALLOCATION": 0,
"BROTLI_DECODER_PARAM_LARGE_WINDOW": 1,
"BROTLI_DECODER_NO_ERROR": 0,
"BROTLI_DECODER_SUCCESS": 1,
"BROTLI_DECODER_NEEDS_MORE_INPUT": 2,
"BROTLI_DECODER_NEEDS_MORE_OUTPUT": 3,
"BROTLI_DECODER_ERROR_FORMAT_EXUBERANT_NIBBLE": -1,
"BROTLI_DECODER_ERROR_FORMAT_RESERVED": -2,
"BROTLI_DECODER_ERROR_FORMAT_EXUBERANT_META_NIBBLE": -3,
"BROTLI_DECODER_ERROR_FORMAT_SIMPLE_HUFFMAN_ALPHABET": -4,
"BROTLI_DECODER_ERROR_FORMAT_SIMPLE_HUFFMAN_SAME": -5,
"BROTLI_DECODER_ERROR_FORMAT_CL_SPACE": -6,
"BROTLI_DECODER_ERROR_FORMAT_HUFFMAN_SPACE": -7,
"BROTLI_DECODER_ERROR_FORMAT_CONTEXT_MAP_REPEAT": -8,
"BROTLI_DECODER_ERROR_FORMAT_BLOCK_LENGTH_1": -9,
"BROTLI_DECODER_ERROR_FORMAT_BLOCK_LENGTH_2": -10,
"BROTLI_DECODER_ERROR_FORMAT_TRANSFORM": -11,
"BROTLI_DECODER_ERROR_FORMAT_DICTIONARY": -12,
"BROTLI_DECODER_ERROR_FORMAT_WINDOW_BITS": -13,
"BROTLI_DECODER_ERROR_FORMAT_PADDING_1": -14,
"BROTLI_DECODER_ERROR_FORMAT_PADDING_2": -15,
"BROTLI_DECODER_ERROR_FORMAT_DISTANCE": -16,
"BROTLI_DECODER_ERROR_DICTIONARY_NOT_SET": -19,
"BROTLI_DECODER_ERROR_INVALID_ARGUMENTS": -20,
"BROTLI_DECODER_ERROR_ALLOC_CONTEXT_MODES": -21,
"BROTLI_DECODER_ERROR_ALLOC_TREE_GROUPS": -22,
"BROTLI_DECODER_ERROR_ALLOC_CONTEXT_MAP": -25,
"BROTLI_DECODER_ERROR_ALLOC_RING_BUFFER_1": -26,
"BROTLI_DECODER_ERROR_ALLOC_RING_BUFFER_2": -27,
"BROTLI_DECODER_ERROR_ALLOC_BLOCK_TYPE_TREES": -30,
"BROTLI_DECODER_ERROR_UNREACHABLE": -31
}`);
}
}
4 changes: 0 additions & 4 deletions src/workerd/api/node/zlib-util.c++
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,9 @@
// https://opensource.org/licenses/Apache-2.0
#include "zlib-util.h"

#include "zlib.h"

namespace workerd::api::node {

uint32_t ZlibUtil::crc32Sync(kj::Array<kj::byte> data, uint32_t value) {
// Note: Bytef is defined in zlib.h
return crc32(value, reinterpret_cast<const Bytef*>(data.begin()), data.size());
}

} // namespace workerd::api::node
Loading

0 comments on commit 6eb4389

Please sign in to comment.