Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nodenext compatibility #187

Merged
merged 3 commits into from
Nov 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
33 changes: 33 additions & 0 deletions test/exports.test.js
Original file line number Diff line number Diff line change
@@ -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')
})
46 changes: 28 additions & 18 deletions types/index.d.ts
Original file line number Diff line number Diff line change
@@ -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<fastifyUnderPressure.UnderPressureOptions> & FastifyUnderPressureExports

declare namespace underPressure {
interface UnderPressureOptions {
declare namespace fastifyUnderPressure {
export interface UnderPressureOptions {
maxEventLoopDelay?: number;
maxEventLoopUtilization?: number;
maxHeapUsedBytes?: number;
Expand All @@ -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<FastifyUnderPressure>): ReturnType<FastifyUnderPressure>
export = fastifyUnderPressure
44 changes: 32 additions & 12 deletions types/index.test-d.ts
Original file line number Diff line number Diff line change
@@ -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" });
Expand All @@ -22,7 +23,7 @@ const server = fastify();
};

() => {
server.register(underPressure, {
server.register(fastifyUnderPressure, {
maxEventLoopDelay: 1000,
message: "Under pressure!",
retryAfter: 50
Expand All @@ -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;
Expand All @@ -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',
Expand All @@ -71,15 +72,15 @@ const server = fastify();
}
});

server.register(underPressure, {
server.register(fastifyUnderPressure, {
exposeStatusRoute: {
routeOpts: {
logLevel: 'silent'
}
}
});

server.register(underPressure, {
server.register(fastifyUnderPressure, {
exposeStatusRoute: {
routeOpts: {
logLevel: 'silent'
Expand All @@ -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)