All files / lib/internal/process signal.js

94.44% Statements 51/54
90.9% Branches 10/11
100% Functions 3/3
94.44% Lines 51/54

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 52 53 54 5521x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 16520x 16520x 16520x 21x 21x 16520x 16520x 163x 163x 163x 163x 163x 163x 163x 163x 163x 163x 163x       163x 163x 163x 16520x 21x 631x 631x 631x 38x 38x 38x 631x 21x 21x 21x 21x 21x  
'use strict';
 
const {
  FunctionPrototypeBind,
  SafeMap,
} = primordials;
 
const {
  errnoException,
} = require('internal/errors');
 
const { signals } = internalBinding('constants').os;
 
let Signal;
const signalWraps = new SafeMap();
 
function isSignal(event) {
  return typeof event === 'string' && signals[event] !== undefined;
}
 
// Detect presence of a listener for the special signal types
function startListeningIfSignal(type) {
  if (isSignal(type) && !signalWraps.has(type)) {
    if (Signal === undefined)
      Signal = internalBinding('signal_wrap').Signal;
    const wrap = new Signal();
 
    wrap.unref();
 
    wrap.onsignal = FunctionPrototypeBind(process.emit, process, type, type);
 
    const signum = signals[type];
    const err = wrap.start(signum);
    if (err) {
      wrap.close();
      throw errnoException(err, 'uv_signal_start');
    }
 
    signalWraps.set(type, wrap);
  }
}
 
function stopListeningIfSignal(type) {
  const wrap = signalWraps.get(type);
  if (wrap !== undefined && process.listenerCount(type) === 0) {
    wrap.close();
    signalWraps.delete(type);
  }
}
 
module.exports = {
  startListeningIfSignal,
  stopListeningIfSignal
};