summaryrefslogtreecommitdiff
path: root/ext/web/14_compression.js
diff options
context:
space:
mode:
authorLeo Kettmeir <crowlkats@toaxl.com>2022-01-24 18:03:06 +0100
committerGitHub <noreply@github.com>2022-01-24 18:03:06 +0100
commit30ddf436d0d48ce0f9238f1728bc83aa4c6dddad (patch)
tree92cb750fe2a2beaf9b5884530bb3419ddee54cb4 /ext/web/14_compression.js
parentae0414fa3581d76af44192567d73dce987c69b9d (diff)
feat(ext/web): add CompressionStream API (#11728)
Co-authored-by: Luca Casonato <hello@lcas.dev> Co-authored-by: Ryan Dahl <ry@tinyclouds.org>
Diffstat (limited to 'ext/web/14_compression.js')
-rw-r--r--ext/web/14_compression.js123
1 files changed, 123 insertions, 0 deletions
diff --git a/ext/web/14_compression.js b/ext/web/14_compression.js
new file mode 100644
index 000000000..1a0f77e66
--- /dev/null
+++ b/ext/web/14_compression.js
@@ -0,0 +1,123 @@
+// 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",
+ "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) {
+ // TODO(lucacasonato): convert chunk to BufferSource
+ 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() {
+ webidl.assertBranded(this, CompressionStream);
+ return this.#transform.readable;
+ }
+
+ get writable() {
+ webidl.assertBranded(this, CompressionStream);
+ return this.#transform.writable;
+ }
+ }
+
+ webidl.configurePrototype(CompressionStream);
+
+ 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) {
+ // TODO(lucacasonato): convert chunk to BufferSource
+ 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() {
+ webidl.assertBranded(this, DecompressionStream);
+ return this.#transform.readable;
+ }
+
+ get writable() {
+ webidl.assertBranded(this, DecompressionStream);
+ return this.#transform.writable;
+ }
+ }
+
+ function maybeEnqueue(controller, output) {
+ if (output && output.byteLength > 0) {
+ controller.enqueue(output);
+ }
+ }
+
+ webidl.configurePrototype(DecompressionStream);
+
+ window.__bootstrap.compression = {
+ CompressionStream,
+ DecompressionStream,
+ };
+})(globalThis);