Repository navigation
node:zlib: force decodeStrings back to true in ZlibBase #47140
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
Open
robobun
wants to merge
3
commits into
main
Choose a base branch
from
robobun/b411d741/zlib-decode-strings
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| })); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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. | ||
|
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", | ||
| }); | ||
|
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); | ||
| }); | ||
| }); | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.