From 9c2fa4d5e790bb9cbd9fa72dbaa10da3baca03de Mon Sep 17 00:00:00 2001 From: Jovi De Croock Date: Sun, 31 Dec 2023 08:50:55 +0100 Subject: [PATCH] try to cast everything to a uint8 --- cli/tests/unit_node/zlib_test.ts | 16 ++++++++++++++++ ext/node/polyfills/_brotli.js | 4 ++++ 2 files changed, 20 insertions(+) diff --git a/cli/tests/unit_node/zlib_test.ts b/cli/tests/unit_node/zlib_test.ts index fc9eaeb5bf1696..957c7cdfcd984f 100644 --- a/cli/tests/unit_node/zlib_test.ts +++ b/cli/tests/unit_node/zlib_test.ts @@ -96,3 +96,19 @@ Deno.test( handle.destroy(); }, ); + +Deno.test("should work with dataview", () => { + const buf = Buffer.from("hello world"); + const compressed = brotliCompressSync(new DataView(buf.buffer)); + const decompressed = brotliDecompressSync(compressed); + assertEquals(decompressed.toString(), "hello world"); +}); + +Deno.test("should work with a buffer from an encoded string", () => { + const encoder = new TextEncoder(); + const buffer = encoder.encode("hello world"); + const buf = Buffer.from(buffer); + const compressed = brotliCompressSync(buf); + const decompressed = brotliDecompressSync(compressed); + assertEquals(decompressed.toString(), "hello world"); +}); diff --git a/ext/node/polyfills/_brotli.js b/ext/node/polyfills/_brotli.js index bf099759b3837c..cd54eedda82497 100644 --- a/ext/node/polyfills/_brotli.js +++ b/ext/node/polyfills/_brotli.js @@ -19,6 +19,10 @@ const toU8 = (input) => { return enc.encode(input); } + if (input.buffer) { + return new Uint8Array(input.buffer); + } + return input; };