zlib: accept ArrayBuffer dictionary in Zstd

Fixes: https://github.com/nodejs/node/issues/64598
Signed-off-by: islandryu <shimaryuhei@gmail.com>
PR-URL: https://github.com/nodejs/node/pull/64599
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
This commit is contained in:
Ryuhei Shima authored and Antoine du Hamel committed 2026-08-04 18:40:44 +02:00
1 parent 72448a82f4
commit 44042c20d4
3 files changed
+33 -3

No files matched your search

+6 -2
View File
@@ -1099,6 +1099,10 @@ added:
- v23.8.0
- v22.15.0
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/64599
description: The `dictionary` option can be a `TypedArray`, `DataView`, or
`ArrayBuffer`.
- version: v26.5.0
pr-url: https://github.com/nodejs/node/pull/64023
description: The `rejectGarbageAfterEnd` option was added.
@@ -1115,8 +1119,8 @@ Each Zstd-based class takes an `options` object. All options are optional.
* `maxOutputLength` {integer} Limits output size when using
[convenience methods][]. **Default:** [`buffer.kMaxLength`][]
* `info` {boolean} If `true`, returns an object with `buffer` and `engine`. **Default:** `false`
* `dictionary` {Buffer} Optional dictionary used to
improve compression efficiency when compressing or decompressing data that
* `dictionary` {Buffer|TypedArray|DataView|ArrayBuffer} Optional dictionary used
to improve compression efficiency when compressing or decompressing data that
shares common patterns with the dictionary.
* `rejectGarbageAfterEnd` {boolean} If `true`, decompression fails when
input remains after the first complete compressed stream. **Default:** `false`
+11 -1
View File
@@ -33,6 +33,7 @@ const {
ObjectSetPrototypeOf,
Symbol,
Uint32Array,
Uint8Array,
} = primordials;
const {
@@ -926,6 +927,15 @@ class Zstd extends ZlibBase {
});
}
let dictionary = opts?.dictionary;
if (dictionary !== undefined && !isArrayBufferView(dictionary)) {
if (isAnyArrayBuffer(dictionary)) {
dictionary = new Uint8Array(dictionary);
} else {
dictionary = undefined;
}
}
const handle = mode === ZSTD_COMPRESS ?
new binding.ZstdCompress() : new binding.ZstdDecompress();
@@ -938,7 +948,7 @@ class Zstd extends ZlibBase {
pledgedSrcSize,
writeState,
processCallback,
opts?.dictionary && isArrayBufferView(opts.dictionary) ? opts.dictionary : undefined,
dictionary,
);
super(opts, mode, handle, zstdDefaultOpts);
@@ -24,3 +24,19 @@ zlib.zstdCompress(input, { dictionary }, common.mustSucceed((compressed) => {
assert.strictEqual(decompressed.toString(), input.toString());
}));
}));
const baseline = zlib.zstdCompressSync(input, { dictionary }).length;
const arrayBuffer = dictionary.buffer.slice(
dictionary.byteOffset, dictionary.byteOffset + dictionary.byteLength);
const uint8 = new Uint8Array(arrayBuffer);
const dataView = new DataView(arrayBuffer);
for (const dict of [arrayBuffer, uint8, dataView]) {
assert.strictEqual(zlib.zstdCompressSync(input, { dictionary: dict }).length,
baseline);
const compressed = zlib.zstdCompressSync(input, { dictionary: dict });
const decompressed = zlib.zstdDecompressSync(compressed, { dictionary: dict });
assert.strictEqual(decompressed.toString(), input.toString());
}