diff options
author | Chengzhong Wu <legendecas@gmail.com> | 2023-05-05 19:22:42 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-05-05 11:22:42 +0000 |
commit | 64549731b6cfe3729cb42896ab1f8229b62ff968 (patch) | |
tree | 3e321ce7840bb51d663a880bba908d3dd26d6e36 /doc/api | |
parent | 3c82d48cc0a440d91dd2c813201d9a4ecc7a2e62 (diff) | |
download | node-new-64549731b6cfe3729cb42896ab1f8229b62ff968.tar.gz |
src: throw DataCloneError on transfering untransferable objects
The HTML StructuredSerializeWithTransfer algorithm defines that when
an untransferable object is in the transfer list, a DataCloneError is
thrown.
An array buffer that is already transferred is also considered as
untransferable.
PR-URL: https://github.com/nodejs/node/pull/47604
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Diffstat (limited to 'doc/api')
-rw-r--r-- | doc/api/worker_threads.md | 44 |
1 files changed, 40 insertions, 4 deletions
diff --git a/doc/api/worker_threads.md b/doc/api/worker_threads.md index f3f500c907..09b695eb96 100644 --- a/doc/api/worker_threads.md +++ b/doc/api/worker_threads.md @@ -130,8 +130,11 @@ added: - v12.19.0 --> +* `object` {any} Any arbitrary JavaScript value. + Mark an object as not transferable. If `object` occurs in the transfer list of -a [`port.postMessage()`][] call, it is ignored. +a [`port.postMessage()`][] call, an error is thrown. This is a no-op if +`object` is a primitive value. In particular, this makes sense for objects that can be cloned, rather than transferred, and which are used by other objects on the sending side. @@ -150,11 +153,17 @@ const typedArray2 = new Float64Array(pooledBuffer); markAsUntransferable(pooledBuffer); const { port1 } = new MessageChannel(); -port1.postMessage(typedArray1, [ typedArray1.buffer ]); +try { + // This will throw an error, because pooledBuffer is not transferable. + port1.postMessage(typedArray1, [ typedArray1.buffer ]); +} catch (error) { + // error.name === 'DataCloneError' +} // The following line prints the contents of typedArray1 -- it still owns -// its memory and has been cloned, not transferred. Without -// `markAsUntransferable()`, this would print an empty Uint8Array. +// its memory and has not been transferred. Without +// `markAsUntransferable()`, this would print an empty Uint8Array and the +// postMessage call would have succeeded. // typedArray2 is intact as well. console.log(typedArray1); console.log(typedArray2); @@ -162,6 +171,29 @@ console.log(typedArray2); There is no equivalent to this API in browsers. +## `worker.isMarkedAsUntransferable(object)` + +<!-- YAML +added: REPLACEME +--> + +* `object` {any} Any JavaScript value. +* Returns: {boolean} + +Check if an object is marked as not transferable with +[`markAsUntransferable()`][]. + +```js +const { markAsUntransferable, isMarkedAsUntransferable } = require('node:worker_threads'); + +const pooledBuffer = new ArrayBuffer(8); +markAsUntransferable(pooledBuffer); + +isMarkedAsUntransferable(pooledBuffer); // Returns true. +``` + +There is no equivalent to this API in browsers. + ## `worker.moveMessagePortToContext(port, contextifiedSandbox)` <!-- YAML @@ -568,6 +600,10 @@ are part of the channel. <!-- YAML added: v10.5.0 changes: + - version: REPLACEME + pr-url: https://github.com/nodejs/node/pull/47604 + description: An error is thrown when an untransferable object is in the + transfer list. - version: - v15.14.0 - v14.18.0 |