All files / lib/internal/cluster utils.js

100% Statements 51/51
100% Branches 16/16
100% Functions 3/3
100% Lines 51/51

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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