Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 36.4k
worker: add markAsUncloneable api#55234
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
Changes from all commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -194,6 +194,38 @@ isMarkedAsUntransferable(pooledBuffer); // Returns true. | ||
| There is no equivalent to this API in browsers. | ||
| ## `worker.markAsUncloneable(object)` | ||
| <!-- YAML | ||
| added: REPLACEME | ||
| --> | ||
| * `object` {any} Any arbitrary JavaScript value. | ||
| Mark an object as not cloneable. If `object` is used as [`message`](#event-message) in | ||
| a [`port.postMessage()`][] call, an error is thrown. This is a no-op if `object` is a | ||
| primitive value. | ||
jazelly marked this conversation as resolved.
Outdated
Uh oh!There was an error while loading. Please reload this page. | ||
| This has no effect on `ArrayBuffer`, or any `Buffer` like objects. | ||
| This operation cannot be undone. | ||
| ```js | ||
| const { markAsUncloneable } = require('node:worker_threads'); | ||
| const anyObject = { foo: 'bar' }; | ||
| markAsUncloneable(anyObject); | ||
| const { port1 } = new MessageChannel(); | ||
jazelly marked this conversation as resolved.
Outdated
Uh oh!There was an error while loading. Please reload this page. | ||
| try { | ||
| // This will throw an error, because anyObject is not cloneable. | ||
| port1.postMessage(anyObject) | ||
| } catch (error) { | ||
| // error.name === 'DataCloneError' | ||
| } | ||
jazelly marked this conversation as resolved.
Outdated
Uh oh!There was an error while loading. Please reload this page. | ||
| ``` | ||
| There is no equivalent to this API in browsers. | ||
jazelly marked this conversation as resolved.
Outdated
Uh oh!There was an error while loading. Please reload this page. | ||
| ## `worker.moveMessagePortToContext(port, contextifiedSandbox)` | ||
| <!-- YAML | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -29,6 +29,14 @@ const { | ||
| oninit: onInitSymbol, | ||
| no_message_symbol: noMessageSymbol, | ||
| } = internalBinding('symbols'); | ||
| const { | ||
| privateSymbols: { | ||
| transfer_mode_private_symbol, | ||
| }, | ||
| constants: { | ||
| kCloneable, | ||
| }, | ||
| } = internalBinding('util'); | ||
| const { | ||
| MessagePort, | ||
| MessageChannel, | ||
| @@ -447,13 +455,21 @@ ObjectDefineProperties(BroadcastChannel.prototype, { | ||
| defineEventHandler(BroadcastChannel.prototype, 'message'); | ||
| defineEventHandler(BroadcastChannel.prototype, 'messageerror'); | ||
| function markAsUncloneable(obj) { | ||
| if ((typeof obj !== 'object' && typeof obj !== 'function') || obj === null) { | ||
| return; | ||
| } | ||
jazelly marked this conversation as resolved.
Outdated
Uh oh!There was an error while loading. Please reload this page. | ||
| obj[transfer_mode_private_symbol] &= ~kCloneable; | ||
| } | ||
| module.exports = { | ||
| drainMessagePort, | ||
| messageTypes, | ||
| kPort, | ||
| kIncrementsPortRef, | ||
| kWaitingStreams, | ||
| kStdioWantsMoreDataCallback, | ||
| markAsUncloneable, | ||
| moveMessagePortToContext, | ||
| MessagePort, | ||
| MessageChannel, | ||
| 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`); | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.