1'use strict'; 2 3const { 4 Map, 5} = primordials; 6 7const { 8 errnoException, 9} = require('internal/errors'); 10 11const { signals } = internalBinding('constants').os; 12 13let Signal; 14const signalWraps = new Map(); 15 16function isSignal(event) { 17 return typeof event === 'string' && signals[event] !== undefined; 18} 19 20// Detect presence of a listener for the special signal types 21function startListeningIfSignal(type) { 22 if (isSignal(type) && !signalWraps.has(type)) { 23 if (Signal === undefined) 24 Signal = internalBinding('signal_wrap').Signal; 25 const wrap = new Signal(); 26 27 wrap.unref(); 28 29 wrap.onsignal = process.emit.bind(process, type, type); 30 31 const signum = signals[type]; 32 const err = wrap.start(signum); 33 if (err) { 34 wrap.close(); 35 throw errnoException(err, 'uv_signal_start'); 36 } 37 38 signalWraps.set(type, wrap); 39 } 40} 41 42function stopListeningIfSignal(type) { 43 const wrap = signalWraps.get(type); 44 if (wrap !== undefined && process.listenerCount(type) === 0) { 45 wrap.close(); 46 signalWraps.delete(type); 47 } 48} 49 50module.exports = { 51 startListeningIfSignal, 52 stopListeningIfSignal 53}; 54