| |
| |
| 'use strict' |
|
|
| const { extractBody, mixinBody, cloneBody } = require('./body') |
| const { Headers, fill: fillHeaders, HeadersList } = require('./headers') |
| const { FinalizationRegistry } = require('../compat/dispatcher-weakref')() |
| const util = require('../core/util') |
| const { |
| isValidHTTPToken, |
| sameOrigin, |
| normalizeMethod, |
| makePolicyContainer, |
| normalizeMethodRecord |
| } = require('./util') |
| const { |
| forbiddenMethodsSet, |
| corsSafeListedMethodsSet, |
| referrerPolicy, |
| requestRedirect, |
| requestMode, |
| requestCredentials, |
| requestCache, |
| requestDuplex |
| } = require('./constants') |
| const { kEnumerableProperty } = util |
| const { kHeaders, kSignal, kState, kGuard, kRealm } = require('./symbols') |
| const { webidl } = require('./webidl') |
| const { getGlobalOrigin } = require('./global') |
| const { URLSerializer } = require('./dataURL') |
| const { kHeadersList, kConstruct } = require('../core/symbols') |
| const assert = require('assert') |
| const { getMaxListeners, setMaxListeners, getEventListeners, defaultMaxListeners } = require('events') |
|
|
| let TransformStream = globalThis.TransformStream |
|
|
| const kAbortController = Symbol('abortController') |
|
|
| const requestFinalizer = new FinalizationRegistry(({ signal, abort }) => { |
| signal.removeEventListener('abort', abort) |
| }) |
|
|
| |
| class Request { |
| |
| constructor (input, init = {}) { |
| if (input === kConstruct) { |
| return |
| } |
|
|
| webidl.argumentLengthCheck(arguments, 1, { header: 'Request constructor' }) |
|
|
| input = webidl.converters.RequestInfo(input) |
| init = webidl.converters.RequestInit(init) |
|
|
| |
| this[kRealm] = { |
| settingsObject: { |
| baseUrl: getGlobalOrigin(), |
| get origin () { |
| return this.baseUrl?.origin |
| }, |
| policyContainer: makePolicyContainer() |
| } |
| } |
|
|
| |
| let request = null |
|
|
| |
| let fallbackMode = null |
|
|
| |
| const baseUrl = this[kRealm].settingsObject.baseUrl |
|
|
| |
| let signal = null |
|
|
| |
| if (typeof input === 'string') { |
| |
| |
| let parsedURL |
| try { |
| parsedURL = new URL(input, baseUrl) |
| } catch (err) { |
| throw new TypeError('Failed to parse URL from ' + input, { cause: err }) |
| } |
|
|
| |
| if (parsedURL.username || parsedURL.password) { |
| throw new TypeError( |
| 'Request cannot be constructed from a URL that includes credentials: ' + |
| input |
| ) |
| } |
|
|
| |
| request = makeRequest({ urlList: [parsedURL] }) |
|
|
| |
| fallbackMode = 'cors' |
| } else { |
| |
|
|
| |
| assert(input instanceof Request) |
|
|
| |
| request = input[kState] |
|
|
| |
| signal = input[kSignal] |
| } |
|
|
| |
| const origin = this[kRealm].settingsObject.origin |
|
|
| |
| let window = 'client' |
|
|
| |
| |
| if ( |
| request.window?.constructor?.name === 'EnvironmentSettingsObject' && |
| sameOrigin(request.window, origin) |
| ) { |
| window = request.window |
| } |
|
|
| |
| if (init.window != null) { |
| throw new TypeError(`'window' option '${window}' must be null`) |
| } |
|
|
| |
| if ('window' in init) { |
| window = 'no-window' |
| } |
|
|
| |
| request = makeRequest({ |
| |
| |
| |
| method: request.method, |
| |
| |
| headersList: request.headersList, |
| |
| unsafeRequest: request.unsafeRequest, |
| |
| client: this[kRealm].settingsObject, |
| |
| window, |
| |
| priority: request.priority, |
| |
| |
| |
| origin: request.origin, |
| |
| referrer: request.referrer, |
| |
| referrerPolicy: request.referrerPolicy, |
| |
| mode: request.mode, |
| |
| credentials: request.credentials, |
| |
| cache: request.cache, |
| |
| redirect: request.redirect, |
| |
| integrity: request.integrity, |
| |
| keepalive: request.keepalive, |
| |
| reloadNavigation: request.reloadNavigation, |
| |
| historyNavigation: request.historyNavigation, |
| |
| urlList: [...request.urlList] |
| }) |
|
|
| const initHasKey = Object.keys(init).length !== 0 |
|
|
| |
| if (initHasKey) { |
| |
| if (request.mode === 'navigate') { |
| request.mode = 'same-origin' |
| } |
|
|
| |
| request.reloadNavigation = false |
|
|
| |
| request.historyNavigation = false |
|
|
| |
| request.origin = 'client' |
|
|
| |
| request.referrer = 'client' |
|
|
| |
| request.referrerPolicy = '' |
|
|
| |
| request.url = request.urlList[request.urlList.length - 1] |
|
|
| |
| request.urlList = [request.url] |
| } |
|
|
| |
| if (init.referrer !== undefined) { |
| |
| const referrer = init.referrer |
|
|
| |
| if (referrer === '') { |
| request.referrer = 'no-referrer' |
| } else { |
| |
| |
| |
| let parsedReferrer |
| try { |
| parsedReferrer = new URL(referrer, baseUrl) |
| } catch (err) { |
| throw new TypeError(`Referrer "${referrer}" is not a valid URL.`, { cause: err }) |
| } |
|
|
| |
| |
| |
| |
| if ( |
| (parsedReferrer.protocol === 'about:' && parsedReferrer.hostname === 'client') || |
| (origin && !sameOrigin(parsedReferrer, this[kRealm].settingsObject.baseUrl)) |
| ) { |
| request.referrer = 'client' |
| } else { |
| |
| request.referrer = parsedReferrer |
| } |
| } |
| } |
|
|
| |
| |
| if (init.referrerPolicy !== undefined) { |
| request.referrerPolicy = init.referrerPolicy |
| } |
|
|
| |
| let mode |
| if (init.mode !== undefined) { |
| mode = init.mode |
| } else { |
| mode = fallbackMode |
| } |
|
|
| |
| if (mode === 'navigate') { |
| throw webidl.errors.exception({ |
| header: 'Request constructor', |
| message: 'invalid request mode navigate.' |
| }) |
| } |
|
|
| |
| if (mode != null) { |
| request.mode = mode |
| } |
|
|
| |
| |
| if (init.credentials !== undefined) { |
| request.credentials = init.credentials |
| } |
|
|
| |
| if (init.cache !== undefined) { |
| request.cache = init.cache |
| } |
|
|
| |
| |
| if (request.cache === 'only-if-cached' && request.mode !== 'same-origin') { |
| throw new TypeError( |
| "'only-if-cached' can be set only with 'same-origin' mode" |
| ) |
| } |
|
|
| |
| if (init.redirect !== undefined) { |
| request.redirect = init.redirect |
| } |
|
|
| |
| if (init.integrity != null) { |
| request.integrity = String(init.integrity) |
| } |
|
|
| |
| if (init.keepalive !== undefined) { |
| request.keepalive = Boolean(init.keepalive) |
| } |
|
|
| |
| if (init.method !== undefined) { |
| |
| let method = init.method |
|
|
| |
| |
| if (!isValidHTTPToken(method)) { |
| throw new TypeError(`'${method}' is not a valid HTTP method.`) |
| } |
|
|
| if (forbiddenMethodsSet.has(method.toUpperCase())) { |
| throw new TypeError(`'${method}' HTTP method is unsupported.`) |
| } |
|
|
| |
| method = normalizeMethodRecord[method] ?? normalizeMethod(method) |
|
|
| |
| request.method = method |
| } |
|
|
| |
| if (init.signal !== undefined) { |
| signal = init.signal |
| } |
|
|
| |
| this[kState] = request |
|
|
| |
| |
| |
| |
| const ac = new AbortController() |
| this[kSignal] = ac.signal |
| this[kSignal][kRealm] = this[kRealm] |
|
|
| |
| if (signal != null) { |
| if ( |
| !signal || |
| typeof signal.aborted !== 'boolean' || |
| typeof signal.addEventListener !== 'function' |
| ) { |
| throw new TypeError( |
| "Failed to construct 'Request': member signal is not of type AbortSignal." |
| ) |
| } |
|
|
| if (signal.aborted) { |
| ac.abort(signal.reason) |
| } else { |
| |
| |
| |
| |
| this[kAbortController] = ac |
|
|
| const acRef = new WeakRef(ac) |
| const abort = function () { |
| const ac = acRef.deref() |
| if (ac !== undefined) { |
| ac.abort(this.reason) |
| } |
| } |
|
|
| |
| |
| try { |
| |
| |
| if (typeof getMaxListeners === 'function' && getMaxListeners(signal) === defaultMaxListeners) { |
| setMaxListeners(100, signal) |
| } else if (getEventListeners(signal, 'abort').length >= defaultMaxListeners) { |
| setMaxListeners(100, signal) |
| } |
| } catch {} |
|
|
| util.addAbortListener(signal, abort) |
| requestFinalizer.register(ac, { signal, abort }) |
| } |
| } |
|
|
| |
| |
| |
| this[kHeaders] = new Headers(kConstruct) |
| this[kHeaders][kHeadersList] = request.headersList |
| this[kHeaders][kGuard] = 'request' |
| this[kHeaders][kRealm] = this[kRealm] |
|
|
| |
| if (mode === 'no-cors') { |
| |
| |
| if (!corsSafeListedMethodsSet.has(request.method)) { |
| throw new TypeError( |
| `'${request.method} is unsupported in no-cors mode.` |
| ) |
| } |
|
|
| |
| this[kHeaders][kGuard] = 'request-no-cors' |
| } |
|
|
| |
| if (initHasKey) { |
| |
| const headersList = this[kHeaders][kHeadersList] |
| |
| |
| |
| const headers = init.headers !== undefined ? init.headers : new HeadersList(headersList) |
|
|
| |
| headersList.clear() |
|
|
| |
| |
| if (headers instanceof HeadersList) { |
| for (const [key, val] of headers) { |
| headersList.append(key, val) |
| } |
| |
| headersList.cookies = headers.cookies |
| } else { |
| |
| fillHeaders(this[kHeaders], headers) |
| } |
| } |
|
|
| |
| |
| const inputBody = input instanceof Request ? input[kState].body : null |
|
|
| |
| |
| |
| if ( |
| (init.body != null || inputBody != null) && |
| (request.method === 'GET' || request.method === 'HEAD') |
| ) { |
| throw new TypeError('Request with GET/HEAD method cannot have body.') |
| } |
|
|
| |
| let initBody = null |
|
|
| |
| if (init.body != null) { |
| |
| |
| |
| const [extractedBody, contentType] = extractBody( |
| init.body, |
| request.keepalive |
| ) |
| initBody = extractedBody |
|
|
| |
| |
| |
| if (contentType && !this[kHeaders][kHeadersList].contains('content-type')) { |
| this[kHeaders].append('content-type', contentType) |
| } |
| } |
|
|
| |
| |
| const inputOrInitBody = initBody ?? inputBody |
|
|
| |
| |
| if (inputOrInitBody != null && inputOrInitBody.source == null) { |
| |
| |
| if (initBody != null && init.duplex == null) { |
| throw new TypeError('RequestInit: duplex option is required when sending a body.') |
| } |
|
|
| |
| |
| if (request.mode !== 'same-origin' && request.mode !== 'cors') { |
| throw new TypeError( |
| 'If request is made from ReadableStream, mode should be "same-origin" or "cors"' |
| ) |
| } |
|
|
| |
| request.useCORSPreflightFlag = true |
| } |
|
|
| |
| let finalBody = inputOrInitBody |
|
|
| |
| if (initBody == null && inputBody != null) { |
| |
| if (util.isDisturbed(inputBody.stream) || inputBody.stream.locked) { |
| throw new TypeError( |
| 'Cannot construct a Request with a Request object that has already been used.' |
| ) |
| } |
|
|
| |
| if (!TransformStream) { |
| TransformStream = require('stream/web').TransformStream |
| } |
|
|
| |
| const identityTransform = new TransformStream() |
| inputBody.stream.pipeThrough(identityTransform) |
| finalBody = { |
| source: inputBody.source, |
| length: inputBody.length, |
| stream: identityTransform.readable |
| } |
| } |
|
|
| |
| this[kState].body = finalBody |
| } |
|
|
| |
| get method () { |
| webidl.brandCheck(this, Request) |
|
|
| |
| return this[kState].method |
| } |
|
|
| |
| get url () { |
| webidl.brandCheck(this, Request) |
|
|
| |
| return URLSerializer(this[kState].url) |
| } |
|
|
| |
| |
| |
| get headers () { |
| webidl.brandCheck(this, Request) |
|
|
| |
| return this[kHeaders] |
| } |
|
|
| |
| |
| get destination () { |
| webidl.brandCheck(this, Request) |
|
|
| |
| return this[kState].destination |
| } |
|
|
| |
| |
| |
| |
| |
| get referrer () { |
| webidl.brandCheck(this, Request) |
|
|
| |
| |
| if (this[kState].referrer === 'no-referrer') { |
| return '' |
| } |
|
|
| |
| |
| if (this[kState].referrer === 'client') { |
| return 'about:client' |
| } |
|
|
| |
| return this[kState].referrer.toString() |
| } |
|
|
| |
| |
| |
| get referrerPolicy () { |
| webidl.brandCheck(this, Request) |
|
|
| |
| return this[kState].referrerPolicy |
| } |
|
|
| |
| |
| |
| get mode () { |
| webidl.brandCheck(this, Request) |
|
|
| |
| return this[kState].mode |
| } |
|
|
| |
| |
| |
| get credentials () { |
| |
| return this[kState].credentials |
| } |
|
|
| |
| |
| |
| get cache () { |
| webidl.brandCheck(this, Request) |
|
|
| |
| return this[kState].cache |
| } |
|
|
| |
| |
| |
| |
| get redirect () { |
| webidl.brandCheck(this, Request) |
|
|
| |
| return this[kState].redirect |
| } |
|
|
| |
| |
| |
| get integrity () { |
| webidl.brandCheck(this, Request) |
|
|
| |
| |
| return this[kState].integrity |
| } |
|
|
| |
| |
| get keepalive () { |
| webidl.brandCheck(this, Request) |
|
|
| |
| return this[kState].keepalive |
| } |
|
|
| |
| |
| get isReloadNavigation () { |
| webidl.brandCheck(this, Request) |
|
|
| |
| |
| return this[kState].reloadNavigation |
| } |
|
|
| |
| |
| get isHistoryNavigation () { |
| webidl.brandCheck(this, Request) |
|
|
| |
| |
| return this[kState].historyNavigation |
| } |
|
|
| |
| |
| |
| get signal () { |
| webidl.brandCheck(this, Request) |
|
|
| |
| return this[kSignal] |
| } |
|
|
| get body () { |
| webidl.brandCheck(this, Request) |
|
|
| return this[kState].body ? this[kState].body.stream : null |
| } |
|
|
| get bodyUsed () { |
| webidl.brandCheck(this, Request) |
|
|
| return !!this[kState].body && util.isDisturbed(this[kState].body.stream) |
| } |
|
|
| get duplex () { |
| webidl.brandCheck(this, Request) |
|
|
| return 'half' |
| } |
|
|
| |
| clone () { |
| webidl.brandCheck(this, Request) |
|
|
| |
| if (this.bodyUsed || this.body?.locked) { |
| throw new TypeError('unusable') |
| } |
|
|
| |
| const clonedRequest = cloneRequest(this[kState]) |
|
|
| |
| |
| const clonedRequestObject = new Request(kConstruct) |
| clonedRequestObject[kState] = clonedRequest |
| clonedRequestObject[kRealm] = this[kRealm] |
| clonedRequestObject[kHeaders] = new Headers(kConstruct) |
| clonedRequestObject[kHeaders][kHeadersList] = clonedRequest.headersList |
| clonedRequestObject[kHeaders][kGuard] = this[kHeaders][kGuard] |
| clonedRequestObject[kHeaders][kRealm] = this[kHeaders][kRealm] |
|
|
| |
| const ac = new AbortController() |
| if (this.signal.aborted) { |
| ac.abort(this.signal.reason) |
| } else { |
| util.addAbortListener( |
| this.signal, |
| () => { |
| ac.abort(this.signal.reason) |
| } |
| ) |
| } |
| clonedRequestObject[kSignal] = ac.signal |
|
|
| |
| return clonedRequestObject |
| } |
| } |
|
|
| mixinBody(Request) |
|
|
| function makeRequest (init) { |
| |
| const request = { |
| method: 'GET', |
| localURLsOnly: false, |
| unsafeRequest: false, |
| body: null, |
| client: null, |
| reservedClient: null, |
| replacesClientId: '', |
| window: 'client', |
| keepalive: false, |
| serviceWorkers: 'all', |
| initiator: '', |
| destination: '', |
| priority: null, |
| origin: 'client', |
| policyContainer: 'client', |
| referrer: 'client', |
| referrerPolicy: '', |
| mode: 'no-cors', |
| useCORSPreflightFlag: false, |
| credentials: 'same-origin', |
| useCredentials: false, |
| cache: 'default', |
| redirect: 'follow', |
| integrity: '', |
| cryptoGraphicsNonceMetadata: '', |
| parserMetadata: '', |
| reloadNavigation: false, |
| historyNavigation: false, |
| userActivation: false, |
| taintedOrigin: false, |
| redirectCount: 0, |
| responseTainting: 'basic', |
| preventNoCacheCacheControlHeaderModification: false, |
| done: false, |
| timingAllowFailed: false, |
| ...init, |
| headersList: init.headersList |
| ? new HeadersList(init.headersList) |
| : new HeadersList() |
| } |
| request.url = request.urlList[0] |
| return request |
| } |
|
|
| |
| function cloneRequest (request) { |
| |
|
|
| |
| const newRequest = makeRequest({ ...request, body: null }) |
|
|
| |
| |
| if (request.body != null) { |
| newRequest.body = cloneBody(request.body) |
| } |
|
|
| |
| return newRequest |
| } |
|
|
| Object.defineProperties(Request.prototype, { |
| method: kEnumerableProperty, |
| url: kEnumerableProperty, |
| headers: kEnumerableProperty, |
| redirect: kEnumerableProperty, |
| clone: kEnumerableProperty, |
| signal: kEnumerableProperty, |
| duplex: kEnumerableProperty, |
| destination: kEnumerableProperty, |
| body: kEnumerableProperty, |
| bodyUsed: kEnumerableProperty, |
| isHistoryNavigation: kEnumerableProperty, |
| isReloadNavigation: kEnumerableProperty, |
| keepalive: kEnumerableProperty, |
| integrity: kEnumerableProperty, |
| cache: kEnumerableProperty, |
| credentials: kEnumerableProperty, |
| attribute: kEnumerableProperty, |
| referrerPolicy: kEnumerableProperty, |
| referrer: kEnumerableProperty, |
| mode: kEnumerableProperty, |
| [Symbol.toStringTag]: { |
| value: 'Request', |
| configurable: true |
| } |
| }) |
|
|
| webidl.converters.Request = webidl.interfaceConverter( |
| Request |
| ) |
|
|
| |
| webidl.converters.RequestInfo = function (V) { |
| if (typeof V === 'string') { |
| return webidl.converters.USVString(V) |
| } |
|
|
| if (V instanceof Request) { |
| return webidl.converters.Request(V) |
| } |
|
|
| return webidl.converters.USVString(V) |
| } |
|
|
| webidl.converters.AbortSignal = webidl.interfaceConverter( |
| AbortSignal |
| ) |
|
|
| |
| webidl.converters.RequestInit = webidl.dictionaryConverter([ |
| { |
| key: 'method', |
| converter: webidl.converters.ByteString |
| }, |
| { |
| key: 'headers', |
| converter: webidl.converters.HeadersInit |
| }, |
| { |
| key: 'body', |
| converter: webidl.nullableConverter( |
| webidl.converters.BodyInit |
| ) |
| }, |
| { |
| key: 'referrer', |
| converter: webidl.converters.USVString |
| }, |
| { |
| key: 'referrerPolicy', |
| converter: webidl.converters.DOMString, |
| |
| allowedValues: referrerPolicy |
| }, |
| { |
| key: 'mode', |
| converter: webidl.converters.DOMString, |
| |
| allowedValues: requestMode |
| }, |
| { |
| key: 'credentials', |
| converter: webidl.converters.DOMString, |
| |
| allowedValues: requestCredentials |
| }, |
| { |
| key: 'cache', |
| converter: webidl.converters.DOMString, |
| |
| allowedValues: requestCache |
| }, |
| { |
| key: 'redirect', |
| converter: webidl.converters.DOMString, |
| |
| allowedValues: requestRedirect |
| }, |
| { |
| key: 'integrity', |
| converter: webidl.converters.DOMString |
| }, |
| { |
| key: 'keepalive', |
| converter: webidl.converters.boolean |
| }, |
| { |
| key: 'signal', |
| converter: webidl.nullableConverter( |
| (signal) => webidl.converters.AbortSignal( |
| signal, |
| { strict: false } |
| ) |
| ) |
| }, |
| { |
| key: 'window', |
| converter: webidl.converters.any |
| }, |
| { |
| key: 'duplex', |
| converter: webidl.converters.DOMString, |
| allowedValues: requestDuplex |
| } |
| ]) |
|
|
| module.exports = { Request, makeRequest } |
|
|