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

web: mark as uncloneable when possible #3709

Merged
merged 8 commits into from
Oct 11, 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
1 change: 1 addition & 0 deletions lib/web/cache/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class Cache {
webidl.illegalConstructor()
}

webidl.util.markAsUncloneable(this)
this.#relevantRequestResponseList = arguments[1]
}

Expand Down
2 changes: 2 additions & 0 deletions lib/web/cache/cachestorage.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ class CacheStorage {
if (arguments[0] !== kConstruct) {
webidl.illegalConstructor()
}

webidl.util.markAsUncloneable(this)
}

async match (request, options = {}) {
Expand Down
2 changes: 2 additions & 0 deletions lib/web/eventsource/eventsource.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ class EventSource extends EventTarget {
// 1. Let ev be a new EventSource object.
super()

webidl.util.markAsUncloneable(this)

const prefix = 'EventSource constructor'
webidl.argumentLengthCheck(arguments, 1, prefix)

Expand Down
2 changes: 2 additions & 0 deletions lib/web/fetch/formdata.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ class FormData {
#state = []

constructor (form) {
webidl.util.markAsUncloneable(this)

if (form !== undefined) {
throw webidl.errors.conversionFailed({
prefix: 'FormData constructor',
Expand Down
2 changes: 2 additions & 0 deletions lib/web/fetch/headers.js
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,8 @@ class Headers {
* @returns
*/
constructor (init = undefined) {
webidl.util.markAsUncloneable(this)

if (init === kConstruct) {
return
}
Expand Down
2 changes: 2 additions & 0 deletions lib/web/fetch/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ class Request {

// https://fetch.spec.whatwg.org/#dom-request
constructor (input, init = undefined) {
webidl.util.markAsUncloneable(this)

if (input === kConstruct) {
return
}
Expand Down
2 changes: 2 additions & 0 deletions lib/web/fetch/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ class Response {

// https://fetch.spec.whatwg.org/#dom-response
constructor (body = null, init = undefined) {
webidl.util.markAsUncloneable(this)

if (body === kConstruct) {
return
}
Expand Down
3 changes: 3 additions & 0 deletions lib/web/fetch/webidl.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict'

const { types, inspect } = require('node:util')
const { markAsUncloneable } = require('node:worker_threads')
const { toUSVString } = require('../../core/util')

const UNDEFINED = 1
Expand Down Expand Up @@ -131,6 +132,8 @@ webidl.util.TypeValueToString = function (o) {
}
}

webidl.util.markAsUncloneable = markAsUncloneable || (() => {})

// https://webidl.spec.whatwg.org/#abstract-opdef-converttoint
webidl.util.ConvertToInt = function (V, bitLength, signedness, opts) {
let upperBound
Expand Down
4 changes: 4 additions & 0 deletions lib/web/websocket/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class MessageEvent extends Event {
constructor (type, eventInitDict = {}) {
if (type === kConstruct) {
super(arguments[1], arguments[2])
webidl.util.markAsUncloneable(this)
return
}

Expand All @@ -25,6 +26,7 @@ class MessageEvent extends Event {
super(type, eventInitDict)

this.#eventInit = eventInitDict
webidl.util.markAsUncloneable(this)
}

get data () {
Expand Down Expand Up @@ -111,6 +113,7 @@ class CloseEvent extends Event {
super(type, eventInitDict)

this.#eventInit = eventInitDict
webidl.util.markAsUncloneable(this)
}

get wasClean () {
Expand Down Expand Up @@ -141,6 +144,7 @@ class ErrorEvent extends Event {
webidl.argumentLengthCheck(arguments, 1, prefix)

super(type, eventInitDict)
webidl.util.markAsUncloneable(this)

type = webidl.converters.DOMString(type, prefix, 'type')
eventInitDict = webidl.converters.ErrorEventInit(eventInitDict ?? {})
Expand Down
2 changes: 2 additions & 0 deletions lib/web/websocket/websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ class WebSocket extends EventTarget {
constructor (url, protocols = []) {
super()

webidl.util.markAsUncloneable(this)

const prefix = 'WebSocket constructor'
webidl.argumentLengthCheck(arguments, 1, prefix)

Expand Down
33 changes: 33 additions & 0 deletions test/node-platform-objects.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
'use strict'

const { tspl } = require('@matteo.collina/tspl')
const { test } = require('node:test')
const { markAsUncloneable } = require('node:worker_threads')
const { Response, Request, FormData, Headers, ErrorEvent, MessageEvent, CloseEvent, EventSource, WebSocket } = require('..')
const { CacheStorage } = require('../lib/web/cache/cachestorage')
const { Cache } = require('../lib/web/cache/cache')
const { kConstruct } = require('../lib/core/symbols')

test('unserializable web instances should be uncloneable if node exposes the api', (t) => {
if (markAsUncloneable !== undefined) {
t = tspl(t, { plan: 11 })
const uncloneables = [
{ Uncloneable: Response, brand: 'Response' },
{ Uncloneable: Request, value: 'http://localhost', brand: 'Request' },
{ Uncloneable: FormData, brand: 'FormData' },
{ Uncloneable: MessageEvent, value: 'dummy event', brand: 'MessageEvent' },
{ Uncloneable: CloseEvent, value: 'dummy event', brand: 'CloseEvent' },
{ Uncloneable: ErrorEvent, value: 'dummy event', brand: 'ErrorEvent' },
{ Uncloneable: EventSource, value: 'http://localhost', brand: 'EventSource' },
{ Uncloneable: Headers, brand: 'Headers' },
{ Uncloneable: WebSocket, value: 'http://localhost', brand: 'WebSocket' },
{ Uncloneable: Cache, value: kConstruct, brand: 'Cache' },
{ Uncloneable: CacheStorage, value: kConstruct, brand: 'CacheStorage' }
]
uncloneables.forEach((platformEntity) => {
t.throws(() => structuredClone(new platformEntity.Uncloneable(platformEntity.value)),
DOMException,
`Cloning ${platformEntity.brand} should throw DOMException`)
})
}
})
6 changes: 6 additions & 0 deletions types/webidl.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ interface WebidlUtil {
Stringify (V: any): string

MakeTypeAssertion <I>(I: I): (arg: any) => arg is I

/**
* Mark a value as uncloneable for Node.js.
* This is only effective in some newer Node.js versions.
*/
markAsUncloneable (V: any): void
}

interface WebidlConverters {
Expand Down
Loading