0
0
mirror of https://github.com/nodejs/node.git synced 2024-11-21 21:19:50 +01:00
nodejs/test/parallel/test-worker-message-port-transfer-native.js
Chengzhong Wu 64549731b6
src: throw DataCloneError on transfering untransferable objects
The HTML StructuredSerializeWithTransfer algorithm defines that when
an untransferable object is in the transfer list, a DataCloneError is
thrown.
An array buffer that is already transferred is also considered as
untransferable.

PR-URL: https://github.com/nodejs/node/pull/47604
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
2023-05-05 11:22:42 +00:00

38 lines
972 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 clone object of unsupported type\.$/
});
port1.close();
}