Skip to content

Commit

Permalink
reuse realm for Request/Response (#3142)
Browse files Browse the repository at this point in the history
  • Loading branch information
KhafraDev committed Apr 21, 2024
1 parent ee2b386 commit 9547fe4
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 13 deletions.
6 changes: 3 additions & 3 deletions lib/web/fetch/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const {
isValidHTTPToken,
sameOrigin,
normalizeMethod,
EnvironmentSettingsObject,
environmentSettingsObject,
normalizeMethodRecord
} = require('./util')
const {
Expand Down Expand Up @@ -54,8 +54,8 @@ class Request {
init = webidl.converters.RequestInit(init)

// https://html.spec.whatwg.org/multipage/webappapis.html#environment-settings-object
// Note: Slow initialization of object literals with getters.
this[kRealm] = new EnvironmentSettingsObject()
this[kRealm] = environmentSettingsObject

// 1. Let request be null.
let request = null

Expand Down
10 changes: 2 additions & 8 deletions lib/web/fetch/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const {
serializeJavascriptValueToJSONString,
isErrorLike,
isomorphicEncode,
EnvironmentSettingsObject
environmentSettingsObject: relevantRealm
} = require('./util')
const {
redirectStatusSet,
Expand All @@ -33,8 +33,6 @@ const textEncoder = new TextEncoder('utf-8')
class Response {
// Creates network error Response.
static error () {
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
// relevant Realm.
Expand All @@ -61,7 +59,6 @@ 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 = new EnvironmentSettingsObject()
const responseObject = fromInnerResponse(makeResponse({}), 'response', relevantRealm)

// 4. Perform initialize a response given responseObject, init, and (body, "application/json").
Expand All @@ -73,8 +70,6 @@ class Response {

// Creates a redirect Response that redirects to url with status status.
static redirect (url, status = 302) {
const relevantRealm = new EnvironmentSettingsObject()

webidl.argumentLengthCheck(arguments, 1, { header: 'Response.redirect' })

url = webidl.converters.USVString(url)
Expand Down Expand Up @@ -125,8 +120,7 @@ class Response {

init = webidl.converters.ResponseInit(init)

// TODO
this[kRealm] = new EnvironmentSettingsObject()
this[kRealm] = relevantRealm

// 1. Set this’s response to a new response.
this[kState] = makeResponse({})
Expand Down
8 changes: 6 additions & 2 deletions lib/web/fetch/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -1570,7 +1570,9 @@ function utf8DecodeBytes (buffer) {
}

class EnvironmentSettingsObjectBase {
baseUrl = getGlobalOrigin()
get baseUrl () {
return getGlobalOrigin()
}

get origin () {
return this.baseUrl?.origin
Expand All @@ -1583,6 +1585,8 @@ class EnvironmentSettingsObject {
settingsObject = new EnvironmentSettingsObjectBase()
}

const environmentSettingsObject = new EnvironmentSettingsObject()

module.exports = {
isAborted,
isCancelled,
Expand Down Expand Up @@ -1635,5 +1639,5 @@ module.exports = {
extractMimeType,
getDecodeSplit,
utf8DecodeBytes,
EnvironmentSettingsObject
environmentSettingsObject
}

0 comments on commit 9547fe4

Please sign in to comment.