{"name":"close-with-grace","version":"2.3.0","keywords":["graceful","shutdown","close","exit"],"author":{"name":"Matteo Collina","email":"hello@matteocollina.com"},"license":"MIT","_id":"close-with-grace@2.3.0","maintainers":[{"name":"matteo.collina","email":"hello@matteocollina.com"}],"homepage":"https://github.com/mcollina/close-with-grace#readme","bugs":{"url":"https://github.com/mcollina/close-with-grace/issues"},"dist":{"shasum":"01c5a6c1c8e2586b9a949e7bda0767f79c001e34","tarball":"http://registry-npm.ywwl.com/close-with-grace/download/close-with-grace-2.3.0.tgz","fileCount":29,"integrity":"sha512-38BS9BuqAml6XFIlSWQcj3eivE05yFV6cJDuYoNGiHrE+h9ud1JtMJIVKXdLWa2Uo2Xt7q/GYczOesEchvBEsw==","signatures":[{"sig":"MEQCIG4sy4Oh1ARPuKPV+t0jRdmL2TiVmJl24NIO96vQ+WL7AiBTlbXScGHElKmM45pivl8HetRbVzDQbaa3xd1I7xy/cA==","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":31884,"size":7128,"noattachment":false,"key":"/close-with-grace/-/close-with-grace-2.3.0.tgz"},"main":"index.js","type":"commonjs","types":"./index.d.ts","gitHead":"124baf51ba12db9265ef1f31d66ffe0fb0145cff","scripts":{"test":"standard && npm run test-only && tsd","test-only":"tape test/*.test.js | tap-dot"},"_npmUser":{"name":"matteo.collina","email":"hello@matteocollina.com"},"repository":{"url":"git+https://github.com/mcollina/close-with-grace.git","type":"git"},"_npmVersion":"10.9.3","description":"Exit your process, gracefully (if possible)","directories":{},"_nodeVersion":"22.19.0","_hasShrinkwrap":false,"devDependencies":{"tsd":"^0.28.0","tape":"^5.1.1","tap-dot":"^2.0.0","standard":"^17.0.0","@fastify/pre-commit":"^2.0.2"},"_npmOperationalInternal":{"tmp":"tmp/close-with-grace_2.3.0_1758632768001_0.3530214869216848","host":"s3://npm-registry-packages-npm-production"},"readme":"# close-with-grace\n\nExit your process, gracefully (if possible) - for Node.js\n\n## Install\n\n```\nnpm i close-with-grace\n```\n\n## Usage\n\n```js\nconst closeWithGrace = require('close-with-grace')\n\n// delay is the number of milliseconds for the graceful close to\n// finish.\ncloseWithGrace({ delay: 500 }, async function ({ signal, err, manual }) {\n  if (err) {\n    console.error(err)\n  }\n  await closeYourServer()\n})\n\n// default delay is 10000\n// to disable delay feature at all, pass falsy value to delay option.\ncloseWithGrace({ delay: false }, () => await somethingUseful())\n```\n\n### Injecting custom logger\n\n```js\nconst closeWithGrace = require('close-with-grace')\n\n// delay is the number of milliseconds for the graceful close to\n// finish.\ncloseWithGrace(\n  {\n    delay: 500,\n    logger: { error: (m) => console.error(`[close-with-grace] ${m}`) }\n  },\n  async function ({ signal, err, manual }) {\n  if (err) {\n    console.error(err)\n  }\n  await closeYourServer()\n})\n\n// default logger is console\n// to disable logging at all, pass falsy value to logger option.\ncloseWithGrace({ logger: false }, () => await somethingUseful())\n```\n\n### Example with Fastify\n\n```js\nimport fastify from 'fastify'\nimport closeWithGrace from 'close-with-grace'\n\nconst app = fastify()\n\ncloseWithGrace(async function ({ signal, err, manual }) {\n  if (err) {\n    app.log.error({ err }, 'server closing with error')\n  } else {\n    app.log.info(`${signal} received, server closing`)\n  }\n  await app.close()\n})\n\nawait app.listen()\n```\n\n### Skipping specific events\n\nSkip specific events from triggering the graceful close. Note: You are responsible for handling skipped events yourself.\n\n```js\nimport closeWithGrace from 'close-with-grace'\n\n// Handle errors separately\nprocess.on('unhandledRejection', (err) => {\n  // Your custom error handling\n})\n\ncloseWithGrace(\n  { skip: ['unhandledRejection', 'uncaughtException'] },\n  async function ({ signal, err, manual }) {\n    await cleanupResources()\n  }\n)\n```\n\n## API\n\n### `closeWithGrace([opts], fn({ err, signal, manual }))`\n\n`closeWithGrace` adds a global listeners to the events:\n\n* `process.once('SIGHUP')`\n* `process.once('SIGINT')`\n* `process.once('SIGQUIT')`\n* `process.once('SIGILL')`\n* `process.once('SIGTRAP')`\n* `process.once('SIGABRT')`\n* `process.once('SIGBUS')`\n* `process.once('SIGFPE')`\n* `process.once('SIGSEGV')`\n* `process.once('SIGUSR2')`\n* `process.once('SIGTERM')`\n* `process.once('uncaughtException')`\n* `process.once('unhandledRejection')`\n* `process.once('beforeExit')`\n\nIn case one of them is emitted, it will call the given function.\nIf it is emitted again, it will terminate the process abruptly.\n\n#### opts\n\n* `delay`: the numbers of milliseconds before abruptly close the\n  process. Default: `10000`.\n  - Pass `false`, `null` or `undefined` to disable this feature.\n\n* `logger`: instance of logger which will be used internally. Default: `console`.\n  - Pass `false`, `null` or `undefined` to disable this feature.\n\n* `skip`: an array of event names to skip from triggering the graceful close callback. Default: `[]`.\n  - Example: `skip: ['unhandledRejection', 'uncaughtException', 'SIGTERM']`\n  - Note: You must handle skipped events yourself, otherwise they may cause the process to crash or exit unexpectedly.\n\n* `onSecondError(error)`: A callback to execute if the process throws an `uncaughtException`\n  or an `unhandledRejection` while `fn` is executing.\n\n* `onSecondSignal(signal)`: A callback to execute if the process receives another\n  signal while `fn` is executing.\n\n* `onTimeout(delay)`: A callback to execute if `fn` failed to completed after `delay` milliseconds.\n\nBoth `onSecondError`, `onSecondSignal` or `onTimeout` can be used to perform custom logic, but `process.exit(1)`\nwill be immediately be invoked after they exit so no asynchronous operations are possible.\n\n#### fn({ err, signal, manual } [, cb])\n\nExecute the given function to perform a graceful close.\nThe function can either return a `Promise` or call the callback.\nIf this function does not error, the process will be closed with\nexit code `0`.\nIf the function rejects with an `Error`, or call the callback with an\n`Error` as first argument, the process will be closed with exit code\n`1`.\n\n#### return values\n\nCalling `closeWithGrace()` will return an object as formed:\n\n* `close()`: close the process, the `manual` argument will be set to\n  true.\n* `uninstall()`: remove all global listeners.\n\n## License\n\nMIT\n","publish_time":1758632768189,"_cnpm_publish_time":1758632768189,"dist-tags":{"latest":"2.5.0"}}