raiton 6.6.0-beta.14 → 6.8.1-beta.17
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 +30 -0
- package/README.md +16 -9
- package/build/bin/index.mjs +9150 -3911
- package/deno.json +4 -1
- package/docs/contributors/README.md +1 -0
- package/docs/contributors/core/README.md +8 -0
- package/docs/contributors/core/scheduler.md +113 -0
- 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 +5 -0
- package/docs/contributors/framework/decorators/scheduler.md +83 -0
- package/docs/contributors/framework/runtime.md +13 -3
- package/docs/user-guide/README.md +9 -5
- package/docs/user-guide/cache.md +79 -0
- package/docs/user-guide/cli.md +35 -3
- package/docs/user-guide/decorators/README.md +3 -0
- package/docs/user-guide/decorators/cache.md +79 -0
- package/docs/user-guide/decorators/mcp.md +2 -2
- package/docs/user-guide/decorators/scheduler.md +75 -0
- package/docs/user-guide/mcp.md +2 -2
- package/docs/user-guide/runtime.md +2 -1
- package/docs/user-guide/scheduler.md +229 -0
- package/package.json +9 -4
- 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 +81 -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 +16 -10
- 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 +17 -16
- package/source/core/injection/index.ts +1 -1
- package/source/core/injection/injection.ts +7 -4
- 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/scheduler/builder.ts +25 -0
- package/source/core/scheduler/cron.ts +400 -0
- package/source/core/scheduler/duration.ts +42 -0
- package/source/core/scheduler/index.ts +6 -0
- package/source/core/scheduler/manager.ts +542 -0
- package/source/core/scheduler/metadata.ts +53 -0
- package/source/core/scheduler/queue/backend.ts +142 -0
- package/source/core/scheduler/queue/index.ts +3 -0
- package/source/core/scheduler/queue/rabbitmq.backend.ts +124 -0
- package/source/core/scheduler/queue/redis.backend.ts +144 -0
- 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 +55 -22
- package/source/framework/artifacts.ts +32 -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 +4 -4
- package/source/framework/decorators/csrf-token.decorator.ts +1 -1
- package/source/framework/decorators/guard.decorator.ts +4 -4
- package/source/framework/decorators/health-check.decorator.ts +3 -3
- package/source/framework/decorators/index.ts +15 -14
- 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/scheduler.decorator.ts +139 -0
- package/source/framework/decorators/socket.decorator.ts +3 -3
- package/source/framework/encryption.ts +2 -2
- package/source/framework/enums/index.ts +9 -8
- package/source/framework/enums/runtime.enum.ts +1 -2
- package/source/framework/enums/scheduler.enum.ts +23 -0
- 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 +4 -4
- package/source/framework/index.ts +18 -17
- 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 +6 -6
- package/source/framework/plugins/security/headers.ts +2 -2
- package/source/framework/plugins/security/index.ts +6 -6
- 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 +1 -1
- package/source/framework/utilities/index.ts +10 -10
- 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 +1 -1
- package/source/types/guard.ts +1 -1
- package/source/types/http-responses.ts +1 -1
- package/source/types/index.ts +27 -26
- 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 +2 -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/scheduler.ts +180 -0
- package/source/types/thread.ts +4 -4
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
import type {RequestContext} from "../../core/context.ts";
|
|
2
|
+
import type {CacheEntry, CacheOptions, CacheStore} from "./types.ts";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Store en mémoire, à durée de vie bornée (TTL) et taille maximale bornée,
|
|
6
|
+
* utilisé par défaut pour la mise en cache des réponses de routes.
|
|
7
|
+
*/
|
|
8
|
+
export class MemoryCacheStore implements CacheStore {
|
|
9
|
+
private entries = new Map<string, CacheEntry>();
|
|
10
|
+
private readonly maxSize: number;
|
|
11
|
+
|
|
12
|
+
constructor(maxSize = 1000) {
|
|
13
|
+
this.maxSize = maxSize;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
get<T = any>(key: string): CacheEntry<T> | undefined {
|
|
17
|
+
const entry = this.entries.get(key);
|
|
18
|
+
if (!entry) return undefined;
|
|
19
|
+
if (entry.expiresAt <= Date.now()) {
|
|
20
|
+
this.entries.delete(key);
|
|
21
|
+
return undefined;
|
|
22
|
+
}
|
|
23
|
+
return entry;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
set<T = any>(key: string, entry: CacheEntry<T>, _ttl?: number): void {
|
|
27
|
+
this.evictIfNeeded();
|
|
28
|
+
this.entries.set(key, entry);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
has(key: string): boolean {
|
|
32
|
+
return this.get(key) !== undefined;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
delete(key: string): boolean {
|
|
36
|
+
return this.entries.delete(key);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
clear(): void {
|
|
40
|
+
this.entries.clear();
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
size(): number {
|
|
44
|
+
return this.entries.size;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
private evictIfNeeded(): void {
|
|
48
|
+
if (this.entries.size < this.maxSize) return;
|
|
49
|
+
const oldestKey = this.entries.keys().next().value;
|
|
50
|
+
if (oldestKey !== undefined) this.entries.delete(oldestKey);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* `CacheManager` configure le comportement global du cache de routes, à la
|
|
56
|
+
* manière des autres managers du framework.
|
|
57
|
+
*/
|
|
58
|
+
export class CacheManager {
|
|
59
|
+
private static _store: CacheStore = new MemoryCacheStore();
|
|
60
|
+
private static _defaults: Partial<CacheOptions> = {};
|
|
61
|
+
|
|
62
|
+
static configure(options: Partial<CacheOptions>): void {
|
|
63
|
+
this._defaults = {...this._defaults, ...options};
|
|
64
|
+
if (options.maxEntries && this._store instanceof MemoryCacheStore) {
|
|
65
|
+
this._store = new MemoryCacheStore(options.maxEntries);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
static getDefaults(): Partial<CacheOptions> {
|
|
70
|
+
return {...this._defaults};
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
static getStore(): CacheStore {
|
|
74
|
+
return this._store;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
static setStore(store: CacheStore): void {
|
|
78
|
+
this._store = store;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
static clear(): void {
|
|
82
|
+
this._store.clear();
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const DEFAULT_METHODS = ['GET', 'HEAD'];
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Construit la clé de cache par défaut : méthode + route + paramètres d'URL.
|
|
90
|
+
*/
|
|
91
|
+
export function defaultCacheKey(context: RequestContext, route: string): string {
|
|
92
|
+
const parts = [context.req.method, route];
|
|
93
|
+
const params = (context as any).params ?? context.req.params;
|
|
94
|
+
if (params) {
|
|
95
|
+
for (const value of Object.values(params)) {
|
|
96
|
+
parts.push(String(value));
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
return parts.join('|');
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Résout les options effectives d'une route (défauts globaux + options locales).
|
|
104
|
+
*/
|
|
105
|
+
export function resolveCacheOptions(route: string, options: CacheOptions = {}): CacheOptions {
|
|
106
|
+
return {
|
|
107
|
+
...CacheManager.getDefaults(),
|
|
108
|
+
...options,
|
|
109
|
+
key: options.key ?? CacheManager.getDefaults().key,
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Calcule la clé de cache pour une requête donnée.
|
|
115
|
+
*/
|
|
116
|
+
export async function buildCacheKey(context: RequestContext, route: string, options: CacheOptions): Promise<string> {
|
|
117
|
+
const base = options.key
|
|
118
|
+
? await options.key(context, route)
|
|
119
|
+
: defaultCacheKey(context, route);
|
|
120
|
+
|
|
121
|
+
const vary = options.vary ?? [];
|
|
122
|
+
if (vary.length === 0) return base;
|
|
123
|
+
|
|
124
|
+
const varyParts = vary.map(name => `${name}=${context.req.headers.get(name) ?? ''}`);
|
|
125
|
+
return `${base}|${varyParts.join('&')}`;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Applique une réponse en cache au contexte (statut + en-têtes + corps).
|
|
130
|
+
*/
|
|
131
|
+
export function sendCachedEntry(context: RequestContext, entry: CacheEntry): void {
|
|
132
|
+
context.reply.status(entry.status);
|
|
133
|
+
for (const [name, value] of Object.entries(entry.headers)) {
|
|
134
|
+
context.reply.header(name, value);
|
|
135
|
+
}
|
|
136
|
+
context.reply.send(entry.body);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Mécanisme cœur du cache de routes, pensé pour être intégré au niveau du
|
|
141
|
+
* handler (dans `createHandler`). Il évite complètement d'exécuter le handler
|
|
142
|
+
* tant qu'une entrée fraîche existe pour la clé de requête.
|
|
143
|
+
*
|
|
144
|
+
* - `get` renvoie l'entrée fraîche si elle existe.
|
|
145
|
+
* - `set` stocke la réponse si elle est éligible (méthode + statut 2xx).
|
|
146
|
+
*/
|
|
147
|
+
export class RouteCache {
|
|
148
|
+
private readonly store: CacheStore;
|
|
149
|
+
private readonly route: string;
|
|
150
|
+
private readonly options: CacheOptions;
|
|
151
|
+
|
|
152
|
+
constructor(route: string, options: CacheOptions = {}) {
|
|
153
|
+
this.route = route;
|
|
154
|
+
this.options = resolveCacheOptions(route, options);
|
|
155
|
+
this.store = CacheManager.getStore();
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
get enabled(): boolean {
|
|
159
|
+
return this.options.enabled ?? true;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
matchesMethod(method: string): boolean {
|
|
163
|
+
return (this.options.methods ?? DEFAULT_METHODS).includes(method.toUpperCase());
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
key(context: RequestContext): Promise<string> {
|
|
167
|
+
return buildCacheKey(context, this.route, this.options);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
async get<T = any>(context: RequestContext, key?: string): Promise<CacheEntry<T> | undefined> {
|
|
171
|
+
return this.store.get<T>(key ?? await this.key(context));
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
async set<T = any>(context: RequestContext, body: T, status = 200, headers: Record<string, string> = {}, key?: string): Promise<void> {
|
|
175
|
+
const ttl = this.options.ttl ?? 60_000;
|
|
176
|
+
this.store.set(key ?? await this.key(context), {
|
|
177
|
+
body,
|
|
178
|
+
status,
|
|
179
|
+
headers,
|
|
180
|
+
createdAt: Date.now(),
|
|
181
|
+
expiresAt: Date.now() + ttl,
|
|
182
|
+
}, ttl);
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
shouldCache(status: number): boolean {
|
|
186
|
+
return status >= 200 && status < 300;
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* Helper de contrôle-manipulation du cache-Control, valeur commode pour les
|
|
192
|
+
* routes qui ne veulent pas passer par le middleware ETag.
|
|
193
|
+
*/
|
|
194
|
+
export function cacheControl(context: RequestContext, value: string | number, publicAccess = true): void {
|
|
195
|
+
const directive = typeof value === 'number' ? `max-age=${value}` : value;
|
|
196
|
+
context.reply.header('Cache-Control', `${publicAccess ? 'public' : 'private'}, ${directive}`);
|
|
197
|
+
}
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
import type {RequestContext} from "../../core/context.ts";
|
|
2
|
+
import type {ETagOptions, ETagResult} from "./types.ts";
|
|
3
|
+
import {HttpStatus} from "../enums/index.ts";
|
|
4
|
+
|
|
5
|
+
const DEFAULT_HEADER_NAME = 'ETag';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Hachage FNV-1a (32 bits) utilisé par défaut pour générer une valeur d'ETag
|
|
9
|
+
* stable à partir du corps sérialisé de la réponse.
|
|
10
|
+
*/
|
|
11
|
+
function fnv1a(input: string): string {
|
|
12
|
+
let hash = 0x811c9dc5;
|
|
13
|
+
for (let i = 0; i < input.length; i++) {
|
|
14
|
+
hash ^= input.charCodeAt(i);
|
|
15
|
+
hash = Math.imul(hash, 0x01000193);
|
|
16
|
+
}
|
|
17
|
+
return (hash >>> 0).toString(16);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Sérialise un corps de réponse en chaîne de caractères, de la même manière
|
|
22
|
+
* que les runtimes du framework le font avant l'envoi.
|
|
23
|
+
*/
|
|
24
|
+
function serialize(body: any): string {
|
|
25
|
+
if (body === undefined || body === null) return '';
|
|
26
|
+
if (typeof body === 'string') return body;
|
|
27
|
+
if (Buffer.isBuffer(body)) return body.toString();
|
|
28
|
+
if (typeof body === 'object' && !(body instanceof Response)) return JSON.stringify(body);
|
|
29
|
+
return String(body);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* `Etagger` est le gestionnaire complet des ETag. Il est entièrement
|
|
34
|
+
* personnalisable par le développeur : algorithme de hachage, marqueur faible,
|
|
35
|
+
* en-tête, cache-control et prédicat d'exclusion.
|
|
36
|
+
*
|
|
37
|
+
* @example
|
|
38
|
+
* const etagger = new Etagger({ weak: true, cacheControl: { maxAge: 300 } })
|
|
39
|
+
* const { tag, notModified } = await etagger.evaluate(context, body)
|
|
40
|
+
*/
|
|
41
|
+
export class Etagger {
|
|
42
|
+
private readonly enabled: boolean;
|
|
43
|
+
private readonly weak: boolean;
|
|
44
|
+
private readonly algorithm?: (body: string) => string | Promise<string>;
|
|
45
|
+
private readonly cacheControl?: ETagOptions['cacheControl'];
|
|
46
|
+
private readonly headerName: string;
|
|
47
|
+
private readonly skip?: ETagOptions['skip'];
|
|
48
|
+
|
|
49
|
+
constructor(options: ETagOptions = {}) {
|
|
50
|
+
this.enabled = options.enabled ?? true;
|
|
51
|
+
this.weak = options.weak ?? false;
|
|
52
|
+
this.algorithm = options.algorithm;
|
|
53
|
+
this.cacheControl = options.cacheControl;
|
|
54
|
+
this.headerName = options.headerName ?? DEFAULT_HEADER_NAME;
|
|
55
|
+
this.skip = options.skip;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Calcule la valeur d'ETag d'un corps de réponse sérialisé.
|
|
60
|
+
*/
|
|
61
|
+
async generate(body: any): Promise<string> {
|
|
62
|
+
const serialized = serialize(body);
|
|
63
|
+
let value: string;
|
|
64
|
+
if (this.algorithm) {
|
|
65
|
+
value = await this.algorithm(serialized);
|
|
66
|
+
} else {
|
|
67
|
+
value = fnv1a(serialized);
|
|
68
|
+
}
|
|
69
|
+
value = `"${value}"`;
|
|
70
|
+
if (this.weak) value = `W/${value}`;
|
|
71
|
+
return value;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Vérifie si la liste d'ETag envoyée par le client (en-tête `If-None-Match`)
|
|
76
|
+
* matche avec l'ETag calculé. Un `*` matche n'importe quelle ressource.
|
|
77
|
+
*/
|
|
78
|
+
matches(clientTags: string | null | undefined, tag: string): boolean {
|
|
79
|
+
if (!clientTags) return false;
|
|
80
|
+
const expected = tag.replace(/^W\//, '');
|
|
81
|
+
return clientTags.split(',').some(part => {
|
|
82
|
+
const candidate = part.trim().replace(/^W\//, '');
|
|
83
|
+
return candidate === '*' || candidate === expected;
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Applique la politique de cache à en-têtes de réponse.
|
|
89
|
+
*/
|
|
90
|
+
applyCacheControl(context: RequestContext): void {
|
|
91
|
+
const cc = this.cacheControl;
|
|
92
|
+
if (!cc) return;
|
|
93
|
+
|
|
94
|
+
const directives: string[] = [];
|
|
95
|
+
if (cc === true) {
|
|
96
|
+
directives.push('no-cache');
|
|
97
|
+
} else {
|
|
98
|
+
if (cc.public) directives.push('public');
|
|
99
|
+
else directives.push('private');
|
|
100
|
+
if (cc.maxAge !== undefined) directives.push(`max-age=${cc.maxAge}`);
|
|
101
|
+
if (cc.noCache) directives.push('no-cache');
|
|
102
|
+
if (cc.noStore) directives.push('no-store');
|
|
103
|
+
if (cc.mustRevalidate) directives.push('must-revalidate');
|
|
104
|
+
}
|
|
105
|
+
context.reply.header('Cache-Control', directives.join(', '));
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Évalue la requête condititionnelle complète. Si un ETag fort/faible est
|
|
110
|
+
* présent et que le client l'a déjà (via `If-None-Match`), la réponse est
|
|
111
|
+
* marquée `notModified` et doit être envoyée avec le statut `304`.
|
|
112
|
+
*/
|
|
113
|
+
async evaluate(context: RequestContext, body: any): Promise<ETagResult> {
|
|
114
|
+
if (!this.enabled || this.skip?.(context, body)) {
|
|
115
|
+
return {tag: '', body, notModified: false};
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
const tag = await this.generate(body);
|
|
119
|
+
const header = this.headerName;
|
|
120
|
+
context.reply.header(header, tag);
|
|
121
|
+
this.applyCacheControl(context);
|
|
122
|
+
|
|
123
|
+
const ifNoneMatch = context.req.headers.get('if-none-match');
|
|
124
|
+
if (this.matches(ifNoneMatch, tag)) {
|
|
125
|
+
return {tag, body: null, notModified: true};
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
return {tag, body, notModified: false};
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Raccourci statique, construit une instance à la volée et évalue.
|
|
133
|
+
*/
|
|
134
|
+
static async apply(context: RequestContext, body: any, options?: ETagOptions): Promise<ETagResult> {
|
|
135
|
+
return new Etagger(options).evaluate(context, body);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Instance singulière utilisée par défaut par le middleware `etag()`.
|
|
141
|
+
*/
|
|
142
|
+
export const defaultEtagger = new Etagger();
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Middleware ETag prêt à l'emploi. À brancher globalement ou par route :
|
|
146
|
+
*
|
|
147
|
+
* ```typescript
|
|
148
|
+
* app.use(etag({ weak: false, cacheControl: { maxAge: 300 } }))
|
|
149
|
+
* ```
|
|
150
|
+
*
|
|
151
|
+
* Le middleware intercepte `context.reply.send`, calcule l'ETag du corps
|
|
152
|
+
* sérialisé, pose l'en-tête correspondant et répond `304 Not Modified` si le
|
|
153
|
+
* client déclare déjà posséder la représentation (`If-None-Match`).
|
|
154
|
+
*/
|
|
155
|
+
export function etag(options: ETagOptions = {}) {
|
|
156
|
+
return async ({context, next}: { context: RequestContext; next: () => Promise<any> }) => {
|
|
157
|
+
const reply = context.reply;
|
|
158
|
+
const originalSend = reply.send.bind(reply);
|
|
159
|
+
const etagger = new Etagger(options);
|
|
160
|
+
|
|
161
|
+
reply.send = (body: any) => {
|
|
162
|
+
etagger.evaluate(context, body).then(({notModified, body: nextBody}) => {
|
|
163
|
+
if (notModified) {
|
|
164
|
+
reply.status(HttpStatus.NOT_MODIFIED);
|
|
165
|
+
originalSend(null);
|
|
166
|
+
return;
|
|
167
|
+
}
|
|
168
|
+
originalSend(nextBody);
|
|
169
|
+
});
|
|
170
|
+
};
|
|
171
|
+
|
|
172
|
+
await next();
|
|
173
|
+
};
|
|
174
|
+
}
|
|
@@ -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,10 +18,14 @@ 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'),
|
|
21
23
|
// Health check metadata key
|
|
22
24
|
HEALTH_CHECK: Symbol('health:check'),
|
|
23
25
|
// MCP metadata keys
|
|
24
26
|
MCP: Symbol('mcp:meta'),
|
|
25
27
|
MCP_ELEMENTS: Symbol('mcp:elements'),
|
|
26
28
|
MCP_ARGUMENTS: Symbol('mcp:arguments'),
|
|
29
|
+
// Scheduler metadata keys
|
|
30
|
+
SCHEDULER: Symbol('scheduler:meta'),
|
|
27
31
|
}
|
|
@@ -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) => {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import {MiddlewareParametersInterface, CsrfGuardOptions} from "../../types";
|
|
2
|
-
import {Middleware} from "./middleware.decorator";
|
|
3
|
-
import {HttpStatus, RaitonResponses} from "
|
|
4
|
-
import {CsrfUtil} from "../utilities/csrf.util";
|
|
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
5
|
|
|
6
6
|
const DEFAULT_METHODS = ['POST', 'PUT', 'PATCH', 'DELETE']
|
|
7
7
|
const DEFAULT_HEADER_NAME = 'X-CSRF-Token'
|
|
@@ -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}) => {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import "reflect-metadata";
|
|
2
|
-
import {METADATA_KEYS} from "../constants/decorators.constant";
|
|
3
|
-
import {getControllerMetadata} from "../../core/controller";
|
|
4
|
-
import {RouteMetaInterface} from "../../types";
|
|
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
5
|
|
|
6
6
|
export interface HealthCheckOptions {
|
|
7
7
|
path?: string;
|
|
@@ -1,14 +1,15 @@
|
|
|
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 "./csrf-guard.decorator"
|
|
9
|
-
export * from "./csrf-token.decorator"
|
|
10
|
-
export * from "./api-response.decorator"
|
|
11
|
-
export * from "./api.decorator"
|
|
12
|
-
export * from "./route-interaction.decorator"
|
|
13
|
-
export * from "./health-check.decorator"
|
|
14
|
-
export * from "./mcp.decorator"
|
|
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"
|
|
15
|
+
export * from "./scheduler.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) => {
|