From ab54a690a698b58e7bb048c62aced9d3553318fd Mon Sep 17 00:00:00 2001 From: przemek-nowicki Date: Wed, 4 May 2022 16:13:11 +0200 Subject: [PATCH] listen and provide callback handler on SIGPIPE signal --- .gitignore | 28 ++++++++++++++++++++++++++++ .npmignore | 26 ++++++++++++++++++++++++++ README.md | 10 +++++++++- index.js | 32 ++++++++++++++++++++++++++++++++ package.json | 30 ++++++++++++++++++++++++++++++ 5 files changed, 125 insertions(+), 1 deletion(-) create mode 100644 .gitignore create mode 100644 .npmignore create mode 100644 index.js create mode 100644 package.json diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0f53990 --- /dev/null +++ b/.gitignore @@ -0,0 +1,28 @@ +# Logs # +/logs +*.log +*.log* + +# Node files # +node_modules/ +npm-debug.log +yarn-error.log + +# OS generated files # +.DS_Store +Thumbs.db + +# Typing # +typings/ + +# Dist # +dist/ + +# IDE # +.idea/ +*.swp +.awcache + +# Generated source-code # +src/**/*.js.map +coverage/ \ No newline at end of file diff --git a/.npmignore b/.npmignore new file mode 100644 index 0000000..c37eac4 --- /dev/null +++ b/.npmignore @@ -0,0 +1,26 @@ +# Logs # +/logs +*.log +*.log* + +# Node files # +node_modules/ +npm-debug.log +yarn-error.log + +# OS generated files # +.DS_Store +Thumbs.db + +# Typing # +typings/ + +# IDE # +.idea/ +*.swp +.awcache + +# Generated source-code # +test/ +coverage/ + diff --git a/README.md b/README.md index 3ce6b4e..b7ce109 100644 --- a/README.md +++ b/README.md @@ -2,8 +2,16 @@ **RnR** - is tiny lib to refresh node setup on runtime without restarting the node server. +# Install + +```sh +npm install --save runtime-node-refresh +``` + +# Usage + It may be very usefult when you want to refresh/update your environment variables let's say your log level without restarting node server. -Note: RnR is using node signals which means it's not working on Worker threads. +Note: RnR is using node signals which means it does not work on Worker threads. diff --git a/index.js b/index.js new file mode 100644 index 0000000..39b189b --- /dev/null +++ b/index.js @@ -0,0 +1,32 @@ +'use strict'; + +let cbToCall; +const process = global.process + +const isProcessOk = (process) => { + return process && + typeof process === 'object' && + typeof process.kill === 'function' && + typeof process.pid === 'number' && + typeof process.on === 'function' +} + +const reloadCbFn = (cb) => { + cbToCall = cb; +}; + +process.on('SIGPIPE', () => { + if (typeof cbToCall === 'function') { + cbToCall(); + } else { + console.error('SIGPIPE triggered but no callback provided to execute!'); + } +}); + +if (!isProcessOk(process)) { + module.exports = () => { + return () => {} + } +} else { + module.exports = reloadCbFn; +} \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..4b86f2c --- /dev/null +++ b/package.json @@ -0,0 +1,30 @@ +{ + "name": "runtime-node-refresh", + "version": "1.0.0", + "description": "Refresh node environment variables on runtime without restarting the node server.", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/przemek-nowicki/runtime-node-refresh.git" + }, + "keywords": [ + "refresh", + "env", + "update", + "env", + "runtime", + "refresh", + "node", + "refresh", + "envs" + ], + "author": "nowicki.przemek@gmail.com", + "license": "ISC", + "bugs": { + "url": "https://github.com/przemek-nowicki/runtime-node-refresh/issues" + }, + "homepage": "https://github.com/przemek-nowicki/runtime-node-refresh#readme" +}