Skip to content

Commit 2d64e56

Browse files
anonrigaduh95
authored andcommitted
zlib: skip the stream in sync convenience calls without options
`zlib.inflateSync(buffer)` and the other synchronous convenience methods constructed a Transform stream for every call, even though only its native handle is used and, without options, the stream is never exposed. They also allocated a fresh output chunk for every call. When no options are passed, drive a native handle created with the default settings directly, and decompress into a scratch buffer kept between calls, copying the result out. Calls with options, including `info`, keep the existing path. Results and errors are unchanged. Signed-off-by: Yagiz Nizipli <yagiz@nizipli.com> Assisted-by: Grok PR-URL: #66864 Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
1 parent 1ab1128 commit 2d64e56

2 files changed

Lines changed: 298 additions & 19 deletions

File tree

‎lib/zlib.js‎

Lines changed: 162 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ const {
3232
ObjectKeys,
3333
ObjectSetPrototypeOf,
3434
ReflectApply,
35+
SafeMap,
3536
Symbol,
3637
SymbolHasInstance,
3738
Uint32Array,
@@ -178,43 +179,128 @@ function zlibBufferOnEnd() {
178179
this.cb(null, buf);
179180
}
180181

181-
function zlibBufferSync(engine, buffer) {
182+
function toSyncInput(buffer) {
182183
if (typeof buffer === 'string') {
183-
buffer = Buffer.from(buffer);
184-
} else if (!isArrayBufferView(buffer)) {
184+
return Buffer.from(buffer);
185+
}
186+
if (!isArrayBufferView(buffer)) {
185187
if (isAnyArrayBuffer(buffer)) {
186-
buffer = Buffer.from(buffer);
187-
} else {
188-
throw new ERR_INVALID_ARG_TYPE(
189-
'buffer',
190-
['string', 'Buffer', 'TypedArray', 'DataView', 'ArrayBuffer'],
191-
buffer,
192-
);
188+
return Buffer.from(buffer);
193189
}
190+
throw new ERR_INVALID_ARG_TYPE(
191+
'buffer',
192+
['string', 'Buffer', 'TypedArray', 'DataView', 'ArrayBuffer'],
193+
buffer,
194+
);
194195
}
196+
return buffer;
197+
}
198+
199+
function zlibBufferSync(engine, buffer) {
200+
buffer = toSyncInput(buffer);
195201
buffer = processChunkSync(engine, buffer, engine._finishFlushFlag);
196202
if (engine._info)
197203
return { buffer, engine };
198204
return buffer;
199205
}
200206

207+
function zlibError(message, errno, code) {
208+
if (code === 'ERR_TRAILING_JUNK_AFTER_STREAM_END') {
209+
return new ERR_TRAILING_JUNK_AFTER_STREAM_END();
210+
}
211+
const error = genericNodeError(message, { errno, code });
212+
error.errno = errno;
213+
error.code = code;
214+
return error;
215+
}
216+
201217
function zlibOnError(message, errno, code) {
202218
const self = this[owner_symbol];
203219
// There is no way to cleanly recover.
204220
// Continuing only obscures problems.
205-
206-
let error;
207-
if (code === 'ERR_TRAILING_JUNK_AFTER_STREAM_END') {
208-
error = new ERR_TRAILING_JUNK_AFTER_STREAM_END();
209-
} else {
210-
error = genericNodeError(message, { errno, code });
211-
error.errno = errno;
212-
error.code = code;
213-
}
221+
const error = zlibError(message, errno, code);
214222
self.destroy(error);
215223
self[kError] = error;
216224
}
217225

226+
// Without options, a synchronous convenience call (zlib.inflateSync(buffer),
227+
// ...) uses the defaults and its engine is never exposed, so it drives a
228+
// native handle directly instead of constructing a Transform stream. It also
229+
// decompresses into a scratch buffer kept between calls and copies the result
230+
// out, instead of allocating a whole output chunk per call.
231+
let syncScratchBuffer;
232+
233+
function zlibSyncOnError(message, errno, code) {
234+
this[kError] = zlibError(message, errno, code);
235+
}
236+
237+
function zlibBufferSyncWithDefaults(ctor, buffer) {
238+
const chunk = toSyncInput(buffer);
239+
const mode = kEngineModes.get(ctor);
240+
const writeState = new Uint32Array(2);
241+
const handle = createDefaultHandle(mode, writeState);
242+
handle.onerror = zlibSyncOnError;
243+
// Take the scratch buffer, so that a nested call (e.g. from an async hook)
244+
// allocates its own.
245+
const scratch = syncScratchBuffer ?? Buffer.allocUnsafeSlow(Z_DEFAULT_CHUNK);
246+
syncScratchBuffer = undefined;
247+
try {
248+
return processSyncWithDefaults(handle, writeState, chunk,
249+
defaultFinishFlush(mode), scratch);
250+
} finally {
251+
handle.close();
252+
syncScratchBuffer = scratch;
253+
}
254+
}
255+
256+
// Same as processChunkSync() for an engine created without options.
257+
function processSyncWithDefaults(handle, writeState, chunk, flushFlag,
258+
scratch) {
259+
const chunkSize = Z_DEFAULT_CHUNK;
260+
let out = scratch;
261+
let inOff = 0;
262+
let availIn = chunk.byteLength;
263+
let buffers = null;
264+
let nread = 0;
265+
266+
while (true) {
267+
handle.writeSync(flushFlag, chunk, inOff, availIn, out, 0, chunkSize);
268+
if (handle[kError] !== undefined)
269+
throw handle[kError];
270+
271+
const availOutAfter = writeState[0];
272+
const availInAfter = writeState[1];
273+
const have = chunkSize - availOutAfter;
274+
if (have > 0) {
275+
nread += have;
276+
if (nread > kMaxLength)
277+
throw new ERR_BUFFER_TOO_LARGE(kMaxLength);
278+
if (out !== scratch) {
279+
buffers.push(out.subarray(0, have));
280+
} else if (availOutAfter === 0) {
281+
buffers = [out];
282+
}
283+
}
284+
if (availOutAfter !== 0)
285+
break;
286+
287+
// The output buffer is full. Continue with the rest of the input.
288+
inOff += availIn - availInAfter;
289+
availIn = availInAfter;
290+
out = Buffer.allocUnsafe(chunkSize);
291+
}
292+
293+
if (nread === 0)
294+
return new FastBuffer();
295+
if (buffers === null) {
296+
// All of the output is in the scratch buffer.
297+
const result = Buffer.allocUnsafe(nread);
298+
scratch.copy(result, 0, 0, nread);
299+
return result;
300+
}
301+
return Buffer.concat(buffers, nread);
302+
}
303+
218304
const FLUSH_BOUND = [
219305
[ Z_NO_FLUSH, Z_BLOCK ],
220306
[ BROTLI_OPERATION_PROCESS, BROTLI_OPERATION_EMIT_METADATA ],
@@ -830,6 +916,8 @@ ObjectSetPrototypeOf(Unzip, Zlib);
830916
function createConvenienceMethod(ctor, sync, prepareOpts) {
831917
if (sync) {
832918
return function syncBufferWrapper(buffer, opts) {
919+
if (opts === undefined)
920+
return zlibBufferSyncWithDefaults(ctor, buffer);
833921
return zlibBufferSync(new ctor(opts), buffer);
834922
};
835923
}
@@ -1039,6 +1127,61 @@ class ZstdDecompress extends Zstd {
10391127
}
10401128
}
10411129

1130+
const kEngineModes = new SafeMap([
1131+
[Deflate, DEFLATE],
1132+
[Inflate, INFLATE],
1133+
[Gzip, GZIP],
1134+
[Gunzip, GUNZIP],
1135+
[DeflateRaw, DEFLATERAW],
1136+
[InflateRaw, INFLATERAW],
1137+
[Unzip, UNZIP],
1138+
[BrotliCompress, BROTLI_ENCODE],
1139+
[BrotliDecompress, BROTLI_DECODE],
1140+
[ZstdCompress, ZSTD_COMPRESS],
1141+
[ZstdDecompress, ZSTD_DECOMPRESS],
1142+
]);
1143+
1144+
// Creates the native handle that the engine for `mode` creates when its
1145+
// constructor gets no options.
1146+
function createDefaultHandle(mode, writeState) {
1147+
let handle;
1148+
switch (mode) {
1149+
case BROTLI_ENCODE:
1150+
case BROTLI_DECODE:
1151+
handle = mode === BROTLI_DECODE ?
1152+
new binding.BrotliDecoder(mode) : new binding.BrotliEncoder(mode);
1153+
brotliInitParamsArray.fill(-1);
1154+
handle.init(brotliInitParamsArray, writeState, processCallback, undefined);
1155+
return handle;
1156+
case ZSTD_COMPRESS:
1157+
zstdInitCParamsArray.fill(-1);
1158+
handle = new binding.ZstdCompress();
1159+
handle.init(zstdInitCParamsArray, undefined, writeState, processCallback,
1160+
undefined, false);
1161+
return handle;
1162+
case ZSTD_DECOMPRESS:
1163+
zstdInitDParamsArray.fill(-1);
1164+
handle = new binding.ZstdDecompress();
1165+
handle.init(zstdInitDParamsArray, undefined, writeState, processCallback,
1166+
undefined, false);
1167+
return handle;
1168+
default:
1169+
handle = new binding.Zlib(mode);
1170+
handle.init(Z_DEFAULT_WINDOWBITS, Z_DEFAULT_COMPRESSION,
1171+
Z_DEFAULT_MEMLEVEL, Z_DEFAULT_STRATEGY, writeState,
1172+
processCallback, undefined, false);
1173+
return handle;
1174+
}
1175+
}
1176+
1177+
function defaultFinishFlush(mode) {
1178+
if (mode === BROTLI_ENCODE || mode === BROTLI_DECODE)
1179+
return brotliDefaultOpts.finishFlush;
1180+
if (mode === ZSTD_COMPRESS || mode === ZSTD_DECOMPRESS)
1181+
return zstdDefaultOpts.finishFlush;
1182+
return zlibDefaultOpts.finishFlush;
1183+
}
1184+
10421185
function createProperty(ctor) {
10431186
return {
10441187
__proto__: null,
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
'use strict';
2+
// Synchronous convenience methods called without options skip the stream and
3+
// reuse an internal output buffer. Check that they behave exactly like calls
4+
// that pass an empty options object, which go through the stream.
5+
6+
const common = require('../common');
7+
const assert = require('assert');
8+
const async_hooks = require('async_hooks');
9+
const fs = require('fs');
10+
const zlib = require('zlib');
11+
12+
const { Z_DEFAULT_CHUNK } = zlib.constants;
13+
14+
const pairs = [
15+
['deflateSync', 'inflateSync'],
16+
['gzipSync', 'gunzipSync'],
17+
['gzipSync', 'unzipSync'],
18+
['deflateRawSync', 'inflateRawSync'],
19+
['brotliCompressSync', 'brotliDecompressSync'],
20+
['zstdCompressSync', 'zstdDecompressSync'],
21+
];
22+
23+
const random = Buffer.alloc(3 * Z_DEFAULT_CHUNK);
24+
{
25+
let seed = 0x9e3779b9;
26+
for (let i = 0; i < random.length; i++) {
27+
seed ^= seed << 13;
28+
seed ^= seed >>> 17;
29+
seed ^= seed << 5;
30+
random[i] = seed & 0xff;
31+
}
32+
}
33+
const text = fs.readFileSync(__filename);
34+
35+
// Outputs that fit in one output buffer, fill it exactly, and span several.
36+
const inputs = [
37+
Buffer.alloc(0),
38+
Buffer.from('hello'),
39+
text,
40+
Buffer.alloc(Z_DEFAULT_CHUNK, 'a'),
41+
Buffer.alloc(Z_DEFAULT_CHUNK + 1, 'b'),
42+
Buffer.concat([text, random, text]),
43+
];
44+
45+
for (const [compress, decompress] of pairs) {
46+
for (const input of inputs) {
47+
const label = `${compress}/${decompress} ${input.length}`;
48+
const compressed = zlib[compress](input);
49+
assert.ok(Buffer.isBuffer(compressed), label);
50+
assert.deepStrictEqual(compressed, zlib[compress](input, {}), label);
51+
52+
const decompressed = zlib[decompress](compressed);
53+
assert.ok(Buffer.isBuffer(decompressed), label);
54+
assert.deepStrictEqual(decompressed, input, label);
55+
assert.deepStrictEqual(decompressed, zlib[decompress](compressed, {}),
56+
label);
57+
}
58+
59+
// Every input type.
60+
const expected = zlib[decompress](zlib[compress]('blah'.repeat(8)));
61+
for (const view of [
62+
'blah'.repeat(8),
63+
...common.getBufferSources(Buffer.from('blah'.repeat(8))),
64+
]) {
65+
assert.deepStrictEqual(zlib[decompress](zlib[compress](view)), expected);
66+
}
67+
}
68+
69+
// Results never share memory with each other.
70+
{
71+
const a = zlib.deflateSync('first');
72+
const b = zlib.deflateSync('second');
73+
const first = zlib.inflateSync(a);
74+
const second = zlib.inflateSync(b);
75+
assert.strictEqual(first.toString(), 'first');
76+
assert.strictEqual(second.toString(), 'second');
77+
second.fill(0);
78+
assert.strictEqual(first.toString(), 'first');
79+
assert.strictEqual(zlib.inflateSync(a).toString(), 'first');
80+
}
81+
82+
// Errors are the same as through the stream.
83+
for (const [method, input] of [
84+
['inflateSync', Buffer.from('not compressed')],
85+
['gunzipSync', Buffer.from('not compressed')],
86+
['unzipSync', Buffer.from('not compressed')],
87+
['inflateRawSync', Buffer.from('not compressed')],
88+
['brotliDecompressSync', Buffer.from('not compressed')],
89+
['zstdDecompressSync', Buffer.from('not compressed')],
90+
['inflateSync', zlib.deflateSync(text).subarray(0, 100)],
91+
['gunzipSync', zlib.gzipSync(text).subarray(0, 100)],
92+
['brotliDecompressSync', zlib.brotliCompressSync(text).subarray(0, 100)],
93+
['zstdDecompressSync', zlib.zstdCompressSync(text).subarray(0, 100)],
94+
]) {
95+
let expected;
96+
assert.throws(() => zlib[method](input, {}), (err) => {
97+
expected = err;
98+
return true;
99+
});
100+
assert.throws(() => zlib[method](input), (err) => {
101+
assert.strictEqual(err.constructor, expected.constructor);
102+
assert.strictEqual(err.message, expected.message);
103+
assert.strictEqual(err.code, expected.code);
104+
assert.strictEqual(err.errno, expected.errno);
105+
return true;
106+
}, method);
107+
}
108+
109+
for (const [compress] of pairs) {
110+
assert.throws(() => zlib[compress](42), {
111+
code: 'ERR_INVALID_ARG_TYPE',
112+
name: 'TypeError',
113+
});
114+
}
115+
116+
// A synchronous call made from an async hook while another one is running.
117+
{
118+
const compressed = zlib.deflateSync(text);
119+
let nested;
120+
const hook = async_hooks.createHook({
121+
before() {
122+
if (nested === undefined) {
123+
nested = null;
124+
nested = zlib.inflateSync(compressed);
125+
}
126+
},
127+
}).enable();
128+
// The error is reported through a callback into JavaScript, which runs the
129+
// before hook.
130+
assert.throws(() => zlib.inflateSync(Buffer.from('not compressed')), {
131+
code: 'Z_DATA_ERROR',
132+
});
133+
hook.disable();
134+
assert.deepStrictEqual(nested, text);
135+
assert.deepStrictEqual(zlib.inflateSync(compressed), text);
136+
}

0 commit comments

Comments
 (0)