@firebreak/vitals 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/checks/postgres.cjs +89 -0
- package/dist/checks/postgres.d.cts +15 -0
- package/dist/checks/postgres.d.ts +15 -0
- package/dist/checks/postgres.mjs +53 -0
- package/dist/checks/redis.cjs +95 -0
- package/dist/checks/redis.d.cts +15 -0
- package/dist/checks/redis.d.ts +15 -0
- package/dist/checks/redis.mjs +59 -0
- package/dist/core-9-MXAO0I.d.cts +55 -0
- package/dist/core-9-MXAO0I.d.ts +55 -0
- package/dist/index.cjs +200 -0
- package/dist/index.d.cts +10 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.mjs +165 -0
- package/dist/integrations/express.cjs +105 -0
- package/dist/integrations/express.d.cts +11 -0
- package/dist/integrations/express.d.ts +11 -0
- package/dist/integrations/express.mjs +78 -0
- package/package.json +93 -0
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/checks/postgres.ts
|
|
31
|
+
var postgres_exports = {};
|
|
32
|
+
__export(postgres_exports, {
|
|
33
|
+
pgClientCheck: () => pgClientCheck,
|
|
34
|
+
pgPoolCheck: () => pgPoolCheck
|
|
35
|
+
});
|
|
36
|
+
module.exports = __toCommonJS(postgres_exports);
|
|
37
|
+
var import_node_perf_hooks = require("perf_hooks");
|
|
38
|
+
|
|
39
|
+
// src/types.ts
|
|
40
|
+
var Status = {
|
|
41
|
+
HEALTHY: 2,
|
|
42
|
+
DEGRADED: 1,
|
|
43
|
+
OUTAGE: 0
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
// src/checks/postgres.ts
|
|
47
|
+
function pgClientCheck(connectionString, options) {
|
|
48
|
+
return async () => {
|
|
49
|
+
const start = import_node_perf_hooks.performance.now();
|
|
50
|
+
const { Client } = await import("pg");
|
|
51
|
+
const client = new Client({ connectionString, ...options });
|
|
52
|
+
try {
|
|
53
|
+
await client.connect();
|
|
54
|
+
await client.query("SELECT 1");
|
|
55
|
+
const latencyMs = Math.round(import_node_perf_hooks.performance.now() - start);
|
|
56
|
+
return { status: Status.HEALTHY, latencyMs, message: "" };
|
|
57
|
+
} catch (error) {
|
|
58
|
+
const latencyMs = Math.round(import_node_perf_hooks.performance.now() - start);
|
|
59
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
60
|
+
return { status: Status.OUTAGE, latencyMs, message };
|
|
61
|
+
} finally {
|
|
62
|
+
client.end().catch(() => {
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
function pgPoolCheck(pool) {
|
|
68
|
+
return async () => {
|
|
69
|
+
const start = import_node_perf_hooks.performance.now();
|
|
70
|
+
let client;
|
|
71
|
+
try {
|
|
72
|
+
client = await pool.connect();
|
|
73
|
+
await client.query("SELECT 1");
|
|
74
|
+
const latencyMs = Math.round(import_node_perf_hooks.performance.now() - start);
|
|
75
|
+
return { status: Status.HEALTHY, latencyMs, message: "" };
|
|
76
|
+
} catch (error) {
|
|
77
|
+
const latencyMs = Math.round(import_node_perf_hooks.performance.now() - start);
|
|
78
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
79
|
+
return { status: Status.OUTAGE, latencyMs, message };
|
|
80
|
+
} finally {
|
|
81
|
+
client?.release();
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
86
|
+
0 && (module.exports = {
|
|
87
|
+
pgClientCheck,
|
|
88
|
+
pgPoolCheck
|
|
89
|
+
});
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { A as AsyncCheckFn } from '../core-9-MXAO0I.cjs';
|
|
2
|
+
import { Pool } from 'pg';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Creates a healthcheck function that opens a fresh pg Client connection,
|
|
6
|
+
* runs `SELECT 1`, and closes the connection.
|
|
7
|
+
*/
|
|
8
|
+
declare function pgClientCheck(connectionString: string, options?: Record<string, unknown>): AsyncCheckFn;
|
|
9
|
+
/**
|
|
10
|
+
* Creates a healthcheck function that borrows a client from an existing pg Pool,
|
|
11
|
+
* runs `SELECT 1`, and releases the client back to the pool.
|
|
12
|
+
*/
|
|
13
|
+
declare function pgPoolCheck(pool: Pool): AsyncCheckFn;
|
|
14
|
+
|
|
15
|
+
export { AsyncCheckFn, pgClientCheck, pgPoolCheck };
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { A as AsyncCheckFn } from '../core-9-MXAO0I.js';
|
|
2
|
+
import { Pool } from 'pg';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Creates a healthcheck function that opens a fresh pg Client connection,
|
|
6
|
+
* runs `SELECT 1`, and closes the connection.
|
|
7
|
+
*/
|
|
8
|
+
declare function pgClientCheck(connectionString: string, options?: Record<string, unknown>): AsyncCheckFn;
|
|
9
|
+
/**
|
|
10
|
+
* Creates a healthcheck function that borrows a client from an existing pg Pool,
|
|
11
|
+
* runs `SELECT 1`, and releases the client back to the pool.
|
|
12
|
+
*/
|
|
13
|
+
declare function pgPoolCheck(pool: Pool): AsyncCheckFn;
|
|
14
|
+
|
|
15
|
+
export { AsyncCheckFn, pgClientCheck, pgPoolCheck };
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
// src/checks/postgres.ts
|
|
2
|
+
import { performance } from "perf_hooks";
|
|
3
|
+
|
|
4
|
+
// src/types.ts
|
|
5
|
+
var Status = {
|
|
6
|
+
HEALTHY: 2,
|
|
7
|
+
DEGRADED: 1,
|
|
8
|
+
OUTAGE: 0
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
// src/checks/postgres.ts
|
|
12
|
+
function pgClientCheck(connectionString, options) {
|
|
13
|
+
return async () => {
|
|
14
|
+
const start = performance.now();
|
|
15
|
+
const { Client } = await import("pg");
|
|
16
|
+
const client = new Client({ connectionString, ...options });
|
|
17
|
+
try {
|
|
18
|
+
await client.connect();
|
|
19
|
+
await client.query("SELECT 1");
|
|
20
|
+
const latencyMs = Math.round(performance.now() - start);
|
|
21
|
+
return { status: Status.HEALTHY, latencyMs, message: "" };
|
|
22
|
+
} catch (error) {
|
|
23
|
+
const latencyMs = Math.round(performance.now() - start);
|
|
24
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
25
|
+
return { status: Status.OUTAGE, latencyMs, message };
|
|
26
|
+
} finally {
|
|
27
|
+
client.end().catch(() => {
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
function pgPoolCheck(pool) {
|
|
33
|
+
return async () => {
|
|
34
|
+
const start = performance.now();
|
|
35
|
+
let client;
|
|
36
|
+
try {
|
|
37
|
+
client = await pool.connect();
|
|
38
|
+
await client.query("SELECT 1");
|
|
39
|
+
const latencyMs = Math.round(performance.now() - start);
|
|
40
|
+
return { status: Status.HEALTHY, latencyMs, message: "" };
|
|
41
|
+
} catch (error) {
|
|
42
|
+
const latencyMs = Math.round(performance.now() - start);
|
|
43
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
44
|
+
return { status: Status.OUTAGE, latencyMs, message };
|
|
45
|
+
} finally {
|
|
46
|
+
client?.release();
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
export {
|
|
51
|
+
pgClientCheck,
|
|
52
|
+
pgPoolCheck
|
|
53
|
+
};
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/checks/redis.ts
|
|
31
|
+
var redis_exports = {};
|
|
32
|
+
__export(redis_exports, {
|
|
33
|
+
ioredisCheck: () => ioredisCheck,
|
|
34
|
+
ioredisClientCheck: () => ioredisClientCheck
|
|
35
|
+
});
|
|
36
|
+
module.exports = __toCommonJS(redis_exports);
|
|
37
|
+
var import_node_perf_hooks = require("perf_hooks");
|
|
38
|
+
|
|
39
|
+
// src/types.ts
|
|
40
|
+
var Status = {
|
|
41
|
+
HEALTHY: 2,
|
|
42
|
+
DEGRADED: 1,
|
|
43
|
+
OUTAGE: 0
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
// src/checks/redis.ts
|
|
47
|
+
function ioredisCheck(url, options) {
|
|
48
|
+
return async () => {
|
|
49
|
+
const start = import_node_perf_hooks.performance.now();
|
|
50
|
+
const { default: RedisClient } = await import("ioredis");
|
|
51
|
+
const client = new RedisClient(url, {
|
|
52
|
+
lazyConnect: true,
|
|
53
|
+
retryStrategy: () => null,
|
|
54
|
+
enableOfflineQueue: false,
|
|
55
|
+
...options
|
|
56
|
+
});
|
|
57
|
+
try {
|
|
58
|
+
await client.connect();
|
|
59
|
+
const result = await client.ping();
|
|
60
|
+
if (result !== "PONG") {
|
|
61
|
+
throw new Error(`PING returned ${result}`);
|
|
62
|
+
}
|
|
63
|
+
const latencyMs = Math.round(import_node_perf_hooks.performance.now() - start);
|
|
64
|
+
return { status: Status.HEALTHY, latencyMs, message: "" };
|
|
65
|
+
} catch (error) {
|
|
66
|
+
const latencyMs = Math.round(import_node_perf_hooks.performance.now() - start);
|
|
67
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
68
|
+
return { status: Status.OUTAGE, latencyMs, message };
|
|
69
|
+
} finally {
|
|
70
|
+
client.disconnect();
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
function ioredisClientCheck(client) {
|
|
75
|
+
return async () => {
|
|
76
|
+
const start = import_node_perf_hooks.performance.now();
|
|
77
|
+
try {
|
|
78
|
+
const result = await client.ping();
|
|
79
|
+
if (result !== "PONG") {
|
|
80
|
+
throw new Error(`PING returned ${result}`);
|
|
81
|
+
}
|
|
82
|
+
const latencyMs = Math.round(import_node_perf_hooks.performance.now() - start);
|
|
83
|
+
return { status: Status.HEALTHY, latencyMs, message: "" };
|
|
84
|
+
} catch (error) {
|
|
85
|
+
const latencyMs = Math.round(import_node_perf_hooks.performance.now() - start);
|
|
86
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
87
|
+
return { status: Status.OUTAGE, latencyMs, message };
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
92
|
+
0 && (module.exports = {
|
|
93
|
+
ioredisCheck,
|
|
94
|
+
ioredisClientCheck
|
|
95
|
+
});
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { A as AsyncCheckFn } from '../core-9-MXAO0I.cjs';
|
|
2
|
+
import Redis from 'ioredis';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Creates a healthcheck function that opens a fresh ioredis connection,
|
|
6
|
+
* sends PING, validates the PONG response, and disconnects.
|
|
7
|
+
*/
|
|
8
|
+
declare function ioredisCheck(url: string, options?: Record<string, unknown>): AsyncCheckFn;
|
|
9
|
+
/**
|
|
10
|
+
* Creates a healthcheck function that pings an existing (shared) ioredis client.
|
|
11
|
+
* Does NOT disconnect the client.
|
|
12
|
+
*/
|
|
13
|
+
declare function ioredisClientCheck(client: Redis): AsyncCheckFn;
|
|
14
|
+
|
|
15
|
+
export { AsyncCheckFn, ioredisCheck, ioredisClientCheck };
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { A as AsyncCheckFn } from '../core-9-MXAO0I.js';
|
|
2
|
+
import Redis from 'ioredis';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Creates a healthcheck function that opens a fresh ioredis connection,
|
|
6
|
+
* sends PING, validates the PONG response, and disconnects.
|
|
7
|
+
*/
|
|
8
|
+
declare function ioredisCheck(url: string, options?: Record<string, unknown>): AsyncCheckFn;
|
|
9
|
+
/**
|
|
10
|
+
* Creates a healthcheck function that pings an existing (shared) ioredis client.
|
|
11
|
+
* Does NOT disconnect the client.
|
|
12
|
+
*/
|
|
13
|
+
declare function ioredisClientCheck(client: Redis): AsyncCheckFn;
|
|
14
|
+
|
|
15
|
+
export { AsyncCheckFn, ioredisCheck, ioredisClientCheck };
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
// src/checks/redis.ts
|
|
2
|
+
import { performance } from "perf_hooks";
|
|
3
|
+
|
|
4
|
+
// src/types.ts
|
|
5
|
+
var Status = {
|
|
6
|
+
HEALTHY: 2,
|
|
7
|
+
DEGRADED: 1,
|
|
8
|
+
OUTAGE: 0
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
// src/checks/redis.ts
|
|
12
|
+
function ioredisCheck(url, options) {
|
|
13
|
+
return async () => {
|
|
14
|
+
const start = performance.now();
|
|
15
|
+
const { default: RedisClient } = await import("ioredis");
|
|
16
|
+
const client = new RedisClient(url, {
|
|
17
|
+
lazyConnect: true,
|
|
18
|
+
retryStrategy: () => null,
|
|
19
|
+
enableOfflineQueue: false,
|
|
20
|
+
...options
|
|
21
|
+
});
|
|
22
|
+
try {
|
|
23
|
+
await client.connect();
|
|
24
|
+
const result = await client.ping();
|
|
25
|
+
if (result !== "PONG") {
|
|
26
|
+
throw new Error(`PING returned ${result}`);
|
|
27
|
+
}
|
|
28
|
+
const latencyMs = Math.round(performance.now() - start);
|
|
29
|
+
return { status: Status.HEALTHY, latencyMs, message: "" };
|
|
30
|
+
} catch (error) {
|
|
31
|
+
const latencyMs = Math.round(performance.now() - start);
|
|
32
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
33
|
+
return { status: Status.OUTAGE, latencyMs, message };
|
|
34
|
+
} finally {
|
|
35
|
+
client.disconnect();
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
function ioredisClientCheck(client) {
|
|
40
|
+
return async () => {
|
|
41
|
+
const start = performance.now();
|
|
42
|
+
try {
|
|
43
|
+
const result = await client.ping();
|
|
44
|
+
if (result !== "PONG") {
|
|
45
|
+
throw new Error(`PING returned ${result}`);
|
|
46
|
+
}
|
|
47
|
+
const latencyMs = Math.round(performance.now() - start);
|
|
48
|
+
return { status: Status.HEALTHY, latencyMs, message: "" };
|
|
49
|
+
} catch (error) {
|
|
50
|
+
const latencyMs = Math.round(performance.now() - start);
|
|
51
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
52
|
+
return { status: Status.OUTAGE, latencyMs, message };
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
export {
|
|
57
|
+
ioredisCheck,
|
|
58
|
+
ioredisClientCheck
|
|
59
|
+
};
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
declare const Status: {
|
|
2
|
+
readonly HEALTHY: 2;
|
|
3
|
+
readonly DEGRADED: 1;
|
|
4
|
+
readonly OUTAGE: 0;
|
|
5
|
+
};
|
|
6
|
+
type StatusValue = (typeof Status)[keyof typeof Status];
|
|
7
|
+
type StatusLabel = 'healthy' | 'degraded' | 'outage';
|
|
8
|
+
declare function statusToLabel(status: StatusValue): StatusLabel;
|
|
9
|
+
declare function statusFromString(value: string): StatusValue;
|
|
10
|
+
interface CheckResult {
|
|
11
|
+
readonly status: StatusValue;
|
|
12
|
+
readonly latencyMs: number;
|
|
13
|
+
readonly message: string;
|
|
14
|
+
}
|
|
15
|
+
interface HealthcheckResponse {
|
|
16
|
+
readonly status: StatusValue;
|
|
17
|
+
readonly timestamp: string;
|
|
18
|
+
readonly checks: Readonly<Record<string, CheckResult>>;
|
|
19
|
+
}
|
|
20
|
+
interface HealthcheckResponseJson {
|
|
21
|
+
status: StatusLabel;
|
|
22
|
+
timestamp: string;
|
|
23
|
+
checks: Record<string, {
|
|
24
|
+
status: StatusLabel;
|
|
25
|
+
latencyMs: number;
|
|
26
|
+
message: string;
|
|
27
|
+
}>;
|
|
28
|
+
}
|
|
29
|
+
declare function toJson(response: HealthcheckResponse): HealthcheckResponseJson;
|
|
30
|
+
declare function httpStatusCode(status: StatusValue): 200 | 503;
|
|
31
|
+
|
|
32
|
+
type AsyncCheckFn = () => Promise<CheckResult>;
|
|
33
|
+
type SyncCheckFn = () => CheckResult;
|
|
34
|
+
interface RegistryOptions {
|
|
35
|
+
defaultTimeout?: number;
|
|
36
|
+
}
|
|
37
|
+
declare class HealthcheckRegistry {
|
|
38
|
+
private readonly checks;
|
|
39
|
+
private readonly defaultTimeout;
|
|
40
|
+
constructor(options?: RegistryOptions);
|
|
41
|
+
add(name: string, fn: AsyncCheckFn, options?: {
|
|
42
|
+
timeout?: number;
|
|
43
|
+
}): void;
|
|
44
|
+
check(name: string, fn: AsyncCheckFn, options?: {
|
|
45
|
+
timeout?: number;
|
|
46
|
+
}): AsyncCheckFn;
|
|
47
|
+
check(name: string, options?: {
|
|
48
|
+
timeout?: number;
|
|
49
|
+
}): (fn: AsyncCheckFn) => AsyncCheckFn;
|
|
50
|
+
run(): Promise<HealthcheckResponse>;
|
|
51
|
+
private runSingle;
|
|
52
|
+
}
|
|
53
|
+
declare function syncCheck(fn: SyncCheckFn): AsyncCheckFn;
|
|
54
|
+
|
|
55
|
+
export { type AsyncCheckFn as A, type CheckResult as C, HealthcheckRegistry as H, Status as S, type HealthcheckResponse as a, type HealthcheckResponseJson as b, type StatusLabel as c, type StatusValue as d, type SyncCheckFn as e, statusToLabel as f, syncCheck as g, httpStatusCode as h, statusFromString as s, toJson as t };
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
declare const Status: {
|
|
2
|
+
readonly HEALTHY: 2;
|
|
3
|
+
readonly DEGRADED: 1;
|
|
4
|
+
readonly OUTAGE: 0;
|
|
5
|
+
};
|
|
6
|
+
type StatusValue = (typeof Status)[keyof typeof Status];
|
|
7
|
+
type StatusLabel = 'healthy' | 'degraded' | 'outage';
|
|
8
|
+
declare function statusToLabel(status: StatusValue): StatusLabel;
|
|
9
|
+
declare function statusFromString(value: string): StatusValue;
|
|
10
|
+
interface CheckResult {
|
|
11
|
+
readonly status: StatusValue;
|
|
12
|
+
readonly latencyMs: number;
|
|
13
|
+
readonly message: string;
|
|
14
|
+
}
|
|
15
|
+
interface HealthcheckResponse {
|
|
16
|
+
readonly status: StatusValue;
|
|
17
|
+
readonly timestamp: string;
|
|
18
|
+
readonly checks: Readonly<Record<string, CheckResult>>;
|
|
19
|
+
}
|
|
20
|
+
interface HealthcheckResponseJson {
|
|
21
|
+
status: StatusLabel;
|
|
22
|
+
timestamp: string;
|
|
23
|
+
checks: Record<string, {
|
|
24
|
+
status: StatusLabel;
|
|
25
|
+
latencyMs: number;
|
|
26
|
+
message: string;
|
|
27
|
+
}>;
|
|
28
|
+
}
|
|
29
|
+
declare function toJson(response: HealthcheckResponse): HealthcheckResponseJson;
|
|
30
|
+
declare function httpStatusCode(status: StatusValue): 200 | 503;
|
|
31
|
+
|
|
32
|
+
type AsyncCheckFn = () => Promise<CheckResult>;
|
|
33
|
+
type SyncCheckFn = () => CheckResult;
|
|
34
|
+
interface RegistryOptions {
|
|
35
|
+
defaultTimeout?: number;
|
|
36
|
+
}
|
|
37
|
+
declare class HealthcheckRegistry {
|
|
38
|
+
private readonly checks;
|
|
39
|
+
private readonly defaultTimeout;
|
|
40
|
+
constructor(options?: RegistryOptions);
|
|
41
|
+
add(name: string, fn: AsyncCheckFn, options?: {
|
|
42
|
+
timeout?: number;
|
|
43
|
+
}): void;
|
|
44
|
+
check(name: string, fn: AsyncCheckFn, options?: {
|
|
45
|
+
timeout?: number;
|
|
46
|
+
}): AsyncCheckFn;
|
|
47
|
+
check(name: string, options?: {
|
|
48
|
+
timeout?: number;
|
|
49
|
+
}): (fn: AsyncCheckFn) => AsyncCheckFn;
|
|
50
|
+
run(): Promise<HealthcheckResponse>;
|
|
51
|
+
private runSingle;
|
|
52
|
+
}
|
|
53
|
+
declare function syncCheck(fn: SyncCheckFn): AsyncCheckFn;
|
|
54
|
+
|
|
55
|
+
export { type AsyncCheckFn as A, type CheckResult as C, HealthcheckRegistry as H, Status as S, type HealthcheckResponse as a, type HealthcheckResponseJson as b, type StatusLabel as c, type StatusValue as d, type SyncCheckFn as e, statusToLabel as f, syncCheck as g, httpStatusCode as h, statusFromString as s, toJson as t };
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var src_exports = {};
|
|
22
|
+
__export(src_exports, {
|
|
23
|
+
HealthcheckRegistry: () => HealthcheckRegistry,
|
|
24
|
+
Status: () => Status,
|
|
25
|
+
extractToken: () => extractToken,
|
|
26
|
+
httpStatusCode: () => httpStatusCode,
|
|
27
|
+
statusFromString: () => statusFromString,
|
|
28
|
+
statusToLabel: () => statusToLabel,
|
|
29
|
+
syncCheck: () => syncCheck,
|
|
30
|
+
toJson: () => toJson,
|
|
31
|
+
verifyToken: () => verifyToken
|
|
32
|
+
});
|
|
33
|
+
module.exports = __toCommonJS(src_exports);
|
|
34
|
+
|
|
35
|
+
// src/types.ts
|
|
36
|
+
var Status = {
|
|
37
|
+
HEALTHY: 2,
|
|
38
|
+
DEGRADED: 1,
|
|
39
|
+
OUTAGE: 0
|
|
40
|
+
};
|
|
41
|
+
function statusToLabel(status) {
|
|
42
|
+
const labels = {
|
|
43
|
+
[Status.HEALTHY]: "healthy",
|
|
44
|
+
[Status.DEGRADED]: "degraded",
|
|
45
|
+
[Status.OUTAGE]: "outage"
|
|
46
|
+
};
|
|
47
|
+
return labels[status];
|
|
48
|
+
}
|
|
49
|
+
function statusFromString(value) {
|
|
50
|
+
const mapping = {
|
|
51
|
+
healthy: Status.HEALTHY,
|
|
52
|
+
ok: Status.HEALTHY,
|
|
53
|
+
degraded: Status.DEGRADED,
|
|
54
|
+
outage: Status.OUTAGE,
|
|
55
|
+
error: Status.OUTAGE
|
|
56
|
+
};
|
|
57
|
+
return mapping[value.toLowerCase()] ?? Status.OUTAGE;
|
|
58
|
+
}
|
|
59
|
+
function toJson(response) {
|
|
60
|
+
return {
|
|
61
|
+
status: statusToLabel(response.status),
|
|
62
|
+
timestamp: response.timestamp,
|
|
63
|
+
checks: Object.fromEntries(
|
|
64
|
+
Object.entries(response.checks).map(([name, result]) => [
|
|
65
|
+
name,
|
|
66
|
+
{ status: statusToLabel(result.status), latencyMs: result.latencyMs, message: result.message }
|
|
67
|
+
])
|
|
68
|
+
)
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
function httpStatusCode(status) {
|
|
72
|
+
return status === Status.HEALTHY ? 200 : 503;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// src/core.ts
|
|
76
|
+
var import_node_perf_hooks = require("perf_hooks");
|
|
77
|
+
var HealthcheckRegistry = class {
|
|
78
|
+
checks = [];
|
|
79
|
+
defaultTimeout;
|
|
80
|
+
constructor(options) {
|
|
81
|
+
this.defaultTimeout = options?.defaultTimeout ?? 5e3;
|
|
82
|
+
}
|
|
83
|
+
add(name, fn, options) {
|
|
84
|
+
if (this.checks.some((c) => c.name === name)) {
|
|
85
|
+
throw new Error(`Check "${name}" is already registered`);
|
|
86
|
+
}
|
|
87
|
+
this.checks.push({
|
|
88
|
+
name,
|
|
89
|
+
fn,
|
|
90
|
+
timeout: options?.timeout ?? this.defaultTimeout
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
check(name, fnOrOptions, options) {
|
|
94
|
+
if (typeof fnOrOptions === "function") {
|
|
95
|
+
const fn = fnOrOptions;
|
|
96
|
+
this.add(name, fn, options);
|
|
97
|
+
return fn;
|
|
98
|
+
}
|
|
99
|
+
const opts = fnOrOptions;
|
|
100
|
+
return (fn) => {
|
|
101
|
+
this.add(name, fn, opts);
|
|
102
|
+
return fn;
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
async run() {
|
|
106
|
+
if (this.checks.length === 0) {
|
|
107
|
+
return {
|
|
108
|
+
status: Status.HEALTHY,
|
|
109
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
110
|
+
checks: {}
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
const results = await Promise.allSettled(
|
|
114
|
+
this.checks.map((entry) => this.runSingle(entry.name, entry.fn, entry.timeout))
|
|
115
|
+
);
|
|
116
|
+
const checksRecord = {};
|
|
117
|
+
const statuses = [];
|
|
118
|
+
for (let i = 0; i < this.checks.length; i++) {
|
|
119
|
+
const result = results[i];
|
|
120
|
+
const name = this.checks[i].name;
|
|
121
|
+
if (result.status === "fulfilled") {
|
|
122
|
+
checksRecord[name] = result.value;
|
|
123
|
+
statuses.push(result.value.status);
|
|
124
|
+
} else {
|
|
125
|
+
checksRecord[name] = {
|
|
126
|
+
status: Status.OUTAGE,
|
|
127
|
+
latencyMs: 0,
|
|
128
|
+
message: String(result.reason)
|
|
129
|
+
};
|
|
130
|
+
statuses.push(Status.OUTAGE);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
const overallStatus = Math.min(...statuses);
|
|
134
|
+
return {
|
|
135
|
+
status: overallStatus,
|
|
136
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
137
|
+
checks: checksRecord
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
async runSingle(name, fn, timeoutMs) {
|
|
141
|
+
const start = import_node_perf_hooks.performance.now();
|
|
142
|
+
const controller = new AbortController();
|
|
143
|
+
const timeoutId = setTimeout(() => controller.abort(), timeoutMs);
|
|
144
|
+
try {
|
|
145
|
+
const result = await Promise.race([
|
|
146
|
+
fn(),
|
|
147
|
+
new Promise((_, reject) => {
|
|
148
|
+
controller.signal.addEventListener("abort", () => {
|
|
149
|
+
reject(new Error(`Check "${name}" timed out after ${timeoutMs}ms`));
|
|
150
|
+
});
|
|
151
|
+
})
|
|
152
|
+
]);
|
|
153
|
+
const latencyMs = Math.round(import_node_perf_hooks.performance.now() - start);
|
|
154
|
+
return { status: result.status, latencyMs, message: result.message };
|
|
155
|
+
} catch (error) {
|
|
156
|
+
const latencyMs = Math.round(import_node_perf_hooks.performance.now() - start);
|
|
157
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
158
|
+
return { status: Status.OUTAGE, latencyMs, message };
|
|
159
|
+
} finally {
|
|
160
|
+
clearTimeout(timeoutId);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
};
|
|
164
|
+
function syncCheck(fn) {
|
|
165
|
+
return async () => fn();
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
// src/auth.ts
|
|
169
|
+
var import_node_crypto = require("crypto");
|
|
170
|
+
function verifyToken(provided, expected) {
|
|
171
|
+
if (!provided || !expected) return false;
|
|
172
|
+
const providedHash = (0, import_node_crypto.createHash)("sha256").update(provided).digest();
|
|
173
|
+
const expectedHash = (0, import_node_crypto.createHash)("sha256").update(expected).digest();
|
|
174
|
+
return (0, import_node_crypto.timingSafeEqual)(providedHash, expectedHash);
|
|
175
|
+
}
|
|
176
|
+
function extractToken(options) {
|
|
177
|
+
const { queryParams, authorizationHeader, queryParamName = "token" } = options;
|
|
178
|
+
if (queryParams) {
|
|
179
|
+
const value = queryParams[queryParamName];
|
|
180
|
+
const str = Array.isArray(value) ? value[0] : value;
|
|
181
|
+
if (str) return str;
|
|
182
|
+
}
|
|
183
|
+
if (authorizationHeader?.startsWith("Bearer ")) {
|
|
184
|
+
const token = authorizationHeader.slice("Bearer ".length);
|
|
185
|
+
if (token) return token;
|
|
186
|
+
}
|
|
187
|
+
return null;
|
|
188
|
+
}
|
|
189
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
190
|
+
0 && (module.exports = {
|
|
191
|
+
HealthcheckRegistry,
|
|
192
|
+
Status,
|
|
193
|
+
extractToken,
|
|
194
|
+
httpStatusCode,
|
|
195
|
+
statusFromString,
|
|
196
|
+
statusToLabel,
|
|
197
|
+
syncCheck,
|
|
198
|
+
toJson,
|
|
199
|
+
verifyToken
|
|
200
|
+
});
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export { A as AsyncCheckFn, C as CheckResult, H as HealthcheckRegistry, a as HealthcheckResponse, b as HealthcheckResponseJson, S as Status, c as StatusLabel, d as StatusValue, e as SyncCheckFn, h as httpStatusCode, s as statusFromString, f as statusToLabel, g as syncCheck, t as toJson } from './core-9-MXAO0I.cjs';
|
|
2
|
+
|
|
3
|
+
declare function verifyToken(provided: string, expected: string): boolean;
|
|
4
|
+
declare function extractToken(options: {
|
|
5
|
+
queryParams?: Record<string, string | string[] | undefined>;
|
|
6
|
+
authorizationHeader?: string | null;
|
|
7
|
+
queryParamName?: string;
|
|
8
|
+
}): string | null;
|
|
9
|
+
|
|
10
|
+
export { extractToken, verifyToken };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export { A as AsyncCheckFn, C as CheckResult, H as HealthcheckRegistry, a as HealthcheckResponse, b as HealthcheckResponseJson, S as Status, c as StatusLabel, d as StatusValue, e as SyncCheckFn, h as httpStatusCode, s as statusFromString, f as statusToLabel, g as syncCheck, t as toJson } from './core-9-MXAO0I.js';
|
|
2
|
+
|
|
3
|
+
declare function verifyToken(provided: string, expected: string): boolean;
|
|
4
|
+
declare function extractToken(options: {
|
|
5
|
+
queryParams?: Record<string, string | string[] | undefined>;
|
|
6
|
+
authorizationHeader?: string | null;
|
|
7
|
+
queryParamName?: string;
|
|
8
|
+
}): string | null;
|
|
9
|
+
|
|
10
|
+
export { extractToken, verifyToken };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
// src/types.ts
|
|
2
|
+
var Status = {
|
|
3
|
+
HEALTHY: 2,
|
|
4
|
+
DEGRADED: 1,
|
|
5
|
+
OUTAGE: 0
|
|
6
|
+
};
|
|
7
|
+
function statusToLabel(status) {
|
|
8
|
+
const labels = {
|
|
9
|
+
[Status.HEALTHY]: "healthy",
|
|
10
|
+
[Status.DEGRADED]: "degraded",
|
|
11
|
+
[Status.OUTAGE]: "outage"
|
|
12
|
+
};
|
|
13
|
+
return labels[status];
|
|
14
|
+
}
|
|
15
|
+
function statusFromString(value) {
|
|
16
|
+
const mapping = {
|
|
17
|
+
healthy: Status.HEALTHY,
|
|
18
|
+
ok: Status.HEALTHY,
|
|
19
|
+
degraded: Status.DEGRADED,
|
|
20
|
+
outage: Status.OUTAGE,
|
|
21
|
+
error: Status.OUTAGE
|
|
22
|
+
};
|
|
23
|
+
return mapping[value.toLowerCase()] ?? Status.OUTAGE;
|
|
24
|
+
}
|
|
25
|
+
function toJson(response) {
|
|
26
|
+
return {
|
|
27
|
+
status: statusToLabel(response.status),
|
|
28
|
+
timestamp: response.timestamp,
|
|
29
|
+
checks: Object.fromEntries(
|
|
30
|
+
Object.entries(response.checks).map(([name, result]) => [
|
|
31
|
+
name,
|
|
32
|
+
{ status: statusToLabel(result.status), latencyMs: result.latencyMs, message: result.message }
|
|
33
|
+
])
|
|
34
|
+
)
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
function httpStatusCode(status) {
|
|
38
|
+
return status === Status.HEALTHY ? 200 : 503;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// src/core.ts
|
|
42
|
+
import { performance } from "perf_hooks";
|
|
43
|
+
var HealthcheckRegistry = class {
|
|
44
|
+
checks = [];
|
|
45
|
+
defaultTimeout;
|
|
46
|
+
constructor(options) {
|
|
47
|
+
this.defaultTimeout = options?.defaultTimeout ?? 5e3;
|
|
48
|
+
}
|
|
49
|
+
add(name, fn, options) {
|
|
50
|
+
if (this.checks.some((c) => c.name === name)) {
|
|
51
|
+
throw new Error(`Check "${name}" is already registered`);
|
|
52
|
+
}
|
|
53
|
+
this.checks.push({
|
|
54
|
+
name,
|
|
55
|
+
fn,
|
|
56
|
+
timeout: options?.timeout ?? this.defaultTimeout
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
check(name, fnOrOptions, options) {
|
|
60
|
+
if (typeof fnOrOptions === "function") {
|
|
61
|
+
const fn = fnOrOptions;
|
|
62
|
+
this.add(name, fn, options);
|
|
63
|
+
return fn;
|
|
64
|
+
}
|
|
65
|
+
const opts = fnOrOptions;
|
|
66
|
+
return (fn) => {
|
|
67
|
+
this.add(name, fn, opts);
|
|
68
|
+
return fn;
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
async run() {
|
|
72
|
+
if (this.checks.length === 0) {
|
|
73
|
+
return {
|
|
74
|
+
status: Status.HEALTHY,
|
|
75
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
76
|
+
checks: {}
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
const results = await Promise.allSettled(
|
|
80
|
+
this.checks.map((entry) => this.runSingle(entry.name, entry.fn, entry.timeout))
|
|
81
|
+
);
|
|
82
|
+
const checksRecord = {};
|
|
83
|
+
const statuses = [];
|
|
84
|
+
for (let i = 0; i < this.checks.length; i++) {
|
|
85
|
+
const result = results[i];
|
|
86
|
+
const name = this.checks[i].name;
|
|
87
|
+
if (result.status === "fulfilled") {
|
|
88
|
+
checksRecord[name] = result.value;
|
|
89
|
+
statuses.push(result.value.status);
|
|
90
|
+
} else {
|
|
91
|
+
checksRecord[name] = {
|
|
92
|
+
status: Status.OUTAGE,
|
|
93
|
+
latencyMs: 0,
|
|
94
|
+
message: String(result.reason)
|
|
95
|
+
};
|
|
96
|
+
statuses.push(Status.OUTAGE);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
const overallStatus = Math.min(...statuses);
|
|
100
|
+
return {
|
|
101
|
+
status: overallStatus,
|
|
102
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
103
|
+
checks: checksRecord
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
async runSingle(name, fn, timeoutMs) {
|
|
107
|
+
const start = performance.now();
|
|
108
|
+
const controller = new AbortController();
|
|
109
|
+
const timeoutId = setTimeout(() => controller.abort(), timeoutMs);
|
|
110
|
+
try {
|
|
111
|
+
const result = await Promise.race([
|
|
112
|
+
fn(),
|
|
113
|
+
new Promise((_, reject) => {
|
|
114
|
+
controller.signal.addEventListener("abort", () => {
|
|
115
|
+
reject(new Error(`Check "${name}" timed out after ${timeoutMs}ms`));
|
|
116
|
+
});
|
|
117
|
+
})
|
|
118
|
+
]);
|
|
119
|
+
const latencyMs = Math.round(performance.now() - start);
|
|
120
|
+
return { status: result.status, latencyMs, message: result.message };
|
|
121
|
+
} catch (error) {
|
|
122
|
+
const latencyMs = Math.round(performance.now() - start);
|
|
123
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
124
|
+
return { status: Status.OUTAGE, latencyMs, message };
|
|
125
|
+
} finally {
|
|
126
|
+
clearTimeout(timeoutId);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
};
|
|
130
|
+
function syncCheck(fn) {
|
|
131
|
+
return async () => fn();
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// src/auth.ts
|
|
135
|
+
import { createHash, timingSafeEqual } from "crypto";
|
|
136
|
+
function verifyToken(provided, expected) {
|
|
137
|
+
if (!provided || !expected) return false;
|
|
138
|
+
const providedHash = createHash("sha256").update(provided).digest();
|
|
139
|
+
const expectedHash = createHash("sha256").update(expected).digest();
|
|
140
|
+
return timingSafeEqual(providedHash, expectedHash);
|
|
141
|
+
}
|
|
142
|
+
function extractToken(options) {
|
|
143
|
+
const { queryParams, authorizationHeader, queryParamName = "token" } = options;
|
|
144
|
+
if (queryParams) {
|
|
145
|
+
const value = queryParams[queryParamName];
|
|
146
|
+
const str = Array.isArray(value) ? value[0] : value;
|
|
147
|
+
if (str) return str;
|
|
148
|
+
}
|
|
149
|
+
if (authorizationHeader?.startsWith("Bearer ")) {
|
|
150
|
+
const token = authorizationHeader.slice("Bearer ".length);
|
|
151
|
+
if (token) return token;
|
|
152
|
+
}
|
|
153
|
+
return null;
|
|
154
|
+
}
|
|
155
|
+
export {
|
|
156
|
+
HealthcheckRegistry,
|
|
157
|
+
Status,
|
|
158
|
+
extractToken,
|
|
159
|
+
httpStatusCode,
|
|
160
|
+
statusFromString,
|
|
161
|
+
statusToLabel,
|
|
162
|
+
syncCheck,
|
|
163
|
+
toJson,
|
|
164
|
+
verifyToken
|
|
165
|
+
};
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/integrations/express.ts
|
|
21
|
+
var express_exports = {};
|
|
22
|
+
__export(express_exports, {
|
|
23
|
+
createHealthcheckMiddleware: () => createHealthcheckMiddleware
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(express_exports);
|
|
26
|
+
|
|
27
|
+
// src/auth.ts
|
|
28
|
+
var import_node_crypto = require("crypto");
|
|
29
|
+
function verifyToken(provided, expected) {
|
|
30
|
+
if (!provided || !expected) return false;
|
|
31
|
+
const providedHash = (0, import_node_crypto.createHash)("sha256").update(provided).digest();
|
|
32
|
+
const expectedHash = (0, import_node_crypto.createHash)("sha256").update(expected).digest();
|
|
33
|
+
return (0, import_node_crypto.timingSafeEqual)(providedHash, expectedHash);
|
|
34
|
+
}
|
|
35
|
+
function extractToken(options) {
|
|
36
|
+
const { queryParams, authorizationHeader, queryParamName = "token" } = options;
|
|
37
|
+
if (queryParams) {
|
|
38
|
+
const value = queryParams[queryParamName];
|
|
39
|
+
const str = Array.isArray(value) ? value[0] : value;
|
|
40
|
+
if (str) return str;
|
|
41
|
+
}
|
|
42
|
+
if (authorizationHeader?.startsWith("Bearer ")) {
|
|
43
|
+
const token = authorizationHeader.slice("Bearer ".length);
|
|
44
|
+
if (token) return token;
|
|
45
|
+
}
|
|
46
|
+
return null;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// src/types.ts
|
|
50
|
+
var Status = {
|
|
51
|
+
HEALTHY: 2,
|
|
52
|
+
DEGRADED: 1,
|
|
53
|
+
OUTAGE: 0
|
|
54
|
+
};
|
|
55
|
+
function statusToLabel(status) {
|
|
56
|
+
const labels = {
|
|
57
|
+
[Status.HEALTHY]: "healthy",
|
|
58
|
+
[Status.DEGRADED]: "degraded",
|
|
59
|
+
[Status.OUTAGE]: "outage"
|
|
60
|
+
};
|
|
61
|
+
return labels[status];
|
|
62
|
+
}
|
|
63
|
+
function toJson(response) {
|
|
64
|
+
return {
|
|
65
|
+
status: statusToLabel(response.status),
|
|
66
|
+
timestamp: response.timestamp,
|
|
67
|
+
checks: Object.fromEntries(
|
|
68
|
+
Object.entries(response.checks).map(([name, result]) => [
|
|
69
|
+
name,
|
|
70
|
+
{ status: statusToLabel(result.status), latencyMs: result.latencyMs, message: result.message }
|
|
71
|
+
])
|
|
72
|
+
)
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
function httpStatusCode(status) {
|
|
76
|
+
return status === Status.HEALTHY ? 200 : 503;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// src/integrations/express.ts
|
|
80
|
+
function createHealthcheckMiddleware(options) {
|
|
81
|
+
const { registry, token = null, queryParamName = "token" } = options;
|
|
82
|
+
return async (req, res) => {
|
|
83
|
+
try {
|
|
84
|
+
if (token !== null) {
|
|
85
|
+
const provided = extractToken({
|
|
86
|
+
queryParams: req.query,
|
|
87
|
+
authorizationHeader: req.headers.authorization ?? null,
|
|
88
|
+
queryParamName
|
|
89
|
+
});
|
|
90
|
+
if (provided === null || !verifyToken(provided, token)) {
|
|
91
|
+
res.status(403).json({ error: "Forbidden" });
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
const response = await registry.run();
|
|
96
|
+
res.status(httpStatusCode(response.status)).json(toJson(response));
|
|
97
|
+
} catch {
|
|
98
|
+
res.status(500).json({ error: "Internal Server Error" });
|
|
99
|
+
}
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
103
|
+
0 && (module.exports = {
|
|
104
|
+
createHealthcheckMiddleware
|
|
105
|
+
});
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { RequestHandler } from 'express';
|
|
2
|
+
import { H as HealthcheckRegistry } from '../core-9-MXAO0I.cjs';
|
|
3
|
+
|
|
4
|
+
interface HealthcheckMiddlewareOptions {
|
|
5
|
+
registry: HealthcheckRegistry;
|
|
6
|
+
token?: string | null;
|
|
7
|
+
queryParamName?: string;
|
|
8
|
+
}
|
|
9
|
+
declare function createHealthcheckMiddleware(options: HealthcheckMiddlewareOptions): RequestHandler;
|
|
10
|
+
|
|
11
|
+
export { type HealthcheckMiddlewareOptions, createHealthcheckMiddleware };
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { RequestHandler } from 'express';
|
|
2
|
+
import { H as HealthcheckRegistry } from '../core-9-MXAO0I.js';
|
|
3
|
+
|
|
4
|
+
interface HealthcheckMiddlewareOptions {
|
|
5
|
+
registry: HealthcheckRegistry;
|
|
6
|
+
token?: string | null;
|
|
7
|
+
queryParamName?: string;
|
|
8
|
+
}
|
|
9
|
+
declare function createHealthcheckMiddleware(options: HealthcheckMiddlewareOptions): RequestHandler;
|
|
10
|
+
|
|
11
|
+
export { type HealthcheckMiddlewareOptions, createHealthcheckMiddleware };
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
// src/auth.ts
|
|
2
|
+
import { createHash, timingSafeEqual } from "crypto";
|
|
3
|
+
function verifyToken(provided, expected) {
|
|
4
|
+
if (!provided || !expected) return false;
|
|
5
|
+
const providedHash = createHash("sha256").update(provided).digest();
|
|
6
|
+
const expectedHash = createHash("sha256").update(expected).digest();
|
|
7
|
+
return timingSafeEqual(providedHash, expectedHash);
|
|
8
|
+
}
|
|
9
|
+
function extractToken(options) {
|
|
10
|
+
const { queryParams, authorizationHeader, queryParamName = "token" } = options;
|
|
11
|
+
if (queryParams) {
|
|
12
|
+
const value = queryParams[queryParamName];
|
|
13
|
+
const str = Array.isArray(value) ? value[0] : value;
|
|
14
|
+
if (str) return str;
|
|
15
|
+
}
|
|
16
|
+
if (authorizationHeader?.startsWith("Bearer ")) {
|
|
17
|
+
const token = authorizationHeader.slice("Bearer ".length);
|
|
18
|
+
if (token) return token;
|
|
19
|
+
}
|
|
20
|
+
return null;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// src/types.ts
|
|
24
|
+
var Status = {
|
|
25
|
+
HEALTHY: 2,
|
|
26
|
+
DEGRADED: 1,
|
|
27
|
+
OUTAGE: 0
|
|
28
|
+
};
|
|
29
|
+
function statusToLabel(status) {
|
|
30
|
+
const labels = {
|
|
31
|
+
[Status.HEALTHY]: "healthy",
|
|
32
|
+
[Status.DEGRADED]: "degraded",
|
|
33
|
+
[Status.OUTAGE]: "outage"
|
|
34
|
+
};
|
|
35
|
+
return labels[status];
|
|
36
|
+
}
|
|
37
|
+
function toJson(response) {
|
|
38
|
+
return {
|
|
39
|
+
status: statusToLabel(response.status),
|
|
40
|
+
timestamp: response.timestamp,
|
|
41
|
+
checks: Object.fromEntries(
|
|
42
|
+
Object.entries(response.checks).map(([name, result]) => [
|
|
43
|
+
name,
|
|
44
|
+
{ status: statusToLabel(result.status), latencyMs: result.latencyMs, message: result.message }
|
|
45
|
+
])
|
|
46
|
+
)
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
function httpStatusCode(status) {
|
|
50
|
+
return status === Status.HEALTHY ? 200 : 503;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// src/integrations/express.ts
|
|
54
|
+
function createHealthcheckMiddleware(options) {
|
|
55
|
+
const { registry, token = null, queryParamName = "token" } = options;
|
|
56
|
+
return async (req, res) => {
|
|
57
|
+
try {
|
|
58
|
+
if (token !== null) {
|
|
59
|
+
const provided = extractToken({
|
|
60
|
+
queryParams: req.query,
|
|
61
|
+
authorizationHeader: req.headers.authorization ?? null,
|
|
62
|
+
queryParamName
|
|
63
|
+
});
|
|
64
|
+
if (provided === null || !verifyToken(provided, token)) {
|
|
65
|
+
res.status(403).json({ error: "Forbidden" });
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
const response = await registry.run();
|
|
70
|
+
res.status(httpStatusCode(response.status)).json(toJson(response));
|
|
71
|
+
} catch {
|
|
72
|
+
res.status(500).json({ error: "Internal Server Error" });
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
export {
|
|
77
|
+
createHealthcheckMiddleware
|
|
78
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@firebreak/vitals",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Deep healthcheck endpoints following the HEALTHCHECK_SPEC format",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": {
|
|
8
|
+
"import": {
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
10
|
+
"default": "./dist/index.mjs"
|
|
11
|
+
},
|
|
12
|
+
"require": {
|
|
13
|
+
"types": "./dist/index.d.cts",
|
|
14
|
+
"default": "./dist/index.cjs"
|
|
15
|
+
}
|
|
16
|
+
},
|
|
17
|
+
"./checks/postgres": {
|
|
18
|
+
"import": {
|
|
19
|
+
"types": "./dist/checks/postgres.d.ts",
|
|
20
|
+
"default": "./dist/checks/postgres.mjs"
|
|
21
|
+
},
|
|
22
|
+
"require": {
|
|
23
|
+
"types": "./dist/checks/postgres.d.cts",
|
|
24
|
+
"default": "./dist/checks/postgres.cjs"
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
"./checks/redis": {
|
|
28
|
+
"import": {
|
|
29
|
+
"types": "./dist/checks/redis.d.ts",
|
|
30
|
+
"default": "./dist/checks/redis.mjs"
|
|
31
|
+
},
|
|
32
|
+
"require": {
|
|
33
|
+
"types": "./dist/checks/redis.d.cts",
|
|
34
|
+
"default": "./dist/checks/redis.cjs"
|
|
35
|
+
}
|
|
36
|
+
},
|
|
37
|
+
"./express": {
|
|
38
|
+
"import": {
|
|
39
|
+
"types": "./dist/integrations/express.d.ts",
|
|
40
|
+
"default": "./dist/integrations/express.mjs"
|
|
41
|
+
},
|
|
42
|
+
"require": {
|
|
43
|
+
"types": "./dist/integrations/express.d.cts",
|
|
44
|
+
"default": "./dist/integrations/express.cjs"
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
},
|
|
48
|
+
"main": "./dist/index.cjs",
|
|
49
|
+
"module": "./dist/index.mjs",
|
|
50
|
+
"types": "./dist/index.d.ts",
|
|
51
|
+
"files": [
|
|
52
|
+
"dist"
|
|
53
|
+
],
|
|
54
|
+
"scripts": {
|
|
55
|
+
"build": "tsup",
|
|
56
|
+
"test": "vitest run",
|
|
57
|
+
"test:watch": "vitest",
|
|
58
|
+
"lint": "tsc --noEmit"
|
|
59
|
+
},
|
|
60
|
+
"peerDependencies": {
|
|
61
|
+
"pg": ">=8.0.0",
|
|
62
|
+
"ioredis": ">=5.0.0",
|
|
63
|
+
"express": ">=4.0.0"
|
|
64
|
+
},
|
|
65
|
+
"peerDependenciesMeta": {
|
|
66
|
+
"pg": {
|
|
67
|
+
"optional": true
|
|
68
|
+
},
|
|
69
|
+
"ioredis": {
|
|
70
|
+
"optional": true
|
|
71
|
+
},
|
|
72
|
+
"express": {
|
|
73
|
+
"optional": true
|
|
74
|
+
}
|
|
75
|
+
},
|
|
76
|
+
"devDependencies": {
|
|
77
|
+
"tsup": "^8.0.0",
|
|
78
|
+
"typescript": "^5.5.0",
|
|
79
|
+
"vitest": "^2.0.0",
|
|
80
|
+
"@types/node": "^22.0.0",
|
|
81
|
+
"@types/express": "^5.0.0",
|
|
82
|
+
"@types/pg": "^8.0.0",
|
|
83
|
+
"ioredis": "^5.0.0",
|
|
84
|
+
"express": "^4.21.0",
|
|
85
|
+
"pg": "^8.13.0",
|
|
86
|
+
"supertest": "^7.0.0",
|
|
87
|
+
"@types/supertest": "^6.0.0"
|
|
88
|
+
},
|
|
89
|
+
"engines": {
|
|
90
|
+
"node": ">=20.0.0"
|
|
91
|
+
},
|
|
92
|
+
"license": "UNLICENSED"
|
|
93
|
+
}
|