@nathapp/nestjs-health 4.0.0 → 4.0.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/dist/interface/health-module-options.interface.d.ts +1 -0
- package/dist/interface/health-module-options.interface.js.map +1 -1
- package/dist/service/health-executor.service.js +11 -1
- package/dist/service/health-executor.service.js.map +1 -1
- package/package.json +5 -5
- package/.eslintrc.js +0 -8
- package/.nax/features/nestjs-health/.nax-acceptance.test.ts +0 -756
- package/.turbo/turbo-build.log +0 -4
- package/CHANGELOG.md +0 -45
- package/lib/constant/health.constants.ts +0 -23
- package/lib/constant/index.ts +0 -1
- package/lib/controller/health.controller.ts +0 -37
- package/lib/controller/index.ts +0 -1
- package/lib/index.ts +0 -6
- package/lib/indicator/cache-health.check.ts +0 -59
- package/lib/indicator/generic-health.check.ts +0 -43
- package/lib/indicator/index.ts +0 -4
- package/lib/indicator/prisma-health.check.ts +0 -59
- package/lib/indicator/queue-health.check.ts +0 -72
- package/lib/interface/health-indicator.interface.ts +0 -18
- package/lib/interface/health-module-options.interface.ts +0 -85
- package/lib/interface/index.ts +0 -2
- package/lib/module/health.module.ts +0 -62
- package/lib/module/index.ts +0 -1
- package/lib/service/health-executor.service.ts +0 -69
- package/lib/service/index.ts +0 -1
- package/test/constant/health.constants.spec.ts +0 -34
- package/test/controller/health.controller.spec.ts +0 -117
- package/test/indicator/cache-health.check.spec.ts +0 -88
- package/test/indicator/generic-health.check.spec.ts +0 -61
- package/test/indicator/prisma-health.check.spec.ts +0 -69
- package/test/indicator/queue-health.check.spec.ts +0 -88
- package/test/interface/health-module-options.spec.ts +0 -59
- package/test/jest-nax.json +0 -27
- package/test/jest-test.json +0 -28
- package/test/module/health.module.spec.ts +0 -311
- package/test/service/health-executor.service.spec.ts +0 -186
- package/tsconfig.json +0 -9
package/.turbo/turbo-build.log
DELETED
package/CHANGELOG.md
DELETED
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
# @nathapp/nestjs-health
|
|
2
|
-
|
|
3
|
-
## 4.0.0
|
|
4
|
-
|
|
5
|
-
### Major Changes
|
|
6
|
-
|
|
7
|
-
- 2319e9d: BREAKING: drop NestJS 8/9 peer ranges and require Node ^20.19.0 || >=22.12.0. Support NestJS 12. Peer ranges now cover NestJS 10, 11 and 12 (Node ^20.19.0 || >=22.12.0 is required because NestJS 12 is ESM-only). The configurable module builder no longer deep-imports a directory that is unresolvable on NestJS 12, and TwoFactorGuard / TwoFactorForceGuard redeclare their constructor DI metadata because NestJS 12 no longer inherits @Optional() from base classes.
|
|
8
|
-
|
|
9
|
-
Other consumer-facing changes:
|
|
10
|
-
|
|
11
|
-
- `@nathapp/nestjs-health`: `@nestjs/terminus` moved from `dependencies` to `peerDependencies` (`^11.0.0 || ^12.0.0`) so NestJS 12 apps can use terminus 12. Install `@nestjs/terminus` in your app.
|
|
12
|
-
- Optional-package loading (`loadPackage`) now throws a descriptive `Error` (original error on `cause`) instead of logging and calling `process.exit(1)` when an optional package is missing.
|
|
13
|
-
|
|
14
|
-
## 4.0.0-rc.0
|
|
15
|
-
|
|
16
|
-
### Major Changes
|
|
17
|
-
|
|
18
|
-
- c3aeb46: BREAKING: drop NestJS 8/9 peer ranges and require Node ^20.19.0 || >=22.12.0. Support NestJS 12. Peer ranges now cover NestJS 10, 11 and 12 (Node ^20.19.0 || >=22.12.0 is required because NestJS 12 is ESM-only). The configurable module builder no longer deep-imports a directory that is unresolvable on NestJS 12, and TwoFactorGuard / TwoFactorForceGuard redeclare their constructor DI metadata because NestJS 12 no longer inherits @Optional() from base classes.
|
|
19
|
-
|
|
20
|
-
Other consumer-facing changes:
|
|
21
|
-
|
|
22
|
-
- `@nathapp/nestjs-health`: `@nestjs/terminus` moved from `dependencies` to `peerDependencies` (`^11.0.0 || ^12.0.0`) so NestJS 12 apps can use terminus 12. Install `@nestjs/terminus` in your app.
|
|
23
|
-
- Optional-package loading (`loadPackage`) now throws a descriptive `Error` (original error on `cause`) instead of logging and calling `process.exit(1)` when an optional package is missing.
|
|
24
|
-
|
|
25
|
-
### Patch Changes
|
|
26
|
-
|
|
27
|
-
- Updated dependencies [c3aeb46]
|
|
28
|
-
- @nathapp/nestjs-cache@4.0.0-rc.0
|
|
29
|
-
- @nathapp/nestjs-common@4.0.0-rc.0
|
|
30
|
-
- @nathapp/nestjs-prisma@4.0.0-rc.0
|
|
31
|
-
- @nathapp/nestjs-queue@4.0.0-rc.0
|
|
32
|
-
|
|
33
|
-
## 3.3.0
|
|
34
|
-
|
|
35
|
-
### Minor Changes
|
|
36
|
-
|
|
37
|
-
- a27440f: 3.3.0 milestone release. The `@nathapp/nestjs-*` packages now version and publish in lockstep at a unified version.
|
|
38
|
-
|
|
39
|
-
- **nestjs-csrf**: first public release — stateless double-submit-cookie CSRF guard (zero-dep HMAC, Express + Fastify, Bearer coexist skip, `@SkipCsrf()`, auto-binds `APP_GUARD`).
|
|
40
|
-
- **nestjs-health**: first public release — terminus aggregator over cache/prisma/queue with k8s liveness/readiness probes via a dynamic controller factory.
|
|
41
|
-
- **nestjs-idempotency**: first public release — `@Idempotent()` interceptor (zero-dep sha256 fingerprint, Express + Fastify, 409-in-flight, 2xx replay, fingerprint mismatch 422, fail-closed on store error).
|
|
42
|
-
- **nestjs-observability**: first public release — Prometheus RED metrics + OpenTelemetry tracing joined to the logging ALS; `initTracing()` pre-bootstrap hook.
|
|
43
|
-
- **nestjs-outbox**: first public release — transactional outbox relay (at-least-once via `ITransactionManager`, lease-claim + backoff, dead-letter).
|
|
44
|
-
- **nestjs-storage**: migrated from aws-sdk v2 to v3 (zero runtime dep via `import type` + lazy `loadPackage`); NFSStorage stub replaced with an S3-style HTTP file-service client.
|
|
45
|
-
- **nestjs-cache**: `delByPattern` now routes through Redis SCAN instead of an O(n) key sweep, with glob-detection and multi-tier iterator support.
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
/** Default route path for the liveness probe. */
|
|
2
|
-
export const DEFAULT_LIVENESS_PATH = 'health/live';
|
|
3
|
-
|
|
4
|
-
/** Default route path for the readiness probe. */
|
|
5
|
-
export const DEFAULT_READINESS_PATH = 'health/ready';
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* DI token for the pluggable cache health source.
|
|
9
|
-
* Uses Symbol.for so the token is identical across realms / duplicate installs.
|
|
10
|
-
*/
|
|
11
|
-
export const CACHE_HEALTH_SOURCE = Symbol.for('CACHE_HEALTH_SOURCE');
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* DI token for the pluggable Prisma health source.
|
|
15
|
-
* Uses Symbol.for so the token is identical across realms / duplicate installs.
|
|
16
|
-
*/
|
|
17
|
-
export const PRISMA_HEALTH_SOURCE = Symbol.for('PRISMA_HEALTH_SOURCE');
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* DI token for the pluggable queue health source.
|
|
21
|
-
* Uses Symbol.for so the token is identical across realms / duplicate installs.
|
|
22
|
-
*/
|
|
23
|
-
export const QUEUE_HEALTH_SOURCE = Symbol.for('QUEUE_HEALTH_SOURCE');
|
package/lib/constant/index.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from './health.constants';
|
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
import { Controller, Get, Type } from '@nestjs/common';
|
|
2
|
-
import { HealthCheck } from '@nestjs/terminus';
|
|
3
|
-
import { HealthExecutorService } from '../service/health-executor.service';
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Builds a NestJS controller class whose `@Get()` paths are **closed over**
|
|
7
|
-
* the resolved `livenessPath` / `readinessPath` options supplied at
|
|
8
|
-
* `register()` time.
|
|
9
|
-
*
|
|
10
|
-
* The factory exists because decorator metadata (e.g. `@Get('foo')`) is
|
|
11
|
-
* captured statically at class-definition time — there is no Nest-supported
|
|
12
|
-
* way to read a runtime option inside a `@Get()` argument. Returning a fresh
|
|
13
|
-
* class per options object is the standard escape hatch.
|
|
14
|
-
*/
|
|
15
|
-
export function createHealthController(paths: {
|
|
16
|
-
livenessPath: string;
|
|
17
|
-
readinessPath: string;
|
|
18
|
-
}): Type<unknown> {
|
|
19
|
-
@Controller()
|
|
20
|
-
class HealthController {
|
|
21
|
-
constructor(private readonly executor: HealthExecutorService) {}
|
|
22
|
-
|
|
23
|
-
@Get(paths.livenessPath)
|
|
24
|
-
@HealthCheck()
|
|
25
|
-
live() {
|
|
26
|
-
return this.executor.runLiveness();
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
@Get(paths.readinessPath)
|
|
30
|
-
@HealthCheck()
|
|
31
|
-
ready() {
|
|
32
|
-
return this.executor.runReadiness();
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
return HealthController;
|
|
37
|
-
}
|
package/lib/controller/index.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from './health.controller';
|
package/lib/index.ts
DELETED
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
import { Inject, Injectable, Optional } from '@nestjs/common';
|
|
2
|
-
import { HealthIndicatorService } from '@nestjs/terminus';
|
|
3
|
-
import { CACHE_HEALTH_SOURCE } from '../constant/health.constants';
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Result shape produced by a cache health source.
|
|
7
|
-
* Expressed as `import type` only at consumer sites so this package does not
|
|
8
|
-
* require `@nathapp/nestjs-cache` to be installed at load time.
|
|
9
|
-
*/
|
|
10
|
-
export interface CacheHealthSource {
|
|
11
|
-
isHealthy(): Promise<{ healthy: boolean; connections?: unknown[] }>;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
/** Default key used when callers don't pass one to {@link CacheHealthCheck.isHealthy}. */
|
|
15
|
-
export const DEFAULT_CACHE_KEY = 'cache';
|
|
16
|
-
|
|
17
|
-
type TerminusResult = { [k: string]: { status: 'up' | 'down' } };
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* Terminus-compatible health check that adapts a pluggable cache source
|
|
21
|
-
* bound to {@link CACHE_HEALTH_SOURCE}.
|
|
22
|
-
*
|
|
23
|
-
* Behaviour:
|
|
24
|
-
* - No source bound (consumer did not provide the token) → `down` with
|
|
25
|
-
* `reason: 'source not provided'`. Never throws.
|
|
26
|
-
* - Source returns `{ healthy: true, connections }` → `up` with the
|
|
27
|
-
* `connections` array on the indicator.
|
|
28
|
-
* - Source returns `{ healthy: false, ... }` → `down` with the
|
|
29
|
-
* `connections` array on the indicator.
|
|
30
|
-
* - Source rejects → `down` with the error message. Never throws out.
|
|
31
|
-
*/
|
|
32
|
-
@Injectable()
|
|
33
|
-
export class CacheHealthCheck {
|
|
34
|
-
constructor(
|
|
35
|
-
private readonly healthIndicatorService: HealthIndicatorService,
|
|
36
|
-
@Optional()
|
|
37
|
-
@Inject(CACHE_HEALTH_SOURCE)
|
|
38
|
-
private readonly source?: CacheHealthSource,
|
|
39
|
-
) {}
|
|
40
|
-
|
|
41
|
-
async isHealthy(key: string = DEFAULT_CACHE_KEY): Promise<TerminusResult> {
|
|
42
|
-
const indicator = this.healthIndicatorService.check(key);
|
|
43
|
-
|
|
44
|
-
if (!this.source) {
|
|
45
|
-
return indicator.down({ reason: 'source not provided' }) as unknown as TerminusResult;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
try {
|
|
49
|
-
const result = await this.source.isHealthy();
|
|
50
|
-
const details = { connections: result.connections };
|
|
51
|
-
return result.healthy
|
|
52
|
-
? (indicator.up(details) as unknown as TerminusResult)
|
|
53
|
-
: (indicator.down(details) as unknown as TerminusResult);
|
|
54
|
-
} catch (err) {
|
|
55
|
-
const message = err instanceof Error ? err.message : String(err);
|
|
56
|
-
return indicator.down({ error: message }) as unknown as TerminusResult;
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
}
|
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
import { Injectable, Optional } from '@nestjs/common';
|
|
2
|
-
import { HealthIndicatorService } from '@nestjs/terminus';
|
|
3
|
-
import type {
|
|
4
|
-
HealthIndicatorSource,
|
|
5
|
-
HealthNormalize,
|
|
6
|
-
} from '../interface/health-indicator.interface';
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* Reusable Terminus-compatible health check that adapts an arbitrary
|
|
10
|
-
* `{ isHealthy() }` source through a caller-supplied `normalize` predicate.
|
|
11
|
-
*
|
|
12
|
-
* Source failures (rejections) are caught and surfaced as `down` so the
|
|
13
|
-
* wrapping executor never sees an exception from this check.
|
|
14
|
-
*/
|
|
15
|
-
@Injectable()
|
|
16
|
-
export class GenericHealthCheck<TResult = unknown> {
|
|
17
|
-
constructor(
|
|
18
|
-
private readonly healthIndicatorService: HealthIndicatorService,
|
|
19
|
-
@Optional() private readonly source?: HealthIndicatorSource<TResult>,
|
|
20
|
-
@Optional() private readonly normalize?: HealthNormalize<TResult>,
|
|
21
|
-
) {}
|
|
22
|
-
|
|
23
|
-
async isHealthy(key = 'generic'): Promise<{ [k: string]: { status: 'up' | 'down' } }> {
|
|
24
|
-
if (!this.source || !this.normalize) {
|
|
25
|
-
const session = this.healthIndicatorService.check(key);
|
|
26
|
-
return session.down();
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
let result: TResult;
|
|
30
|
-
try {
|
|
31
|
-
result = await this.source.isHealthy();
|
|
32
|
-
} catch (err) {
|
|
33
|
-
return this.healthIndicatorService
|
|
34
|
-
.check(key)
|
|
35
|
-
.down({ error: err instanceof Error ? err.message : String(err) });
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
const isUp = this.normalize(result);
|
|
39
|
-
return isUp
|
|
40
|
-
? this.healthIndicatorService.check(key).up()
|
|
41
|
-
: this.healthIndicatorService.check(key).down();
|
|
42
|
-
}
|
|
43
|
-
}
|
package/lib/indicator/index.ts
DELETED
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
import { Inject, Injectable, Optional } from '@nestjs/common';
|
|
2
|
-
import { HealthIndicatorService } from '@nestjs/terminus';
|
|
3
|
-
import { PRISMA_HEALTH_SOURCE } from '../constant/health.constants';
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Result shape produced by a Prisma health source.
|
|
7
|
-
* Expressed as `import type` only at consumer sites so this package does not
|
|
8
|
-
* require `@nathapp/nestjs-prisma` to be installed at load time.
|
|
9
|
-
*/
|
|
10
|
-
export interface PrismaHealthSource {
|
|
11
|
-
isHealthy(): Promise<{ status: 'up' | 'down'; message?: string }>;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
/** Default key used when callers don't pass one to {@link PrismaHealthCheck.isHealthy}. */
|
|
15
|
-
export const DEFAULT_PRISMA_KEY = 'prisma';
|
|
16
|
-
|
|
17
|
-
type TerminusResult = { [k: string]: { status: 'up' | 'down' } };
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* Terminus-compatible health check that adapts a pluggable Prisma source
|
|
21
|
-
* bound to {@link PRISMA_HEALTH_SOURCE}.
|
|
22
|
-
*
|
|
23
|
-
* Behaviour:
|
|
24
|
-
* - No source bound → `down` with `reason: 'source not provided'`. Never throws.
|
|
25
|
-
* - Source returns `{ status: 'up' }` → `up` (message included if present).
|
|
26
|
-
* - Source returns `{ status: 'down', message }` → `down` with `message`.
|
|
27
|
-
* - Source rejects → `down` with the error message. Never throws out.
|
|
28
|
-
*/
|
|
29
|
-
@Injectable()
|
|
30
|
-
export class PrismaHealthCheck {
|
|
31
|
-
constructor(
|
|
32
|
-
private readonly healthIndicatorService: HealthIndicatorService,
|
|
33
|
-
@Optional()
|
|
34
|
-
@Inject(PRISMA_HEALTH_SOURCE)
|
|
35
|
-
private readonly source?: PrismaHealthSource,
|
|
36
|
-
) {}
|
|
37
|
-
|
|
38
|
-
async isHealthy(key: string = DEFAULT_PRISMA_KEY): Promise<TerminusResult> {
|
|
39
|
-
const indicator = this.healthIndicatorService.check(key);
|
|
40
|
-
|
|
41
|
-
if (!this.source) {
|
|
42
|
-
return indicator.down({ reason: 'source not provided' }) as unknown as TerminusResult;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
try {
|
|
46
|
-
const result = await this.source.isHealthy();
|
|
47
|
-
const details: { message?: string } = {};
|
|
48
|
-
if (result.message !== undefined) {
|
|
49
|
-
details.message = result.message;
|
|
50
|
-
}
|
|
51
|
-
return result.status === 'up'
|
|
52
|
-
? (indicator.up(details) as unknown as TerminusResult)
|
|
53
|
-
: (indicator.down(details) as unknown as TerminusResult);
|
|
54
|
-
} catch (err) {
|
|
55
|
-
const message = err instanceof Error ? err.message : String(err);
|
|
56
|
-
return indicator.down({ error: message }) as unknown as TerminusResult;
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
}
|
|
@@ -1,72 +0,0 @@
|
|
|
1
|
-
import { Inject, Injectable, Optional } from '@nestjs/common';
|
|
2
|
-
import { HealthIndicatorService } from '@nestjs/terminus';
|
|
3
|
-
import { QUEUE_HEALTH_SOURCE } from '../constant/health.constants';
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Result shape produced by a queue health source.
|
|
7
|
-
* Expressed as `import type` only at consumer sites so this package does not
|
|
8
|
-
* require `@nathapp/nestjs-queue` to be installed at load time.
|
|
9
|
-
*/
|
|
10
|
-
export interface QueueHealthSource {
|
|
11
|
-
isHealthy(): Promise<{
|
|
12
|
-
healthy: boolean;
|
|
13
|
-
provider: string;
|
|
14
|
-
connections: unknown[];
|
|
15
|
-
latencyMs?: number;
|
|
16
|
-
}>;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
/** Default key used when callers don't pass one to {@link QueueHealthCheck.isHealthy}. */
|
|
20
|
-
export const DEFAULT_QUEUE_KEY = 'queue';
|
|
21
|
-
|
|
22
|
-
type TerminusResult = { [k: string]: { status: 'up' | 'down' } };
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* Terminus-compatible health check that adapts a pluggable queue source
|
|
26
|
-
* bound to {@link QUEUE_HEALTH_SOURCE}.
|
|
27
|
-
*
|
|
28
|
-
* Behaviour:
|
|
29
|
-
* - No source bound → `down` with `reason: 'source not provided'`. Never throws.
|
|
30
|
-
* - Source returns `{ healthy: true, provider, connections, latencyMs? }` → `up`
|
|
31
|
-
* with those details.
|
|
32
|
-
* - Source returns `{ healthy: false, ... }` → `down` with those details.
|
|
33
|
-
* - Source rejects → `down` with the error message. Never throws out.
|
|
34
|
-
*/
|
|
35
|
-
@Injectable()
|
|
36
|
-
export class QueueHealthCheck {
|
|
37
|
-
constructor(
|
|
38
|
-
private readonly healthIndicatorService: HealthIndicatorService,
|
|
39
|
-
@Optional()
|
|
40
|
-
@Inject(QUEUE_HEALTH_SOURCE)
|
|
41
|
-
private readonly source?: QueueHealthSource,
|
|
42
|
-
) {}
|
|
43
|
-
|
|
44
|
-
async isHealthy(key: string = DEFAULT_QUEUE_KEY): Promise<TerminusResult> {
|
|
45
|
-
const indicator = this.healthIndicatorService.check(key);
|
|
46
|
-
|
|
47
|
-
if (!this.source) {
|
|
48
|
-
return indicator.down({ reason: 'source not provided' }) as unknown as TerminusResult;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
try {
|
|
52
|
-
const result = await this.source.isHealthy();
|
|
53
|
-
const details: {
|
|
54
|
-
provider?: string;
|
|
55
|
-
connections?: unknown[];
|
|
56
|
-
latencyMs?: number;
|
|
57
|
-
} = {
|
|
58
|
-
provider: result.provider,
|
|
59
|
-
connections: result.connections,
|
|
60
|
-
};
|
|
61
|
-
if (result.latencyMs !== undefined) {
|
|
62
|
-
details.latencyMs = result.latencyMs;
|
|
63
|
-
}
|
|
64
|
-
return result.healthy
|
|
65
|
-
? (indicator.up(details) as unknown as TerminusResult)
|
|
66
|
-
: (indicator.down(details) as unknown as TerminusResult);
|
|
67
|
-
} catch (err) {
|
|
68
|
-
const message = err instanceof Error ? err.message : String(err);
|
|
69
|
-
return indicator.down({ error: message }) as unknown as TerminusResult;
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
}
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Generic health source contract.
|
|
3
|
-
*
|
|
4
|
-
* A source exposes a single `isHealthy()` call returning an arbitrary result
|
|
5
|
-
* shape. The `GenericHealthCheck` consumes this contract together with a
|
|
6
|
-
* caller-supplied `normalize` predicate that maps the source result to a
|
|
7
|
-
* boolean. Sources are intentionally agnostic of Terminus so cache, Prisma,
|
|
8
|
-
* and queue packages can plug in without leaking Terminus types.
|
|
9
|
-
*/
|
|
10
|
-
export interface HealthIndicatorSource<TResult = unknown> {
|
|
11
|
-
isHealthy(): PromiseLike<TResult> | TResult;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* Normalizer contract: maps a source's `isHealthy()` result to a boolean.
|
|
16
|
-
* Returning `true` → "up"; returning `false` → "down".
|
|
17
|
-
*/
|
|
18
|
-
export type HealthNormalize<TResult = unknown> = (result: TResult) => boolean;
|
|
@@ -1,85 +0,0 @@
|
|
|
1
|
-
import type { Type } from '@nestjs/common';
|
|
2
|
-
import {
|
|
3
|
-
DEFAULT_LIVENESS_PATH,
|
|
4
|
-
DEFAULT_READINESS_PATH,
|
|
5
|
-
} from '../constant/health.constants';
|
|
6
|
-
|
|
7
|
-
/** Controller path overrides for the default liveness/readiness routes. */
|
|
8
|
-
export interface HealthControllerOptions {
|
|
9
|
-
livenessPath?: string;
|
|
10
|
-
readinessPath?: string;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* Resolved controller contract:
|
|
15
|
-
* - `false` means "do not register a controller at all" (services are still provided).
|
|
16
|
-
* - Otherwise the merged liveness/readiness paths are returned.
|
|
17
|
-
*/
|
|
18
|
-
export type ResolvedHealthControllerOptions =
|
|
19
|
-
| false
|
|
20
|
-
| {
|
|
21
|
-
livenessPath: string;
|
|
22
|
-
readinessPath: string;
|
|
23
|
-
};
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* Resolve the controller option block into a concrete value usable by
|
|
27
|
-
* `HealthModule`.
|
|
28
|
-
*
|
|
29
|
-
* - `false` → returned as `false` (no controller).
|
|
30
|
-
* - `undefined` / object → returns merged paths with defaults applied.
|
|
31
|
-
* - Empty or whitespace-only strings fall back to the defaults.
|
|
32
|
-
*/
|
|
33
|
-
export function resolveHealthControllerOptions(
|
|
34
|
-
controller?: false | HealthControllerOptions,
|
|
35
|
-
): ResolvedHealthControllerOptions {
|
|
36
|
-
if (controller === false) {
|
|
37
|
-
return false;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
const livenessPath =
|
|
41
|
-
controller?.livenessPath && controller.livenessPath.trim().length > 0
|
|
42
|
-
? controller.livenessPath
|
|
43
|
-
: DEFAULT_LIVENESS_PATH;
|
|
44
|
-
const readinessPath =
|
|
45
|
-
controller?.readinessPath && controller.readinessPath.trim().length > 0
|
|
46
|
-
? controller.readinessPath
|
|
47
|
-
: DEFAULT_READINESS_PATH;
|
|
48
|
-
|
|
49
|
-
return { livenessPath, readinessPath };
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
/**
|
|
53
|
-
* Module options consumed by `HealthModule.register()`.
|
|
54
|
-
*
|
|
55
|
-
* - `controller` controls whether and where the default HTTP controller is mounted.
|
|
56
|
-
* - `liveness` lists adapters that run for the liveness probe.
|
|
57
|
-
* - `readiness` lists adapters that run for the readiness probe.
|
|
58
|
-
*/
|
|
59
|
-
export interface HealthModuleOptions {
|
|
60
|
-
controller?: false | HealthControllerOptions;
|
|
61
|
-
liveness?: Type<unknown>[];
|
|
62
|
-
readiness?: Type<unknown>[];
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
/** Options token consumed by HealthModule (ConfigurableModuleClass). */
|
|
66
|
-
export const HEALTH_MODULE_OPTIONS = 'HEALTH_MODULE_OPTIONS';
|
|
67
|
-
|
|
68
|
-
/**
|
|
69
|
-
* Async registration options for HealthModule.
|
|
70
|
-
*
|
|
71
|
-
* `controller` is resolved synchronously (mirroring `register()`), not via
|
|
72
|
-
* `useFactory` — the controller class is built from static `@Get()` decorator
|
|
73
|
-
* metadata at `registerAsync()` call time, before any async-resolved value
|
|
74
|
-
* exists, so its shape cannot depend on the factory's output. `liveness` and
|
|
75
|
-
* `readiness` remain fully async since the executor resolves them lazily via
|
|
76
|
-
* injected `HEALTH_MODULE_OPTIONS`.
|
|
77
|
-
*/
|
|
78
|
-
export interface HealthAsyncModuleOptions {
|
|
79
|
-
controller?: false | HealthControllerOptions;
|
|
80
|
-
useFactory?: (
|
|
81
|
-
...args: unknown[]
|
|
82
|
-
) => Promise<HealthModuleOptions> | HealthModuleOptions;
|
|
83
|
-
inject?: unknown[];
|
|
84
|
-
imports?: unknown[];
|
|
85
|
-
}
|
package/lib/interface/index.ts
DELETED
|
@@ -1,62 +0,0 @@
|
|
|
1
|
-
import { DynamicModule, Module } from '@nestjs/common';
|
|
2
|
-
import { ConfigurableModuleClass } from '@nathapp/nestjs-common';
|
|
3
|
-
import { TerminusModule } from '@nestjs/terminus';
|
|
4
|
-
import { CacheHealthCheck } from '../indicator/cache-health.check';
|
|
5
|
-
import { GenericHealthCheck } from '../indicator/generic-health.check';
|
|
6
|
-
import { PrismaHealthCheck } from '../indicator/prisma-health.check';
|
|
7
|
-
import { QueueHealthCheck } from '../indicator/queue-health.check';
|
|
8
|
-
import { HealthExecutorService } from '../service/health-executor.service';
|
|
9
|
-
import { createHealthController } from '../controller/health.controller';
|
|
10
|
-
import {
|
|
11
|
-
HEALTH_MODULE_OPTIONS,
|
|
12
|
-
HealthAsyncModuleOptions,
|
|
13
|
-
HealthModuleOptions,
|
|
14
|
-
resolveHealthControllerOptions,
|
|
15
|
-
} from '../interface';
|
|
16
|
-
|
|
17
|
-
const ADAPTERS = [
|
|
18
|
-
CacheHealthCheck,
|
|
19
|
-
PrismaHealthCheck,
|
|
20
|
-
QueueHealthCheck,
|
|
21
|
-
GenericHealthCheck,
|
|
22
|
-
];
|
|
23
|
-
|
|
24
|
-
function assembleHealthModule(
|
|
25
|
-
baseModule: DynamicModule,
|
|
26
|
-
controllerOptions: HealthModuleOptions['controller'],
|
|
27
|
-
): DynamicModule {
|
|
28
|
-
const controller = resolveHealthControllerOptions(controllerOptions);
|
|
29
|
-
const controllers =
|
|
30
|
-
controller === false ? [] : [createHealthController(controller)];
|
|
31
|
-
|
|
32
|
-
return {
|
|
33
|
-
...baseModule,
|
|
34
|
-
global: true,
|
|
35
|
-
module: HealthModule,
|
|
36
|
-
imports: [TerminusModule, ...(baseModule.imports ?? [])],
|
|
37
|
-
controllers,
|
|
38
|
-
providers: [
|
|
39
|
-
...(baseModule.providers ?? []),
|
|
40
|
-
...ADAPTERS,
|
|
41
|
-
HealthExecutorService,
|
|
42
|
-
],
|
|
43
|
-
exports: [HealthExecutorService],
|
|
44
|
-
};
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
@Module({})
|
|
48
|
-
export class HealthModule extends ConfigurableModuleClass<HealthModuleOptions>(
|
|
49
|
-
HEALTH_MODULE_OPTIONS,
|
|
50
|
-
) {
|
|
51
|
-
static register(options?: HealthModuleOptions): DynamicModule {
|
|
52
|
-
const baseModule = super.register(options ?? {});
|
|
53
|
-
return assembleHealthModule(baseModule, options?.controller);
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
static registerAsync(options: HealthAsyncModuleOptions): DynamicModule {
|
|
57
|
-
const baseModule = (
|
|
58
|
-
super.registerAsync as (o: HealthAsyncModuleOptions) => DynamicModule
|
|
59
|
-
)(options);
|
|
60
|
-
return assembleHealthModule(baseModule, options.controller);
|
|
61
|
-
}
|
|
62
|
-
}
|
package/lib/module/index.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from './health.module';
|
|
@@ -1,69 +0,0 @@
|
|
|
1
|
-
import { Inject, Injectable, Type } from '@nestjs/common';
|
|
2
|
-
import { ModuleRef } from '@nestjs/core';
|
|
3
|
-
import { HealthCheckService } from '@nestjs/terminus';
|
|
4
|
-
import { from, Observable } from 'rxjs';
|
|
5
|
-
import {
|
|
6
|
-
HEALTH_MODULE_OPTIONS,
|
|
7
|
-
HealthModuleOptions,
|
|
8
|
-
} from '../interface/health-module-options.interface';
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* Minimal contract every probe adapter must satisfy.
|
|
12
|
-
*
|
|
13
|
-
* Adapters own their terminus result key: built-in indicators
|
|
14
|
-
* (`CacheHealthCheck`, `PrismaHealthCheck`, `QueueHealthCheck`,
|
|
15
|
-
* `GenericHealthCheck`) accept an optional `key` argument and fall back to a
|
|
16
|
-
* canonical default (`cache`, `prisma`, `queue`) when called with none. The
|
|
17
|
-
* executor therefore invokes each adapter with **no** key argument and lets
|
|
18
|
-
* the adapter decide how to label its result entry — so callers that wire
|
|
19
|
-
* `CacheHealthCheck` into `readiness` continue to see `result.details.cache`,
|
|
20
|
-
* not `result.details.CacheHealthCheck`.
|
|
21
|
-
*
|
|
22
|
-
* Returned objects follow `@nestjs/terminus`'s `HealthIndicatorResult`
|
|
23
|
-
* shape — a flat key → `{ status, ...details }` map. The executor never
|
|
24
|
-
* inspects the body; it just hands it to terminus via `HealthCheckService`.
|
|
25
|
-
*/
|
|
26
|
-
export interface HealthIndicatorLike {
|
|
27
|
-
isHealthy(
|
|
28
|
-
key?: string,
|
|
29
|
-
): Promise<Record<string, { status: 'up' | 'down' }>> | Record<string, { status: 'up' | 'down' }>;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* Resolves configured adapter **class tokens** through the live Nest injector
|
|
34
|
-
* and composes their `isHealthy()` results via terminus'
|
|
35
|
-
* `HealthCheckService.check()`.
|
|
36
|
-
*
|
|
37
|
-
* Adapter arrays live on `HealthModuleOptions.liveness` and `.readiness`.
|
|
38
|
-
* Empty arrays resolve to `{ status: 'ok' }` (terminus returns OK for `[]`).
|
|
39
|
-
* On error status, terminus throws `ServiceUnavailableException` — the
|
|
40
|
-
* executor surfaces it as an Observable error so Nest's default exception
|
|
41
|
-
* filter renders HTTP 503 on the readiness route.
|
|
42
|
-
*/
|
|
43
|
-
@Injectable()
|
|
44
|
-
export class HealthExecutorService {
|
|
45
|
-
constructor(
|
|
46
|
-
private readonly health: HealthCheckService,
|
|
47
|
-
private readonly moduleRef: ModuleRef,
|
|
48
|
-
@Inject(HEALTH_MODULE_OPTIONS)
|
|
49
|
-
private readonly options: HealthModuleOptions,
|
|
50
|
-
) {}
|
|
51
|
-
|
|
52
|
-
runLiveness(): Observable<import('@nestjs/terminus').HealthCheckResult> {
|
|
53
|
-
return this.run(this.options.liveness ?? []);
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
runReadiness(): Observable<import('@nestjs/terminus').HealthCheckResult> {
|
|
57
|
-
return this.run(this.options.readiness ?? []);
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
private run(
|
|
61
|
-
types: Type<unknown>[],
|
|
62
|
-
): Observable<import('@nestjs/terminus').HealthCheckResult> {
|
|
63
|
-
const checks = types.map((T) => {
|
|
64
|
-
const inst = this.moduleRef.get<HealthIndicatorLike>(T, { strict: false });
|
|
65
|
-
return () => inst.isHealthy();
|
|
66
|
-
});
|
|
67
|
-
return from(this.health.check(checks));
|
|
68
|
-
}
|
|
69
|
-
}
|
package/lib/service/index.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from './health-executor.service';
|
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from '@jest/globals';
|
|
2
|
-
import { Test } from '@nestjs/testing';
|
|
3
|
-
import { TerminusModule } from '@nestjs/terminus';
|
|
4
|
-
import {
|
|
5
|
-
CACHE_HEALTH_SOURCE,
|
|
6
|
-
PRISMA_HEALTH_SOURCE,
|
|
7
|
-
QUEUE_HEALTH_SOURCE,
|
|
8
|
-
} from '../../lib';
|
|
9
|
-
|
|
10
|
-
describe('health constants', () => {
|
|
11
|
-
it('CACHE_HEALTH_SOURCE is a truthy Symbol.for token', () => {
|
|
12
|
-
expect(CACHE_HEALTH_SOURCE).toBeTruthy();
|
|
13
|
-
expect(typeof CACHE_HEALTH_SOURCE).toBe('symbol');
|
|
14
|
-
expect(CACHE_HEALTH_SOURCE).toBe(Symbol.for('CACHE_HEALTH_SOURCE'));
|
|
15
|
-
});
|
|
16
|
-
|
|
17
|
-
it('PRISMA_HEALTH_SOURCE is a truthy Symbol.for token', () => {
|
|
18
|
-
expect(PRISMA_HEALTH_SOURCE).toBeTruthy();
|
|
19
|
-
expect(typeof PRISMA_HEALTH_SOURCE).toBe('symbol');
|
|
20
|
-
expect(PRISMA_HEALTH_SOURCE).toBe(Symbol.for('PRISMA_HEALTH_SOURCE'));
|
|
21
|
-
});
|
|
22
|
-
|
|
23
|
-
it('QUEUE_HEALTH_SOURCE is a truthy Symbol.for token', () => {
|
|
24
|
-
expect(QUEUE_HEALTH_SOURCE).toBeTruthy();
|
|
25
|
-
expect(typeof QUEUE_HEALTH_SOURCE).toBe('symbol');
|
|
26
|
-
expect(QUEUE_HEALTH_SOURCE).toBe(Symbol.for('QUEUE_HEALTH_SOURCE'));
|
|
27
|
-
});
|
|
28
|
-
|
|
29
|
-
it('the three source tokens are pairwise distinct', () => {
|
|
30
|
-
expect(CACHE_HEALTH_SOURCE).not.toBe(PRISMA_HEALTH_SOURCE);
|
|
31
|
-
expect(PRISMA_HEALTH_SOURCE).not.toBe(QUEUE_HEALTH_SOURCE);
|
|
32
|
-
expect(CACHE_HEALTH_SOURCE).not.toBe(QUEUE_HEALTH_SOURCE);
|
|
33
|
-
});
|
|
34
|
-
});
|