Skip to content

Commit

Permalink
fix: mark more web instances uncloneable
Browse files Browse the repository at this point in the history
  • Loading branch information
jazelly committed Oct 11, 2024
1 parent e6c3af6 commit 959efaa
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 6 deletions.
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
1 change: 1 addition & 0 deletions lib/web/websocket/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,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
18 changes: 12 additions & 6 deletions test/node-platform-objects.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,26 @@
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('..')
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('undici instances should be uncloneable if node exposes api', async (t) => {
test('unserializable web instances should be uncloneable if node exposes the api', (t) => {
if (markAsUncloneable !== undefined) {
t = tspl(t, { plan: 8 })
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: 'message', brand: 'MessageEvent' },
{ Uncloneable: CloseEvent, value: 'dummy type', brand: 'CloseEvent' },
{ 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: 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)),
Expand Down
6 changes: 6 additions & 0 deletions types/webidl.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ interface WebidlUtil {
* Stringifies {@param V}
*/
Stringify (V: any): string

/**
* 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

0 comments on commit 959efaa

Please sign in to comment.