node/test/parallel/test-worker-message-port-transfer-native.js
Anna Henningsen 7bd2a3fcb4
worker: use DataCloneError for unknown native objects
This aligns the behaviour better with the web.

PR-URL: https://github.com/nodejs/node/pull/28025
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
2019-06-10 15:22:54 +02:00

38 lines
975 B
JavaScript

// 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();
}