Skip to content
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
25 changes: 25 additions & 0 deletions src/node/internal/internal_zlib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
import { Buffer } from 'node-internal:internal_buffer';
import { validateUint32 } from 'node-internal:validators';
import { ERR_INVALID_ARG_TYPE } from 'node-internal:internal_errors';
import { isAnyArrayBuffer } from 'node-internal:internal_types';
import {
Zlib,
Brotli,
Expand Down Expand Up @@ -58,6 +59,20 @@ export function crc32(
return zlibUtil.crc32(data, value);
}

function validateInput(data: unknown): void {
if (
typeof data !== 'string' &&
!ArrayBuffer.isView(data) &&
!isAnyArrayBuffer(data)
) {
throw new ERR_INVALID_ARG_TYPE(
'buffer',
['string', 'Buffer', 'TypedArray', 'DataView', 'ArrayBuffer'],
data
);
}
}

function processChunk(
engine: ZlibBase,
data: ArrayBufferView | string
Expand All @@ -77,6 +92,7 @@ function zlibSyncImpl(
options: ZlibOptions,
mode: (typeof ZlibMode)[keyof typeof ZlibMode]
): ZlibResult {
validateInput(data);
if (!options.info) {
// Fast path, where we send the data directly to C++
return Buffer.from(zlibUtil.zlibSync(data, options, mode));
Expand Down Expand Up @@ -140,6 +156,7 @@ export function brotliDecompressSync(
data: ArrayBufferView | string,
options: BrotliOptions = {}
): ZlibResult {
validateInput(data);
if (!options.info) {
// Fast path, where we send the data directly to C++
return Buffer.from(zlibUtil.brotliDecompressSync(data, options));
Expand All @@ -153,6 +170,7 @@ export function brotliCompressSync(
data: ArrayBufferView | string,
options: BrotliOptions = {}
): ZlibResult {
validateInput(data);
if (!options.info) {
// Fast path, where we send the data directly to C++
return Buffer.from(zlibUtil.brotliCompressSync(data, options));
Expand Down Expand Up @@ -209,6 +227,7 @@ function zlibImpl(
optionsOrCallback,
callbackOrUndefined
);
validateInput(data);

if (!options.info) {
// Fast path
Expand Down Expand Up @@ -294,6 +313,7 @@ export function brotliDecompress(
optionsOrCallback,
callbackOrUndefined
);
validateInput(data);

if (!options.info) {
// Fast path
Expand Down Expand Up @@ -322,6 +342,7 @@ export function brotliCompress(
optionsOrCallback,
callbackOrUndefined
);
validateInput(data);

if (!options.info) {
// Fast path
Expand All @@ -345,6 +366,7 @@ export function zstdDecompressSync(
data: ArrayBufferView | string,
options: ZstdOptions = {}
): ZlibResult {
validateInput(data);
if (!options.info) {
// Fast path, where we send the data directly to C++
return Buffer.from(zlibUtil.zstdDecompressSync(data, options));
Expand All @@ -358,6 +380,7 @@ export function zstdCompressSync(
data: ArrayBufferView | string,
options: ZstdOptions = {}
): ZlibResult {
validateInput(data);
if (!options.info) {
// Fast path, where we send the data directly to C++
return Buffer.from(zlibUtil.zstdCompressSync(data, options));
Expand All @@ -376,6 +399,7 @@ export function zstdDecompress(
optionsOrCallback,
callbackOrUndefined
);
validateInput(data);

if (!options.info) {
// Fast path
Expand Down Expand Up @@ -404,6 +428,7 @@ export function zstdCompress(
optionsOrCallback,
callbackOrUndefined
);
validateInput(data);

if (!options.info) {
// Fast path
Expand Down
110 changes: 83 additions & 27 deletions src/workerd/api/node/tests/zlib-nodejs-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1380,13 +1380,29 @@ export const zlibNotStringOrBuffer = {
[1, 2, 3],
{ foo: 'bar' },
]) {
assert.throws(
() => zlib.deflateSync(input),
// TODO(soon): Use same error code as NodeJS
{
assert.throws(() => zlib.deflateSync(input), {
code: 'ERR_INVALID_ARG_TYPE',
name: 'TypeError',
message:
/^The "buffer" argument must be of type string or an instance of Buffer, TypedArray, DataView, or ArrayBuffer\. Received /,
});
for (const fn of [
zlib.deflate,
zlib.gzip,
zlib.brotliCompress,
zlib.zstdCompress,
]) {
assert.throws(() => fn(input, () => {}), {
code: 'ERR_INVALID_ARG_TYPE',
name: 'TypeError',
}
);
});
}
for (const fn of [zlib.brotliCompressSync, zlib.zstdCompressSync]) {
assert.throws(() => fn(input), {
code: 'ERR_INVALID_ARG_TYPE',
name: 'TypeError',
});
}
}
},
};
Expand Down Expand Up @@ -1429,18 +1445,20 @@ export const zlibFromGzipWithTrailingGarbage = {
Buffer.alloc(10),
]);

assert.throws(
() => zlib.gunzipSync(data),
/^Error: unknown compression method$/
);
assert.throws(() => zlib.gunzipSync(data), {
name: 'Error',
code: 'Z_DATA_ERROR',
errno: -3,
message: 'unknown compression method',
});

{
const { promise, resolve } = Promise.withResolvers();
zlib.gunzip(data, (err, result) => {
// TODO(soon): use same error code as NodeJS
// TODO(soon): Do our messages have a redundant "Error: " word?
assert.strictEqual(err.name, 'Error');
assert.match(err.message, /unknown compression method/);
assert.strictEqual(err.code, 'Z_DATA_ERROR');
assert.strictEqual(err.errno, -3);
assert.strictEqual(err.message, 'unknown compression method');
assert.strictEqual(result, undefined);
resolve();
});
Expand All @@ -1456,18 +1474,20 @@ export const zlibFromGzipWithTrailingGarbage = {
Buffer.from([0x1f, 0x8b, 0xff, 0xff]),
]);

assert.throws(
() => zlib.gunzipSync(data),
/^Error: unknown compression method$/
);
assert.throws(() => zlib.gunzipSync(data), {
name: 'Error',
code: 'Z_DATA_ERROR',
errno: -3,
message: 'unknown compression method',
});

{
const { promise, resolve } = Promise.withResolvers();
zlib.gunzip(data, (err, result) => {
// TODO(soon): Use same error code as NodeJS
// TODO(soon): Do our messages have a redundant "Error: " word?
assert.strictEqual(err.name, 'Error');
assert.match(err.message, /unknown compression method/);
assert.strictEqual(err.code, 'Z_DATA_ERROR');
assert.strictEqual(err.errno, -3);
assert.strictEqual(err.message, 'unknown compression method');
assert.strictEqual(result, undefined);
resolve();
});
Expand Down Expand Up @@ -2218,17 +2238,21 @@ export const maxOutputLength = {
{
const { promise, resolve } = Promise.withResolvers();
zlib.brotliDecompress(encoded, { maxOutputLength: 64 }, (err) => {
// TODO(soon): Make error the same as NodeJS
assert.strictEqual(err.name, 'RangeError');
assert.strictEqual(err.code, 'ERR_BUFFER_TOO_LARGE');
assert.match(err.message, /Memory limit exceeded/);
resolve();
});
await promise;
}

// Sync
assert.throws(function () {
zlib.brotliDecompressSync(encoded, { maxOutputLength: 64 });
}, RangeError);
assert.throws(
function () {
zlib.brotliDecompressSync(encoded, { maxOutputLength: 64 });
},
{ name: 'RangeError', code: 'ERR_BUFFER_TOO_LARGE' }
);

// Async
{
Expand Down Expand Up @@ -2432,13 +2456,12 @@ export const brotli = {

{
// Test options.flush range
// TODO(soon): Use the same code and message as NodeJS
assert.throws(
() => {
zlib.brotliCompressSync('', { flush: zlib.constants.Z_FINISH });
},
{
//code: 'ERR_OUT_OF_RANGE',
code: 'ERR_OUT_OF_RANGE',
name: 'RangeError',
message:
'The value of "options.flush" is out of range. It must be >= 0 ' +
Expand All @@ -2453,7 +2476,7 @@ export const brotli = {
});
},
{
//code: 'ERR_OUT_OF_RANGE',
code: 'ERR_OUT_OF_RANGE',
name: 'RangeError',
message:
'The value of "options.finishFlush" is out of range. It must be ' +
Expand Down Expand Up @@ -3019,3 +3042,36 @@ export const zlibParamsAfterFailedWriteNoStalePointers = {
}
},
};

// One-shot APIs attach Node's error codes on the fast path into C++.
export const oneShotErrorCodes = {
test() {
// Inputs accepted by the C++ fast path stay accepted.
const raw = zlib.deflateSync('abc');
for (const input of [
raw,
new Uint8Array(raw),
new DataView(raw.buffer, raw.byteOffset, raw.byteLength),
new Uint8Array(raw).buffer,
]) {
assert.strictEqual(zlib.inflateSync(input).toString(), 'abc');
}

for (const [fn, name] of [
[zlib.deflateSync, 'flush'],
[zlib.deflateSync, 'finishFlush'],
[zlib.zstdCompressSync, 'flush'],
[zlib.zstdCompressSync, 'finishFlush'],
]) {
assert.throws(() => fn('', { [name]: 99 }), {
name: 'RangeError',
code: 'ERR_OUT_OF_RANGE',
message: new RegExp(`^The value of "options.${name}" .* Received 99$`),
});
}
assert.throws(() => zlib.deflateSync('', { chunkSize: 1 }), {
name: 'RangeError',
code: 'ERR_OUT_OF_RANGE',
});
},
};
Loading
Loading