@godaddy/terminus
Advanced tools
Comparing version 4.11.2 to 4.12.0
@@ -6,9 +6,5 @@ 'use strict' | ||
const SUCCESS_RESPONSE = JSON.stringify({ | ||
status: 'ok' | ||
}) | ||
let SUCCESS_RESPONSE | ||
const FAILURE_RESPONSE = JSON.stringify({ | ||
status: 'error' | ||
}) | ||
let FAILURE_RESPONSE | ||
@@ -19,3 +15,4 @@ function noopResolves () { | ||
async function sendSuccess (res, { info, verbatim, statusOk, headers }) { | ||
async function sendSuccess (res, { info, verbatim, statusOk, statusOkResponse, headers }) { | ||
res.statusCode = statusOk | ||
res.setHeader('Content-Type', 'application/json') | ||
@@ -27,5 +24,3 @@ res.writeHead(statusOk, headers) | ||
Object.assign( | ||
{ | ||
status: 'ok' | ||
}, | ||
statusOkResponse, | ||
verbatim ? info : { info, details: info } | ||
@@ -40,3 +35,3 @@ ) | ||
async function sendFailure (res, options) { | ||
const { error, headers, onSendFailureDuringShutdown, exposeStackTraces, statusCode, statusError } = options | ||
const { error, headers, onSendFailureDuringShutdown, exposeStackTraces, statusCode, statusResponse, statusError, statusErrorResponse } = options | ||
@@ -61,12 +56,15 @@ function replaceErrors (_, value) { | ||
} | ||
res.statusCode = statusCode || statusError | ||
const responseBody = statusResponse || statusErrorResponse | ||
res.setHeader('Content-Type', 'application/json') | ||
res.writeHead(statusCode || statusError, headers) | ||
if (error) { | ||
return res.end(JSON.stringify({ | ||
status: 'error', | ||
error: error, | ||
details: error | ||
}, replaceErrors)) | ||
return res.end(JSON.stringify( | ||
Object.assign( | ||
responseBody, { | ||
error, | ||
details: error | ||
}), replaceErrors)) | ||
} | ||
res.end(FAILURE_RESPONSE) | ||
res.end(statusResponse ? JSON.stringify(responseBody) : FAILURE_RESPONSE) | ||
} | ||
@@ -81,3 +79,3 @@ | ||
function decorateWithHealthCheck (server, state, options) { | ||
const { healthChecks, logger, headers, onSendFailureDuringShutdown, sendFailuresDuringShutdown, caseInsensitive, statusOk, statusError } = options | ||
const { healthChecks, logger, headers, onSendFailureDuringShutdown, sendFailuresDuringShutdown, caseInsensitive, statusOk, statusOkResponse, statusError, statusErrorResponse } = options | ||
@@ -90,3 +88,3 @@ let hasSetHandler = false | ||
if (state.isShuttingDown && sendFailuresDuringShutdown) { | ||
return sendFailure(res, { onSendFailureDuringShutdown, statusError }) | ||
return sendFailure(res, { onSendFailureDuringShutdown, statusError, statusErrorResponse }) | ||
} | ||
@@ -98,2 +96,4 @@ let info | ||
logger('healthcheck failed', error) | ||
const statusCode = error.statusCode | ||
const statusResponse = error.statusResponse | ||
return sendFailure( | ||
@@ -105,4 +105,6 @@ res, | ||
exposeStackTraces: healthChecks.__unsafeExposeStackTraces, | ||
statusCode: error.statusCode, | ||
statusError | ||
statusCode, | ||
statusResponse, | ||
statusError, | ||
statusErrorResponse | ||
} | ||
@@ -113,3 +115,3 @@ ) | ||
res, | ||
{ info, verbatim: healthChecks.verbatim, statusOk, headers } | ||
{ info, verbatim: healthChecks.verbatim, statusOk, statusOkResponse, headers } | ||
) | ||
@@ -197,3 +199,5 @@ } | ||
statusOk = 200, | ||
statusOkResponse = { status: 'ok' }, | ||
statusError = 503, | ||
statusErrorResponse = { status: 'error' }, | ||
headers = options.headers || {} | ||
@@ -204,2 +208,6 @@ } = options | ||
SUCCESS_RESPONSE = JSON.stringify(statusOkResponse) | ||
FAILURE_RESPONSE = JSON.stringify(statusErrorResponse) | ||
if (Object.keys(healthChecks).length > 0) { | ||
@@ -213,3 +221,5 @@ decorateWithHealthCheck(server, state, { | ||
statusOk, | ||
statusOkResponse, | ||
statusError, | ||
statusErrorResponse, | ||
headers | ||
@@ -216,0 +226,0 @@ }) |
{ | ||
"name": "@godaddy/terminus", | ||
"version": "4.11.2", | ||
"version": "4.12.0", | ||
"description": "", | ||
@@ -14,2 +14,11 @@ "main": "index.js", | ||
}, | ||
"files": [ | ||
"lib", | ||
"typings", | ||
"index.js", | ||
"LICENSE", | ||
"README", | ||
"package.json", | ||
"package-lock.json" | ||
], | ||
"repository": { | ||
@@ -34,9 +43,8 @@ "type": "git", | ||
"express": "^4.16.2", | ||
"mocha": "^8.2.1", | ||
"node-fetch": "^2.0.0", | ||
"mocha": "^10.0.0", | ||
"node-fetch": "^2.6.7", | ||
"nyc": "^15.0.0", | ||
"pre-commit": "^1.2.2", | ||
"semantic-release": "^17.4.2", | ||
"standard": "^16.0.3", | ||
"supertest": "^4.0.0", | ||
"semantic-release": "^19.0.5", | ||
"standard": "^17.0.0", | ||
"typescript": "^3.9.9" | ||
@@ -43,0 +51,0 @@ }, |
@@ -61,3 +61,5 @@ # terminus | ||
statusOk, // [optional = 200] status to be returned for successful healthchecks | ||
statusOkResponse, // [optional = { status: 'ok' }] status response to be returned for successful healthchecks | ||
statusError, // [optional = 503] status to be returned for unsuccessful healthchecks | ||
statusErrorResponse, // [optional = { status: 'error' }] status response to be returned for unsuccessful healthchecks | ||
@@ -184,2 +186,9 @@ // cleanup options | ||
### With cluster (and eg. express) | ||
If you want to use (`cluster`)[https://nodejs.org/api/cluster.html] to use more than one CPU, you need to use `terminus` per worker. | ||
This is heavily inspired by https://medium.com/@gaurav.lahoti/graceful-shutdown-of-node-js-workers-dd58bbff9e30. | ||
See `example/express.cluster.js`. | ||
## How to set Terminus up with Kubernetes? | ||
@@ -186,0 +195,0 @@ |
@@ -27,3 +27,5 @@ declare module "@godaddy/terminus" { | ||
statusOk?: number, | ||
statusOkResponse?: Record<string, unknown>, | ||
statusError?: number, | ||
statusErrorResponse?: Record<string, unknown>, | ||
useExit0?: boolean, | ||
@@ -30,0 +32,0 @@ onSignal?: () => Promise<any>; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
Shell access
Supply chain riskThis module accesses the system shell. Accessing the system shell increases the risk of executing arbitrary code.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
62347
12
29
1575
233
56