Skip to content

Commit d920b7c

Browse files
authored
src: throw DOMException on cloning non-serializable objects
Instead of TypeError, throwing DOMException in accordance to the HTML structured serialize algorithms. PR-URL: #47839 Fixes: #40841 Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Yagiz Nizipli <[email protected]>
1 parent e43bf4c commit d920b7c

7 files changed

+61
-38
lines changed

doc/api/errors.md

+36-23
Original file line numberDiff line numberDiff line change
@@ -2084,12 +2084,6 @@ urlSearchParams.has.call(buf, 'foo');
20842084
// Throws a TypeError with code 'ERR_INVALID_THIS'
20852085
```
20862086

2087-
<a id="ERR_INVALID_TRANSFER_OBJECT"></a>
2088-
2089-
### `ERR_INVALID_TRANSFER_OBJECT`
2090-
2091-
An invalid transfer object was passed to `postMessage()`.
2092-
20932087
<a id="ERR_INVALID_TUPLE"></a>
20942088

20952089
### `ERR_INVALID_TUPLE`
@@ -2287,23 +2281,6 @@ The V8 platform used by this instance of Node.js does not support creating
22872281
Workers. This is caused by lack of embedder support for Workers. In particular,
22882282
this error will not occur with standard builds of Node.js.
22892283

2290-
<a id="ERR_MISSING_TRANSFERABLE_IN_TRANSFER_LIST"></a>
2291-
2292-
### `ERR_MISSING_TRANSFERABLE_IN_TRANSFER_LIST`
2293-
2294-
<!-- YAML
2295-
added: v15.0.0
2296-
-->
2297-
2298-
An object that needs to be explicitly listed in the `transferList` argument
2299-
is in the object passed to a [`postMessage()`][] call, but is not provided
2300-
in the `transferList` for that call. Usually, this is a `MessagePort`.
2301-
2302-
In Node.js versions prior to v15.0.0, the error code being used here was
2303-
[`ERR_MISSING_MESSAGE_PORT_IN_TRANSFER_LIST`][]. However, the set of
2304-
transferable object types has been expanded to cover more types than
2305-
`MessagePort`.
2306-
23072284
<a id="ERR_MODULE_NOT_FOUND"></a>
23082285

23092286
### `ERR_MODULE_NOT_FOUND`
@@ -3300,6 +3277,20 @@ removed: v15.0.0
33003277

33013278
An invalid or unknown file encoding was passed.
33023279

3280+
<a id="ERR_INVALID_TRANSFER_OBJECT"></a>
3281+
3282+
### `ERR_INVALID_TRANSFER_OBJECT`
3283+
3284+
<!-- YAML
3285+
removed: REPLACEME
3286+
changes:
3287+
- version: REPLACEME
3288+
pr-url: https://github.com/nodejs/node/pull/47839
3289+
description: A `DOMException` is thrown instead.
3290+
-->
3291+
3292+
An invalid transfer object was passed to `postMessage()`.
3293+
33033294
<a id="ERR_MISSING_MESSAGE_PORT_IN_TRANSFER_LIST"></a>
33043295

33053296
### `ERR_MISSING_MESSAGE_PORT_IN_TRANSFER_LIST`
@@ -3312,6 +3303,28 @@ This error code was replaced by [`ERR_MISSING_TRANSFERABLE_IN_TRANSFER_LIST`][]
33123303
in Node.js v15.0.0, because it is no longer accurate as other types of
33133304
transferable objects also exist now.
33143305

3306+
<a id="ERR_MISSING_TRANSFERABLE_IN_TRANSFER_LIST"></a>
3307+
3308+
### `ERR_MISSING_TRANSFERABLE_IN_TRANSFER_LIST`
3309+
3310+
<!-- YAML
3311+
added: v15.0.0
3312+
removed: REPLACEME
3313+
changes:
3314+
- version: REPLACEME
3315+
pr-url: https://github.com/nodejs/node/pull/47839
3316+
description: A `DOMException` is thrown instead.
3317+
-->
3318+
3319+
An object that needs to be explicitly listed in the `transferList` argument
3320+
is in the object passed to a [`postMessage()`][] call, but is not provided
3321+
in the `transferList` for that call. Usually, this is a `MessagePort`.
3322+
3323+
In Node.js versions prior to v15.0.0, the error code being used here was
3324+
[`ERR_MISSING_MESSAGE_PORT_IN_TRANSFER_LIST`][]. However, the set of
3325+
transferable object types has been expanded to cover more types than
3326+
`MessagePort`.
3327+
33153328
<a id="ERR_NAPI_CONS_PROTOTYPE_OBJECT"></a>
33163329

33173330
### `ERR_NAPI_CONS_PROTOTYPE_OBJECT`

src/env_properties.h

+4
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@
7474
V(channel_string, "channel") \
7575
V(chunks_sent_since_last_write_string, "chunksSentSinceLastWrite") \
7676
V(clone_unsupported_type_str, "Cannot clone object of unsupported type.") \
77+
V(clone_transfer_needed_str, \
78+
"Object that needs transfer was found in message but not listed in " \
79+
"transferList") \
80+
V(clone_untransferable_str, "Found invalid value in transferList.") \
7781
V(code_string, "code") \
7882
V(commonjs_string, "commonjs") \
7983
V(config_string, "config") \

src/node_errors.h

-6
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,11 @@ void AppendExceptionLine(Environment* env,
7575
V(ERR_INVALID_MODULE, Error) \
7676
V(ERR_INVALID_STATE, Error) \
7777
V(ERR_INVALID_THIS, TypeError) \
78-
V(ERR_INVALID_TRANSFER_OBJECT, TypeError) \
7978
V(ERR_INVALID_URL, TypeError) \
8079
V(ERR_INVALID_URL_SCHEME, TypeError) \
8180
V(ERR_MEMORY_ALLOCATION_FAILED, Error) \
8281
V(ERR_MESSAGE_TARGET_CONTEXT_UNAVAILABLE, Error) \
8382
V(ERR_MISSING_ARGS, TypeError) \
84-
V(ERR_MISSING_TRANSFERABLE_IN_TRANSFER_LIST, TypeError) \
8583
V(ERR_MISSING_PASSPHRASE, TypeError) \
8684
V(ERR_MISSING_PLATFORM_FOR_WORKER, Error) \
8785
V(ERR_MODULE_NOT_FOUND, Error) \
@@ -168,16 +166,12 @@ ERRORS_WITH_CODE(V)
168166
V(ERR_INVALID_MODULE, "No such module") \
169167
V(ERR_INVALID_STATE, "Invalid state") \
170168
V(ERR_INVALID_THIS, "Value of \"this\" is the wrong type") \
171-
V(ERR_INVALID_TRANSFER_OBJECT, "Found invalid object in transferList") \
172169
V(ERR_INVALID_URL_SCHEME, "The URL must be of scheme file:") \
173170
V(ERR_MEMORY_ALLOCATION_FAILED, "Failed to allocate memory") \
174171
V(ERR_OSSL_EVP_INVALID_DIGEST, "Invalid digest used") \
175172
V(ERR_MESSAGE_TARGET_CONTEXT_UNAVAILABLE, \
176173
"A message object could not be deserialized successfully in the target " \
177174
"vm.Context") \
178-
V(ERR_MISSING_TRANSFERABLE_IN_TRANSFER_LIST, \
179-
"Object that needs transfer was found in message but not listed " \
180-
"in transferList") \
181175
V(ERR_MISSING_PLATFORM_FOR_WORKER, \
182176
"The V8 platform used by this instance of Node does not support " \
183177
"creating Workers") \

src/node_messaging.cc

+3-3
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,7 @@ class SerializerDelegate : public ValueSerializer::Delegate {
431431
return Just(true);
432432
}
433433
}
434-
THROW_ERR_MISSING_TRANSFERABLE_IN_TRANSFER_LIST(env_);
434+
ThrowDataCloneError(env_->clone_transfer_needed_str());
435435
return Nothing<bool>();
436436
}
437437

@@ -475,7 +475,7 @@ Maybe<bool> Message::Serialize(Environment* env,
475475
Local<Value> entry_val = transfer_list_v[i];
476476
if (!entry_val->IsObject()) {
477477
// Only object can be transferred.
478-
THROW_ERR_INVALID_TRANSFER_OBJECT(env);
478+
ThrowDataCloneException(context, env->clone_untransferable_str());
479479
return Nothing<bool>();
480480
}
481481
Local<Object> entry = entry_val.As<Object>();
@@ -533,7 +533,7 @@ Maybe<bool> Message::Serialize(Environment* env,
533533
host_object = BaseObjectPtr<BaseObject>{Unwrap<BaseObject>(entry)};
534534
} else {
535535
if (!JSTransferable::IsJSTransferable(env, context, entry)) {
536-
THROW_ERR_INVALID_TRANSFER_OBJECT(env);
536+
ThrowDataCloneException(context, env->clone_untransferable_str());
537537
return Nothing<bool>();
538538
}
539539
JSTransferable* js_transferable = JSTransferable::Wrap(env, entry);

test/parallel/test-whatwg-webstreams-transfer.js

+12-4
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,9 @@ const theData = 'hello';
105105
});
106106

107107
assert.throws(() => port2.postMessage(readable), {
108-
code: 'ERR_MISSING_TRANSFERABLE_IN_TRANSFER_LIST',
108+
constructor: DOMException,
109+
name: 'DataCloneError',
110+
code: 25,
109111
});
110112

111113
port2.postMessage(readable, [readable]);
@@ -155,7 +157,9 @@ const theData = 'hello';
155157
});
156158

157159
assert.throws(() => port2.postMessage(readable), {
158-
code: 'ERR_MISSING_TRANSFERABLE_IN_TRANSFER_LIST',
160+
constructor: DOMException,
161+
name: 'DataCloneError',
162+
code: 25,
159163
});
160164

161165
port2.postMessage(readable, [readable]);
@@ -206,7 +210,9 @@ const theData = 'hello';
206210
});
207211

208212
assert.throws(() => port2.postMessage(writable), {
209-
code: 'ERR_MISSING_TRANSFERABLE_IN_TRANSFER_LIST',
213+
constructor: DOMException,
214+
name: 'DataCloneError',
215+
code: 25,
210216
});
211217

212218
port2.postMessage(writable, [writable]);
@@ -292,7 +298,9 @@ const theData = 'hello';
292298
});
293299

294300
assert.throws(() => port2.postMessage(transform), {
295-
code: 'ERR_MISSING_TRANSFERABLE_IN_TRANSFER_LIST',
301+
constructor: DOMException,
302+
name: 'DataCloneError',
303+
code: 25,
296304
});
297305

298306
port2.postMessage(transform, [transform]);

test/parallel/test-worker-message-port-transfer-filehandle.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ const { once } = require('events');
1414
assert.throws(() => {
1515
port1.postMessage(fh);
1616
}, {
17-
code: 'ERR_MISSING_TRANSFERABLE_IN_TRANSFER_LIST'
17+
constructor: DOMException,
18+
name: 'DataCloneError',
19+
code: 25,
1820
});
1921

2022
// Check that transferring FileHandle instances works.

test/parallel/test-worker-workerdata-messageport.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,9 @@ const meowScript = () => 'meow';
5454
workerData,
5555
transferList: []
5656
}), {
57-
code: 'ERR_MISSING_TRANSFERABLE_IN_TRANSFER_LIST',
57+
constructor: DOMException,
58+
name: 'DataCloneError',
59+
code: 25,
5860
message: 'Object that needs transfer was found in message but not ' +
5961
'listed in transferList'
6062
});

0 commit comments

Comments
 (0)