CoolFace
Datasetpublic

basant307/AI_Governance_Project

sourceHugging Faceapache-2.0updated 2mo agoView on Hugging Face
0likes48downloads
index.js203 linesDownload Raw Back to signal-exit
1// Note: since nyc uses this module to output coverage, any lines2// that are in the direct sync flow of nyc's outputCoverage are3// ignored, since we can never get coverage for them.4// grab a reference to node's real process object right away5var process = global.process6 7const processOk = function (process) {8  return process &&9    typeof process === 'object' &&10    typeof process.removeListener === 'function' &&11    typeof process.emit === 'function' &&12    typeof process.reallyExit === 'function' &&13    typeof process.listeners === 'function' &&14    typeof process.kill === 'function' &&15    typeof process.pid === 'number' &&16    typeof process.on === 'function'17}18 19// some kind of non-node environment, just no-op20/* istanbul ignore if */21if (!processOk(process)) {22  module.exports = function () {23    return function () {}24  }25} else {26  var assert = require('assert')27  var signals = require('./signals.js')28  var isWin = /^win/i.test(process.platform)29 30  var EE = require('events')31  /* istanbul ignore if */32  if (typeof EE !== 'function') {33    EE = EE.EventEmitter34  }35 36  var emitter37  if (process.__signal_exit_emitter__) {38    emitter = process.__signal_exit_emitter__39  } else {40    emitter = process.__signal_exit_emitter__ = new EE()41    emitter.count = 042    emitter.emitted = {}43  }44 45  // Because this emitter is a global, we have to check to see if a46  // previous version of this library failed to enable infinite listeners.47  // I know what you're about to say.  But literally everything about48  // signal-exit is a compromise with evil.  Get used to it.49  if (!emitter.infinite) {50    emitter.setMaxListeners(Infinity)51    emitter.infinite = true52  }53 54  module.exports = function (cb, opts) {55    /* istanbul ignore if */56    if (!processOk(global.process)) {57      return function () {}58    }59    assert.equal(typeof cb, 'function', 'a callback must be provided for exit handler')60 61    if (loaded === false) {62      load()63    }64 65    var ev = 'exit'66    if (opts && opts.alwaysLast) {67      ev = 'afterexit'68    }69 70    var remove = function () {71      emitter.removeListener(ev, cb)72      if (emitter.listeners('exit').length === 0 &&73          emitter.listeners('afterexit').length === 0) {74        unload()75      }76    }77    emitter.on(ev, cb)78 79    return remove80  }81 82  var unload = function unload () {83    if (!loaded || !processOk(global.process)) {84      return85    }86    loaded = false87 88    signals.forEach(function (sig) {89      try {90        process.removeListener(sig, sigListeners[sig])91      } catch (er) {}92    })93    process.emit = originalProcessEmit94    process.reallyExit = originalProcessReallyExit95    emitter.count -= 196  }97  module.exports.unload = unload98 99  var emit = function emit (event, code, signal) {100    /* istanbul ignore if */101    if (emitter.emitted[event]) {102      return103    }104    emitter.emitted[event] = true105    emitter.emit(event, code, signal)106  }107 108  // { <signal>: <listener fn>, ... }109  var sigListeners = {}110  signals.forEach(function (sig) {111    sigListeners[sig] = function listener () {112      /* istanbul ignore if */113      if (!processOk(global.process)) {114        return115      }116      // If there are no other listeners, an exit is coming!117      // Simplest way: remove us and then re-send the signal.118      // We know that this will kill the process, so we can119      // safely emit now.120      var listeners = process.listeners(sig)121      if (listeners.length === emitter.count) {122        unload()123        emit('exit', null, sig)124        /* istanbul ignore next */125        emit('afterexit', null, sig)126        /* istanbul ignore next */127        if (isWin && sig === 'SIGHUP') {128          // "SIGHUP" throws an `ENOSYS` error on Windows,129          // so use a supported signal instead130          sig = 'SIGINT'131        }132        /* istanbul ignore next */133        process.kill(process.pid, sig)134      }135    }136  })137 138  module.exports.signals = function () {139    return signals140  }141 142  var loaded = false143 144  var load = function load () {145    if (loaded || !processOk(global.process)) {146      return147    }148    loaded = true149 150    // This is the number of onSignalExit's that are in play.151    // It's important so that we can count the correct number of152    // listeners on signals, and don't wait for the other one to153    // handle it instead of us.154    emitter.count += 1155 156    signals = signals.filter(function (sig) {157      try {158        process.on(sig, sigListeners[sig])159        return true160      } catch (er) {161        return false162      }163    })164 165    process.emit = processEmit166    process.reallyExit = processReallyExit167  }168  module.exports.load = load169 170  var originalProcessReallyExit = process.reallyExit171  var processReallyExit = function processReallyExit (code) {172    /* istanbul ignore if */173    if (!processOk(global.process)) {174      return175    }176    process.exitCode = code || /* istanbul ignore next */ 0177    emit('exit', process.exitCode, null)178    /* istanbul ignore next */179    emit('afterexit', process.exitCode, null)180    /* istanbul ignore next */181    originalProcessReallyExit.call(process, process.exitCode)182  }183 184  var originalProcessEmit = process.emit185  var processEmit = function processEmit (ev, arg) {186    if (ev === 'exit' && processOk(global.process)) {187      /* istanbul ignore else */188      if (arg !== undefined) {189        process.exitCode = arg190      }191      var ret = originalProcessEmit.apply(this, arguments)192      /* istanbul ignore next */193      emit('exit', process.exitCode, null)194      /* istanbul ignore next */195      emit('afterexit', process.exitCode, null)196      /* istanbul ignore next */197      return ret198    } else {199      return originalProcessEmit.apply(this, arguments)200    }201  }202}203 
basant307/AI_Governance_Project · CoolFace