Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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: 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 @@ -420,6 +420,8 @@ class Headers {
#headersList

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 @@ -113,6 +113,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 @@ -119,6 +120,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
3 changes: 3 additions & 0 deletions lib/web/websocket/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class MessageEvent extends Event {
constructor (type, eventInitDict = {}) {
if (type === kConstruct) {
super(arguments[1], arguments[2])
webidl.util.markAsUncloneable(this)
return
}

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

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

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

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

get wasClean () {
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
27 changes: 27 additions & 0 deletions test/node-platform-objects.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
'use strict'

const { tspl } = require('@matteo.collina/tspl')
const { test } = require('node:test')
const { markAsUncloneable } = require('node:worker_threads')
const { Response, Request, FormData, Headers, MessageEvent, CloseEvent, EventSource, WebSocket } = require('..')

test('undici instances should be uncloneable if node exposes api', async (t) => {
if (markAsUncloneable !== undefined) {
t = tspl(t, { plan: 8 })
const uncloneables = [
{ Uncloneable: Response, brand: 'Response' },
{ Uncloneable: Request, value: 'http://localhost', brand: 'Request' },
{ Uncloneable: FormData, brand: 'FormData' },
{ Uncloneable: MessageEvent, value: 'message', brand: 'MessageEvent' },
{ Uncloneable: CloseEvent, value: 'dummy type', brand: 'CloseEvent' },
{ Uncloneable: EventSource, value: 'http://localhost', brand: 'EventSource' },
{ Uncloneable: Headers, brand: 'Headers' },
{ Uncloneable: WebSocket, value: 'http://localhost', brand: 'WebSocket' }
]
uncloneables.forEach((platformEntity) => {
t.throws(() => structuredClone(new platformEntity.Uncloneable(platformEntity.value)),
DOMException,
`Cloning ${platformEntity.brand} should throw DOMException`)
})
}
})
Loading