Skip to content

Commit

Permalink
[fetch] Add Response.json static method
Browse files Browse the repository at this point in the history
This commit adds support for the `Response.json` static method as
specified in whatwg/fetch#1392.

All WPTs from web-platform-tests/wpt#32825 pass.

Bug: 1305358
Change-Id: Iaafdd514ed12644b6433c02e7d076ce43f51b9ef
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3639064
Reviewed-by: Yutaka Hirano <yhirano@chromium.org>
Reviewed-by: Adam Rice <ricea@chromium.org>
Commit-Queue: Yutaka Hirano <yhirano@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1019479}
  • Loading branch information
lucacasonato authored and Chromium LUCI CQ committed Jun 30, 2022
1 parent 43b91dc commit e680cd1
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 80 deletions.
34 changes: 34 additions & 0 deletions third_party/blink/renderer/core/fetch/response.cc
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,40 @@ Response* Response::redirect(ScriptState* script_state,
return r;
}

Response* Response::staticJson(ScriptState* script_state,
ScriptValue data,
const ResponseInit* init,
ExceptionState& exception_state) {
// "1. Let bytes the result of running serialize a JavaScript value to JSON
// bytes on data."
v8::Local<v8::String> v8_string;
v8::TryCatch try_catch(script_state->GetIsolate());
if (!v8::JSON::Stringify(script_state->GetContext(), data.V8Value())
.ToLocal(&v8_string)) {
exception_state.RethrowV8Exception(try_catch.Exception());
return nullptr;
}

String string = ToBlinkString<String>(v8_string, kDoNotExternalize);

// JSON.stringify can fail to produce a string value in one of two ways: it
// can throw an exception (as with unserializable objects), or it can return
// `undefined` (as with e.g. passing a function). If JSON.stringify returns
// `undefined`, the v8 API then coerces it to the string value "undefined".
// Check for this, and consider it a failure.
if (string == "undefined") {
exception_state.ThrowTypeError("The data is not JSON serializable");
return nullptr;
}

BodyStreamBuffer* body_buffer = BodyStreamBuffer::Create(
script_state, MakeGarbageCollected<FormDataBytesConsumer>(string),
/*abort_signal=*/nullptr, /*cached_metadata_handler=*/nullptr);
String content_type = "application/json";

return Create(script_state, body_buffer, content_type, init, exception_state);
}

FetchResponseData* Response::CreateUnfilteredFetchResponseDataWithoutBody(
ScriptState* script_state,
mojom::blink::FetchAPIResponse& fetch_api_response) {
Expand Down
4 changes: 4 additions & 0 deletions third_party/blink/renderer/core/fetch/response.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ class CORE_EXPORT Response final : public ScriptWrappable,
const String& url,
uint16_t status,
ExceptionState&);
static Response* staticJson(ScriptState*,
ScriptValue data,
const ResponseInit*,
ExceptionState&);

static FetchResponseData* CreateUnfilteredFetchResponseDataWithoutBody(
ScriptState*,
Expand Down
1 change: 1 addition & 0 deletions third_party/blink/renderer/core/fetch/response.idl
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ enum ResponseType { "basic", "cors", "default", "error", "opaque", "opaqueredire
[CallWith=ScriptState, RaisesException] constructor(optional any body, optional ResponseInit init = {});
[CallWith=ScriptState, NewObject] static Response error();
[CallWith=ScriptState, NewObject, RaisesException] static Response redirect(USVString url, optional unsigned short status = 302);
[CallWith=ScriptState, NewObject, RaisesException, ImplementedAs=staticJson] static Response json(any data, optional ResponseInit init = {});
readonly attribute ResponseType type;
readonly attribute USVString url;
readonly attribute boolean redirected;
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

0 comments on commit e680cd1

Please sign in to comment.