Skip to content

Commit b7091f6

Browse files
committed
src: throw DOMException on cloning non-serializable objects
Instead of TypeError, throwing DOMException in accordance to the HTML structured serialize algorithms.
1 parent e90dadf commit b7091f6

7 files changed

+60
-37
lines changed

doc/api/errors.md

+36-23
Original file line numberDiff line numberDiff line change
@@ -2103,12 +2103,6 @@ urlSearchParams.has.call(buf, 'foo');
21032103
// Throws a TypeError with code 'ERR_INVALID_THIS'
21042104
```
21052105

2106-
<a id="ERR_INVALID_TRANSFER_OBJECT"></a>
2107-
2108-
### `ERR_INVALID_TRANSFER_OBJECT`
2109-
2110-
An invalid transfer object was passed to `postMessage()`.
2111-
21122106
<a id="ERR_INVALID_TUPLE"></a>
21132107

21142108
### `ERR_INVALID_TUPLE`
@@ -2306,23 +2300,6 @@ The V8 platform used by this instance of Node.js does not support creating
23062300
Workers. This is caused by lack of embedder support for Workers. In particular,
23072301
this error will not occur with standard builds of Node.js.
23082302

2309-
<a id="ERR_MISSING_TRANSFERABLE_IN_TRANSFER_LIST"></a>
2310-
2311-
### `ERR_MISSING_TRANSFERABLE_IN_TRANSFER_LIST`
2312-
2313-
<!-- YAML
2314-
added: v15.0.0
2315-
-->
2316-
2317-
An object that needs to be explicitly listed in the `transferList` argument
2318-
is in the object passed to a [`postMessage()`][] call, but is not provided
2319-
in the `transferList` for that call. Usually, this is a `MessagePort`.
2320-
2321-
In Node.js versions prior to v15.0.0, the error code being used here was
2322-
[`ERR_MISSING_MESSAGE_PORT_IN_TRANSFER_LIST`][]. However, the set of
2323-
transferable object types has been expanded to cover more types than
2324-
`MessagePort`.
2325-
23262303
<a id="ERR_MODULE_NOT_FOUND"></a>
23272304

23282305
### `ERR_MODULE_NOT_FOUND`
@@ -3305,6 +3282,20 @@ removed: v15.0.0
33053282

33063283
An invalid or unknown file encoding was passed.
33073284

3285+
<a id="ERR_INVALID_TRANSFER_OBJECT"></a>
3286+
3287+
### `ERR_INVALID_TRANSFER_OBJECT`
3288+
3289+
<!-- YAML
3290+
removed: REPLACEME
3291+
changes:
3292+
- version: REPLACEME
3293+
pr-url: https://github.com./nodejs/node/pull/47839
3294+
description: A `DOMException` is thrown instead.
3295+
-->
3296+
3297+
An invalid transfer object was passed to `postMessage()`.
3298+
33083299
<a id="ERR_MISSING_MESSAGE_PORT_IN_TRANSFER_LIST"></a>
33093300

33103301
### `ERR_MISSING_MESSAGE_PORT_IN_TRANSFER_LIST`
@@ -3317,6 +3308,28 @@ This error code was replaced by [`ERR_MISSING_TRANSFERABLE_IN_TRANSFER_LIST`][]
33173308
in Node.js v15.0.0, because it is no longer accurate as other types of
33183309
transferable objects also exist now.
33193310

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

33223335
### `ERR_NAPI_CONS_PROTOTYPE_OBJECT`

src/env_properties.h

+4
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@
6868
V(channel_string, "channel") \
6969
V(chunks_sent_since_last_write_string, "chunksSentSinceLastWrite") \
7070
V(clone_unsupported_type_str, "Cannot clone object of unsupported type.") \
71+
V(clone_transfer_needed_str, \
72+
"Object that needs transfer was found in message but not listed in " \
73+
"transferList") \
74+
V(clone_untransferable_str, "Found invalid object in transferList.") \
7175
V(code_string, "code") \
7276
V(commonjs_string, "commonjs") \
7377
V(config_string, "config") \

src/node_errors.h

-6
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,10 @@ void AppendExceptionLine(Environment* env,
7373
V(ERR_INVALID_MODULE, Error) \
7474
V(ERR_INVALID_STATE, Error) \
7575
V(ERR_INVALID_THIS, TypeError) \
76-
V(ERR_INVALID_TRANSFER_OBJECT, TypeError) \
7776
V(ERR_INVALID_URL, TypeError) \
7877
V(ERR_MEMORY_ALLOCATION_FAILED, Error) \
7978
V(ERR_MESSAGE_TARGET_CONTEXT_UNAVAILABLE, Error) \
8079
V(ERR_MISSING_ARGS, TypeError) \
81-
V(ERR_MISSING_TRANSFERABLE_IN_TRANSFER_LIST, TypeError) \
8280
V(ERR_MISSING_PASSPHRASE, TypeError) \
8381
V(ERR_MISSING_PLATFORM_FOR_WORKER, Error) \
8482
V(ERR_NON_CONTEXT_AWARE_DISABLED, Error) \
@@ -164,15 +162,11 @@ ERRORS_WITH_CODE(V)
164162
V(ERR_INVALID_MODULE, "No such module") \
165163
V(ERR_INVALID_STATE, "Invalid state") \
166164
V(ERR_INVALID_THIS, "Value of \"this\" is the wrong type") \
167-
V(ERR_INVALID_TRANSFER_OBJECT, "Found invalid object in transferList") \
168165
V(ERR_MEMORY_ALLOCATION_FAILED, "Failed to allocate memory") \
169166
V(ERR_OSSL_EVP_INVALID_DIGEST, "Invalid digest used") \
170167
V(ERR_MESSAGE_TARGET_CONTEXT_UNAVAILABLE, \
171168
"A message object could not be deserialized successfully in the target " \
172169
"vm.Context") \
173-
V(ERR_MISSING_TRANSFERABLE_IN_TRANSFER_LIST, \
174-
"Object that needs transfer was found in message but not listed " \
175-
"in transferList") \
176170
V(ERR_MISSING_PLATFORM_FOR_WORKER, \
177171
"The V8 platform used by this instance of Node does not support " \
178172
"creating Workers") \

src/node_messaging.cc

+2-2
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,7 @@ class SerializerDelegate : public ValueSerializer::Delegate {
429429
}
430430

431431
if (mode == BaseObject::TransferMode::kTransferable) {
432-
THROW_ERR_MISSING_TRANSFERABLE_IN_TRANSFER_LIST(env_);
432+
ThrowDataCloneError(env_->clone_transfer_needed_str());
433433
return Nothing<bool>();
434434
}
435435

@@ -560,7 +560,7 @@ Maybe<bool> Message::Serialize(Environment* env,
560560
}
561561
}
562562

563-
THROW_ERR_INVALID_TRANSFER_OBJECT(env);
563+
ThrowDataCloneException(context, env->clone_untransferable_str());
564564
return Nothing<bool>();
565565
}
566566
if (delegate.AddNestedHostObjects().IsNothing())

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)