Skip to content

Commit b3a3d84

Browse files
authored
fix(node:zlib): gzip & gzipSync should accept ArrayBuffer (denoland#26762)
Closes denoland#26638
1 parent 700f54a commit b3a3d84

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

ext/node/polyfills/_zlib.mjs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { nextTick } from "ext:deno_node/_next_tick.ts";
1414
import {
1515
isAnyArrayBuffer,
1616
isArrayBufferView,
17+
isUint8Array,
1718
} from "ext:deno_node/internal/util/types.ts";
1819

1920
var kRangeErrorMessage = "Cannot create final Buffer. It would be larger " +
@@ -158,6 +159,12 @@ export const inflateRawSync = function (buffer, opts) {
158159
function sanitizeInput(input) {
159160
if (typeof input === "string") input = Buffer.from(input);
160161

162+
if (isArrayBufferView(input) && !isUint8Array(input)) {
163+
input = Buffer.from(input.buffer, input.byteOffset, input.byteLength);
164+
} else if (isAnyArrayBuffer(input)) {
165+
input = Buffer.from(input);
166+
}
167+
161168
if (
162169
!Buffer.isBuffer(input) &&
163170
(input.buffer && !input.buffer.constructor === ArrayBuffer)

tests/unit_node/zlib_test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
createBrotliCompress,
1111
createBrotliDecompress,
1212
createDeflate,
13+
gzip,
1314
gzipSync,
1415
unzipSync,
1516
} from "node:zlib";
@@ -210,3 +211,17 @@ Deno.test("createBrotliCompress params", async () => {
210211
);
211212
assertEquals(output.length, input.length);
212213
});
214+
215+
Deno.test("gzip() and gzipSync() accept ArrayBuffer", async () => {
216+
const deffered = Promise.withResolvers<void>();
217+
const buf = new ArrayBuffer(0);
218+
let output: Buffer;
219+
gzip(buf, (_err, data) => {
220+
output = data;
221+
deffered.resolve();
222+
});
223+
await deffered.promise;
224+
assert(output! instanceof Buffer);
225+
const outputSync = gzipSync(buf);
226+
assert(outputSync instanceof Buffer);
227+
});

0 commit comments

Comments
 (0)