MessagePort 类
版本历史
版本变更
v14.7.0
This class now inherits from EventTarget rather than from EventEmitter.
v10.5.0
新增于: v10.5.0
Instances of the worker.MessagePort class represent one end of an
asynchronous, two-way communications channel. It can be used to transfer
structured data, memory regions and other MessagePorts between different
Workers.
With the exception of MessagePorts being [EventEmitter][]s rather
than EventTargets, this implementation matches browser MessagePorts.
'close' 事件
新增于: v10.5.0
The 'close' event is emitted once either side of the channel has been
disconnected.
const { MessageChannel } = require('worker_threads');
const { port1, port2 } = new MessageChannel();
// Prints:
// foobar
// closed!
port2.on('message', (message) => console.log(message));
port2.on('close', () => console.log('closed!'));
port1.postMessage('foobar');
port1.close();
'message' 事件
新增于: v10.5.0
value The transmitted value
The 'message' event is emitted for any incoming message, containing the cloned
input of port.postMessage().
Listeners on this event receive a clone of the value parameter as passed
to postMessage() and no further arguments.
'messageerror' 事件
新增于: v14.5.0, v12.19.0
error An Error object
The 'messageerror' event is emitted when deserializing a message failed.
Currently, this event is emitted when there is an error occurring while
instantiating the posted JS object on the receiving end. Such situations
are rare, but can happen, for instance, when certain Node.js API objects
are received in a vm.Context (where Node.js APIs are currently
unavailable).
port.close()
新增于: v10.5.0
Disables further sending of messages on either side of the connection.
This method can be called when no further communication will happen over this
MessagePort.
The 'close' event is emitted on both MessagePort instances that
are part of the channel.
port.postMessage(value[, transferList])
版本历史
版本变更
v15.14.0
Add 'BlockList' to the list of cloneable types.
v15.9.0
Add 'Histogram' types to the list of cloneable types.
v15.6.0
Added X509Certificate to the list of cloneable types.
v15.0.0
Added CryptoKey to the list of cloneable types.
v14.5.0, v12.19.0
Added KeyObject to the list of cloneable types.
v14.5.0, v12.19.0
Added FileHandle to the list of transferable types.
v10.5.0
新增于: v10.5.0
value
transferList
Sends a JavaScript value to the receiving side of this channel.
value is transferred in a way which is compatible with
the HTML structured clone algorithm.
In particular, the significant differences to JSON are:
value may contain circular references.
value may contain instances of builtin JS types such as RegExps,
BigInts, Maps, Sets, etc.
value may contain typed arrays, both using ArrayBuffers
and SharedArrayBuffers.
value may contain WebAssembly.Module instances.
const { MessageChannel } = require('worker_threads');
const { port1, port2 } = new MessageChannel();
port1.on('message', (message) => console.log(message));
const circularData = {};
circularData.foo = circularData;
// Prints: { foo: [Circular] }
port2.postMessage(circularData);
transferList may be a list of ArrayBuffer, MessagePort and
FileHandle objects.
After transferring, they are not usable on the sending side of the channel
anymore (even if they are not contained in value). Unlike with
child processes, transferring handles such as network sockets is currently
not supported.
If value contains SharedArrayBuffer instances, those are accessible
from either thread. They cannot be listed in transferList.
value may still contain ArrayBuffer instances that are not in
transferList; in that case, the underlying memory is copied rather than moved.
const { MessageChannel } = require('worker_threads');
const { port1, port2 } = new MessageChannel();
port1.on('message', (message) => console.log(message));
const uint8Array = new Uint8Array([ 1, 2, 3, 4 ]);
// This posts a copy of `uint8Array`:
port2.postMessage(uint8Array);
// This does not copy data, but renders `uint8Array` unusable:
port2.postMessage(uint8Array, [ uint8Array.buffer ]);
// The memory for the `sharedUint8Array` is accessible from both the
// original and the copy received by `.on('message')`:
const sharedUint8Array = new Uint8Array(new SharedArrayBuffer(4));
port2.postMessage(sharedUint8Array);
// This transfers a freshly created message port to the receiver.
// This can be used, for example, to create communication channels between
// multiple `Worker` threads that are children of the same parent thread.
const otherChannel = new MessageChannel();
port2.postMessage({ port: otherChannel.port1 }, [ otherChannel.port1 ]);
The message object is cloned immediately, and can be modified after
posting without having side effects.
For more information on the serialization and deserialization mechanisms
behind this API, see the serialization API of the v8 module.
传输 TypedArray 和 Buffer 时的注意事项
All TypedArray and Buffer instances are views over an underlying
ArrayBuffer. That is, it is the ArrayBuffer that actually stores
the raw data while the TypedArray and Buffer objects provide a
way of viewing and manipulating the data. It is possible and common
for multiple views to be created over the same ArrayBuffer instance.
Great care must be taken when using a transfer list to transfer an
ArrayBuffer as doing so causes all TypedArray and Buffer
instances that share that same ArrayBuffer to become unusable.
const ab = new ArrayBuffer(10);
const u1 = new Uint8Array(ab);
const u2 = new Uint16Array(ab);
console.log(u2.length); // prints 5
port.postMessage(u1, [u1.buffer]);
console.log(u2.length); // prints 0
For Buffer instances, specifically, whether the underlying
ArrayBuffer can be transferred or cloned depends entirely on how
instances were created, which often cannot be reliably determined.
An ArrayBuffer can be marked with markAsUntransferable() to indicate
that it should always be cloned and never transferred.
Depending on how a Buffer instance was created, it may or may
not own its underlying ArrayBuffer. An ArrayBuffer must not
be transferred unless it is known that the Buffer instance
owns it. In particular, for Buffers created from the internal
Buffer pool (using, for instance Buffer.from() or Buffer.allocUnsafe()),
transferring them is not possible and they are always cloned,
which sends a copy of the entire Buffer pool.
This behavior may come with unintended higher memory
usage and possible security concerns.
See Buffer.allocUnsafe() for more details on Buffer pooling.
The ArrayBuffers for Buffer instances created using
Buffer.alloc() or Buffer.allocUnsafeSlow() can always be
transferred but doing so renders all other existing views of
those ArrayBuffers unusable.
使用原型、类和访问器克隆对象时的注意事项
Because object cloning uses the HTML structured clone algorithm,
non-enumerable properties, property accessors, and object prototypes are
not preserved. In particular, Buffer objects will be read as
plain Uint8Arrays on the receiving side, and instances of JavaScript
classes will be cloned as plain JavaScript objects.
const b = Symbol('b');
class Foo{
#a = 1;
constructor() {
this[b] = 2;
this.c = 3;
}
get d() { return 4; }
}
const { port1, port2 } = new MessageChannel();
port1.onmessage = ({ data }) => console.log(data);
port2.postMessage(new Foo());
// Prints: { c: 3 }
This limitation extends to many built-in objects, such as the global URL
object:
const { port1, port2 } = new MessageChannel();
port1.onmessage = ({ data }) => console.log(data);
port2.postMessage(new URL('https://example.org'));
// Prints: { }
port.ref()
新增于: v10.5.0
Opposite of unref(). Calling ref() on a previously unref()ed port does
not let the program exit if it's the only active handle left (the default
behavior). If the port is ref()ed, calling ref() again has no effect.
If listeners are attached or removed using .on('message'), the port
is ref()ed and unref()ed automatically depending on whether
listeners for the event exist.
port.start()
新增于: v10.5.0
Starts receiving messages on this MessagePort. When using this port
as an event emitter, this is called automatically once 'message'
listeners are attached.
This method exists for parity with the Web MessagePort API. In Node.js,
it is only useful for ignoring messages when no event listener is present.
Node.js also diverges in its handling of .onmessage. Setting it
automatically calls .start(), but unsetting it lets messages queue up
until a new handler is set or the port is discarded.
port.unref()
新增于: v10.5.0
Calling unref() on a port allows the thread to exit if this is the only
active handle in the event system. If the port is already unref()ed calling
unref() again has no effect.
If listeners are attached or removed using .on('message'), the port is
ref()ed and unref()ed automatically depending on whether
listeners for the event exist.