Lightpanda: the headless browser designed for AI and automation
webapi: CountQueuingStrategy and ByteLengthQueuingStrategy
Adrià Arrufata8ff1c6c91ab
diff --git a/src/browser/js/bridge.zig b/src/browser/js/bridge.zigindex f79297a77..427196ee4 100644--- a/src/browser/js/bridge.zig+++ b/src/browser/js/bridge.zig@@ -1189,6 +1189,8 @@ pub const PageJsApis = flattenTypes(&.{@import("../webapi/streams/ReadableStreamDefaultController.zig"),@import("../webapi/streams/WritableStream.zig"),@import("../webapi/streams/WritableStreamDefaultWriter.zig"),+ @import("../webapi/streams/CountQueuingStrategy.zig"),+ @import("../webapi/streams/ByteLengthQueuingStrategy.zig"),@import("../webapi/streams/WritableStreamDefaultController.zig"),@import("../webapi/streams/TransformStream.zig"),@import("../webapi/Node.zig"),@@ -1282,6 +1284,8 @@ const worker_common_apis = [_]type{@import("../webapi/streams/ReadableStreamDefaultController.zig"),@import("../webapi/streams/WritableStream.zig"),@import("../webapi/streams/WritableStreamDefaultWriter.zig"),+ @import("../webapi/streams/CountQueuingStrategy.zig"),+ @import("../webapi/streams/ByteLengthQueuingStrategy.zig"),@import("../webapi/streams/WritableStreamDefaultController.zig"),@import("../webapi/encoding/TextEncoderStream.zig"),@import("../webapi/encoding/TextDecoderStream.zig"),diff --git a/src/browser/tests/streams/readable_stream.html b/src/browser/tests/streams/readable_stream.htmlindex c82fd9851..47582dec8 100644--- a/src/browser/tests/streams/readable_stream.html+++ b/src/browser/tests/streams/readable_stream.html@@ -372,3 +372,20 @@testing.expectEqual(7, r1.value.num);})();</script>++<script id=queuing_strategies>+ {+ const count = new CountQueuingStrategy({ highWaterMark: 3 });+ testing.expectEqual(3, count.highWaterMark);+ testing.expectEqual(1, count.size('anything'));++ const bytes = new ByteLengthQueuingStrategy({ highWaterMark: 1024 });+ testing.expectEqual(1024, bytes.highWaterMark);+ testing.expectEqual(4, bytes.size(new Uint8Array(4)));+ testing.expectEqual(8, bytes.size(new ArrayBuffer(8)));+ testing.expectError('TypeError', () => bytes.size(7));++ const stream = new ReadableStream({}, count);+ testing.expectEqual('function', typeof stream.getReader);+ }+</script>diff --git a/src/browser/webapi/streams/ByteLengthQueuingStrategy.zig b/src/browser/webapi/streams/ByteLengthQueuingStrategy.zignew file mode 100644index 000000000..d602b60a0--- /dev/null+++ b/src/browser/webapi/streams/ByteLengthQueuingStrategy.zig@@ -0,0 +1,59 @@+// Copyright (C) 2023-2025 Lightpanda (Selecy SAS)+//+// Francis Bouvier <[email protected]>+// Pierre Tachoire <[email protected]>+//+// This program is free software: you can redistribute it and/or modify+// it under the terms of the GNU Affero General Public License as+// published by the Free Software Foundation, either version 3 of the+// License, or (at your option) any later version.+//+// This program is distributed in the hope that it will be useful,+// but WITHOUT ANY WARRANTY; without even the implied warranty of+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+// GNU Affero General Public License for more details.+//+// You should have received a copy of the GNU Affero General Public License+// along with this program. If not, see <https://www.gnu.org/licenses/>.++const js = @import("../../js/js.zig");++const Execution = js.Execution;++const ByteLengthQueuingStrategy = @This();++_high_water_mark: f64,++const Init = struct {+ highWaterMark: f64,+};++pub fn init(init_: Init, exec: *const Execution) !*ByteLengthQueuingStrategy {+ return exec._factory.create(ByteLengthQueuingStrategy{ ._high_water_mark = init_.highWaterMark });+}++pub fn getHighWaterMark(self: *const ByteLengthQueuingStrategy) f64 {+ return self._high_water_mark;+}++pub fn size(_: *const ByteLengthQueuingStrategy, chunk: js.Value) !f64 {+ if (!chunk.isObject()) {+ return error.TypeError;+ }+ const sized = try chunk.toObject().toZig(struct { byteLength: f64 });+ return sized.byteLength;+}++pub const JsApi = struct {+ pub const bridge = js.Bridge(ByteLengthQueuingStrategy);++ pub const Meta = struct {+ pub const name = "ByteLengthQueuingStrategy";+ pub const prototype_chain = bridge.prototypeChain();+ pub var class_id: bridge.ClassId = undefined;+ };++ pub const constructor = bridge.constructor(ByteLengthQueuingStrategy.init, .{});+ pub const highWaterMark = bridge.accessor(ByteLengthQueuingStrategy.getHighWaterMark, null, .{});+ pub const size = bridge.function(ByteLengthQueuingStrategy.size, .{});+};diff --git a/src/browser/webapi/streams/CountQueuingStrategy.zig b/src/browser/webapi/streams/CountQueuingStrategy.zignew file mode 100644index 000000000..0fe4e8ad8--- /dev/null+++ b/src/browser/webapi/streams/CountQueuingStrategy.zig@@ -0,0 +1,55 @@+// Copyright (C) 2023-2025 Lightpanda (Selecy SAS)+//+// Francis Bouvier <[email protected]>+// Pierre Tachoire <[email protected]>+//+// This program is free software: you can redistribute it and/or modify+// it under the terms of the GNU Affero General Public License as+// published by the Free Software Foundation, either version 3 of the+// License, or (at your option) any later version.+//+// This program is distributed in the hope that it will be useful,+// but WITHOUT ANY WARRANTY; without even the implied warranty of+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+// GNU Affero General Public License for more details.+//+// You should have received a copy of the GNU Affero General Public License+// along with this program. If not, see <https://www.gnu.org/licenses/>.++const js = @import("../../js/js.zig");++const Execution = js.Execution;++const CountQueuingStrategy = @This();++_high_water_mark: f64,++const Init = struct {+ highWaterMark: f64,+};++pub fn init(init_: Init, exec: *const Execution) !*CountQueuingStrategy {+ return exec._factory.create(CountQueuingStrategy{ ._high_water_mark = init_.highWaterMark });+}++pub fn getHighWaterMark(self: *const CountQueuingStrategy) f64 {+ return self._high_water_mark;+}++pub fn size(_: *const CountQueuingStrategy) u32 {+ return 1;+}++pub const JsApi = struct {+ pub const bridge = js.Bridge(CountQueuingStrategy);++ pub const Meta = struct {+ pub const name = "CountQueuingStrategy";+ pub const prototype_chain = bridge.prototypeChain();+ pub var class_id: bridge.ClassId = undefined;+ };++ pub const constructor = bridge.constructor(CountQueuingStrategy.init, .{});+ pub const highWaterMark = bridge.accessor(CountQueuingStrategy.getHighWaterMark, null, .{});+ pub const size = bridge.function(CountQueuingStrategy.size, .{});+};