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