New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

fastify-healthcheck

Package Overview
Dependencies
Maintainers
1
Versions
20
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fastify-healthcheck - npm Package Compare versions

Comparing version 0.1.1 to 0.2.0

Dockerfile-fail.example

22

CHANGELOG.md
# Change Log
## [0.2.0](https://github.com/smartiniOnGitHub/fastify-healthcheck/releases/tag/0.2.0) (2018-11-27)
Summary Changelog:
- Delegate to the [under-pressure](https://www.npmjs.com/package/under-pressure)
plugin the logic to report the status of the web application
- To fully encapsulate `under-pressure` features, removed the dependency
on [fastify-plugin](https://github.com/fastify/fastify-plugin);
for more info look [here](https://github.com/fastify/fastify/blob/master/docs/Plugins.md#handle-the-scope),
[here](https://github.com/fastify/fastify/blob/master/docs/Plugins-Guide.md#how-to-handle-encapsulation-and-distribution)
- Update Fastify dependencies to '1.1.0' or higher (but on 1.x),
but without `fastify-plugin` to check it now (see related changes)
- Add another example (`example-under-pressure-fail`) with `under-pressure`
configured to always return a `Service Unavailable` error (HTTP 503),
and related `Dockerfile-fail.example` to simplify its usage (as a sample)
- Update `healthcheck` standalone script to return 0 for success,
or the HTTP error code in case of failure (or 1 in case of other failure)
## [0.1.1](https://github.com/smartiniOnGitHub/fastify-healthcheck/releases/tag/0.1.1) (2018-11-13)
Summary Changelog:
- Maintenance release to fix Fastify dependencies to '1.x' to avoid breaking changes because Fastify '2.x' will be released soon
- Updated dependencies to latest Fastify plugin (1.2.1) and Fastify 1.x (1.13.0)
- Maintenance release to fix Fastify dependencies to '1.x',
to avoid breaking changes because Fastify '2.x' will be released soon
- Updated dependencies to latest Fastify plugin (1.2.1)
and Fastify 1.x (1.13.0)

@@ -8,0 +26,0 @@ ## [0.1.0](https://github.com/smartiniOnGitHub/fastify-healthcheck/releases/tag/0.1.0) (2018-10-31)

5

example/example.js

@@ -22,5 +22,6 @@ /*

fastify.register(require('../src/plugin'), {
// url: '/custom-health',
// healthcheckUrl: '/custom-health',
// healthcheckUrlDisable: true,
// healthcheckUrlAlwaysFail: true
// healthcheckUrlAlwaysFail: true,
// underPressureOptions: { } // no under-pressure specific options set here
})

@@ -27,0 +28,0 @@

{
"name": "fastify-healthcheck",
"version": "0.1.1",
"version": "0.2.0",
"description": "Fastify Plugin to serve responses for health checks",

@@ -9,2 +9,3 @@ "main": "src/plugin",

"docker:build": "docker build -t fastify-healthcheck-example -f Dockerfile.example .",
"docker:build:fail": "docker build -t fastify-healthcheck-example -f Dockerfile-fail.example .",
"docker:run": "docker run --rm --name fastify-healthcheck-example -d -p 3000:3000 -t fastify-healthcheck-example",

@@ -19,2 +20,3 @@ "docker:inspect": "docker exec -it fastify-healthcheck-example bash",

"example": "node example/example",
"example-under-pressure-fail": "node example/example-under-pressure-fail",
"start": "node src/healthcheck",

@@ -26,3 +28,3 @@ "lint": "standard \"./*.js\" \"./src/**/*.js\" \"./test/**/*.test.js\" \"./example/**/*.js\"",

"dependencies": {
"fastify-plugin": "^1.2.1"
"under-pressure": "^0.3.0"
},

@@ -33,3 +35,3 @@ "devDependencies": {

"standard": "^12.0.1",
"tap": "^12.0.1"
"tap": "^12.1.0"
},

@@ -36,0 +38,0 @@ "peerDependencies": {},

@@ -14,3 +14,3 @@ # fastify-healthcheck

With this plugin, Fastify could expose an healthcheck route configured
With this plugin, Fastify by default expose an healthcheck route configured
for `/health` GET requests, and even a script that can be executed to get

@@ -24,6 +24,16 @@ content via HTTP GET from that running web application.

will be used, but if needed can be specified:
- `url`, to set a different uri for the healthcheck route
- `healthcheckUrl`, to set a different uri for the healthcheck route
- `healthcheckUrlDisable`, to not publish the healthcheck route
- `healthcheckUrlAlwaysFail`, to always return failure responses (useful to test failure responses)
Under the hood, the healthcheck status is determined by the
[under-pressure](https://www.npmjs.com/package/under-pressure) plugin,
used here as a dependency; so it's possible to specify all its
configuration options here.
To use all default values for `healthcheck` options, do not set its options
(or set with undefined values); in that way no `under-pressure` specific
options will be overridden by them.
Sample usage:

@@ -39,3 +49,3 @@

// example with custom healthcheck url and response to always fail
// fastify.register(require('fastify-healthcheck'), { url: '/custom-health', healthcheckUrlAlwaysFail: true })
// fastify.register(require('fastify-healthcheck'), { healthcheckUrl: '/custom-health', healthcheckUrlAlwaysFail: true })
//

@@ -46,4 +56,5 @@

// To test, for example (in another terminal session) do:
// `npm start`, or
// `curl http://127.0.0.1:3000/health` => returning an HTTP response 200 (OK)
// and a JSON response like: {"statusCode":200,"status":"UP"}
// and a JSON response like: {"statusCode":200,"status":"ok"}
// or run the healthcheck script, for example with:

@@ -64,7 +75,13 @@ // `node src/healthcheck http://localhost:3000/health`

like in the following sequence:
- `docker:build`, to build the image
- `docker:run`, to start the container from generated image, in detached mode
- `docker:healthcheck-manual`, to run the healthcheck script in the container but manually
- `docker:build`, to build the image, where the entry point is the example
- `docker:build:fail`, to build the image, but as entry point the example
that is triggering the `Service Unavailable` error (HTTP 503) in the
healthcheck route
- `docker:run`, to start the container from generated image,
in detached mode
- `docker:healthcheck-manual`, to run the healthcheck script in the
container but manually
- `docker:status`, to get the health status of the container
- and others like: `docker:inspect` (interactive), `docker:log` (<CTRL>C to close), `docker:process`, etc ...
- and others like: `docker:inspect` (interactive), `docker:log`
(<CTRL>C to close), `docker:process`, etc ...
- `docker:stop`, to stop running container

@@ -76,3 +93,3 @@ - `docker:clean`, to remove generated image

Fastify 1.x .
Fastify ^1.1.0 .

@@ -82,3 +99,7 @@

By default the plugin map a default endpoint on the URI `/health` to be
To fully encapsulate `under-pressure` features inside the scope
of this plugin, the plugin is not exposed by [fastify-plugin](https://github.com/fastify/fastify-plugin);
for more info look [here](https://github.com/fastify/fastify/blob/master/docs/Plugins.md#handle-the-scope), [here](https://github.com/fastify/fastify/blob/master/docs/Plugins-Guide.md#how-to-handle-encapsulation-and-distribution).
The plugin map a default endpoint on the URI `/health` to be
called via GET, but it's possible to change it with the setting 'url'

@@ -91,3 +112,3 @@ in plugin options.

do the healthcheck without the need to use other tools
like 'curl' or 'wget' that must be available in the container.
like `curl` or `wget` that must be available in the container.

@@ -102,3 +123,8 @@ Both approaches could be useful in most common cases, like

To execute the healthcheck script from another Node.js project/package,
you need to run something like:
`node node_modules/fastify-healthcheck/src/healthcheck http://localhost:8000/health`,
with the webapp exposed to the port `8000` in this case.
## License

@@ -105,0 +131,0 @@

@@ -20,2 +20,3 @@ /*

// for example it can be called by container health checks
// return code will be 0 for success, or the HTTP error code

@@ -32,3 +33,3 @@ // use Node.js 'http' integrated module,

if (options.log === true) {
console.log(`call healthcheck at: ${url} ...`)
console.log(`GET call for healthcheck at: ${url} ...`)
}

@@ -38,3 +39,7 @@

if (options.log === true) {
console.log(`status: ${res.statusCode}`)
console.log(`statusCode: ${res.statusCode}`)
if (res.statusMessage) {
console.log(`statusMessage: '${res.statusMessage}'`)
}
console.log(`----------------`)
}

@@ -44,3 +49,3 @@ if (res.statusCode === 200) {

} else {
process.exit(1)
process.exit(res.statusCode || 1)
}

@@ -54,5 +59,5 @@ })

}
process.exit(1)
process.exit(err.statusCode || 1)
})
request.end()

@@ -18,41 +18,66 @@ /*

const fp = require('fastify-plugin')
function fastifyHealthchecks (fastify, options, next) {
function fastifyHealthcheck (fastify, options, next) {
const {
url = '/health',
healthcheckUrl = '/health',
healthcheckUrlDisable = false,
healthcheckUrlAlwaysFail = false
healthcheckUrlAlwaysFail = false,
underPressureOptions = { }
} = options
// console.log(`DEBUG - plugin options: ${JSON.stringify(options)}`)
if (typeof url !== 'string') {
throw new TypeError(`The option url must be a string, instead got a '${typeof url}'`)
ensureIsString(healthcheckUrl, 'healthcheckUrl')
ensureIsBoolean(healthcheckUrlDisable, 'healthcheckUrlDisable')
ensureIsBoolean(healthcheckUrlAlwaysFail, 'healthcheckUrlAlwaysFail')
ensureIsObject(underPressureOptions, 'underPressureOptions')
// execute plugin code
// register under-pressure plugin
// note that it will trigger automatic failure responses
// in all routes defined here when current values are higher
// that threshold values set
fastify.register(require('under-pressure'), underPressureOptions)
let healthcheckHandler = normalHandler
if (healthcheckUrlAlwaysFail !== null && healthcheckUrlAlwaysFail === true) {
healthcheckHandler = failHandler
}
// execute plugin code
if (healthcheckUrlDisable === null || healthcheckUrlDisable === false) {
fastify.route({
method: 'GET',
url: url,
handler: _healthcheckHandler
url: healthcheckUrl,
handler: healthcheckHandler
})
}
function _healthcheckHandler (req, reply) {
// return a simple health check message and an HTTP success code
if (healthcheckUrlAlwaysFail === false) {
reply.send({ statusCode: 200, status: 'UP' })
} else {
// unless plugin option to always fail is raised
reply.code(500).send({ statusCode: 500, status: 'DOWN' })
}
next()
}
function failHandler (req, reply) {
reply.code(500).send({ statusCode: 500, status: 'ko' })
}
function normalHandler (req, reply) {
reply.code(200).send({ statusCode: 200, status: 'ok' })
}
function ensureIsString (arg, name) {
if (arg !== null && typeof arg !== 'string') {
throw new TypeError(`The argument '${name}' must be a string, instead got a '${typeof arg}'`)
}
}
next()
function ensureIsBoolean (arg, name) {
if (arg !== null && typeof arg !== 'boolean') {
throw new TypeError(`The argument '${name}' must be a boolean, instead got a '${typeof arg}'`)
}
}
module.exports = fp(fastifyHealthchecks, {
fastify: '1.x',
name: 'fastify-healthcheck'
})
function ensureIsObject (arg, name) {
if (arg !== null && typeof arg !== 'object') {
throw new TypeError(`The argument '${name}' must be a object, instead got a '${typeof arg}'`)
}
}
// not using fastify-plugin, to fully encapsulate under-pressure plugin
module.exports = fastifyHealthcheck
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc