summaryrefslogtreecommitdiff
path: root/ext/node/polyfills/_zlib.mjs
diff options
context:
space:
mode:
authorJovi De Croock <decroockjovi@gmail.com>2024-01-03 14:33:51 +0100
committerGitHub <noreply@github.com>2024-01-03 19:03:51 +0530
commitf5ad15b5048ec34346cb86450c5cba0509b584db (patch)
treefcb7c0f71186e17ce688780fb0ed0880a143c2d8 /ext/node/polyfills/_zlib.mjs
parent59cfe20f3be727096d328ecb2196478ae01447f6 (diff)
fix(node/zlib): accept dataview and buffer in zlib bindings (#21756)
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.
Diffstat (limited to 'ext/node/polyfills/_zlib.mjs')
-rw-r--r--ext/node/polyfills/_zlib.mjs21
1 files changed, 18 insertions, 3 deletions
diff --git a/ext/node/polyfills/_zlib.mjs b/ext/node/polyfills/_zlib.mjs
index a66ab6d04..15a0a51e3 100644
--- a/ext/node/polyfills/_zlib.mjs
+++ b/ext/node/polyfills/_zlib.mjs
@@ -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);
@@ -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;