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 21 // in lib/internal/child_process.js 22 message = { cmd: 'NODE_CLUSTER', ...message, seq }; 23 24 if (typeof cb === 'function') 25 callbacks.set(seq, cb); 26 27 seq += 1; 28 return proc.send(message, handle); 29} 30 31// Returns an internalMessage listener that hands off normal messages 32// to the callback but intercepts and redirects ACK messages. 33function internal(worker, cb) { 34 return function onInternalMessage(message, handle) { 35 if (message.cmd !== 'NODE_CLUSTER') 36 return; 37 38 let fn = cb; 39 40 if (message.ack !== undefined) { 41 const callback = callbacks.get(message.ack); 42 43 if (callback !== undefined) { 44 fn = callback; 45 callbacks.delete(message.ack); 46 } 47 } 48 49 ReflectApply(fn, worker, arguments); 50 }; 51} 52