You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
import{middlewares}from"@eser/http";// Allow all originsconstcors=middlewares.cors();// Allow specific originsconstcors=middlewares.cors({origin: ["https://example.com","https://app.example.com"],credentials: true,maxAge: 86400,});// Dynamic origin checkconstcors=middlewares.cors({origin: (origin)=>origin.endsWith(".example.com"),});
CSP Middleware
import{middlewares}from"@eser/http";// Use default secure policyconstcsp=middlewares.csp();// Custom policy with nonce for inline scriptsconstcsp=middlewares.csp({useNonce: true,directives: {"default-src": "'self'","img-src": ["'self'","data:","https:"],},});// Report-only mode for testingconstcsp=middlewares.csp({reportOnly: true});
CSRF Middleware
import{middlewares}from"@eser/http";// Basic usageconstcsrf=middlewares.csrf();// Custom configurationconstcsrf=middlewares.csrf({cookie: "my_csrf_token",header: "X-My-CSRF-Token",excludePaths: ["/api/webhooks/*","/health"],});// Client-side: read token from cookie, include in header// fetch("/api/data", { headers: { "X-CSRF-Token": tokenFromCookie }})
Rate Limiter
Instance-based rate limiting with automatic cleanup and SSRF protection.
import{middlewares}from"@eser/http";// Create a rate limiter instanceconstlimiter=middlewares.createRateLimiter({maxRequests: 100,// Max requests per windowwindowMs: 60_000,// 1 minute windowskipPaths: ["/health","/api/public"],trustProxy: true,// Trust X-Forwarded-For header});// In your request handlerfunctionhandleRequest(request: Request){consturl=newURL(request.url);// Check rate limitconstrateLimitResponse=limiter.check(request,url.pathname);if(rateLimitResponse){returnrateLimitResponse;// 429 Too Many Requests}// Process request...constresponse=newResponse("OK");// Add rate limit headers to responseconstclientIp=middlewares.getClientIp(request,true);constheaders=limiter.getHeaders(clientIp,url.pathname);for(const[key,value]ofObject.entries(headers)){response.headers.set(key,value);}returnresponse;}// Cleanup on server shutdownprocess.on("SIGTERM",()=>{limiter.stop();});
Response Headers
The rate limiter adds these headers to responses:
Header
Description
X-RateLimit-Limit
Maximum requests per window
X-RateLimit-Remaining
Requests remaining in window
X-RateLimit-Reset
Unix timestamp when window resets
Retry-After
Seconds until retry (when limited)
Handler Adapter
Bridge between Web Request/Response and @eser/functions handler pattern
(HttpEvent/HttpResponse):
import*ashttpAdapterfrom"@eser/http/adapter";import*ashttpResponsefrom"@eser/http/response";import*ashandlerfrom"@eser/functions/handler";import*astaskfrom"@eser/functions/task";// Convert a Web Request to an HttpEventconsteventResult=awaithttpAdapter.fromRequest(request);// Convert an HttpResponse back to a Web ResponseconstwebResponse=httpResponse.toWebResponse({status: 200,headers: {"content-type": "application/json"},body: {ok: true},});