1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-24 08:09:08 -05:00

fix(ext/node): support dictionary option in zlib init (#20035)

Fixes https://github.com/denoland/deno/issues/19540
This commit is contained in:
Divy Srivastava 2023-08-11 17:12:35 +05:30
parent 8e060e7da8
commit 3615afa217
4 changed files with 36 additions and 7 deletions

View file

@ -11,6 +11,7 @@ import {
brotliDecompressSync,
createBrotliCompress,
createBrotliDecompress,
createDeflate,
} from "node:zlib";
import { Buffer } from "node:buffer";
import { createReadStream, createWriteStream } from "node:fs";
@ -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;
},
);

View file

@ -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();
@ -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)
}

View file

@ -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";
@ -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");
}
}
@ -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);

View file

@ -149,7 +149,7 @@ class Zlib {
windowBits,
memLevel,
strategy,
dictionary,
dictionary ?? new Uint8Array(0),
);
if (err != Z_OK) {