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
package/deno.json
CHANGED
|
@@ -4,6 +4,9 @@
|
|
|
4
4
|
"@/": "./source/"
|
|
5
5
|
},
|
|
6
6
|
"tasks": {
|
|
7
|
-
"
|
|
7
|
+
"dev": "deno run --allow-all --watch source/bin/index.ts dev",
|
|
8
|
+
"build": "deno run --allow-all source/bin/index.ts build",
|
|
9
|
+
"start": "deno run --allow-all source/bin/index.ts start",
|
|
10
|
+
"compile": "deno compile --no-check --allow-all --output build/bin/raiton source/bin/index.ts"
|
|
8
11
|
}
|
|
9
12
|
}
|
|
@@ -29,6 +29,7 @@ docs/
|
|
|
29
29
|
│ ├── middleware.md ← Pipeline middleware
|
|
30
30
|
│ ├── controller.md ← ControllerBuilder, compilation
|
|
31
31
|
│ ├── injection.md ← Injection de dépendances
|
|
32
|
+
│ ├── scheduler.md ← SchedulerManager, cron, files d'attente
|
|
32
33
|
│ ├── plugin-scope.md ← Système de plugins
|
|
33
34
|
│ ├── config.md ← Configuration
|
|
34
35
|
│ └── builder-thread.md ← Build, thread, cycle de vie
|
|
@@ -13,6 +13,7 @@ Le noyau de Raiton gère le cycle de vie de l'application, le routage, le middle
|
|
|
13
13
|
| [middleware.md](middleware.md) | Pipeline middleware — `MiddlewarePipeline` |
|
|
14
14
|
| [controller.md](controller.md) | Contrôleurs — `ControllerBuilder`, compilation |
|
|
15
15
|
| [injection.md](injection.md) | Injection de dépendances — `Injection`, scopes |
|
|
16
|
+
| [scheduler.md](scheduler.md) | Task scheduler — `SchedulerManager`, cron, files d'attente |
|
|
16
17
|
| [plugin-scope.md](plugin-scope.md) | Système de plugins — `PluginScope` |
|
|
17
18
|
| [config.md](config.md) | Configuration — `RaitonConfig` |
|
|
18
19
|
| [builder-thread.md](builder-thread.md) | Build et thread — `RaitonBuilder`, `RaitonThread` |
|
|
@@ -55,6 +56,13 @@ core/
|
|
|
55
56
|
│ └── injection.ts → Injection (conteneur DI)
|
|
56
57
|
├── socket/
|
|
57
58
|
│ └── metadata.ts → getSocketMetadata
|
|
59
|
+
├── scheduler/
|
|
60
|
+
│ ├── cron.ts → parseur cron + prochain run
|
|
61
|
+
│ ├── duration.ts → parseur de durées
|
|
62
|
+
│ ├── metadata.ts → registre des schedulers
|
|
63
|
+
│ ├── manager.ts → SchedulerManager
|
|
64
|
+
│ ├── builder.ts → compileScheduler
|
|
65
|
+
│ └── queue/ → backends mémoire, Redis, RabbitMQ
|
|
58
66
|
├── artifacts/
|
|
59
67
|
│ ├── artifact.ts → Artifact
|
|
60
68
|
│ └── runner.ts → artifactRunner (stub)
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
# Scheduler
|
|
2
|
+
|
|
3
|
+
**Fichiers :** `source/core/scheduler/`
|
|
4
|
+
**Import :** `import { SchedulerManager, compileScheduler, parseCron, MemoryQueueBackend, RedisQueueBackend, RabbitMqQueueBackend } from "raiton/core"`
|
|
5
|
+
|
|
6
|
+
> **Navigation :** [← injection.md](injection.md) | [↑ core/](README.md) | [plugin-scope.md →](plugin-scope.md)
|
|
7
|
+
|
|
8
|
+
## Vue d'ensemble
|
|
9
|
+
|
|
10
|
+
Le module `core/scheduler/` fournit un task scheduler déclaratif. Il repose sur un `SchedulerManager` singleton qui détient les jobs compilés, les planifie (cron, intervalle, timeout) et consomme les files d'attente via des backends interchangeables.
|
|
11
|
+
|
|
12
|
+
```
|
|
13
|
+
core/scheduler/
|
|
14
|
+
├── cron.ts → parseur cron pur + calcul du prochain run (timezone-aware)
|
|
15
|
+
├── duration.ts → parseur de durées ("5s", "1h30m", …) vers millisecondes
|
|
16
|
+
├── metadata.ts → registre des schedulers (getSchedulerMetadata, registerScheduler)
|
|
17
|
+
├── manager.ts → SchedulerManager (cycle de vie, jobs, workers, contrôle)
|
|
18
|
+
├── builder.ts → compileScheduler (résolution DI + enregistrement)
|
|
19
|
+
└── queue/
|
|
20
|
+
├── backend.ts → BaseQueueBackend + MemoryQueueBackend
|
|
21
|
+
├── redis.backend.ts → RedisQueueBackend (import paresseux de `redis`)
|
|
22
|
+
└── rabbitmq.backend.ts → RabbitMqQueueBackend (import paresseux de `amqplib`)
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## Décorateurs → métadonnées
|
|
26
|
+
|
|
27
|
+
Les décorateurs (`framework/decorators/scheduler.decorator.ts`) écrivent les métadonnées sur le prototype de la classe, de la même façon que les sockets ou MCP :
|
|
28
|
+
|
|
29
|
+
- `@Scheduler()` — marque la classe, l'enregistre en DI (`Injectable`) et dans le registre
|
|
30
|
+
- `@Cron()`, `@Every()`, `@Timeout()`, `@Task()` — poussent un `JobMetaInterface` dans `meta.jobs`
|
|
31
|
+
|
|
32
|
+
```typescript
|
|
33
|
+
interface JobMetaInterface {
|
|
34
|
+
propertyKey: string
|
|
35
|
+
name: string
|
|
36
|
+
type: 'cron' | 'interval' | 'timeout' | 'task'
|
|
37
|
+
cron?: string
|
|
38
|
+
interval?: number
|
|
39
|
+
delay?: number
|
|
40
|
+
queue?: string
|
|
41
|
+
options: TaskOptionsInterface
|
|
42
|
+
}
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## `compileScheduler`
|
|
46
|
+
|
|
47
|
+
Compile une classe décorée en job enregistré dans le `SchedulerManager`.
|
|
48
|
+
|
|
49
|
+
```typescript
|
|
50
|
+
compileScheduler(SchedulerClass)
|
|
51
|
+
// 1. Lit les métadonnées (getSchedulerMetadata)
|
|
52
|
+
// 2. Ré-enregistre dans le registre (registerScheduler)
|
|
53
|
+
// 3. Résout l'instance via Injection.resolve(SchedulerClass)
|
|
54
|
+
// 4. SchedulerManager.register(name, instance, metadata.jobs)
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
Appelé par `ControllerBuilder.build()` pour les artifacts `*.scheduler.ts`, et par `Artifacts.reloadScheduler()` en HMR.
|
|
58
|
+
|
|
59
|
+
## `SchedulerManager`
|
|
60
|
+
|
|
61
|
+
Singleton (`SchedulerManager.current`) implémentant `SchedulerControlInterface`.
|
|
62
|
+
|
|
63
|
+
### Cycle de vie
|
|
64
|
+
|
|
65
|
+
- `register(name, instance, jobs)` — crée les `JobRuntime`, les ajoute au `_jobs`
|
|
66
|
+
- `start()` — connecte les backends, arme les timers, lance les workers
|
|
67
|
+
- `stop()` — coupe les timers, attend les workers, déconnecte les backends
|
|
68
|
+
|
|
69
|
+
Le démarrage est piloté par `RaitonThread.run()` (après le scan des artifacts) et l'arrêt par `RaitonThread.stop()`.
|
|
70
|
+
|
|
71
|
+
### Planification
|
|
72
|
+
|
|
73
|
+
- **cron** : `armCron()` calcule le prochain run via `parseCron(...).next()` puis pose un `setTimeout` auto-réarmant (pas de dérive)
|
|
74
|
+
- **intervalle** : `armInterval()` pose un `setTimeout` auto-réarmant de `interval` ms
|
|
75
|
+
- **timeout** : `armTimeout()` pose un `setTimeout` unique
|
|
76
|
+
|
|
77
|
+
### Exécution
|
|
78
|
+
|
|
79
|
+
- `dispatchNow()` applique la garde `singleton` (et `catchUp`)
|
|
80
|
+
- `runOnce()` gère compteurs, `retries`, `retryDelay` et `timeout`
|
|
81
|
+
- `consume()` boucle `backend.dequeue()` pour les tâches `consume: true`, avec `concurrency`
|
|
82
|
+
|
|
83
|
+
### Contrôle
|
|
84
|
+
|
|
85
|
+
`dispatch()` / `trigger()` (in-process), `enqueue()` (file), `pause()`, `resume()`, `cancel()`, `getJobs()`, `getJob()`, `has()`.
|
|
86
|
+
|
|
87
|
+
## Backends de file
|
|
88
|
+
|
|
89
|
+
`QueueBackendInterface` est le contrat d'un backend :
|
|
90
|
+
|
|
91
|
+
```typescript
|
|
92
|
+
interface QueueBackendInterface {
|
|
93
|
+
readonly name: string
|
|
94
|
+
connect(): Promise<void>
|
|
95
|
+
disconnect(): Promise<void>
|
|
96
|
+
enqueue(queue, message): Promise<string>
|
|
97
|
+
dequeue(queue, options?): Promise<QueueMessageInterface[]>
|
|
98
|
+
acknowledge(queue, receipt): Promise<void>
|
|
99
|
+
reject(queue, receipt, requeue?): Promise<void>
|
|
100
|
+
length(queue): Promise<number>
|
|
101
|
+
purge(queue): Promise<void>
|
|
102
|
+
}
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
- `MemoryQueueBackend` (défaut) — listes en mémoire, visibilité timeout, dequeue bloquant par polling
|
|
106
|
+
- `RedisQueueBackend` — `RPUSH` / `LMOVE` vers une liste `:processing` / `LREM`
|
|
107
|
+
- `RabbitMqQueueBackend` — `sendToQueue` / `get` + ack/nack manuel
|
|
108
|
+
|
|
109
|
+
`registerBackend(name, backend)` et `mapQueue(queue, backendName)` permettent d'aiguiller chaque file vers un backend. Les paquets `redis` et `amqplib` sont importés paresseusement (`@ts-ignore`) et ne sont requis que si le backend correspondant est utilisé.
|
|
110
|
+
|
|
111
|
+
---
|
|
112
|
+
|
|
113
|
+
[← injection.md](injection.md) | [↑ core/](README.md) | [plugin-scope.md →](plugin-scope.md)
|
|
@@ -14,6 +14,7 @@ Le dossier `source/framework/` regroupe les briques publiques du framework: déc
|
|
|
14
14
|
| [env.md](env.md) | Variables d’environnement |
|
|
15
15
|
| [base-classes.md](base-classes.md) | DTO, ViewModel, services, repositories, bag de paramètres |
|
|
16
16
|
| [decorators/](decorators/README.md) | Décorateurs du framework |
|
|
17
|
+
| [cache/](cache/README.md) | ETag et cache de routes |
|
|
17
18
|
| [plugins/](plugins/README.md) | Body parser, OpenAPI, sécurité |
|
|
18
19
|
|
|
19
20
|
## Ce que fournit ce module
|
|
@@ -38,6 +39,7 @@ framework/
|
|
|
38
39
|
├── parameter-bag.ts
|
|
39
40
|
├── artifacts.ts
|
|
40
41
|
├── env.ts
|
|
42
|
+
├── cache/
|
|
41
43
|
├── decorators/
|
|
42
44
|
├── plugins/
|
|
43
45
|
├── runtime/
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
# Module Cache & ETag
|
|
2
|
+
|
|
3
|
+
**Fichiers :** `source/framework/cache/`
|
|
4
|
+
**Import :** `import { Cache, etag, Etagger, CacheManager } from "raiton/framework"`
|
|
5
|
+
|
|
6
|
+
> **Navigation :** [← framework/](../README.md) | [↑ framework/](../README.md) | [etag.md →](etag.md)
|
|
7
|
+
|
|
8
|
+
Le module `cache/` regroupe deux mécanismes complémentaires de performance HTTP :
|
|
9
|
+
les **ETag** (validation) et le **cache de routes** (expiration).
|
|
10
|
+
|
|
11
|
+
## Table des matières
|
|
12
|
+
|
|
13
|
+
| Fichier | Description |
|
|
14
|
+
|---------|-------------|
|
|
15
|
+
| [etag.md](etag.md) | Gestion complète et personnalisable des ETag |
|
|
16
|
+
| [cache.md](cache.md) | Cache de réponses sur les routes via décorateur |
|
|
17
|
+
|
|
18
|
+
## Vue d'ensemble des exports
|
|
19
|
+
|
|
20
|
+
```text
|
|
21
|
+
cache/
|
|
22
|
+
├── index.ts → Réexporte le module
|
|
23
|
+
├── types.ts → Options ETag & Cache
|
|
24
|
+
├── etag.ts → Etagger (manager) + etag() (middleware)
|
|
25
|
+
├── cache.ts → MemoryCacheStore + CacheManager + createCacheMiddleware
|
|
26
|
+
└── cache.decorator.ts → @Cache (décorateur de route)
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## Les deux stratégies
|
|
30
|
+
|
|
31
|
+
| Mécanisme | Objectif | Déclencheur | Coût du handler |
|
|
32
|
+
|-----------|----------|-------------|-----------------|
|
|
33
|
+
| `etag()` / `Etagger` | Validation (`304 Not Modified`) | En-tête `If-None-Match` | Exécuté |
|
|
34
|
+
| `@Cache()` | Expiration (« fresh », pas d'exécution) | TTL | Évité |
|
|
35
|
+
|
|
36
|
+
Les deux mécanismes s'empilent : un `@Cache()` réduit le travail du handler,
|
|
37
|
+
tandis qu'un `etag()` en amont (global) permet au client de valider sa copie.
|
|
38
|
+
|
|
39
|
+
## Activation
|
|
40
|
+
|
|
41
|
+
L'ETag et le cache **ne sont pas activés par défaut** — contrairement aux
|
|
42
|
+
en-têtes de sécurité — afin de ne pas modifier le comportement des applications
|
|
43
|
+
existantes. Le développeur les branche explicitement.
|
|
44
|
+
|
|
45
|
+
```typescript
|
|
46
|
+
import { etag } from "raiton/framework"
|
|
47
|
+
|
|
48
|
+
const app = new Application({ port: 3000 })
|
|
49
|
+
|
|
50
|
+
app.use(etag({ weak: false, cacheControl: { maxAge: 60 } }))
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
---
|
|
54
|
+
|
|
55
|
+
[← framework/](../README.md) | [↑ framework/](../README.md) | [etag.md →](etag.md)
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
# Cache de routes — `@Cache()`, `CacheManager`
|
|
2
|
+
|
|
3
|
+
**Fichiers :** `source/framework/cache/cache.ts`, `source/framework/cache/cache.decorator.ts`
|
|
4
|
+
**Import :** `import { Cache, CacheManager, MemoryCacheStore } from "raiton/framework"`
|
|
5
|
+
|
|
6
|
+
> **Navigation :** [← etag.md](etag.md) | [↑ README](README.md) | [framework/](../README.md)
|
|
7
|
+
|
|
8
|
+
Le cache de routes évite complètement d'exécuter le handler tant que la réponse
|
|
9
|
+
est **fraîche** (dans la fenêtre de TTL). C'est une stratégie d'**expiration**,
|
|
10
|
+
à distinguer de la **validation** apportée par les ETag.
|
|
11
|
+
|
|
12
|
+
## `@Cache(options)` — décorateur de route
|
|
13
|
+
|
|
14
|
+
```typescript
|
|
15
|
+
import { Controllable, Get, Cache } from "raiton/framework"
|
|
16
|
+
|
|
17
|
+
@Controllable("/products")
|
|
18
|
+
export class ProductController {
|
|
19
|
+
@Get("/featured")
|
|
20
|
+
@Cache({ ttl: 60_000 })
|
|
21
|
+
featured() {
|
|
22
|
+
return this.products.findFeatured()
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
Le décorateur enregistre un middleware sur la route. Tant que l'entrée est
|
|
28
|
+
fraîche, la réponse est servie depuis le store **sans** appeler le handler.
|
|
29
|
+
À expiration, le handler s'exécute à nouveau et la réponse est stockée.
|
|
30
|
+
|
|
31
|
+
## `CacheOptions`
|
|
32
|
+
|
|
33
|
+
| Option | Type | défaut | Description |
|
|
34
|
+
|--------|------|--------|-------------|
|
|
35
|
+
| `ttl` | `number` | `60_000` | durée de validité (ms) |
|
|
36
|
+
| `enabled` | `boolean` | `true` | active le cache de la route |
|
|
37
|
+
| `maxEntries` | `number` | `1000` | taille max du store en mémoire |
|
|
38
|
+
| `key` | `(context, route?) => string` | méthode + pathname + params | clé personnalisée |
|
|
39
|
+
| `vary` | `string[]` | `[]` | en-têtes participant à la clé (segmentation) |
|
|
40
|
+
| `methods` | `string[]` | `['GET', 'HEAD']` | méthodes HTTP mises en cache |
|
|
41
|
+
|
|
42
|
+
### Segmenter par en-tête
|
|
43
|
+
|
|
44
|
+
```typescript
|
|
45
|
+
@Cache({ ttl: 30_000, vary: ["accept-language"] })
|
|
46
|
+
list() { /* ... */ }
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
Deux langues différentes produiront deux entrées de cache distinctes.
|
|
50
|
+
|
|
51
|
+
## `CacheManager` — configuration globale
|
|
52
|
+
|
|
53
|
+
Comme les autres managers du framework, `CacheManager` configure le comportement
|
|
54
|
+
par défaut et le store partagé.
|
|
55
|
+
|
|
56
|
+
```typescript
|
|
57
|
+
CacheManager.configure({ ttl: 30_000, maxEntries: 5000 })
|
|
58
|
+
CacheManager.setStore(redisStore) // store alternatif, p.ex. Redis
|
|
59
|
+
CacheManager.clear() // vide le cache
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
## `MemoryCacheStore` & `CacheStore`
|
|
63
|
+
|
|
64
|
+
Le store par défaut est un `MemoryCacheStore` borné (TTL + taille max). Un store
|
|
65
|
+
alternatif doit implémenter l'interface `CacheStore` :
|
|
66
|
+
|
|
67
|
+
```typescript
|
|
68
|
+
interface CacheStore {
|
|
69
|
+
get<T>(key: string): CacheEntry<T> | undefined
|
|
70
|
+
set<T>(key: string, entry: CacheEntry<T>, ttl?: number): void
|
|
71
|
+
has(key: string): boolean
|
|
72
|
+
delete(key: string): boolean
|
|
73
|
+
clear(): void
|
|
74
|
+
size(): number
|
|
75
|
+
}
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
## Combinaison avec les ETag
|
|
79
|
+
|
|
80
|
+
Le middleware `etag()` (global) s'exécute avant le middleware `@Cache()` (route).
|
|
81
|
+
Sur une réponse servie depuis le cache, l'ETag est calculé sur le corps mis en
|
|
82
|
+
cache : le client peut donc **valider** une réponse déjà **fraîche**.
|
|
83
|
+
|
|
84
|
+
---
|
|
85
|
+
|
|
86
|
+
[← etag.md](etag.md) | [↑ README](README.md) | [framework/](../README.md)
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
# ETag — `Etagger`, `etag()`
|
|
2
|
+
|
|
3
|
+
**Fichiers :** `source/framework/cache/etag.ts`
|
|
4
|
+
**Import :** `import { Etagger, etag, defaultEtagger } from "raiton/framework"`
|
|
5
|
+
|
|
6
|
+
> **Navigation :** [← README](README.md) | [↑ README](README.md) | [cache.md →](cache.md)
|
|
7
|
+
|
|
8
|
+
Système **complet et personnalisable** de gestion des ETag. Il permet de :
|
|
9
|
+
|
|
10
|
+
- calculer un ETag stable depuis le corps sérialisé de la réponse (hachage FNV-1a par défaut) ;
|
|
11
|
+
- répondre `304 Not Modified` quand le client déclare posséder la représentation (`If-None-Match`) ;
|
|
12
|
+
- choisir un ETag **fort** ou **faible** (`W/"..."`) ;
|
|
13
|
+
- remplacer l'algorithme de hachage par un algorithme **personnalisé** ;
|
|
14
|
+
- piloter l'en-tête `Cache-Control` automatiquement ;
|
|
15
|
+
- exclure certaines requêtes/réponses du calcul.
|
|
16
|
+
|
|
17
|
+
## `etag(options?)` — middleware prêt à l'emploi
|
|
18
|
+
|
|
19
|
+
```typescript
|
|
20
|
+
app.use(etag({
|
|
21
|
+
weak: false,
|
|
22
|
+
cacheControl: { maxAge: 60 },
|
|
23
|
+
}))
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
Le middleware intercepte `context.reply.send`, calcule l'ETag du corps,
|
|
27
|
+
pose l'en-tête `ETag`, applique `Cache-Control` et, si `If-None-Match` matche,
|
|
28
|
+
répond `304` sans corps.
|
|
29
|
+
|
|
30
|
+
## `ETagOptions`
|
|
31
|
+
|
|
32
|
+
| Option | Type | défaut | Description |
|
|
33
|
+
|--------|------|--------|-------------|
|
|
34
|
+
| `enabled` | `boolean` | `true` | active la génération |
|
|
35
|
+
| `weak` | `boolean` | `false` | ETag faible (`W/"..."`) |
|
|
36
|
+
| `algorithm` | `(body: string) => string \| Promise<string>` | FNV-1a | hasher personnalisé |
|
|
37
|
+
| `cacheControl` | `boolean \| { ... }` | `false` | en-têtes `Cache-Control` automatiques |
|
|
38
|
+
| `headerName` | `string` | `'ETag'` | nom de l'en-tête de réponse |
|
|
39
|
+
| `skip` | `(context, body) => boolean` | – | exclure une requête/réponse |
|
|
40
|
+
|
|
41
|
+
### `cacheControl`
|
|
42
|
+
|
|
43
|
+
- `true` → `no-cache`
|
|
44
|
+
- `{ public: true, maxAge: 60 }` → `public, max-age=60`
|
|
45
|
+
- `{ noStore: true }` → `private, no-store`
|
|
46
|
+
|
|
47
|
+
## `Etagger` — utilisation programmatique
|
|
48
|
+
|
|
49
|
+
```typescript
|
|
50
|
+
const etagger = new Etagger({ weak: true, cacheControl: { maxAge: 300 } })
|
|
51
|
+
|
|
52
|
+
const { tag, body, notModified } = await etagger.evaluate(context, payload)
|
|
53
|
+
if (notModified) {
|
|
54
|
+
context.reply.status(304)
|
|
55
|
+
context.reply.send(null)
|
|
56
|
+
} else {
|
|
57
|
+
context.reply.send(body)
|
|
58
|
+
}
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
### API de `Etagger`
|
|
62
|
+
|
|
63
|
+
| Méthode | Description |
|
|
64
|
+
|---------|-------------|
|
|
65
|
+
| `generate(body)` | calcule la valeur d'ETag (`"..."` ou `W/"..."`) |
|
|
66
|
+
| `matches(clientTags, tag)` | teste `If-None-Match` (`*` accepté) |
|
|
67
|
+
| `evaluate(context, body)` | applique en-têtes + décision 304, renvoie `{ tag, body, notModified }` |
|
|
68
|
+
| `static apply(context, body, options?)` | raccourci un seul appel |
|
|
69
|
+
|
|
70
|
+
## Exemple — hachage personnalisé
|
|
71
|
+
|
|
72
|
+
```typescript
|
|
73
|
+
app.use(etag({ algorithm: async (body) => {
|
|
74
|
+
const bytes = new TextEncoder().encode(body)
|
|
75
|
+
const digest = await crypto.subtle.digest("SHA-1", bytes)
|
|
76
|
+
return [...new Uint8Array(digest)].map(b => b.toString(16).padStart(2, "0")).join("")
|
|
77
|
+
}}))
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
---
|
|
81
|
+
|
|
82
|
+
[← README](README.md) | [↑ README](README.md) | [cache.md →](cache.md)
|
|
@@ -15,6 +15,7 @@
|
|
|
15
15
|
| [middleware.md](middleware.md) | `@Middleware` |
|
|
16
16
|
| [socket.md](socket.md) | `@Socket`, `@OnSocketConnect`, `@OnSocketMessage`... |
|
|
17
17
|
| [openapi.md](openapi.md) | `@ApiTags`, `@ApiOperation`, `@ApiResponse`... |
|
|
18
|
+
| [scheduler.md](scheduler.md) | `@Scheduler`, `@Cron`, `@Every`, `@Timeout`, `@Task` |
|
|
18
19
|
|
|
19
20
|
Les décorateurs utilisent `reflect-metadata` et `experimentalDecorators: true` + `emitDecoratorMetadata: true` dans `tsconfig.json`.
|
|
20
21
|
|
|
@@ -28,6 +29,7 @@ Les décorateurs utilisent `reflect-metadata` et `experimentalDecorators: true`
|
|
|
28
29
|
| `@Injectable` | Classe | Enregistrement DI | [Voir](injection.md) |
|
|
29
30
|
| `@Inject` | Constructeur/propriété | Injection de dépendance | [Voir](injection.md) |
|
|
30
31
|
| `@Middleware` | Classe/méthode | Middleware sur contrôleur/route | [Voir](middleware.md) |
|
|
32
|
+
| `@Cache` | Méthode | Cache de réponse de route | [Voir](../cache/cache.md) |
|
|
31
33
|
| `@Socket` | Classe | Handler socket | [Voir](socket.md) |
|
|
32
34
|
| `@OnSocket*` | Méthode | Événement socket | [Voir](socket.md) |
|
|
33
35
|
| `@ApiTags` | Classe/méthode | Tag OpenAPI | [Voir](openapi.md) |
|
|
@@ -44,6 +46,9 @@ Les décorateurs utilisent `reflect-metadata` et `experimentalDecorators: true`
|
|
|
44
46
|
| `@ApiNotFoundResponse` | Méthode | Raccourci 404 | [Voir](openapi.md) |
|
|
45
47
|
| `@ApiInternalServerErrorResponse` | Méthode | Raccourci 500 | [Voir](openapi.md) |
|
|
46
48
|
| `@RouteInteraction` | Méthode | Description d'interaction | [Voir](openapi.md) |
|
|
49
|
+
| `@Scheduler` | Classe | Conteneur de jobs planifiés | [Voir](scheduler.md) |
|
|
50
|
+
| `@Cron`/`@Every`/`@Timeout` | Méthode | Planification de job | [Voir](scheduler.md) |
|
|
51
|
+
| `@Task` | Méthode | Tâche nommée (dispatch/enqueue) | [Voir](scheduler.md) |
|
|
47
52
|
|
|
48
53
|
## Règles générales
|
|
49
54
|
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
# Décorateurs Scheduler
|
|
2
|
+
|
|
3
|
+
**Fichier :** `source/framework/decorators/scheduler.decorator.ts`
|
|
4
|
+
**Import :** `import { Scheduler, Cron, Every, Timeout, Task } from "raiton/framework"`
|
|
5
|
+
|
|
6
|
+
> **Navigation :** [← openapi.md](openapi.md) | [↑ decorators/](README.md)
|
|
7
|
+
|
|
8
|
+
Les décorateurs scheduler déclarent des jobs planifiés et des tâches nommées, compilés par `compileScheduler` puis gérés par le `SchedulerManager`.
|
|
9
|
+
|
|
10
|
+
## `@Scheduler(options?)`
|
|
11
|
+
|
|
12
|
+
Marque une classe comme conteneur de jobs. Enregistre la classe en DI (`Injectable` transient) et dans le registre des schedulers.
|
|
13
|
+
|
|
14
|
+
```typescript
|
|
15
|
+
@Scheduler({ name: "maintenance", queue: "default", timezone: "UTC" })
|
|
16
|
+
export class MaintenanceScheduler { /* ... */ }
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
**Fonctionnement interne :**
|
|
20
|
+
1. `Injectable(LifetimeEnum.TRANSIENT, name)` → enregistre dans le DI
|
|
21
|
+
2. `getSchedulerMetadata(target.prototype)` → métadonnées de classe
|
|
22
|
+
3. `registerScheduler(target)` → registre des schedulers
|
|
23
|
+
|
|
24
|
+
## `@Cron(expression, options?)`
|
|
25
|
+
|
|
26
|
+
Planifie une méthode selon une expression cron 5 ou 6 champs (ou une macro).
|
|
27
|
+
|
|
28
|
+
```typescript
|
|
29
|
+
@Cron("0 3 * * *", { name: "daily-cleanup", timezone: "Europe/Paris" })
|
|
30
|
+
async cleanup(ctx: TaskContextInterface) { /* ... */ }
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## `@Every(interval, options?)` (alias `@Interval`)
|
|
34
|
+
|
|
35
|
+
Planifie une méthode à intervalle fixe. `interval` est un nombre de millisecondes ou une durée `"5s"`, `"1m30s"`, `"2h"`. L'option `immediate` exécute une première fois au démarrage.
|
|
36
|
+
|
|
37
|
+
```typescript
|
|
38
|
+
@Every("5m", { name: "heartbeat", immediate: true })
|
|
39
|
+
async heartbeat() { /* ... */ }
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
## `@Timeout(delay, options?)`
|
|
43
|
+
|
|
44
|
+
Planifie une exécution unique après `delay`.
|
|
45
|
+
|
|
46
|
+
```typescript
|
|
47
|
+
@Timeout("30s", { name: "warmup" })
|
|
48
|
+
async warmup() { /* ... */ }
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
## `@Task(name, options?)`
|
|
52
|
+
|
|
53
|
+
Déclare une tâche nommée : dispatchable en process, enqueueable sur une file et, si `consume: true`, consommée en tant que worker.
|
|
54
|
+
|
|
55
|
+
```typescript
|
|
56
|
+
@Task("email:welcome", { queue: "emails", consume: true, retries: 3, concurrency: 5 })
|
|
57
|
+
async welcome(ctx: TaskContextInterface<{ to: string }>) { /* ... */ }
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
## Métadonnées
|
|
61
|
+
|
|
62
|
+
Les métadonnées sont stockées sur le prototype via `METADATA_KEYS.SCHEDULER` (symbole Reflect) :
|
|
63
|
+
|
|
64
|
+
```typescript
|
|
65
|
+
interface SchedulerMetaInterface {
|
|
66
|
+
name: string
|
|
67
|
+
queue: string
|
|
68
|
+
timezone?: string
|
|
69
|
+
jobs: JobMetaInterface[]
|
|
70
|
+
}
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
Chaque job (`JobMetaInterface`) porte `propertyKey`, `name`, `type` (`cron` | `interval` | `timeout` | `task`), le champ de planification (`cron` | `interval` | `delay`), `queue` et `options`.
|
|
74
|
+
|
|
75
|
+
## Notes
|
|
76
|
+
|
|
77
|
+
- Les artifacts `*.scheduler.ts` (ainsi que `*.task.ts`, `*.cron.ts`, `*.job.ts`) sont scannés au boot et rechargés en HMR (`hmr:scheduler`).
|
|
78
|
+
- Le handler reçoit un `TaskContextInterface` (`name`, `queue`, `payload`, `trigger`, `attempt`, `scheduler`…).
|
|
79
|
+
- Les options communes (`singleton`, `concurrency`, `retries`, `retryDelay`, `timeout`, `catchUp`, `enabled`, `consume`, `priority`, `data`) sont résolues par `SchedulerManager.resolveOptions()`.
|
|
80
|
+
|
|
81
|
+
---
|
|
82
|
+
|
|
83
|
+
[← openapi.md](openapi.md) | [↑ decorators/](README.md)
|
|
@@ -14,6 +14,7 @@ class Runtime {
|
|
|
14
14
|
constructor(public readonly type: RuntimeType)
|
|
15
15
|
|
|
16
16
|
get isNode(): boolean
|
|
17
|
+
get isDeno(): boolean
|
|
17
18
|
get isBun(): boolean
|
|
18
19
|
|
|
19
20
|
adapter(): RuntimeAdapterInterface
|
|
@@ -25,11 +26,15 @@ class Runtime {
|
|
|
25
26
|
|
|
26
27
|
```typescript
|
|
27
28
|
// Détection automatique (dans RaitonThread.setup) :
|
|
28
|
-
|
|
29
|
+
// Bun si présent, sinon Deno si présent, sinon Node
|
|
30
|
+
const defaultRuntime = isBunUsed
|
|
31
|
+
? RuntimeType.Bun
|
|
32
|
+
: (isDenoUsed ? RuntimeType.Deno : RuntimeType.Node)
|
|
29
33
|
|
|
30
34
|
// Manuel :
|
|
31
35
|
const runtime = new Runtime(RuntimeType.Bun)
|
|
32
36
|
const runtime = new Runtime(RuntimeType.Node)
|
|
37
|
+
const runtime = new Runtime(RuntimeType.Deno)
|
|
33
38
|
```
|
|
34
39
|
|
|
35
40
|
### Création du serveur
|
|
@@ -77,11 +82,16 @@ node/
|
|
|
77
82
|
- `NodeRequest` : wrapper autour de `IncomingMessage` avec interface `RequestInterface`
|
|
78
83
|
- `NodeReply` : wrapper autour de `ServerResponse` avec interface `ReplyInterface`
|
|
79
84
|
|
|
80
|
-
### `denoRuntime` (
|
|
85
|
+
### `denoRuntime` (Deno.serve)
|
|
81
86
|
|
|
82
87
|
**Fichier :** `source/framework/runtime/deno/server.ts`
|
|
83
88
|
|
|
84
|
-
|
|
89
|
+
Utilise `Deno.serve()` et `Deno.upgradeWebSocket()` pour les sockets.
|
|
90
|
+
|
|
91
|
+
- **Serveur** : `Deno.serve({ port, hostname })`
|
|
92
|
+
- **Requêtes** : Native `Request` / `Response`
|
|
93
|
+
- **Sockets** : `Deno.upgradeWebSocket()`
|
|
94
|
+
- **Recommandé** pour une production stable et rapide sur Deno
|
|
85
95
|
|
|
86
96
|
## Interface `RuntimeAdapterInterface`
|
|
87
97
|
|
|
@@ -19,11 +19,13 @@ Ce dossier décrit comment utiliser Raiton pour créer un serveur d'API.
|
|
|
19
19
|
| [responses.md](responses.md) | Structurer les réponses et les erreurs |
|
|
20
20
|
| [body-parser.md](body-parser.md) | Lire JSON, formulaires et fichiers |
|
|
21
21
|
| [security.md](security.md) | Sécurité HTTP, CORS, rate-limit, body-limit |
|
|
22
|
+
| [cache.md](cache.md) | ETag (validation) et cache de routes (`@Cache`) |
|
|
22
23
|
| [health-check.md](health-check.md) | Endpoint de contrôle et vérifications de santé |
|
|
23
24
|
| [https.md](https.md) | HTTPS, certificats et environnements |
|
|
24
25
|
| [openapi/README.md](openapi/README.md) | Générer et documenter l'API |
|
|
25
26
|
| [sockets.md](sockets.md) | Déclarer et gérer les sockets |
|
|
26
27
|
| [mcp.md](mcp.md) | Exposer outils, prompts et ressources via le protocole MCP |
|
|
28
|
+
| [scheduler.md](scheduler.md) | Planifier des tâches (cron, intervalle) et gérer des files d’attente |
|
|
27
29
|
| [tutorial.md](tutorial.md) | Construire une mini API complète de bout en bout |
|
|
28
30
|
| [plugins.md](plugins.md) | Vue d'ensemble des plugins intégrés |
|
|
29
31
|
| [types.md](types.md) | Comprendre les types utiles au développeur |
|
|
@@ -49,11 +51,12 @@ Ce dossier décrit comment utiliser Raiton pour créer un serveur d'API.
|
|
|
49
51
|
15. lire [openapi/README.md](openapi/README.md)
|
|
50
52
|
16. lire [sockets.md](sockets.md)
|
|
51
53
|
17. lire [mcp.md](mcp.md)
|
|
52
|
-
18. lire [
|
|
53
|
-
19. lire [
|
|
54
|
-
20. lire [
|
|
55
|
-
21. lire [
|
|
56
|
-
22. lire [
|
|
54
|
+
18. lire [scheduler.md](scheduler.md)
|
|
55
|
+
19. lire [health-check.md](health-check.md)
|
|
56
|
+
20. lire [types.md](types.md)
|
|
57
|
+
21. lire [tutorial.md](tutorial.md)
|
|
58
|
+
22. lire [plugins.md](plugins.md)
|
|
59
|
+
23. lire [cli.md](cli.md)
|
|
57
60
|
|
|
58
61
|
## Ce que couvre ce guide
|
|
59
62
|
|
|
@@ -65,6 +68,7 @@ Ce dossier décrit comment utiliser Raiton pour créer un serveur d'API.
|
|
|
65
68
|
- les plugins de sécurité et de documentation
|
|
66
69
|
- le protocole HTTPS et la gestion des certificats
|
|
67
70
|
- les endpoints et vérifications de santé (health-check)
|
|
71
|
+
- la planification de tâches et les files d’attente (scheduler)
|
|
68
72
|
- le cycle local `dev` / `build` / `start`
|
|
69
73
|
|
|
70
74
|
> **Navigation :** [← docs/](../README.md) | [← contributeurs](../contributors/README.md)
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
# Cache & ETag
|
|
2
|
+
|
|
3
|
+
> **Navigation :** [← security](security.md) | [health-check →](health-check.md)
|
|
4
|
+
|
|
5
|
+
Raiton fournit deux mécanismes de performance HTTP complémentaires :
|
|
6
|
+
|
|
7
|
+
- **ETag** (validation) : le client valide sa copie et reçoit `304 Not Modified`.
|
|
8
|
+
- **Cache de routes** (expiration) : le handler n'est pas ré-exécuté tant que
|
|
9
|
+
la réponse est fraîche.
|
|
10
|
+
|
|
11
|
+
## ETag — validation avec `etag()`
|
|
12
|
+
|
|
13
|
+
Branchez le middleware `etag()` sur votre application. Il calcule un ETag de
|
|
14
|
+
chaque réponse, pose l'en-tête `ETag` et répond `304` si le client le possède
|
|
15
|
+
déjà (`If-None-Match`).
|
|
16
|
+
|
|
17
|
+
```typescript
|
|
18
|
+
import { Application } from "raiton/core"
|
|
19
|
+
import { etag } from "raiton/framework"
|
|
20
|
+
|
|
21
|
+
const app = new Application({ port: 3000 })
|
|
22
|
+
|
|
23
|
+
app.use(etag({
|
|
24
|
+
weak: false,
|
|
25
|
+
cacheControl: { maxAge: 60, public: true },
|
|
26
|
+
}))
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
### Personnaliser
|
|
30
|
+
|
|
31
|
+
`etag()` accepte un algorithme de hachage, le marqueur faible, le nom d'en-tête
|
|
32
|
+
et un prédicat d'exclusion :
|
|
33
|
+
|
|
34
|
+
```typescript
|
|
35
|
+
app.use(etag({
|
|
36
|
+
weak: true,
|
|
37
|
+
algorithm: async (body) =>
|
|
38
|
+
crypto.createHash("sha1").update(body).digest("hex"),
|
|
39
|
+
skip: (ctx) => ctx.req.method !== "GET",
|
|
40
|
+
}))
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## Cache de routes — `@Cache()`
|
|
44
|
+
|
|
45
|
+
Mettez en cache la réponse d'une route pendant un `ttl` sans ré-exécuter le
|
|
46
|
+
handler :
|
|
47
|
+
|
|
48
|
+
```typescript
|
|
49
|
+
import { Controllable, Get, Cache } from "raiton/framework"
|
|
50
|
+
|
|
51
|
+
@Controllable("/products")
|
|
52
|
+
export class ProductController {
|
|
53
|
+
@Get("/featured")
|
|
54
|
+
@Cache({ ttl: 60_000, vary: ["accept-language"] })
|
|
55
|
+
featured() {
|
|
56
|
+
return this.catalogue.featured()
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
Configurez globalement les défauts et le store :
|
|
62
|
+
|
|
63
|
+
```typescript
|
|
64
|
+
import { CacheManager } from "raiton/framework"
|
|
65
|
+
|
|
66
|
+
CacheManager.configure({ ttl: 30_000, maxEntries: 5000 })
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
## Les deux ensemble
|
|
70
|
+
|
|
71
|
+
Un `@Cache()` global (ou par route) réduit le travail du handler, et un `etag()`
|
|
72
|
+
en amont permet au client de valider sa copie — y compris les réponses servies
|
|
73
|
+
depuis le cache.
|
|
74
|
+
|
|
75
|
+
> Pour le détail de toutes les options : voir le [décorateur `@Cache`](decorators/cache.md).
|
|
76
|
+
|
|
77
|
+
---
|
|
78
|
+
|
|
79
|
+
[← security](security.md) | [health-check →](health-check.md)
|