Skip to content

Commit d7641d8

Browse files
addaleaxBridgeAR
authored andcommitted
worker: use DataCloneError for unknown native objects
This aligns the behaviour better with the web. PR-URL: #28025 Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Rich Trott <[email protected]> Reviewed-By: Joyee Cheung <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Tiancheng "Timothy" Gu <[email protected]>
1 parent 5fc4e48 commit d7641d8

File tree

5 files changed

+49
-9
lines changed

5 files changed

+49
-9
lines changed

doc/api/errors.md

+10-6
Original file line numberDiff line numberDiff line change
@@ -671,12 +671,6 @@ An operation outside the bounds of a `Buffer` was attempted.
671671
An attempt has been made to create a `Buffer` larger than the maximum allowed
672672
size.
673673

674-
<a id="ERR_CANNOT_TRANSFER_OBJECT"></a>
675-
### ERR_CANNOT_TRANSFER_OBJECT
676-
677-
The value passed to `postMessage()` contained an object that is not supported
678-
for transferring.
679-
680674
<a id="ERR_CANNOT_WATCH_SIGINT"></a>
681675
### ERR_CANNOT_WATCH_SIGINT
682676

@@ -2013,6 +2007,16 @@ A module file could not be resolved while attempting a [`require()`][] or
20132007
> Stability: 0 - Deprecated. These error codes are either inconsistent, or have
20142008
> been removed.
20152009
2010+
<a id="ERR_CANNOT_TRANSFER_OBJECT"></a>
2011+
### ERR_CANNOT_TRANSFER_OBJECT
2012+
<!--
2013+
added: v10.5.0
2014+
removed: REPLACEME
2015+
-->
2016+
2017+
The value passed to `postMessage()` contained an object that is not supported
2018+
for transferring.
2019+
20162020
<a id="ERR_CLOSED_MESSAGE_PORT"></a>
20172021
### ERR_CLOSED_MESSAGE_PORT
20182022
<!-- YAML

src/env.h

+1
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ constexpr size_t kFsStatsBufferLength = kFsStatsFieldsNumber * 2;
158158
V(change_string, "change") \
159159
V(channel_string, "channel") \
160160
V(chunks_sent_since_last_write_string, "chunksSentSinceLastWrite") \
161+
V(clone_unsupported_type_str, "Cannot transfer object of unsupported type.") \
161162
V(code_string, "code") \
162163
V(commonjs_string, "commonjs") \
163164
V(config_string, "config") \

src/node_errors.h

-2
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ void FatalException(v8::Isolate* isolate,
5454
V(ERR_BUFFER_CONTEXT_NOT_AVAILABLE, Error) \
5555
V(ERR_BUFFER_OUT_OF_BOUNDS, RangeError) \
5656
V(ERR_BUFFER_TOO_LARGE, Error) \
57-
V(ERR_CANNOT_TRANSFER_OBJECT, TypeError) \
5857
V(ERR_CONSTRUCT_CALL_REQUIRED, Error) \
5958
V(ERR_INVALID_ARG_VALUE, TypeError) \
6059
V(ERR_INVALID_ARG_TYPE, TypeError) \
@@ -100,7 +99,6 @@ void FatalException(v8::Isolate* isolate,
10099
#define PREDEFINED_ERROR_MESSAGES(V) \
101100
V(ERR_BUFFER_CONTEXT_NOT_AVAILABLE, \
102101
"Buffer is not available for the current Context") \
103-
V(ERR_CANNOT_TRANSFER_OBJECT, "Cannot transfer object of unsupported type")\
104102
V(ERR_CONSTRUCT_CALL_REQUIRED, "Cannot call constructor without `new`") \
105103
V(ERR_INVALID_TRANSFER_OBJECT, "Found invalid object in transferList") \
106104
V(ERR_MEMORY_ALLOCATION_FAILED, "Failed to allocate memory") \

src/node_messaging.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ class SerializerDelegate : public ValueSerializer::Delegate {
232232
return WriteMessagePort(Unwrap<MessagePort>(object));
233233
}
234234

235-
THROW_ERR_CANNOT_TRANSFER_OBJECT(env_);
235+
ThrowDataCloneError(env_->clone_unsupported_type_str());
236236
return Nothing<bool>();
237237
}
238238

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Flags: --expose-internals
2+
'use strict';
3+
const common = require('../common');
4+
const assert = require('assert');
5+
const { MessageChannel } = require('worker_threads');
6+
const { internalBinding } = require('internal/test/binding');
7+
8+
// Test that passing native objects and functions to .postMessage() throws
9+
// DataCloneError exceptions.
10+
11+
{
12+
const { port1, port2 } = new MessageChannel();
13+
port2.once('message', common.mustNotCall());
14+
15+
assert.throws(() => {
16+
port1.postMessage(function foo() {});
17+
}, {
18+
name: 'DataCloneError',
19+
message: /function foo\(\) \{\} could not be cloned\.$/
20+
});
21+
port1.close();
22+
}
23+
24+
{
25+
const { port1, port2 } = new MessageChannel();
26+
port2.once('message', common.mustNotCall());
27+
28+
const nativeObject = new (internalBinding('js_stream').JSStream)();
29+
30+
assert.throws(() => {
31+
port1.postMessage(nativeObject);
32+
}, {
33+
name: 'DataCloneError',
34+
message: /Cannot transfer object of unsupported type\.$/
35+
});
36+
port1.close();
37+
}

0 commit comments

Comments
 (0)