UNPKG

@socketsecurity/lib

Version:

Core utilities and infrastructure for Socket.dev security tools

251 lines (250 loc) 7.12 kB
"use strict"; /* Socket Lib - Built with esbuild */ var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var signal_exit_exports = {}; __export(signal_exit_exports, { load: () => load, onExit: () => onExit, signals: () => signals, unload: () => unload }); module.exports = __toCommonJS(signal_exit_exports); const ReflectApply = Reflect.apply; const globalProcess = globalThis.process; const originalProcessEmit = globalProcess?.emit; const platform = globalProcess?.platform ?? ""; const originalProcessReallyExit = globalProcess?.reallyExit; const WIN32 = platform === "win32"; let _events; // @__NO_SIDE_EFFECTS__ function getEvents() { if (_events === void 0) { _events = require("node:events"); } return _events; } let _emitter; // @__NO_SIDE_EFFECTS__ function getEmitter() { if (_emitter === void 0) { if (globalProcess?.__signal_exit_emitter__) { _emitter = globalProcess.__signal_exit_emitter__; } else if (globalProcess) { const EventEmitter = (/* @__PURE__ */ getEvents()).EventEmitter; _emitter = globalProcess.__signal_exit_emitter__ = new EventEmitter(); _emitter.count = 0; _emitter.emitted = { __proto__: null }; } if (_emitter && !_emitter.infinite) { _emitter.setMaxListeners(Number.POSITIVE_INFINITY); _emitter.infinite = true; } } return _emitter; } let _sigListeners; // @__NO_SIDE_EFFECTS__ function getSignalListeners() { if (_sigListeners === void 0) { _sigListeners = { __proto__: null }; const emitter = /* @__PURE__ */ getEmitter(); const sigs = /* @__PURE__ */ getSignals(); for (const sig of sigs) { _sigListeners[sig] = function listener() { const listeners = globalProcess?.listeners(sig) || []; if (listeners.length === emitter.count) { /* @__PURE__ */ unload(); /* @__PURE__ */ emit("exit", null, sig); /* @__PURE__ */ emit("afterexit", null, sig); const killSig = WIN32 && sig === "SIGHUP" ? "SIGINT" : sig; globalProcess?.kill(globalProcess?.pid, killSig); } }; } } return _sigListeners; } let _signals; // @__NO_SIDE_EFFECTS__ function getSignals() { if (_signals === void 0) { _signals = ["SIGABRT", "SIGALRM", "SIGHUP", "SIGINT", "SIGTERM"]; if (!WIN32) { _signals.push( "SIGVTALRM", "SIGXCPU", "SIGXFSZ", "SIGUSR2", "SIGTRAP", "SIGSYS", "SIGQUIT", "SIGIOT" // should detect profiler and enable/disable accordingly. // see #21 // 'SIGPROF' ); } if (platform === "linux") { _signals.push("SIGIO", "SIGPOLL", "SIGPWR", "SIGSTKFLT", "SIGUNUSED"); } } return _signals; } // @__NO_SIDE_EFFECTS__ function emit(event, code, signal) { const emitter = /* @__PURE__ */ getEmitter(); if (emitter.emitted?.[event]) { return; } if (emitter.emitted) { emitter.emitted[event] = true; } emitter.emit(event, code, signal); } let loaded = false; // @__NO_SIDE_EFFECTS__ function load() { if (loaded || !globalProcess) { return; } loaded = true; const emitter = /* @__PURE__ */ getEmitter(); if (emitter.count !== void 0) { emitter.count += 1; } const sigs = /* @__PURE__ */ getSignals(); const sigListeners = /* @__PURE__ */ getSignalListeners(); _signals = sigs.filter((sig) => { try { globalProcess.on( sig, sigListeners[sig] ); return true; } catch { } return false; }); globalProcess.emit = processEmit; globalProcess.reallyExit = processReallyExit; } // @__NO_SIDE_EFFECTS__ function processEmit(eventName, exitCode, ...args) { if (eventName === "exit") { let actualExitCode = exitCode; if (actualExitCode === void 0) { const processExitCode = globalProcess?.exitCode; actualExitCode = typeof processExitCode === "number" ? processExitCode : void 0; } else if (globalProcess) { globalProcess.exitCode = actualExitCode; } const result = ReflectApply( originalProcessEmit, this, [eventName, actualExitCode, ...args] ); const numExitCode = typeof actualExitCode === "number" ? actualExitCode : null; /* @__PURE__ */ emit("exit", numExitCode, null); /* @__PURE__ */ emit("afterexit", numExitCode, null); return result; } return ReflectApply( originalProcessEmit, this, [eventName, exitCode, ...args] ); } // @__NO_SIDE_EFFECTS__ function processReallyExit(code) { const exitCode = code || 0; if (globalProcess) { globalProcess.exitCode = exitCode; } /* @__PURE__ */ emit("exit", exitCode, null); /* @__PURE__ */ emit("afterexit", exitCode, null); ReflectApply( originalProcessReallyExit, globalProcess, [exitCode] ); throw new Error("processReallyExit should never return"); } // @__NO_SIDE_EFFECTS__ function onExit(cb, options) { if (!globalProcess) { return function remove() { }; } if (typeof cb !== "function") { throw new TypeError("a callback must be provided for exit handler"); } if (loaded === false) { /* @__PURE__ */ load(); } const { alwaysLast } = { __proto__: null, ...options }; let eventName = "exit"; if (alwaysLast) { eventName = "afterexit"; } const emitter = /* @__PURE__ */ getEmitter(); emitter.on(eventName, cb); return function remove() { emitter.removeListener(eventName, cb); if (!emitter.listeners("exit").length && !emitter.listeners("afterexit").length) { /* @__PURE__ */ unload(); } }; } // @__NO_SIDE_EFFECTS__ function signals() { return _signals; } // @__NO_SIDE_EFFECTS__ function unload() { if (!loaded || !globalProcess) { return; } loaded = false; const sigs = /* @__PURE__ */ getSignals(); const sigListeners = /* @__PURE__ */ getSignalListeners(); for (const sig of sigs) { try { globalProcess.removeListener( sig, sigListeners[sig] ); } catch { } } globalProcess.emit = originalProcessEmit; if (originalProcessReallyExit !== void 0) { globalProcess.reallyExit = originalProcessReallyExit; } const emitter = /* @__PURE__ */ getEmitter(); if (emitter.count !== void 0) { emitter.count -= 1; } } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { load, onExit, signals, unload });