Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

under-pressure

Package Overview
Dependencies
Maintainers
13
Versions
25
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

under-pressure - npm Package Compare versions

Comparing version 5.6.0 to 5.7.0

3

index.d.ts
import {
FastifyInstance,
FastifyPlugin,

@@ -21,3 +22,3 @@ FastifyReply,

retryAfter?: number;
healthCheck?: () => Promise<boolean>;
healthCheck?: (fastify: FastifyInstance) => Promise<Record<string, unknown> | boolean>;
healthCheckInterval?: number;

@@ -24,0 +25,0 @@ pressureHandler?: (request: FastifyRequest, reply: FastifyReply, type: string, value: number | undefined) => Promise<void> | void;

@@ -62,2 +62,4 @@ 'use strict'

fastify.decorate('memoryUsage', memoryUsage)
const timer = setInterval(updateMemoryUsage, sampleInterval)

@@ -74,3 +76,3 @@ timer.unref()

try {
externalsHealthy = await healthCheck()
externalsHealthy = await healthCheck(fastify)
} catch (error) {

@@ -92,3 +94,2 @@ externalsHealthy = false

fastify.decorate('memoryUsage', memoryUsage)
fastify.addHook('onClose', onClose)

@@ -225,3 +226,3 @@

try {
const checkResult = await healthCheck()
const checkResult = await healthCheck(fastify)
if (!checkResult) {

@@ -228,0 +229,0 @@ req.log.error('external health check failed')

@@ -41,5 +41,5 @@ import underPressure from ".";

server.register(underPressure, {
healthCheck: async function () {
healthCheck: async function (fastifyInstance) {
// do some magic to check if your db connection is healthy, etc...
return true;
return fastifyInstance.register === server.register;
},

@@ -46,0 +46,0 @@ healthCheckInterval: 500

{
"name": "under-pressure",
"version": "5.6.0",
"version": "5.7.0",
"description": "Measure process load with automatic handling of 'Service Unavailable' plugin for Fastify.",

@@ -33,3 +33,3 @@ "main": "index.js",

"devDependencies": {
"@types/node": "^14.0.1",
"@types/node": "^15.0.1",
"fastify": "^3.0.0",

@@ -41,3 +41,3 @@ "pre-commit": "^1.2.2",

"standard": "^16.0.0",
"tap": "^14.10.7",
"tap": "^15.0.3",
"tsd": "^0.14.0",

@@ -44,0 +44,0 @@ "typescript": "^4.0.3"

# under-pressure
[![Build Status](https://img.shields.io/github/workflow/status/fastify/under-pressure/CI)](https://github.com/fastify/under-pressure/actions)
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](http://standardjs.com/)
![CI](https://github.com/fastify/under-pressure/workflows/CI/badge.svg)
[![NPM version](https://img.shields.io/npm/v/under-pressure.svg?style=flat)](https://www.npmjs.com/package/under-pressure)
[![Known Vulnerabilities](https://snyk.io/test/github/fastify/under-pressure/badge.svg)](https://snyk.io/test/github/fastify/under-pressure)
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](https://standardjs.com/)
Measure process load with automatic handling of *"Service Unavailable"* plugin for Fastify.
It can check `maxEventLoopDelay`, `maxHeapUsedBytes`, `maxRssBytes` and `maxEventLoopUtilization` values.
You can also specify custom health check, to verify the status of
You can also specify a custom health check, to verify the status of
external resources.

@@ -73,3 +75,3 @@

Since [`eventLoopUtilization`](https://nodejs.org/api/perf_hooks.html#perf_hooks_performance_eventlooputilization_utilization1_utilization2) is only available in Node version 14.0.0 and 12.19.0 the check will be disbaled in other versions.
Since [`eventLoopUtilization`](https://nodejs.org/api/perf_hooks.html#perf_hooks_performance_eventlooputilization_utilization1_utilization2) is only available in Node version 14.0.0 and 12.19.0 the check will be disabled in other versions.

@@ -86,3 +88,3 @@ Thanks to the encapsulation model of Fastify, you can selectively use this plugin in some subset of routes or even with different thresholds in different plugins.

You can provide a pressure handler in the options to handle the pressure errors. The advantage is that you know for which reason the error occured. And the request can be handled as if nothing happened.
You can provide a pressure handler in the options to handle the pressure errors. The advantage is that you know why the error occurred. Moreover, the request can be handled as if nothing happened.

@@ -108,3 +110,3 @@ ```js

It is possible as well to return a Promise which will call `reply.send` (or something else).
It is possible as well to return a Promise that will call `reply.send` (or something else).

@@ -155,8 +157,18 @@ ```js

extraValue: { type: 'string' },
metrics: {
type: 'object',
properties: {
eventLoopDelay: { type: 'number' },
rssBytes: { type: 'number' },
heapUsed: { type: 'number' },
eventLoopUtilized: { type: 'number' },
},
},
// ...
}
},
healthCheck: async () => {
healthCheck: async (fastifyInstance) => {
return {
extraValue: await getExtraValue(),
metrics: fastifyInstance.memoryUsage(),
// ...

@@ -169,5 +181,5 @@ }

#### Custom health checks
If needed you can pass a custom `healthCheck` property which is an async function and `under-pressure` will allow you to check the status of other components of your service.
If needed you can pass a custom `healthCheck` property, which is an async function, and `under-pressure` will allow you to check the status of other components of your service.
This function should return a promise which resolves to a boolean value or to an object. The `healthCheck` function can be called either:
This function should return a promise that resolves to a boolean value or to an object. The `healthCheck` function can be called either:

@@ -185,3 +197,3 @@ * every X milliseconds, the time can be

fastify.register(require('under-pressure'), {
healthCheck: async function () {
healthCheck: async function (fastifyInstance) {
// do some magic to check if your db connection is healthy, etc...

@@ -198,3 +210,3 @@ return true

The default value is different depending on which Node version is used. On version 8 and 10 it is `5`, while on version 11.10.0 and up it is `1000`. This difference is due to the fact that from version 11.10.0 the event loop delay can be sampled with [`monitorEventLoopDelay`](https://nodejs.org/docs/latest-v12.x/api/perf_hooks.html#perf_hooks_perf_hooks_monitoreventloopdelay_options) and this allows to increase the interval value.
The default value is different depending on which Node version is used. In version 8 and 10 it is `5`, while on version 11.10.0 and up it is `1000`. This difference is because from version 11.10.0 the event loop delay can be sampled with [`monitorEventLoopDelay`](https://nodejs.org/docs/latest-v12.x/api/perf_hooks.html#perf_hooks_perf_hooks_monitoreventloopdelay_options) and this allows to increase the interval value.

@@ -214,3 +226,3 @@ ```js

This project is kindly sponsored by [LetzDoIt](http://www.letzdoitapp.com/).
This project is kindly sponsored by [LetzDoIt](https://www.letzdoitapp.com/).

@@ -217,0 +229,0 @@ <a name="license"></a>

@@ -34,4 +34,4 @@ 'use strict'

t.error(err)
t.strictEqual(response.statusCode, 200)
t.deepEqual(JSON.parse(body), { status: 'ok' })
t.equal(response.statusCode, 200)
t.same(JSON.parse(body), { status: 'ok' })
fastify.close()

@@ -46,3 +46,3 @@ })

const fastify = Fastify()
t.tearDown(() => fastify.close())
t.teardown(() => fastify.close())

@@ -57,3 +57,3 @@ fastify.register(underPressure, {

t.error(err)
t.strictEqual(response.statusCode, 404)
t.equal(response.statusCode, 404)
})

@@ -65,4 +65,4 @@

t.error(err)
t.strictEqual(response.statusCode, 200)
t.deepEqual(JSON.parse(response.payload), { status: 'ok' })
t.equal(response.statusCode, 200)
t.same(JSON.parse(response.payload), { status: 'ok' })
})

@@ -82,5 +82,5 @@ })

process.nextTick(() => block(500))
t.strictEqual(routeOptions.url, '/alive')
t.strictEqual(routeOptions.logLevel, 'silent', 'log level not set')
t.deepEqual(routeOptions.config, customConfig, 'config not set')
t.equal(routeOptions.url, '/alive')
t.equal(routeOptions.logLevel, 'silent', 'log level not set')
t.same(routeOptions.config, customConfig, 'config not set')
fastify.close()

@@ -110,4 +110,4 @@ })

process.nextTick(() => block(500))
t.strictEqual(routeOptions.url, '/status')
t.strictEqual(routeOptions.logLevel, 'silent', 'log level not set')
t.equal(routeOptions.url, '/status')
t.equal(routeOptions.logLevel, 'silent', 'log level not set')
fastify.close()

@@ -135,5 +135,5 @@ })

process.nextTick(() => block(500))
t.strictEqual(routeOptions.url, '/alive')
t.strictEqual(routeOptions.logLevel, 'silent', 'log level not set')
t.deepEqual(routeOptions.schema, Object.assign({}, routeSchemaOpts, {
t.equal(routeOptions.url, '/alive')
t.equal(routeOptions.logLevel, 'silent', 'log level not set')
t.same(routeOptions.schema, Object.assign({}, routeSchemaOpts, {
response: {

@@ -173,5 +173,5 @@ 200: {

process.nextTick(() => block(500))
t.strictEqual(routeOptions.url, '/status')
t.strictEqual(routeOptions.logLevel, 'silent', 'log level not set')
t.deepEqual(routeOptions.schema, Object.assign({}, routeSchemaOpts, {
t.equal(routeOptions.url, '/status')
t.equal(routeOptions.logLevel, 'silent', 'log level not set')
t.same(routeOptions.schema, Object.assign({}, routeSchemaOpts, {
response: {

@@ -178,0 +178,0 @@ 200: {

@@ -43,5 +43,5 @@ 'use strict'

t.error(err)
t.strictEqual(response.statusCode, 503)
t.strictEqual(response.headers['retry-after'], '10')
t.deepEqual(JSON.parse(body), {
t.equal(response.statusCode, 503)
t.equal(response.headers['retry-after'], '10')
t.same(JSON.parse(body), {
code: 'FST_UNDER_PRESSURE',

@@ -81,5 +81,5 @@ error: 'Service Unavailable',

t.error(err)
t.strictEqual(response.statusCode, 503)
t.strictEqual(response.headers['retry-after'], '10')
t.deepEqual(JSON.parse(body), {
t.equal(response.statusCode, 503)
t.equal(response.headers['retry-after'], '10')
t.same(JSON.parse(body), {
code: 'FST_UNDER_PRESSURE',

@@ -118,5 +118,5 @@ error: 'Service Unavailable',

t.error(err)
t.strictEqual(response.statusCode, 503)
t.strictEqual(response.headers['retry-after'], '10')
t.deepEqual(JSON.parse(body), {
t.equal(response.statusCode, 503)
t.equal(response.headers['retry-after'], '10')
t.same(JSON.parse(body), {
code: 'FST_UNDER_PRESSURE',

@@ -155,5 +155,5 @@ error: 'Service Unavailable',

t.error(err)
t.strictEqual(response.statusCode, 503)
t.strictEqual(response.headers['retry-after'], '10')
t.deepEqual(JSON.parse(body), {
t.equal(response.statusCode, 503)
t.equal(response.headers['retry-after'], '10')
t.same(JSON.parse(body), {
code: 'FST_UNDER_PRESSURE',

@@ -194,5 +194,5 @@ error: 'Service Unavailable',

t.error(err)
t.strictEqual(response.statusCode, 503)
t.strictEqual(response.headers['retry-after'], '50')
t.deepEqual(JSON.parse(body), {
t.equal(response.statusCode, 503)
t.equal(response.headers['retry-after'], '50')
t.same(JSON.parse(body), {
code: 'FST_UNDER_PRESSURE',

@@ -246,4 +246,4 @@ error: 'Service Unavailable',

t.error(err)
t.strictEqual(response.statusCode, 418)
t.deepEqual(JSON.parse(body), {
t.equal(response.statusCode, 418)
t.same(JSON.parse(body), {
code: 'FST_CUSTOM_ERROR',

@@ -271,6 +271,6 @@ error: 'I\'m a Teapot',

fastify.get('/', (req, reply) => {
t.true(fastify.memoryUsage().eventLoopDelay > 0)
t.true(fastify.memoryUsage().heapUsed > 0)
t.true(fastify.memoryUsage().rssBytes > 0)
t.true(fastify.memoryUsage().eventLoopUtilized >= 0)
t.ok(fastify.memoryUsage().eventLoopDelay > 0)
t.ok(fastify.memoryUsage().heapUsed > 0)
t.ok(fastify.memoryUsage().rssBytes > 0)
t.ok(fastify.memoryUsage().eventLoopUtilized >= 0)
reply.send({ hello: 'world' })

@@ -281,3 +281,3 @@ })

t.error(err)
t.is(typeof fastify.memoryUsage, 'function')
t.equal(typeof fastify.memoryUsage, 'function')
fastify.server.unref()

@@ -298,4 +298,4 @@

t.error(err)
t.strictEqual(response.statusCode, 200)
t.deepEqual(JSON.parse(body), { hello: 'world' })
t.equal(response.statusCode, 200)
t.same(JSON.parse(body), { hello: 'world' })
fastify.close()

@@ -313,6 +313,6 @@ })

fastify.get('/', (req, reply) => {
t.true(fastify.memoryUsage().eventLoopDelay > 0)
t.true(fastify.memoryUsage().heapUsed > 0)
t.true(fastify.memoryUsage().rssBytes > 0)
t.true(fastify.memoryUsage().eventLoopUtilized >= 0)
t.ok(fastify.memoryUsage().eventLoopDelay > 0)
t.ok(fastify.memoryUsage().heapUsed > 0)
t.ok(fastify.memoryUsage().rssBytes > 0)
t.ok(fastify.memoryUsage().eventLoopUtilized >= 0)
reply.send({ hello: 'world' })

@@ -323,3 +323,3 @@ })

t.error(err)
t.is(typeof fastify.memoryUsage, 'function')
t.equal(typeof fastify.memoryUsage, 'function')
fastify.server.unref()

@@ -340,4 +340,4 @@

t.error(err)
t.strictEqual(response.statusCode, 200)
t.deepEqual(JSON.parse(body), { hello: 'world' })
t.equal(response.statusCode, 200)
t.same(JSON.parse(body), { hello: 'world' })
fastify.close()

@@ -349,3 +349,3 @@ })

test('Custom health check', t => {
t.plan(7)
t.plan(8)

@@ -376,5 +376,5 @@ t.test('should return 503 when custom health check returns false for healthCheck', t => {

t.error(err)
t.strictEqual(response.statusCode, 503)
t.strictEqual(response.headers['retry-after'], '10')
t.deepEqual(JSON.parse(body), {
t.equal(response.statusCode, 503)
t.equal(response.headers['retry-after'], '10')
t.same(JSON.parse(body), {
code: 'FST_UNDER_PRESSURE',

@@ -412,4 +412,4 @@ error: 'Service Unavailable',

t.error(err)
t.strictEqual(response.statusCode, 200)
t.deepEqual(JSON.parse(body), {
t.equal(response.statusCode, 200)
t.same(JSON.parse(body), {
hello: 'world'

@@ -446,4 +446,4 @@ })

t.error(err)
t.strictEqual(response.statusCode, 200)
t.deepEqual(JSON.parse(body), {
t.equal(response.statusCode, 200)
t.same(JSON.parse(body), {
hello: 'world'

@@ -459,5 +459,5 @@ })

t.error(err)
t.strictEqual(response.statusCode, 503)
t.strictEqual(response.headers['retry-after'], '10')
t.deepEqual(JSON.parse(body), {
t.equal(response.statusCode, 503)
t.equal(response.headers['retry-after'], '10')
t.same(JSON.parse(body), {
code: 'FST_UNDER_PRESSURE',

@@ -482,3 +482,3 @@ error: 'Service Unavailable',

await wait(100)
t.false(called)
t.notOk(called)
called = true

@@ -491,3 +491,3 @@ },

t.error(err)
t.true(called)
t.ok(called)
fastify.close()

@@ -520,5 +520,5 @@ })

t.error(err)
t.strictEqual(response.statusCode, 503)
t.strictEqual(response.headers['retry-after'], '10')
t.deepEqual(JSON.parse(body), {
t.equal(response.statusCode, 503)
t.equal(response.headers['retry-after'], '10')
t.same(JSON.parse(body), {
code: 'FST_UNDER_PRESSURE',

@@ -560,5 +560,5 @@ error: 'Service Unavailable',

t.error(err)
t.strictEqual(response.statusCode, 503)
t.strictEqual(response.headers['retry-after'], '10')
t.deepEqual(JSON.parse(body), {
t.equal(response.statusCode, 503)
t.equal(response.headers['retry-after'], '10')
t.same(JSON.parse(body), {
code: 'FST_UNDER_PRESSURE',

@@ -604,4 +604,4 @@ error: 'Service Unavailable',

t.error(err)
t.strictEqual(response.statusCode, 200)
t.deepEqual(JSON.parse(body), {
t.equal(response.statusCode, 200)
t.same(JSON.parse(body), {
some: 'value',

@@ -615,2 +615,40 @@ anotherValue: 'another',

})
t.test('should be fastify instance as argument in the healthCheck function', t => {
t.plan(6)
const fastify = Fastify()
fastify.register(underPressure, {
healthCheck: async (fastifyInstance) => {
t.pass('healthcheck called')
return {
fastifyInstanceOk: fastifyInstance === fastify,
status: 'overrride status'
}
},
exposeStatusRoute: {
routeResponseSchemaOpts: {
fastifyInstanceOk: { type: 'boolean' }
}
}
})
fastify.listen(0, (err, address) => {
t.error(err)
fastify.server.unref()
sget({
method: 'GET',
url: address + '/status'
}, (err, response, body) => {
t.error(err)
t.equal(response.statusCode, 200)
t.same(JSON.parse(body), {
fastifyInstanceOk: true,
status: 'overrride status'
})
fastify.close()
})
})
})
})

@@ -617,0 +655,0 @@

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

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