[Nom du Worker]
Type de ressource : Cloudflare Worker (Microservice FaaS / Middleware)
Fichier source :services/[worker-name]/src/index.ts
Statut : [Actif / Déprécié / En développement]
Slice : [Numéro de la tranche verticale]
Version : 1.0
1. Vue dâensemble (DOC)
Section titled â1. Vue dâensemble (DOC)â1.1. Description
Section titled â1.1. Descriptionâ[Description concise du rĂŽle de ce Worker dans lâarchitecture L3]
1.2. Objectif
Section titled â1.2. Objectifâ- [Objectif principal : ex. intercepter toutes les requĂȘtes HTTP pour appliquer les politiques de sĂ©curitĂ©]
- [BĂ©nĂ©fice pour lâarchitecture : ex. dĂ©couplage de la logique Edge du framework Astro]
1.3. Caractéristiques
Section titled â1.3. CaractĂ©ristiquesâ| PropriĂ©tĂ© | Valeur |
|---|---|
| Type | [Middleware HTTP / Microservice métier / Cron Trigger / Pages Function] |
| Trigger | [HTTP fetch / Cron / Queue consumer / Les deux] |
| Route publique | https://[worker-name].[subdomain].workers.dev ou sous-domaine dédié |
| Environnements | [Production / Staging / Preview] |
| Consommateurs | [Astro / Sanity / Navigateur / Autres Workers] |
2. Architecture et flux (DOC)
Section titled â2. Architecture et flux (DOC)â2.1. Diagramme de flux
Section titled â2.1. Diagramme de fluxâ[EntrĂ©e : Client / Astro / Sanity / Cron] â âŒ[Cloudflare Worker] â âą Validation des entrĂ©es â âą Logique mĂ©tier â âą Interaction avec les bindings (KV, R2, Queue) âŒ[Services tiers / Storage] â âą Sanity API / CRM / R2 / KV âŒ[RĂ©ponse / Action]2.2. Composants impliquĂ©s
Section titled â2.2. Composants impliquĂ©sâ| Composant | RĂŽle | Type |
|---|---|---|
| [Nom] | [Description] | [Binding / Service tiers / Client] |
3. Configuration (CODE)
Section titled â3. Configuration (CODE)â3.1. Variables dâenvironnement
Section titled â3.1. Variables dâenvironnementâ| Variable | Description | Obligatoire | Valeur par dĂ©faut |
|---|---|---|---|
| [Variable] | [Description] | [â /â] | [Valeur] |
3.2. Bindings Cloudflare
Section titled â3.2. Bindings Cloudflareâ| Binding | Type | Nom | Description |
|---|---|---|---|
| [Nom] | [KV / R2 / Queue / Service / Secret] | [Binding name] | [Description] |
3.3. Configuration Wrangler (wrangler.toml)
Section titled â3.3. Configuration Wrangler (wrangler.toml)âname = "[worker-name]"main = "src/index.ts"compatibility_date = "2026-09-01"
[vars]# Variables non-sensibles[VAR_NAME] = "value"
# Bindings[[kv_namespaces]]binding = "KV_BINDING"id = "your-kv-namespace-id"
[[r2_buckets]]binding = "R2_BUCKET"bucket_name = "bucket-name"
[[queues.producers]]queue = "queue-name"binding = "QUEUE_BINDING"
[triggers]crons = ["0 * * * *"] # Toutes les heures4. Implémentation (CODE)
Section titled â4. ImplĂ©mentation (CODE)â4.1. Handler HTTP fetch
Section titled â4.1. Handler HTTP fetchâexport interface Env { KV_BINDING: KVNamespace; R2_BUCKET: R2Bucket; QUEUE_BINDING: Queue; [SECRET_NAME]: string;}
export default { async fetch(request: Request, env: Env): Promise<Response> { const url = new URL(request.url);
// Logique métier if (url.pathname === '/api/endpoint') { return handleEndpoint(request, env); }
return new Response('Not Found', { status: 404 }); },};4.2. Handler Cron (si applicable)
Section titled â4.2. Handler Cron (si applicable)âexport default { async scheduled(event: ScheduledEvent, env: Env, ctx: ExecutionContext): Promise<void> { // Logique cron console.log('Cron triggered', { cron: event.cron }); },};4.3. Handler Queue (si applicable)
Section titled â4.3. Handler Queue (si applicable)âexport default { async queue(batch: MessageBatch<unknown>, env: Env): Promise<void> { for (const message of batch.messages) { await processMessage(message, env); message.ack(); } },};5. Contrats dâEntrĂ©e/Sortie (SPEC)
Section titled â5. Contrats dâEntrĂ©e/Sortie (SPEC)â5.1. Inputs (RequĂȘte HTTP)
Section titled â5.1. Inputs (RequĂȘte HTTP)â| ParamĂštre | Type | Source | Validation |
|---|---|---|---|
body |
object |
Request body | Validation Zod |
headers |
Headers |
Request headers | Validation des en-tĂȘtes |
5.2. Outputs (Réponse HTTP)
Section titled â5.2. Outputs (RĂ©ponse HTTP)â| Code HTTP | Condition | Description |
|---|---|---|
| 200 | SuccÚs | Opération réussie |
| 400 | Erreur client | RequĂȘte invalide |
| 500 | Erreur serveur | Erreur interne |
5.3. Side effects
Section titled â5.3. Side effectsâ| Effet | Description | FrĂ©quence |
|---|---|---|
| [Ăcriture KV] | [Description] | [Ă chaque appel / Conditionnel] |
6. Tests attendus (TEST)
Section titled â6. Tests attendus (TEST)â6.1. Tests unitaires
Section titled â6.1. Tests unitairesâ| ScĂ©nario | Description | RĂ©sultat attendu |
|---|---|---|
| Handler fetch | RequĂȘte HTTP valide | RĂ©ponse 200 |
| Validation des entrées | Données invalides | Erreur 400 |
6.2. Tests dâintĂ©gration
Section titled â6.2. Tests dâintĂ©grationâ| ScĂ©nario | DĂ©pendances | Description | RĂ©sultat attendu |
|---|---|---|---|
| Flux complet | Bindings + Services tiers | Opération end-to-end | SuccÚs |
6.3. Tests E2E
Section titled â6.3. Tests E2Eâ| ScĂ©nario | Parcours utilisateur | Description | RĂ©sultat attendu |
|---|---|---|---|
| DĂ©ploiement | Build â Deploy | DĂ©ploiement sur Cloudflare | Worker fonctionnel |
7. IntĂ©gration (INTĂG)
Section titled â7. IntĂ©gration (INTĂG)â7.1. IntĂ©gration avec les composants
Section titled â7.1. IntĂ©gration avec les composantsâ| Composant | Type | RĂŽle | MĂ©thode dâintĂ©gration |
|---|---|---|---|
| Astro | L2 | Consommateur | HTTP fetch |
| Sanity | L1 | Producteur | Webhook |
| Autres Workers | L3 | Communication | Service binding |
7.2. Points dâattention
Section titled â7.2. Points dâattentionâ- Les bindings doivent ĂȘtre configurĂ©s dans wrangler.toml
- Les secrets doivent ĂȘtre injectĂ©s via wrangler secret put
- Les handlers doivent ĂȘtre correctement exportĂ©s
7.3. Vérification de cohérence
Section titled â7.3. VĂ©rification de cohĂ©renceâ- Le Worker se dĂ©ploie correctement
- Les bindings sont accessibles
- Les tests unitaires passent
- Les tests E2E passent
8. Sécurité
Section titled â8. SĂ©curitĂ©â8.1. MĂ©canismes de sĂ©curitĂ©
Section titled â8.1. MĂ©canismes de sĂ©curitĂ©â- â Validation des entrĂ©es
- â Secrets injectĂ©s via wrangler secret put
- â HTTPS forcĂ©
8.2. RÚgles de sécurité
Section titled â8.2. RĂšgles de sĂ©curitĂ©â- â Ne jamais logger les secrets
- â Valider toutes les entrĂ©es
- â Utiliser les permissions minimales
9. Performance et limites
Section titled â9. Performance et limitesâ9.1. Limites Cloudflare
Section titled â9.1. Limites Cloudflareâ| Limite | Valeur | StratĂ©gie dâoptimisation |
|---|---|---|
| CPU time | 10 ms (free) / 50 ms (paid) | Optimiser le code |
| Memory | 128 MB | Ăviter les gros objets |
| Requests | 100 000 (free) / 10M (paid) | Cache Edge |
9.2. Optimisations
Section titled â9.2. Optimisationsâ- â Utiliser le cache Edge
- â Minimiser les allocations mĂ©moire
- â Utiliser des opĂ©rations asynchrones
10. Débogage et troubleshooting
Section titled â10. DĂ©bogage et troubleshootingâ10.1. ProblĂšmes courants
Section titled â10.1. ProblĂšmes courantsâ| SymptĂŽme | Cause probable | Solution |
|---|---|---|
| Worker échoue | Binding manquant | Vérifier wrangler.toml |
| Secret inaccessible | Secret non injecté | wrangler secret put |
| Timeout | CPU time dépassé | Optimiser le code |
10.2. Commandes de debug
Section titled â10.2. Commandes de debugâ# DĂ©ploiement localwrangler dev
# Déploiement productionwrangler deploy --env production
# Logs en temps réelwrangler tail11. Bonnes pratiques
Section titled â11. Bonnes pratiquesâ11.1. Architecture
Section titled â11.1. Architectureâ- â Handlers sĂ©parĂ©s par type (fetch, cron, queue)
- â Validation des entrĂ©es
- â Gestion des erreurs
11.2. Code
Section titled â11.2. Codeâ- â TypeScript strict
- â Logs structurĂ©s
- â Documentation des fonctions
11.3. Maintenance
Section titled â11.3. Maintenanceâ- â Mettre Ă jour les dĂ©pendances rĂ©guliĂšrement
- â Documenter les changements dans ce fichier
12. Ăvolutions et roadmap
Section titled â12. Ăvolutions et roadmapâ12.1. AmĂ©liorations planifiĂ©es
Section titled â12.1. AmĂ©liorations planifiĂ©esâ- [Ăvolution 1]
- [Ăvolution 2]
12.2. Limitations connues
Section titled â12.2. Limitations connuesâ- â [Limitation 1]
- â [Limitation 2]
13. Ressources liées
Section titled â13. Ressources liĂ©esâ- Documentation Cloudflare : Workers
- DAT L3 : §5 - Workers Cloudflare
14. Historique des modifications
Section titled â14. Historique des modificationsâ| Date | Version | Description | Auteur |
|---|---|---|---|
| YYYY-MM-DD | 1.0 | Création initiale | [Nom] |