Skip to content
Open
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
4 changes: 3 additions & 1 deletion src/js/node/zlib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,11 +280,13 @@ function ZlibBase(
// prettier-ignore
maxOutputLength = checkRangesOrGetDefault(opts.maxOutputLength, "options.maxOutputLength", 1, kMaxLength, kMaxLength);

if (opts.encoding || opts.objectMode || opts.writableObjectMode) {
// The native handle takes only buffers: https://github.com/nodejs/node/blob/v26.12.1/lib/zlib.js#L277-L284
if (opts.encoding || opts.objectMode || opts.writableObjectMode || opts.decodeStrings === false) {
opts = { ...opts };
opts.encoding = null;
opts.objectMode = false;
opts.writableObjectMode = false;
opts.decodeStrings = true;
Comment thread
robobun marked this conversation as resolved.
Resolved
}
}

Expand Down
32 changes: 32 additions & 0 deletions test/js/node/test/parallel/test-zlib-decode-strings.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
'use strict';

// Strings must be decoded before they reach the native handle, even when
// decodeStrings: false is passed. This used to abort the process.

const common = require('../common');
const assert = require('assert');
const zlib = require('zlib');

const input = 'héllo wörld';

const pairs = [
[zlib.createGzip, zlib.gunzipSync],
[zlib.createDeflate, zlib.inflateSync],
[zlib.createDeflateRaw, zlib.inflateRawSync],
[zlib.createBrotliCompress, zlib.brotliDecompressSync],
[zlib.createZstdCompress, zlib.zstdDecompressSync],
];

for (const [createCompress, decompressSync] of pairs) {
const compress = createCompress({ decodeStrings: false });
const chunks = [];
compress.on('data', (chunk) => chunks.push(chunk));
compress.on('end', common.mustCall(() => {
assert.strictEqual(decompressSync(Buffer.concat(chunks)).toString(), input);
}));
compress.end(input);
}

zlib.gzip(input, { decodeStrings: false }, common.mustSucceed((compressed) => {
assert.strictEqual(zlib.gunzipSync(compressed).toString(), input);
}));
95 changes: 95 additions & 0 deletions test/js/node/zlib/zlib-decode-strings.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/**
* All tests in this file run in both Bun and Node.js: `bun test` runs them
* here, and the last test runs this same file under Node.js.
*
* The native handle of a zlib stream takes only buffers. So Node.js sets
* `decodeStrings` back to true, as it does for `encoding` and `objectMode`
* (nodejs/node#66359, v26.11.0):
* https://github.com/nodejs/node/blob/v26.12.1/lib/zlib.js#L277-L284
*/
import assert from "node:assert";
import type { Transform, TransformOptions } from "node:stream";
import { finished } from "node:stream/promises";
import { describe, test } from "node:test";
import { fileURLToPath } from "node:url";
import { promisify } from "node:util";
import zlib from "node:zlib";

// @types/node declares no stream options for the zlib streams.
type Options = zlib.ZlibOptions &
zlib.BrotliOptions &
zlib.ZstdOptions &
Pick<TransformOptions, "decodeStrings" | "defaultEncoding">;
type Create = (options: Options) => Transform;
type Callback = (input: string, options: Options, callback: (error: Error | null, result: Buffer) => void) => void;

const codecs: [name: string, create: Create, createInverse: Create, compress: Callback, decompress: Callback][] = [
["gzip", zlib.createGzip, zlib.createGunzip, zlib.gzip, zlib.gunzip],
["deflate", zlib.createDeflate, zlib.createInflate, zlib.deflate, zlib.inflate],
["deflateRaw", zlib.createDeflateRaw, zlib.createInflateRaw, zlib.deflateRaw, zlib.inflateRaw],
["brotli", zlib.createBrotliCompress, zlib.createBrotliDecompress, zlib.brotliCompress, zlib.brotliDecompress],
["zstd", zlib.createZstdCompress, zlib.createZstdDecompress, zlib.zstdCompress, zlib.zstdDecompress],
];

async function output(stream: Transform, write: (stream: Transform) => void) {
const chunks: Buffer[] = [];
stream.on("data", chunk => chunks.push(chunk));
write(stream);
// Rejects for an error, and for a stream that closes before it ends.
await finished(stream);
return Buffer.concat(chunks);
}

describe("decodeStrings: false", () => {
for (const [name, create, createInverse, compress, decompress] of codecs) {
test(`${name}: the streams decode each string with its encoding`, async () => {
const compressed = await output(create({ decodeStrings: false, defaultEncoding: "hex" }), stream => {
// No encoding: the stream uses defaultEncoding.
stream.write("68656c6c6f");
stream.end(" wörld", "utf8");
});
const decompressed = await output(createInverse({ decodeStrings: false }), stream => {
stream.end(compressed.toString("base64"), "base64");
});
assert.strictEqual(decompressed.toString(), "hello wörld");
});

test(`${name}: the callback functions decode a string`, async () => {
const compressed = await promisify(compress)("hello wörld", { decodeStrings: false });
const options: Options = { decodeStrings: false, defaultEncoding: "base64" };
const decompressed = await promisify(decompress)(compressed.toString("base64"), options);
assert.strictEqual(decompressed.toString(), "hello wörld");
});
}

test("the options object of the caller is not changed", async () => {
const options: Options = Object.freeze({ decodeStrings: false });
const compressed = await output(zlib.createGzip(options), stream => stream.end("hello wörld"));
assert.strictEqual(zlib.gunzipSync(compressed).toString(), "hello wörld");
assert.deepStrictEqual(options, { decodeStrings: false });
});
});

// Only in Bun: when Node.js runs this file it must not spawn itself again.
Comment thread
robobun marked this conversation as resolved.
Resolved
if (typeof Bun !== "undefined") {
const { bunEnv, nodeExeAtLeast } = await import("harness");
// Before v26.11.0 the string reaches the native handle, and that aborts the process.
const node = nodeExeAtLeast("26.11.0");

describe("Node.js compatibility", () => {
(node ? test : test.skip)("all tests pass in Node.js", async () => {
// A direct run, not `node --test`: the runner mode forks a second node
// process per file. node:test still exits non-zero on any failure.
// --no-warnings: Node.js warns on stderr that the package of this file has no "type".
await using proc = Bun.spawn({
cmd: [node!, "--no-warnings", "--v8-pool-size=1", fileURLToPath(import.meta.url)],
env: { ...bunEnv, UV_THREADPOOL_SIZE: "2" },
stdout: "pipe",
stderr: "pipe",
});
Comment thread
robobun marked this conversation as resolved.
Resolved
const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]);
// stdout is the report of node:test. It has the duration of each test, so a failure shows it as its message.
assert.deepStrictEqual({ stderr, exitCode }, { stderr: "", exitCode: 0 }, stdout);
});
});
}
Loading