2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2022-01-24 12:03:06 -05:00
|
|
|
|
|
|
|
// @ts-check
|
|
|
|
/// <reference path="../../core/lib.deno_core.d.ts" />
|
|
|
|
/// <reference path="./internal.d.ts" />
|
|
|
|
/// <reference path="./lib.deno_web.d.ts" />
|
|
|
|
|
2023-12-07 08:21:01 -05:00
|
|
|
import { core, primordials } from "ext:core/mod.js";
|
2024-01-10 17:37:25 -05:00
|
|
|
const {
|
|
|
|
op_compression_finish,
|
|
|
|
op_compression_new,
|
|
|
|
op_compression_write,
|
|
|
|
} = core.ensureFastOps();
|
2023-04-02 13:41:41 -04:00
|
|
|
const {
|
2023-11-19 03:13:38 -05:00
|
|
|
SymbolFor,
|
|
|
|
ObjectPrototypeIsPrototypeOf,
|
2023-04-02 13:41:41 -04:00
|
|
|
TypedArrayPrototypeGetByteLength,
|
|
|
|
} = primordials;
|
2024-01-10 17:37:25 -05:00
|
|
|
|
2023-03-08 06:44:54 -05:00
|
|
|
import * as webidl from "ext:deno_webidl/00_webidl.js";
|
2023-11-19 03:13:38 -05:00
|
|
|
import { createFilteredInspectProxy } from "ext:deno_console/01_console.js";
|
2023-03-08 06:44:54 -05:00
|
|
|
import { TransformStream } from "ext:deno_web/06_streams.js";
|
2023-02-07 14:22:46 -05:00
|
|
|
|
|
|
|
webidl.converters.CompressionFormat = webidl.createEnumConverter(
|
|
|
|
"CompressionFormat",
|
|
|
|
[
|
|
|
|
"deflate",
|
|
|
|
"deflate-raw",
|
|
|
|
"gzip",
|
|
|
|
],
|
|
|
|
);
|
|
|
|
|
|
|
|
class CompressionStream {
|
|
|
|
#transform;
|
|
|
|
|
|
|
|
constructor(format) {
|
|
|
|
const prefix = "Failed to construct 'CompressionStream'";
|
2023-04-12 15:58:57 -04:00
|
|
|
webidl.requiredArguments(arguments.length, 1, prefix);
|
2023-05-01 06:47:13 -04:00
|
|
|
format = webidl.converters.CompressionFormat(format, prefix, "Argument 1");
|
2023-02-07 14:22:46 -05:00
|
|
|
|
2024-01-10 17:37:25 -05:00
|
|
|
const rid = op_compression_new(format, false);
|
2023-02-07 14:22:46 -05:00
|
|
|
|
|
|
|
this.#transform = new TransformStream({
|
|
|
|
transform(chunk, controller) {
|
2023-05-01 06:47:13 -04:00
|
|
|
chunk = webidl.converters.BufferSource(chunk, prefix, "chunk");
|
2024-01-10 17:37:25 -05:00
|
|
|
const output = op_compression_write(
|
2023-02-07 14:22:46 -05:00
|
|
|
rid,
|
|
|
|
chunk,
|
|
|
|
);
|
|
|
|
maybeEnqueue(controller, output);
|
|
|
|
},
|
|
|
|
flush(controller) {
|
2024-01-10 17:37:25 -05:00
|
|
|
const output = op_compression_finish(rid);
|
2023-02-07 14:22:46 -05:00
|
|
|
maybeEnqueue(controller, output);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
this[webidl.brand] = webidl.brand;
|
2022-01-24 12:03:06 -05:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
get readable() {
|
|
|
|
webidl.assertBranded(this, CompressionStreamPrototype);
|
|
|
|
return this.#transform.readable;
|
2022-01-24 12:03:06 -05:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
get writable() {
|
|
|
|
webidl.assertBranded(this, CompressionStreamPrototype);
|
|
|
|
return this.#transform.writable;
|
2022-01-24 12:03:06 -05:00
|
|
|
}
|
2023-11-19 03:13:38 -05:00
|
|
|
|
|
|
|
[SymbolFor("Deno.privateCustomInspect")](inspect, inspectOptions) {
|
|
|
|
return inspect(
|
|
|
|
createFilteredInspectProxy({
|
|
|
|
object: this,
|
|
|
|
evaluate: ObjectPrototypeIsPrototypeOf(
|
|
|
|
CompressionStreamPrototype,
|
|
|
|
this,
|
|
|
|
),
|
|
|
|
keys: [
|
|
|
|
"readable",
|
|
|
|
"writable",
|
|
|
|
],
|
|
|
|
}),
|
|
|
|
inspectOptions,
|
|
|
|
);
|
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
|
2023-10-09 23:01:01 -04:00
|
|
|
webidl.configureInterface(CompressionStream);
|
2023-02-07 14:22:46 -05:00
|
|
|
const CompressionStreamPrototype = CompressionStream.prototype;
|
|
|
|
|
|
|
|
class DecompressionStream {
|
|
|
|
#transform;
|
|
|
|
|
|
|
|
constructor(format) {
|
|
|
|
const prefix = "Failed to construct 'DecompressionStream'";
|
2023-04-12 15:58:57 -04:00
|
|
|
webidl.requiredArguments(arguments.length, 1, prefix);
|
2023-05-01 06:47:13 -04:00
|
|
|
format = webidl.converters.CompressionFormat(format, prefix, "Argument 1");
|
2023-02-07 14:22:46 -05:00
|
|
|
|
2024-01-10 17:37:25 -05:00
|
|
|
const rid = op_compression_new(format, true);
|
2023-02-07 14:22:46 -05:00
|
|
|
|
|
|
|
this.#transform = new TransformStream({
|
|
|
|
transform(chunk, controller) {
|
2023-05-01 06:47:13 -04:00
|
|
|
chunk = webidl.converters.BufferSource(chunk, prefix, "chunk");
|
2024-01-10 17:37:25 -05:00
|
|
|
const output = op_compression_write(
|
2023-02-07 14:22:46 -05:00
|
|
|
rid,
|
|
|
|
chunk,
|
|
|
|
);
|
|
|
|
maybeEnqueue(controller, output);
|
|
|
|
},
|
|
|
|
flush(controller) {
|
2024-01-10 17:37:25 -05:00
|
|
|
const output = op_compression_finish(rid);
|
2023-02-07 14:22:46 -05:00
|
|
|
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;
|
|
|
|
}
|
2023-11-19 03:13:38 -05:00
|
|
|
|
|
|
|
[SymbolFor("Deno.privateCustomInspect")](inspect, inspectOptions) {
|
|
|
|
return inspect(
|
|
|
|
createFilteredInspectProxy({
|
|
|
|
object: this,
|
|
|
|
evaluate: ObjectPrototypeIsPrototypeOf(
|
|
|
|
DecompressionStreamPrototype,
|
|
|
|
this,
|
|
|
|
),
|
|
|
|
keys: [
|
|
|
|
"readable",
|
|
|
|
"writable",
|
|
|
|
],
|
|
|
|
}),
|
|
|
|
inspectOptions,
|
|
|
|
);
|
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function maybeEnqueue(controller, output) {
|
2023-04-02 13:41:41 -04:00
|
|
|
if (output && TypedArrayPrototypeGetByteLength(output) > 0) {
|
2023-02-07 14:22:46 -05:00
|
|
|
controller.enqueue(output);
|
|
|
|
}
|
|
|
|
}
|
2022-01-24 12:03:06 -05:00
|
|
|
|
2023-10-09 23:01:01 -04:00
|
|
|
webidl.configureInterface(DecompressionStream);
|
2023-02-07 14:22:46 -05:00
|
|
|
const DecompressionStreamPrototype = DecompressionStream.prototype;
|
2022-01-24 12:03:06 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
export { CompressionStream, DecompressionStream };
|