mirror of
https://github.com/denoland/deno.git
synced 2024-11-21 15:04:11 -05:00
fix(node/zlib): cast Dataview and Buffer to uint8 (#21746)
This fixes point 2 of #20516 This adds a conversion from Dataview/Buffer by returning `obj.buffer` which can be converted to a `UInt8Array`. Question: Regarding point 4 of the mentioned issue would it be appropriate to copy the toU8 helper to the `zlib.mjs` methods?
This commit is contained in:
parent
3db9c8742d
commit
08fc8d50e3
2 changed files with 20 additions and 0 deletions
|
@ -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");
|
||||
});
|
||||
|
|
|
@ -19,6 +19,10 @@ const toU8 = (input) => {
|
|||
return enc.encode(input);
|
||||
}
|
||||
|
||||
if (input.buffer) {
|
||||
return new Uint8Array(input.buffer);
|
||||
}
|
||||
|
||||
return input;
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in a new issue