Security and Utility Middlewares
Discover built-in middlewares for security headers, CSRF defense, body size limits, ETags, Server-Timing, and response compression.
Taser.js bundles a suite of production-grade security and performance utility middlewares.
Secure Headers (@taserjs/router/secure-headers)
Sets standard HTTP security headers (HSTS, X-Content-Type-Options, X-Frame-Options, Content-Security-Policy):
import { secureHeaders } from "@taserjs/router/secure-headers";
import { t } from "@taserjs/router";
export default t.layout("/*").use(
secureHeaders({
contentSecurityPolicy: {
defaultSrc: ["'self'"],
scriptSrc: ["'self'", "'unsafe-inline'"],
},
strictTransportSecurity: {
maxAge: 31536000,
includeSubDomains: true,
preload: true,
},
xFrameOptions: "DENY",
xContentTypeOptions: "nosniff",
}),
);Body Size Limits (@taserjs/router/body-limit)
Prevents denial-of-service memory exhaustion by enforcing maximum request body limits:
import { bodyLimit } from "@taserjs/router/body-limit";
import { payloadTooLarge } from "@taserjs/router/reply";
import { t } from "@taserjs/router";
export default t.layout("/uploads").use(
bodyLimit({
maxSize: 10 * 1024 * 1024, // 10MB limit
onError: () => {
return payloadTooLarge({ message: "Payload too large. Maximum size is 10MB." });
},
}),
);CSRF Defense (@taserjs/router/csrf)
Validates incoming request origin against server origins to prevent cross-site request forgery attacks on state-changing requests (POST, PUT, PATCH, DELETE):
import { csrf } from "@taserjs/router/csrf";
import { t } from "@taserjs/router";
export default t.layout("/*").use(
csrf({
origin: ["https://app.example.com"],
}),
);Automatic ETags (@taserjs/router/etag)
Calculates cryptographic hash ETags for response bodies and automatically responds with 304 Not Modified when clients send a matching If-None-Match header:
import { etag } from "@taserjs/router/etag";
import { t } from "@taserjs/router";
export default t.layout("/*").use(
etag({
weak: true,
}),
);Server-Timing (@taserjs/router/timing)
Adds standard Server-Timing headers to responses, enabling Chrome DevTools and observability platforms to measure internal route latency:
import { timing } from "@taserjs/router/timing";
import { t } from "@taserjs/router";
export default t.layout("/*").use(timing());Response Compression (@taserjs/router/compress)
Automatically compresses outgoing HTTP response payloads using gzip, deflate, or brotli based on client Accept-Encoding:
import { compress } from "@taserjs/router/compress";
import { t } from "@taserjs/router";
export default t.layout("/*").use(
compress({
threshold: 1024, // Compress responses larger than 1KB
}),
);Summary of Built-in Middlewares
| Middleware Import | Package | Primary Purpose |
|---|---|---|
cors | @taserjs/router/cors | Cross-Origin Resource Sharing |
jwt | @taserjs/router/jwt | HMAC/RSA Bearer Token Verification |
jwk | @taserjs/router/jwk | Remote JSON Web Key Set Auth |
secureHeaders | @taserjs/router/secure-headers | CSP, HSTS, X-Frame-Options |
bodyLimit | @taserjs/router/body-limit | Memory protection payload limiting |
csrf | @taserjs/router/csrf | Cross-Site Request Forgery mitigation |
etag | @taserjs/router/etag | 304 Cache validation |
timing | @taserjs/router/timing | Server-Timing performance metrics |
compress | @taserjs/router/compress | Gzip / Deflate payload compression |
JWT and JWKS Authentication
Verify JSON Web Tokens (JWT) and remote JWKS key sets (Auth0, Clerk, Supabase) with built-in typed authentication middleware for Taser.js APIs.
Standalone API
Build standalone, zero-host HTTP APIs with pure Taser.js, Vite, and Nitro. Maximum throughput, zero boilerplate, and web-standard Request/Response execution.