Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

worker: use DataCloneError for unknown native objects #28025

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions doc/api/errors.md
Original file line number Diff line number Diff line change
Expand Up @@ -671,12 +671,6 @@ An operation outside the bounds of a `Buffer` was attempted.
An attempt has been made to create a `Buffer` larger than the maximum allowed
size.

<a id="ERR_CANNOT_TRANSFER_OBJECT"></a>
### ERR_CANNOT_TRANSFER_OBJECT

The value passed to `postMessage()` contained an object that is not supported
for transferring.

<a id="ERR_CANNOT_WATCH_SIGINT"></a>
### ERR_CANNOT_WATCH_SIGINT

Expand Down Expand Up @@ -2013,6 +2007,16 @@ A module file could not be resolved while attempting a [`require()`][] or
> Stability: 0 - Deprecated. These error codes are either inconsistent, or have
> been removed.

<a id="ERR_CANNOT_TRANSFER_OBJECT"></a>
### ERR_CANNOT_TRANSFER_OBJECT
<!--
added: v10.5.0
removed: REPLACEME
-->

The value passed to `postMessage()` contained an object that is not supported
for transferring.

<a id="ERR_CLOSED_MESSAGE_PORT"></a>
### ERR_CLOSED_MESSAGE_PORT
<!-- YAML
Expand Down
1 change: 1 addition & 0 deletions src/env.h
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ constexpr size_t kFsStatsBufferLength = kFsStatsFieldsNumber * 2;
V(change_string, "change") \
V(channel_string, "channel") \
V(chunks_sent_since_last_write_string, "chunksSentSinceLastWrite") \
V(clone_unsupported_type_str, "Cannot transfer object of unsupported type.") \
targos marked this conversation as resolved.
Show resolved Hide resolved
V(code_string, "code") \
V(commonjs_string, "commonjs") \
V(config_string, "config") \
Expand Down
2 changes: 0 additions & 2 deletions src/node_errors.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ void FatalException(v8::Isolate* isolate,
V(ERR_BUFFER_CONTEXT_NOT_AVAILABLE, Error) \
V(ERR_BUFFER_OUT_OF_BOUNDS, RangeError) \
V(ERR_BUFFER_TOO_LARGE, Error) \
V(ERR_CANNOT_TRANSFER_OBJECT, TypeError) \
V(ERR_CONSTRUCT_CALL_REQUIRED, Error) \
V(ERR_INVALID_ARG_VALUE, TypeError) \
V(ERR_INVALID_ARG_TYPE, TypeError) \
Expand Down Expand Up @@ -100,7 +99,6 @@ void FatalException(v8::Isolate* isolate,
#define PREDEFINED_ERROR_MESSAGES(V) \
V(ERR_BUFFER_CONTEXT_NOT_AVAILABLE, \
"Buffer is not available for the current Context") \
V(ERR_CANNOT_TRANSFER_OBJECT, "Cannot transfer object of unsupported type")\
V(ERR_CONSTRUCT_CALL_REQUIRED, "Cannot call constructor without `new`") \
V(ERR_INVALID_TRANSFER_OBJECT, "Found invalid object in transferList") \
V(ERR_MEMORY_ALLOCATION_FAILED, "Failed to allocate memory") \
Expand Down
2 changes: 1 addition & 1 deletion src/node_messaging.cc
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ class SerializerDelegate : public ValueSerializer::Delegate {
return WriteMessagePort(Unwrap<MessagePort>(object));
}

THROW_ERR_CANNOT_TRANSFER_OBJECT(env_);
ThrowDataCloneError(env_->clone_unsupported_type_str());
return Nothing<bool>();
}

Expand Down
37 changes: 37 additions & 0 deletions test/parallel/test-worker-message-port-transfer-native.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Flags: --expose-internals
'use strict';
const common = require('../common');
const assert = require('assert');
const { MessageChannel } = require('worker_threads');
const { internalBinding } = require('internal/test/binding');

// Test that passing native objects and functions to .postMessage() throws
// DataCloneError exceptions.

{
const { port1, port2 } = new MessageChannel();
port2.once('message', common.mustNotCall());

assert.throws(() => {
port1.postMessage(function foo() {});
}, {
name: 'DataCloneError',
message: /function foo\(\) \{\} could not be cloned\.$/
});
port1.close();
}

{
const { port1, port2 } = new MessageChannel();
port2.once('message', common.mustNotCall());

const nativeObject = new (internalBinding('js_stream').JSStream)();

assert.throws(() => {
port1.postMessage(nativeObject);
}, {
name: 'DataCloneError',
message: /Cannot transfer object of unsupported type\.$/
});
port1.close();
}