| 'use strict' |
|
|
| const { Headers, HeadersList, fill } = require('./headers') |
| const { extractBody, cloneBody, mixinBody } = require('./body') |
| const util = require('../core/util') |
| const { kEnumerableProperty } = util |
| const { |
| isValidReasonPhrase, |
| isCancelled, |
| isAborted, |
| isBlobLike, |
| serializeJavascriptValueToJSONString, |
| isErrorLike, |
| isomorphicEncode |
| } = require('./util') |
| const { |
| redirectStatusSet, |
| nullBodyStatus, |
| DOMException |
| } = require('./constants') |
| const { kState, kHeaders, kGuard, kRealm } = require('./symbols') |
| const { webidl } = require('./webidl') |
| const { FormData } = require('./formdata') |
| const { getGlobalOrigin } = require('./global') |
| const { URLSerializer } = require('./dataURL') |
| const { kHeadersList, kConstruct } = require('../core/symbols') |
| const assert = require('assert') |
| const { types } = require('util') |
|
|
| const ReadableStream = globalThis.ReadableStream || require('stream/web').ReadableStream |
| const textEncoder = new TextEncoder('utf-8') |
|
|
| |
| class Response { |
| |
| static error () { |
| |
| const relevantRealm = { settingsObject: {} } |
|
|
| |
| |
| |
| const responseObject = new Response() |
| responseObject[kState] = makeNetworkError() |
| responseObject[kRealm] = relevantRealm |
| responseObject[kHeaders][kHeadersList] = responseObject[kState].headersList |
| responseObject[kHeaders][kGuard] = 'immutable' |
| responseObject[kHeaders][kRealm] = relevantRealm |
| return responseObject |
| } |
|
|
| |
| static json (data, init = {}) { |
| webidl.argumentLengthCheck(arguments, 1, { header: 'Response.json' }) |
|
|
| if (init !== null) { |
| init = webidl.converters.ResponseInit(init) |
| } |
|
|
| |
| const bytes = textEncoder.encode( |
| serializeJavascriptValueToJSONString(data) |
| ) |
|
|
| |
| const body = extractBody(bytes) |
|
|
| |
| |
| const relevantRealm = { settingsObject: {} } |
| const responseObject = new Response() |
| responseObject[kRealm] = relevantRealm |
| responseObject[kHeaders][kGuard] = 'response' |
| responseObject[kHeaders][kRealm] = relevantRealm |
|
|
| |
| initializeResponse(responseObject, init, { body: body[0], type: 'application/json' }) |
|
|
| |
| return responseObject |
| } |
|
|
| |
| static redirect (url, status = 302) { |
| const relevantRealm = { settingsObject: {} } |
|
|
| webidl.argumentLengthCheck(arguments, 1, { header: 'Response.redirect' }) |
|
|
| url = webidl.converters.USVString(url) |
| status = webidl.converters['unsigned short'](status) |
|
|
| |
| |
| |
| |
| let parsedURL |
| try { |
| parsedURL = new URL(url, getGlobalOrigin()) |
| } catch (err) { |
| throw Object.assign(new TypeError('Failed to parse URL from ' + url), { |
| cause: err |
| }) |
| } |
|
|
| |
| if (!redirectStatusSet.has(status)) { |
| throw new RangeError('Invalid status code ' + status) |
| } |
|
|
| |
| |
| const responseObject = new Response() |
| responseObject[kRealm] = relevantRealm |
| responseObject[kHeaders][kGuard] = 'immutable' |
| responseObject[kHeaders][kRealm] = relevantRealm |
|
|
| |
| responseObject[kState].status = status |
|
|
| |
| const value = isomorphicEncode(URLSerializer(parsedURL)) |
|
|
| |
| responseObject[kState].headersList.append('location', value) |
|
|
| |
| return responseObject |
| } |
|
|
| |
| constructor (body = null, init = {}) { |
| if (body !== null) { |
| body = webidl.converters.BodyInit(body) |
| } |
|
|
| init = webidl.converters.ResponseInit(init) |
|
|
| |
| this[kRealm] = { settingsObject: {} } |
|
|
| |
| this[kState] = makeResponse({}) |
|
|
| |
| |
| |
| this[kHeaders] = new Headers(kConstruct) |
| this[kHeaders][kGuard] = 'response' |
| this[kHeaders][kHeadersList] = this[kState].headersList |
| this[kHeaders][kRealm] = this[kRealm] |
|
|
| |
| let bodyWithType = null |
|
|
| |
| if (body != null) { |
| const [extractedBody, type] = extractBody(body) |
| bodyWithType = { body: extractedBody, type } |
| } |
|
|
| |
| initializeResponse(this, init, bodyWithType) |
| } |
|
|
| |
| get type () { |
| webidl.brandCheck(this, Response) |
|
|
| |
| return this[kState].type |
| } |
|
|
| |
| get url () { |
| webidl.brandCheck(this, Response) |
|
|
| const urlList = this[kState].urlList |
|
|
| |
| |
| |
| const url = urlList[urlList.length - 1] ?? null |
|
|
| if (url === null) { |
| return '' |
| } |
|
|
| return URLSerializer(url, true) |
| } |
|
|
| |
| get redirected () { |
| webidl.brandCheck(this, Response) |
|
|
| |
| |
| return this[kState].urlList.length > 1 |
| } |
|
|
| |
| get status () { |
| webidl.brandCheck(this, Response) |
|
|
| |
| return this[kState].status |
| } |
|
|
| |
| get ok () { |
| webidl.brandCheck(this, Response) |
|
|
| |
| |
| return this[kState].status >= 200 && this[kState].status <= 299 |
| } |
|
|
| |
| get statusText () { |
| webidl.brandCheck(this, Response) |
|
|
| |
| |
| return this[kState].statusText |
| } |
|
|
| |
| get headers () { |
| webidl.brandCheck(this, Response) |
|
|
| |
| return this[kHeaders] |
| } |
|
|
| get body () { |
| webidl.brandCheck(this, Response) |
|
|
| return this[kState].body ? this[kState].body.stream : null |
| } |
|
|
| get bodyUsed () { |
| webidl.brandCheck(this, Response) |
|
|
| return !!this[kState].body && util.isDisturbed(this[kState].body.stream) |
| } |
|
|
| |
| clone () { |
| webidl.brandCheck(this, Response) |
|
|
| |
| if (this.bodyUsed || (this.body && this.body.locked)) { |
| throw webidl.errors.exception({ |
| header: 'Response.clone', |
| message: 'Body has already been consumed.' |
| }) |
| } |
|
|
| |
| const clonedResponse = cloneResponse(this[kState]) |
|
|
| |
| |
| const clonedResponseObject = new Response() |
| clonedResponseObject[kState] = clonedResponse |
| clonedResponseObject[kRealm] = this[kRealm] |
| clonedResponseObject[kHeaders][kHeadersList] = clonedResponse.headersList |
| clonedResponseObject[kHeaders][kGuard] = this[kHeaders][kGuard] |
| clonedResponseObject[kHeaders][kRealm] = this[kHeaders][kRealm] |
|
|
| return clonedResponseObject |
| } |
| } |
|
|
| mixinBody(Response) |
|
|
| Object.defineProperties(Response.prototype, { |
| type: kEnumerableProperty, |
| url: kEnumerableProperty, |
| status: kEnumerableProperty, |
| ok: kEnumerableProperty, |
| redirected: kEnumerableProperty, |
| statusText: kEnumerableProperty, |
| headers: kEnumerableProperty, |
| clone: kEnumerableProperty, |
| body: kEnumerableProperty, |
| bodyUsed: kEnumerableProperty, |
| [Symbol.toStringTag]: { |
| value: 'Response', |
| configurable: true |
| } |
| }) |
|
|
| Object.defineProperties(Response, { |
| json: kEnumerableProperty, |
| redirect: kEnumerableProperty, |
| error: kEnumerableProperty |
| }) |
|
|
| |
| function cloneResponse (response) { |
| |
|
|
| |
| |
| |
| if (response.internalResponse) { |
| return filterResponse( |
| cloneResponse(response.internalResponse), |
| response.type |
| ) |
| } |
|
|
| |
| const newResponse = makeResponse({ ...response, body: null }) |
|
|
| |
| |
| if (response.body != null) { |
| newResponse.body = cloneBody(response.body) |
| } |
|
|
| |
| return newResponse |
| } |
|
|
| function makeResponse (init) { |
| return { |
| aborted: false, |
| rangeRequested: false, |
| timingAllowPassed: false, |
| requestIncludesCredentials: false, |
| type: 'default', |
| status: 200, |
| timingInfo: null, |
| cacheState: '', |
| statusText: '', |
| ...init, |
| headersList: init.headersList |
| ? new HeadersList(init.headersList) |
| : new HeadersList(), |
| urlList: init.urlList ? [...init.urlList] : [] |
| } |
| } |
|
|
| function makeNetworkError (reason) { |
| const isError = isErrorLike(reason) |
| return makeResponse({ |
| type: 'error', |
| status: 0, |
| error: isError |
| ? reason |
| : new Error(reason ? String(reason) : reason), |
| aborted: reason && reason.name === 'AbortError' |
| }) |
| } |
|
|
| function makeFilteredResponse (response, state) { |
| state = { |
| internalResponse: response, |
| ...state |
| } |
|
|
| return new Proxy(response, { |
| get (target, p) { |
| return p in state ? state[p] : target[p] |
| }, |
| set (target, p, value) { |
| assert(!(p in state)) |
| target[p] = value |
| return true |
| } |
| }) |
| } |
|
|
| |
| function filterResponse (response, type) { |
| |
| |
| if (type === 'basic') { |
| |
| |
| |
|
|
| |
| return makeFilteredResponse(response, { |
| type: 'basic', |
| headersList: response.headersList |
| }) |
| } else if (type === 'cors') { |
| |
| |
| |
| |
|
|
| |
| return makeFilteredResponse(response, { |
| type: 'cors', |
| headersList: response.headersList |
| }) |
| } else if (type === 'opaque') { |
| |
| |
| |
|
|
| return makeFilteredResponse(response, { |
| type: 'opaque', |
| urlList: Object.freeze([]), |
| status: 0, |
| statusText: '', |
| body: null |
| }) |
| } else if (type === 'opaqueredirect') { |
| |
| |
| |
|
|
| return makeFilteredResponse(response, { |
| type: 'opaqueredirect', |
| status: 0, |
| statusText: '', |
| headersList: [], |
| body: null |
| }) |
| } else { |
| assert(false) |
| } |
| } |
|
|
| |
| function makeAppropriateNetworkError (fetchParams, err = null) { |
| |
| assert(isCancelled(fetchParams)) |
|
|
| |
| |
| return isAborted(fetchParams) |
| ? makeNetworkError(Object.assign(new DOMException('The operation was aborted.', 'AbortError'), { cause: err })) |
| : makeNetworkError(Object.assign(new DOMException('Request was cancelled.'), { cause: err })) |
| } |
|
|
| |
| function initializeResponse (response, init, body) { |
| |
| |
| if (init.status !== null && (init.status < 200 || init.status > 599)) { |
| throw new RangeError('init["status"] must be in the range of 200 to 599, inclusive.') |
| } |
|
|
| |
| |
| if ('statusText' in init && init.statusText != null) { |
| |
| |
| if (!isValidReasonPhrase(String(init.statusText))) { |
| throw new TypeError('Invalid statusText') |
| } |
| } |
|
|
| |
| if ('status' in init && init.status != null) { |
| response[kState].status = init.status |
| } |
|
|
| |
| if ('statusText' in init && init.statusText != null) { |
| response[kState].statusText = init.statusText |
| } |
|
|
| |
| if ('headers' in init && init.headers != null) { |
| fill(response[kHeaders], init.headers) |
| } |
|
|
| |
| if (body) { |
| |
| if (nullBodyStatus.includes(response.status)) { |
| throw webidl.errors.exception({ |
| header: 'Response constructor', |
| message: 'Invalid response status code ' + response.status |
| }) |
| } |
|
|
| |
| response[kState].body = body.body |
|
|
| |
| |
| if (body.type != null && !response[kState].headersList.contains('Content-Type')) { |
| response[kState].headersList.append('content-type', body.type) |
| } |
| } |
| } |
|
|
| webidl.converters.ReadableStream = webidl.interfaceConverter( |
| ReadableStream |
| ) |
|
|
| webidl.converters.FormData = webidl.interfaceConverter( |
| FormData |
| ) |
|
|
| webidl.converters.URLSearchParams = webidl.interfaceConverter( |
| URLSearchParams |
| ) |
|
|
| |
| webidl.converters.XMLHttpRequestBodyInit = function (V) { |
| if (typeof V === 'string') { |
| return webidl.converters.USVString(V) |
| } |
|
|
| if (isBlobLike(V)) { |
| return webidl.converters.Blob(V, { strict: false }) |
| } |
|
|
| if (types.isArrayBuffer(V) || types.isTypedArray(V) || types.isDataView(V)) { |
| return webidl.converters.BufferSource(V) |
| } |
|
|
| if (util.isFormDataLike(V)) { |
| return webidl.converters.FormData(V, { strict: false }) |
| } |
|
|
| if (V instanceof URLSearchParams) { |
| return webidl.converters.URLSearchParams(V) |
| } |
|
|
| return webidl.converters.DOMString(V) |
| } |
|
|
| |
| webidl.converters.BodyInit = function (V) { |
| if (V instanceof ReadableStream) { |
| return webidl.converters.ReadableStream(V) |
| } |
|
|
| |
| |
| if (V?.[Symbol.asyncIterator]) { |
| return V |
| } |
|
|
| return webidl.converters.XMLHttpRequestBodyInit(V) |
| } |
|
|
| webidl.converters.ResponseInit = webidl.dictionaryConverter([ |
| { |
| key: 'status', |
| converter: webidl.converters['unsigned short'], |
| defaultValue: 200 |
| }, |
| { |
| key: 'statusText', |
| converter: webidl.converters.ByteString, |
| defaultValue: '' |
| }, |
| { |
| key: 'headers', |
| converter: webidl.converters.HeadersInit |
| } |
| ]) |
|
|
| module.exports = { |
| makeNetworkError, |
| makeResponse, |
| makeAppropriateNetworkError, |
| filterResponse, |
| Response, |
| cloneResponse |
| } |
|
|