Ignore BOM only when the option is set on TextDecoderStream

This commit is contained in:
Pierre Tachoire
2026-03-03 08:47:36 +01:00
parent 24221748e1
commit 252b3c3bf6
2 changed files with 21 additions and 4 deletions

View File

@@ -47,7 +47,24 @@ pub fn init(label_: ?[]const u8, opts_: ?InitOpts, page: *Page) !TextDecoderStre
}
const opts = opts_ orelse InitOpts{};
const transform = try TransformStream.initWithZigTransform(&decodeTransform, page);
const decodeFn: TransformStream.ZigTransformFn = blk: {
if (opts.ignoreBOM) {
break :blk struct {
fn decode(controller: *TransformStream.DefaultController, chunk: js.Value) !void {
return decodeTransform(controller, chunk, true);
}
}.decode;
} else {
break :blk struct {
fn decode(controller: *TransformStream.DefaultController, chunk: js.Value) !void {
return decodeTransform(controller, chunk, false);
}
}.decode;
}
};
const transform = try TransformStream.initWithZigTransform(decodeFn, page);
return .{
._transform = transform,
._fatal = opts.fatal,
@@ -55,13 +72,13 @@ pub fn init(label_: ?[]const u8, opts_: ?InitOpts, page: *Page) !TextDecoderStre
};
}
fn decodeTransform(controller: *TransformStream.DefaultController, chunk: js.Value) !void {
fn decodeTransform(controller: *TransformStream.DefaultController, chunk: js.Value, ignoreBOM: bool) !void {
// chunk should be a Uint8Array; decode it as UTF-8 string
const typed_array = try chunk.toZig(js.TypedArray(u8));
var input = typed_array.values;
// Strip UTF-8 BOM if present
if (std.mem.startsWith(u8, input, &.{ 0xEF, 0xBB, 0xBF })) {
if (ignoreBOM == false and std.mem.startsWith(u8, input, &.{ 0xEF, 0xBB, 0xBF })) {
input = input[3..];
}

View File

@@ -27,7 +27,7 @@ const TransformStream = @This();
pub const DefaultController = TransformStreamDefaultController;
const ZigTransformFn = *const fn (*TransformStreamDefaultController, js.Value) anyerror!void;
pub const ZigTransformFn = *const fn (*TransformStreamDefaultController, js.Value) anyerror!void;
_readable: *ReadableStream,
_writable: *WritableStream,