summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ext/node/polyfills/_brotli.js65
1 files changed, 44 insertions, 21 deletions
diff --git a/ext/node/polyfills/_brotli.js b/ext/node/polyfills/_brotli.js
index 8b1f53012..1524bf85c 100644
--- a/ext/node/polyfills/_brotli.js
+++ b/ext/node/polyfills/_brotli.js
@@ -1,9 +1,18 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
-// TODO(petamoriken): enable prefer-primordials for node polyfills
-// deno-lint-ignore-file prefer-primordials
-
-import { core } from "ext:core/mod.js";
+import { core, primordials } from "ext:core/mod.js";
+const {
+ Uint8Array,
+ PromisePrototypeThen,
+ PromisePrototypeCatch,
+ ObjectValues,
+ TypedArrayPrototypeSlice,
+ TypedArrayPrototypeSubarray,
+ TypedArrayPrototypeGetByteLength,
+ DataViewPrototypeGetBuffer,
+ TypedArrayPrototypeGetBuffer,
+} = primordials;
+const { isTypedArray, isDataView, close } = core;
import {
op_brotli_compress,
op_brotli_compress_async,
@@ -28,8 +37,10 @@ const toU8 = (input) => {
return enc.encode(input);
}
- if (input.buffer) {
- return new Uint8Array(input.buffer);
+ if (isTypedArray(input)) {
+ return new Uint8Array(TypedArrayPrototypeGetBuffer(input));
+ } else if (isDataView(input)) {
+ return new Uint8Array(DataViewPrototypeGetBuffer(input));
}
return input;
@@ -52,18 +63,20 @@ export class BrotliDecompress extends Transform {
// TODO(littledivy): use `encoding` argument
transform(chunk, _encoding, callback) {
const input = toU8(chunk);
- const output = new Uint8Array(chunk.byteLength);
+ const output = new Uint8Array(TypedArrayPrototypeGetByteLength(chunk));
const avail = op_brotli_decompress_stream(context, input, output);
- this.push(output.slice(0, avail));
+ // deno-lint-ignore prefer-primordials
+ this.push(TypedArrayPrototypeSlice(output, 0, avail));
callback();
},
flush(callback) {
const output = new Uint8Array(1024);
let avail;
while ((avail = op_brotli_decompress_stream_end(context, output)) > 0) {
- this.push(output.slice(0, avail));
+ // deno-lint-ignore prefer-primordials
+ this.push(TypedArrayPrototypeSlice(output, 0, avail));
}
- core.close(context);
+ close(context);
callback();
},
});
@@ -84,7 +97,8 @@ export class BrotliCompress extends Transform {
const output = new Uint8Array(brotliMaxCompressedSize(input.length));
const written = op_brotli_compress_stream(context, input, output);
if (written > 0) {
- this.push(output.slice(0, written));
+ // deno-lint-ignore prefer-primordials
+ this.push(TypedArrayPrototypeSlice(output, 0, written));
}
callback();
},
@@ -92,14 +106,15 @@ export class BrotliCompress extends Transform {
const output = new Uint8Array(1024);
let avail;
while ((avail = op_brotli_compress_stream_end(context, output)) > 0) {
- this.push(output.slice(0, avail));
+ // deno-lint-ignore prefer-primordials
+ this.push(TypedArrayPrototypeSlice(output, 0, avail));
}
- core.close(context);
+ close(context);
callback();
},
});
- const params = Object.values(options?.params ?? {});
+ const params = ObjectValues(options?.params ?? {});
this.#context = op_create_brotli_compress(params);
const context = this.#context;
}
@@ -144,9 +159,13 @@ export function brotliCompress(
}
const { quality, lgwin, mode } = oneOffCompressOptions(options);
- op_brotli_compress_async(buf, quality, lgwin, mode)
- .then((result) => callback(null, Buffer.from(result)))
- .catch((err) => callback(err));
+ PromisePrototypeCatch(
+ PromisePrototypeThen(
+ op_brotli_compress_async(buf, quality, lgwin, mode),
+ (result) => callback(null, Buffer.from(result)),
+ ),
+ (err) => callback(err),
+ );
}
export function brotliCompressSync(
@@ -158,14 +177,18 @@ export function brotliCompressSync(
const { quality, lgwin, mode } = oneOffCompressOptions(options);
const len = op_brotli_compress(buf, output, quality, lgwin, mode);
- return Buffer.from(output.subarray(0, len));
+ return Buffer.from(TypedArrayPrototypeSubarray(output, 0, len));
}
export function brotliDecompress(input) {
const buf = toU8(input);
- return op_brotli_decompress_async(buf)
- .then((result) => callback(null, Buffer.from(result)))
- .catch((err) => callback(err));
+ return PromisePrototypeCatch(
+ PromisePrototypeThen(
+ op_brotli_decompress_async(buf),
+ (result) => callback(null, Buffer.from(result)),
+ ),
+ (err) => callback(err),
+ );
}
export function brotliDecompressSync(input) {