From b3a3d84ce249ff126f92e7a0849ec0a6ce26e973 Mon Sep 17 00:00:00 2001 From: Satya Rohith Date: Wed, 6 Nov 2024 19:42:24 +0530 Subject: [PATCH] fix(node:zlib): gzip & gzipSync should accept ArrayBuffer (#26762) Closes https://github.com/denoland/deno/issues/26638 --- ext/node/polyfills/_zlib.mjs | 7 +++++++ tests/unit_node/zlib_test.ts | 15 +++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/ext/node/polyfills/_zlib.mjs b/ext/node/polyfills/_zlib.mjs index 851bd602f2..07fc440ef5 100644 --- a/ext/node/polyfills/_zlib.mjs +++ b/ext/node/polyfills/_zlib.mjs @@ -14,6 +14,7 @@ import { nextTick } from "ext:deno_node/_next_tick.ts"; import { isAnyArrayBuffer, isArrayBufferView, + isUint8Array, } from "ext:deno_node/internal/util/types.ts"; var kRangeErrorMessage = "Cannot create final Buffer. It would be larger " + @@ -158,6 +159,12 @@ export const inflateRawSync = function (buffer, opts) { function sanitizeInput(input) { if (typeof input === "string") input = Buffer.from(input); + if (isArrayBufferView(input) && !isUint8Array(input)) { + input = Buffer.from(input.buffer, input.byteOffset, input.byteLength); + } else if (isAnyArrayBuffer(input)) { + input = Buffer.from(input); + } + if ( !Buffer.isBuffer(input) && (input.buffer && !input.buffer.constructor === ArrayBuffer) diff --git a/tests/unit_node/zlib_test.ts b/tests/unit_node/zlib_test.ts index 8bce5ce7d1..0eff95445d 100644 --- a/tests/unit_node/zlib_test.ts +++ b/tests/unit_node/zlib_test.ts @@ -10,6 +10,7 @@ import { createBrotliCompress, createBrotliDecompress, createDeflate, + gzip, gzipSync, unzipSync, } from "node:zlib"; @@ -210,3 +211,17 @@ Deno.test("createBrotliCompress params", async () => { ); assertEquals(output.length, input.length); }); + +Deno.test("gzip() and gzipSync() accept ArrayBuffer", async () => { + const deffered = Promise.withResolvers(); + const buf = new ArrayBuffer(0); + let output: Buffer; + gzip(buf, (_err, data) => { + output = data; + deffered.resolve(); + }); + await deffered.promise; + assert(output! instanceof Buffer); + const outputSync = gzipSync(buf); + assert(outputSync instanceof Buffer); +});