All files / lib/internal/process signal.js

94.34% Statements 50/53
90.91% Branches 10/11
100% Functions 3/3
94.34% Lines 50/53

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 5418x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 14712x 14712x 14712x 18x 18x 14712x 14712x 86x 86x 86x 86x 86x 86x 86x 86x 86x 86x 86x       86x 86x 86x 14712x 18x 524x 524x 524x 20x 20x 20x 524x 18x 18x 18x 18x 18x  
'use strict';
 
const {
  Map,
} = primordials;
 
const {
  errnoException,
} = require('internal/errors');
 
const { signals } = internalBinding('constants').os;
 
let Signal;
const signalWraps = new Map();
 
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 = process.emit.bind(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
};