1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-24 15:19:26 -05:00
denoland-deno/ext/web/14_compression.js
Bartek Iwańczuk c1fc7b2cd5
refactor: pull 'core', 'internals', 'primordials' from ES module (#21462)
This commit refactors how we access "core", "internals" and
"primordials" objects coming from `deno_core`, in our internal JavaScript code.

Instead of capturing them from "globalThis.__bootstrap" namespace, we
import them from recently added "ext:core/mod.js" file.
2023-12-07 14:21:01 +01:00

152 lines
3.9 KiB
JavaScript

// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
// @ts-check
/// <reference path="../../core/lib.deno_core.d.ts" />
/// <reference path="./internal.d.ts" />
/// <reference path="./lib.deno_web.d.ts" />
import { core, primordials } from "ext:core/mod.js";
const ops = core.ops;
const {
SymbolFor,
ObjectPrototypeIsPrototypeOf,
TypedArrayPrototypeGetByteLength,
} = primordials;
import * as webidl from "ext:deno_webidl/00_webidl.js";
import { createFilteredInspectProxy } from "ext:deno_console/01_console.js";
import { TransformStream } from "ext:deno_web/06_streams.js";
webidl.converters.CompressionFormat = webidl.createEnumConverter(
"CompressionFormat",
[
"deflate",
"deflate-raw",
"gzip",
],
);
class CompressionStream {
#transform;
constructor(format) {
const prefix = "Failed to construct 'CompressionStream'";
webidl.requiredArguments(arguments.length, 1, prefix);
format = webidl.converters.CompressionFormat(format, prefix, "Argument 1");
const rid = ops.op_compression_new(format, false);
this.#transform = new TransformStream({
transform(chunk, controller) {
chunk = webidl.converters.BufferSource(chunk, prefix, "chunk");
const output = ops.op_compression_write(
rid,
chunk,
);
maybeEnqueue(controller, output);
},
flush(controller) {
const output = ops.op_compression_finish(rid);
maybeEnqueue(controller, output);
},
});
this[webidl.brand] = webidl.brand;
}
get readable() {
webidl.assertBranded(this, CompressionStreamPrototype);
return this.#transform.readable;
}
get writable() {
webidl.assertBranded(this, CompressionStreamPrototype);
return this.#transform.writable;
}
[SymbolFor("Deno.privateCustomInspect")](inspect, inspectOptions) {
return inspect(
createFilteredInspectProxy({
object: this,
evaluate: ObjectPrototypeIsPrototypeOf(
CompressionStreamPrototype,
this,
),
keys: [
"readable",
"writable",
],
}),
inspectOptions,
);
}
}
webidl.configureInterface(CompressionStream);
const CompressionStreamPrototype = CompressionStream.prototype;
class DecompressionStream {
#transform;
constructor(format) {
const prefix = "Failed to construct 'DecompressionStream'";
webidl.requiredArguments(arguments.length, 1, prefix);
format = webidl.converters.CompressionFormat(format, prefix, "Argument 1");
const rid = ops.op_compression_new(format, true);
this.#transform = new TransformStream({
transform(chunk, controller) {
chunk = webidl.converters.BufferSource(chunk, prefix, "chunk");
const output = ops.op_compression_write(
rid,
chunk,
);
maybeEnqueue(controller, output);
},
flush(controller) {
const output = ops.op_compression_finish(rid);
maybeEnqueue(controller, output);
},
});
this[webidl.brand] = webidl.brand;
}
get readable() {
webidl.assertBranded(this, DecompressionStreamPrototype);
return this.#transform.readable;
}
get writable() {
webidl.assertBranded(this, DecompressionStreamPrototype);
return this.#transform.writable;
}
[SymbolFor("Deno.privateCustomInspect")](inspect, inspectOptions) {
return inspect(
createFilteredInspectProxy({
object: this,
evaluate: ObjectPrototypeIsPrototypeOf(
DecompressionStreamPrototype,
this,
),
keys: [
"readable",
"writable",
],
}),
inspectOptions,
);
}
}
function maybeEnqueue(controller, output) {
if (output && TypedArrayPrototypeGetByteLength(output) > 0) {
controller.enqueue(output);
}
}
webidl.configureInterface(DecompressionStream);
const DecompressionStreamPrototype = DecompressionStream.prototype;
export { CompressionStream, DecompressionStream };