Skip to content

Commit

Permalink
fix(ext/node): support dictionary option in zlib init (#20035)
Browse files Browse the repository at this point in the history
Fixes #19540
  • Loading branch information
littledivy committed Aug 21, 2023
1 parent 8e060e7 commit 3615afa
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 7 deletions.
18 changes: 18 additions & 0 deletions cli/tests/unit_node/zlib_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
brotliDecompressSync,
createBrotliCompress,
createBrotliDecompress,
createDeflate,
} from "node:zlib";
import { Buffer } from "node:buffer";
import { createReadStream, createWriteStream } from "node:fs";
Expand Down Expand Up @@ -60,3 +61,20 @@ Deno.test("brotli compression", async () => {
// pass
}
});

Deno.test(
"zlib create deflate with dictionary",
{ sanitizeResources: false },
async () => {
const promise = deferred();
const handle = createDeflate({
dictionary: Buffer.alloc(0),
});

handle.on("close", () => promise.resolve());
handle.end();
handle.destroy();

await promise;
},
);
8 changes: 6 additions & 2 deletions ext/node/ops/zlib/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ pub fn op_zlib_init(
window_bits: i32,
mem_level: i32,
strategy: i32,
dictionary: Option<&[u8]>,
dictionary: &[u8],
) -> Result<i32, AnyError> {
let resource = zlib(state, handle)?;
let mut zlib = resource.inner.borrow_mut();
Expand Down Expand Up @@ -373,7 +373,11 @@ pub fn op_zlib_init(

zlib.init_stream()?;

zlib.dictionary = dictionary.map(|buf| buf.to_vec());
zlib.dictionary = if !dictionary.is_empty() {
Some(dictionary.to_vec())
} else {
None
};

Ok(zlib.err)
}
Expand Down
15 changes: 11 additions & 4 deletions ext/node/polyfills/_zlib.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ import util from "node:util";
import { ok as assert } from "node:assert";
import { zlib as zlibConstants } from "ext:deno_node/internal_binding/constants.ts";
import { nextTick } from "ext:deno_node/_next_tick.ts";
import {
isAnyArrayBuffer,
isArrayBufferView,
} from "ext:deno_node/internal/util/types.ts";

var kRangeErrorMessage = "Cannot create final Buffer. It would be larger " +
"than 0x" + kMaxLength.toString(16) + " bytes";
Expand Down Expand Up @@ -321,9 +325,12 @@ function Zlib(opts, mode) {
}
}

if (opts.dictionary) {
if (!Buffer.isBuffer(opts.dictionary)) {
throw new Error("Invalid dictionary: it should be a Buffer instance");
let dictionary = opts.dictionary;
if (dictionary !== undefined && !isArrayBufferView(dictionary)) {
if (isAnyArrayBuffer(dictionary)) {
dictionary = Buffer.from(dictionary);
} else {
throw new TypeError("Invalid dictionary");
}
}

Expand Down Expand Up @@ -354,7 +361,7 @@ function Zlib(opts, mode) {
level,
opts.memLevel || zlibConstants.Z_DEFAULT_MEMLEVEL,
strategy,
opts.dictionary,
dictionary,
);

this._buffer = Buffer.allocUnsafe(this._chunkSize);
Expand Down
2 changes: 1 addition & 1 deletion ext/node/polyfills/_zlib_binding.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ class Zlib {
windowBits,
memLevel,
strategy,
dictionary,
dictionary ?? new Uint8Array(0),
);

if (err != Z_OK) {
Expand Down

0 comments on commit 3615afa

Please sign in to comment.