Skip to content
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
20 changes: 13 additions & 7 deletions lib/web/cache/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class Cache {
}

async match (request, options = {}) {
webidl.brandCheck(this, Cache)
webidl.brandCheck(this, webidl.is.Cache)

const prefix = 'Cache.match'
webidl.argumentLengthCheck(arguments, 1, prefix)
Expand All @@ -59,7 +59,7 @@ class Cache {
}

async matchAll (request = undefined, options = {}) {
webidl.brandCheck(this, Cache)
webidl.brandCheck(this, webidl.is.Cache)

const prefix = 'Cache.matchAll'
if (request !== undefined) request = webidl.converters.RequestInfo(request)
Expand All @@ -69,7 +69,7 @@ class Cache {
}

async add (request) {
webidl.brandCheck(this, Cache)
webidl.brandCheck(this, webidl.is.Cache)

const prefix = 'Cache.add'
webidl.argumentLengthCheck(arguments, 1, prefix)
Expand All @@ -87,7 +87,7 @@ class Cache {
}

async addAll (requests) {
webidl.brandCheck(this, Cache)
webidl.brandCheck(this, webidl.is.Cache)

const prefix = 'Cache.addAll'
webidl.argumentLengthCheck(arguments, 1, prefix)
Expand Down Expand Up @@ -257,7 +257,7 @@ class Cache {
}

async put (request, response) {
webidl.brandCheck(this, Cache)
webidl.brandCheck(this, webidl.is.Cache)

const prefix = 'Cache.put'
webidl.argumentLengthCheck(arguments, 2, prefix)
Expand Down Expand Up @@ -388,7 +388,7 @@ class Cache {
}

async delete (request, options = {}) {
webidl.brandCheck(this, Cache)
webidl.brandCheck(this, webidl.is.Cache)

const prefix = 'Cache.delete'
webidl.argumentLengthCheck(arguments, 1, prefix)
Expand Down Expand Up @@ -454,7 +454,7 @@ class Cache {
* @returns {Promise<readonly Request[]>}
*/
async keys (request = undefined, options = {}) {
webidl.brandCheck(this, Cache)
webidl.brandCheck(this, webidl.is.Cache)

const prefix = 'Cache.keys'

Expand Down Expand Up @@ -804,6 +804,12 @@ class Cache {
// 6.
return Object.freeze(responseList)
}

static {
webidl.is.Cache = (arg) => {
return arg != null && typeof arg === 'object' && #relevantRequestResponseList in arg
}
}
}

Object.defineProperties(Cache.prototype, {
Expand Down
16 changes: 11 additions & 5 deletions lib/web/cache/cachestorage.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class CacheStorage {
}

async match (request, options = {}) {
webidl.brandCheck(this, CacheStorage)
webidl.brandCheck(this, webidl.is.CacheStorage)
webidl.argumentLengthCheck(arguments, 1, 'CacheStorage.match')

request = webidl.converters.RequestInfo(request)
Expand Down Expand Up @@ -58,7 +58,7 @@ class CacheStorage {
* @returns {Promise<boolean>}
*/
async has (cacheName) {
webidl.brandCheck(this, CacheStorage)
webidl.brandCheck(this, webidl.is.CacheStorage)

const prefix = 'CacheStorage.has'
webidl.argumentLengthCheck(arguments, 1, prefix)
Expand All @@ -76,7 +76,7 @@ class CacheStorage {
* @returns {Promise<Cache>}
*/
async open (cacheName) {
webidl.brandCheck(this, CacheStorage)
webidl.brandCheck(this, webidl.is.CacheStorage)

const prefix = 'CacheStorage.open'
webidl.argumentLengthCheck(arguments, 1, prefix)
Expand Down Expand Up @@ -110,7 +110,7 @@ class CacheStorage {
* @returns {Promise<boolean>}
*/
async delete (cacheName) {
webidl.brandCheck(this, CacheStorage)
webidl.brandCheck(this, webidl.is.CacheStorage)

const prefix = 'CacheStorage.delete'
webidl.argumentLengthCheck(arguments, 1, prefix)
Expand All @@ -125,14 +125,20 @@ class CacheStorage {
* @returns {Promise<string[]>}
*/
async keys () {
webidl.brandCheck(this, CacheStorage)
webidl.brandCheck(this, webidl.is.CacheStorage)

// 2.1
const keys = this.#caches.keys()

// 2.2
return [...keys]
}

static {
webidl.is.CacheStorage = (arg) => {
return arg != null && typeof arg === 'object' && #caches in arg
}
}
}

Object.defineProperties(CacheStorage.prototype, {
Expand Down
22 changes: 16 additions & 6 deletions lib/web/cookies/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,19 @@
const { parseSetCookie } = require('./parse')
const { stringify } = require('./util')
const { webidl } = require('../webidl')
const { Headers } = require('../fetch/headers')

const brandChecks = webidl.brandCheckMultiple([Headers, globalThis.Headers].filter(Boolean))
const globalHeadersBrandCheck = (arg) => webidl.brandCheck(arg, webidl.util.MakeTypeAssertion(globalThis.Headers))
const undiciHeadersBrandCheck = (arg) => webidl.brandCheck(arg, webidl.is.Headers)

function brandCheckHeaders (arg) {
try {
undiciHeadersBrandCheck(arg)
return
} catch {
}

globalHeadersBrandCheck(arg)
}

/**
* @typedef {Object} Cookie
Expand All @@ -28,7 +38,7 @@ const brandChecks = webidl.brandCheckMultiple([Headers, globalThis.Headers].filt
function getCookies (headers) {
webidl.argumentLengthCheck(arguments, 1, 'getCookies')

brandChecks(headers)
brandCheckHeaders(headers)

const cookie = headers.get('cookie')

Expand Down Expand Up @@ -57,7 +67,7 @@ function getCookies (headers) {
* @returns {void}
*/
function deleteCookie (headers, name, attributes) {
brandChecks(headers)
brandCheckHeaders(headers)

const prefix = 'deleteCookie'
webidl.argumentLengthCheck(arguments, 2, prefix)
Expand All @@ -82,7 +92,7 @@ function deleteCookie (headers, name, attributes) {
function getSetCookies (headers) {
webidl.argumentLengthCheck(arguments, 1, 'getSetCookies')

brandChecks(headers)
brandCheckHeaders(headers)

const cookies = headers.getSetCookie()

Expand Down Expand Up @@ -111,7 +121,7 @@ function parseCookie (cookie) {
function setCookie (headers, cookie) {
webidl.argumentLengthCheck(arguments, 2, 'setCookie')

brandChecks(headers)
brandCheckHeaders(headers)

cookie = webidl.converters.Cookie(cookie)

Expand Down
26 changes: 25 additions & 1 deletion lib/web/eventsource/eventsource.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,8 @@ class EventSource extends EventTarget {
* @readonly
*/
get readyState () {
webidl.brandCheck(this, webidl.is.EventSource)

return this.#readyState
}

Expand All @@ -200,6 +202,8 @@ class EventSource extends EventTarget {
* @returns {string}
*/
get url () {
webidl.brandCheck(this, webidl.is.EventSource)

return this.#url
}

Expand All @@ -208,6 +212,8 @@ class EventSource extends EventTarget {
* instantiated with CORS credentials set (true), or not (false, the default).
*/
get withCredentials () {
webidl.brandCheck(this, webidl.is.EventSource)

return this.#withCredentials
}

Expand Down Expand Up @@ -363,7 +369,7 @@ class EventSource extends EventTarget {
* CLOSED.
*/
close () {
webidl.brandCheck(this, EventSource)
webidl.brandCheck(this, webidl.is.EventSource)

if (this.#readyState === CLOSED) return
this.#readyState = CLOSED
Expand All @@ -372,10 +378,14 @@ class EventSource extends EventTarget {
}

get onopen () {
webidl.brandCheck(this, webidl.is.EventSource)

return this.#events.open
}

set onopen (fn) {
webidl.brandCheck(this, webidl.is.EventSource)

if (this.#events.open) {
this.removeEventListener('open', this.#events.open)
}
Expand All @@ -391,10 +401,14 @@ class EventSource extends EventTarget {
}

get onmessage () {
webidl.brandCheck(this, webidl.is.EventSource)

return this.#events.message
}

set onmessage (fn) {
webidl.brandCheck(this, webidl.is.EventSource)

if (this.#events.message) {
this.removeEventListener('message', this.#events.message)
}
Expand All @@ -410,10 +424,14 @@ class EventSource extends EventTarget {
}

get onerror () {
webidl.brandCheck(this, webidl.is.EventSource)

return this.#events.error
}

set onerror (fn) {
webidl.brandCheck(this, webidl.is.EventSource)

if (this.#events.error) {
this.removeEventListener('error', this.#events.error)
}
Expand All @@ -427,6 +445,12 @@ class EventSource extends EventTarget {
this.#events.error = null
}
}

static {
webidl.is.EventSource = (arg) => {
return arg != null && typeof arg === 'object' && #events in arg
}
}
}

const constantsPropertyDescriptors = {
Expand Down
24 changes: 12 additions & 12 deletions lib/web/fetch/body.js
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ function cloneBody (body) {
}
}

function bodyMixinMethods (instance, getInternalState) {
function bodyMixinMethods (brandCheck, getInternalState) {
const methods = {
blob () {
// The blob() method steps are to return the result of
Expand All @@ -313,7 +313,7 @@ function bodyMixinMethods (instance, getInternalState) {
// Return a Blob whose contents are bytes and type attribute
// is mimeType.
return new Blob([bytes], { type: mimeType })
}, instance, getInternalState)
}, brandCheck, getInternalState)
},

arrayBuffer () {
Expand All @@ -323,19 +323,19 @@ function bodyMixinMethods (instance, getInternalState) {
// whose contents are bytes.
return consumeBody(this, (bytes) => {
return new Uint8Array(bytes).buffer
}, instance, getInternalState)
}, brandCheck, 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, getInternalState)
return consumeBody(this, utf8DecodeBytes, brandCheck, 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, getInternalState)
return consumeBody(this, parseJSONFromBytes, brandCheck, getInternalState)
},

formData () {
Expand Down Expand Up @@ -383,7 +383,7 @@ function bodyMixinMethods (instance, getInternalState) {
throw new TypeError(
'Content-Type was not one of "multipart/form-data" or "application/x-www-form-urlencoded".'
)
}, instance, getInternalState)
}, brandCheck, getInternalState)
},

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

textStream () {
Expand Down Expand Up @@ -442,20 +442,20 @@ function bodyMixinMethods (instance, getInternalState) {
return methods
}

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

/**
* @see https://fetch.spec.whatwg.org/#concept-body-consume-body
* @param {any} object internal state
* @param {(value: unknown) => unknown} convertBytesToJSValue
* @param {any} instance
* @param {import('../../../types/webidl').WebidlIsFunction} brandCheck
* @param {(target: any) => any} getInternalState
*/
function consumeBody (object, convertBytesToJSValue, instance, getInternalState) {
function consumeBody (object, convertBytesToJSValue, brandCheck, getInternalState) {
try {
webidl.brandCheck(object, instance)
webidl.brandCheck(object, brandCheck)
} catch (e) {
return Promise.reject(e)
}
Expand Down
Loading
Loading