@@ -128,10 +128,18 @@ if (isMainThread) {
128
128
added:
129
129
- v14.5.0
130
130
- v12.19.0
131
+ changes:
132
+ - version: REPLACEME
133
+ pr-url: https://github.com/nodejs/node/pull/47604
134
+ description: An error is thrown when the untransferable object is in the
135
+ transfer list.
131
136
-->
132
137
138
+ * ` object ` {any} Any arbitrary JavaScript value.
139
+
133
140
Mark an object as not transferable. If ` object ` occurs in the transfer list of
134
- a [ ` port.postMessage() ` ] [ ] call, it is ignored.
141
+ a [ ` port.postMessage() ` ] [ ] call, an error is thrown. This is a no-op if
142
+ ` object ` is a primitive value.
135
143
136
144
In particular, this makes sense for objects that can be cloned, rather than
137
145
transferred, and which are used by other objects on the sending side.
@@ -150,10 +158,15 @@ const typedArray2 = new Float64Array(pooledBuffer);
150
158
markAsUntransferable (pooledBuffer);
151
159
152
160
const { port1 } = new MessageChannel ();
153
- port1 .postMessage (typedArray1, [ typedArray1 .buffer ]);
161
+ try {
162
+ // This will throw an error, because pooledBuffer is not transferable.
163
+ port1 .postMessage (typedArray1, [ typedArray1 .buffer ]);
164
+ } catch (error) {
165
+ // error.name === 'DataCloneError'
166
+ }
154
167
155
168
// The following line prints the contents of typedArray1 -- it still owns
156
- // its memory and has been cloned, not transferred. Without
169
+ // its memory and has not been transferred. Without
157
170
// `markAsUntransferable()`, this would print an empty Uint8Array.
158
171
// typedArray2 is intact as well.
159
172
console .log (typedArray1);
@@ -162,6 +175,29 @@ console.log(typedArray2);
162
175
163
176
There is no equivalent to this API in browsers.
164
177
178
+ ## ` worker.isMarkedAsUntransferable(object) `
179
+
180
+ <!-- YAML
181
+ added: REPLACEME
182
+ -->
183
+
184
+ * ` object ` {any} Any arbitrary JavaScript value.
185
+ * Returns: {boolean}
186
+
187
+ Check is an object is marked as not transferable with
188
+ [ ` markAsUntransferable() ` ] [ ] .
189
+
190
+ ``` js
191
+ const { markAsUntransferable , isMarkedAsUntransferable } = require (' node:worker_threads' );
192
+
193
+ const pooledBuffer = new ArrayBuffer (8 );
194
+ markAsUntransferable (pooledBuffer);
195
+
196
+ isMarkedAsUntransferable (pooledBuffer); // Returns true.
197
+ ```
198
+
199
+ There is no equivalent to this API in browsers.
200
+
165
201
## ` worker.moveMessagePortToContext(port, contextifiedSandbox) `
166
202
167
203
<!-- YAML
@@ -568,6 +604,10 @@ are part of the channel.
568
604
<!-- YAML
569
605
added: v10.5.0
570
606
changes:
607
+ - version: REPLACEME
608
+ pr-url: https://github.com/nodejs/node/pull/47604
609
+ description: An error is thrown when an untransferable object is in the
610
+ transfer list.
571
611
- version:
572
612
- v15.14.0
573
613
- v14.18.0
0 commit comments