Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove symbols from web specs #3633

Merged
merged 5 commits into from
Sep 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ module.exports.setGlobalOrigin = setGlobalOrigin
module.exports.getGlobalOrigin = getGlobalOrigin

const { CacheStorage } = require('./lib/web/cache/cachestorage')
const { kConstruct } = require('./lib/web/cache/symbols')
const { kConstruct } = require('./lib/core/symbols')

// Cache & CacheStorage are tightly coupled with fetch. Even if it may run
// in an older version of Node, it doesn't have any use without fetch.
Expand Down
29 changes: 14 additions & 15 deletions lib/web/cache/cache.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
'use strict'

const { kConstruct } = require('./symbols')
const { kConstruct } = require('../../core/symbols')
const { urlEquals, getFieldValues } = require('./util')
const { kEnumerableProperty, isDisturbed } = require('../../core/util')
const { webidl } = require('../fetch/webidl')
const { Response, cloneResponse, fromInnerResponse } = require('../fetch/response')
const { Request, fromInnerRequest } = require('../fetch/request')
const { kState } = require('../fetch/symbols')
const { Response, cloneResponse, fromInnerResponse, getResponseState } = require('../fetch/response')
const { Request, fromInnerRequest, getRequestState } = require('../fetch/request')
const { fetching } = require('../fetch/index')
const { urlIsHttpHttpsScheme, createDeferredPromise, readAllBytes } = require('../fetch/util')
const assert = require('node:assert')
Expand Down Expand Up @@ -115,7 +114,7 @@ class Cache {
}

// 3.1
const r = request[kState]
const r = getRequestState(request)

// 3.2
if (!urlIsHttpHttpsScheme(r.url) || r.method !== 'GET') {
Expand All @@ -133,7 +132,7 @@ class Cache {
// 5.
for (const request of requests) {
// 5.1
const r = new Request(request)[kState]
const r = getRequestState(new Request(request))

// 5.2
if (!urlIsHttpHttpsScheme(r.url)) {
Expand Down Expand Up @@ -270,9 +269,9 @@ class Cache {

// 2.
if (request instanceof Request) {
innerRequest = request[kState]
innerRequest = getRequestState(request)
} else { // 3.
innerRequest = new Request(request)[kState]
innerRequest = getRequestState(new Request(request))
}

// 4.
Expand All @@ -284,7 +283,7 @@ class Cache {
}

// 5.
const innerResponse = response[kState]
const innerResponse = getResponseState(response)

// 6.
if (innerResponse.status === 206) {
Expand Down Expand Up @@ -402,15 +401,15 @@ class Cache {
let r = null

if (request instanceof Request) {
r = request[kState]
r = getRequestState(request)

if (r.method !== 'GET' && !options.ignoreMethod) {
return false
}
} else {
assert(typeof request === 'string')

r = new Request(request)[kState]
r = getRequestState(new Request(request))
}

/** @type {CacheBatchOperation[]} */
Expand Down Expand Up @@ -469,14 +468,14 @@ class Cache {
// 2.1
if (request instanceof Request) {
// 2.1.1
r = request[kState]
r = getRequestState(request)

// 2.1.2
if (r.method !== 'GET' && !options.ignoreMethod) {
return []
}
} else if (typeof request === 'string') { // 2.2
r = new Request(request)[kState]
r = getRequestState(new Request(request))
}
}

Expand Down Expand Up @@ -751,15 +750,15 @@ class Cache {
if (request !== undefined) {
if (request instanceof Request) {
// 2.1.1
r = request[kState]
r = getRequestState(request)

// 2.1.2
if (r.method !== 'GET' && !options.ignoreMethod) {
return []
}
} else if (typeof request === 'string') {
// 2.2.1
r = new Request(request)[kState]
r = getRequestState(new Request(request))
}
}

Expand Down
2 changes: 1 addition & 1 deletion lib/web/cache/cachestorage.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
'use strict'

const { kConstruct } = require('./symbols')
const { Cache } = require('./cache')
const { webidl } = require('../fetch/webidl')
const { kEnumerableProperty } = require('../../core/util')
const { kConstruct } = require('../../core/symbols')

class CacheStorage {
/**
Expand Down
5 changes: 0 additions & 5 deletions lib/web/cache/symbols.js

This file was deleted.

57 changes: 31 additions & 26 deletions lib/web/fetch/body.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ const {
extractMimeType,
utf8DecodeBytes
} = require('./util')
const { FormData } = require('./formdata')
const { kState } = require('./symbols')
const { FormData, setFormDataState } = require('./formdata')
const { webidl } = require('./webidl')
const { Blob } = require('node:buffer')
const assert = require('node:assert')
Expand Down Expand Up @@ -307,7 +306,7 @@ function throwIfAborted (state) {
}
}

function bodyMixinMethods (instance) {
function bodyMixinMethods (instance, getInternalState) {
const methods = {
blob () {
// The blob() method steps are to return the result of
Expand All @@ -316,7 +315,7 @@ function bodyMixinMethods (instance) {
// contents are bytes and whose type attribute is this’s
// MIME type.
return consumeBody(this, (bytes) => {
let mimeType = bodyMimeType(this)
let mimeType = bodyMimeType(getInternalState(this))

if (mimeType === null) {
mimeType = ''
Expand All @@ -327,7 +326,7 @@ function bodyMixinMethods (instance) {
// Return a Blob whose contents are bytes and type attribute
// is mimeType.
return new Blob([bytes], { type: mimeType })
}, instance)
}, instance, getInternalState)
},

arrayBuffer () {
Expand All @@ -337,27 +336,27 @@ function bodyMixinMethods (instance) {
// whose contents are bytes.
return consumeBody(this, (bytes) => {
return new Uint8Array(bytes).buffer
}, instance)
}, instance, getInternalState)
},

text () {
// The text() method steps are to return the result of running
// consume body with this and UTF-8 decode.
return consumeBody(this, utf8DecodeBytes, instance)
return consumeBody(this, utf8DecodeBytes, instance, getInternalState)
},

json () {
// The json() method steps are to return the result of running
// consume body with this and parse JSON from bytes.
return consumeBody(this, parseJSONFromBytes, instance)
return consumeBody(this, parseJSONFromBytes, instance, getInternalState)
},

formData () {
// The formData() method steps are to return the result of running
// consume body with this and the following step given a byte sequence bytes:
return consumeBody(this, (value) => {
// 1. Let mimeType be the result of get the MIME type with this.
const mimeType = bodyMimeType(this)
const mimeType = bodyMimeType(getInternalState(this))

// 2. If mimeType is non-null, then switch on mimeType’s essence and run
// the corresponding steps:
Expand All @@ -375,7 +374,7 @@ function bodyMixinMethods (instance) {
// 3. Return a new FormData object, appending each entry,
// resulting from the parsing operation, to its entry list.
const fd = new FormData()
fd[kState] = parsed
setFormDataState(fd, parsed)

return fd
}
Expand All @@ -401,7 +400,7 @@ function bodyMixinMethods (instance) {
throw new TypeError(
'Content-Type was not one of "multipart/form-data" or "application/x-www-form-urlencoded".'
)
}, instance)
}, instance, getInternalState)
},

bytes () {
Expand All @@ -410,33 +409,36 @@ function bodyMixinMethods (instance) {
// result of creating a Uint8Array from bytes in this’s relevant realm.
return consumeBody(this, (bytes) => {
return new Uint8Array(bytes)
}, instance)
}, instance, getInternalState)
}
}

return methods
}

function mixinBody (prototype) {
Object.assign(prototype.prototype, bodyMixinMethods(prototype))
function mixinBody (prototype, getInternalState) {
Object.assign(prototype.prototype, bodyMixinMethods(prototype, getInternalState))
}

/**
* @see https://fetch.spec.whatwg.org/#concept-body-consume-body
* @param {Response|Request} object
* @param {any} object internal state
* @param {(value: unknown) => unknown} convertBytesToJSValue
* @param {Response|Request} instance
* @param {any} instance
* @param {(target: any) => any} getInternalState
*/
async function consumeBody (object, convertBytesToJSValue, instance) {
async function consumeBody (object, convertBytesToJSValue, instance, getInternalState) {
webidl.brandCheck(object, instance)

const state = getInternalState(object)

// 1. If object is unusable, then return a promise rejected
// with a TypeError.
if (bodyUnusable(object)) {
if (bodyUnusable(state)) {
throw new TypeError('Body is unusable: Body has already been read')
}

throwIfAborted(object[kState])
throwIfAborted(state)

// 2. Let promise be a new promise.
const promise = createDeferredPromise()
Expand All @@ -458,22 +460,25 @@ async function consumeBody (object, convertBytesToJSValue, instance) {

// 5. If object’s body is null, then run successSteps with an
// empty byte sequence.
if (object[kState].body == null) {
if (state.body == null) {
successSteps(Buffer.allocUnsafe(0))
return promise.promise
}

// 6. Otherwise, fully read object’s body given successSteps,
// errorSteps, and object’s relevant global object.
await fullyReadBody(object[kState].body, successSteps, errorSteps)
fullyReadBody(state.body, successSteps, errorSteps)

// 7. Return promise.
return promise.promise
}

// https://fetch.spec.whatwg.org/#body-unusable
/**
* @see https://fetch.spec.whatwg.org/#body-unusable
* @param {any} object internal state
*/
function bodyUnusable (object) {
const body = object[kState].body
const body = object.body

// An object including the Body interface mixin is
// said to be unusable if its body is non-null and
Expand All @@ -491,14 +496,14 @@ function parseJSONFromBytes (bytes) {

/**
* @see https://fetch.spec.whatwg.org/#concept-body-mime-type
* @param {import('./response').Response|import('./request').Request} requestOrResponse
* @param {any} object internal state
*/
function bodyMimeType (requestOrResponse) {
function bodyMimeType (object) {
// 1. Let headers be null.
// 2. If requestOrResponse is a Request object, then set headers to requestOrResponse’s request’s header list.
// 3. Otherwise, set headers to requestOrResponse’s response’s header list.
/** @type {import('./headers').HeadersList} */
const headers = requestOrResponse[kState].headersList
const headers = object.headersList

// 4. Let mimeType be the result of extracting a MIME type from headers.
const mimeType = extractMimeType(headers)
Expand Down
Loading
Loading