raiton 6.5.0-beta.13 → 6.7.0-beta.15
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/CHANGELOG.md +31 -0
- package/README.md +16 -9
- package/build/bin/index.mjs +10288 -6059
- package/deno.json +4 -1
- package/docs/contributors/framework/README.md +2 -0
- package/docs/contributors/framework/cache/README.md +55 -0
- package/docs/contributors/framework/cache/cache.md +86 -0
- package/docs/contributors/framework/cache/etag.md +82 -0
- package/docs/contributors/framework/decorators/README.md +1 -0
- package/docs/contributors/framework/plugins/csrf.md +277 -0
- package/docs/contributors/framework/plugins/security.md +40 -6
- package/docs/contributors/framework/runtime.md +13 -3
- package/docs/user-guide/README.md +8 -4
- package/docs/user-guide/best-practices.md +1 -0
- package/docs/user-guide/cache.md +79 -0
- package/docs/user-guide/cli.md +35 -3
- package/docs/user-guide/csrf.md +557 -0
- package/docs/user-guide/decorators/README.md +3 -0
- package/docs/user-guide/decorators/cache.md +79 -0
- package/docs/user-guide/decorators/health-check.md +125 -0
- package/docs/user-guide/health-check.md +117 -0
- package/docs/user-guide/runtime.md +2 -1
- package/docs/user-guide/security.md +8 -2
- package/package.json +8 -3
- package/source/bin/bootstrapper.ts +4 -4
- package/source/bin/cli-tools.ts +27 -3
- package/source/bin/cli.ts +2 -2
- package/source/bin/commands.registry.ts +17 -0
- package/source/bin/constants.ts +1 -0
- package/source/bin/index.ts +2 -2
- package/source/commands/artifact.command.ts +2 -2
- package/source/commands/build.command.ts +2 -2
- package/source/commands/develop.command.ts +4 -5
- package/source/commands/grafts.command.ts +1 -1
- package/source/commands/start.command.ts +2 -2
- package/source/core/application.ts +10 -10
- package/source/core/artifacts/artifact.ts +1 -1
- package/source/core/artifacts/index.ts +1 -1
- package/source/core/builder.ts +69 -15
- package/source/core/bytes.util.ts +1 -1
- package/source/core/commands.ts +24 -14
- package/source/core/config/config.ts +2 -2
- package/source/core/config/define.ts +2 -2
- package/source/core/config/index.ts +2 -2
- package/source/core/context.ts +1 -1
- package/source/core/controller/builder.ts +31 -8
- package/source/core/controller/compiler.ts +6 -6
- package/source/core/controller/index.ts +4 -4
- package/source/core/controller/metadata.ts +2 -2
- package/source/core/controller/tracker.ts +1 -1
- package/source/core/directories.ts +3 -2
- package/source/core/guards.ts +1 -1
- package/source/core/helpers/index.ts +1 -1
- package/source/core/hooks.ts +1 -1
- package/source/core/index.ts +16 -16
- package/source/core/injection/index.ts +1 -1
- package/source/core/injection/injection.ts +3 -3
- package/source/core/mcp/builder.ts +8 -8
- package/source/core/mcp/index.ts +3 -3
- package/source/core/mcp/metadata.ts +2 -2
- package/source/core/mcp/tracker.ts +1 -1
- package/source/core/middleware/compose.ts +2 -2
- package/source/core/middleware/index.ts +2 -2
- package/source/core/middleware/pipeline.ts +2 -2
- package/source/core/plugins/index.ts +2 -2
- package/source/core/plugins/plugin.ts +1 -1
- package/source/core/plugins/scope.ts +4 -4
- package/source/core/raiton.ts +1 -1
- package/source/core/router/handler.ts +30 -6
- package/source/core/router/handler.utils.ts +3 -3
- package/source/core/router/index.ts +4 -4
- package/source/core/router/matcher.ts +1 -1
- package/source/core/router/route.ts +1 -1
- package/source/core/router/router.ts +4 -4
- package/source/core/server.ts +1 -1
- package/source/core/socket/builder.ts +2 -2
- package/source/core/socket/index.ts +2 -2
- package/source/core/socket/metadata.ts +2 -2
- package/source/core/thread.ts +53 -20
- package/source/framework/artifacts.ts +8 -5
- package/source/framework/cache/cache.decorator.ts +35 -0
- package/source/framework/cache/cache.ts +197 -0
- package/source/framework/cache/etag.ts +174 -0
- package/source/framework/cache/index.ts +4 -0
- package/source/framework/cache/types.ts +111 -0
- package/source/framework/constants/decorators.constant.ts +4 -0
- package/source/framework/constants/index.ts +2 -2
- package/source/framework/decorators/access-guard.decorator.ts +2 -2
- package/source/framework/decorators/api-response.decorator.ts +1 -1
- package/source/framework/decorators/api.decorator.ts +1 -1
- package/source/framework/decorators/controllable.decorator.ts +3 -3
- package/source/framework/decorators/csrf-guard.decorator.ts +60 -0
- package/source/framework/decorators/csrf-token.decorator.ts +20 -0
- package/source/framework/decorators/guard.decorator.ts +4 -4
- package/source/framework/decorators/health-check.decorator.ts +50 -0
- package/source/framework/decorators/index.ts +14 -11
- package/source/framework/decorators/injection.decorator.ts +3 -3
- package/source/framework/decorators/mcp.decorator.ts +4 -4
- package/source/framework/decorators/middleware.decorator.ts +2 -2
- package/source/framework/decorators/parametrable.decorator.ts +2 -2
- package/source/framework/decorators/routable.decorator.ts +3 -3
- package/source/framework/decorators/route-interaction.decorator.ts +4 -4
- package/source/framework/decorators/socket.decorator.ts +3 -3
- package/source/framework/encryption.ts +2 -2
- package/source/framework/enums/csrf.enum.ts +4 -0
- package/source/framework/enums/index.ts +8 -7
- package/source/framework/enums/runtime.enum.ts +1 -2
- package/source/framework/env.ts +2 -2
- package/source/framework/exceptions/http-exception.ts +3 -3
- package/source/framework/exceptions/index.ts +2 -2
- package/source/framework/health-check.ts +137 -0
- package/source/framework/index.ts +18 -16
- package/source/framework/mcp/handler.ts +3 -3
- package/source/framework/mcp/index.ts +3 -3
- package/source/framework/mcp/protocol.ts +1 -1
- package/source/framework/mcp/streamable-http.ts +3 -3
- package/source/framework/parameter-bag.ts +2 -2
- package/source/framework/plugins/body-parser.plugin.ts +3 -3
- package/source/framework/plugins/index.ts +4 -4
- package/source/framework/plugins/mcp.plugin.ts +5 -5
- package/source/framework/plugins/openapi.plugin.ts +4 -4
- package/source/framework/plugins/security/body-limit.ts +3 -3
- package/source/framework/plugins/security/cors.ts +2 -2
- package/source/framework/plugins/security/csrf.ts +145 -0
- package/source/framework/plugins/security/headers.ts +2 -2
- package/source/framework/plugins/security/index.ts +7 -5
- package/source/framework/plugins/security/method-guard.ts +3 -3
- package/source/framework/plugins/security/rate-limit.ts +3 -3
- package/source/framework/repositories.ts +1 -1
- package/source/framework/responses/error.ts +2 -2
- package/source/framework/responses/helpers.ts +3 -3
- package/source/framework/responses/http-throwable.ts +3 -3
- package/source/framework/responses/http.ts +3 -3
- package/source/framework/responses/index.ts +4 -4
- package/source/framework/runtime/bun/server.ts +5 -5
- package/source/framework/runtime/deno/server.ts +156 -96
- package/source/framework/runtime/index.ts +11 -17
- package/source/framework/runtime/node/reply.ts +1 -1
- package/source/framework/runtime/node/request.ts +2 -2
- package/source/framework/runtime/node/server.ts +3 -3
- package/source/framework/runtime/web/server.ts +2 -2
- package/source/framework/services.ts +1 -1
- package/source/framework/utilities/artifact.util.ts +4 -0
- package/source/framework/utilities/csrf.util.ts +162 -0
- package/source/framework/utilities/index.ts +10 -9
- package/source/framework/utilities/openapi.utils.ts +7 -7
- package/source/framework/utilities/utilities.util.ts +1 -1
- package/source/types/application.ts +2 -2
- package/source/types/builder.ts +1 -1
- package/source/types/config.ts +1 -1
- package/source/types/controller.ts +3 -3
- package/source/types/core.ts +1 -1
- package/source/types/csrf.ts +38 -0
- package/source/types/guard.ts +1 -1
- package/source/types/http-responses.ts +1 -1
- package/source/types/index.ts +26 -25
- package/source/types/injection.ts +1 -1
- package/source/types/middleware.ts +1 -1
- package/source/types/plugin.ts +1 -1
- package/source/types/raiton.ts +1 -1
- package/source/types/responses.ts +2 -2
- package/source/types/router.ts +2 -2
- package/source/types/runtime.ts +3 -5
- package/source/types/thread.ts +4 -4
- /package/source/framework/utilities/{https.ts → https.util.ts} +0 -0
- /package/source/framework/utilities/{url.ts → url.util.ts} +0 -0
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import type {RequestContext} from "../../core/context.ts";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Options de configuration du système de cache de routes et des ETag.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
export type CacheKeyCallable = (context: RequestContext, route?: string) => string | Promise<string>;
|
|
8
|
+
|
|
9
|
+
export interface ETagOptions {
|
|
10
|
+
/**
|
|
11
|
+
* Active ou désactive la génération d'ETag. (défaut: `true`)
|
|
12
|
+
*/
|
|
13
|
+
enabled?: boolean;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Marqueur d'ETag faible (`W/"..."`). Les ETag faibles permettent les réponses
|
|
17
|
+
* équivalentes mais pas identiques octet pour octet. (défaut: `false`)
|
|
18
|
+
*/
|
|
19
|
+
weak?: boolean;
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Fonction de hachage personnalisée. Reçoit le corps sérialisé et renvoie
|
|
23
|
+
* une chaîne qui servira de valeur d'ETag. (défaut: hachage FNV-1a interne)
|
|
24
|
+
*/
|
|
25
|
+
algorithm?: (body: string) => string | Promise<string>;
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Ajoute automatiquement un en-tête `Cache-Control` adapté aux ETag.
|
|
29
|
+
* Peut être un booléen ou un objet de contrôle fin. (défaut: `false`)
|
|
30
|
+
*/
|
|
31
|
+
cacheControl?: boolean | {
|
|
32
|
+
maxAge?: number;
|
|
33
|
+
public?: boolean;
|
|
34
|
+
noCache?: boolean;
|
|
35
|
+
noStore?: boolean;
|
|
36
|
+
mustRevalidate?: boolean;
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Nom de l'en-tête de réponse qui portera la valeur d'ETag. (défaut: `ETag`)
|
|
41
|
+
*/
|
|
42
|
+
headerName?: string;
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Prédicat permettant d'exclure certaines réponses ou requêtes de la
|
|
46
|
+
* génération d'ETag. Renvoie `true` pour sauter la génération.
|
|
47
|
+
*/
|
|
48
|
+
skip?: (context: RequestContext, body: any) => boolean;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export interface ETagResult {
|
|
52
|
+
tag: string;
|
|
53
|
+
body: any;
|
|
54
|
+
notModified: boolean;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export interface CacheOptions {
|
|
58
|
+
/**
|
|
59
|
+
* Durée de validité en millisecondes. (défaut: `60_000`)
|
|
60
|
+
*/
|
|
61
|
+
ttl?: number;
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Active ou désactive la mise en cache de la route. (défaut: `true`)
|
|
65
|
+
*/
|
|
66
|
+
enabled?: boolean;
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Nombre maximum d'entrées du cache en mémoire. (défaut: `1000`)
|
|
70
|
+
*/
|
|
71
|
+
maxEntries?: number;
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Fonction de calcul de la clé de cache. Reçoit le contexte de requête.
|
|
75
|
+
* (défaut: méthode + URL sans query)
|
|
76
|
+
*/
|
|
77
|
+
key?: CacheKeyCallable;
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* En-têtes de requête dont la valeur doit participer à la construction de
|
|
81
|
+
* la clé de cache (segmentation du cache par acceptation de contenu…).
|
|
82
|
+
*/
|
|
83
|
+
vary?: string[];
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Ne met en cache que pour certaines méthodes HTTP. (défaut: `['GET', 'HEAD']`)
|
|
87
|
+
*/
|
|
88
|
+
methods?: string[];
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export interface CacheEntry<T = any> {
|
|
92
|
+
body: T;
|
|
93
|
+
status: number;
|
|
94
|
+
headers: Record<string, string>;
|
|
95
|
+
expiresAt: number;
|
|
96
|
+
createdAt: number;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export interface CacheStore {
|
|
100
|
+
get<T = any>(key: string): CacheEntry<T> | undefined;
|
|
101
|
+
|
|
102
|
+
set<T = any>(key: string, entry: CacheEntry<T>, ttl?: number): void;
|
|
103
|
+
|
|
104
|
+
has(key: string): boolean;
|
|
105
|
+
|
|
106
|
+
delete(key: string): boolean;
|
|
107
|
+
|
|
108
|
+
clear(): void;
|
|
109
|
+
|
|
110
|
+
size(): number;
|
|
111
|
+
}
|
|
@@ -18,6 +18,10 @@ export const METADATA_KEYS = {
|
|
|
18
18
|
API_PROPERTY: Symbol('api:property'),
|
|
19
19
|
// Route interaction metadata key
|
|
20
20
|
ROUTE_INTERACTION: Symbol('route:interaction'),
|
|
21
|
+
// Route cache metadata key
|
|
22
|
+
CACHE: Symbol('route:cache'),
|
|
23
|
+
// Health check metadata key
|
|
24
|
+
HEALTH_CHECK: Symbol('health:check'),
|
|
21
25
|
// MCP metadata keys
|
|
22
26
|
MCP: Symbol('mcp:meta'),
|
|
23
27
|
MCP_ELEMENTS: Symbol('mcp:elements'),
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export * from "./decorators.constant"
|
|
2
|
-
export * from "./microservices.constant"
|
|
1
|
+
export * from "./decorators.constant.ts"
|
|
2
|
+
export * from "./microservices.constant.ts"
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
// import {IAccessGuardDeclaration} from "../../types";
|
|
2
|
-
// import {SYSTEM_DECORATORS_KEYS} from "../constants";
|
|
1
|
+
// import {IAccessGuardDeclaration} from "../../types/index.ts";
|
|
2
|
+
// import {SYSTEM_DECORATORS_KEYS} from "../constants/index.ts";
|
|
3
3
|
//
|
|
4
4
|
// export function AccessGuard(declaration: IAccessGuardDeclaration){
|
|
5
5
|
// return (target: any, methodKey: any)=> {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import {getControllerMetadata} from "../../core/controller";
|
|
2
|
-
import {Injectable} from "
|
|
1
|
+
import {getControllerMetadata} from "../../core/controller/index.ts";
|
|
2
|
+
import {Injectable} from "../index.ts";
|
|
3
3
|
import {LifetimeEnum} from "@protorians/core";
|
|
4
|
-
import {ControllerDecoratorCallable} from "../../types";
|
|
4
|
+
import {ControllerDecoratorCallable} from "../../types/index.ts";
|
|
5
5
|
|
|
6
6
|
export function Controllable(prefix = '') {
|
|
7
7
|
return (target: any) => {
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import {MiddlewareParametersInterface, CsrfGuardOptions} from "../../types/index.ts";
|
|
2
|
+
import {Middleware} from "./middleware.decorator.ts";
|
|
3
|
+
import {HttpStatus, RaitonResponses} from "../index.ts";
|
|
4
|
+
import {CsrfUtil} from "../utilities/csrf.util.ts";
|
|
5
|
+
|
|
6
|
+
const DEFAULT_METHODS = ['POST', 'PUT', 'PATCH', 'DELETE']
|
|
7
|
+
const DEFAULT_HEADER_NAME = 'X-CSRF-Token'
|
|
8
|
+
const DEFAULT_COOKIE_NAME = '_csrf'
|
|
9
|
+
|
|
10
|
+
export function CsrfGuard(options: CsrfGuardOptions = {}) {
|
|
11
|
+
return Middleware(async ({context, next}: MiddlewareParametersInterface) => {
|
|
12
|
+
const method = (context.req.method ?? '').toUpperCase()
|
|
13
|
+
const methods = (options.methods ?? DEFAULT_METHODS).map(m => m.toUpperCase())
|
|
14
|
+
|
|
15
|
+
if (!methods.includes(method)) {
|
|
16
|
+
return next()
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
if (options.skipPaths) {
|
|
20
|
+
const requestPath = new URL(context.req.url, 'http://localhost').pathname
|
|
21
|
+
if (options.skipPaths.some(p => requestPath.startsWith(p))) {
|
|
22
|
+
return next()
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const headerToken = context.req.headers.get(DEFAULT_HEADER_NAME.toLowerCase())
|
|
27
|
+
const cookieHeader = context.req.headers.get('cookie') ?? ''
|
|
28
|
+
const cookieToken = CsrfUtil.extractCookieValue(cookieHeader, DEFAULT_COOKIE_NAME)
|
|
29
|
+
|
|
30
|
+
if (!headerToken || !cookieToken) {
|
|
31
|
+
context.reply.status(HttpStatus.FORBIDDEN)
|
|
32
|
+
return context.reply.send(
|
|
33
|
+
RaitonResponses(
|
|
34
|
+
'CSRF token missing',
|
|
35
|
+
null,
|
|
36
|
+
HttpStatus.FORBIDDEN,
|
|
37
|
+
{error: true}
|
|
38
|
+
)
|
|
39
|
+
)
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const valid = CsrfUtil.timingSafeEqual(cookieToken, headerToken)
|
|
43
|
+
if (!valid) {
|
|
44
|
+
context.reply.status(HttpStatus.FORBIDDEN)
|
|
45
|
+
return context.reply.send(
|
|
46
|
+
RaitonResponses(
|
|
47
|
+
'CSRF token mismatch',
|
|
48
|
+
null,
|
|
49
|
+
HttpStatus.FORBIDDEN,
|
|
50
|
+
{error: true}
|
|
51
|
+
)
|
|
52
|
+
)
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
context.state = context.state ?? {}
|
|
56
|
+
context.state.csrfValidated = true
|
|
57
|
+
|
|
58
|
+
await next()
|
|
59
|
+
})
|
|
60
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import {getControllerMetadata} from "../../core/index.ts";
|
|
2
|
+
|
|
3
|
+
export function CsrfToken() {
|
|
4
|
+
return (target: any, propertyKey?: string) => {
|
|
5
|
+
const meta = getControllerMetadata(target.prototype || target)
|
|
6
|
+
const segment = propertyKey ? propertyKey : '@'
|
|
7
|
+
|
|
8
|
+
if (!meta.middlewares[segment]) {
|
|
9
|
+
meta.middlewares[segment] = []
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
meta.middlewares[segment].push(async ({context, next}: any) => {
|
|
13
|
+
const token = context.state?.csrfToken
|
|
14
|
+
if (token) {
|
|
15
|
+
context.reply.header('X-CSRF-Token', token)
|
|
16
|
+
}
|
|
17
|
+
await next()
|
|
18
|
+
})
|
|
19
|
+
}
|
|
20
|
+
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import {GuardOptions} from "../../types";
|
|
2
|
-
import {Middleware} from "./middleware.decorator";
|
|
3
|
-
import {RaitonGuards} from "../../core/guards";
|
|
4
|
-
import {HttpStatus, RaitonResponses} from "
|
|
1
|
+
import {GuardOptions} from "../../types/index.ts";
|
|
2
|
+
import {Middleware} from "./middleware.decorator.ts";
|
|
3
|
+
import {RaitonGuards} from "../../core/guards.ts";
|
|
4
|
+
import {HttpStatus, RaitonResponses} from "../index.ts";
|
|
5
5
|
|
|
6
6
|
export function createGuardDecoration({name, handler}: GuardOptions) {
|
|
7
7
|
return Middleware(async ({next, context}) => {
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import "reflect-metadata";
|
|
2
|
+
import {METADATA_KEYS} from "../constants/decorators.constant.ts";
|
|
3
|
+
import {getControllerMetadata} from "../../core/controller/index.ts";
|
|
4
|
+
import {RouteMetaInterface} from "../../types/index.ts";
|
|
5
|
+
|
|
6
|
+
export interface HealthCheckOptions {
|
|
7
|
+
path?: string;
|
|
8
|
+
tags?: string[];
|
|
9
|
+
summary?: string;
|
|
10
|
+
description?: string;
|
|
11
|
+
excludeFromDocs?: boolean;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export interface HealthCheckMetadata {
|
|
15
|
+
options: HealthCheckOptions;
|
|
16
|
+
routeMeta?: RouteMetaInterface;
|
|
17
|
+
controllerPrefix?: string;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const DEFAULT_HEALTH_CHECK_OPTIONS: HealthCheckOptions = {
|
|
21
|
+
path: '/health',
|
|
22
|
+
tags: ['Health'],
|
|
23
|
+
summary: 'Health check endpoint',
|
|
24
|
+
description: 'Returns the health status of the application',
|
|
25
|
+
excludeFromDocs: false,
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
export function HealthCheck(options: HealthCheckOptions = {}) {
|
|
29
|
+
return (target: any, propertyKey: string) => {
|
|
30
|
+
const mergedOptions = {...DEFAULT_HEALTH_CHECK_OPTIONS, ...options};
|
|
31
|
+
const controllerMeta = getControllerMetadata(target.prototype || target);
|
|
32
|
+
const route = controllerMeta.routes.find(r => r.propertyKey === propertyKey);
|
|
33
|
+
|
|
34
|
+
const metadata: HealthCheckMetadata = {
|
|
35
|
+
options: mergedOptions,
|
|
36
|
+
routeMeta: route,
|
|
37
|
+
controllerPrefix: controllerMeta.prefix,
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
Reflect.defineMetadata(METADATA_KEYS.HEALTH_CHECK, metadata, target, propertyKey);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export function getHealthCheckMetadata(target: any, propertyKey: string): HealthCheckMetadata | undefined {
|
|
45
|
+
return Reflect.getMetadata(METADATA_KEYS.HEALTH_CHECK, target, propertyKey);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export function isHealthCheck(target: any, propertyKey: string): boolean {
|
|
49
|
+
return Reflect.hasMetadata(METADATA_KEYS.HEALTH_CHECK, target, propertyKey);
|
|
50
|
+
}
|
|
@@ -1,11 +1,14 @@
|
|
|
1
|
-
export * from "./controllable.decorator"
|
|
2
|
-
export * from "./socket.decorator"
|
|
3
|
-
export * from "./middleware.decorator"
|
|
4
|
-
export * from "./routable.decorator"
|
|
5
|
-
export * from "./parametrable.decorator"
|
|
6
|
-
export * from "./injection.decorator"
|
|
7
|
-
export * from "./guard.decorator"
|
|
8
|
-
export * from "./
|
|
9
|
-
export * from "./
|
|
10
|
-
export * from "./
|
|
11
|
-
export * from "./
|
|
1
|
+
export * from "./controllable.decorator.ts"
|
|
2
|
+
export * from "./socket.decorator.ts"
|
|
3
|
+
export * from "./middleware.decorator.ts"
|
|
4
|
+
export * from "./routable.decorator.ts"
|
|
5
|
+
export * from "./parametrable.decorator.ts"
|
|
6
|
+
export * from "./injection.decorator.ts"
|
|
7
|
+
export * from "./guard.decorator.ts"
|
|
8
|
+
export * from "./csrf-guard.decorator.ts"
|
|
9
|
+
export * from "./csrf-token.decorator.ts"
|
|
10
|
+
export * from "./api-response.decorator.ts"
|
|
11
|
+
export * from "./api.decorator.ts"
|
|
12
|
+
export * from "./route-interaction.decorator.ts"
|
|
13
|
+
export * from "./health-check.decorator.ts"
|
|
14
|
+
export * from "./mcp.decorator.ts"
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import "reflect-metadata";
|
|
2
|
-
import type {ConstructorType} from "../../types/contruct";
|
|
3
|
-
import {Injection} from "../../core/injection";
|
|
2
|
+
import type {ConstructorType} from "../../types/contruct.ts";
|
|
3
|
+
import {Injection} from "../../core/injection/index.ts";
|
|
4
4
|
import {LifetimeEnum} from "@protorians/core";
|
|
5
5
|
import {Logger} from "@protorians/logger";
|
|
6
|
-
import {METADATA_KEYS} from "../constants";
|
|
6
|
+
import {METADATA_KEYS} from "../constants/index.ts";
|
|
7
7
|
|
|
8
8
|
|
|
9
9
|
export function Injectable(lifetime?: LifetimeEnum, name?: string, scope?: any) {
|
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
import "reflect-metadata";
|
|
2
|
-
import {registerMcpServer, unregisterMcpServer, getMcpServerMetadata} from "../../core/mcp";
|
|
3
|
-
import {Injectable} from ".";
|
|
2
|
+
import {registerMcpServer, unregisterMcpServer, getMcpServerMetadata} from "../../core/mcp/index.ts";
|
|
3
|
+
import {Injectable} from "./index.ts";
|
|
4
4
|
import {LifetimeEnum} from "@protorians/core";
|
|
5
|
-
import {METADATA_KEYS} from "../constants";
|
|
5
|
+
import {METADATA_KEYS} from "../constants/index.ts";
|
|
6
6
|
import type {
|
|
7
7
|
McpArgumentMetaInterface,
|
|
8
8
|
McpArgumentSchema,
|
|
9
9
|
McpElementMetaInterface,
|
|
10
10
|
McpElementType,
|
|
11
11
|
McpServerOptionsInterface,
|
|
12
|
-
} from "../../types";
|
|
12
|
+
} from "../../types/index.ts";
|
|
13
13
|
|
|
14
14
|
/**
|
|
15
15
|
* Decorate a class as an MCP server. The class methods decorated with
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {MiddlewareCallable} from "../../types";
|
|
2
|
-
import {getControllerMetadata} from "../../core";
|
|
1
|
+
import {MiddlewareCallable} from "../../types/index.ts";
|
|
2
|
+
import {getControllerMetadata} from "../../core/index.ts";
|
|
3
3
|
|
|
4
4
|
|
|
5
5
|
export function Middleware(middleware: MiddlewareCallable) {
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import "reflect-metadata";
|
|
2
|
-
import type {ContextInterface, ParamMetaInterface} from "../../types";
|
|
3
|
-
import {METADATA_KEYS, Parametrable} from "
|
|
2
|
+
import type {ContextInterface, ParamMetaInterface} from "../../types/index.ts";
|
|
3
|
+
import {METADATA_KEYS, Parametrable} from "../index.ts";
|
|
4
4
|
|
|
5
5
|
function createRouteParametrableDecorator(type: ParamMetaInterface['type'], callable?: (context: ContextInterface) => any) {
|
|
6
6
|
return (key?: string) => {
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import {getControllerMetadata} from "../../core";
|
|
2
|
-
import {HttpMethod} from "
|
|
3
|
-
import {ControllerMetaInterface, RouteDecoratorCallable, RouteMetaInterface} from "../../types";
|
|
1
|
+
import {getControllerMetadata} from "../../core/index.ts";
|
|
2
|
+
import {HttpMethod} from "../index.ts";
|
|
3
|
+
import {ControllerMetaInterface, RouteDecoratorCallable, RouteMetaInterface} from "../../types/index.ts";
|
|
4
4
|
|
|
5
5
|
|
|
6
6
|
function stabilizeRoute(meta: ControllerMetaInterface, {path, method, propertyKey}: Partial<RouteMetaInterface>) {
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import "reflect-metadata";
|
|
2
|
-
import {METADATA_KEYS} from "../constants/decorators.constant";
|
|
3
|
-
import {getControllerMetadata} from "../../core/controller";
|
|
4
|
-
import {RouteInteractionsSubscriber} from "../../types/router";
|
|
5
|
-
import { Raiton } from "../../core/raiton";
|
|
2
|
+
import {METADATA_KEYS} from "../constants/decorators.constant.ts";
|
|
3
|
+
import {getControllerMetadata} from "../../core/controller/index.ts";
|
|
4
|
+
import {RouteInteractionsSubscriber} from "../../types/router.ts";
|
|
5
|
+
import { Raiton } from "../../core/raiton.ts";
|
|
6
6
|
|
|
7
7
|
/**
|
|
8
8
|
* Represents a route interaction that can be rendered with context data
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import {getSocketMetadata, registerSocket} from "../../core/socket";
|
|
2
|
-
import {Injectable} from "
|
|
1
|
+
import {getSocketMetadata, registerSocket} from "../../core/socket/index.ts";
|
|
2
|
+
import {Injectable} from "../index.ts";
|
|
3
3
|
import {LifetimeEnum} from "@protorians/core";
|
|
4
|
-
import {SocketEventType} from "../../types";
|
|
4
|
+
import {SocketEventType} from "../../types/index.ts";
|
|
5
5
|
|
|
6
6
|
export function Socket(namespace = '/') {
|
|
7
7
|
return (target: any) => {
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import crypto from "node:crypto";
|
|
2
2
|
import argon2, {Options} from "argon2";
|
|
3
|
-
import {HashAlgoEnum, PasswordAlgoEnum} from "./enums";
|
|
3
|
+
import {HashAlgoEnum, PasswordAlgoEnum} from "./enums/index.ts";
|
|
4
4
|
import bcrypt from "bcrypt";
|
|
5
|
-
import {DerivationOptionsInterface, EncryptionResultType, ScryptOptionsInterface} from "../types";
|
|
5
|
+
import {DerivationOptionsInterface, EncryptionResultType, ScryptOptionsInterface} from "../types/index.ts";
|
|
6
6
|
|
|
7
7
|
export class Encryption {
|
|
8
8
|
static get algos() {
|
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
export * from "./http-method.enum"
|
|
2
|
-
export * from "./http-parameters.enum"
|
|
3
|
-
export * from "./http-status.enum"
|
|
4
|
-
export * from "./encrypted.enum"
|
|
5
|
-
export * from "./event.message.enum"
|
|
6
|
-
export * from "./timestamp.enum"
|
|
7
|
-
export * from "./runtime.enum"
|
|
1
|
+
export * from "./http-method.enum.ts"
|
|
2
|
+
export * from "./http-parameters.enum.ts"
|
|
3
|
+
export * from "./http-status.enum.ts"
|
|
4
|
+
export * from "./encrypted.enum.ts"
|
|
5
|
+
export * from "./event.message.enum.ts"
|
|
6
|
+
export * from "./timestamp.enum.ts"
|
|
7
|
+
export * from "./runtime.enum.ts"
|
|
8
|
+
export * from "./csrf.enum.ts"
|
package/source/framework/env.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {GenericValueType} from "../types/generic";
|
|
2
|
-
import {getType} from "./utilities";
|
|
1
|
+
import {GenericValueType} from "../types/generic.ts";
|
|
2
|
+
import {getType} from "./utilities/index.ts";
|
|
3
3
|
|
|
4
4
|
export function env<T>(key: string, defaultValue?: T, type?: GenericValueType): T | undefined {
|
|
5
5
|
const value = process.env[key] || defaultValue;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import {HttpStatus} from "../enums/http-status.enum";
|
|
2
|
-
import {Raiton} from "../../core";
|
|
3
|
-
import {HttpResponseBaseInterface} from "../../types";
|
|
1
|
+
import {HttpStatus} from "../enums/http-status.enum.ts";
|
|
2
|
+
import {Raiton} from "../../core/index.ts";
|
|
3
|
+
import {HttpResponseBaseInterface} from "../../types/index.ts";
|
|
4
4
|
|
|
5
5
|
export class HttpException extends Error {
|
|
6
6
|
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export * from "./http-exception"
|
|
2
|
-
export * from "./throwable"
|
|
1
|
+
export * from "./http-exception.ts"
|
|
2
|
+
export * from "./throwable.ts"
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
import {HttpStatus, HttpMethod} from "./enums/index.ts";
|
|
2
|
+
import {RaitonResponses} from "./responses/helpers.ts";
|
|
3
|
+
import {ApplicationInterface} from "../types/application.ts";
|
|
4
|
+
import {RequestContext} from "../core/context.ts";
|
|
5
|
+
|
|
6
|
+
export interface HealthCheckConfig {
|
|
7
|
+
enabled: boolean;
|
|
8
|
+
path: string;
|
|
9
|
+
response?: {
|
|
10
|
+
status?: 'healthy' | 'unhealthy' | 'degraded';
|
|
11
|
+
statusCode?: HttpStatus;
|
|
12
|
+
message?: string;
|
|
13
|
+
};
|
|
14
|
+
checks?: HealthCheckCheck[];
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export type HealthCheckStatus = 'healthy' | 'unhealthy' | 'degraded';
|
|
18
|
+
|
|
19
|
+
export interface HealthCheckResult {
|
|
20
|
+
name: string;
|
|
21
|
+
status: HealthCheckStatus;
|
|
22
|
+
message?: string;
|
|
23
|
+
duration?: number;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export interface HealthCheckCheck {
|
|
27
|
+
name: string;
|
|
28
|
+
check: () => Promise<{ status: HealthCheckStatus; message?: string }> | { status: HealthCheckStatus; message?: string };
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export const DEFAULT_HEALTH_CHECK_CONFIG: HealthCheckConfig = {
|
|
32
|
+
enabled: true,
|
|
33
|
+
path: '/health',
|
|
34
|
+
response: {
|
|
35
|
+
status: 'healthy',
|
|
36
|
+
statusCode: HttpStatus.OK,
|
|
37
|
+
message: 'Service is healthy',
|
|
38
|
+
},
|
|
39
|
+
checks: [],
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
export class HealthCheckManager {
|
|
43
|
+
private static _config: HealthCheckConfig = {...DEFAULT_HEALTH_CHECK_CONFIG};
|
|
44
|
+
private static _customHandler: ((ctx: RequestContext) => Promise<any> | any) | null = null;
|
|
45
|
+
|
|
46
|
+
static configure(config: Partial<HealthCheckConfig>): void {
|
|
47
|
+
this._config = {...this._config, ...config};
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
static getConfig(): HealthCheckConfig {
|
|
51
|
+
return {...this._config};
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
static registerCustomHandler(handler: (ctx: RequestContext) => Promise<any> | any): void {
|
|
55
|
+
this._customHandler = handler;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
static async runChecks(): Promise<HealthCheckResult[]> {
|
|
59
|
+
const results: HealthCheckResult[] = [];
|
|
60
|
+
|
|
61
|
+
for (const check of this._config.checks || []) {
|
|
62
|
+
const start = Date.now();
|
|
63
|
+
try {
|
|
64
|
+
const result = await check.check();
|
|
65
|
+
results.push({
|
|
66
|
+
name: check.name,
|
|
67
|
+
status: result.status,
|
|
68
|
+
message: result.message,
|
|
69
|
+
duration: Date.now() - start,
|
|
70
|
+
});
|
|
71
|
+
} catch (error: any) {
|
|
72
|
+
results.push({
|
|
73
|
+
name: check.name,
|
|
74
|
+
status: 'unhealthy',
|
|
75
|
+
message: error.message || 'Check failed',
|
|
76
|
+
duration: Date.now() - start,
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
return results;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
static async handleHealthCheck(ctx: RequestContext): Promise<any> {
|
|
85
|
+
if (this._customHandler) {
|
|
86
|
+
return this._customHandler(ctx);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const checks = await this.runChecks();
|
|
90
|
+
const timestamp = new Date().toISOString();
|
|
91
|
+
|
|
92
|
+
let overallStatus: HealthCheckStatus = 'healthy';
|
|
93
|
+
if (checks.some(c => c.status === 'unhealthy')) {
|
|
94
|
+
overallStatus = 'unhealthy';
|
|
95
|
+
} else if (checks.some(c => c.status === 'degraded')) {
|
|
96
|
+
overallStatus = 'degraded';
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const statusCode = overallStatus === 'unhealthy'
|
|
100
|
+
? HttpStatus.SERVICE_UNAVAILABLE
|
|
101
|
+
: (this._config.response?.statusCode || HttpStatus.OK);
|
|
102
|
+
|
|
103
|
+
return RaitonResponses(
|
|
104
|
+
this._config.response?.message || `Service is ${overallStatus}`,
|
|
105
|
+
{
|
|
106
|
+
status: overallStatus,
|
|
107
|
+
timestamp,
|
|
108
|
+
checks: checks.length > 0 ? checks : undefined,
|
|
109
|
+
uptime: process.uptime(),
|
|
110
|
+
version: process.env.npm_package_version || '0.0.0',
|
|
111
|
+
},
|
|
112
|
+
statusCode,
|
|
113
|
+
{error: overallStatus === 'unhealthy'}
|
|
114
|
+
);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
export function hasRegisteredHealthCheck(app: ApplicationInterface): boolean {
|
|
119
|
+
const config = HealthCheckManager.getConfig();
|
|
120
|
+
const rootScope = (app as any).root;
|
|
121
|
+
if (!rootScope?.router) return false;
|
|
122
|
+
|
|
123
|
+
const routes = rootScope.router.getRoutes();
|
|
124
|
+
return routes.some((route: any) =>
|
|
125
|
+
route.path === config.path && route.method === HttpMethod.GET
|
|
126
|
+
);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
export function registerDefaultHealthCheck(app: ApplicationInterface): void {
|
|
130
|
+
const config = HealthCheckManager.getConfig();
|
|
131
|
+
if (!config.enabled) return;
|
|
132
|
+
if (hasRegisteredHealthCheck(app)) return;
|
|
133
|
+
|
|
134
|
+
app.get(config.path, (ctx: RequestContext) => {
|
|
135
|
+
return HealthCheckManager.handleHealthCheck(ctx);
|
|
136
|
+
});
|
|
137
|
+
}
|