From 2f00b0add476bb151bc3a713da165296906cfc2a Mon Sep 17 00:00:00 2001 From: Divy Srivastava Date: Fri, 11 Aug 2023 17:12:35 +0530 Subject: fix(ext/node): support dictionary option in zlib init (#20035) Fixes https://github.com/denoland/deno/issues/19540 --- ext/node/polyfills/_zlib.mjs | 15 +++++++++++---- ext/node/polyfills/_zlib_binding.mjs | 2 +- 2 files changed, 12 insertions(+), 5 deletions(-) (limited to 'ext/node/polyfills') diff --git a/ext/node/polyfills/_zlib.mjs b/ext/node/polyfills/_zlib.mjs index f73365800..a66ab6d04 100644 --- a/ext/node/polyfills/_zlib.mjs +++ b/ext/node/polyfills/_zlib.mjs @@ -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); diff --git a/ext/node/polyfills/_zlib_binding.mjs b/ext/node/polyfills/_zlib_binding.mjs index a04e7fed7..0b155cfd5 100644 --- a/ext/node/polyfills/_zlib_binding.mjs +++ b/ext/node/polyfills/_zlib_binding.mjs @@ -149,7 +149,7 @@ class Zlib { windowBits, memLevel, strategy, - dictionary, + dictionary ?? new Uint8Array(0), ); if (err != Z_OK) { -- cgit v1.2.3