{"_id":"async-cleanup","name":"async-cleanup","dist-tags":{"latest":"1.0.0"},"versions":{"1.0.0":{"name":"async-cleanup","version":"1.0.0","description":"Asynchronous process cleanup for Node.js and Typescript","keywords":["async","cleanup","exit","process","signal"],"homepage":"https://github.com/trevorr/async-cleanup#readme","bugs":{"url":"https://github.com/trevorr/async-cleanup/issues"},"repository":{"type":"git","url":"git+https://github.com/trevorr/async-cleanup.git"},"license":"ISC","author":{"name":"Trevor Robinson"},"main":"dist/index.js","types":"dist/index.d.ts","scripts":{"build":"rm -rf dist && tsc --project tsconfig.build.json","examples":"concurrently 'ts-node examples/readme.ts' 'ts-node examples/readme.ts' 'ts-node examples/readme.ts'","lint":"eslint src","lint:ci":"eslint src --format junit --output-file test-results/eslint/eslint.xml","prepare":"npm run build","prepublishOnly":"npm run test && npm run lint","prettier":"prettier --write src","test":"jest src","test:ci":"jest src --ci --runInBand --reporters=default --reporters=jest-junit"},"devDependencies":{"@types/jest":"^28.1.6","@types/node":"^18.7.1","@typescript-eslint/eslint-plugin":"^5.33.0","@typescript-eslint/parser":"^5.33.0","concurrently":"^7.3.0","eslint":"^8.21.0","jest":"^28.1.3","jest-junit":"^14.0.0","prettier":"^2.7.1","ts-jest":"^28.0.7","ts-node":"^10.9.1","typescript":"^4.7.4"},"jest-junit":{"outputDirectory":"test-results/junit","outputName":"junit.xml"},"prettier":{"semi":true},"gitHead":"d890d9ca7ea78d3d4e745f4eec891fd7a0cafe66","_id":"async-cleanup@1.0.0","_nodeVersion":"16.14.2","_npmVersion":"8.5.0","dist":{"integrity":"sha512-nwvuXd9/HeN/dAOyi0JIVB/Ujk9+LUNX2jkyBr6p9tR//6084+rFAXMJYWFyN7FI8SPt+Pkkq+NKCA2mnLOfOA==","shasum":"f3ffcdc2a027b79c974fac19d15c18db6b178991","tarball":"https://registry.npmjs.org/async-cleanup/-/async-cleanup-1.0.0.tgz","fileCount":6,"unpackedSize":14293,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQD012dlf5jqfeTZGMaO9tPqf25JCigpyoQMKH7skxSxgAIhAIHdU9+pKDW4XfB2JJKNRfIPMI4IQftod62a6SYUqMQ5"}],"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v4.10.10\r\nComment: https://openpgpjs.org\r\n\r\nwsFzBAEBCAAGBQJi9D9LACEJED1NWxICdlZqFiEECWMYAoorWMhJKdjhPU1b\r\nEgJ2VmqXtA//T9DLj3cbazWYRQvgQFR0HEyl4KGrFaEoSuNh36YUdDOVlpEB\r\n9qHvUntU7xodEJduThvh/QF8vhusptmstGvACffhf9JKAupmCzoTwS2S3Cit\r\nlOf0rLkK+ERlQw3rAkIf3ABLD6Vcf39TOS4DAaGOMfQb3gWmcQZxtW7QZTw6\r\n4cFRswPyNrjxBGsF/dL+FhiEjbnmqNBmqosG2GvO/xu/32sbmXXWPR+S2L5/\r\nEMl/tdv9Dg0bRsbVzUFF753WqfAvgpGLivEFTg8KrR/knA9cLQEPuQp6ddPK\r\ny57BaSx6pKNSNe2tzohNr7jw9vp1580nluc26hhksaRf6tNIX6KNzTbaRLS6\r\nMWtsy7rNqI87lKcAbIgmUif442XBf2hyR7yyuVs4sBUlLwq0fOy4koK1LuBU\r\nvMSSn0VAu0Vpejlz9GngMn6/C5mhswygjcOPIrUCi6Fm5rOK4lcLgdj5zPzp\r\ngqFj4wVXb9pumJ1UH8KaQvSfNm7gC2N/l8sfJFXeFevTItawPSk717Ya8KBR\r\noHFy+7YCgaJaydPmOyll/nxm1vsxbl8fKEPOjhEFq2eoCXBwuMyXxORB4d6z\r\n/DYB635uaa0NkQMSRqfvXUrTt8V5SJpqTCIvpEoFpWN2eImhSpTChaSmS+Tg\r\nG842TXRaXkG5a0miI1PNsi1FNX0aCjQ4t3M=\r\n=VVkT\r\n-----END PGP SIGNATURE-----\r\n"},"_npmUser":{"name":"trevorr","email":"trevor@scurrilous.com"},"directories":{},"maintainers":[{"name":"trevorr","email":"trevor@scurrilous.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/async-cleanup_1.0.0_1660174155080_0.2932798924229072"},"_hasShrinkwrap":false}},"time":{"created":"2022-08-10T23:29:15.080Z","1.0.0":"2022-08-10T23:29:15.270Z","modified":"2022-08-10T23:29:15.442Z"},"maintainers":[{"name":"trevorr","email":"trevor@scurrilous.com"}],"description":"Asynchronous process cleanup for Node.js and Typescript","homepage":"https://github.com/trevorr/async-cleanup#readme","keywords":["async","cleanup","exit","process","signal"],"repository":{"type":"git","url":"git+https://github.com/trevorr/async-cleanup.git"},"author":{"name":"Trevor Robinson"},"bugs":{"url":"https://github.com/trevorr/async-cleanup/issues"},"license":"ISC","readme":"# async-cleanup: Asynchronous process cleanup for Node.js and Typescript\n\n[![npm](https://img.shields.io/npm/v/async-cleanup)](https://www.npmjs.com/package/async-cleanup)\n[![CircleCI](https://img.shields.io/circleci/build/github/trevorr/async-cleanup)](https://circleci.com/gh/trevorr/async-cleanup)\n\nA common need for Node.js applications is to execute some sort of cleanup code before the process exits, regardless of what caused it to exit.\nFurthermore, cleanup code often involves asynchronous operations such as file or network I/O.\nFor example, imagine having a lock file that prevents multiple instances of an application from running concurrently.\nWhether the application completes successfully, crashes from an unhandled exception, or is interrupted with Ctrl-C, the lock file must be deleted before exit.\nThis library makes writing asynchronous cleanup code as easy as calling a single function.\nIt has no runtime dependencies, compiles to ES6 for wide compatibility, and has a package size of 5.1 kB.\n\n## Installation\n\n```sh\nnpm install async-cleanup\n```\n\n## Usage\n\nTypical usage might look something like [this](examples/readme.ts):\n\n```ts\nimport { addCleanupListener, exitAfterCleanup } from \"async-cleanup\";\nimport { unlink, writeFile } from \"fs/promises\";\n\nvoid (async function () {\n  const path = \".lockfile\";\n  try {\n    await writeFile(path, String(process.pid), { flag: \"wx\" });\n  } catch (err) {\n    if ((err as { code?: string }).code === \"EEXIST\") {\n      console.log(`${path} already exists`);\n    } else {\n      console.log(`Error writing ${path}`, err);\n    }\n    // Can't use process.exit with async cleanup\n    await exitAfterCleanup(1);\n  }\n  console.log(`Created ${path}`);\n\n  addCleanupListener(async () => {\n    await unlink(path);\n    console.log(`Deleted ${path}`);\n  });\n\n  // Do stuff...\n\n  console.log(\"Stuff done\");\n})();\n```\n\nThe output of the program above might look like this (depending on how concurrently it is run):\n\n```\n$ concurrently 'ts-node examples/readme.ts' 'ts-node examples/readme.ts' 'ts-node examples/readme.ts'\n\n[1] .lockfile already exists\n[0] .lockfile already exists\n[2] Created .lockfile\n[2] Stuff done\n[2] Exiting with code 0 due to empty event loop\n[2] Deleted .lockfile\n[1] ts-node examples/readme.ts exited with code 1\n[2] ts-node examples/readme.ts exited with code 0\n[0] ts-node examples/readme.ts exited with code 1\n```\n\n## API Reference\n\n```ts\n/**\n * A possibly asynchronous function invoked with the process is about to exit.\n */\ntype CleanupListener = () => void | Promise<void>;\n\n/**\n * Registers a new cleanup listener.\n * Adding the same listener more than once has no effect.\n */\nfunction addCleanupListener(listener: CleanupListener): void;\n\n/**\n * Removes an existing cleanup listener, and returns whether the listener was registered.\n */\nfunction removeCleanupListener(listener: CleanupListener): boolean;\n\n/**\n * Executes all cleanup listeners and then exits the process.\n * Call this instead of `process.exit` to ensure all listeners are fully executed.\n */\nfunction exitAfterCleanup(code?: number): Promise<never>;\n\n/**\n * Executes all cleanup listeners and then kills the process with the given signal.\n */\nfunction killAfterCleanup(signal: ExitSignal): Promise<void>;\n\n/**\n * Signals that can terminate the process.\n */\ntype ExitSignal =\n  | \"SIGBREAK\"\n  | \"SIGHUP\"\n  | \"SIGINT\"\n  | \"SIGTERM\"\n  | \"SIGUSR2\"\n  | \"SIGKILL\"\n  | \"SIGQUIT\"\n  | \"SIGSTOP\";\n```\n\n## License\n\n`async-cleanup` is available under the [ISC license](LICENSE).\n","readmeFilename":"README.md"}