Skip to content

Lightpanda: the headless browser designed for AI and automation

support for brotli in compression streams

Halil Durake49195a942de
diff --git a/build.zig b/build.zig
index 64b7f9166..5e67e4528 100644
--- a/build.zig
+++ b/build.zig
@@ -526,12 +526,17 @@ fn linkCurl(b: *Build, mod: *Build.Module, deps: Deps, is_tsan: bool, section: b
translator.addIncludePath(dep.path("include"));
mod.addImport("curl", translator.mod);
+ // zlib and brotli are linked to curl for their installed headers, and to
+ // mod since Compression Streams call them directly.
const zlib = buildZlib(b, deps.target, deps.optimize, is_tsan, section);
curl.root_module.linkLibrary(zlib);
mod.linkLibrary(zlib);
const brotli = buildBrotli(b, deps.target, deps.optimize, is_tsan, section);
- for (brotli) |lib| curl.root_module.linkLibrary(lib);
+ inline for (brotli) |lib| {
+ curl.root_module.linkLibrary(lib);
+ mod.linkLibrary(lib);
+ }
const zstd = buildZstd(b, deps.target, deps.optimize, is_tsan, section);
curl.root_module.linkLibrary(zstd);
diff --git a/src/browser/webapi/compression/CompressionStream.zig b/src/browser/webapi/compression/CompressionStream.zig
index 099e1cb61..4196dc22a 100644
--- a/src/browser/webapi/compression/CompressionStream.zig
+++ b/src/browser/webapi/compression/CompressionStream.zig
@@ -19,22 +19,23 @@
const js = @import("../../js/js.zig");
const Execution = js.Execution;
-const zlib = @import("zlib.zig");
const ReadableStream = @import("../streams/ReadableStream.zig");
const WritableStream = @import("../streams/WritableStream.zig");
const TransformStream = @import("../streams/TransformStream.zig");
+const compress = @import("compress.zig");
+
const CompressionStream = @This();
_transform: *TransformStream,
-_zlib: zlib.Stream(.compress),
+_compressor: compress.Compressor,
-pub fn init(format: zlib.Format, exec: *const Execution) !*CompressionStream {
+pub fn init(format: compress.Format, exec: *const Execution) !*CompressionStream {
const self = try exec._factory.create(CompressionStream{
._transform = undefined,
- ._zlib = .init(exec, format),
+ ._compressor = .init(exec, format),
});
- self._transform = try TransformStream.initWithZigTransformer(self._zlib.transformer(), exec);
+ self._transform = try TransformStream.initWithZigTransformer(self._compressor.transformer(), exec);
return self;
}
@@ -62,5 +63,7 @@ pub const JsApi = struct {
const testing = @import("../../../testing.zig");
test "WebApi: CompressionStream" {
- try testing.htmlRunner("compression/compression_stream.html", .{});
+ // brotli_output_before_close compresses 1.3 MiB at quality 11; that's
+ // slow under TSAN.
+ try testing.htmlRunner("compression/compression_stream.html", .{ .timeout_ms = 8000 });
}
diff --git a/src/browser/webapi/compression/DecompressionStream.zig b/src/browser/webapi/compression/DecompressionStream.zig
index 93df8bbf7..ad2418be0 100644
--- a/src/browser/webapi/compression/DecompressionStream.zig
+++ b/src/browser/webapi/compression/DecompressionStream.zig
@@ -18,6 +18,7 @@
const js = @import("../../js/js.zig");
+const brotli = @import("brotli.zig");
const zlib = @import("zlib.zig");
const ReadableStream = @import("../streams/ReadableStream.zig");
const WritableStream = @import("../streams/WritableStream.zig");
@@ -25,17 +26,19 @@ const TransformStream = @import("../streams/TransformStream.zig");
const Execution = js.Execution;
+const compress = @import("compress.zig");
+
const DecompressionStream = @This();
_transform: *TransformStream,
-_zlib: zlib.Stream(.decompress),
+_decompressor: compress.Decompressor,
-pub fn init(format: zlib.Format, exec: *const Execution) !*DecompressionStream {
+pub fn init(format: compress.Format, exec: *const Execution) !*DecompressionStream {
const self = try exec._factory.create(DecompressionStream{
._transform = undefined,
- ._zlib = .init(exec, format),
+ ._decompressor = .init(exec, format),
});
- self._transform = try TransformStream.initWithZigTransformer(self._zlib.transformer(), exec);
+ self._transform = try TransformStream.initWithZigTransformer(self._decompressor.transformer(), exec);
return self;
}
diff --git a/src/browser/webapi/compression/brotli.zig b/src/browser/webapi/compression/brotli.zig
new file mode 100644
index 000000000..6b6816cf1
--- /dev/null
+++ b/src/browser/webapi/compression/brotli.zig
@@ -0,0 +1,352 @@
+// Copyright (C) 2023-2026 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 std = @import("std");
+
+const brotli = @import("../../../sys/brotli.zig");
+
+const js = @import("../../js/js.zig");
+const Execution = js.Execution;
+
+const TransformStream = @import("../streams/TransformStream.zig");
+const ZigTransformer = TransformStream.ZigTransformer;
+
+const State = enum {
+ /// Stream is dormant. Nothing is allocated at this state.
+ idle,
+ active,
+ ended,
+};
+
+/// Does brotli encoding in streaming fashion.
+pub const EncodingStream = struct {
+ exec: *const Execution,
+ state: State = .idle,
+ /// Lazily initialized, valid if `state` is `active`.
+ stream: *brotli.BrotliEncoderState = undefined,
+
+ // Public API.
+
+ pub fn init(exec: *const Execution) EncodingStream {
+ return .{ .exec = exec };
+ }
+
+ pub fn transformer(self: *EncodingStream) ZigTransformer {
+ return .{ .ctx = self, .vtable = vtable };
+ }
+
+ // Transformer API.
+
+ const vtable: *const ZigTransformer.VTable = &.{
+ .transform = transform,
+ .flush = flush,
+ };
+
+ fn transform(ctx: ?*anyopaque, controller: *TransformStream.TransformStreamDefaultController, chunk: js.Value) !void {
+ const self: *EncodingStream = @ptrCast(@alignCast(ctx));
+ const input = chunk.toZig(js.BufferSource) catch {
+ return self.fail(controller, "Chunk is not an ArrayBuffer or ArrayBufferView");
+ };
+
+ if (input.bytes.len == 0) return;
+ return self.process(controller, input.bytes, false);
+ }
+
+ fn flush(ctx: ?*anyopaque, controller: *TransformStream.TransformStreamDefaultController) !void {
+ const encoding_stream: *EncodingStream = @ptrCast(@alignCast(ctx));
+ return encoding_stream.process(controller, "", true);
+ }
+
+ // Implementation.
+
+ fn end(self: *EncodingStream) void {
+ switch (self.state) {
+ .idle, .ended => {},
+ .active => {
+ brotli.BrotliEncoderDestroyInstance(self.stream);
+ self.state = .ended;
+ },
+ }
+ }
+
+ // Errors the stream, releasing internal stream.
+ fn fail(
+ self: *EncodingStream,
+ controller: *TransformStream.TransformStreamDefaultController,
+ message: []const u8,
+ ) !void {
+ self.end();
+ try controller.typeError(message);
+ return self.exec.js.typeError(message);
+ }
+
+ /// Feed the stream and collect encoded bytes.
+ fn feed(
+ self: *EncodingStream,
+ controller: *TransformStream.TransformStreamDefaultController,
+ input: []const u8,
+ finish: bool,
+ ) !void {
+ const stream = self.stream;
+ var avail_in: usize = input.len;
+ var next_in: [*c]const u8 = input.ptr;
+
+ if (finish) {
+ // Operation finalizes the stream.
+ while (brotli.BrotliEncoderIsFinished(stream) == brotli.BROTLI_FALSE) {
+ // When *available_out is 0, next_out is allowed to be NULL.
+ var avail_out: usize = 0;
+ const rc = brotli.BrotliEncoderCompressStream(stream, .finish, &avail_in, &next_in, &avail_out, null, null);
+ if (rc == brotli.BROTLI_FALSE) {
+ return error.EncodingFailed;
+ }
+ // Pull everything from encoder.
+ while (brotli.BrotliEncoderHasMoreOutput(stream) == brotli.BROTLI_TRUE) {
+ var size: usize = 0;
+ const out = brotli.BrotliEncoderTakeOutput(stream, &size);
+ // Only valid until the next encoder call; enqueue copies it.
+ try controller.enqueueNoSideEffects(.{ .uint8array = .{ .values = out[0..size] } });
+ }
+ }
+ // encoding ended.
+ self.end();
+ } else {
+ // Operation processes more bytes, but does not finalize the stream.
+ while (true) {
+ var avail_out: usize = 0;
+ const rc = brotli.BrotliEncoderCompressStream(stream, .process, &avail_in, &next_in, &avail_out, null, null);
+ if (rc == brotli.BROTLI_FALSE) {
+ return error.EncodingFailed;
+ }
+
+ while (brotli.BrotliEncoderHasMoreOutput(stream) == brotli.BROTLI_TRUE) {
+ var size: usize = 0;
+ const out = brotli.BrotliEncoderTakeOutput(stream, &size);
+ try controller.enqueueNoSideEffects(.{ .uint8array = .{ .values = out[0..size] } });
+ }
+ // Done processing if no more input.
+ if (avail_in == 0) break;
+ }
+ }
+
+ // Wake readers.
+ return controller.fulfillPendingReads();
+ }
+
+ fn process(
+ self: *EncodingStream,
+ controller: *TransformStream.DefaultController,
+ input: []const u8,
+ finish: bool,
+ ) !void {
+ state_machine: switch (self.state) {
+ .idle => {
+ const stream = brotli.BrotliEncoderCreateInstance(alloc, free, @constCast(self.exec)) orelse {
+ return error.OutOfMemory;
+ };
+ self.stream = stream;
+ self.state = .active;
+ continue :state_machine self.state;
+ },
+ .active => {
+ self.feed(controller, input, finish) catch |err| switch (err) {
+ error.StreamNotReadable => return self.fail(controller, "The readable side is closed"),
+ error.EncodingFailed => return self.fail(controller, "Encoding failed"),
+ else => return err,
+ };
+ },
+ .ended => {},
+ }
+ }
+};
+
+/// Does brotli decoding in streaming fashion.
+pub const DecodingStream = struct {
+ exec: *const Execution,
+ state: State = .idle,
+ /// Lazily initialized, valid if `state` is `active`.
+ stream: *brotli.BrotliDecoderState = undefined,
+
+ // Public API.
+
+ pub fn init(exec: *const Execution) DecodingStream {
+ return .{ .exec = exec };
+ }
+
+ pub fn transformer(self: *DecodingStream) ZigTransformer {
+ return .{ .ctx = self, .vtable = vtable };
+ }
+
+ // Transformer API.
+
+ const vtable: *const ZigTransformer.VTable = &.{
+ .transform = transform,
+ .flush = flush,
+ };
+
+ fn transform(ctx: ?*anyopaque, controller: *TransformStream.TransformStreamDefaultController, chunk: js.Value) !void {
+ const self: *DecodingStream = @ptrCast(@alignCast(ctx));
+ const input = chunk.toZig(js.BufferSource) catch {
+ return self.fail(controller, "Chunk is not an ArrayBuffer or ArrayBufferView");
+ };
+
+ if (self.state == .ended) {
+ // The compressed data already ended. (Every other way to end
+ // errors the writable side.)
+ if (input.bytes.len > 0) {
+ return self.fail(controller, "Junk found after end of compressed data");
+ }
+ return;
+ }
+
+ if (input.bytes.len == 0) return;
+ return self.process(controller, input.bytes);
+ }
+
+ fn flush(ctx: ?*anyopaque, controller: *TransformStream.TransformStreamDefaultController) !void {
+ const self: *DecodingStream = @ptrCast(@alignCast(ctx));
+ // Each chunk is fully decoded as it's written, so by now the
+ // compressed data must have ended.
+ if (self.state != .ended) {
+ return self.fail(controller, "Compressed input was truncated");
+ }
+ }
+
+ // Implementation.
+
+ fn end(self: *DecodingStream) void {
+ switch (self.state) {
+ .idle, .ended => {},
+ .active => {
+ brotli.BrotliDecoderDestroyInstance(self.stream);
+ self.state = .ended;
+ },
+ }
+ }
+
+ // Errors the stream, releasing internal stream.
+ fn fail(
+ self: *DecodingStream,
+ controller: *TransformStream.TransformStreamDefaultController,
+ message: []const u8,
+ ) !void {
+ self.end();
+ try controller.typeError(message);
+ return self.exec.js.typeError(message);
+ }
+
+ /// Feed the stream and collect decoded bytes.
+ fn feed(
+ self: *DecodingStream,
+ controller: *TransformStream.TransformStreamDefaultController,
+ input: []const u8,
+ ) !void {
+ const stream = self.stream;
+ var avail_in: usize = input.len;
+ var next_in: [*c]const u8 = input.ptr;
+
+ while (true) {
+ // When *available_out is 0, next_out is allowed to be NULL.
+ var avail_out: usize = 0;
+ const result = brotli.BrotliDecoderDecompressStream(stream, &avail_in, &next_in, &avail_out, null, null);
+
+ // Pull everything from decoder. Its output isn't contiguous, so
+ // where the ring buffer wraps it takes more than one call.
+ while (brotli.BrotliDecoderHasMoreOutput(stream) == brotli.BROTLI_TRUE) {
+ var size: usize = 0;
+ const out = brotli.BrotliDecoderTakeOutput(stream, &size);
+ // Only valid until the next decoder call; enqueue copies it.
+ try controller.enqueueNoSideEffects(.{ .uint8array = .{ .values = out[0..size] } });
+ }
+
+ switch (result) {
+ .needs_more_output => {},
+ // All input is consumed; the compressed data continues in a
+ // later chunk.
+ .needs_more_input => break,
+ .success => {
+ // Input is never consumed past the end of the compressed
+ // data, so whatever is left is junk.
+ const has_junk = avail_in > 0;
+ self.end();
+ controller.fulfillPendingReads();
+ if (has_junk) {
+ return error.HasJunkData;
+ }
+ return;
+ },
+ .@"error" => return error.InvalidData,
+ }
+ }
+
+ // Wake readers.
+ return controller.fulfillPendingReads();
+ }
+
+ fn process(
+ self: *DecodingStream,
+ controller: *TransformStream.DefaultController,
+ input: []const u8,
+ ) !void {
+ state_machine: switch (self.state) {
+ .idle => {
+ const stream = brotli.BrotliDecoderCreateInstance(alloc, free, @constCast(self.exec)) orelse {
+ return error.OutOfMemory;
+ };
+ self.stream = stream;
+ self.state = .active;
+ continue :state_machine self.state;
+ },
+ .active => {
+ self.feed(controller, input) catch |err| switch (err) {
+ error.StreamNotReadable => return self.fail(controller, "The readable side is closed"),
+ error.HasJunkData => return self.fail(controller, "Junk found after end of data"),
+ error.InvalidData => return self.fail(controller, "The data was not valid"),
+ else => return err,
+ };
+ },
+ .ended => {},
+ }
+ }
+};
+
+const HEADER = 16;
+const alignment: std.mem.Alignment = .fromByteUnits(HEADER);
+
+fn alloc(raw_exec: ?*anyopaque, size: usize) callconv(.c) ?*anyopaque {
+ const exec: *const Execution = @ptrCast(@alignCast(raw_exec));
+ const allocator = exec._factory.storageAllocator();
+
+ const total = std.math.add(usize, size, HEADER) catch return null;
+ const block = allocator.alignedAlloc(u8, alignment, total) catch return null;
+ // Write total size of allocation.
+ std.mem.writeInt(usize, block[0..@sizeOf(usize)], total, .native);
+ // Conceal header part.
+ return block.ptr + HEADER;
+}
+
+fn free(raw_exec: ?*anyopaque, maybe_address: ?*anyopaque) callconv(.c) void {
+ const address = maybe_address orelse return;
+
+ const exec: *const Execution = @ptrCast(@alignCast(raw_exec));
+ const allocator = exec._factory.storageAllocator();
+
+ const block: [*]align(HEADER) u8 = @ptrCast(@alignCast(@as([*]u8, @ptrCast(address)) - HEADER));
+ const total = std.mem.readInt(usize, block[0..@sizeOf(usize)], .native);
+ allocator.free(block[0..total]);
+}
diff --git a/src/browser/webapi/compression/compress.zig b/src/browser/webapi/compression/compress.zig
new file mode 100644
index 000000000..0b0e78cfd
--- /dev/null
+++ b/src/browser/webapi/compression/compress.zig
@@ -0,0 +1,70 @@
+// Copyright (C) 2023-2026 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 zlib = @import("zlib.zig");
+const brotli = @import("brotli.zig");
+
+const js = @import("../../js/js.zig");
+const TransformStream = @import("../streams/TransformStream.zig");
+
+const Execution = js.Execution;
+
+pub const Format = enum {
+ brotli,
+ gzip,
+ deflate,
+ @"deflate-raw",
+
+ pub const js_enum_from_string = true;
+};
+
+pub const Compressor = union(enum(u1)) {
+ deflate: zlib.Deflate(.compress),
+ brotli: brotli.EncodingStream,
+
+ pub fn init(exec: *const Execution, format: Format) Compressor {
+ return switch (format) {
+ inline .deflate, .@"deflate-raw", .gzip => .{ .deflate = .init(exec, format) },
+ .brotli => .{ .brotli = .init(exec) },
+ };
+ }
+
+ pub fn transformer(self: *Compressor) TransformStream.ZigTransformer {
+ return switch (self.*) {
+ inline else => |*compressor| compressor.transformer(),
+ };
+ }
+};
+
+pub const Decompressor = union(enum(u1)) {
+ inflate: zlib.Deflate(.decompress),
+ brotli: brotli.DecodingStream,
+
+ pub fn init(exec: *const Execution, format: Format) Decompressor {
+ return switch (format) {
+ inline .deflate, .@"deflate-raw", .gzip => .{ .inflate = .init(exec, format) },
+ .brotli => .{ .brotli = .init(exec) },
+ };
+ }
+
+ pub fn transformer(self: *Decompressor) TransformStream.ZigTransformer {
+ return switch (self.*) {
+ inline else => |*decompressor| decompressor.transformer(),
+ };
+ }
+};
diff --git a/src/browser/webapi/compression/zlib.zig b/src/browser/webapi/compression/zlib.zig
index cb2fd361b..06a9656ea 100644
--- a/src/browser/webapi/compression/zlib.zig
+++ b/src/browser/webapi/compression/zlib.zig
@@ -1,4 +1,4 @@
-// Copyright (C) 2023-2026 Lightpanda (Selecy SAS)
+// Copyright (C) 2023-2026 Lightpanda (Selecy SAS)
//
// Francis Bouvier <[email protected]>
// Pierre Tachoire <[email protected]>
@@ -19,19 +19,15 @@
const std = @import("std");
const zlib = @import("../../../sys/zlib.zig");
+const brotli = @import("../../../sys/brotli.zig");
const js = @import("../../js/js.zig");
const TransformStream = @import("../streams/TransformStream.zig");
+const ZigTransformer = TransformStream.ZigTransformer;
const Execution = js.Execution;
-pub const Format = enum {
- deflate,
- @"deflate-raw",
- gzip,
-
- pub const js_enum_from_string = true;
-};
+const Format = @import("compress.zig").Format;
const State = enum {
/// Stream is dormant. Nothing is allocated at this state.
@@ -42,9 +38,8 @@ const State = enum {
ended,
};
-/// The zlib transformer behind CompressionStream and DecompressionStream.
-/// https://compression.spec.whatwg.org/
-pub fn Stream(comptime mode: enum(u1) { compress, decompress }) type {
+/// Covers gzip, deflate and deflate-raw.
+pub fn Deflate(comptime mode: enum(u1) { compress, decompress }) type {
return struct {
exec: *const Execution,
state: State = .idle,
@@ -52,26 +47,32 @@ pub fn Stream(comptime mode: enum(u1) { compress, decompress }) type {
/// Internal zlib stream; lazily initialized. Valid if `state` is `active`.
stream: zlib.z_stream = undefined,
- pub fn init(exec: *const Execution, format: Format) Stream(mode) {
+ const vtable: *const ZigTransformer.VTable = &.{
+ .transform = transform,
+ .flush = flush,
+ };
+
+ pub fn init(exec: *const Execution, format: Format) Deflate(mode) {
return .{ .format = format, .exec = exec };
}
- pub fn transformer(self: *Stream(mode)) TransformStream.ZigTransformer {
- return .{ .ctx = self, .transform = transform, .flush = flush };
+ pub fn transformer(self: *Deflate(mode)) ZigTransformer {
+ return .{ .ctx = self, .vtable = vtable };
}
/// Initializes internal `stream`.
- fn initStream(self: *Stream(mode)) !void {
+ fn initStream(self: *Deflate(mode)) !void {
// This must be done before the first use of deflate(). The zalloc,
// zfree, and opaque fields in the strm structure must be initialized
// before calling deflateInit().
// https://zlib.net/zlib_how.html
- self.stream = .{ .zalloc = zlib_alloc, .zfree = zlib_free, .@"opaque" = @constCast(self.exec) };
+ self.stream = .{ .zalloc = alloc, .zfree = free, .@"opaque" = @constCast(self.exec) };
// TODO: Match Chrome.
const window_bits: c_int = switch (self.format) {
.deflate => zlib.MAX_WBITS,
.@"deflate-raw" => -zlib.MAX_WBITS,
.gzip => zlib.MAX_WBITS + 16,
+ .brotli => unreachable,
};
// Default compression level Chrome picks.
// https://github.com/chromium/chromium/blob/1750d75d37b768db7fc6970edb2c4cd3eea0e1a3/third_party/blink/renderer/modules/compression/compression_stream.cc#L53-L55
@@ -90,7 +91,7 @@ pub fn Stream(comptime mode: enum(u1) { compress, decompress }) type {
};
}
- fn end(self: *Stream(mode)) void {
+ fn end(self: *Deflate(mode)) void {
switch (self.state) {
.idle, .ended => {},
.active => {
@@ -104,21 +105,16 @@ pub fn Stream(comptime mode: enum(u1) { compress, decompress }) type {
}
// Errors the stream, releasing internal stream.
- fn fail(self: *Stream(mode), controller: *TransformStream.DefaultController, message: []const u8) !void {
+ fn fail(self: *Deflate(mode), controller: *TransformStream.DefaultController, message: []const u8) !void {
self.end();
try controller.typeError(message);
return self.exec.js.typeError(message);
}
- const FeedError = error{
- StreamNotReadable,
- OutOfMemory,
- HasJunkData,
- InvalidData,
- };
+ const FeedError = error{ StreamNotReadable, OutOfMemory, HasJunkData, InvalidData };
fn feed(
- self: *Stream(mode),
+ self: *Deflate(mode),
controller: *TransformStream.DefaultController,
input: []const u8,
comptime output_buffer_size: u32,
@@ -189,7 +185,7 @@ pub fn Stream(comptime mode: enum(u1) { compress, decompress }) type {
}
fn process(
- self: *Stream(mode),
+ self: *Deflate(mode),
controller: *TransformStream.DefaultController,
input: []const u8,
finish: bool,
@@ -220,7 +216,7 @@ pub fn Stream(comptime mode: enum(u1) { compress, decompress }) type {
}
fn transform(ctx: ?*anyopaque, controller: *TransformStream.DefaultController, chunk: js.Value) !void {
- const self: *Stream(mode) = @ptrCast(@alignCast(ctx.?));
+ const self: *Deflate(mode) = @ptrCast(@alignCast(ctx.?));
const input = chunk.toZig(js.BufferSource) catch {
return self.fail(controller, "Chunk is not an ArrayBuffer or ArrayBufferView");
};
@@ -241,7 +237,7 @@ pub fn Stream(comptime mode: enum(u1) { compress, decompress }) type {
}
fn flush(ctx: ?*anyopaque, controller: *TransformStream.DefaultController) !void {
- const self: *Stream(mode) = @ptrCast(@alignCast(ctx.?));
+ const self: *Deflate(mode) = @ptrCast(@alignCast(ctx.?));
switch (comptime mode) {
.compress => return self.process(controller, "", true),
// Each chunk is fully inflated as it's written, so by now the
@@ -260,7 +256,7 @@ pub fn Stream(comptime mode: enum(u1) { compress, decompress }) type {
const HEADER = 16;
const alignment: std.mem.Alignment = .fromByteUnits(HEADER);
-fn zlib_alloc(userdata: ?*anyopaque, items: zlib.uInt, size: zlib.uInt) callconv(.c) ?*anyopaque {
+fn alloc(userdata: ?*anyopaque, items: zlib.uInt, size: zlib.uInt) callconv(.c) ?*anyopaque {
const exec: *const Execution = @ptrCast(@alignCast(userdata.?));
const allocator = exec._factory.storageAllocator();
@@ -273,7 +269,7 @@ fn zlib_alloc(userdata: ?*anyopaque, items: zlib.uInt, size: zlib.uInt) callconv
return block.ptr + HEADER;
}
-fn zlib_free(userdata: ?*anyopaque, ptr: ?*anyopaque) callconv(.c) void {
+fn free(userdata: ?*anyopaque, ptr: ?*anyopaque) callconv(.c) void {
const payload = ptr orelse return;
const exec: *const Execution = @ptrCast(@alignCast(userdata.?));
const allocator = exec._factory.storageAllocator();
diff --git a/src/browser/webapi/encoding/TextDecoderStream.zig b/src/browser/webapi/encoding/TextDecoderStream.zig
index c5cc2ad71..9e1acd66e 100644
--- a/src/browser/webapi/encoding/TextDecoderStream.zig
+++ b/src/browser/webapi/encoding/TextDecoderStream.zig
@@ -48,23 +48,29 @@ pub fn init(label_: ?[]const u8, opts_: ?InitOpts, exec: *const Execution) !Text
}
const opts = opts_ orelse InitOpts{};
- const decodeFn: TransformStream.ZigTransformFn = blk: {
+ const vtable: *const TransformStream.ZigTransformer.VTable = blk: {
if (opts.ignoreBOM) {
- break :blk struct {
- fn decode(_: ?*anyopaque, controller: *TransformStream.DefaultController, chunk: js.Value) !void {
- return decodeTransform(controller, chunk, true);
- }
- }.decode;
+ break :blk &.{
+ .transform = struct {
+ fn decode(_: ?*anyopaque, controller: *TransformStream.DefaultController, chunk: js.Value) !void {
+ return decodeTransform(controller, chunk, true);
+ }
+ }.decode,
+ .flush = TransformStream.ZigTransformer.noopFlush,
+ };
} else {
- break :blk struct {
- fn decode(_: ?*anyopaque, controller: *TransformStream.DefaultController, chunk: js.Value) !void {
- return decodeTransform(controller, chunk, false);
- }
- }.decode;
+ break :blk &.{
+ .transform = struct {
+ fn decode(_: ?*anyopaque, controller: *TransformStream.DefaultController, chunk: js.Value) !void {
+ return decodeTransform(controller, chunk, false);
+ }
+ }.decode,
+ .flush = TransformStream.ZigTransformer.noopFlush,
+ };
}
};
- const transform = try TransformStream.initWithZigTransformer(.{ .transform = decodeFn }, exec);
+ const transform = try TransformStream.initWithZigTransformer(.{ .vtable = vtable }, exec);
return .{
._transform = transform,
diff --git a/src/browser/webapi/encoding/TextEncoderStream.zig b/src/browser/webapi/encoding/TextEncoderStream.zig
index f1977e4e8..69a9427f8 100644
--- a/src/browser/webapi/encoding/TextEncoderStream.zig
+++ b/src/browser/webapi/encoding/TextEncoderStream.zig
@@ -29,7 +29,10 @@ const TextEncoderStream = @This();
_transform: *TransformStream,
pub fn init(exec: *const Execution) !TextEncoderStream {
- const transform = try TransformStream.initWithZigTransformer(.{ .transform = &encodeTransform }, exec);
+ const transform = try TransformStream.initWithZigTransformer(.{ .vtable = &.{
+ .transform = &encodeTransform,
+ .flush = TransformStream.ZigTransformer.noopFlush,
+ } }, exec);
return .{
._transform = transform,
};
diff --git a/src/browser/webapi/streams/TransformStream.zig b/src/browser/webapi/streams/TransformStream.zig
index 9dca4b991..14bbff7cf 100644
--- a/src/browser/webapi/streams/TransformStream.zig
+++ b/src/browser/webapi/streams/TransformStream.zig
@@ -28,14 +28,18 @@ const TransformStream = @This();
pub const DefaultController = TransformStreamDefaultController;
-pub const ZigTransformFn = *const fn (ctx: ?*anyopaque, *TransformStreamDefaultController, js.Value) anyerror!void;
-pub const ZigFlushFn = *const fn (ctx: ?*anyopaque, *TransformStreamDefaultController) anyerror!void;
-
-/// A transformer implemented in Zig; `ctx` is passed back to both callbacks.
+/// A transformer implemented in Zig; `ctx` is passed back to every `vtable` callback.
pub const ZigTransformer = struct {
ctx: ?*anyopaque = null,
- transform: ZigTransformFn,
- flush: ?ZigFlushFn = null,
+ vtable: *const VTable,
+
+ pub const VTable = struct {
+ transform: *const fn (ctx: ?*anyopaque, *TransformStreamDefaultController, js.Value) anyerror!void,
+ flush: *const fn (ctx: ?*anyopaque, *TransformStreamDefaultController) anyerror!void,
+ };
+
+ /// Use this when flush is not needed.
+ pub fn noopFlush(_: ?*anyopaque, _: *TransformStreamDefaultController) !void {}
};
_readable: *ReadableStream,
@@ -100,7 +104,7 @@ pub fn initWithZigTransformer(zig_transformer: ZigTransformer, exec: *const Exec
pub fn transformWrite(self: *TransformStream, chunk: js.Value, exec: *const Execution) !void {
if (self._controller._zig_transformer) |zig| {
// Zig-level transform (used by TextEncoderStream etc.)
- return zig.transform(zig.ctx, self._controller, chunk);
+ return zig.vtable.transform(zig.ctx, self._controller, chunk);
}
if (self._controller._transform_fn) |transform_fn| {
@@ -116,9 +120,7 @@ pub fn transformWrite(self: *TransformStream, chunk: js.Value, exec: *const Exec
pub fn transformClose(self: *TransformStream, exec: *const Execution) !void {
if (self._controller._zig_transformer) |zig| {
- if (zig.flush) |flush| {
- try flush(zig.ctx, self._controller);
- }
+ try zig.vtable.flush(zig.ctx, self._controller);
} else if (self._controller._flush_fn) |flush_fn| {
var ls: js.Local.Scope = undefined;
exec.js.localScope(&ls);
diff --git a/src/sys/brotli.zig b/src/sys/brotli.zig
new file mode 100644
index 000000000..154c8d67d
--- /dev/null
+++ b/src/sys/brotli.zig
@@ -0,0 +1,90 @@
+// Copyright (C) 2023-2026 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/>.
+
+//! brotli utilities.
+
+pub const BROTLI_BOOL = c_int;
+pub const BROTLI_TRUE: BROTLI_BOOL = 1;
+pub const BROTLI_FALSE: BROTLI_BOOL = 0;
+
+/// Passing null for both alloc_func and free_func makes brotli use malloc/free.
+const brotli_alloc_func = ?*const fn (@"opaque": ?*anyopaque, size: usize) callconv(.c) ?*anyopaque;
+const brotli_free_func = ?*const fn (@"opaque": ?*anyopaque, address: ?*anyopaque) callconv(.c) void;
+
+// Encoder.
+
+pub const BROTLI_MIN_QUALITY: u32 = 0;
+pub const BROTLI_MAX_QUALITY: u32 = 11;
+pub const BROTLI_DEFAULT_QUALITY: u32 = 11;
+pub const BROTLI_MIN_WINDOW_BITS: u32 = 10;
+pub const BROTLI_MAX_WINDOW_BITS: u32 = 24;
+pub const BROTLI_DEFAULT_WINDOW: u32 = 22;
+
+pub const BrotliEncoderState = opaque {};
+
+pub const BrotliEncoderMode = enum(c_int) {
+ generic = 0,
+ text = 1,
+ font = 2,
+};
+
+pub const BrotliEncoderOperation = enum(c_int) {
+ process = 0,
+ flush = 1,
+ finish = 2,
+ emit_metadata = 3,
+};
+
+pub const BrotliEncoderParameter = enum(c_int) {
+ mode = 0,
+ quality = 1,
+ lgwin = 2,
+ lgblock = 3,
+ disable_literal_context_modeling = 4,
+ size_hint = 5,
+ large_window = 6,
+ npostfix = 7,
+ ndirect = 8,
+ stream_offset = 9,
+};
+
+pub extern fn BrotliEncoderCreateInstance(alloc_func: brotli_alloc_func, free_func: brotli_free_func, @"opaque": ?*anyopaque) ?*BrotliEncoderState;
+pub extern fn BrotliEncoderSetParameter(state: *BrotliEncoderState, param: BrotliEncoderParameter, value: u32) BROTLI_BOOL;
+pub extern fn BrotliEncoderCompressStream(state: *BrotliEncoderState, op: BrotliEncoderOperation, available_in: *usize, next_in: *[*c]const u8, available_out: *usize, next_out: ?*[*c]u8, total_out: ?*usize) BROTLI_BOOL;
+pub extern fn BrotliEncoderIsFinished(state: *BrotliEncoderState) BROTLI_BOOL;
+pub extern fn BrotliEncoderHasMoreOutput(state: *BrotliEncoderState) BROTLI_BOOL;
+pub extern fn BrotliEncoderDestroyInstance(state: *BrotliEncoderState) void;
+pub extern fn BrotliEncoderTakeOutput(state: *BrotliEncoderState, size: *usize) [*c]const u8;
+
+// Decoder.
+
+pub const BrotliDecoderState = opaque {};
+
+pub const BrotliDecoderResult = enum(c_int) {
+ @"error" = 0,
+ success = 1,
+ needs_more_input = 2,
+ needs_more_output = 3,
+};
+
+pub extern fn BrotliDecoderCreateInstance(alloc_func: brotli_alloc_func, free_func: brotli_free_func, @"opaque": ?*anyopaque) ?*BrotliDecoderState;
+pub extern fn BrotliDecoderDecompressStream(state: *BrotliDecoderState, available_in: *usize, next_in: *[*c]const u8, available_out: *usize, next_out: ?*[*c]u8, total_out: ?*usize) BrotliDecoderResult;
+pub extern fn BrotliDecoderIsFinished(state: *const BrotliDecoderState) BROTLI_BOOL;
+pub extern fn BrotliDecoderHasMoreOutput(state: *const BrotliDecoderState) BROTLI_BOOL;
+pub extern fn BrotliDecoderDestroyInstance(state: *BrotliDecoderState) void;
+pub extern fn BrotliDecoderTakeOutput(state: *BrotliDecoderState, size: *usize) [*c]const u8;