@nexusts/health 0.7.0 → 0.7.2
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 +2 -2
- package/dist/health.controller.d.ts +60 -0
- package/dist/health.module.d.ts +28 -0
- package/dist/health.service.d.ts +33 -0
- package/dist/index.d.ts +37 -0
- package/dist/index.js +4 -20
- package/dist/index.js.map +5 -5
- package/dist/indicators/drizzle.d.ts +17 -0
- package/dist/indicators/index.d.ts +58 -0
- package/dist/types.d.ts +104 -0
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -15,7 +15,7 @@ This module is part of the NexusTS monorepo. Each module is published as its own
|
|
|
15
15
|
Most apps start with just the core:
|
|
16
16
|
|
|
17
17
|
```bash
|
|
18
|
-
bun add @nexusts/core
|
|
18
|
+
bun add @nexusts/core
|
|
19
19
|
```
|
|
20
20
|
|
|
21
21
|
Then add this module only if you need it:
|
|
@@ -26,7 +26,7 @@ bun add @nexusts/health
|
|
|
26
26
|
|
|
27
27
|
## Peer dependencies
|
|
28
28
|
|
|
29
|
-
None
|
|
29
|
+
**None.** No external dependencies.
|
|
30
30
|
|
|
31
31
|
## Usage
|
|
32
32
|
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `HealthController` — built-in `/health/live`, `/health/ready`,
|
|
3
|
+
* `/health/startup` endpoints.
|
|
4
|
+
*
|
|
5
|
+
* Mount `HealthModule.forRoot({...})` in your app module to get
|
|
6
|
+
* these routes automatically. Override paths or add an auth token
|
|
7
|
+
* via `HealthConfig`.
|
|
8
|
+
*/
|
|
9
|
+
import type { Context } from "hono";
|
|
10
|
+
import { HealthCheckService } from "./health.service.js";
|
|
11
|
+
import type { HealthConfig } from "./types.js";
|
|
12
|
+
export declare class HealthController {
|
|
13
|
+
private readonly health;
|
|
14
|
+
constructor(health: HealthCheckService);
|
|
15
|
+
live(c: Context, _res: Response): Promise<Response & import("hono").TypedResponse<{
|
|
16
|
+
status: import("./types.js").HealthStatus;
|
|
17
|
+
results: {
|
|
18
|
+
name: string;
|
|
19
|
+
result: {
|
|
20
|
+
status: import("./types.js").HealthStatus;
|
|
21
|
+
data?: import("hono/utils/types").JSONValue;
|
|
22
|
+
message?: string;
|
|
23
|
+
};
|
|
24
|
+
}[];
|
|
25
|
+
durationMs: number;
|
|
26
|
+
timestamp: string;
|
|
27
|
+
}, 200 | 503, "json">>;
|
|
28
|
+
ready(c: Context, _res: Response): Promise<Response & import("hono").TypedResponse<{
|
|
29
|
+
status: import("./types.js").HealthStatus;
|
|
30
|
+
results: {
|
|
31
|
+
name: string;
|
|
32
|
+
result: {
|
|
33
|
+
status: import("./types.js").HealthStatus;
|
|
34
|
+
data?: import("hono/utils/types").JSONValue;
|
|
35
|
+
message?: string;
|
|
36
|
+
};
|
|
37
|
+
}[];
|
|
38
|
+
durationMs: number;
|
|
39
|
+
timestamp: string;
|
|
40
|
+
}, 200 | 503, "json">>;
|
|
41
|
+
startup(c: Context, _res: Response): Promise<Response & import("hono").TypedResponse<{
|
|
42
|
+
status: import("./types.js").HealthStatus;
|
|
43
|
+
results: {
|
|
44
|
+
name: string;
|
|
45
|
+
result: {
|
|
46
|
+
status: import("./types.js").HealthStatus;
|
|
47
|
+
data?: import("hono/utils/types").JSONValue;
|
|
48
|
+
message?: string;
|
|
49
|
+
};
|
|
50
|
+
}[];
|
|
51
|
+
durationMs: number;
|
|
52
|
+
timestamp: string;
|
|
53
|
+
}, 200 | 503, "json">>;
|
|
54
|
+
private respond;
|
|
55
|
+
}
|
|
56
|
+
declare module "./health.service.js" {
|
|
57
|
+
interface HealthCheckService {
|
|
58
|
+
config: HealthConfig;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `HealthModule` — drop-in module for `/health/live`, `/health/ready`,
|
|
3
|
+
* `/health/startup` endpoints.
|
|
4
|
+
*
|
|
5
|
+
* Usage:
|
|
6
|
+
* @Module({
|
|
7
|
+
* imports: [
|
|
8
|
+
* HealthModule.forRoot({
|
|
9
|
+
* builtIn: {
|
|
10
|
+
* memory: true,
|
|
11
|
+
* disk: { threshold: 0.1 },
|
|
12
|
+
* http: { url: 'https://api.stripe.com/v1/healthcheck' },
|
|
13
|
+
* },
|
|
14
|
+
* }),
|
|
15
|
+
* ],
|
|
16
|
+
* })
|
|
17
|
+
* export class AppModule {}
|
|
18
|
+
*
|
|
19
|
+
* Then `/health/live`, `/health/ready`, `/health/startup` respond
|
|
20
|
+
* with a JSON body. Status 200 on `'up'`, 503 on `'down'`.
|
|
21
|
+
*/
|
|
22
|
+
import "reflect-metadata";
|
|
23
|
+
import type { HealthConfig } from "./types.js";
|
|
24
|
+
export declare class HealthModule {
|
|
25
|
+
static forRoot(config?: HealthConfig): {
|
|
26
|
+
new (): {};
|
|
27
|
+
};
|
|
28
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `HealthCheckService` — runs a list of indicators in parallel and
|
|
3
|
+
* aggregates the result.
|
|
4
|
+
*
|
|
5
|
+
* Typically injected into a controller that mounts the
|
|
6
|
+
* `/health/live`, `/health/ready`, `/health/startup` endpoints.
|
|
7
|
+
*/
|
|
8
|
+
import type { HealthCheckResult, HealthIndicator, HealthCheckKind, HealthConfig } from "./types.js";
|
|
9
|
+
export declare class HealthCheckService {
|
|
10
|
+
/** DI token — use with `@Inject(HealthCheckService.TOKEN)`. */
|
|
11
|
+
static readonly TOKEN: unique symbol;
|
|
12
|
+
/** Registered indicators keyed by name. */
|
|
13
|
+
indicators: Map<string, HealthIndicator>;
|
|
14
|
+
/** Public, read-only view of the resolved config. */
|
|
15
|
+
config: HealthConfig;
|
|
16
|
+
constructor(config?: HealthConfig);
|
|
17
|
+
/**
|
|
18
|
+
* Register an indicator at runtime (e.g. a DB-specific indicator
|
|
19
|
+
* from a feature module).
|
|
20
|
+
*/
|
|
21
|
+
register(indicator: HealthIndicator): void;
|
|
22
|
+
/** Remove a registered indicator. */
|
|
23
|
+
unregister(name: string): boolean;
|
|
24
|
+
/** List registered indicator names. */
|
|
25
|
+
list(): string[];
|
|
26
|
+
/**
|
|
27
|
+
* Run all registered indicators in parallel and aggregate.
|
|
28
|
+
*
|
|
29
|
+
* await health.check() → 200 if all 'up', 503 if any 'down'.
|
|
30
|
+
*/
|
|
31
|
+
check(kind?: HealthCheckKind): Promise<HealthCheckResult>;
|
|
32
|
+
private registerBuiltIns;
|
|
33
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Public API for `nexusjs/health`.
|
|
3
|
+
*
|
|
4
|
+
* Quick start:
|
|
5
|
+
* // src/app/app.module.ts
|
|
6
|
+
* import { Module } from 'nexusjs';
|
|
7
|
+
* import { HealthModule } from 'nexusjs/health';
|
|
8
|
+
*
|
|
9
|
+
* @Module({
|
|
10
|
+
* imports: [
|
|
11
|
+
* HealthModule.forRoot({
|
|
12
|
+
* builtIn: { memory: true, disk: { threshold: 0.1 } },
|
|
13
|
+
* }),
|
|
14
|
+
* ],
|
|
15
|
+
* })
|
|
16
|
+
* export class AppModule {}
|
|
17
|
+
*
|
|
18
|
+
* Endpoints (auto-mounted by HealthController):
|
|
19
|
+
* GET /health/live — fast in-process check (no DB ping)
|
|
20
|
+
* GET /health/ready — runs every registered indicator
|
|
21
|
+
* GET /health/startup — same as readiness; distinct path for K8s
|
|
22
|
+
*
|
|
23
|
+
* Response body:
|
|
24
|
+
* {
|
|
25
|
+
* "status": "up" | "down",
|
|
26
|
+
* "results": [{ "name": "memory", "result": { "status": "up", "data": {...} } }, ...],
|
|
27
|
+
* "durationMs": 12,
|
|
28
|
+
* "timestamp": "2026-06-20T12:00:00.000Z"
|
|
29
|
+
* }
|
|
30
|
+
*
|
|
31
|
+
* Status code: 200 when all 'up', 503 when any 'down'.
|
|
32
|
+
*/
|
|
33
|
+
export * from "./types.js";
|
|
34
|
+
export { HealthCheckService } from "./health.service.js";
|
|
35
|
+
export { HealthController } from "./health.controller.js";
|
|
36
|
+
export { HealthModule } from "./health.module.js";
|
|
37
|
+
export { MemoryHealthIndicator, DiskHealthIndicator, HttpHealthIndicator, CustomPingIndicator, } from "./indicators/index.js";
|
package/dist/index.js
CHANGED
|
@@ -1,20 +1,4 @@
|
|
|
1
1
|
// @bun
|
|
2
|
-
var __create = Object.create;
|
|
3
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
4
|
-
var __defProp = Object.defineProperty;
|
|
5
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
-
var __toESM = (mod, isNodeMode, target) => {
|
|
8
|
-
target = mod != null ? __create(__getProtoOf(mod)) : {};
|
|
9
|
-
const to = isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target;
|
|
10
|
-
for (let key of __getOwnPropNames(mod))
|
|
11
|
-
if (!__hasOwnProp.call(to, key))
|
|
12
|
-
__defProp(to, key, {
|
|
13
|
-
get: () => mod[key],
|
|
14
|
-
enumerable: true
|
|
15
|
-
});
|
|
16
|
-
return to;
|
|
17
|
-
};
|
|
18
2
|
var __legacyDecorateClassTS = function(decorators, target, key, desc) {
|
|
19
3
|
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
20
4
|
if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
|
|
@@ -36,7 +20,7 @@ var __require = import.meta.require;
|
|
|
36
20
|
class HealthIndicator {
|
|
37
21
|
}
|
|
38
22
|
// packages/health/src/health.service.ts
|
|
39
|
-
import { Inject, Injectable } from "@nexusts/core
|
|
23
|
+
import { Inject, Injectable } from "@nexusts/core";
|
|
40
24
|
|
|
41
25
|
// packages/health/src/indicators/drizzle.ts
|
|
42
26
|
class DrizzleHealthIndicator {
|
|
@@ -271,7 +255,7 @@ HealthCheckService = __legacyDecorateClassTS([
|
|
|
271
255
|
])
|
|
272
256
|
], HealthCheckService);
|
|
273
257
|
// packages/health/src/health.controller.ts
|
|
274
|
-
import { Controller, Get, Req, Res, Inject as Inject2 } from "@nexusts/core
|
|
258
|
+
import { Controller, Get, Req, Res, Inject as Inject2 } from "@nexusts/core";
|
|
275
259
|
class HealthController {
|
|
276
260
|
health;
|
|
277
261
|
constructor(health) {
|
|
@@ -334,7 +318,7 @@ HealthController = __legacyDecorateClassTS([
|
|
|
334
318
|
], HealthController);
|
|
335
319
|
// packages/health/src/health.module.ts
|
|
336
320
|
import"reflect-metadata";
|
|
337
|
-
import { Module } from "@nexusts/core
|
|
321
|
+
import { Module } from "@nexusts/core";
|
|
338
322
|
class HealthModule {
|
|
339
323
|
static forRoot(config = {}) {
|
|
340
324
|
class ConfiguredHealthModule {
|
|
@@ -377,5 +361,5 @@ export {
|
|
|
377
361
|
CustomPingIndicator
|
|
378
362
|
};
|
|
379
363
|
|
|
380
|
-
//# debugId=
|
|
364
|
+
//# debugId=1299AEB0BD61DB2664756E2164756E21
|
|
381
365
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -3,13 +3,13 @@
|
|
|
3
3
|
"sources": ["../src/types.ts", "../src/health.service.ts", "../src/indicators/drizzle.ts", "../src/indicators/index.ts", "../src/health.controller.ts", "../src/health.module.ts"],
|
|
4
4
|
"sourcesContent": [
|
|
5
5
|
"/**\n * Health check types — the contract for `nexusjs/health`.\n *\n * Mirrors `@nestjs/terminus` and `@adonisjs/health`. Three check\n * kinds:\n *\n * - **Liveness** — am I alive? Used by Kubernetes to decide when\n * to restart the pod. Should be a fast, in-process check.\n *\n * - **Readiness** — am I ready to serve traffic? Used by load\n * balancers and K8s to decide when to send requests. May include\n * DB / cache pings.\n *\n * - **Startup** — has my initialization finished? Used by K8s to\n * gate deployment rollouts.\n *\n * Each check returns a `HealthIndicatorResult`. `status: 'up'` means\n * the check passed; `down` means it failed (the indicator's data\n * carries the error message).\n */\n\nexport type HealthStatus = \"up\" | \"down\";\n\nexport interface HealthIndicatorResult<T = unknown> {\n\t/** Whether the check passed. */\n\tstatus: HealthStatus;\n\t/** Optional data attached to the check (e.g. ping latency). */\n\tdata?: T;\n\t/** Error message when status is 'down'. */\n\tmessage?: string;\n}\n\n/**\n * A single health indicator. Indicators are usually singletons that\n * wrap a connection (DB, cache, HTTP API). The `check()` method\n * performs a fast liveness probe.\n *\n * class DbHealthIndicator extends HealthIndicator {\n * name = 'database';\n * async check() {\n * await this.db.ping();\n * return { status: 'up' };\n * }\n * }\n */\nexport abstract class HealthIndicator {\n\tabstract readonly name: string;\n\tabstract check(): Promise<HealthIndicatorResult>;\n}\n\n/** Result of one check plus its indicator name. */\nexport interface HealthCheckEntry {\n\tname: string;\n\tresult: HealthIndicatorResult;\n}\n\n/** Result of `HealthCheckService.check([...])`. */\nexport interface HealthCheckResult {\n\t/** Aggregate status. `'up'` iff every indicator returned `'up'`. */\n\tstatus: HealthStatus;\n\t/** Per-indicator results. */\n\tresults: HealthCheckEntry[];\n\t/** Total wall-clock time (ms). */\n\tdurationMs: number;\n\t/** ISO timestamp. */\n\ttimestamp: string;\n}\n\n/** Which kind of check we're running. */\nexport type HealthCheckKind = \"liveness\" | \"readiness\" | \"startup\";\n\n/** Configuration for the HealthModule. */\nexport interface HealthConfig {\n\t/**\n\t * Path for the liveness probe. Default: `/health/live`.\n\t * Set to null to disable.\n\t */\n\tlivenessPath?: string | null;\n\t/**\n\t * Path for the readiness probe. Default: `/health/ready`.\n\t */\n\treadinessPath?: string | null;\n\t/**\n\t * Path for the startup probe. Default: `/health/startup`.\n\t */\n\tstartupPath?: string | null;\n\t/**\n\t * Optional token to gate the health endpoints. When set, requests\n\t * must include `Authorization: Bearer <token>`. Useful for\n\t * protecting internal health endpoints from public exposure.\n\t */\n\tauthToken?: string;\n\t/**\n\t * Built-in indicators to register automatically. Currently\n\t * supports 'memory' (heap pressure). 'disk' and 'http' require\n\t * additional config (see config docs).\n\t */\n\tbuiltIn?: {\n\t\tmemory?: boolean | { threshold?: number /* heap pressure 0-1 */ };\n\t\tdisk?: { threshold?: number /* fraction free, e.g. 0.1 */; path?: string };\n\t\thttp?: { url: string; timeoutMs?: number };\n\t};\n}\n",
|
|
6
|
-
"/**\n * `HealthCheckService` — runs a list of indicators in parallel and\n * aggregates the result.\n *\n * Typically injected into a controller that mounts the\n * `/health/live`, `/health/ready`, `/health/startup` endpoints.\n */\n\nimport { Inject, Injectable } from \"@nexusts/core
|
|
6
|
+
"/**\n * `HealthCheckService` — runs a list of indicators in parallel and\n * aggregates the result.\n *\n * Typically injected into a controller that mounts the\n * `/health/live`, `/health/ready`, `/health/startup` endpoints.\n */\n\nimport { Inject, Injectable } from \"@nexusts/core\";\nimport type {\n\tHealthCheckResult,\n\tHealthCheckEntry,\n\tHealthIndicator,\n\tHealthIndicatorResult,\n\tHealthCheckKind,\n\tHealthConfig,\n} from \"./types.js\";\nimport {\n\tMemoryHealthIndicator,\n\tDiskHealthIndicator,\n\tHttpHealthIndicator,\n} from \"./indicators/index.js\";\n\n@Injectable()\nexport class HealthCheckService {\n\t/** DI token — use with `@Inject(HealthCheckService.TOKEN)`. */\n\tstatic readonly TOKEN = Symbol.for(\"nexus:HealthCheckService\");\n\n\t/** Registered indicators keyed by name. */\n\tindicators = new Map<string, HealthIndicator>();\n\t/** Public, read-only view of the resolved config. */\n\tconfig: HealthConfig;\n\n\tconstructor(@Inject(\"HEALTH_CONFIG\") config: HealthConfig = {}) {\n\t\tthis.config = config;\n\t\tthis.registerBuiltIns();\n\t}\n\n\t/**\n\t * Register an indicator at runtime (e.g. a DB-specific indicator\n\t * from a feature module).\n\t */\n\tregister(indicator: HealthIndicator): void {\n\t\tthis.indicators.set(indicator.name, indicator);\n\t}\n\n\t/** Remove a registered indicator. */\n\tunregister(name: string): boolean {\n\t\treturn this.indicators.delete(name);\n\t}\n\n\t/** List registered indicator names. */\n\tlist(): string[] {\n\t\treturn [...this.indicators.keys()];\n\t}\n\n\t/**\n\t * Run all registered indicators in parallel and aggregate.\n\t *\n\t * await health.check() → 200 if all 'up', 503 if any 'down'.\n\t */\n\tasync check(kind: HealthCheckKind = \"readiness\"): Promise<HealthCheckResult> {\n\t\tconst start = Date.now();\n\t\tconst indicators = [...this.indicators.values()];\n\t\tconst settled = await Promise.allSettled(\n\t\t\tindicators.map((i) => i.check()),\n\t\t);\n\t\tconst entries: HealthCheckEntry[] = indicators.map((i, idx) => {\n\t\t\tconst s = settled[idx]!;\n\t\t\tif (s.status === \"fulfilled\") {\n\t\t\t\treturn { name: i.name, result: s.value };\n\t\t\t}\n\t\t\tconst err = s.reason;\n\t\t\treturn {\n\t\t\t\tname: i.name,\n\t\t\t\tresult: {\n\t\t\t\t\tstatus: \"down\",\n\t\t\t\t\tmessage: err instanceof Error ? err.message : String(err),\n\t\t\t\t},\n\t\t\t};\n\t\t});\n\t\tconst status = entries.every((e) => e.result.status === \"up\")\n\t\t\t? \"up\"\n\t\t\t: \"down\";\n\t\treturn {\n\t\t\tstatus,\n\t\t\tresults: entries,\n\t\t\tdurationMs: Date.now() - start,\n\t\t\ttimestamp: new Date().toISOString(),\n\t\t};\n\t}\n\n\t// ===========================================================================\n\t// Internal\n\t// ===========================================================================\n\n\tprivate registerBuiltIns(): void {\n\t\tconst bi = this.config.builtIn ?? {};\n\t\tif (bi.memory) {\n\t\t\tconst opts = typeof bi.memory === \"object\" ? bi.memory : {};\n\t\t\tthis.register(new MemoryHealthIndicator(opts));\n\t\t}\n\t\tif (bi.disk) {\n\t\t\tthis.register(new DiskHealthIndicator(bi.disk));\n\t\t}\n\t\tif (bi.http) {\n\t\t\t// Default name is derived from the URL host.\n\t\t\tconst host = (() => {\n\t\t\t\ttry {\n\t\t\t\t\treturn new URL(bi.http.url).host || \"http\";\n\t\t\t\t} catch {\n\t\t\t\t\treturn \"http\";\n\t\t\t\t}\n\t\t\t})();\n\t\t\tthis.register(new HttpHealthIndicator(host, bi.http));\n\t\t}\n\t}\n}",
|
|
7
7
|
"/**\n * DrizzleHealthIndicator — runs a `SELECT 1` against the database.\n *\n * new DrizzleHealthIndicator('database', drizzleService, { timeoutMs: 3000 })\n */\nimport type { HealthIndicator, HealthIndicatorResult } from \"../types.js\";\n\nexport class DrizzleHealthIndicator implements HealthIndicator {\n\treadonly name: string;\n\t#db: { rawQuery<T = unknown>(sql: string, params?: unknown[]): Promise<T[]> };\n\t#timeoutMs: number;\n\t/** Optional probe SQL. Default: 'SELECT 1'. */\n\t#probe: string;\n\n\tconstructor(\n\t\tname: string,\n\t\tdb: {\n\t\t\trawQuery<T = unknown>(sql: string, params?: unknown[]): Promise<T[]>;\n\t\t},\n\t\toptions: { timeoutMs?: number; probe?: string } = {},\n\t) {\n\t\tthis.name = name;\n\t\tthis.#db = db;\n\t\tthis.#timeoutMs = options.timeoutMs ?? 3000;\n\t\tthis.#probe = options.probe ?? \"SELECT 1\";\n\t}\n\n\tasync check(): Promise<HealthIndicatorResult> {\n\t\tconst start = Date.now();\n\t\ttry {\n\t\t\tconst probe = this.#probe;\n\t\t\tawait Promise.race([\n\t\t\t\tthis.#db.rawQuery(probe),\n\t\t\t\tnew Promise<never>((_, reject) =>\n\t\t\t\t\tsetTimeout(\n\t\t\t\t\t\t() =>\n\t\t\t\t\t\t\treject(new Error(`probe timed out after ${this.#timeoutMs}ms`)),\n\t\t\t\t\t\tthis.#timeoutMs,\n\t\t\t\t\t),\n\t\t\t\t),\n\t\t\t]);\n\t\t\treturn {\n\t\t\t\tstatus: \"up\",\n\t\t\t\tdata: { latencyMs: Date.now() - start, probe: this.#probe },\n\t\t\t};\n\t\t} catch (err) {\n\t\t\treturn {\n\t\t\t\tstatus: \"down\",\n\t\t\t\tmessage: err instanceof Error ? err.message : String(err),\n\t\t\t\tdata: { latencyMs: Date.now() - start, probe: this.#probe },\n\t\t\t};\n\t\t}\n\t}\n}\n",
|
|
8
8
|
"/**\n * Built-in health indicators.\n *\n * Each one extends `HealthIndicator` and lives in this folder so the\n * core service stays small. New built-ins (DB, Redis, ...) can be\n * added here.\n */\n\nexport { DrizzleHealthIndicator } from \"./drizzle.js\";\n\nimport type { HealthIndicator, HealthIndicatorResult } from \"../types.js\";\n\n/**\n * Memory pressure indicator. Reports `'down'` when heap usage\n * exceeds the configured threshold (default: 0.9 = 90%).\n */\nexport class MemoryHealthIndicator implements HealthIndicator {\n\treadonly name = \"memory\";\n\t#threshold: number;\n\n\tconstructor(options: { threshold?: number } = {}) {\n\t\tthis.#threshold = options.threshold ?? 0.9;\n\t}\n\n\tasync check(): Promise<HealthIndicatorResult> {\n\t\tconst mem = process.memoryUsage();\n\t\tconst total = mem.heapTotal;\n\t\tconst used = mem.heapUsed;\n\t\tconst ratio = total > 0 ? used / total : 0;\n\t\tif (ratio > this.#threshold) {\n\t\t\treturn {\n\t\t\t\tstatus: \"down\",\n\t\t\t\tmessage: `heap usage ${(ratio * 100).toFixed(1)}% exceeds threshold ${(this.#threshold * 100).toFixed(0)}%`,\n\t\t\t\tdata: { heapUsed: used, heapTotal: total, ratio },\n\t\t\t};\n\t\t}\n\t\treturn {\n\t\t\tstatus: \"up\",\n\t\t\tdata: { heapUsed: used, heapTotal: total, ratio },\n\t\t};\n\t}\n}\n\n/**\n * Disk space indicator. Reports `'down'` when free fraction falls\n * below the threshold.\n */\nexport class DiskHealthIndicator implements HealthIndicator {\n\treadonly name = \"disk\";\n\t#threshold: number;\n\t#path: string;\n\n\tconstructor(options: { threshold?: number; path?: string } = {}) {\n\t\tthis.#threshold = options.threshold ?? 0.1; // 10% free\n\t\tthis.#path = options.path ?? process.cwd();\n\t}\n\n\tasync check(): Promise<HealthIndicatorResult> {\n\t\ttry {\n\t\t\t// Best-effort: rely on Bun / Node to throw if statfs is unsupported.\n\t\t\t// We use a tiny shell-out only when the runtime exposes one.\n\t\t\t// Fall back to 'up' if we can't tell.\n\t\t\tconst statfs = (await import(\"node:fs/promises\")\n\t\t\t\t.then((m) => m.statfs)\n\t\t\t\t.catch(() => null)) as\n\t\t\t\t| ((p: string) => Promise<{\n\t\t\t\t\t\tbavail: number;\n\t\t\t\t\t\tbsize: number;\n\t\t\t\t\t\tblocks: number;\n\t\t\t\t\t\tbfree: number;\n\t\t\t\t }>)\n\t\t\t\t| null;\n\t\t\tif (!statfs) {\n\t\t\t\treturn { status: \"up\", message: \"statfs unavailable; skipping\" };\n\t\t\t}\n\t\t\tconst s = await statfs(this.#path);\n\t\t\tconst free = s.bavail * s.bsize;\n\t\t\tconst total = s.blocks * s.bsize;\n\t\t\tconst freeRatio = total > 0 ? free / total : 1;\n\t\t\tif (freeRatio < this.#threshold) {\n\t\t\t\treturn {\n\t\t\t\t\tstatus: \"down\",\n\t\t\t\t\tmessage: `disk free ${(freeRatio * 100).toFixed(1)}% below threshold ${(this.#threshold * 100).toFixed(0)}%`,\n\t\t\t\t\tdata: { free, total, freeRatio, path: this.#path },\n\t\t\t\t};\n\t\t\t}\n\t\t\treturn {\n\t\t\t\tstatus: \"up\",\n\t\t\t\tdata: { free, total, freeRatio, path: this.#path },\n\t\t\t};\n\t\t} catch (err) {\n\t\t\treturn {\n\t\t\t\tstatus: \"down\",\n\t\t\t\tmessage: err instanceof Error ? err.message : String(err),\n\t\t\t};\n\t\t}\n\t}\n}\n\n/**\n * HTTP ping indicator. GETs a URL and reports `'up'` on any 2xx.\n */\nexport class HttpHealthIndicator implements HealthIndicator {\n\treadonly name: string;\n\t#url: string;\n\t#timeoutMs: number;\n\n\tconstructor(name: string, options: { url: string; timeoutMs?: number }) {\n\t\tthis.name = name;\n\t\tthis.#url = options.url;\n\t\tthis.#timeoutMs = options.timeoutMs ?? 3000;\n\t}\n\n\tasync check(): Promise<HealthIndicatorResult> {\n\t\tconst ctrl = new AbortController();\n\t\tconst timer = setTimeout(() => ctrl.abort(), this.#timeoutMs);\n\t\ttry {\n\t\t\tconst res = await fetch(this.#url, { signal: ctrl.signal });\n\t\t\tif (res.status >= 200 && res.status < 300) {\n\t\t\t\treturn { status: \"up\", data: { status: res.status } };\n\t\t\t}\n\t\t\treturn {\n\t\t\t\tstatus: \"down\",\n\t\t\t\tmessage: `HTTP ${res.status}`,\n\t\t\t\tdata: { status: res.status },\n\t\t\t};\n\t\t} catch (err) {\n\t\t\treturn {\n\t\t\t\tstatus: \"down\",\n\t\t\t\tmessage: err instanceof Error ? err.message : String(err),\n\t\t\t};\n\t\t} finally {\n\t\t\tclearTimeout(timer);\n\t\t}\n\t}\n}\n\n/**\n * User-supplied ping indicator. Wrap a `ping()` function — typically\n * a DB driver's health check.\n *\n * new CustomPingIndicator('database', async () => db.ping())\n */\nexport class CustomPingIndicator implements HealthIndicator {\n\treadonly name: string;\n\t#ping: () => Promise<void> | void;\n\t#timeoutMs: number;\n\n\tconstructor(\n\t\tname: string,\n\t\tping: () => Promise<void> | void,\n\t\ttimeoutMs = 3000,\n\t) {\n\t\tthis.name = name;\n\t\tthis.#ping = ping;\n\t\tthis.#timeoutMs = timeoutMs;\n\t}\n\n\tasync check(): Promise<HealthIndicatorResult> {\n\t\tconst ctrl = new AbortController();\n\t\tconst timer = setTimeout(() => ctrl.abort(), this.#timeoutMs);\n\t\ttry {\n\t\t\tawait Promise.race([\n\t\t\t\tPromise.resolve(this.#ping()),\n\t\t\t\tnew Promise<never>((_, reject) =>\n\t\t\t\t\tsetTimeout(\n\t\t\t\t\t\t() =>\n\t\t\t\t\t\t\treject(new Error(`ping timed out after ${this.#timeoutMs}ms`)),\n\t\t\t\t\t\tthis.#timeoutMs,\n\t\t\t\t\t),\n\t\t\t\t),\n\t\t\t]);\n\t\t\treturn { status: \"up\" };\n\t\t} catch (err) {\n\t\t\treturn {\n\t\t\t\tstatus: \"down\",\n\t\t\t\tmessage: err instanceof Error ? err.message : String(err),\n\t\t\t};\n\t\t} finally {\n\t\t\tclearTimeout(timer);\n\t\t}\n\t}\n}\n",
|
|
9
|
-
"/**\n * `HealthController` — built-in `/health/live`, `/health/ready`,\n * `/health/startup` endpoints.\n *\n * Mount `HealthModule.forRoot({...})` in your app module to get\n * these routes automatically. Override paths or add an auth token\n * via `HealthConfig`.\n */\n\nimport { Controller, Get, Req, Res, Inject } from \"@nexusts/core
|
|
10
|
-
"/**\n * `HealthModule` — drop-in module for `/health/live`, `/health/ready`,\n * `/health/startup` endpoints.\n *\n * Usage:\n * @Module({\n * imports: [\n * HealthModule.forRoot({\n * builtIn: {\n * memory: true,\n * disk: { threshold: 0.1 },\n * http: { url: 'https://api.stripe.com/v1/healthcheck' },\n * },\n * }),\n * ],\n * })\n * export class AppModule {}\n *\n * Then `/health/live`, `/health/ready`, `/health/startup` respond\n * with a JSON body. Status 200 on `'up'`, 503 on `'down'`.\n */\n\nimport \"reflect-metadata\";\nimport { Module } from \"@nexusts/core
|
|
9
|
+
"/**\n * `HealthController` — built-in `/health/live`, `/health/ready`,\n * `/health/startup` endpoints.\n *\n * Mount `HealthModule.forRoot({...})` in your app module to get\n * these routes automatically. Override paths or add an auth token\n * via `HealthConfig`.\n */\n\nimport { Controller, Get, Req, Res, Inject } from \"@nexusts/core\";\nimport type { Context } from \"hono\";\nimport { HealthCheckService } from \"./health.service.js\";\nimport type { HealthCheckKind, HealthConfig } from \"./types.js\";\n\n@Controller()\nexport class HealthController {\n\tconstructor(@Inject(HealthCheckService.TOKEN) private readonly health: HealthCheckService) {}\n\n\t@Get(\"/health/live\")\n\tasync live(@Req() c: Context, @Res() _res: Response) {\n\t\treturn this.respond(c, \"liveness\", this.health.config.livenessPath ?? \"/health/live\");\n\t}\n\n\t@Get(\"/health/ready\")\n\tasync ready(@Req() c: Context, @Res() _res: Response) {\n\t\treturn this.respond(c, \"readiness\", this.health.config.readinessPath ?? \"/health/ready\");\n\t}\n\n\t@Get(\"/health/startup\")\n\tasync startup(@Req() c: Context, @Res() _res: Response) {\n\t\treturn this.respond(c, \"startup\", this.health.config.startupPath ?? \"/health/startup\");\n\t}\n\n\tprivate async respond(c: Context, kind: HealthCheckKind, _configuredPath: string) {\n\t\tconst result = await this.health.check(kind);\n\t\tconst status = result.status === \"up\" ? 200 : 503;\n\t\treturn c.json(result, status);\n\t}\n}\n\n// Augment HealthCheckService to expose config (used by the controller).\ndeclare module \"./health.service.js\" {\n\tinterface HealthCheckService {\n\t\tconfig: HealthConfig;\n\t}\n}",
|
|
10
|
+
"/**\n * `HealthModule` — drop-in module for `/health/live`, `/health/ready`,\n * `/health/startup` endpoints.\n *\n * Usage:\n * @Module({\n * imports: [\n * HealthModule.forRoot({\n * builtIn: {\n * memory: true,\n * disk: { threshold: 0.1 },\n * http: { url: 'https://api.stripe.com/v1/healthcheck' },\n * },\n * }),\n * ],\n * })\n * export class AppModule {}\n *\n * Then `/health/live`, `/health/ready`, `/health/startup` respond\n * with a JSON body. Status 200 on `'up'`, 503 on `'down'`.\n */\n\nimport \"reflect-metadata\";\nimport { Module } from \"@nexusts/core\";\nimport { HealthCheckService } from \"./health.service.js\";\nimport { HealthController } from \"./health.controller.js\";\nimport type { HealthConfig } from \"./types.js\";\n\n@Module({\n\tcontrollers: [HealthController],\n\tproviders: [\n\t\tHealthCheckService,\n\t\t{ provide: HealthCheckService.TOKEN, useExisting: HealthCheckService },\n\t],\n\texports: [HealthCheckService, HealthCheckService.TOKEN],\n})\nexport class HealthModule {\n\tstatic forRoot(config: HealthConfig = {}) {\n\t\t@Module({\n\t\t\tcontrollers: [HealthController],\n\t\t\tproviders: [\n\t\t\t\tHealthCheckService,\n\t\t\t\t{ provide: HealthCheckService.TOKEN, useExisting: HealthCheckService },\n\t\t\t\t{ provide: \"HEALTH_CONFIG\", useValue: config },\n\t\t\t],\n\t\t\texports: [HealthCheckService, HealthCheckService.TOKEN],\n\t\t})\n\t\tclass ConfiguredHealthModule {}\n\n\t\tObject.defineProperty(ConfiguredHealthModule, \"name\", {\n\t\t\tvalue: \"ConfiguredHealthModule\",\n\t\t});\n\n\t\treturn ConfiguredHealthModule;\n\t}\n}\n"
|
|
11
11
|
],
|
|
12
|
-
"mappings": "
|
|
13
|
-
"debugId": "
|
|
12
|
+
"mappings": ";;;;;;;;;;;;;;;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;AAAA;AAAA,GAdM,iBAcN;AAdM,mBAAN;AAAA,EADN,WAAW;AAAA,EAEE,mCAAO,mBAAmB,KAAK;AAAA,EADtC;AAAA;AAAA;AAAA,GAAM;;ACOb;AACA;AAaO,MAAM,aAAa;AAAA,SAClB,OAAO,CAAC,SAAuB,CAAC,GAAG;AAAA,IAUzC,MAAM,uBAAuB;AAAA,IAAC;AAAA,IAAxB,yBAAN;AAAA,MATC,OAAO;AAAA,QACP,aAAa,CAAC,gBAAgB;AAAA,QAC9B,WAAW;AAAA,UACV;AAAA,UACA,EAAE,SAAS,mBAAmB,OAAO,aAAa,mBAAmB;AAAA,UACrE,EAAE,SAAS,iBAAiB,UAAU,OAAO;AAAA,QAC9C;AAAA,QACA,SAAS,CAAC,oBAAoB,mBAAmB,KAAK;AAAA,MACvD,CAAC;AAAA,OACK;AAAA,IAEN,OAAO,eAAe,wBAAwB,QAAQ;AAAA,MACrD,OAAO;AAAA,IACR,CAAC;AAAA,IAED,OAAO;AAAA;AAET;AAnBa,eAAN;AAAA,EARN,OAAO;AAAA,IACP,aAAa,CAAC,gBAAgB;AAAA,IAC9B,WAAW;AAAA,MACV;AAAA,MACA,EAAE,SAAS,mBAAmB,OAAO,aAAa,mBAAmB;AAAA,IACtE;AAAA,IACA,SAAS,CAAC,oBAAoB,mBAAmB,KAAK;AAAA,EACvD,CAAC;AAAA,GACY;",
|
|
13
|
+
"debugId": "1299AEB0BD61DB2664756E2164756E21",
|
|
14
14
|
"names": []
|
|
15
15
|
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* DrizzleHealthIndicator — runs a `SELECT 1` against the database.
|
|
3
|
+
*
|
|
4
|
+
* new DrizzleHealthIndicator('database', drizzleService, { timeoutMs: 3000 })
|
|
5
|
+
*/
|
|
6
|
+
import type { HealthIndicator, HealthIndicatorResult } from "../types.js";
|
|
7
|
+
export declare class DrizzleHealthIndicator implements HealthIndicator {
|
|
8
|
+
#private;
|
|
9
|
+
readonly name: string;
|
|
10
|
+
constructor(name: string, db: {
|
|
11
|
+
rawQuery<T = unknown>(sql: string, params?: unknown[]): Promise<T[]>;
|
|
12
|
+
}, options?: {
|
|
13
|
+
timeoutMs?: number;
|
|
14
|
+
probe?: string;
|
|
15
|
+
});
|
|
16
|
+
check(): Promise<HealthIndicatorResult>;
|
|
17
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Built-in health indicators.
|
|
3
|
+
*
|
|
4
|
+
* Each one extends `HealthIndicator` and lives in this folder so the
|
|
5
|
+
* core service stays small. New built-ins (DB, Redis, ...) can be
|
|
6
|
+
* added here.
|
|
7
|
+
*/
|
|
8
|
+
export { DrizzleHealthIndicator } from "./drizzle.js";
|
|
9
|
+
import type { HealthIndicator, HealthIndicatorResult } from "../types.js";
|
|
10
|
+
/**
|
|
11
|
+
* Memory pressure indicator. Reports `'down'` when heap usage
|
|
12
|
+
* exceeds the configured threshold (default: 0.9 = 90%).
|
|
13
|
+
*/
|
|
14
|
+
export declare class MemoryHealthIndicator implements HealthIndicator {
|
|
15
|
+
#private;
|
|
16
|
+
readonly name = "memory";
|
|
17
|
+
constructor(options?: {
|
|
18
|
+
threshold?: number;
|
|
19
|
+
});
|
|
20
|
+
check(): Promise<HealthIndicatorResult>;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Disk space indicator. Reports `'down'` when free fraction falls
|
|
24
|
+
* below the threshold.
|
|
25
|
+
*/
|
|
26
|
+
export declare class DiskHealthIndicator implements HealthIndicator {
|
|
27
|
+
#private;
|
|
28
|
+
readonly name = "disk";
|
|
29
|
+
constructor(options?: {
|
|
30
|
+
threshold?: number;
|
|
31
|
+
path?: string;
|
|
32
|
+
});
|
|
33
|
+
check(): Promise<HealthIndicatorResult>;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* HTTP ping indicator. GETs a URL and reports `'up'` on any 2xx.
|
|
37
|
+
*/
|
|
38
|
+
export declare class HttpHealthIndicator implements HealthIndicator {
|
|
39
|
+
#private;
|
|
40
|
+
readonly name: string;
|
|
41
|
+
constructor(name: string, options: {
|
|
42
|
+
url: string;
|
|
43
|
+
timeoutMs?: number;
|
|
44
|
+
});
|
|
45
|
+
check(): Promise<HealthIndicatorResult>;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* User-supplied ping indicator. Wrap a `ping()` function — typically
|
|
49
|
+
* a DB driver's health check.
|
|
50
|
+
*
|
|
51
|
+
* new CustomPingIndicator('database', async () => db.ping())
|
|
52
|
+
*/
|
|
53
|
+
export declare class CustomPingIndicator implements HealthIndicator {
|
|
54
|
+
#private;
|
|
55
|
+
readonly name: string;
|
|
56
|
+
constructor(name: string, ping: () => Promise<void> | void, timeoutMs?: number);
|
|
57
|
+
check(): Promise<HealthIndicatorResult>;
|
|
58
|
+
}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Health check types — the contract for `nexusjs/health`.
|
|
3
|
+
*
|
|
4
|
+
* Mirrors `@nestjs/terminus` and `@adonisjs/health`. Three check
|
|
5
|
+
* kinds:
|
|
6
|
+
*
|
|
7
|
+
* - **Liveness** — am I alive? Used by Kubernetes to decide when
|
|
8
|
+
* to restart the pod. Should be a fast, in-process check.
|
|
9
|
+
*
|
|
10
|
+
* - **Readiness** — am I ready to serve traffic? Used by load
|
|
11
|
+
* balancers and K8s to decide when to send requests. May include
|
|
12
|
+
* DB / cache pings.
|
|
13
|
+
*
|
|
14
|
+
* - **Startup** — has my initialization finished? Used by K8s to
|
|
15
|
+
* gate deployment rollouts.
|
|
16
|
+
*
|
|
17
|
+
* Each check returns a `HealthIndicatorResult`. `status: 'up'` means
|
|
18
|
+
* the check passed; `down` means it failed (the indicator's data
|
|
19
|
+
* carries the error message).
|
|
20
|
+
*/
|
|
21
|
+
export type HealthStatus = "up" | "down";
|
|
22
|
+
export interface HealthIndicatorResult<T = unknown> {
|
|
23
|
+
/** Whether the check passed. */
|
|
24
|
+
status: HealthStatus;
|
|
25
|
+
/** Optional data attached to the check (e.g. ping latency). */
|
|
26
|
+
data?: T;
|
|
27
|
+
/** Error message when status is 'down'. */
|
|
28
|
+
message?: string;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* A single health indicator. Indicators are usually singletons that
|
|
32
|
+
* wrap a connection (DB, cache, HTTP API). The `check()` method
|
|
33
|
+
* performs a fast liveness probe.
|
|
34
|
+
*
|
|
35
|
+
* class DbHealthIndicator extends HealthIndicator {
|
|
36
|
+
* name = 'database';
|
|
37
|
+
* async check() {
|
|
38
|
+
* await this.db.ping();
|
|
39
|
+
* return { status: 'up' };
|
|
40
|
+
* }
|
|
41
|
+
* }
|
|
42
|
+
*/
|
|
43
|
+
export declare abstract class HealthIndicator {
|
|
44
|
+
abstract readonly name: string;
|
|
45
|
+
abstract check(): Promise<HealthIndicatorResult>;
|
|
46
|
+
}
|
|
47
|
+
/** Result of one check plus its indicator name. */
|
|
48
|
+
export interface HealthCheckEntry {
|
|
49
|
+
name: string;
|
|
50
|
+
result: HealthIndicatorResult;
|
|
51
|
+
}
|
|
52
|
+
/** Result of `HealthCheckService.check([...])`. */
|
|
53
|
+
export interface HealthCheckResult {
|
|
54
|
+
/** Aggregate status. `'up'` iff every indicator returned `'up'`. */
|
|
55
|
+
status: HealthStatus;
|
|
56
|
+
/** Per-indicator results. */
|
|
57
|
+
results: HealthCheckEntry[];
|
|
58
|
+
/** Total wall-clock time (ms). */
|
|
59
|
+
durationMs: number;
|
|
60
|
+
/** ISO timestamp. */
|
|
61
|
+
timestamp: string;
|
|
62
|
+
}
|
|
63
|
+
/** Which kind of check we're running. */
|
|
64
|
+
export type HealthCheckKind = "liveness" | "readiness" | "startup";
|
|
65
|
+
/** Configuration for the HealthModule. */
|
|
66
|
+
export interface HealthConfig {
|
|
67
|
+
/**
|
|
68
|
+
* Path for the liveness probe. Default: `/health/live`.
|
|
69
|
+
* Set to null to disable.
|
|
70
|
+
*/
|
|
71
|
+
livenessPath?: string | null;
|
|
72
|
+
/**
|
|
73
|
+
* Path for the readiness probe. Default: `/health/ready`.
|
|
74
|
+
*/
|
|
75
|
+
readinessPath?: string | null;
|
|
76
|
+
/**
|
|
77
|
+
* Path for the startup probe. Default: `/health/startup`.
|
|
78
|
+
*/
|
|
79
|
+
startupPath?: string | null;
|
|
80
|
+
/**
|
|
81
|
+
* Optional token to gate the health endpoints. When set, requests
|
|
82
|
+
* must include `Authorization: Bearer <token>`. Useful for
|
|
83
|
+
* protecting internal health endpoints from public exposure.
|
|
84
|
+
*/
|
|
85
|
+
authToken?: string;
|
|
86
|
+
/**
|
|
87
|
+
* Built-in indicators to register automatically. Currently
|
|
88
|
+
* supports 'memory' (heap pressure). 'disk' and 'http' require
|
|
89
|
+
* additional config (see config docs).
|
|
90
|
+
*/
|
|
91
|
+
builtIn?: {
|
|
92
|
+
memory?: boolean | {
|
|
93
|
+
threshold?: number;
|
|
94
|
+
};
|
|
95
|
+
disk?: {
|
|
96
|
+
threshold?: number;
|
|
97
|
+
path?: string;
|
|
98
|
+
};
|
|
99
|
+
http?: {
|
|
100
|
+
url: string;
|
|
101
|
+
timeoutMs?: number;
|
|
102
|
+
};
|
|
103
|
+
};
|
|
104
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nexusts/health",
|
|
3
|
-
"version": "0.7.
|
|
3
|
+
"version": "0.7.2",
|
|
4
4
|
"description": "Health check endpoints (live, ready, startup)",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -26,6 +26,6 @@
|
|
|
26
26
|
],
|
|
27
27
|
"license": "MIT",
|
|
28
28
|
"dependencies": {
|
|
29
|
-
"@nexusts/core": "^0.7.
|
|
29
|
+
"@nexusts/core": "^0.7.2"
|
|
30
30
|
}
|
|
31
31
|
}
|