2022-01-24 12:03:06 -05:00
|
|
|
// Copyright 2018-2022 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" />
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
((window) => {
|
|
|
|
const core = window.Deno.core;
|
|
|
|
const webidl = window.__bootstrap.webidl;
|
|
|
|
const { TransformStream } = window.__bootstrap.streams;
|
|
|
|
|
|
|
|
webidl.converters.CompressionFormat = webidl.createEnumConverter(
|
|
|
|
"CompressionFormat",
|
|
|
|
[
|
|
|
|
"deflate",
|
2022-06-14 12:10:28 -04:00
|
|
|
"deflate-raw",
|
2022-01-24 12:03:06 -05:00
|
|
|
"gzip",
|
|
|
|
],
|
|
|
|
);
|
|
|
|
|
|
|
|
class CompressionStream {
|
|
|
|
#transform;
|
|
|
|
|
|
|
|
constructor(format) {
|
|
|
|
const prefix = "Failed to construct 'CompressionStream'";
|
|
|
|
webidl.requiredArguments(arguments.length, 1, { prefix });
|
|
|
|
format = webidl.converters.CompressionFormat(format, {
|
|
|
|
prefix,
|
|
|
|
context: "Argument 1",
|
|
|
|
});
|
|
|
|
|
|
|
|
const rid = core.opSync("op_compression_new", format, false);
|
|
|
|
|
|
|
|
this.#transform = new TransformStream({
|
|
|
|
transform(chunk, controller) {
|
2022-02-16 06:21:46 -05:00
|
|
|
chunk = webidl.converters.BufferSource(chunk, {
|
|
|
|
prefix,
|
|
|
|
context: "chunk",
|
|
|
|
});
|
2022-01-24 12:03:06 -05:00
|
|
|
const output = core.opSync(
|
|
|
|
"op_compression_write",
|
|
|
|
rid,
|
|
|
|
chunk,
|
|
|
|
);
|
|
|
|
maybeEnqueue(controller, output);
|
|
|
|
},
|
|
|
|
flush(controller) {
|
|
|
|
const output = core.opSync("op_compression_finish", rid);
|
|
|
|
maybeEnqueue(controller, output);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
this[webidl.brand] = webidl.brand;
|
|
|
|
}
|
|
|
|
|
|
|
|
get readable() {
|
2022-02-01 12:06:11 -05:00
|
|
|
webidl.assertBranded(this, CompressionStreamPrototype);
|
2022-01-24 12:03:06 -05:00
|
|
|
return this.#transform.readable;
|
|
|
|
}
|
|
|
|
|
|
|
|
get writable() {
|
2022-02-01 12:06:11 -05:00
|
|
|
webidl.assertBranded(this, CompressionStreamPrototype);
|
2022-01-24 12:03:06 -05:00
|
|
|
return this.#transform.writable;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
webidl.configurePrototype(CompressionStream);
|
2022-02-01 12:06:11 -05:00
|
|
|
const CompressionStreamPrototype = CompressionStream.prototype;
|
2022-01-24 12:03:06 -05:00
|
|
|
|
|
|
|
class DecompressionStream {
|
|
|
|
#transform;
|
|
|
|
|
|
|
|
constructor(format) {
|
|
|
|
const prefix = "Failed to construct 'DecompressionStream'";
|
|
|
|
webidl.requiredArguments(arguments.length, 1, { prefix });
|
|
|
|
format = webidl.converters.CompressionFormat(format, {
|
|
|
|
prefix,
|
|
|
|
context: "Argument 1",
|
|
|
|
});
|
|
|
|
|
|
|
|
const rid = core.opSync("op_compression_new", format, true);
|
|
|
|
|
|
|
|
this.#transform = new TransformStream({
|
|
|
|
transform(chunk, controller) {
|
2022-02-16 06:21:46 -05:00
|
|
|
chunk = webidl.converters.BufferSource(chunk, {
|
|
|
|
prefix,
|
|
|
|
context: "chunk",
|
|
|
|
});
|
2022-01-24 12:03:06 -05:00
|
|
|
const output = core.opSync(
|
|
|
|
"op_compression_write",
|
|
|
|
rid,
|
|
|
|
chunk,
|
|
|
|
);
|
|
|
|
maybeEnqueue(controller, output);
|
|
|
|
},
|
|
|
|
flush(controller) {
|
|
|
|
const output = core.opSync("op_compression_finish", rid);
|
|
|
|
maybeEnqueue(controller, output);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
this[webidl.brand] = webidl.brand;
|
|
|
|
}
|
|
|
|
|
|
|
|
get readable() {
|
2022-02-01 12:06:11 -05:00
|
|
|
webidl.assertBranded(this, DecompressionStreamPrototype);
|
2022-01-24 12:03:06 -05:00
|
|
|
return this.#transform.readable;
|
|
|
|
}
|
|
|
|
|
|
|
|
get writable() {
|
2022-02-01 12:06:11 -05:00
|
|
|
webidl.assertBranded(this, DecompressionStreamPrototype);
|
2022-01-24 12:03:06 -05:00
|
|
|
return this.#transform.writable;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function maybeEnqueue(controller, output) {
|
|
|
|
if (output && output.byteLength > 0) {
|
|
|
|
controller.enqueue(output);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
webidl.configurePrototype(DecompressionStream);
|
2022-02-01 12:06:11 -05:00
|
|
|
const DecompressionStreamPrototype = DecompressionStream.prototype;
|
2022-01-24 12:03:06 -05:00
|
|
|
|
|
|
|
window.__bootstrap.compression = {
|
|
|
|
CompressionStream,
|
|
|
|
DecompressionStream,
|
|
|
|
};
|
|
|
|
})(globalThis);
|