pax_global_header00006660000000000000000000000064140164407200014510gustar00rootroot0000000000000052 comment=06b9fa48ef2c8245b42c558136d75e52ef833022 exit-hook-2.2.1/000077500000000000000000000000001401644072000134215ustar00rootroot00000000000000exit-hook-2.2.1/.editorconfig000066400000000000000000000002571401644072000161020ustar00rootroot00000000000000root = true [*] indent_style = tab end_of_line = lf charset = utf-8 trim_trailing_whitespace = true insert_final_newline = true [*.yml] indent_style = space indent_size = 2 exit-hook-2.2.1/.gitattributes000066400000000000000000000000231401644072000163070ustar00rootroot00000000000000* text=auto eol=lf exit-hook-2.2.1/.github/000077500000000000000000000000001401644072000147615ustar00rootroot00000000000000exit-hook-2.2.1/.github/funding.yml000066400000000000000000000001631401644072000171360ustar00rootroot00000000000000github: sindresorhus open_collective: sindresorhus tidelift: npm/exit-hook custom: https://sindresorhus.com/donate exit-hook-2.2.1/.github/security.md000066400000000000000000000002631401644072000171530ustar00rootroot00000000000000# Security Policy To report a security vulnerability, please use the [Tidelift security contact](https://tidelift.com/security). Tidelift will coordinate the fix and disclosure. exit-hook-2.2.1/.github/workflows/000077500000000000000000000000001401644072000170165ustar00rootroot00000000000000exit-hook-2.2.1/.github/workflows/main.yml000066400000000000000000000007201401644072000204640ustar00rootroot00000000000000name: CI on: - push - pull_request jobs: test: name: Node.js ${{ matrix.node-version }} runs-on: ubuntu-latest strategy: fail-fast: false matrix: node-version: - 14 - 12 - 10 - 8 - 6 steps: - uses: actions/checkout@v2 - uses: actions/setup-node@v1 with: node-version: ${{ matrix.node-version }} - run: npm install - run: npm test exit-hook-2.2.1/.gitignore000066400000000000000000000000271401644072000154100ustar00rootroot00000000000000node_modules yarn.lock exit-hook-2.2.1/.npmrc000066400000000000000000000000231401644072000145340ustar00rootroot00000000000000package-lock=false exit-hook-2.2.1/fixture.js000066400000000000000000000004221401644072000154430ustar00rootroot00000000000000'use strict'; const exitHook = require('.'); exitHook(() => { console.log('foo'); }); exitHook(() => { console.log('bar'); }); const unsubscribe = exitHook(() => { console.log('baz'); }); unsubscribe(); process.exit(); // eslint-disable-line unicorn/no-process-exit exit-hook-2.2.1/index.d.ts000066400000000000000000000013301401644072000153170ustar00rootroot00000000000000/** Run some code when the process exits. The `process.on('exit')` event doesn't catch all the ways a process can exit. This package is useful for cleaning up before exiting. @param callback - The callback to execute when the process exits. @returns A function that removes the hook when called. @example ``` import exitHook = require('exit-hook'); exitHook(() => { console.log('Exiting'); }); // You can add multiple hooks, even across files exitHook(() => { console.log('Exiting 2'); }); throw new Error('🦄'); //=> 'Exiting' //=> 'Exiting 2' // Removing an exit hook: const unsubscribe = exitHook(() => {}); unsubscribe(); ``` */ declare function exitHook(callback: () => void): () => void; export = exitHook; exit-hook-2.2.1/index.js000066400000000000000000000017561401644072000150770ustar00rootroot00000000000000'use strict'; const callbacks = new Set(); let isCalled = false; let isRegistered = false; function exit(exit, signal) { if (isCalled) { return; } isCalled = true; for (const callback of callbacks) { callback(); } if (exit === true) { process.exit(128 + signal); // eslint-disable-line unicorn/no-process-exit } } module.exports = callback => { callbacks.add(callback); if (!isRegistered) { isRegistered = true; process.once('exit', exit); process.once('SIGINT', exit.bind(null, true, 2)); process.once('SIGTERM', exit.bind(null, true, 15)); // PM2 Cluster shutdown message. Caught to support async handlers with pm2, needed because // explicitly calling process.exit() doesn't trigger the beforeExit event, and the exit // event cannot support async handlers, since the event loop is never called after it. process.on('message', message => { if (message === 'shutdown') { exit(true, -128); } }); } return () => { callbacks.delete(callback); }; }; exit-hook-2.2.1/index.test-d.ts000066400000000000000000000002471401644072000163020ustar00rootroot00000000000000import {expectType} from 'tsd'; import exitHook = require('./index.js'); const unsubscribe = exitHook(() => {}); expectType<() => void>(unsubscribe); unsubscribe(); exit-hook-2.2.1/license000066400000000000000000000021351401644072000147670ustar00rootroot00000000000000MIT License Copyright (c) Sindre Sorhus (https://sindresorhus.com) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. exit-hook-2.2.1/package.json000066400000000000000000000013551401644072000157130ustar00rootroot00000000000000{ "name": "exit-hook", "version": "2.2.1", "description": "Run some code when the process exits", "license": "MIT", "repository": "sindresorhus/exit-hook", "funding": "https://github.com/sponsors/sindresorhus", "author": { "name": "Sindre Sorhus", "email": "sindresorhus@gmail.com", "url": "https://sindresorhus.com" }, "engines": { "node": ">=6" }, "scripts": { "test": "xo && ava && tsd" }, "files": [ "index.js", "index.d.ts" ], "keywords": [ "exit", "quit", "process", "hook", "graceful", "handler", "shutdown", "sigterm", "sigint", "terminate", "kill", "stop", "event", "signal" ], "devDependencies": { "ava": "^1.4.1", "execa": "^1.0.0", "tsd": "^0.7.2", "xo": "^0.24.0" } } exit-hook-2.2.1/readme.md000066400000000000000000000023201401644072000151750ustar00rootroot00000000000000# exit-hook > Run some code when the process exits The `process.on('exit')` event doesn't catch all the ways a process can exit. This package is useful for cleaning up before exiting. ## Install ``` $ npm install exit-hook ``` ## Usage ```js const exitHook = require('exit-hook'); exitHook(() => { console.log('Exiting'); }); // You can add multiple hooks, even across files exitHook(() => { console.log('Exiting 2'); }); throw new Error('🦄'); //=> 'Exiting' //=> 'Exiting 2' ``` Removing an exit hook: ```js const exitHook = require('exit-hook'); const unsubscribe = exitHook(() => {}); unsubscribe(); ``` ## API ### exitHook(callback) Returns a function that removes the hook when called. #### callback Type: `Function` The callback to execute when the process exits. ---
Get professional support for this package with a Tidelift subscription
Tidelift helps make open source sustainable for maintainers while giving companies
assurances about security, maintenance, and licensing for their dependencies.
exit-hook-2.2.1/test.js000066400000000000000000000012531401644072000147370ustar00rootroot00000000000000import test from 'ava'; import execa from 'execa'; import exitHook from '.'; test('main', async t => { const {stdout} = await execa(process.execPath, ['fixture.js']); t.is(stdout, 'foo\nbar'); }); test('listener count', t => { t.is(process.listenerCount('exit'), 0); const unsubscribe1 = exitHook(() => {}); const unsubscribe2 = exitHook(() => {}); t.is(process.listenerCount('exit'), 1); // Remove all listeners unsubscribe1(); unsubscribe2(); t.is(process.listenerCount('exit'), 1); // Re-add listener const unsubscribe3 = exitHook(() => {}); t.is(process.listenerCount('exit'), 1); // Remove again unsubscribe3(); t.is(process.listenerCount('exit'), 1); });