diff --git a/index.js b/index.js index 058b6a7..6d29b8c 100644 --- a/index.js +++ b/index.js @@ -21,7 +21,7 @@ function getSampleInterval (value, eventLoopResolution) { return monitorEventLoopDelay ? Math.max(eventLoopResolution, sampleInterval) : sampleInterval } -async function underPressure (fastify, opts) { +async function fastifyUnderPressure (fastify, opts) { opts = opts || {} const resolution = 10 @@ -261,10 +261,12 @@ function now () { return (ts[0] * 1e3) + (ts[1] / 1e6) } -module.exports = fp(underPressure, { +module.exports = fp(fastifyUnderPressure, { fastify: '4.x', name: '@fastify/under-pressure' }) +module.exports.default = fastifyUnderPressure +module.exports.fastifyUnderPressure = fastifyUnderPressure module.exports.TYPE_EVENT_LOOP_DELAY = TYPE_EVENT_LOOP_DELAY module.exports.TYPE_EVENT_LOOP_UTILIZATION = TYPE_EVENT_LOOP_UTILIZATION diff --git a/test/exports.test.js b/test/exports.test.js new file mode 100644 index 0000000..a9d95f7 --- /dev/null +++ b/test/exports.test.js @@ -0,0 +1,33 @@ +'use strict' + +const { test } = require('tap') +const fastifyUnderPressure = require('..') +const { TYPE_EVENT_LOOP_DELAY, TYPE_EVENT_LOOP_UTILIZATION, TYPE_HEALTH_CHECK, TYPE_HEAP_USED_BYTES, TYPE_RSS_BYTES } = require('..') + +test('module.exports', t => { + t.plan(20) + + t.equal(typeof fastifyUnderPressure.default, 'function') + t.equal(typeof fastifyUnderPressure.fastifyUnderPressure, 'function') + t.equal(fastifyUnderPressure.fastifyUnderPressure.name, 'fastifyUnderPressure') + t.equal(fastifyUnderPressure.fastifyUnderPressure, fastifyUnderPressure.fastifyUnderPressure.fastifyUnderPressure) + t.equal(fastifyUnderPressure.fastifyUnderPressure.default, fastifyUnderPressure.fastifyUnderPressure.fastifyUnderPressure) + + t.equal(TYPE_EVENT_LOOP_DELAY, 'eventLoopDelay') + t.equal(TYPE_EVENT_LOOP_UTILIZATION, 'eventLoopUtilization') + t.equal(TYPE_HEALTH_CHECK, 'healthCheck') + t.equal(TYPE_HEAP_USED_BYTES, 'heapUsedBytes') + t.equal(TYPE_RSS_BYTES, 'rssBytes') + + t.equal(fastifyUnderPressure.TYPE_RSS_BYTES, 'rssBytes') + t.equal(fastifyUnderPressure.TYPE_EVENT_LOOP_DELAY, 'eventLoopDelay') + t.equal(fastifyUnderPressure.TYPE_EVENT_LOOP_UTILIZATION, 'eventLoopUtilization') + t.equal(fastifyUnderPressure.TYPE_HEALTH_CHECK, 'healthCheck') + t.equal(fastifyUnderPressure.TYPE_HEAP_USED_BYTES, 'heapUsedBytes') + + t.equal(fastifyUnderPressure.fastifyUnderPressure.TYPE_EVENT_LOOP_DELAY, 'eventLoopDelay') + t.equal(fastifyUnderPressure.fastifyUnderPressure.TYPE_EVENT_LOOP_UTILIZATION, 'eventLoopUtilization') + t.equal(fastifyUnderPressure.fastifyUnderPressure.TYPE_HEALTH_CHECK, 'healthCheck') + t.equal(fastifyUnderPressure.fastifyUnderPressure.TYPE_HEAP_USED_BYTES, 'heapUsedBytes') + t.equal(fastifyUnderPressure.fastifyUnderPressure.TYPE_RSS_BYTES, 'rssBytes') +}) diff --git a/types/index.d.ts b/types/index.d.ts index 4c5b5b5..9c374ef 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -1,18 +1,28 @@ import { FastifyInstance, - FastifyPluginCallback, + FastifyPluginAsync, FastifyReply, FastifyRequest } from "fastify"; -export const TYPE_EVENT_LOOP_DELAY = 'eventLoopDelay' -export const TYPE_HEAP_USED_BYTES = 'heapUsedBytes' -export const TYPE_RSS_BYTES = 'rssBytes' -export const TYPE_HEALTH_CHECK = 'healthCheck' -export const TYPE_EVENT_LOOP_UTILIZATION = 'eventLoopUtilization' +declare module "fastify" { + interface FastifyInstance { + memoryUsage(): { heapUsed: number; rssBytes: number; eventLoopDelay: number; eventLoopUtilized: number }; + } +} + +interface FastifyUnderPressureExports { + TYPE_EVENT_LOOP_DELAY: 'eventLoopDelay' + TYPE_HEAP_USED_BYTES: 'heapUsedBytes' + TYPE_RSS_BYTES: 'rssBytes' + TYPE_HEALTH_CHECK: 'healthCheck' + TYPE_EVENT_LOOP_UTILIZATION: 'eventLoopUtilization' +} + +type FastifyUnderPressure = FastifyPluginAsync & FastifyUnderPressureExports -declare namespace underPressure { - interface UnderPressureOptions { +declare namespace fastifyUnderPressure { + export interface UnderPressureOptions { maxEventLoopDelay?: number; maxEventLoopUtilization?: number; maxHeapUsedBytes?: number; @@ -26,16 +36,16 @@ declare namespace underPressure { exposeStatusRoute?: boolean | string | { routeOpts: object; routeSchemaOpts?: object; routeResponseSchemaOpts?: object; url?: string }; customError?: Error; } -} -declare module "fastify" { - interface FastifyInstance { - memoryUsage(): { heapUsed: number; rssBytes: number; eventLoopDelay: number; eventLoopUtilized: number }; - } -} + export const TYPE_EVENT_LOOP_DELAY = 'eventLoopDelay' + export const TYPE_HEAP_USED_BYTES = 'heapUsedBytes' + export const TYPE_RSS_BYTES = 'rssBytes' + export const TYPE_HEALTH_CHECK = 'healthCheck' + export const TYPE_EVENT_LOOP_UTILIZATION = 'eventLoopUtilization' -declare let underPressure: FastifyPluginCallback< - underPressure.UnderPressureOptions ->; + export const fastifyUnderPressure: FastifyUnderPressure + export { fastifyUnderPressure as default } +} -export default underPressure; +declare function fastifyUnderPressure(...params: Parameters): ReturnType +export = fastifyUnderPressure diff --git a/types/index.test-d.ts b/types/index.test-d.ts index 965d6ad..0b2ac8c 100644 --- a/types/index.test-d.ts +++ b/types/index.test-d.ts @@ -1,16 +1,17 @@ -import underPressure from ".."; +import fastifyUnderPressure, { fastifyUnderPressure as namedFastifyUnderPressure, TYPE_EVENT_LOOP_DELAY, TYPE_EVENT_LOOP_UTILIZATION, TYPE_HEALTH_CHECK, TYPE_HEAP_USED_BYTES, TYPE_RSS_BYTES } from ".."; import fastify from "fastify"; +import { expectType } from "tsd"; const server = fastify(); () => { - server.register(underPressure, { + server.register(fastifyUnderPressure, { maxEventLoopDelay: 1000, maxHeapUsedBytes: 100000000, maxRssBytes: 100000000 }); - server.register(underPressure); + server.register(fastifyUnderPressure); server.get("/", (req, reply) => { reply.send({ hello: "world" }); @@ -22,7 +23,7 @@ const server = fastify(); }; () => { - server.register(underPressure, { + server.register(fastifyUnderPressure, { maxEventLoopDelay: 1000, message: "Under pressure!", retryAfter: 50 @@ -37,7 +38,7 @@ const server = fastify(); }; () => { - server.register(underPressure, { + server.register(fastifyUnderPressure, { healthCheck: async function (fastifyInstance) { // do some magic to check if your db connection is healthy, etc... return fastifyInstance.register === server.register; @@ -47,21 +48,21 @@ const server = fastify(); }; () => { - server.register(underPressure, { + server.register(fastifyUnderPressure, { sampleInterval: 10 }); } () => { - server.register(underPressure, { + server.register(fastifyUnderPressure, { exposeStatusRoute: '/v2/status', }); - server.register(underPressure, { + server.register(fastifyUnderPressure, { exposeStatusRoute: true }); - server.register(underPressure, { + server.register(fastifyUnderPressure, { exposeStatusRoute: { routeOpts: { logLevel: 'silent', @@ -71,7 +72,7 @@ const server = fastify(); } }); - server.register(underPressure, { + server.register(fastifyUnderPressure, { exposeStatusRoute: { routeOpts: { logLevel: 'silent' @@ -79,7 +80,7 @@ const server = fastify(); } }); - server.register(underPressure, { + server.register(fastifyUnderPressure, { exposeStatusRoute: { routeOpts: { logLevel: 'silent' @@ -90,7 +91,26 @@ const server = fastify(); } }) - server.register(underPressure, { + server.register(fastifyUnderPressure, { customError: new Error('custom error message') }); }; + +expectType<'eventLoopDelay'>(fastifyUnderPressure.TYPE_EVENT_LOOP_DELAY) +expectType<'heapUsedBytes'>(fastifyUnderPressure.TYPE_HEAP_USED_BYTES) +expectType<'rssBytes'>(fastifyUnderPressure.TYPE_RSS_BYTES) +expectType<'healthCheck'>(fastifyUnderPressure.TYPE_HEALTH_CHECK) +expectType<'eventLoopUtilization'>(fastifyUnderPressure.TYPE_EVENT_LOOP_UTILIZATION) + +expectType<'eventLoopDelay'>(namedFastifyUnderPressure.TYPE_EVENT_LOOP_DELAY) +expectType<'heapUsedBytes'>(namedFastifyUnderPressure.TYPE_HEAP_USED_BYTES) +expectType<'rssBytes'>(namedFastifyUnderPressure.TYPE_RSS_BYTES) +expectType<'healthCheck'>(namedFastifyUnderPressure.TYPE_HEALTH_CHECK) +expectType<'eventLoopUtilization'>(namedFastifyUnderPressure.TYPE_EVENT_LOOP_UTILIZATION) + +expectType<'eventLoopDelay'>(TYPE_EVENT_LOOP_DELAY) +expectType<'heapUsedBytes'>(TYPE_HEAP_USED_BYTES) +expectType<'rssBytes'>(TYPE_RSS_BYTES) +expectType<'healthCheck'>(TYPE_HEALTH_CHECK) +expectType<'eventLoopUtilization'>(TYPE_EVENT_LOOP_UTILIZATION) +