Skip to content

Commit a0d82d7

Browse files
committed
doc: document Brotli/iter dictionary option
Signed-off-by: islandryu <shimaryuhei@gmail.com>
1 parent 4f844f4 commit a0d82d7

2 files changed

Lines changed: 43 additions & 8 deletions

File tree

doc/api/zlib.md

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -870,6 +870,9 @@ Each Brotli-based class takes an `options` object. All options are optional.
870870
* `finishFlush` {integer} **Default:** `zlib.constants.BROTLI_OPERATION_FINISH`
871871
* `chunkSize` {integer} **Default:** `16 * 1024`
872872
* `params` {Object} Key-value object containing indexed [Brotli parameters][].
873+
* `dictionary` {Buffer|TypedArray|DataView|ArrayBuffer} Optional dictionary used
874+
to improve compression efficiency when compressing or decompressing data that
875+
shares common patterns with the dictionary.
873876
* `maxOutputLength` {integer} Limits output size when using
874877
[convenience methods][]. **Default:** [`buffer.kMaxLength`][]
875878
* `info` {boolean} If `true`, returns an object with `buffer` and `engine`. **Default:** `false`
@@ -1857,7 +1860,7 @@ added: v25.9.0
18571860
* `BROTLI_PARAM_LGBLOCK` -- input block size (log2).
18581861
See the [Brotli compressor options][] in the zlib documentation for the
18591862
full list.
1860-
* `dictionary` {Buffer|TypedArray|DataView}
1863+
* `dictionary` {Buffer|TypedArray|DataView|ArrayBuffer}
18611864
* Returns: {Object} A stateful transform.
18621865

18631866
Create a Brotli compression transform. Output is compatible with
@@ -1877,7 +1880,7 @@ added: v25.9.0
18771880
* `windowBits` {number} **Default:** `Z_DEFAULT_WINDOWBITS` (15).
18781881
* `memLevel` {number} **Default:** `9`.
18791882
* `strategy` {number} **Default:** `Z_DEFAULT_STRATEGY`.
1880-
* `dictionary` {Buffer|TypedArray|DataView}
1883+
* `dictionary` {Buffer|TypedArray|DataView|ArrayBuffer}
18811884
* Returns: {Object} A stateful transform.
18821885

18831886
Create a deflate compression transform. Output is compatible with
@@ -1897,7 +1900,7 @@ added: v25.9.0
18971900
* `windowBits` {number} **Default:** `Z_DEFAULT_WINDOWBITS` (15).
18981901
* `memLevel` {number} **Default:** `9`.
18991902
* `strategy` {number} **Default:** `Z_DEFAULT_STRATEGY`.
1900-
* `dictionary` {Buffer|TypedArray|DataView}
1903+
* `dictionary` {Buffer|TypedArray|DataView|ArrayBuffer}
19011904
* Returns: {Object} A stateful transform.
19021905

19031906
Create a gzip compression transform. Output is compatible with `zlib.gunzip()`
@@ -1924,7 +1927,7 @@ added: v25.9.0
19241927
See the [Zstd compressor options][] in the zlib documentation for the
19251928
full list.
19261929
* `pledgedSrcSize` {number} Expected uncompressed size (optional hint).
1927-
* `dictionary` {Buffer|TypedArray|DataView}
1930+
* `dictionary` {Buffer|TypedArray|DataView|ArrayBuffer}
19281931
* Returns: {Object} A stateful transform.
19291932

19301933
Create a Zstandard compression transform. Output is compatible with
@@ -1948,7 +1951,7 @@ added: v25.9.0
19481951
Window Brotli" mode (not compatible with [RFC 7932][]).
19491952
See the [Brotli decompressor options][] in the zlib documentation for
19501953
details.
1951-
* `dictionary` {Buffer|TypedArray|DataView}
1954+
* `dictionary` {Buffer|TypedArray|DataView|ArrayBuffer}
19521955
* Returns: {Object} A stateful transform.
19531956

19541957
Create a Brotli decompression transform.
@@ -1964,7 +1967,7 @@ added: v25.9.0
19641967
* `options` {Object}
19651968
* `chunkSize` {number} Output buffer size. **Default:** `65536` (64 KB).
19661969
* `windowBits` {number} **Default:** `Z_DEFAULT_WINDOWBITS` (15).
1967-
* `dictionary` {Buffer|TypedArray|DataView}
1970+
* `dictionary` {Buffer|TypedArray|DataView|ArrayBuffer}
19681971
* Returns: {Object} A stateful transform.
19691972

19701973
Create a deflate decompression transform.
@@ -1980,7 +1983,7 @@ added: v25.9.0
19801983
* `options` {Object}
19811984
* `chunkSize` {number} Output buffer size. **Default:** `65536` (64 KB).
19821985
* `windowBits` {number} **Default:** `Z_DEFAULT_WINDOWBITS` (15).
1983-
* `dictionary` {Buffer|TypedArray|DataView}
1986+
* `dictionary` {Buffer|TypedArray|DataView|ArrayBuffer}
19841987
* Returns: {Object} A stateful transform.
19851988

19861989
Create a gzip decompression transform.
@@ -2001,7 +2004,7 @@ added: v25.9.0
20012004
will allocate. Limits memory usage against malicious input.
20022005
See the [Zstd decompressor options][] in the zlib documentation for
20032006
details.
2004-
* `dictionary` {Buffer|TypedArray|DataView}
2007+
* `dictionary` {Buffer|TypedArray|DataView|ArrayBuffer}
20052008
* Returns: {Object} A stateful transform.
20062009

20072010
Create a Zstandard decompression transform.

test/parallel/test-stream-iter-transform-sync.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,37 @@ function testEmptyInput() {
204204
assert.strictEqual(result, '');
205205
}
206206

207+
// =============================================================================
208+
// Dictionary tests
209+
// =============================================================================
210+
211+
function testDictionaryAcceptsArrayBufferAndView() {
212+
const dict =
213+
Buffer.from('the quick brown fox jumps over the lazy dog '.repeat(4));
214+
const inputBuf = Buffer.from(dict);
215+
const arrayBufferDict = dict.buffer.slice(
216+
dict.byteOffset, dict.byteOffset + dict.byteLength);
217+
const dataViewDict = new DataView(arrayBufferDict);
218+
219+
for (const [compress, decompress] of [
220+
[compressBrotliSync, decompressBrotliSync],
221+
[compressZstdSync, decompressZstdSync],
222+
]) {
223+
const withBuffer = bytesSync(
224+
pullSync(fromSync(inputBuf), compress({ dictionary: dict }))).byteLength;
225+
226+
for (const dictionary of [arrayBufferDict, dataViewDict]) {
227+
const size = bytesSync(
228+
pullSync(fromSync(inputBuf), compress({ dictionary }))).byteLength;
229+
assert.strictEqual(size, withBuffer);
230+
231+
const result = roundTripBytes(inputBuf, compress({ dictionary }),
232+
decompress({ dictionary }));
233+
assert.deepStrictEqual(result, inputBuf);
234+
}
235+
}
236+
}
237+
207238
// =============================================================================
208239
// Run all tests
209240
// =============================================================================
@@ -223,5 +254,6 @@ testBrotliWithOptions();
223254
testMixedStatelessAndStateful();
224255
testEarlyExit();
225256
testEmptyInput();
257+
testDictionaryAcceptsArrayBufferAndView();
226258

227259
common.mustCall()();

0 commit comments

Comments
 (0)