The most basic File implementation.

Almost silly as-is, but handles this case:

```
if (input instanceof File) {
   throw Error('file not supported')
}
```

as seen on reddit.
This commit is contained in:
Karl Seguin
2025-08-26 13:25:30 +08:00
parent ccc9618102
commit c7674926c3
2 changed files with 41 additions and 0 deletions

View File

@@ -34,6 +34,7 @@ const WebApis = struct {
@import("url/url.zig").Interfaces,
@import("xhr/xhr.zig").Interfaces,
@import("xhr/form_data.zig").Interfaces,
@import("xhr/File.zig"),
@import("xmlserializer/xmlserializer.zig").Interfaces,
});
};

40
src/browser/xhr/File.zig Normal file
View File

@@ -0,0 +1,40 @@
// Copyright (C) 2023-2025 Lightpanda (Selecy SAS)
//
// Francis Bouvier <francis@lightpanda.io>
// Pierre Tachoire <pierre@lightpanda.io>
//
// 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");
// https://w3c.github.io/FileAPI/#file-section
const File = @This();
// Very incomplete. The prototype for this is Blob, which we don't have.
// This minimum "implementation" is added because some JavaScript code just
// checks: if (x instanceof File) throw Error(...)
pub fn constructor() File {
return .{};
}
const testing = @import("../../testing.zig");
test "Browser.File" {
var runner = try testing.jsRunner(testing.tracking_allocator, .{ .html = "" });
defer runner.deinit();
try runner.testCases(&.{
.{ "let f = new File()", null },
.{ "f instanceof File", "true" },
}, .{});
}