@firebreak/vitals 1.2.2 → 1.3.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/README.md +25 -1
- package/dist/checks/databricks.d.cts +1 -1
- package/dist/checks/databricks.d.ts +1 -1
- package/dist/checks/http.d.cts +1 -1
- package/dist/checks/http.d.ts +1 -1
- package/dist/checks/postgres.d.cts +1 -1
- package/dist/checks/postgres.d.ts +1 -1
- package/dist/checks/redis.d.cts +1 -1
- package/dist/checks/redis.d.ts +1 -1
- package/dist/{core-BJ2Z0rRi.d.cts → core-Bee03bJm.d.cts} +7 -1
- package/dist/{core-BJ2Z0rRi.d.ts → core-Bee03bJm.d.ts} +7 -1
- package/dist/{handler-R2U3ygLo.d.ts → handler-COH7lot9.d.cts} +2 -1
- package/dist/{handler-Bvf66wzh.d.cts → handler-CVYUad84.d.ts} +2 -1
- package/dist/index.cjs +302 -18
- package/dist/index.d.cts +10 -3
- package/dist/index.d.ts +10 -3
- package/dist/index.mjs +301 -18
- package/dist/integrations/express.cjs +282 -19
- package/dist/integrations/express.d.cts +2 -2
- package/dist/integrations/express.d.ts +2 -2
- package/dist/integrations/express.mjs +282 -19
- package/dist/integrations/next.cjs +308 -18
- package/dist/integrations/next.d.cts +2 -2
- package/dist/integrations/next.d.ts +2 -2
- package/dist/integrations/next.mjs +308 -18
- package/package.json +1 -1
|
@@ -35,7 +35,7 @@ function statusToLabel(status) {
|
|
|
35
35
|
return labels[status];
|
|
36
36
|
}
|
|
37
37
|
function toJson(response) {
|
|
38
|
-
|
|
38
|
+
const json = {
|
|
39
39
|
status: statusToLabel(response.status),
|
|
40
40
|
timestamp: response.timestamp,
|
|
41
41
|
checks: Object.fromEntries(
|
|
@@ -45,38 +45,62 @@ function toJson(response) {
|
|
|
45
45
|
])
|
|
46
46
|
)
|
|
47
47
|
};
|
|
48
|
+
if (response.cachedAt !== void 0) {
|
|
49
|
+
json.cachedAt = response.cachedAt;
|
|
50
|
+
}
|
|
51
|
+
return json;
|
|
48
52
|
}
|
|
49
53
|
function httpStatusCode(status) {
|
|
50
54
|
return status === Status.HEALTHY ? 200 : 503;
|
|
51
55
|
}
|
|
52
56
|
|
|
53
57
|
// src/handler.ts
|
|
54
|
-
var RESERVED_METADATA_KEYS = ["status", "timestamp", "checks"];
|
|
58
|
+
var RESERVED_METADATA_KEYS = ["status", "timestamp", "checks", "cachedAt"];
|
|
55
59
|
function createHealthcheckHandler(options) {
|
|
56
|
-
const { registry, token =
|
|
60
|
+
const { registry, token: rawToken, deep = false, queryParamName = "token", metadata = {} } = options;
|
|
61
|
+
const isEmptyToken = typeof rawToken === "string" && rawToken.trim() === "";
|
|
62
|
+
if (deep && isEmptyToken) {
|
|
63
|
+
throw new Error(
|
|
64
|
+
"Cannot use `deep: true` with an empty string token. This would expose deep healthcheck data without authentication. Either set a non-empty token or explicitly set `token: null`."
|
|
65
|
+
);
|
|
66
|
+
}
|
|
67
|
+
const token = rawToken == null || isEmptyToken ? null : rawToken;
|
|
57
68
|
for (const key of Object.keys(metadata)) {
|
|
58
69
|
if (RESERVED_METADATA_KEYS.includes(key)) {
|
|
59
70
|
throw new Error(`Metadata key '${key}' is reserved. Use a different key name.`);
|
|
60
71
|
}
|
|
61
72
|
}
|
|
62
73
|
return async (req) => {
|
|
63
|
-
if (token
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
if (provided === null) {
|
|
70
|
-
const body = {
|
|
71
|
-
status: "ok",
|
|
72
|
-
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
73
|
-
...metadata
|
|
74
|
+
if (token === null) {
|
|
75
|
+
if (deep) {
|
|
76
|
+
const response2 = await registry.run();
|
|
77
|
+
return {
|
|
78
|
+
status: httpStatusCode(response2.status),
|
|
79
|
+
body: { ...metadata, ...toJson(response2) }
|
|
74
80
|
};
|
|
75
|
-
return { status: 200, body };
|
|
76
|
-
}
|
|
77
|
-
if (!verifyToken(provided, token)) {
|
|
78
|
-
return { status: 403, body: { error: "Forbidden" } };
|
|
79
81
|
}
|
|
82
|
+
const body = {
|
|
83
|
+
status: "ok",
|
|
84
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
85
|
+
...metadata
|
|
86
|
+
};
|
|
87
|
+
return { status: 200, body };
|
|
88
|
+
}
|
|
89
|
+
const provided = extractToken({
|
|
90
|
+
queryParams: req.queryParams,
|
|
91
|
+
authorizationHeader: req.authorizationHeader,
|
|
92
|
+
queryParamName
|
|
93
|
+
});
|
|
94
|
+
if (provided === null) {
|
|
95
|
+
const body = {
|
|
96
|
+
status: "ok",
|
|
97
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
98
|
+
...metadata
|
|
99
|
+
};
|
|
100
|
+
return { status: 200, body };
|
|
101
|
+
}
|
|
102
|
+
if (!verifyToken(provided, token)) {
|
|
103
|
+
return { status: 403, body: { error: "Forbidden" } };
|
|
80
104
|
}
|
|
81
105
|
const response = await registry.run();
|
|
82
106
|
return {
|
|
@@ -86,7 +110,267 @@ function createHealthcheckHandler(options) {
|
|
|
86
110
|
};
|
|
87
111
|
}
|
|
88
112
|
|
|
113
|
+
// src/html.ts
|
|
114
|
+
function escapeHtml(str) {
|
|
115
|
+
return str.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """).replace(/'/g, "'");
|
|
116
|
+
}
|
|
117
|
+
function statusColor(status) {
|
|
118
|
+
switch (status) {
|
|
119
|
+
case "healthy":
|
|
120
|
+
case "ok":
|
|
121
|
+
return "#22C55E";
|
|
122
|
+
case "degraded":
|
|
123
|
+
return "#F59E0B";
|
|
124
|
+
case "outage":
|
|
125
|
+
return "#ED1C24";
|
|
126
|
+
default:
|
|
127
|
+
return "#ED1C24";
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
var DEEP_KNOWN_KEYS = /* @__PURE__ */ new Set(["status", "timestamp", "checks", "cachedAt"]);
|
|
131
|
+
function isDeepResponse(body) {
|
|
132
|
+
return "checks" in body && "status" in body && body.status !== "ok";
|
|
133
|
+
}
|
|
134
|
+
function isShallowResponse(body) {
|
|
135
|
+
return "status" in body && body.status === "ok";
|
|
136
|
+
}
|
|
137
|
+
function isErrorResponse(body) {
|
|
138
|
+
return "error" in body && !("status" in body);
|
|
139
|
+
}
|
|
140
|
+
function renderStatusBadge(label, color) {
|
|
141
|
+
return `<span style="
|
|
142
|
+
display: inline-flex;
|
|
143
|
+
align-items: center;
|
|
144
|
+
gap: 8px;
|
|
145
|
+
padding: 4px 12px;
|
|
146
|
+
border-radius: 9999px;
|
|
147
|
+
background: ${color}1A;
|
|
148
|
+
font-family: 'Inter', system-ui, sans-serif;
|
|
149
|
+
font-size: 12px;
|
|
150
|
+
font-weight: 600;
|
|
151
|
+
letter-spacing: 0.05em;
|
|
152
|
+
text-transform: uppercase;
|
|
153
|
+
color: ${color};
|
|
154
|
+
"><span style="
|
|
155
|
+
width: 8px;
|
|
156
|
+
height: 8px;
|
|
157
|
+
border-radius: 50%;
|
|
158
|
+
background: ${color};
|
|
159
|
+
box-shadow: 0 0 6px ${color}80;
|
|
160
|
+
"></span>${escapeHtml(label)}</span>`;
|
|
161
|
+
}
|
|
162
|
+
function renderMetadataItems(items) {
|
|
163
|
+
if (items.length === 0) return "";
|
|
164
|
+
return `<div style="
|
|
165
|
+
display: flex;
|
|
166
|
+
flex-wrap: wrap;
|
|
167
|
+
gap: 24px;
|
|
168
|
+
margin-top: 16px;
|
|
169
|
+
">${items.map(
|
|
170
|
+
([label, value]) => `<div>
|
|
171
|
+
<div style="
|
|
172
|
+
font-family: 'Inter', system-ui, sans-serif;
|
|
173
|
+
font-size: 10px;
|
|
174
|
+
font-weight: 600;
|
|
175
|
+
letter-spacing: 0.05em;
|
|
176
|
+
text-transform: uppercase;
|
|
177
|
+
color: #A1A1AA;
|
|
178
|
+
margin-bottom: 4px;
|
|
179
|
+
">${escapeHtml(label)}</div>
|
|
180
|
+
<div style="
|
|
181
|
+
font-family: 'JetBrains Mono', monospace;
|
|
182
|
+
font-size: 13px;
|
|
183
|
+
color: #E4E4E7;
|
|
184
|
+
">${escapeHtml(value)}</div>
|
|
185
|
+
</div>`
|
|
186
|
+
).join("")}</div>`;
|
|
187
|
+
}
|
|
188
|
+
function renderCheckRow(name, check) {
|
|
189
|
+
const color = statusColor(check.status);
|
|
190
|
+
return `<div style="
|
|
191
|
+
display: flex;
|
|
192
|
+
align-items: center;
|
|
193
|
+
gap: 12px;
|
|
194
|
+
padding: 12px 0;
|
|
195
|
+
border-top: 1px solid #27272A;
|
|
196
|
+
">
|
|
197
|
+
<span style="
|
|
198
|
+
width: 8px;
|
|
199
|
+
height: 8px;
|
|
200
|
+
border-radius: 50%;
|
|
201
|
+
background: ${color};
|
|
202
|
+
box-shadow: 0 0 6px ${color}80;
|
|
203
|
+
flex-shrink: 0;
|
|
204
|
+
"></span>
|
|
205
|
+
<span style="
|
|
206
|
+
font-family: 'Inter', system-ui, sans-serif;
|
|
207
|
+
font-size: 12px;
|
|
208
|
+
text-transform: uppercase;
|
|
209
|
+
letter-spacing: 0.05em;
|
|
210
|
+
color: ${color};
|
|
211
|
+
font-weight: 600;
|
|
212
|
+
min-width: 72px;
|
|
213
|
+
">${escapeHtml(check.status)}</span>
|
|
214
|
+
<span style="
|
|
215
|
+
font-family: 'Inter', system-ui, sans-serif;
|
|
216
|
+
font-size: 14px;
|
|
217
|
+
font-weight: 500;
|
|
218
|
+
color: #E4E4E7;
|
|
219
|
+
flex: 1;
|
|
220
|
+
">${escapeHtml(name)}</span>
|
|
221
|
+
<span style="
|
|
222
|
+
font-family: 'JetBrains Mono', monospace;
|
|
223
|
+
font-size: 12px;
|
|
224
|
+
color: #A1A1AA;
|
|
225
|
+
min-width: 60px;
|
|
226
|
+
text-align: right;
|
|
227
|
+
">${escapeHtml(String(check.latencyMs))}ms</span>
|
|
228
|
+
<span style="
|
|
229
|
+
font-family: 'JetBrains Mono', monospace;
|
|
230
|
+
font-size: 12px;
|
|
231
|
+
color: #71717A;
|
|
232
|
+
max-width: 200px;
|
|
233
|
+
overflow: hidden;
|
|
234
|
+
text-overflow: ellipsis;
|
|
235
|
+
white-space: nowrap;
|
|
236
|
+
">${escapeHtml(check.message)}</span>
|
|
237
|
+
</div>`;
|
|
238
|
+
}
|
|
239
|
+
function renderPage(borderColor, content) {
|
|
240
|
+
return `<!DOCTYPE html>
|
|
241
|
+
<html lang="en">
|
|
242
|
+
<head>
|
|
243
|
+
<meta charset="utf-8">
|
|
244
|
+
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
245
|
+
<title>Healthcheck</title>
|
|
246
|
+
<style></style>
|
|
247
|
+
</head>
|
|
248
|
+
<body style="
|
|
249
|
+
margin: 0;
|
|
250
|
+
padding: 40px 20px;
|
|
251
|
+
background: #08080A;
|
|
252
|
+
min-height: 100vh;
|
|
253
|
+
display: flex;
|
|
254
|
+
justify-content: center;
|
|
255
|
+
align-items: flex-start;
|
|
256
|
+
box-sizing: border-box;
|
|
257
|
+
">
|
|
258
|
+
<div style="
|
|
259
|
+
width: 100%;
|
|
260
|
+
max-width: 560px;
|
|
261
|
+
background: #18181B;
|
|
262
|
+
border: 1px solid #27272A;
|
|
263
|
+
border-left: 3px solid ${borderColor};
|
|
264
|
+
border-radius: 8px;
|
|
265
|
+
padding: 24px;
|
|
266
|
+
">
|
|
267
|
+
${content}
|
|
268
|
+
</div>
|
|
269
|
+
</body>
|
|
270
|
+
</html>`;
|
|
271
|
+
}
|
|
272
|
+
function renderHealthcheckHtml(body) {
|
|
273
|
+
if (isErrorResponse(body)) {
|
|
274
|
+
const color = "#ED1C24";
|
|
275
|
+
return renderPage(
|
|
276
|
+
color,
|
|
277
|
+
`<div style="display: flex; align-items: center; justify-content: space-between;">
|
|
278
|
+
<span style="
|
|
279
|
+
font-family: 'Inter', system-ui, sans-serif;
|
|
280
|
+
font-size: 18px;
|
|
281
|
+
font-weight: 700;
|
|
282
|
+
color: #E4E4E7;
|
|
283
|
+
">Healthcheck</span>
|
|
284
|
+
${renderStatusBadge("ERROR", color)}
|
|
285
|
+
</div>
|
|
286
|
+
<div style="
|
|
287
|
+
margin-top: 16px;
|
|
288
|
+
font-family: 'JetBrains Mono', monospace;
|
|
289
|
+
font-size: 13px;
|
|
290
|
+
color: #EF4444;
|
|
291
|
+
">${escapeHtml(body.error)}</div>`
|
|
292
|
+
);
|
|
293
|
+
}
|
|
294
|
+
if (isShallowResponse(body)) {
|
|
295
|
+
const color = statusColor("ok");
|
|
296
|
+
const { status: _, timestamp, ...rest } = body;
|
|
297
|
+
const metadataItems = Object.entries(rest).map(
|
|
298
|
+
([k, v]) => [k, String(v)]
|
|
299
|
+
);
|
|
300
|
+
const allItems = [
|
|
301
|
+
["timestamp", timestamp],
|
|
302
|
+
...metadataItems
|
|
303
|
+
];
|
|
304
|
+
return renderPage(
|
|
305
|
+
color,
|
|
306
|
+
`<div style="display: flex; align-items: center; justify-content: space-between;">
|
|
307
|
+
<span style="
|
|
308
|
+
font-family: 'Inter', system-ui, sans-serif;
|
|
309
|
+
font-size: 18px;
|
|
310
|
+
font-weight: 700;
|
|
311
|
+
color: #E4E4E7;
|
|
312
|
+
">Healthcheck</span>
|
|
313
|
+
${renderStatusBadge("OK", color)}
|
|
314
|
+
</div>
|
|
315
|
+
${renderMetadataItems(allItems)}`
|
|
316
|
+
);
|
|
317
|
+
}
|
|
318
|
+
if (isDeepResponse(body)) {
|
|
319
|
+
const color = statusColor(body.status);
|
|
320
|
+
const { timestamp, checks } = body;
|
|
321
|
+
const metadataItems = Object.entries(body).filter(([k]) => !DEEP_KNOWN_KEYS.has(k)).map(([k, v]) => [k, String(v)]);
|
|
322
|
+
const allItems = [
|
|
323
|
+
["timestamp", timestamp],
|
|
324
|
+
...metadataItems
|
|
325
|
+
];
|
|
326
|
+
const checkRows = Object.entries(checks).sort(([a], [b]) => a.localeCompare(b)).map(([name, check]) => renderCheckRow(name, check)).join("");
|
|
327
|
+
return renderPage(
|
|
328
|
+
color,
|
|
329
|
+
`<div style="display: flex; align-items: center; justify-content: space-between;">
|
|
330
|
+
<span style="
|
|
331
|
+
font-family: 'Inter', system-ui, sans-serif;
|
|
332
|
+
font-size: 18px;
|
|
333
|
+
font-weight: 700;
|
|
334
|
+
color: #E4E4E7;
|
|
335
|
+
">Healthcheck</span>
|
|
336
|
+
${renderStatusBadge(body.status, color)}
|
|
337
|
+
</div>
|
|
338
|
+
${renderMetadataItems(allItems)}
|
|
339
|
+
<div style="margin-top: 20px;">
|
|
340
|
+
${checkRows}
|
|
341
|
+
</div>`
|
|
342
|
+
);
|
|
343
|
+
}
|
|
344
|
+
return renderPage("#ED1C24", `<pre style="color: #E4E4E7; font-family: 'JetBrains Mono', monospace;">${escapeHtml(JSON.stringify(body, null, 2))}</pre>`);
|
|
345
|
+
}
|
|
346
|
+
|
|
89
347
|
// src/integrations/next.ts
|
|
348
|
+
function parseMediaType(header, mediaType) {
|
|
349
|
+
const parts = header.split(",");
|
|
350
|
+
let bestMatch = null;
|
|
351
|
+
for (let i = 0; i < parts.length; i++) {
|
|
352
|
+
const trimmed = parts[i].trim();
|
|
353
|
+
const [type, ...params] = trimmed.split(";").map((s) => s.trim());
|
|
354
|
+
const exact = type === mediaType;
|
|
355
|
+
if (!exact && type !== "*/*") continue;
|
|
356
|
+
const qParam = params.find((p) => p.startsWith("q="));
|
|
357
|
+
const quality = qParam ? parseFloat(qParam.slice(2)) || 0 : 1;
|
|
358
|
+
if (!bestMatch || exact && !bestMatch.exact || exact === bestMatch.exact && quality > bestMatch.quality) {
|
|
359
|
+
bestMatch = { quality, index: i, exact };
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
return bestMatch ? { quality: bestMatch.quality, index: bestMatch.index } : null;
|
|
363
|
+
}
|
|
364
|
+
function prefersHtml(acceptHeader) {
|
|
365
|
+
if (!acceptHeader) return false;
|
|
366
|
+
const lower = acceptHeader.toLowerCase();
|
|
367
|
+
const html = parseMediaType(lower, "text/html");
|
|
368
|
+
const json = parseMediaType(lower, "application/json");
|
|
369
|
+
if (!html || html.quality <= 0) return false;
|
|
370
|
+
if (!json) return true;
|
|
371
|
+
if (html.quality !== json.quality) return html.quality > json.quality;
|
|
372
|
+
return html.index < json.index;
|
|
373
|
+
}
|
|
90
374
|
function createNextHandler(options) {
|
|
91
375
|
const handle = createHealthcheckHandler(options);
|
|
92
376
|
return async (request) => {
|
|
@@ -97,6 +381,12 @@ function createNextHandler(options) {
|
|
|
97
381
|
queryParams,
|
|
98
382
|
authorizationHeader: request.headers.get("authorization")
|
|
99
383
|
});
|
|
384
|
+
if (prefersHtml(request.headers.get("accept"))) {
|
|
385
|
+
return new Response(renderHealthcheckHtml(result.body), {
|
|
386
|
+
status: result.status,
|
|
387
|
+
headers: { "content-type": "text/html; charset=utf-8" }
|
|
388
|
+
});
|
|
389
|
+
}
|
|
100
390
|
return Response.json(result.body, { status: result.status });
|
|
101
391
|
} catch {
|
|
102
392
|
return Response.json({ error: "Internal Server Error" }, { status: 500 });
|