{"name":"exit-hook","version":"3.1.3","description":"Run some code when the process exits","license":"MIT","repository":{"type":"git","url":"git+https://github.com/sindresorhus/exit-hook.git"},"funding":"https://github.com/sponsors/sindresorhus","author":{"name":"Sindre Sorhus","email":"sindresorhus@gmail.com","url":"https://sindresorhus.com"},"type":"module","exports":"./index.js","engines":{"node":"^12.20.0 || ^14.13.1 || >=16.0.0"},"scripts":{"test":"xo && ava && tsd"},"keywords":["exit","quit","process","hook","graceful","handler","shutdown","sigterm","sigint","terminate","kill","stop","event","signal","async","asynchronous"],"devDependencies":{"ava":"^3.15.0","execa":"^5.1.1","tsd":"^0.17.0","xo":"^0.44.0"},"types":"./index.d.ts","gitHead":"1779ccf53cab626b878769bd0ee7e048d19debfc","bugs":{"url":"https://github.com/sindresorhus/exit-hook/issues"},"homepage":"https://github.com/sindresorhus/exit-hook#readme","_id":"exit-hook@3.1.3","_nodeVersion":"12.22.1","_npmVersion":"8.19.2","dist":{"integrity":"sha512-FWLhJ1JnPm8rLe4c6GOeHKZ+n7B8kII6MXudoFmUJ9fvqdke8kMf7Y7KxDtyb7A4R4KFNOXr7kuyasxEnMkJWw==","shasum":"38054c9f3976afca5a10d50639223abb0ef9f4bf","tarball":"http://registry-npm.ywwl.com/exit-hook/download/exit-hook-3.1.3.tgz","fileCount":5,"unpackedSize":10932,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQCjLxGspJn8kE/lKeJd3EcW92a7xRhaGKqke3TYreFT4wIgPvmcn0IqiyW95O272+FTLIhFIE/VELA8RT+f4/Xr2Hs="}],"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v4.10.10\r\nComment: https://openpgpjs.org\r\n\r\nwsFzBAEBCAAGBQJjkc9jACEJED1NWxICdlZqFiEECWMYAoorWMhJKdjhPU1b\r\nEgJ2VmqADw//WSdBJcSBIr1EQTjHGctQhDY3BMW4bhWaTaqg/HQZGisDi0aK\r\nKJcMnQ2Q9MFwf8gYVuVmxlgo5yH0jip66raA0KI+4nO3uVvKVdyo2cdSCo0/\r\njy3P31CmH1lkQaYNryKf1PrQd/9hnnQ83SV7oe3O3DhFhIEdl2bfuPHncj1H\r\nEyGtYk95FLvWt0b/gAXRH5av/kx6I86rqr5jfrnXyl6G76AAXtvrX3SOIX+d\r\nEgkuDJ1oGwuUunreKLDCSL3D34UYcHB4xh2Tf8ec9zTsf19LmnvM+g1tCB6y\r\nKKLcTTVaj9IQvyVhB+ouBdX/g4HcFTo7m2imbWG4/6EVDCzRVL1t6msgNgRD\r\nqfshwb6s83szPZDx9Mlvf1Hnrsz4ZQ7fRinYTWb5y3WP+zTzai2CFsQ2WnY+\r\nVFHtBlzDDWVC/zpDFIPG4Hdym7h9QwZqGttFnHff6BGkY7ufCLTvZbwLDN/J\r\nbrDgBeLOVXTAGR0LS0J+sC3q+e3f52MpN0hqQjivkp7K7tosBibwvr+Wvhkv\r\nF2Akn1RcxgC67jaHFb3b/v3Tl/CrHgGUT0dPzf5GERsZ/aok5FV/1dBjb81l\r\nwXEhkiW0LQfAlyzIro0Vh915ey34cUN+KCreTfDpo+iyWoRI9rGYFLVrK5v/\r\nb5SyvxaA9ZOnLaETL7tXwmca+j+s0yK61mk=\r\n=JGKX\r\n-----END PGP SIGNATURE-----\r\n","size":3925,"noattachment":false,"key":"/exit-hook/-/exit-hook-3.1.3.tgz"},"_npmUser":{"name":"sindresorhus","email":"sindresorhus@gmail.com"},"directories":{},"maintainers":[{"name":"sindresorhus","email":"sindresorhus@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/exit-hook_3.1.3_1670500195637_0.5092634482636627"},"_hasShrinkwrap":false,"readme":"# exit-hook\n\n> Run some code when the process exits\n\nThe `process.on('exit')` event doesn't catch all the ways a process can exit.\n\nThis package is useful for cleaning up before exiting.\n\n## Install\n\n```sh\nnpm install exit-hook\n```\n\n## Usage\n\n```js\nimport exitHook from 'exit-hook';\n\nexitHook(signal => {\n\tconsole.log(`Exiting with signal: ${signal}`);\n});\n\n// You can add multiple hooks, even across files\nexitHook(() => {\n\tconsole.log('Exiting 2');\n});\n\nthrow new Error('🦄');\n\n//=> 'Exiting'\n//=> 'Exiting 2'\n```\n\nRemoving an exit hook:\n\n```js\nimport exitHook from 'exit-hook';\n\nconst unsubscribe = exitHook(() => {});\n\nunsubscribe();\n```\n\n## API\n\n### exitHook(onExit)\n\nRegister a function to run during `process.exit`.\n\nReturns a function that removes the hook when called.\n\n#### onExit\n\nType: `(signal: number) => void`\n\nThe callback function to execute when the process exits.\n\n### asyncExitHook(onExit, options)\n\nRegister a function to run during `gracefulExit`.\n\nReturns a function that removes the hook when called.\n\nPlease see [Async Notes](#asynchronous-exit-notes) for considerations when using the asynchronous API.\n\n#### onExit\n\nType: `(signal: number) => (void | Promise<void>)`\n\nThe callback function to execute when the process exits via `gracefulExit`, and will be wrapped in `Promise.resolve`.\n\n#### options\n\nType: `object`\n\n##### wait\n\nType: `number`\n\nThe amount of time in milliseconds that the `onExit` function is expected to take. When multiple async handlers are registered, the longest `wait` time will be used.\n\n```js\nimport {asyncExitHook} from 'exit-hook';\n\nasyncExitHook(async () => {\n\tconsole.log('Exiting');\n}, {\n\twait: 300\n});\n\nthrow new Error('🦄');\n\n//=> 'Exiting'\n```\n\nRemoving an asynchronous exit hook:\n\n```js\nimport {asyncExitHook} from 'exit-hook';\n\nconst unsubscribe = asyncExitHook(async () => {\n\tconsole.log('Exiting');\n}, {\n\twait: 300\n});\n\nunsubscribe();\n```\n\n### gracefulExit(signal?: number): void\n\nExit the process and make a best-effort to complete all asynchronous hooks.\n\nIf you are using `asyncExitHook`, consider using `gracefulExit()` instead of `process.exit()` to ensure all asynchronous tasks are given an opportunity to run.\n\n```js\nimport {gracefulExit} from 'exit-hook';\n\ngracefulExit();\n```\n\n#### signal\n\nType: `number`\\\nDefault: `0`\n\nThe exit code to use. Same as the argument to `process.exit()`.\n\n## Asynchronous Exit Notes\n\n**tl;dr** If you have 100% control over how your process terminates, then you can swap `exitHook` and `process.exit` for `asyncExitHook` and `gracefulExit` respectively. Otherwise, keep reading to understand important tradeoffs if you're using `asyncExitHook`.\n\nNode.js does not offer an asynchronous shutdown API by default [#1](https://github.com/nodejs/node/discussions/29480#discussioncomment-99213) [#2](https://github.com/nodejs/node/discussions/29480#discussioncomment-99217), so `asyncExitHook` and `gracefulExit` will make a \"best effort\" attempt to shut down the process and run your asynchronous tasks.\n\nIf you have asynchronous hooks registered and your Node.js process is terminated in a synchronous manner, a `SYNCHRONOUS TERMINATION NOTICE` error will be logged to the console. To avoid this, ensure you're only exiting via `gracefulExit` or that an upstream process manager is sending a `SIGINT` or `SIGTERM` signal to Node.js.\n\nAsynchronous hooks should make a \"best effort\" to perform their tasks within the `wait` time, but also be written to assume they may not complete their tasks before termination.\n","publish_time":1670500195794,"_cnpm_publish_time":1670500195794,"dist-tags":{"latest":"5.1.0"}}