0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-03-03 17:34:47 -05:00

fix(ext/node): use primordials in ext/node/polyfills/_brotli.js (#24235)

Towards #24236
This commit is contained in:
Asher Gomez 2024-06-20 17:44:23 +10:00 committed by GitHub
parent 510db0a86e
commit 6ab143335a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

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