|
| 1 | +// Copyright (C) 2023-2025 Lightpanda (Selecy SAS) |
| 2 | +// |
| 3 | +// Francis Bouvier <francis@lightpanda.io> |
| 4 | +// Pierre Tachoire <pierre@lightpanda.io> |
| 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 Writer = std.Io.Writer; |
| 21 | + |
| 22 | +const builtin = @import("builtin"); |
| 23 | +const is_windows = builtin.os.tag == .windows; |
| 24 | + |
| 25 | +const Page = @import("../page.zig").Page; |
| 26 | +const js = @import("../js/js.zig"); |
| 27 | + |
| 28 | +/// https://w3c.github.io/FileAPI/#blob-section |
| 29 | +/// https://developer.mozilla.org/en-US/docs/Web/API/Blob |
| 30 | +const Blob = @This(); |
| 31 | + |
| 32 | +/// Immutable slice of blob. |
| 33 | +/// Note that another blob may hold a pointer/slice to this, |
| 34 | +/// so its better to leave the deallocation of it to arena allocator. |
| 35 | +slice: []const u8, |
| 36 | +/// MIME attached to blob. Can be an empty string. |
| 37 | +mime: []const u8, |
| 38 | + |
| 39 | +const ConstructorOptions = struct { |
| 40 | + /// MIME type. |
| 41 | + type: []const u8 = "", |
| 42 | + /// How to handle newline (LF) characters. |
| 43 | + /// `transparent` means do nothing, `native` expects CRLF (\r\n) on Windows. |
| 44 | + endings: []const u8 = "transparent", |
| 45 | +}; |
| 46 | + |
| 47 | +/// Creates a new Blob. |
| 48 | +pub fn constructor( |
| 49 | + maybe_blob_parts: ?[]const []const u8, |
| 50 | + maybe_options: ?ConstructorOptions, |
| 51 | + page: *Page, |
| 52 | +) !Blob { |
| 53 | + const options: ConstructorOptions = maybe_options orelse .{}; |
| 54 | + |
| 55 | + if (maybe_blob_parts) |blob_parts| { |
| 56 | + var w: Writer.Allocating = .init(page.arena); |
| 57 | + const use_native_endings = std.mem.eql(u8, options.endings, "native"); |
| 58 | + try writeBlobParts(&w.writer, blob_parts, use_native_endings); |
| 59 | + |
| 60 | + const written = w.written(); |
| 61 | + |
| 62 | + return .{ .slice = written, .mime = options.type }; |
| 63 | + } |
| 64 | + |
| 65 | + // We don't have `blob_parts`, why would you want a Blob anyway then? |
| 66 | + return .{ .slice = "", .mime = options.type }; |
| 67 | +} |
| 68 | + |
| 69 | +/// Writes blob parts to given `Writer` by desired encoding. |
| 70 | +fn writeBlobParts( |
| 71 | + writer: *Writer, |
| 72 | + blob_parts: []const []const u8, |
| 73 | + use_native_endings: bool, |
| 74 | +) !void { |
| 75 | + // Transparent. |
| 76 | + if (!use_native_endings) { |
| 77 | + for (blob_parts) |part| { |
| 78 | + try writer.writeAll(part); |
| 79 | + } |
| 80 | + |
| 81 | + return; |
| 82 | + } |
| 83 | + |
| 84 | + // TODO: Windows support. |
| 85 | + // TODO: Vector search. |
| 86 | + |
| 87 | + // Linux & Unix. |
| 88 | + // Both Firefox and Chrome implement it as such: |
| 89 | + // CRLF => LF |
| 90 | + // CR => LF |
| 91 | + // So even though CR is not followed by LF, it gets replaced. |
| 92 | + // |
| 93 | + // I believe this is because such scenario is possible: |
| 94 | + // ``` |
| 95 | + // let parts = [ "the quick\r", "\nbrown fox" ]; |
| 96 | + // ``` |
| 97 | + // In the example, one should have to check the part before in order to |
| 98 | + // understand that CRLF is being presented in the final buffer. |
| 99 | + // So they took a simpler approach, here's what given blob parts produce: |
| 100 | + // ``` |
| 101 | + // "the quick\n\nbrown fox" |
| 102 | + // ``` |
| 103 | + scan_parts: for (blob_parts) |part| { |
| 104 | + var end: usize = 0; |
| 105 | + var start = end; |
| 106 | + while (end < part.len) { |
| 107 | + if (part[end] == '\r') { |
| 108 | + try writer.writeAll(part[start..end]); |
| 109 | + try writer.writeByte('\n'); |
| 110 | + |
| 111 | + // Part ends with CR. We can continue to next part. |
| 112 | + if (end + 1 == part.len) { |
| 113 | + continue :scan_parts; |
| 114 | + } |
| 115 | + |
| 116 | + // If next char is LF, skip it too. |
| 117 | + if (part[end + 1] == '\n') { |
| 118 | + start = end + 2; |
| 119 | + } else { |
| 120 | + start = end + 1; |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + end += 1; |
| 125 | + } |
| 126 | + |
| 127 | + // Write the remaining. We get this in such situations: |
| 128 | + // `the quick brown\rfox` |
| 129 | + // `the quick brown\r\nfox` |
| 130 | + try writer.writeAll(part[start..end]); |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +pub fn get_size(self: *const Blob) usize { |
| 135 | + return self.slice.len; |
| 136 | +} |
| 137 | + |
| 138 | +pub fn get_str(self: *const Blob) []const u8 { |
| 139 | + return self.slice; |
| 140 | +} |
| 141 | + |
| 142 | +const testing = @import("../../testing.zig"); |
| 143 | +test "Browser: File.Blob" { |
| 144 | + try testing.htmlRunner("file/blob.html"); |
| 145 | +} |
0 commit comments