@hansenexus/health 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +38 -2
- package/dist/convex-probe.d.ts +10 -0
- package/dist/convex-probe.js +42 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.js +33 -0
- package/package.json +42 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 hansenexus GmbH
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,39 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @hansenexus/health
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Shared health-endpoint factory. Powers `/api/health` on every Next.js app.
|
|
4
|
+
|
|
5
|
+
## Usage
|
|
6
|
+
|
|
7
|
+
```ts
|
|
8
|
+
// apps/<app>/src/app/api/health/route.ts
|
|
9
|
+
import { createHealthHandler } from "@hansenexus/health";
|
|
10
|
+
|
|
11
|
+
export const GET = createHealthHandler({
|
|
12
|
+
convexUrl: process.env.NEXT_PUBLIC_CONVEX_URL,
|
|
13
|
+
});
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
Static apps without Convex can omit `convexUrl` — deep probes return `convex: "skipped"`.
|
|
17
|
+
|
|
18
|
+
## Modes
|
|
19
|
+
|
|
20
|
+
| Request | Behaviour |
|
|
21
|
+
|---|---|
|
|
22
|
+
| `GET /api/health` | Fast `{ status: "ok", ts }`. What kubelet hits every 10s for liveness/readiness. No external IO. |
|
|
23
|
+
| `GET /api/health?deep=1` | Same shape plus `convex`, `convexLatencyMs`. Used by `health-check.yml` cron (15min) to detect Convex backend rot. Returns HTTP 503 if Convex probe is `down`. |
|
|
24
|
+
|
|
25
|
+
## Tuning
|
|
26
|
+
|
|
27
|
+
- `timeoutMs` (default `3000`) — `/version` round-trip cap.
|
|
28
|
+
- `deep: true` constructor flag forces deep mode on every request (rare — useful for a dedicated deep route).
|
|
29
|
+
|
|
30
|
+
## What it does NOT do
|
|
31
|
+
|
|
32
|
+
- No auth check. `/version` is auth-free; that's the point.
|
|
33
|
+
- No Convex action invocation. Adds cost, leaks state. The cron probe is shallow on purpose; deep regression tests belong in e2e.
|
|
34
|
+
|
|
35
|
+
## Where it's called
|
|
36
|
+
|
|
37
|
+
- k8s probes: `/api/health` (shallow) — `k8s/base/deployment.yaml`.
|
|
38
|
+
- CI cron: `/api/health?deep=1` — `.github/workflows/health-check.yml`.
|
|
39
|
+
- Local smoke: `bun mcp/scripts/check-production-health.ts` (consumes `?deep=1` once shipped).
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export type ConvexProbeStatus = "ok" | "degraded" | "down" | "skipped";
|
|
2
|
+
export interface ConvexProbeResult {
|
|
3
|
+
status: ConvexProbeStatus;
|
|
4
|
+
latencyMs: number;
|
|
5
|
+
httpStatus?: number;
|
|
6
|
+
error?: string;
|
|
7
|
+
}
|
|
8
|
+
export declare function probeConvex(convexUrl: string | undefined, opts?: {
|
|
9
|
+
timeoutMs?: number | undefined;
|
|
10
|
+
}): Promise<ConvexProbeResult>;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
// src/convex-probe.ts
|
|
2
|
+
const DEFAULT_TIMEOUT_MS = 3e3;
|
|
3
|
+
const DEGRADED_LATENCY_MS = 1500;
|
|
4
|
+
async function probeConvex(convexUrl, opts = {}) {
|
|
5
|
+
const startedAt = Date.now();
|
|
6
|
+
if (!convexUrl) return {
|
|
7
|
+
status: "skipped",
|
|
8
|
+
latencyMs: 0
|
|
9
|
+
};
|
|
10
|
+
const timeoutMs = opts.timeoutMs ?? DEFAULT_TIMEOUT_MS;
|
|
11
|
+
const controller = new AbortController();
|
|
12
|
+
const timer = setTimeout(() => controller.abort(), timeoutMs);
|
|
13
|
+
const target = `${convexUrl.replace(/\/$/, "")}/version`;
|
|
14
|
+
try {
|
|
15
|
+
const res = await fetch(target, {
|
|
16
|
+
signal: controller.signal,
|
|
17
|
+
cache: "no-store",
|
|
18
|
+
headers: { accept: "application/json,text/plain,*/*" }
|
|
19
|
+
});
|
|
20
|
+
const latencyMs = Date.now() - startedAt;
|
|
21
|
+
if (!res.ok) return {
|
|
22
|
+
status: "down",
|
|
23
|
+
latencyMs,
|
|
24
|
+
httpStatus: res.status,
|
|
25
|
+
error: `convex /version returned ${res.status}`
|
|
26
|
+
};
|
|
27
|
+
return {
|
|
28
|
+
status: latencyMs > DEGRADED_LATENCY_MS ? "degraded" : "ok",
|
|
29
|
+
latencyMs,
|
|
30
|
+
httpStatus: res.status
|
|
31
|
+
};
|
|
32
|
+
} catch (err) {
|
|
33
|
+
return {
|
|
34
|
+
status: "down",
|
|
35
|
+
latencyMs: Date.now() - startedAt,
|
|
36
|
+
error: err instanceof Error ? err.message : String(err)
|
|
37
|
+
};
|
|
38
|
+
} finally {
|
|
39
|
+
clearTimeout(timer);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
export { probeConvex };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { ConvexProbeResult, ConvexProbeStatus, probeConvex } from "./convex-probe.js";
|
|
2
|
+
export interface CreateHealthHandlerOptions {
|
|
3
|
+
convexUrl?: string;
|
|
4
|
+
deep?: boolean;
|
|
5
|
+
timeoutMs?: number;
|
|
6
|
+
}
|
|
7
|
+
export declare function createHealthHandler(opts?: CreateHealthHandlerOptions): (req?: Request) => Promise<Response>;
|
|
8
|
+
export { type ConvexProbeResult, type ConvexProbeStatus, probeConvex };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { probeConvex } from "./convex-probe.js";
|
|
2
|
+
// src/index.ts
|
|
3
|
+
function shouldRunDeepProbe(req, forced) {
|
|
4
|
+
if (forced) return true;
|
|
5
|
+
if (!req || !("url" in req) || !req.url) return false;
|
|
6
|
+
try {
|
|
7
|
+
const flag = new URL(req.url, "http://localhost").searchParams.get("deep");
|
|
8
|
+
return flag === "1" || flag === "true";
|
|
9
|
+
} catch {
|
|
10
|
+
return false;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
function createHealthHandler(opts = {}) {
|
|
14
|
+
const { convexUrl, deep: forcedDeep, timeoutMs } = opts;
|
|
15
|
+
return async function GET(req) {
|
|
16
|
+
const ts = Date.now();
|
|
17
|
+
if (!shouldRunDeepProbe(req, forcedDeep)) return Response.json({
|
|
18
|
+
status: "ok",
|
|
19
|
+
ts
|
|
20
|
+
});
|
|
21
|
+
const probe = await probeConvex(convexUrl, { timeoutMs });
|
|
22
|
+
const status = probe.status === "down" ? "degraded" : "ok";
|
|
23
|
+
return Response.json({
|
|
24
|
+
status,
|
|
25
|
+
ts,
|
|
26
|
+
convex: probe.status,
|
|
27
|
+
convexLatencyMs: probe.latencyMs,
|
|
28
|
+
...probe.httpStatus !== void 0 ? { convexHttp: probe.httpStatus } : {},
|
|
29
|
+
...probe.error ? { convexError: probe.error } : {}
|
|
30
|
+
}, { status: status === "ok" ? 200 : 503 });
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
export { createHealthHandler, probeConvex };
|
package/package.json
CHANGED
|
@@ -1,6 +1,44 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hansenexus/health",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Health-endpoint factory for Next.js apps, with an optional Convex deep probe.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/hansenexus/platform.git",
|
|
10
|
+
"directory": "packages/health"
|
|
11
|
+
},
|
|
12
|
+
"exports": {
|
|
13
|
+
".": {
|
|
14
|
+
"types": "./dist/index.d.ts",
|
|
15
|
+
"import": "./dist/index.js"
|
|
16
|
+
},
|
|
17
|
+
"./convex-probe": {
|
|
18
|
+
"types": "./dist/convex-probe.d.ts",
|
|
19
|
+
"import": "./dist/convex-probe.js"
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
"files": [
|
|
23
|
+
"dist",
|
|
24
|
+
"README.md",
|
|
25
|
+
"LICENSE"
|
|
26
|
+
],
|
|
27
|
+
"sideEffects": false,
|
|
28
|
+
"publishConfig": {
|
|
29
|
+
"access": "public"
|
|
30
|
+
},
|
|
31
|
+
"scripts": {
|
|
32
|
+
"build": "tsdown",
|
|
33
|
+
"typecheck": "tsc --noEmit",
|
|
34
|
+
"test": "bun test"
|
|
35
|
+
},
|
|
36
|
+
"peerDependencies": {
|
|
37
|
+
"next": "^16.0.0"
|
|
38
|
+
},
|
|
39
|
+
"devDependencies": {
|
|
40
|
+
"@types/bun": "^1.4.4",
|
|
41
|
+
"tsdown": "^0.23.7",
|
|
42
|
+
"typescript": "^5.9.3"
|
|
43
|
+
}
|
|
44
|
+
}
|