From 18d45baa740f42e89e48ed56390e38b4075000ed Mon Sep 17 00:00:00 2001 From: lino-levan <11367844+lino-levan@users.noreply.github.com> Date: Sun, 24 Sep 2023 11:41:54 -0700 Subject: [PATCH] feat: streams API --- ext/lib.rs | 1 + ext/web/mod.js | 10 ++++++++++ ext/web/streams.js | 24 ++++++++++++++++++++++++ 3 files changed, 35 insertions(+) create mode 100644 ext/web/streams.js diff --git a/ext/lib.rs b/ext/lib.rs index 07c2fea..df392f3 100644 --- a/ext/lib.rs +++ b/ext/lib.rs @@ -41,6 +41,7 @@ pub mod extensions { "battery/mod.js", "web/mod.js", "web/events.js", + "web/streams.js", "console/mod.js", "console/printer.js", "console/formatter.js", diff --git a/ext/web/mod.js b/ext/web/mod.js index 0341ffc..1943b55 100644 --- a/ext/web/mod.js +++ b/ext/web/mod.js @@ -1,6 +1,16 @@ import { CustomEvent, Event, EventTarget } from "ext:bueno/web/events.js"; +import { + ByteLengthQueuingStrategy, + CountQueuingStrategy, +} from "ext:bueno/web/streams.js"; globalThis.navigator = {}; + +// Event API globalThis.Event = Event; globalThis.CustomEvent = CustomEvent; globalThis.EventTarget = EventTarget; + +// Streams API +globalThis.ByteLengthQueuingStrategy = ByteLengthQueuingStrategy; +globalThis.CountQueuingStrategy = CountQueuingStrategy; diff --git a/ext/web/streams.js b/ext/web/streams.js new file mode 100644 index 0000000..57b7dfc --- /dev/null +++ b/ext/web/streams.js @@ -0,0 +1,24 @@ +// Ported from https://github.com/whatwg/streams/tree/main/reference-implementation + +export class ByteLengthQueuingStrategy { + constructor({ highWaterMark }) { + this.highWaterMark = highWaterMark; + } + + /** + * @param {Uint8Array} chunk + */ + size(chunk) { + return chunk.byteLength; + } +} + +export class CountQueuingStrategy { + constructor({ highWaterMark }) { + this.highWaterMark = highWaterMark; + } + + get size() { + return 1; + } +}