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
8 changes: 6 additions & 2 deletions lib/zlib.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,9 @@ function zlibBufferOnEnd() {
buf = new FastBuffer();
} else {
const bufs = this.buffers;
buf = (bufs.length === 1 ? bufs[0] : Buffer.concat(bufs, this.nread));
buf = (bufs.length === 1 ?
(bufs[0].byteLength === bufs[0].buffer.byteLength ? bufs[0] : Buffer.from(bufs[0])) :
Buffer.concat(bufs, this.nread));
}
this.close();
if (this._info)
Expand Down Expand Up @@ -497,7 +499,9 @@ function processChunkSync(self, chunk, flushFlag) {
if (nread === 0)
return new FastBuffer();

return (buffers.length === 1 ? buffers[0] : Buffer.concat(buffers, nread));
return (buffers.length === 1 ?
(buffers[0].byteLength === buffers[0].buffer.byteLength ? buffers[0] : Buffer.from(buffers[0])) :
Buffer.concat(buffers, nread));
}

function processChunk(self, chunk, flushFlag, cb) {
Expand Down
81 changes: 81 additions & 0 deletions test/parallel/test-zlib-one-shot-memory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
'use strict';
const common = require('../common');
const assert = require('assert');
const zlib = require('zlib');

// Test that one-shot convenience methods (async and sync) do not retain
// oversized unpooled backing ArrayBuffers (such as the default chunkSize)
// when output size is small.

const smallPayload = Buffer.from('hello world'.repeat(10));
const chunkSize = 128 * 1024;

// Test async and sync compress convenience methods
for (const method of [
'gzip',
'deflate',
'deflateRaw',
'brotliCompress',
'zstdCompress',
]) {
zlib[method](smallPayload, { chunkSize }, common.mustSucceed((buf) => {
assert.ok(Buffer.isBuffer(buf));

// The backing ArrayBuffer should not retain the 128KB unpooled chunk
assert.ok(
buf.buffer.byteLength < chunkSize,
`${method} result backing ArrayBuffer should not retain chunkSize padding`
);
assert.ok(
buf.buffer.byteLength <= Buffer.poolSize,
`${method} result backing ArrayBuffer should not exceed Buffer.poolSize`
);
}));

// Test sync convenience method
const syncMethod = `${method}Sync`;
const syncResult = zlib[syncMethod](smallPayload, { chunkSize });
assert.ok(Buffer.isBuffer(syncResult));
assert.ok(
syncResult.buffer.byteLength < chunkSize,
`${syncMethod} result backing ArrayBuffer should not retain chunkSize padding`
);
assert.ok(

Check failure on line 43 in test/parallel/test-zlib-one-shot-memory.js

View workflow job for this annotation

GitHub Actions / test-linux (ubuntu-24.04-arm)

--- stderr --- node:internal/assert/utils:146 throw error; ^ AssertionError [ERR_ASSERTION]: gzipSync result backing ArrayBuffer should not exceed Buffer.poolSize at Object.<anonymous> (/home/runner/work/node/node/node/test/parallel/test-zlib-one-shot-memory.js:43:10) at Module._compile (node:internal/modules/cjs/loader:1924:14) at Object..js (node:internal/modules/cjs/loader:2064:10) at Module.load (node:internal/modules/cjs/loader:1646:32) at Module._load (node:internal/modules/cjs/loader:1438:12) at wrapModuleLoad (node:internal/modules/cjs/loader:261:19) at Module.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:154:5) at node:internal/main/run_main_module:33:47 { generatedMessage: false, code: 'ERR_ASSERTION', actual: false, expected: true, operator: '==', diff: 'simple' } Node.js v27.0.0-pre Command: out/Release/node /home/runner/work/node/node/node/test/parallel/test-zlib-one-shot-memory.js

Check failure on line 43 in test/parallel/test-zlib-one-shot-memory.js

View workflow job for this annotation

GitHub Actions / test-linux (ubuntu-24.04)

--- stderr --- node:internal/assert/utils:146 throw error; ^ AssertionError [ERR_ASSERTION]: gzipSync result backing ArrayBuffer should not exceed Buffer.poolSize at Object.<anonymous> (/home/runner/work/node/node/node/test/parallel/test-zlib-one-shot-memory.js:43:10) at Module._compile (node:internal/modules/cjs/loader:1924:14) at Object..js (node:internal/modules/cjs/loader:2064:10) at Module.load (node:internal/modules/cjs/loader:1646:32) at Module._load (node:internal/modules/cjs/loader:1438:12) at wrapModuleLoad (node:internal/modules/cjs/loader:261:19) at Module.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:154:5) at node:internal/main/run_main_module:33:47 { generatedMessage: false, code: 'ERR_ASSERTION', actual: false, expected: true, operator: '==', diff: 'simple' } Node.js v27.0.0-pre Command: out/Release/node /home/runner/work/node/node/node/test/parallel/test-zlib-one-shot-memory.js

Check failure on line 43 in test/parallel/test-zlib-one-shot-memory.js

View workflow job for this annotation

GitHub Actions / test-macOS

--- stderr --- node:internal/assert/utils:146 throw error; ^ AssertionError [ERR_ASSERTION]: gzipSync result backing ArrayBuffer should not exceed Buffer.poolSize at Object.<anonymous> (/Users/runner/work/node/node/node/test/parallel/test-zlib-one-shot-memory.js:43:10) at Module._compile (node:internal/modules/cjs/loader:1924:14) at Object..js (node:internal/modules/cjs/loader:2064:10) at Module.load (node:internal/modules/cjs/loader:1646:32) at Module._load (node:internal/modules/cjs/loader:1438:12) at wrapModuleLoad (node:internal/modules/cjs/loader:261:19) at Module.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:154:5) at node:internal/main/run_main_module:33:47 { generatedMessage: false, code: 'ERR_ASSERTION', actual: false, expected: true, operator: '==', diff: 'simple' } Node.js v27.0.0-pre Command: out/Release/node /Users/runner/work/node/node/node/test/parallel/test-zlib-one-shot-memory.js

Check failure on line 43 in test/parallel/test-zlib-one-shot-memory.js

View workflow job for this annotation

GitHub Actions / x86_64-darwin: with shared libraries / build

--- stderr --- node:internal/assert/utils:146 throw error; ^ AssertionError [ERR_ASSERTION]: gzipSync result backing ArrayBuffer should not exceed Buffer.poolSize at Object.<anonymous> (/Users/runner/work/_temp/node-v27.0.0-nightly2026-08-30138e90bba2-slim/test/parallel/test-zlib-one-shot-memory.js:43:10) at Module._compile (node:internal/modules/cjs/loader:1924:14) at Object..js (node:internal/modules/cjs/loader:2064:10) at Module.load (node:internal/modules/cjs/loader:1646:32) at Module._load (node:internal/modules/cjs/loader:1438:12) at wrapModuleLoad (node:internal/modules/cjs/loader:261:19) at Module.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:154:5) at node:internal/main/run_main_module:33:47 { generatedMessage: false, code: 'ERR_ASSERTION', actual: false, expected: true, operator: '==', diff: 'simple' } Node.js v27.0.0-pre Command: out/Release/node /Users/runner/work/_temp/node-v27.0.0-nightly2026-08-30138e90bba2-slim/test/parallel/test-zlib-one-shot-memory.js

Check failure on line 43 in test/parallel/test-zlib-one-shot-memory.js

View workflow job for this annotation

GitHub Actions / x86_64-linux: with shared libraries and perfetto / build

--- stderr --- node:internal/assert/utils:146 throw error; ^ AssertionError [ERR_ASSERTION]: gzipSync result backing ArrayBuffer should not exceed Buffer.poolSize at Object.<anonymous> (/home/runner/work/_temp/node-v27.0.0-nightly2026-08-30138e90bba2-slim/test/parallel/test-zlib-one-shot-memory.js:43:10) at Module._compile (node:internal/modules/cjs/loader:1924:14) at Object..js (node:internal/modules/cjs/loader:2064:10) at Module.load (node:internal/modules/cjs/loader:1646:32) at Module._load (node:internal/modules/cjs/loader:1438:12) at wrapModuleLoad (node:internal/modules/cjs/loader:261:19) at Module.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:154:5) at node:internal/main/run_main_module:33:47 { generatedMessage: false, code: 'ERR_ASSERTION', actual: false, expected: true, operator: '==', diff: 'simple' } Node.js v27.0.0-pre Command: out/Release/node /home/runner/work/_temp/node-v27.0.0-nightly2026-08-30138e90bba2-slim/test/parallel/test-zlib-one-shot-memory.js

Check failure on line 43 in test/parallel/test-zlib-one-shot-memory.js

View workflow job for this annotation

GitHub Actions / aarch64-darwin: with shared libraries / build

--- stderr --- node:internal/assert/utils:146 throw error; ^ AssertionError [ERR_ASSERTION]: gzipSync result backing ArrayBuffer should not exceed Buffer.poolSize at Object.<anonymous> (/Users/runner/work/_temp/node-v27.0.0-nightly2026-08-30138e90bba2-slim/test/parallel/test-zlib-one-shot-memory.js:43:10) at Module._compile (node:internal/modules/cjs/loader:1924:14) at Object..js (node:internal/modules/cjs/loader:2064:10) at Module.load (node:internal/modules/cjs/loader:1646:32) at Module._load (node:internal/modules/cjs/loader:1438:12) at wrapModuleLoad (node:internal/modules/cjs/loader:261:19) at Module.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:154:5) at node:internal/main/run_main_module:33:47 { generatedMessage: false, code: 'ERR_ASSERTION', actual: false, expected: true, operator: '==', diff: 'simple' } Node.js v27.0.0-pre Command: out/Release/node /Users/runner/work/_temp/node-v27.0.0-nightly2026-08-30138e90bba2-slim/test/parallel/test-zlib-one-shot-memory.js

Check failure on line 43 in test/parallel/test-zlib-one-shot-memory.js

View workflow job for this annotation

GitHub Actions / aarch64-linux: with shared boringssl-0.20260803.0 / build

--- stderr --- node:internal/assert/utils:146 throw error; ^ AssertionError [ERR_ASSERTION]: gzipSync result backing ArrayBuffer should not exceed Buffer.poolSize at Object.<anonymous> (/home/runner/work/_temp/node-v27.0.0-nightly2026-08-30138e90bba2-slim/test/parallel/test-zlib-one-shot-memory.js:43:10) at Module._compile (node:internal/modules/cjs/loader:1924:14) at Object..js (node:internal/modules/cjs/loader:2064:10) at Module.load (node:internal/modules/cjs/loader:1646:32) at Module._load (node:internal/modules/cjs/loader:1438:12) at wrapModuleLoad (node:internal/modules/cjs/loader:261:19) at Module.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:154:5) at node:internal/main/run_main_module:33:47 { generatedMessage: false, code: 'ERR_ASSERTION', actual: false, expected: true, operator: '==', diff: 'simple' } Node.js v27.0.0-pre Command: out/Release/node /home/runner/work/_temp/node-v27.0.0-nightly2026-08-30138e90bba2-slim/test/parallel/test-zlib-one-shot-memory.js

Check failure on line 43 in test/parallel/test-zlib-one-shot-memory.js

View workflow job for this annotation

GitHub Actions / aarch64-linux: with shared openssl-fips-3.5.7 / build

--- stderr --- node:internal/assert/utils:146 throw error; ^ AssertionError [ERR_ASSERTION]: gzipSync result backing ArrayBuffer should not exceed Buffer.poolSize at Object.<anonymous> (/home/runner/work/_temp/node-v27.0.0-nightly2026-08-30138e90bba2-slim/test/parallel/test-zlib-one-shot-memory.js:43:10) at Module._compile (node:internal/modules/cjs/loader:1924:14) at Object..js (node:internal/modules/cjs/loader:2064:10) at Module.load (node:internal/modules/cjs/loader:1646:32) at Module._load (node:internal/modules/cjs/loader:1438:12) at wrapModuleLoad (node:internal/modules/cjs/loader:261:19) at Module.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:154:5) at node:internal/main/run_main_module:33:47 { generatedMessage: false, code: 'ERR_ASSERTION', actual: false, expected: true, operator: '==', diff: 'simple' } Node.js v27.0.0-pre Command: out/Release/node /home/runner/work/_temp/node-v27.0.0-nightly2026-08-30138e90bba2-slim/test/parallel/test-zlib-one-shot-memory.js

Check failure on line 43 in test/parallel/test-zlib-one-shot-memory.js

View workflow job for this annotation

GitHub Actions / aarch64-linux: with shared openssl-4.0.1 / build

--- stderr --- node:internal/assert/utils:146 throw error; ^ AssertionError [ERR_ASSERTION]: gzipSync result backing ArrayBuffer should not exceed Buffer.poolSize at Object.<anonymous> (/home/runner/work/_temp/node-v27.0.0-nightly2026-08-30138e90bba2-slim/test/parallel/test-zlib-one-shot-memory.js:43:10) at Module._compile (node:internal/modules/cjs/loader:1924:14) at Object..js (node:internal/modules/cjs/loader:2064:10) at Module.load (node:internal/modules/cjs/loader:1646:32) at Module._load (node:internal/modules/cjs/loader:1438:12) at wrapModuleLoad (node:internal/modules/cjs/loader:261:19) at Module.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:154:5) at node:internal/main/run_main_module:33:47 { generatedMessage: false, code: 'ERR_ASSERTION', actual: false, expected: true, operator: '==', diff: 'simple' } Node.js v27.0.0-pre Command: out/Release/node /home/runner/work/_temp/node-v27.0.0-nightly2026-08-30138e90bba2-slim/test/parallel/test-zlib-one-shot-memory.js

Check failure on line 43 in test/parallel/test-zlib-one-shot-memory.js

View workflow job for this annotation

GitHub Actions / aarch64-linux: with shared openssl-3.6.3 / build

--- stderr --- node:internal/assert/utils:146 throw error; ^ AssertionError [ERR_ASSERTION]: gzipSync result backing ArrayBuffer should not exceed Buffer.poolSize at Object.<anonymous> (/home/runner/work/_temp/node-v27.0.0-nightly2026-08-30138e90bba2-slim/test/parallel/test-zlib-one-shot-memory.js:43:10) at Module._compile (node:internal/modules/cjs/loader:1924:14) at Object..js (node:internal/modules/cjs/loader:2064:10) at Module.load (node:internal/modules/cjs/loader:1646:32) at Module._load (node:internal/modules/cjs/loader:1438:12) at wrapModuleLoad (node:internal/modules/cjs/loader:261:19) at Module.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:154:5) at node:internal/main/run_main_module:33:47 { generatedMessage: false, code: 'ERR_ASSERTION', actual: false, expected: true, operator: '==', diff: 'simple' } Node.js v27.0.0-pre Command: out/Release/node /home/runner/work/_temp/node-v27.0.0-nightly2026-08-30138e90bba2-slim/test/parallel/test-zlib-one-shot-memory.js

Check failure on line 43 in test/parallel/test-zlib-one-shot-memory.js

View workflow job for this annotation

GitHub Actions / aarch64-linux: with shared openssl-3.5.7 / build

--- stderr --- node:internal/assert/utils:146 throw error; ^ AssertionError [ERR_ASSERTION]: gzipSync result backing ArrayBuffer should not exceed Buffer.poolSize at Object.<anonymous> (/home/runner/work/_temp/node-v27.0.0-nightly2026-08-30138e90bba2-slim/test/parallel/test-zlib-one-shot-memory.js:43:10) at Module._compile (node:internal/modules/cjs/loader:1924:14) at Object..js (node:internal/modules/cjs/loader:2064:10) at Module.load (node:internal/modules/cjs/loader:1646:32) at Module._load (node:internal/modules/cjs/loader:1438:12) at wrapModuleLoad (node:internal/modules/cjs/loader:261:19) at Module.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:154:5) at node:internal/main/run_main_module:33:47 { generatedMessage: false, code: 'ERR_ASSERTION', actual: false, expected: true, operator: '==', diff: 'simple' } Node.js v27.0.0-pre Command: out/Release/node /home/runner/work/_temp/node-v27.0.0-nightly2026-08-30138e90bba2-slim/test/parallel/test-zlib-one-shot-memory.js

Check failure on line 43 in test/parallel/test-zlib-one-shot-memory.js

View workflow job for this annotation

GitHub Actions / aarch64-linux: with shared openssl-3.0.21 / build

--- stderr --- node:internal/assert/utils:146 throw error; ^ AssertionError [ERR_ASSERTION]: gzipSync result backing ArrayBuffer should not exceed Buffer.poolSize at Object.<anonymous> (/home/runner/work/_temp/node-v27.0.0-nightly2026-08-30138e90bba2-slim/test/parallel/test-zlib-one-shot-memory.js:43:10) at Module._compile (node:internal/modules/cjs/loader:1924:14) at Object..js (node:internal/modules/cjs/loader:2064:10) at Module.load (node:internal/modules/cjs/loader:1646:32) at Module._load (node:internal/modules/cjs/loader:1438:12) at wrapModuleLoad (node:internal/modules/cjs/loader:261:19) at Module.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:154:5) at node:internal/main/run_main_module:33:47 { generatedMessage: false, code: 'ERR_ASSERTION', actual: false, expected: true, operator: '==', diff: 'simple' } Node.js v27.0.0-pre Command: out/Release/node /home/runner/work/_temp/node-v27.0.0-nightly2026-08-30138e90bba2-slim/test/parallel/test-zlib-one-shot-memory.js
syncResult.buffer.byteLength <= Buffer.poolSize,
`${syncMethod} result backing ArrayBuffer should not exceed Buffer.poolSize`
);
}

// Test decompress convenience methods
zlib.gzip(smallPayload, common.mustSucceed((compressed) => {
zlib.gunzip(compressed, { chunkSize }, common.mustSucceed((decompressed) => {
assert.strictEqual(decompressed.toString(), smallPayload.toString());
assert.ok(
decompressed.buffer.byteLength < chunkSize,
'gunzip result backing ArrayBuffer should not retain chunkSize padding'
);
assert.ok(
decompressed.buffer.byteLength <= Buffer.poolSize,
'gunzip result backing ArrayBuffer should not exceed Buffer.poolSize'
);
}));

const syncDecompressed = zlib.gunzipSync(compressed, { chunkSize });
assert.strictEqual(syncDecompressed.toString(), smallPayload.toString());
assert.ok(
syncDecompressed.buffer.byteLength < chunkSize,
'gunzipSync result backing ArrayBuffer should not retain chunkSize padding'
);
assert.ok(
syncDecompressed.buffer.byteLength <= Buffer.poolSize,
'gunzipSync result backing ArrayBuffer should not exceed Buffer.poolSize'
);
}));

// Test error path on invalid data
{
const invalidGzip = Buffer.from([0x1f, 0x8b, 0x00, 0x00]);
zlib.gunzip(invalidGzip, { chunkSize }, common.mustCall((err) => {
assert.ok(err);
}));
}
Loading