Skip to content

Commit

Permalink
fix(node/zlib): accept dataview and buffer in zlib bindings (#21756)
Browse files Browse the repository at this point in the history
Fixes #20516 
Follow up to #21747 and #21746

This tackles the last point of #20516 where certain inputs weren't
accepted in the other zlib methods

This adds the `toU8` conversion of `_brotli` to `_zlib.mjs`, when we
create the ZLibBuffer, we'll sanitize the input. I noticed that the
async had no handler for `string` input so I added that as well.
  • Loading branch information
JoviDeCroock authored and bartlomieju committed Jan 4, 2024
1 parent 2d561a8 commit 53c876f
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 3 deletions.
30 changes: 30 additions & 0 deletions cli/tests/unit_node/zlib_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import {
createBrotliCompress,
createBrotliDecompress,
createDeflate,
gzipSync,
unzipSync,
} from "node:zlib";
import { Buffer } from "node:buffer";
import { createReadStream, createWriteStream } from "node:fs";
Expand All @@ -32,6 +34,13 @@ Deno.test("brotli compression async", async () => {
assertEquals(decompressed.toString(), "hello world");
});

Deno.test("gzip compression sync", { sanitizeResources: false }, () => {
const buf = Buffer.from("hello world");
const compressed = gzipSync(buf);
const decompressed = unzipSync(compressed);
assertEquals(decompressed.toString(), "hello world");
});

Deno.test("brotli compression", async () => {
const { promise, resolve } = Promise.withResolvers<void>();
const compress = createBrotliCompress();
Expand Down Expand Up @@ -125,3 +134,24 @@ Deno.test("should work with a buffer from an encoded string", () => {
const decompressed = brotliDecompressSync(compressed);
assertEquals(decompressed.toString(), "hello world");
});

Deno.test(
"zlib compression with dataview",
{ sanitizeResources: false },
() => {
const buf = Buffer.from("hello world");
const compressed = gzipSync(new DataView(buf.buffer));
const decompressed = unzipSync(compressed);
assertEquals(decompressed.toString(), "hello world");
},
);

Deno.test("zlib compression with an encoded string", {
sanitizeResources: false,
}, () => {
const encoder = new TextEncoder();
const buffer = encoder.encode("hello world");
const compressed = gzipSync(buffer);
const decompressed = unzipSync(compressed);
assertEquals(decompressed.toString(), "hello world");
});
21 changes: 18 additions & 3 deletions ext/node/polyfills/_zlib.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,27 @@ export const inflateRawSync = function (buffer, opts) {
return zlibBufferSync(new InflateRaw(opts), buffer);
};

function sanitizeInput(input) {
if (typeof input === "string") input = Buffer.from(input);

if (
!Buffer.isBuffer(input) &&
(input.buffer && !input.buffer.constructor === ArrayBuffer)
) throw new TypeError("Not a string, buffer or dataview");

if (input.buffer) {
input = new Uint8Array(input.buffer, input.byteOffset, input.byteLength);
}

return input;
}

function zlibBuffer(engine, buffer, callback) {
var buffers = [];
var nread = 0;

buffer = sanitizeInput(buffer);

engine.on("error", onError);
engine.on("end", onEnd);

Expand Down Expand Up @@ -197,9 +214,7 @@ function zlibBuffer(engine, buffer, callback) {
}

function zlibBufferSync(engine, buffer) {
if (typeof buffer === "string") buffer = Buffer.from(buffer);

if (!Buffer.isBuffer(buffer)) throw new TypeError("Not a string or buffer");
buffer = sanitizeInput(buffer);

var flushFlag = engine._finishFlushFlag;

Expand Down

0 comments on commit 53c876f

Please sign in to comment.