@velajs/vela 0.4.0 → 0.4.1
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/health/health.http.d.ts +14 -0
- package/dist/health/health.http.js +75 -0
- package/dist/health/health.indicator.d.ts +9 -0
- package/dist/health/health.indicator.js +32 -0
- package/dist/health/health.module.d.ts +2 -0
- package/dist/health/health.module.js +26 -0
- package/dist/health/health.service.d.ts +7 -0
- package/dist/health/health.service.js +65 -0
- package/dist/health/health.types.d.ts +15 -0
- package/dist/health/health.types.js +1 -0
- package/dist/health/index.d.ts +6 -0
- package/dist/health/index.js +4 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/package.json +1 -1
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { HealthIndicatorService } from './health.indicator';
|
|
2
|
+
import type { HealthIndicatorResult, ResponseCheckCallback } from './health.types';
|
|
3
|
+
export interface HttpPingOptions {
|
|
4
|
+
method?: string;
|
|
5
|
+
headers?: Record<string, string>;
|
|
6
|
+
timeout?: number;
|
|
7
|
+
expectedStatus?: number;
|
|
8
|
+
}
|
|
9
|
+
export declare class HttpHealthIndicator {
|
|
10
|
+
private indicator;
|
|
11
|
+
constructor(indicator: HealthIndicatorService);
|
|
12
|
+
pingCheck(key: string, url: string, options?: HttpPingOptions): Promise<HealthIndicatorResult>;
|
|
13
|
+
responseCheck(key: string, url: string, callback: ResponseCheckCallback, options?: HttpPingOptions): Promise<HealthIndicatorResult>;
|
|
14
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
function _ts_decorate(decorators, target, key, desc) {
|
|
2
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
3
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
4
|
+
else for(var i = decorators.length - 1; i >= 0; i--)if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
5
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
|
+
}
|
|
7
|
+
function _ts_metadata(k, v) {
|
|
8
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
9
|
+
}
|
|
10
|
+
import { Injectable } from "../container/index.js";
|
|
11
|
+
import { HealthIndicatorService } from "./health.indicator.js";
|
|
12
|
+
export class HttpHealthIndicator {
|
|
13
|
+
indicator;
|
|
14
|
+
constructor(indicator){
|
|
15
|
+
this.indicator = indicator;
|
|
16
|
+
}
|
|
17
|
+
async pingCheck(key, url, options) {
|
|
18
|
+
const { method = 'GET', headers, timeout = 5000, expectedStatus } = options ?? {};
|
|
19
|
+
try {
|
|
20
|
+
const response = await fetch(url, {
|
|
21
|
+
method,
|
|
22
|
+
headers,
|
|
23
|
+
signal: AbortSignal.timeout(timeout)
|
|
24
|
+
});
|
|
25
|
+
const statusCode = response.status;
|
|
26
|
+
const isHealthy = expectedStatus !== undefined ? statusCode === expectedStatus : statusCode >= 200 && statusCode < 300;
|
|
27
|
+
if (isHealthy) {
|
|
28
|
+
return this.indicator.check(key).up({
|
|
29
|
+
statusCode
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
return this.indicator.check(key).down({
|
|
33
|
+
statusCode,
|
|
34
|
+
message: response.statusText
|
|
35
|
+
});
|
|
36
|
+
} catch (error) {
|
|
37
|
+
const message = error instanceof Error ? error.message : 'Unknown error';
|
|
38
|
+
return this.indicator.check(key).down({
|
|
39
|
+
message
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
async responseCheck(key, url, callback, options) {
|
|
44
|
+
const { method = 'GET', headers, timeout = 5000 } = options ?? {};
|
|
45
|
+
try {
|
|
46
|
+
const response = await fetch(url, {
|
|
47
|
+
method,
|
|
48
|
+
headers,
|
|
49
|
+
signal: AbortSignal.timeout(timeout)
|
|
50
|
+
});
|
|
51
|
+
const statusCode = response.status;
|
|
52
|
+
const isHealthy = await callback(response);
|
|
53
|
+
if (isHealthy) {
|
|
54
|
+
return this.indicator.check(key).up({
|
|
55
|
+
statusCode
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
return this.indicator.check(key).down({
|
|
59
|
+
statusCode
|
|
60
|
+
});
|
|
61
|
+
} catch (error) {
|
|
62
|
+
const message = error instanceof Error ? error.message : 'Unknown error';
|
|
63
|
+
return this.indicator.check(key).down({
|
|
64
|
+
message
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
HttpHealthIndicator = _ts_decorate([
|
|
70
|
+
Injectable(),
|
|
71
|
+
_ts_metadata("design:type", Function),
|
|
72
|
+
_ts_metadata("design:paramtypes", [
|
|
73
|
+
typeof HealthIndicatorService === "undefined" ? Object : HealthIndicatorService
|
|
74
|
+
])
|
|
75
|
+
], HttpHealthIndicator);
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { HealthIndicatorResult } from './health.types';
|
|
2
|
+
interface HealthIndicatorBuilder {
|
|
3
|
+
up(data?: Record<string, unknown>): HealthIndicatorResult;
|
|
4
|
+
down(data?: Record<string, unknown>): HealthIndicatorResult;
|
|
5
|
+
}
|
|
6
|
+
export declare class HealthIndicatorService {
|
|
7
|
+
check(key: string): HealthIndicatorBuilder;
|
|
8
|
+
}
|
|
9
|
+
export {};
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
function _ts_decorate(decorators, target, key, desc) {
|
|
2
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
3
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
4
|
+
else for(var i = decorators.length - 1; i >= 0; i--)if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
5
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
|
+
}
|
|
7
|
+
import { Injectable } from "../container/index.js";
|
|
8
|
+
export class HealthIndicatorService {
|
|
9
|
+
check(key) {
|
|
10
|
+
return {
|
|
11
|
+
up (data) {
|
|
12
|
+
return {
|
|
13
|
+
[key]: {
|
|
14
|
+
status: 'up',
|
|
15
|
+
...data
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
},
|
|
19
|
+
down (data) {
|
|
20
|
+
return {
|
|
21
|
+
[key]: {
|
|
22
|
+
status: 'down',
|
|
23
|
+
...data
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
HealthIndicatorService = _ts_decorate([
|
|
31
|
+
Injectable()
|
|
32
|
+
], HealthIndicatorService);
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
function _ts_decorate(decorators, target, key, desc) {
|
|
2
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
3
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
4
|
+
else for(var i = decorators.length - 1; i >= 0; i--)if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
5
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
|
+
}
|
|
7
|
+
import { Module } from "../module/index.js";
|
|
8
|
+
import { HealthCheckService } from "./health.service.js";
|
|
9
|
+
import { HealthIndicatorService } from "./health.indicator.js";
|
|
10
|
+
import { HttpHealthIndicator } from "./health.http.js";
|
|
11
|
+
export class HealthModule {
|
|
12
|
+
}
|
|
13
|
+
HealthModule = _ts_decorate([
|
|
14
|
+
Module({
|
|
15
|
+
providers: [
|
|
16
|
+
HealthCheckService,
|
|
17
|
+
HealthIndicatorService,
|
|
18
|
+
HttpHealthIndicator
|
|
19
|
+
],
|
|
20
|
+
exports: [
|
|
21
|
+
HealthCheckService,
|
|
22
|
+
HealthIndicatorService,
|
|
23
|
+
HttpHealthIndicator
|
|
24
|
+
]
|
|
25
|
+
})
|
|
26
|
+
], HealthModule);
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { BeforeApplicationShutdown } from '../lifecycle/index';
|
|
2
|
+
import type { HealthCheckResult, HealthIndicatorFunction } from './health.types';
|
|
3
|
+
export declare class HealthCheckService implements BeforeApplicationShutdown {
|
|
4
|
+
private isShuttingDown;
|
|
5
|
+
beforeApplicationShutdown(): void;
|
|
6
|
+
check(indicators: HealthIndicatorFunction[]): Promise<HealthCheckResult>;
|
|
7
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
function _ts_decorate(decorators, target, key, desc) {
|
|
2
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
3
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
4
|
+
else for(var i = decorators.length - 1; i >= 0; i--)if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
5
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
|
+
}
|
|
7
|
+
import { Injectable } from "../container/index.js";
|
|
8
|
+
import { ServiceUnavailableException } from "../errors/http-exception.js";
|
|
9
|
+
export class HealthCheckService {
|
|
10
|
+
isShuttingDown = false;
|
|
11
|
+
beforeApplicationShutdown() {
|
|
12
|
+
this.isShuttingDown = true;
|
|
13
|
+
}
|
|
14
|
+
async check(indicators) {
|
|
15
|
+
if (this.isShuttingDown) {
|
|
16
|
+
const result = {
|
|
17
|
+
status: 'shutting_down',
|
|
18
|
+
info: {},
|
|
19
|
+
error: {},
|
|
20
|
+
details: {}
|
|
21
|
+
};
|
|
22
|
+
throw new ServiceUnavailableException('Service Unavailable', result);
|
|
23
|
+
}
|
|
24
|
+
const results = await Promise.allSettled(indicators.map((fn)=>fn()));
|
|
25
|
+
const info = {};
|
|
26
|
+
const error = {};
|
|
27
|
+
const details = {};
|
|
28
|
+
for (const result of results){
|
|
29
|
+
if (result.status === 'fulfilled') {
|
|
30
|
+
const indicatorResult = result.value;
|
|
31
|
+
for (const [key, value] of Object.entries(indicatorResult)){
|
|
32
|
+
details[key] = value;
|
|
33
|
+
if (value.status === 'up') {
|
|
34
|
+
info[key] = value;
|
|
35
|
+
} else {
|
|
36
|
+
error[key] = value;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
} else {
|
|
40
|
+
const message = result.reason instanceof Error ? result.reason.message : 'Health check failed';
|
|
41
|
+
const key = 'unknown';
|
|
42
|
+
const value = {
|
|
43
|
+
status: 'down',
|
|
44
|
+
message
|
|
45
|
+
};
|
|
46
|
+
details[key] = value;
|
|
47
|
+
error[key] = value;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
const status = Object.keys(error).length > 0 ? 'error' : 'ok';
|
|
51
|
+
const checkResult = {
|
|
52
|
+
status,
|
|
53
|
+
info,
|
|
54
|
+
error,
|
|
55
|
+
details
|
|
56
|
+
};
|
|
57
|
+
if (status === 'error') {
|
|
58
|
+
throw new ServiceUnavailableException('Service Unavailable', checkResult);
|
|
59
|
+
}
|
|
60
|
+
return checkResult;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
HealthCheckService = _ts_decorate([
|
|
64
|
+
Injectable()
|
|
65
|
+
], HealthCheckService);
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export type HealthCheckStatus = 'ok' | 'error' | 'shutting_down';
|
|
2
|
+
export interface HealthIndicatorResult {
|
|
3
|
+
[key: string]: {
|
|
4
|
+
status: 'up' | 'down';
|
|
5
|
+
[key: string]: unknown;
|
|
6
|
+
};
|
|
7
|
+
}
|
|
8
|
+
export interface HealthCheckResult {
|
|
9
|
+
status: HealthCheckStatus;
|
|
10
|
+
info: HealthIndicatorResult;
|
|
11
|
+
error: HealthIndicatorResult;
|
|
12
|
+
details: HealthIndicatorResult;
|
|
13
|
+
}
|
|
14
|
+
export type HealthIndicatorFunction = () => Promise<HealthIndicatorResult>;
|
|
15
|
+
export type ResponseCheckCallback = (response: Response) => boolean | Promise<boolean>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { };
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export { HealthModule } from './health.module';
|
|
2
|
+
export { HealthCheckService } from './health.service';
|
|
3
|
+
export { HealthIndicatorService } from './health.indicator';
|
|
4
|
+
export { HttpHealthIndicator } from './health.http';
|
|
5
|
+
export type { HealthCheckResult, HealthCheckStatus, HealthIndicatorResult, HealthIndicatorFunction, ResponseCheckCallback, } from './health.types';
|
|
6
|
+
export type { HttpPingOptions } from './health.http';
|
package/dist/index.d.ts
CHANGED
|
@@ -18,6 +18,8 @@ export { EventEmitterModule, EventEmitter, EventEmitterSubscriber, OnEvent, ON_E
|
|
|
18
18
|
export type { EventHandler, OnEventMetadata } from './event-emitter/index';
|
|
19
19
|
export { ScheduleModule, ScheduleRegistry, ScheduleExecutor, Cron, Interval, SCHEDULE_MODULE_OPTIONS, CRON_METADATA, INTERVAL_METADATA, } from './schedule/index';
|
|
20
20
|
export type { RegisteredCronJob, RegisteredIntervalJob, CronMetadata, IntervalMetadata, ScheduleModuleOptions, } from './schedule/index';
|
|
21
|
+
export { HealthModule, HealthCheckService, HealthIndicatorService, HttpHealthIndicator, } from './health/index';
|
|
22
|
+
export type { HealthCheckResult, HealthCheckStatus, HealthIndicatorResult, HealthIndicatorFunction, ResponseCheckCallback, HttpPingOptions, } from './health/index';
|
|
21
23
|
export { ThrottlerModule, ThrottlerGuard, ThrottlerStorage, Throttle, SkipThrottle, THROTTLER_OPTIONS, THROTTLER_STORAGE, THROTTLE_METADATA, SKIP_THROTTLE_METADATA, } from './throttler/index';
|
|
22
24
|
export type { ThrottlerModuleOptions, ThrottleConfig, ThrottlerStore, ThrottlerStorageRecord, RateLimitInfo, } from './throttler/index';
|
|
23
25
|
export { Module } from './module/index';
|
package/dist/index.js
CHANGED
|
@@ -21,6 +21,8 @@ export { CacheModule, CacheService, CacheInterceptor, MemoryCacheStore, CacheKey
|
|
|
21
21
|
export { EventEmitterModule, EventEmitter, EventEmitterSubscriber, OnEvent, ON_EVENT_METADATA } from "./event-emitter/index.js";
|
|
22
22
|
// Schedule
|
|
23
23
|
export { ScheduleModule, ScheduleRegistry, ScheduleExecutor, Cron, Interval, SCHEDULE_MODULE_OPTIONS, CRON_METADATA, INTERVAL_METADATA } from "./schedule/index.js";
|
|
24
|
+
// Health
|
|
25
|
+
export { HealthModule, HealthCheckService, HealthIndicatorService, HttpHealthIndicator } from "./health/index.js";
|
|
24
26
|
// Throttler
|
|
25
27
|
export { ThrottlerModule, ThrottlerGuard, ThrottlerStorage, Throttle, SkipThrottle, THROTTLER_OPTIONS, THROTTLER_STORAGE, THROTTLE_METADATA, SKIP_THROTTLE_METADATA } from "./throttler/index.js";
|
|
26
28
|
// Module
|