
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
message-gate
Advanced tools
A lightweight library for seamless communication between threads, workers, and isolated contexts in JavaScript and TypeScript.
npm install message-gate
import MessageGate from "message-gate";
const [ gateA, gateB ] = MessageGate.createChannels();
import MessageGate from "message-gate";
const worker = new Worker("WORKER_URL", { type: "module" });
const { gate, port } = MessageGate.createChannelAndPort();
worker.postMessage(port, port);
import MessageGate from "message-gate";
self.onmessage = (port) => {
self.gate = MessageGate.createFromPort(port);
self.onmessage = undefined;
}
MessageGate.createChannels(handlersA?, handlersB?)
Creates two interconnected channels.
const [gateA, gateB] = MessageGate.createChannels(
{ 'event-a': handlerA },
{ 'event-b': handlerB }
);
MessageGate.createChannelAndPort(handlers?)
Creates a gate and returns both the gate and its port.
const { gate, port } = MessageGate.createChannelAndPort(handlers);
MessageGate.createFromPort(port, handlers?)
Creates a gate from an existing MessagePort.
const gate = MessageGate.createFromPort(messagePort, {
'custom-event': (data) => { /* handler */ }
});
MessageGate.createPorts()
Creates a pair of connected MessagePorts.
const [port1, port2] = MessageGate.createPorts();
on(action: string, handler: Function)
gate.on('data-received', (data) => {
console.log('Data:', data);
});
off(action: string)
gate.off('data-received');
send(action: string, data: any)
gate.send('update-data', { value: 42 });
post(action: string, data: any): Promise<any>
const result = await gate.post('fetch-data', { query: 'test' });
sendTransfer(action: string, data: any, transfer: Transferable[])
const buffer = new ArrayBuffer(1024);
gate.sendTransfer('transfer-data', { buffer }, [buffer]);
postTransfer(action: string, data: any, transfer: Transferable[]): Promise<any>
const result = await gate.postTransfer('process-buffer', { buffer }, [buffer]);
close()
gate.close();
sidePort: MessagePort | undefined
Get the opposite port of the channel if gate was created with new MessageGate().
const oppositePort = gate.sidePort;
// Send large data efficiently
const largeBuffer = new ArrayBuffer(1024 * 1024);
gate.sendTransfer('large-data', { buffer: largeBuffer }, [largeBuffer]);
// Receive and process transferables
gate.on('large-data', (data) => {
// data.buffer is now transferred, not copied
processBuffer(data.buffer);
});
Contributions are welcome! Please feel free to submit a Pull Request.
MIT License - feel free to use in commercial projects.
For bugs and feature requests, please create an issue on GitHub.
FAQs
Seamless communication between threads, workers and isolated contexts
We found that message-gate demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.