Skip to content

Commit 7a4db97

Browse files
committed
zlib: support pooled output buffers in sync codecs
Add an options.offset to codec.process() and codec.end() so callers can append output into a shared, pooled buffer at a byte offset instead of handing in a fresh buffer (or a sliced view) on every call. This matches how buffers are typically pooled in Node and lets a parser reuse one allocation across many process/end calls. Offsets beyond the output buffer are rejected with ERR_OUT_OF_RANGE. Refs: #67846
1 parent 3193835 commit 7a4db97

3 files changed

Lines changed: 65 additions & 9 deletions

File tree

‎doc/api/zlib.md‎

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2624,15 +2624,19 @@ added: v28.0.0
26242624
* `options` {Object}
26252625
* `flush` {integer} The flush flag. Defaults to the algorithm default
26262626
(`Z_NO_FLUSH`, `BROTLI_OPERATION_PROCESS`, or `ZSTD_e_continue`).
2627+
* `offset` {integer} The byte offset into `output` at which to start writing.
2628+
Default: `0`. This lets a caller append output into a shared, pooled buffer
2629+
across calls instead of handing in a fresh buffer each time.
26272630
* Returns: {Object} An object with `bytesRead`, `bytesWritten`, and `finished`
26282631
properties.
26292632

2630-
Processes `input`, writing as much output as fits in `output`. The returned
2631-
`bytesRead` is how many input bytes were consumed and `bytesWritten` is how many
2632-
bytes were written to `output`. When `bytesWritten` equals the length of
2633-
`output`, the output buffer is full and the caller should call `process()`
2634-
again with the remaining input and a fresh output buffer. `finished` is `true`
2635-
once the end of the compressed stream (or frame) has been reached.
2633+
Processes `input`, writing as much output as fits in `output` (starting at
2634+
`options.offset`). The returned `bytesRead` is how many input bytes were
2635+
consumed and `bytesWritten` is how many bytes were written to `output`. When
2636+
`bytesWritten` equals `output.byteLength - offset`, the output buffer is full
2637+
and the caller should call `process()` again with the remaining input and a
2638+
fresh output buffer (or a new `offset`). `finished` is `true` once the end of
2639+
the compressed stream (or frame) has been reached.
26362640

26372641
### `codec.end(output)`
26382642

@@ -2641,6 +2645,9 @@ added: v28.0.0
26412645
-->
26422646

26432647
* `output` {Buffer|TypedArray|DataView|ArrayBuffer} A buffer to write into.
2648+
* `options` {Object}
2649+
* `offset` {integer} The byte offset into `output` at which to start writing.
2650+
Default: `0`.
26442651
* Returns: {Object} An object with `bytesWritten` and `finished` properties.
26452652

26462653
Performs the final flush (the finish flush) with no new input, writing any

‎lib/zlib.js‎

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1217,18 +1217,21 @@ class ZlibSync {
12171217
input = toSyncInput(input);
12181218
output = toSyncOutput(output);
12191219
const flush = this._resolveFlush(options);
1220+
const offset = this._resolveOffset(output, options);
12201221
return this._write(input, 0, input.byteLength,
1221-
output, 0, output.byteLength, flush);
1222+
output, offset, output.byteLength - offset, flush);
12221223
}
12231224

1224-
end(output) {
1225+
end(output, options) {
12251226
if (this._closed)
12261227
throw new ERR_STREAM_PREMATURE_CLOSE();
12271228
output = toSyncOutput(output);
1229+
const offset = this._resolveOffset(output, options);
12281230
if (this._finished)
12291231
return { __proto__: null, bytesWritten: 0, finished: true };
12301232
return this._write(null, 0, 0,
1231-
output, 0, output.byteLength, this._finishFlushFlag);
1233+
output, offset, output.byteLength - offset,
1234+
this._finishFlushFlag);
12321235
}
12331236

12341237
reset() {
@@ -1261,6 +1264,19 @@ class ZlibSync {
12611264
bound[0], bound[1], this._defaultFlushFlag);
12621265
}
12631266

1267+
// The byte offset into `output` at which to start writing. This lets callers
1268+
// append into a shared, pooled buffer across `process()` and `end()` calls
1269+
// instead of handing in a fresh buffer (or a sliced view) every time.
1270+
_resolveOffset(output, options) {
1271+
const offset = options?.offset ?? 0;
1272+
validateUint32(offset, 'options.offset');
1273+
if (offset > output.byteLength) {
1274+
throw new ERR_OUT_OF_RANGE(
1275+
'options.offset', `<= ${output.byteLength}`, offset);
1276+
}
1277+
return offset;
1278+
}
1279+
12641280
_write(inBuf, inOff, inLen, outBuf, outOff, outLen, flush) {
12651281
const handle = this._handle;
12661282
const state = this._writeState;

‎test/parallel/test-zlib-incremental-sync.js‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,39 @@ for (const [name, Ctor] of [
124124
assert.strictEqual(zlib.gunzipSync(roundTrip).toString(), inputString);
125125
}
126126

127+
// `offset` lets a caller append output into a shared, pooled buffer, avoiding a
128+
// fresh allocation per call.
129+
{
130+
const codec = new zlib.GzipSync();
131+
const pool = Buffer.allocUnsafe(1024);
132+
let poolOffset = 0;
133+
const r = codec.process(input, pool,
134+
{ flush: zlib.constants.Z_NO_FLUSH, offset: poolOffset });
135+
poolOffset += r.bytesWritten;
136+
const t = codec.end(pool, { offset: poolOffset });
137+
poolOffset += t.bytesWritten;
138+
assert.strictEqual(t.finished, true);
139+
140+
const compressed = pool.subarray(0, poolOffset);
141+
142+
// Decompress back into another pooled buffer, again using `offset`.
143+
const decoder = new zlib.GunzipSync();
144+
const outPool = Buffer.allocUnsafe(input.length * 2);
145+
let outOffset = 0;
146+
const dr = decoder.process(compressed, outPool,
147+
{ flush: zlib.constants.Z_NO_FLUSH, offset: outOffset });
148+
outOffset += dr.bytesWritten;
149+
assert.strictEqual(dr.finished, true);
150+
assert.strictEqual(outPool.subarray(0, outOffset).toString(), inputString);
151+
}
152+
153+
// An offset beyond the output buffer is rejected.
154+
{
155+
const codec = new zlib.GzipSync();
156+
assert.throws(() => codec.process(input, Buffer.allocUnsafe(8), { offset: 9 }),
157+
{ code: 'ERR_OUT_OF_RANGE' });
158+
}
159+
127160
// `process` rejects further input once a stream has finished, while `end`
128161
// becomes a no-op.
129162
{

0 commit comments

Comments
 (0)