• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3const {
4  ReflectApply,
5  SafeMap,
6} = primordials;
7
8module.exports = {
9  sendHelper,
10  internal
11};
12
13const callbacks = new SafeMap();
14let seq = 0;
15
16function sendHelper(proc, message, handle, cb) {
17  if (!proc.connected)
18    return false;
19
20  // Mark message as internal. See INTERNAL_PREFIX in lib/child_process.js
21  message = { cmd: 'NODE_CLUSTER', ...message, seq };
22
23  if (typeof cb === 'function')
24    callbacks.set(seq, cb);
25
26  seq += 1;
27  return proc.send(message, handle);
28}
29
30// Returns an internalMessage listener that hands off normal messages
31// to the callback but intercepts and redirects ACK messages.
32function internal(worker, cb) {
33  return function onInternalMessage(message, handle) {
34    if (message.cmd !== 'NODE_CLUSTER')
35      return;
36
37    let fn = cb;
38
39    if (message.ack !== undefined) {
40      const callback = callbacks.get(message.ack);
41
42      if (callback !== undefined) {
43        fn = callback;
44        callbacks.delete(message.ack);
45      }
46    }
47
48    ReflectApply(fn, worker, arguments);
49  };
50}
51