|
| 1 | +# Architecture |
| 2 | + |
| 3 | +Documentation on the architecture of the worker (i.e. how it works, how it fits into Node.js' infrastructure, etc.). |
| 4 | + |
| 5 | +## Network Request Flow |
| 6 | + |
| 7 | +A high-level overview of how a request flows through Node.js' infrastructure: |
| 8 | + |
| 9 | +```mermaid |
| 10 | +flowchart LR |
| 11 | + request[Request] --> cloudflare(Cloudflare Routing Rules) |
| 12 | + cloudflare -- /dist/, /download/, /docs/, /api/, /metrics/ --> worker@{ shape: procs, label: "Release Worker"} |
| 13 | + cloudflare -- /... --> website(Website) |
| 14 | + worker -- Cache miss --> r2[(R2 bucket)] |
| 15 | + worker -- Error --> originServer(Origin Server) |
| 16 | + originServer |
| 17 | + website |
| 18 | + r2 |
| 19 | +``` |
| 20 | + |
| 21 | +## Worker Request Flow |
| 22 | + |
| 23 | +The Release Worker uses a middleware approach to routing requests. |
| 24 | + |
| 25 | +When an instance of the worker starts up, it registers a number of routes and their middlewares. |
| 26 | +It then builds a "chain" of middlewares to call in the same order they're given to handle the request. |
| 27 | + |
| 28 | +When a request hits the worker, the router gives it to the first middleware in the chain. |
| 29 | +That middleware can then either handle the request and return a response or pass it onto the next middleware. |
| 30 | +This goes on until the request is handled or we run out of middlewares to handle the request, upon which we throw an error. |
| 31 | + |
| 32 | +We currently have the following middlewares (in no particular order): |
| 33 | + |
| 34 | +- [CacheMiddleware](../src/middleware/cacheMiddleware.ts) - Caches responses to GET request. |
| 35 | +- [R2Middleware](../src/middleware/r2Middleware.ts) - Fetches resource from R2. |
| 36 | +- [OriginMiddleware](../src/middleware/originMiddleware.ts) - Fetches resource from the origin server. |
| 37 | + Used as a fallback if the R2 middleware fails. |
| 38 | +- [NotFoundMiddleware](../src/middleware/notFoundMiddleware.ts) - Handles not found requests. |
| 39 | +- [OptionsMiddleware](../src/middleware/optionsMiddleware.ts) - Handles OPTIONS requests. |
| 40 | +- [SubstituteMiddleware](../src/middleware/subtituteMiddleware.ts) - Handles requests that need URL substituing (i.e. `/dist/latest/` -> `/dist/<latest version>`) and then feeds them back into the router. |
| 41 | + |
| 42 | +### Diagram |
| 43 | + |
| 44 | +```mermaid |
| 45 | +flowchart TD |
| 46 | + request[Request] --> worker(Release Worker) |
| 47 | + worker --> routerHandle("Router.handle") |
| 48 | + routerHandle -- HTTP GET --> cacheMiddleware("Cache Middleware") |
| 49 | + routerHandle -- HTTP HEAD --> r2Middleware |
| 50 | + routerHandle -- HTTP OPTIONS --> optionsMiddleware("Options Middleware") |
| 51 | + routerHandle -- Request --> substituteMiddleware("Substitute Middleware") |
| 52 | + substituteMiddleware -- Substituted Request --> routerHandle |
| 53 | + cacheMiddleware -- Cache miss --> r2Middleware("R2 Middleware") |
| 54 | + r2Middleware -- Error --> originMiddleware("Origin Middleware") |
| 55 | +``` |
0 commit comments