forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
External modules need a way to decorate their objects so that node can recognize it as a host object for serialization process. Exposing a way for turning off instead of turning on is much safer. PR-URL: nodejs#55234 Refs: nodejs#55178 Reviewed-By: Chengzhong Wu <legendecas@gmail.com> Reviewed-By: Daeyeon Jeong <daeyeon.dev@gmail.com> Reviewed-By: Matthew Aitken <maitken033380023@gmail.com>
- Loading branch information
Showing
4 changed files
with
120 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
'use strict'; | ||
|
||
require('../common'); | ||
const assert = require('assert'); | ||
const { markAsUncloneable } = require('node:worker_threads'); | ||
const { mustCall } = require('../common'); | ||
|
||
const expectedErrorName = 'DataCloneError'; | ||
|
||
// Uncloneables cannot be cloned during message posting | ||
{ | ||
const anyObject = { foo: 'bar' }; | ||
markAsUncloneable(anyObject); | ||
const { port1 } = new MessageChannel(); | ||
assert.throws(() => port1.postMessage(anyObject), { | ||
constructor: DOMException, | ||
name: expectedErrorName, | ||
code: 25, | ||
}, `Should throw ${expectedErrorName} when posting uncloneables`); | ||
} | ||
|
||
// Uncloneables cannot be cloned during structured cloning | ||
{ | ||
class MockResponse extends Response { | ||
constructor() { | ||
super(); | ||
markAsUncloneable(this); | ||
} | ||
} | ||
structuredClone(MockResponse.prototype); | ||
|
||
markAsUncloneable(MockResponse.prototype); | ||
const r = new MockResponse(); | ||
assert.throws(() => structuredClone(r), { | ||
constructor: DOMException, | ||
name: expectedErrorName, | ||
code: 25, | ||
}, `Should throw ${expectedErrorName} when cloning uncloneables`); | ||
} | ||
|
||
// markAsUncloneable cannot affect ArrayBuffer | ||
{ | ||
const pooledBuffer = new ArrayBuffer(8); | ||
const { port1, port2 } = new MessageChannel(); | ||
markAsUncloneable(pooledBuffer); | ||
port1.postMessage(pooledBuffer); | ||
port2.on('message', mustCall((value) => { | ||
assert.deepStrictEqual(value, pooledBuffer); | ||
port2.close(mustCall()); | ||
})); | ||
} | ||
|
||
// markAsUncloneable can affect Node.js built-in object like Blob | ||
{ | ||
const cloneableBlob = new Blob(); | ||
const { port1, port2 } = new MessageChannel(); | ||
port1.postMessage(cloneableBlob); | ||
port2.on('message', mustCall((value) => { | ||
assert.deepStrictEqual(value, cloneableBlob); | ||
port2.close(mustCall()); | ||
})); | ||
|
||
const uncloneableBlob = new Blob(); | ||
markAsUncloneable(uncloneableBlob); | ||
assert.throws(() => port1.postMessage(uncloneableBlob), { | ||
constructor: DOMException, | ||
name: expectedErrorName, | ||
code: 25, | ||
}, `Should throw ${expectedErrorName} when cloning uncloneables`); | ||
} |