add 'arraybuffer' responseType to XHR

This commit is contained in:
Karl Seguin
2026-02-02 07:45:21 +08:00
parent 7c98a27c53
commit 176d42f625
3 changed files with 31 additions and 0 deletions

View File

@@ -125,6 +125,26 @@
})
</script>
<script id=xhr6>
const req6 = new XMLHttpRequest()
testing.async(async (restore) => {
await new Promise((resolve) => {
req6.onload = resolve;
req6.open('GET', 'http://127.0.0.1:9582/xhr/binary')
req6.responseType ='arraybuffer'
req6.send()
});
restore();
testing.expectEqual(200, req6.status);
testing.expectEqual('OK', req6.statusText);
testing.expectEqual(7, req6.response.byteLength);
testing.expectEqual([0, 0, 1, 2, 0, 0, 9], new Int32Array(req6.response));
testing.expectEqual('', typeof req6.response);
testing.expectEqual('arraybuffer', req6.responseType);
});
</script>
<script id=xhr_redirect>
testing.async(async (restore) => {
const req = new XMLHttpRequest();

View File

@@ -69,12 +69,14 @@ const Response = union(ResponseType) {
text: []const u8,
json: js.Value.Global,
document: *Node.Document,
arraybuffer: js.ArrayBuffer,
};
const ResponseType = enum {
text,
json,
document,
arraybuffer,
// TODO: other types to support
};
@@ -302,6 +304,7 @@ pub fn getResponse(self: *XMLHttpRequest, page: *Page) !?Response {
try page.parseHtmlAsChildren(document.asNode(), data);
break :blk .{ .document = document };
},
.arraybuffer => .{ .arraybuffer = .{ .values = data } },
};
self._response = res;

View File

@@ -558,6 +558,14 @@ fn testHTTPHandler(req: *std.http.Server.Request) !void {
});
}
if (std.mem.eql(u8, path, "/xhr/binary")) {
return req.respond(&.{ 0, 0, 1, 2, 0, 0, 9 }, .{
.extra_headers = &.{
.{ .name = "Content-Type", .value = "application/octet-stream" },
},
});
}
if (std.mem.startsWith(u8, path, "/src/browser/tests/")) {
// strip off leading / so that it's relative to CWD
return TestHTTPServer.sendFile(req, path[1..]);