How dbiesecke/lilredirector works
Project Type and Purpose
• This is an open-source Cloudflare Workers library (“lilredirector”) that you embed into your Worker script to handle HTTP redirects at the edge.
• Key goal: take a simple redirects.js array of path→URL mappings (with support for path parameters), run on every fetch event, and either issue a redirect or render a read-only UI (HTML/JSON/M3U/MSX) under a configurable base path.
File Structure
• Top‐level files:
• package.json / tsconfig.json: Node + TypeScript configuration.
• README.md: usage, config, authentication.
• src/ directory (core code):
• index.ts – exports the main redirector(event, redirects, config) function.
• router.ts – matches incoming request paths to redirect rules, extracts parameters.
• template-*.ts/.js – HTML/UI rendering for dashboard (template-index.ts, template-json.ts, template-m3u.js/ts, template-msx.ts).
• template.ts – shared templating logic.
• types.ts – TypeScript definitions for config, redirects, responses.
• CI / Deployment: .github/workflows/build.yml, .npmignore, .prettierrc, license files.
Extract Insights from README
• Entry point: import lilredirector & your redirects.js, attach to addEventListener('fetch').
• Configurable options: baseUrl, basicAuthentication, removeTrailingSlashes.
• Authentication: built-in Basic Auth or integrate Cloudflare Access.
• Default UI endpoint: /_redirects (serves built-in dashboard).
Main Components & Relationships
a. External Actors / Services
• Client (browser or HTTP client)
• (Optional) Cloudflare Access Zero-Trust authentication layer
b. Cloudflare Edge & Workers Runtime
• The serverless execution environment that receives HTTP requests.
c. Your User Worker Script
• addEventListener('fetch', event => respondWith(handler(event)))
• Imports redirector and redirects config.
d. lilredirector Module
• Core Handler (index.ts)
• Router (router.ts)
• UI Templates (HTML/JSON/M3U/MSX)
• Types & Config Logic
e. Redirect Rules Config
• Static array in redirects.js (or .ts) defining path patterns & targets.
f. Optional Auth Component
• Basic Auth middleware inside redirector
• Cloudflare Access sits in front of Worker (optional).
g. HTTP Responses
• 3xx Redirects
• 200 UI Page (HTML)
• 200 JSON/M3U/MSX output
• 4xx/5xx Errors
Data Flow / Interaction Steps
1. Client → HTTP Request → Cloudflare Edge
2. Edge → Worker Runtime → invokes your Worker Script handler(event)
3. handler calls redirector(event, redirects, config)
4. Inside redirector:
• If path under baseUrl → (optional auth) → serve UI via template modules
• Else route path through router.ts → match against redirects → build redirect Response
• If no match → error or falls back to other Worker code
5. handler returns the Response or handles error
Architectural Patterns & Principles
• Serverless / Event‐driven design
• Modular separation: Routing, Templating, Core Handler, Config
• Configuration‐driven behavior (baseUrl, auth, slash handling)
• Single Responsibility: each template file handles one output format