diff options
author | 2023-03-18 16:15:21 -0700 | |
---|---|---|
committer | 2023-03-18 16:15:21 -0700 | |
commit | 96fcecdcc9ebae4c81b0238519eaeb4cca2b9881 (patch) | |
tree | 8a9adea138bba3e8412ff611185bddc890836501 | |
parent | 12c4ed89e380fa248f7db115a9df63478a7a3f56 (diff) | |
download | bun-96fcecdcc9ebae4c81b0238519eaeb4cca2b9881.tar.gz bun-96fcecdcc9ebae4c81b0238519eaeb4cca2b9881.tar.zst bun-96fcecdcc9ebae4c81b0238519eaeb4cca2b9881.zip |
[fetch] Make the default body value `null` when unspecified
This is better aligned with the fetch spec
-rw-r--r-- | src/bun.js/api/server.zig | 14 | ||||
-rw-r--r-- | src/bun.js/webcore/body.zig | 16 | ||||
-rw-r--r-- | src/bun.js/webcore/request.zig | 4 | ||||
-rw-r--r-- | test/js/web/fetch/body.test.ts | 39 |
4 files changed, 58 insertions, 15 deletions
diff --git a/src/bun.js/api/server.zig b/src/bun.js/api/server.zig index d9f59bb07..3cfce400e 100644 --- a/src/bun.js/api/server.zig +++ b/src/bun.js/api/server.zig @@ -2252,7 +2252,7 @@ fn NewRequestContext(comptime ssl_enabled: bool, comptime debug_mode: bool, comp this.has_called_error_handler = true; var args = [_]JSC.C.JSValueRef{value.asObjectRef()}; const result = JSC.C.JSObjectCallAsFunctionReturnValue(this.server.globalThis, this.server.config.onError.asObjectRef(), this.server.thisObject.asObjectRef(), 1, &args); - + defer result.ensureStillAlive(); if (!result.isEmptyOrUndefinedOrNull()) { if (result.toError()) |err| { this.finishRunningErrorHandler(err, status); @@ -2535,7 +2535,7 @@ fn NewRequestContext(comptime ssl_enabled: bool, comptime debug_mode: bool, comp if (request.as(Request)) |req| { var old = req.body; old.Locked.onReceiveValue = null; - req.body = .{ .Empty = {} }; + req.body = .{ .Null = {} }; old.resolve(&req.body, this.server.globalThis); return; } @@ -2546,7 +2546,7 @@ fn NewRequestContext(comptime ssl_enabled: bool, comptime debug_mode: bool, comp if (request.as(Request)) |req| { var old = req.body; old.Locked.onReceiveValue = null; - req.body = .{ .Empty = {} }; + req.body = .{ .Null = {} }; old.toError(error.RequestBodyTooLarge, this.server.globalThis); return; } @@ -2563,7 +2563,7 @@ fn NewRequestContext(comptime ssl_enabled: bool, comptime debug_mode: bool, comp if (request.as(Request)) |req| { var old = req.body; old.Locked.onReceiveValue = null; - req.body = .{ .Empty = {} }; + req.body = .{ .Null = {} }; old.resolve(&req.body, this.server.globalThis); return; } @@ -4319,7 +4319,7 @@ pub fn NewServer(comptime ssl_enabled_: bool, comptime debug_mode_: bool) type { var url: URL = undefined; var first_arg = args.nextEat().?; - var body: JSC.WebCore.Body.Value = .{ .Empty = {} }; + var body: JSC.WebCore.Body.Value = .{ .Null = {} }; var existing_request: ?WebCore.Request = null; // TODO: set Host header // TODO: set User-Agent header @@ -4738,7 +4738,7 @@ pub fn NewServer(comptime ssl_enabled_: bool, comptime debug_mode_: bool) type { .uws_request = req, .https = ssl_enabled, .body = .{ - .Empty = {}, + .Null = {}, }, }; @@ -4822,7 +4822,7 @@ pub fn NewServer(comptime ssl_enabled_: bool, comptime debug_mode_: bool) type { .upgrader = ctx, .https = ssl_enabled, .body = .{ - .Empty = {}, + .Null = {}, }, }; ctx.upgrade_context = upgrade_ctx; diff --git a/src/bun.js/webcore/body.zig b/src/bun.js/webcore/body.zig index 2eed85808..85a52bc3f 100644 --- a/src/bun.js/webcore/body.zig +++ b/src/bun.js/webcore/body.zig @@ -416,7 +416,7 @@ pub const Body = struct { } if (drain_result == .empty or drain_result == .aborted) { - this.* = .{ .Empty = void{} }; + this.* = .{ .Null = void{} }; return JSC.WebCore.ReadableStream.empty(globalThis); } @@ -849,12 +849,12 @@ pub const Body = struct { if (tag == .InternalBlob) { this.InternalBlob.clearAndFree(); - this.* = Value{ .Empty = {} }; //Value.empty; + this.* = Value{ .Null = {} }; //Value.empty; } if (tag == .Blob) { this.Blob.deinit(); - this.* = Value{ .Empty = {} }; //Value.empty; + this.* = Value{ .Null = {} }; //Value.empty; } if (tag == .Error) { @@ -882,6 +882,10 @@ pub const Body = struct { return Value{ .Blob = this.Blob.dupe() }; } + if (this.* == .Null) { + return Value{ .Null = {} }; + } + return Value{ .Empty = {} }; } }; @@ -892,7 +896,7 @@ pub const Body = struct { .headers = null, .status_code = 404, }, - .value = Value{ .Empty = {} }, //Value.empty, + .value = Value{ .Null = {} }, }; } @@ -901,7 +905,7 @@ pub const Body = struct { .init = Init{ .status_code = 200, }, - .value = Value{ .Empty = {} }, //Value.empty, + .value = Value{ .Null = {} }, }; } @@ -938,7 +942,7 @@ pub const Body = struct { init: JSValue, ) ?Body { var body = Body{ - .value = Value{ .Empty = {} }, + .value = Value{ .Null = {} }, .init = Init{ .headers = null, .status_code = 200 }, }; var allocator = getAllocator(globalThis); diff --git a/src/bun.js/webcore/request.zig b/src/bun.js/webcore/request.zig index d6484d2ba..ede8cab5d 100644 --- a/src/bun.js/webcore/request.zig +++ b/src/bun.js/webcore/request.zig @@ -59,7 +59,7 @@ pub const Request = struct { headers: ?*FetchHeaders = null, signal: ?*AbortSignal = null, - body: Body.Value = Body.Value{ .Empty = {} }, + body: Body.Value = Body.Value{ .Null = {} }, method: Method = Method.GET, uws_request: ?*uws.Request = null, https: bool = false, @@ -164,7 +164,7 @@ pub const Request = struct { pub fn fromRequestContext(ctx: *RequestContext) !Request { var req = Request{ .url = bun.asByteSlice(ctx.getFullURL()), - .body = .{ .Empty = {} }, + .body = .{ .Null = {} }, .method = ctx.method, .headers = FetchHeaders.createFromPicoHeaders(ctx.request.headers), .url_was_allocated = true, diff --git a/test/js/web/fetch/body.test.ts b/test/js/web/fetch/body.test.ts index 40a70c5b3..fa3d4e53b 100644 --- a/test/js/web/fetch/body.test.ts +++ b/test/js/web/fetch/body.test.ts @@ -624,6 +624,45 @@ for (const { body, fn } of bodyTypes) { }); } }); + + describe("new Response()", () => { + ["text", "arrayBuffer", "blob"].map(method => { + test(method, async () => { + const result = new Response(); + expect(result).toHaveProperty("bodyUsed", false); + + // @ts-expect-error + await result[method](); + expect(result).toHaveProperty("bodyUsed", false); + }); + }); + }); + + describe('new Request(url, {method: "POST" })', () => { + ["text", "arrayBuffer", "blob"].map(method => { + test(method, async () => { + const result = new Request("https://example.com", { method: "POST" }); + expect(result).toHaveProperty("bodyUsed", false); + + // @ts-expect-error + await result[method](); + expect(result).toHaveProperty("bodyUsed", false); + }); + }); + }); + + describe("new Request(url)", () => { + ["text", "arrayBuffer", "blob"].map(method => { + test(method, async () => { + const result = new Request("https://example.com"); + expect(result).toHaveProperty("bodyUsed", false); + + // @ts-expect-error + await result[method](); + expect(result).toHaveProperty("bodyUsed", false); + }); + }); + }); }); } |