diff options
author | 2023-02-28 00:02:44 -0800 | |
---|---|---|
committer | 2023-02-28 00:12:32 -0800 | |
commit | 12d6db0cad2554562f3f5034c3bc5c448702a270 (patch) | |
tree | 4dfac5705969ea2b3a31e5d2f3a1f2800808c470 | |
parent | ec7929b251b91745f676ce85f173516186f36c6e (diff) | |
download | bun-12d6db0cad2554562f3f5034c3bc5c448702a270.tar.gz bun-12d6db0cad2554562f3f5034c3bc5c448702a270.tar.zst bun-12d6db0cad2554562f3f5034c3bc5c448702a270.zip |
Add explicit Null tag to Body
This reverts commit e538bb31ad7a2c4b4ce2b1f7d6b18a3140939950.
-rw-r--r-- | src/bun.js/webcore/body.zig | 22 | ||||
-rw-r--r-- | test/bun.js/fetch.test.js | 139 |
2 files changed, 68 insertions, 93 deletions
diff --git a/src/bun.js/webcore/body.zig b/src/bun.js/webcore/body.zig index f76587f74..97c9f7874 100644 --- a/src/bun.js/webcore/body.zig +++ b/src/bun.js/webcore/body.zig @@ -294,6 +294,7 @@ pub const Body = struct { Used: void, Empty: void, Error: JSValue, + Null: void, pub fn toBlobIfPossible(this: *Value) void { if (this.* != .Locked) @@ -355,6 +356,7 @@ pub const Body = struct { Used, Empty, Error, + Null, }; // pub const empty = Value{ .Empty = void{} }; @@ -363,12 +365,12 @@ pub const Body = struct { JSC.markBinding(@src()); switch (this.*) { - .Empty => { - return JSValue.jsNull(); - }, - .Used => { + .Used, .Empty => { return JSC.WebCore.ReadableStream.empty(globalThis); }, + .Null => { + return JSValue.null; + }, .InternalBlob, .Blob, // .InlineBlob, @@ -436,12 +438,15 @@ pub const Body = struct { } } - pub fn fromJS(globalThis: *JSGlobalObject, value: JSValue) ?Value { + pub fn fromJS( + globalThis: *JSGlobalObject, + value: JSValue, + ) ?Value { value.ensureStillAlive(); if (value.isEmptyOrUndefinedOrNull()) { return Body.Value{ - .Empty = void{}, + .Null = void{}, }; } @@ -976,10 +981,6 @@ pub fn BodyMixin(comptime Type: type) type { ) callconv(.C) JSValue { var body: *Body.Value = this.getBodyValue(); - if (body.* == .Empty) { - return JSValue.jsNull(); - } - if (body.* == .Used) { // TODO: make this closed return JSC.WebCore.ReadableStream.empty(globalThis); @@ -1001,7 +1002,6 @@ pub fn BodyMixin(comptime Type: type) type { _: *JSC.CallFrame, ) callconv(.C) JSC.JSValue { var value: *Body.Value = this.getBodyValue(); - if (value.* == .Used) { return handleBodyAlreadyUsed(globalObject); } diff --git a/test/bun.js/fetch.test.js b/test/bun.js/fetch.test.js index 1c27c53a0..ec903341f 100644 --- a/test/bun.js/fetch.test.js +++ b/test/bun.js/fetch.test.js @@ -523,31 +523,33 @@ function testBlobInterface(blobbyConstructor, hasBlobFn) { if (withGC) gc(); }); - it(`${jsonObject.hello === true ? "latin1" : "utf16"} arrayBuffer -> json${withGC ? " (with gc) " : "" - }`, async () => { - if (withGC) gc(); - var response = blobbyConstructor(new TextEncoder().encode(JSON.stringify(jsonObject))); - if (withGC) gc(); - expect(JSON.stringify(await response.json())).toBe(JSON.stringify(jsonObject)); - if (withGC) gc(); - }); + it(`${jsonObject.hello === true ? "latin1" : "utf16"} arrayBuffer -> json${ + withGC ? " (with gc) " : "" + }`, async () => { + if (withGC) gc(); + var response = blobbyConstructor(new TextEncoder().encode(JSON.stringify(jsonObject))); + if (withGC) gc(); + expect(JSON.stringify(await response.json())).toBe(JSON.stringify(jsonObject)); + if (withGC) gc(); + }); - it(`${jsonObject.hello === true ? "latin1" : "utf16"} arrayBuffer -> invalid json${withGC ? " (with gc) " : "" - }`, async () => { - if (withGC) gc(); - var response = blobbyConstructor( - new TextEncoder().encode(JSON.stringify(jsonObject) + " NOW WE ARE INVALID JSON"), - ); - if (withGC) gc(); - var failed = false; - try { - await response.json(); - } catch (e) { - failed = true; - } - expect(failed).toBe(true); - if (withGC) gc(); - }); + it(`${jsonObject.hello === true ? "latin1" : "utf16"} arrayBuffer -> invalid json${ + withGC ? " (with gc) " : "" + }`, async () => { + if (withGC) gc(); + var response = blobbyConstructor( + new TextEncoder().encode(JSON.stringify(jsonObject) + " NOW WE ARE INVALID JSON"), + ); + if (withGC) gc(); + var failed = false; + try { + await response.json(); + } catch (e) { + failed = true; + } + expect(failed).toBe(true); + if (withGC) gc(); + }); it(`${jsonObject.hello === true ? "latin1" : "utf16"} text${withGC ? " (with gc) " : ""}`, async () => { if (withGC) gc(); @@ -557,14 +559,15 @@ function testBlobInterface(blobbyConstructor, hasBlobFn) { if (withGC) gc(); }); - it(`${jsonObject.hello === true ? "latin1" : "utf16"} arrayBuffer -> text${withGC ? " (with gc) " : "" - }`, async () => { - if (withGC) gc(); - var response = blobbyConstructor(new TextEncoder().encode(JSON.stringify(jsonObject))); - if (withGC) gc(); - expect(await response.text()).toBe(JSON.stringify(jsonObject)); - if (withGC) gc(); - }); + it(`${jsonObject.hello === true ? "latin1" : "utf16"} arrayBuffer -> text${ + withGC ? " (with gc) " : "" + }`, async () => { + if (withGC) gc(); + var response = blobbyConstructor(new TextEncoder().encode(JSON.stringify(jsonObject))); + if (withGC) gc(); + expect(await response.text()).toBe(JSON.stringify(jsonObject)); + if (withGC) gc(); + }); it(`${jsonObject.hello === true ? "latin1" : "utf16"} arrayBuffer${withGC ? " (with gc) " : ""}`, async () => { if (withGC) gc(); @@ -589,29 +592,30 @@ function testBlobInterface(blobbyConstructor, hasBlobFn) { if (withGC) gc(); }); - it(`${jsonObject.hello === true ? "latin1" : "utf16"} arrayBuffer -> arrayBuffer${withGC ? " (with gc) " : "" - }`, async () => { - if (withGC) gc(); + it(`${jsonObject.hello === true ? "latin1" : "utf16"} arrayBuffer -> arrayBuffer${ + withGC ? " (with gc) " : "" + }`, async () => { + if (withGC) gc(); - var response = blobbyConstructor(new TextEncoder().encode(JSON.stringify(jsonObject))); - if (withGC) gc(); + var response = blobbyConstructor(new TextEncoder().encode(JSON.stringify(jsonObject))); + if (withGC) gc(); - const bytes = new TextEncoder().encode(JSON.stringify(jsonObject)); - if (withGC) gc(); + const bytes = new TextEncoder().encode(JSON.stringify(jsonObject)); + if (withGC) gc(); - const compare = new Uint8Array(await response.arrayBuffer()); - if (withGC) gc(); + const compare = new Uint8Array(await response.arrayBuffer()); + if (withGC) gc(); - withoutAggressiveGC(() => { - for (let i = 0; i < compare.length; i++) { - if (withGC) gc(); + withoutAggressiveGC(() => { + for (let i = 0; i < compare.length; i++) { + if (withGC) gc(); - expect(compare[i]).toBe(bytes[i]); - if (withGC) gc(); - } - }); - if (withGC) gc(); + expect(compare[i]).toBe(bytes[i]); + if (withGC) gc(); + } }); + if (withGC) gc(); + }); hasBlobFn && it(`${jsonObject.hello === true ? "latin1" : "utf16"} blob${withGC ? " (with gc) " : ""}`, async () => { @@ -668,7 +672,7 @@ describe("Bun.file", () => { it("size is Infinity on a fifo", () => { try { unlinkSync("/tmp/test-fifo"); - } catch (e) { } + } catch (e) {} mkfifo("/tmp/test-fifo"); const { size } = Bun.file("/tmp/test-fifo"); @@ -686,14 +690,14 @@ describe("Bun.file", () => { beforeAll(async () => { try { unlinkSync("/tmp/my-new-file"); - } catch { } + } catch {} await Bun.write("/tmp/my-new-file", "hey"); chmodSync("/tmp/my-new-file", 0o000); }); afterAll(() => { try { unlinkSync("/tmp/my-new-file"); - } catch { } + } catch {} }); forEachMethod(m => () => { @@ -706,7 +710,7 @@ describe("Bun.file", () => { beforeAll(() => { try { unlinkSync("/tmp/does-not-exist"); - } catch { } + } catch {} }); forEachMethod(m => async () => { @@ -857,12 +861,6 @@ describe("Response", () => { expect(response.status).toBe(407); }); - it("body nullable", () => { - expect(new Response(null).body).toBeNull(); - expect(new Response(undefined).body).toBeNull(); - expect(new Response().body).toBeNull(); - }); - it("supports headers", () => { var response = Response.json("hello", { headers: { @@ -978,29 +976,6 @@ describe("Request", () => { expect(await clone.text()).toBe("<div>hello</div>"); }); - it("body nullable", async () => { - gc(); - { - const req = new Request("https://hello.com", { body: null }); - expect(req.body).toBeNull(); - } - gc(); - { - const req = new Request("https://hello.com", { body: undefined }); - expect(req.body).toBeNull(); - } - gc(); - { - const req = new Request("https://hello.com"); - expect(req.body).toBeNull(); - } - gc(); - { - const req = new Request("https://hello.com", { body: "" }); - expect(req.body).toBeNull(); - } - }); - it("signal", async () => { gc(); const controller = new AbortController(); |