Skip to content

Commit 0a626a2

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 f68ff9f commit 0a626a2

6 files changed

+24
-14
lines changed

src/env_properties.h

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

src/node_errors.h

-6
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,9 @@ void AppendExceptionLine(Environment* env,
7272
V(ERR_INVALID_MODULE, Error) \
7373
V(ERR_INVALID_STATE, Error) \
7474
V(ERR_INVALID_THIS, TypeError) \
75-
V(ERR_INVALID_TRANSFER_OBJECT, TypeError) \
7675
V(ERR_MEMORY_ALLOCATION_FAILED, Error) \
7776
V(ERR_MESSAGE_TARGET_CONTEXT_UNAVAILABLE, Error) \
7877
V(ERR_MISSING_ARGS, TypeError) \
79-
V(ERR_MISSING_TRANSFERABLE_IN_TRANSFER_LIST, TypeError) \
8078
V(ERR_MISSING_PASSPHRASE, TypeError) \
8179
V(ERR_MISSING_PLATFORM_FOR_WORKER, Error) \
8280
V(ERR_NON_CONTEXT_AWARE_DISABLED, Error) \
@@ -162,15 +160,11 @@ ERRORS_WITH_CODE(V)
162160
V(ERR_INVALID_MODULE, "No such module") \
163161
V(ERR_INVALID_STATE, "Invalid state") \
164162
V(ERR_INVALID_THIS, "Value of \"this\" is the wrong type") \
165-
V(ERR_INVALID_TRANSFER_OBJECT, "Found invalid object in transferList") \
166163
V(ERR_MEMORY_ALLOCATION_FAILED, "Failed to allocate memory") \
167164
V(ERR_OSSL_EVP_INVALID_DIGEST, "Invalid digest used") \
168165
V(ERR_MESSAGE_TARGET_CONTEXT_UNAVAILABLE, \
169166
"A message object could not be deserialized successfully in the target " \
170167
"vm.Context") \
171-
V(ERR_MISSING_TRANSFERABLE_IN_TRANSFER_LIST, \
172-
"Object that needs transfer was found in message but not listed " \
173-
"in transferList") \
174168
V(ERR_MISSING_PLATFORM_FOR_WORKER, \
175169
"The V8 platform used by this instance of Node does not support " \
176170
"creating Workers") \

src/node_messaging.cc

+2-2
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ class SerializerDelegate : public ValueSerializer::Delegate {
406406
}
407407

408408
if (mode == BaseObject::TransferMode::kTransferable) {
409-
THROW_ERR_MISSING_TRANSFERABLE_IN_TRANSFER_LIST(env_);
409+
ThrowDataCloneError(env_->clone_transfer_needed_str());
410410
return Nothing<bool>();
411411
}
412412

@@ -531,7 +531,7 @@ Maybe<bool> Message::Serialize(Environment* env,
531531
}
532532
}
533533

534-
THROW_ERR_INVALID_TRANSFER_OBJECT(env);
534+
ThrowDataCloneException(context, env->clone_untransferable_str());
535535
return Nothing<bool>();
536536
}
537537
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)