diff options
-rw-r--r-- | integration/bunjs-only-snippets/fetch.test.js | 15 | ||||
-rw-r--r-- | src/javascript/jsc/bindings/bindings.cpp | 9 | ||||
-rw-r--r-- | src/javascript/jsc/bindings/bindings.zig | 7 | ||||
-rw-r--r-- | src/javascript/jsc/webcore/response.zig | 3 |
4 files changed, 33 insertions, 1 deletions
diff --git a/integration/bunjs-only-snippets/fetch.test.js b/integration/bunjs-only-snippets/fetch.test.js index b1ab57366..2f976fd83 100644 --- a/integration/bunjs-only-snippets/fetch.test.js +++ b/integration/bunjs-only-snippets/fetch.test.js @@ -376,6 +376,21 @@ describe("Response", () => { expect(await clone.text()).toBe("<div>hello</div>"); gc(); }); + it("invalid json", async () => { + gc(); + var body = new Response("<div>hello</div>", { + headers: { + "content-type": "text/html; charset=utf-8", + }, + }); + try { + await body.json(); + expect(false).toBe(true); + } catch (exception) { + expect(exception instanceof SyntaxError); + } + }); + testBlobInterface((data) => new Response(data), true); }); diff --git a/src/javascript/jsc/bindings/bindings.cpp b/src/javascript/jsc/bindings/bindings.cpp index aaff3102c..112ca81b7 100644 --- a/src/javascript/jsc/bindings/bindings.cpp +++ b/src/javascript/jsc/bindings/bindings.cpp @@ -249,7 +249,14 @@ unsigned char JSC__JSValue__jsType(JSC__JSValue JSValue0) JSC__JSValue JSC__JSValue__parseJSON(JSC__JSValue JSValue0, JSC__JSGlobalObject* arg1) { JSC::JSValue jsValue = JSC::JSValue::decode(JSValue0); - return JSC::JSValue::encode(JSC::JSONParse(arg1, jsValue.toWTFString(arg1))); + + JSC::JSValue result = JSC::JSONParse(arg1, jsValue.toWTFString(arg1)); + + if (!result) { + result = JSC::JSValue(JSC::createSyntaxError(arg1->globalObject(), "Failed to parse JSON")); + } + + return JSC::JSValue::encode(result); } void JSC__JSGlobalObject__deleteModuleRegistryEntry(JSC__JSGlobalObject* global, ZigString* arg1) diff --git a/src/javascript/jsc/bindings/bindings.zig b/src/javascript/jsc/bindings/bindings.zig index 3b3182809..88ecb6bfe 100644 --- a/src/javascript/jsc/bindings/bindings.zig +++ b/src/javascript/jsc/bindings/bindings.zig @@ -2007,6 +2007,13 @@ pub const JSValue = enum(u64) { else => false, }; } + /// Empty as in "JSValue {}" rather than an empty string + pub fn isEmpty(this: JSValue) bool { + return switch (@enumToInt(this)) { + 0 => true, + else => false, + }; + } pub fn isBoolean(this: JSValue) bool { return cppFn("isBoolean", .{this}); } diff --git a/src/javascript/jsc/webcore/response.zig b/src/javascript/jsc/webcore/response.zig index 721ffc5ca..c907c4b0f 100644 --- a/src/javascript/jsc/webcore/response.zig +++ b/src/javascript/jsc/webcore/response.zig @@ -1708,6 +1708,9 @@ pub const Blob = struct { value: JSC.JSValue, global: *JSGlobalObject, ) JSC.JSValue { + if (value.isError()) { + return JSC.JSPromise.rejectedPromiseValue(global, value); + } return JSC.JSPromise.resolvedPromiseValue(global, value); } |