fix(node:zlib): gzip & gzipSync should accept ArrayBuffer (#26762)

Closes https://github.com/denoland/deno/issues/26638
This commit is contained in:
Satya Rohith 2024-11-06 19:42:24 +05:30 committed by GitHub
parent 700f54a13c
commit b3a3d84ce2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 22 additions and 0 deletions

View File

@ -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)

View File

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