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