diff --git a/benchmarks/fetch/request-creation.mjs b/benchmarks/fetch/request-creation.mjs new file mode 100644 index 00000000000..c6ebf28d0dd --- /dev/null +++ b/benchmarks/fetch/request-creation.mjs @@ -0,0 +1,8 @@ +import { bench, run } from 'mitata' +import { Request } from '../../lib/web/fetch/request.js' + +const input = 'https://example.com/post' + +bench('new Request(input)', () => new Request(input, undefined)) + +await run() diff --git a/lib/web/fetch/request.js b/lib/web/fetch/request.js index a35b90bf9f3..b12ffb5d3cd 100644 --- a/lib/web/fetch/request.js +++ b/lib/web/fetch/request.js @@ -11,7 +11,7 @@ const { isValidHTTPToken, sameOrigin, normalizeMethod, - makePolicyContainer, + EnvironmentSettingsObject, normalizeMethodRecord } = require('./util') const { @@ -27,7 +27,6 @@ const { const { kEnumerableProperty } = util const { kHeaders, kSignal, kState, kGuard, kRealm, kDispatcher } = require('./symbols') const { webidl } = require('./webidl') -const { getGlobalOrigin } = require('./global') const { URLSerializer } = require('./data-url') const { kHeadersList, kConstruct } = require('../../core/symbols') const assert = require('node:assert') @@ -55,16 +54,8 @@ class Request { init = webidl.converters.RequestInit(init) // https://html.spec.whatwg.org/multipage/webappapis.html#environment-settings-object - this[kRealm] = { - settingsObject: { - baseUrl: getGlobalOrigin(), - get origin () { - return this.baseUrl?.origin - }, - policyContainer: makePolicyContainer() - } - } - + // Note: Slow initialization of object literals with getters. + this[kRealm] = new EnvironmentSettingsObject() // 1. Let request be null. let request = null @@ -366,7 +357,6 @@ class Request { // (https://dom.spec.whatwg.org/#dom-abortsignal-any) const ac = new AbortController() this[kSignal] = ac.signal - this[kSignal][kRealm] = this[kRealm] // 29. If signal is not null, then make this’s signal follow signal. if (signal != null) { @@ -436,7 +426,6 @@ class Request { this[kHeaders] = new Headers(kConstruct) this[kHeaders][kHeadersList] = request.headersList this[kHeaders][kGuard] = 'request' - this[kHeaders][kRealm] = this[kRealm] // 31. If this’s request’s mode is "no-cors", then: if (mode === 'no-cors') { @@ -881,11 +870,9 @@ function fromInnerRequest (innerRequest, signal, guard, realm) { request[kState] = innerRequest request[kRealm] = realm request[kSignal] = signal - request[kSignal][kRealm] = realm request[kHeaders] = new Headers(kConstruct) request[kHeaders][kHeadersList] = innerRequest.headersList request[kHeaders][kGuard] = guard - request[kHeaders][kRealm] = realm return request } diff --git a/lib/web/fetch/response.js b/lib/web/fetch/response.js index 182e3dc0199..763df67d6eb 100644 --- a/lib/web/fetch/response.js +++ b/lib/web/fetch/response.js @@ -12,7 +12,8 @@ const { isBlobLike, serializeJavascriptValueToJSONString, isErrorLike, - isomorphicEncode + isomorphicEncode, + EnvironmentSettingsObject } = require('./util') const { redirectStatusSet, @@ -21,7 +22,6 @@ const { const { kState, kHeaders, kGuard, kRealm } = require('./symbols') const { webidl } = require('./webidl') const { FormData } = require('./formdata') -const { getGlobalOrigin } = require('./global') const { URLSerializer } = require('./data-url') const { kHeadersList, kConstruct } = require('../../core/symbols') const assert = require('node:assert') @@ -33,8 +33,7 @@ const textEncoder = new TextEncoder('utf-8') class Response { // Creates network error Response. static error () { - // TODO - const relevantRealm = { settingsObject: {} } + const relevantRealm = new EnvironmentSettingsObject() // The static error() method steps are to return the result of creating a // Response object, given a new network error, "immutable", and this’s @@ -62,7 +61,7 @@ class Response { // 3. Let responseObject be the result of creating a Response object, given a new response, // "response", and this’s relevant Realm. - const relevantRealm = { settingsObject: {} } + const relevantRealm = new EnvironmentSettingsObject() const responseObject = fromInnerResponse(makeResponse({}), 'response', relevantRealm) // 4. Perform initialize a response given responseObject, init, and (body, "application/json"). @@ -74,7 +73,7 @@ class Response { // Creates a redirect Response that redirects to url with status status. static redirect (url, status = 302) { - const relevantRealm = { settingsObject: {} } + const relevantRealm = new EnvironmentSettingsObject() webidl.argumentLengthCheck(arguments, 1, { header: 'Response.redirect' }) @@ -87,7 +86,7 @@ class Response { // TODO: base-URL? let parsedURL try { - parsedURL = new URL(url, getGlobalOrigin()) + parsedURL = new URL(url, relevantRealm.settingsObject.baseUrl) } catch (err) { throw new TypeError(`Failed to parse URL from ${url}`, { cause: err }) } @@ -127,7 +126,7 @@ class Response { init = webidl.converters.ResponseInit(init) // TODO - this[kRealm] = { settingsObject: {} } + this[kRealm] = new EnvironmentSettingsObject() // 1. Set this’s response to a new response. this[kState] = makeResponse({}) @@ -138,7 +137,6 @@ class Response { this[kHeaders] = new Headers(kConstruct) this[kHeaders][kGuard] = 'response' this[kHeaders][kHeadersList] = this[kState].headersList - this[kHeaders][kRealm] = this[kRealm] // 3. Let bodyWithType be null. let bodyWithType = null @@ -522,7 +520,6 @@ function fromInnerResponse (innerResponse, guard, realm) { response[kHeaders] = new Headers(kConstruct) response[kHeaders][kHeadersList] = innerResponse.headersList response[kHeaders][kGuard] = guard - response[kHeaders][kRealm] = realm return response } diff --git a/lib/web/fetch/util.js b/lib/web/fetch/util.js index da55a7c4dfc..71df40a3793 100644 --- a/lib/web/fetch/util.js +++ b/lib/web/fetch/util.js @@ -1569,6 +1569,20 @@ function utf8DecodeBytes (buffer) { return output } +class EnvironmentSettingsObjectBase { + baseUrl = getGlobalOrigin() + + get origin () { + return this.baseUrl?.origin + } + + policyContainer = makePolicyContainer() +} + +class EnvironmentSettingsObject { + settingsObject = new EnvironmentSettingsObjectBase() +} + module.exports = { isAborted, isCancelled, @@ -1620,5 +1634,6 @@ module.exports = { createInflate, extractMimeType, getDecodeSplit, - utf8DecodeBytes + utf8DecodeBytes, + EnvironmentSettingsObject } diff --git a/test/fetch/request.js b/test/fetch/request.js index 41ae8a836d7..58eb87ecc1b 100644 --- a/test/fetch/request.js +++ b/test/fetch/request.js @@ -499,8 +499,6 @@ test('fromInnerRequest', () => { assert.strictEqual(request[kState], innerRequest) assert.strictEqual(request[kRealm], realm) assert.strictEqual(request[kSignal], signal) - assert.strictEqual(request[kSignal][kRealm], realm) assert.strictEqual(request[kHeaders][kHeadersList], innerRequest.headersList) assert.strictEqual(request[kHeaders][kGuard], 'immutable') - assert.strictEqual(request[kHeaders][kRealm], realm) }) diff --git a/test/fetch/response.js b/test/fetch/response.js index 1f44f99f15d..50b54223363 100644 --- a/test/fetch/response.js +++ b/test/fetch/response.js @@ -286,5 +286,4 @@ test('fromInnerResponse', () => { assert.strictEqual(response[kRealm], realm) assert.strictEqual(response[kHeaders][kHeadersList], innerResponse.headersList) assert.strictEqual(response[kHeaders][kGuard], 'immutable') - assert.strictEqual(response[kHeaders][kRealm], realm) })