|
| 1 | +// Copyright (C) 2023-2024 Lightpanda (Selecy SAS) |
| 2 | +// |
| 3 | +// Francis Bouvier <[email protected]> |
| 4 | +// Pierre Tachoire <[email protected]> |
| 5 | +// |
| 6 | +// This program is free software: you can redistribute it and/or modify |
| 7 | +// it under the terms of the GNU Affero General Public License as |
| 8 | +// published by the Free Software Foundation, either version 3 of the |
| 9 | +// License, or (at your option) any later version. |
| 10 | +// |
| 11 | +// This program is distributed in the hope that it will be useful, |
| 12 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | +// GNU Affero General Public License for more details. |
| 15 | +// |
| 16 | +// You should have received a copy of the GNU Affero General Public License |
| 17 | +// along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 18 | + |
| 19 | +const std = @import("std"); |
| 20 | +const log = @import("../../log.zig"); |
| 21 | + |
| 22 | +const v8 = @import("v8"); |
| 23 | +const Page = @import("../page.zig").Page; |
| 24 | +const Env = @import("../env.zig").Env; |
| 25 | + |
| 26 | +const ReadableStream = @This(); |
| 27 | +const ReadableStreamDefaultReader = @import("ReadableStreamDefaultReader.zig"); |
| 28 | +const ReadableStreamDefaultController = @import("ReadableStreamDefaultController.zig"); |
| 29 | + |
| 30 | +const State = union(enum) { |
| 31 | + readable, |
| 32 | + closed: ?[]const u8, |
| 33 | + errored: Env.JsObject, |
| 34 | +}; |
| 35 | + |
| 36 | +// This promise resolves when a stream is canceled. |
| 37 | +cancel_resolver: Env.PromiseResolver, |
| 38 | +locked: bool = false, |
| 39 | +state: State = .readable, |
| 40 | + |
| 41 | +// A queue would be ideal here but I don't want to pay the cost of the priority operation. |
| 42 | +queue: std.ArrayListUnmanaged([]const u8) = .empty, |
| 43 | + |
| 44 | +const UnderlyingSource = struct { |
| 45 | + start: ?Env.Function = null, |
| 46 | + pull: ?Env.Function = null, |
| 47 | + cancel: ?Env.Function = null, |
| 48 | + type: ?[]const u8 = null, |
| 49 | +}; |
| 50 | + |
| 51 | +const QueueingStrategy = struct { |
| 52 | + size: ?Env.Function = null, |
| 53 | + high_water_mark: f64 = 1.0, |
| 54 | +}; |
| 55 | + |
| 56 | +pub fn constructor(underlying: ?UnderlyingSource, strategy: ?QueueingStrategy, page: *Page) !*ReadableStream { |
| 57 | + _ = strategy; |
| 58 | + |
| 59 | + const cancel_resolver = Env.PromiseResolver{ |
| 60 | + .js_context = page.main_context, |
| 61 | + .resolver = v8.PromiseResolver.init(page.main_context.v8_context), |
| 62 | + }; |
| 63 | + |
| 64 | + const stream = try page.arena.create(ReadableStream); |
| 65 | + stream.* = ReadableStream{ .cancel_resolver = cancel_resolver }; |
| 66 | + |
| 67 | + const controller = ReadableStreamDefaultController{ .stream = stream }; |
| 68 | + |
| 69 | + // call start |
| 70 | + if (underlying) |src| { |
| 71 | + if (src.start) |start| { |
| 72 | + try start.call(void, .{controller}); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + log.info(.browser, "rs aux", .{ .queue_len = stream.queue.items.len }); |
| 77 | + |
| 78 | + return stream; |
| 79 | +} |
| 80 | + |
| 81 | +pub fn _cancel(self: *const ReadableStream) Env.Promise { |
| 82 | + return self.cancel_resolver.promise(); |
| 83 | +} |
| 84 | + |
| 85 | +pub fn get_locked(self: *const ReadableStream) bool { |
| 86 | + return self.locked; |
| 87 | +} |
| 88 | + |
| 89 | +const GetReaderOptions = struct { |
| 90 | + mode: ?[]const u8 = null, |
| 91 | +}; |
| 92 | + |
| 93 | +pub fn _getReader(self: *ReadableStream, _options: ?GetReaderOptions, page: *Page) ReadableStreamDefaultReader { |
| 94 | + const options = _options orelse GetReaderOptions{}; |
| 95 | + _ = options; |
| 96 | + |
| 97 | + return ReadableStreamDefaultReader.constructor(self, page); |
| 98 | +} |
| 99 | + |
| 100 | +const testing = @import("../../testing.zig"); |
| 101 | +test "streams: ReadableStream" { |
| 102 | + var runner = try testing.jsRunner(testing.tracking_allocator, .{ .url = "https://lightpanda.io" }); |
| 103 | + defer runner.deinit(); |
| 104 | + |
| 105 | + try runner.testCases(&.{ |
| 106 | + .{ "var readResult;", "undefined" }, |
| 107 | + .{ |
| 108 | + \\ const stream = new ReadableStream({ |
| 109 | + \\ start(controller) { |
| 110 | + \\ controller.enqueue("hello"); |
| 111 | + \\ controller.enqueue("world"); |
| 112 | + \\ controller.close(); |
| 113 | + \\ } |
| 114 | + \\ }); |
| 115 | + , |
| 116 | + undefined, |
| 117 | + }, |
| 118 | + .{ |
| 119 | + \\ const reader = stream.getReader(); |
| 120 | + \\ (async function () { readResult = await reader.read() }()); |
| 121 | + \\ false; |
| 122 | + , |
| 123 | + "false", |
| 124 | + }, |
| 125 | + .{ "reader", "[object ReadableStreamDefaultReader]" }, |
| 126 | + .{ "readResult.value", "hello" }, |
| 127 | + .{ "readResult.done", "false" }, |
| 128 | + }, .{}); |
| 129 | +} |
0 commit comments