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" />
|
|
|
|
|
2024-01-26 17:46:46 -05:00
|
|
|
import { primordials } from "ext:core/mod.js";
|
|
|
|
import {
|
2024-01-10 17:37:25 -05:00
|
|
|
op_compression_finish,
|
|
|
|
op_compression_new,
|
|
|
|
op_compression_write,
|
2024-01-26 17:46:46 -05:00
|
|
|
} from "ext:core/ops";
|
2023-04-02 13:41:41 -04:00
|
|
|
const {
|
2023-11-19 03:13:38 -05:00
|
|
|
SymbolFor,
|
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";
|
2024-08-16 07:34:09 -04:00
|
|
|
import { privateInspect } from "ext:deno_console/01_console.js";
|
2024-03-22 12:21:05 -04:00
|
|
|
import { TransformStream } from "./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-02-13 16:45:23 -05:00
|
|
|
const output = op_compression_finish(rid, true);
|
2023-02-07 14:22:46 -05:00
|
|
|
maybeEnqueue(controller, output);
|
|
|
|
},
|
2024-02-13 16:45:23 -05:00
|
|
|
cancel: (_reason) => {
|
|
|
|
op_compression_finish(rid, false);
|
|
|
|
},
|
2023-02-07 14:22:46 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
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) {
|
2024-08-16 07:34:09 -04:00
|
|
|
return privateInspect(
|
|
|
|
this,
|
|
|
|
["readable", "writable"],
|
|
|
|
inspect,
|
2023-11-19 03:13:38 -05:00
|
|
|
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-02-13 16:45:23 -05:00
|
|
|
const output = op_compression_finish(rid, true);
|
2023-02-07 14:22:46 -05:00
|
|
|
maybeEnqueue(controller, output);
|
|
|
|
},
|
2024-02-13 16:45:23 -05:00
|
|
|
cancel: (_reason) => {
|
|
|
|
op_compression_finish(rid, false);
|
|
|
|
},
|
2023-02-07 14:22:46 -05: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 03:13:38 -05:00
|
|
|
|
|
|
|
[SymbolFor("Deno.privateCustomInspect")](inspect, inspectOptions) {
|
2024-08-16 07:34:09 -04:00
|
|
|
return privateInspect(
|
|
|
|
this,
|
|
|
|
["readable", "writable"],
|
|
|
|
inspect,
|
2023-11-19 03:13:38 -05:00
|
|
|
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 };
|