MCPcopy Create free account
hub / github.com/nodejs/node / json

Method json

deps/undici/src/lib/web/fetch/response.js:45–69  ·  view source on GitHub ↗
(data, init = undefined)

Source from the content-addressed store, hash-verified

43
44 // https://fetch.spec.whatwg.org/#dom-response-json
45 static json (data, init = undefined) {
46 webidl.argumentLengthCheck(arguments, 1, 'Response.json')
47
48 if (init !== null) {
49 init = webidl.converters.ResponseInit(init)
50 }
51
52 // 1. Let bytes the result of running serialize a JavaScript value to JSON bytes on data.
53 const bytes = textEncoder.encode(
54 serializeJavascriptValueToJSONString(data)
55 )
56
57 // 2. Let body be the result of extracting bytes.
58 const body = extractBody(bytes)
59
60 // 3. Let responseObject be the result of creating a Response object, given a new response,
61 // "response", and this’s relevant Realm.
62 const responseObject = fromInnerResponse(makeResponse({}), 'response')
63
64 // 4. Perform initialize a response given responseObject, init, and (body, "application/json").
65 initializeResponse(responseObject, init, { body: body[0], type: 'application/json' })
66
67 // 5. Return responseObject.
68 return responseObject
69 }
70
71 // Creates a redirect Response that redirects to url with status status.
72 static redirect (url, status = 302) {

Callers

nothing calls this directly

Calls 7

argumentLengthCheckMethod · 0.80
encodeMethod · 0.80
extractBodyFunction · 0.70
fromInnerResponseFunction · 0.70
makeResponseFunction · 0.70
initializeResponseFunction · 0.70

Tested by

no test coverage detected