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 committed by GitHub
parent 65db8814c3
commit 2f00b0add4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 36 additions and 7 deletions

View file

@ -11,6 +11,7 @@ import {
brotliDecompressSync, brotliDecompressSync,
createBrotliCompress, createBrotliCompress,
createBrotliDecompress, createBrotliDecompress,
createDeflate,
} from "node:zlib"; } from "node:zlib";
import { Buffer } from "node:buffer"; import { Buffer } from "node:buffer";
import { createReadStream, createWriteStream } from "node:fs"; import { createReadStream, createWriteStream } from "node:fs";
@ -60,3 +61,20 @@ Deno.test("brotli compression", async () => {
// pass // 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, window_bits: i32,
mem_level: i32, mem_level: i32,
strategy: i32, strategy: i32,
dictionary: Option<&[u8]>, dictionary: &[u8],
) -> Result<i32, AnyError> { ) -> Result<i32, AnyError> {
let resource = zlib(state, handle)?; let resource = zlib(state, handle)?;
let mut zlib = resource.inner.borrow_mut(); let mut zlib = resource.inner.borrow_mut();
@ -373,7 +373,11 @@ pub fn op_zlib_init(
zlib.init_stream()?; 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) Ok(zlib.err)
} }

View file

@ -11,6 +11,10 @@ import util from "node:util";
import { ok as assert } from "node:assert"; import { ok as assert } from "node:assert";
import { zlib as zlibConstants } from "ext:deno_node/internal_binding/constants.ts"; import { zlib as zlibConstants } from "ext:deno_node/internal_binding/constants.ts";
import { nextTick } from "ext:deno_node/_next_tick.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 " + var kRangeErrorMessage = "Cannot create final Buffer. It would be larger " +
"than 0x" + kMaxLength.toString(16) + " bytes"; "than 0x" + kMaxLength.toString(16) + " bytes";
@ -321,9 +325,12 @@ function Zlib(opts, mode) {
} }
} }
if (opts.dictionary) { let dictionary = opts.dictionary;
if (!Buffer.isBuffer(opts.dictionary)) { if (dictionary !== undefined && !isArrayBufferView(dictionary)) {
throw new Error("Invalid dictionary: it should be a Buffer instance"); if (isAnyArrayBuffer(dictionary)) {
dictionary = Buffer.from(dictionary);
} else {
throw new TypeError("Invalid dictionary");
} }
} }
@ -354,7 +361,7 @@ function Zlib(opts, mode) {
level, level,
opts.memLevel || zlibConstants.Z_DEFAULT_MEMLEVEL, opts.memLevel || zlibConstants.Z_DEFAULT_MEMLEVEL,
strategy, strategy,
opts.dictionary, dictionary,
); );
this._buffer = Buffer.allocUnsafe(this._chunkSize); this._buffer = Buffer.allocUnsafe(this._chunkSize);

View file

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