rhythmjs

Search documentation

Search guides, the tutorial and every package.

On this page

@rhythmjs/security/rate-limit#

TypeScript
function rateLimit(options?: RateLimitOptions): Middleware<RhythmHttpContext>
function rateLimitWs(options?: RateLimitOptions): RateLimitWsMiddleware
function memoryRateLimitStore(options?: MemoryRateLimitStoreOptions): RateLimitStore
RateLimitOptions#
limit (default 100), windowMs (default 60 000), store (RateLimitStore: increment(key, windowMs) returning RateLimitInfo ({ count, resetAt }), plus reset(key); sync or async), keyOf(request), skip(request), headers (emit RateLimit-*; default true), message, and trustProxy (default false; true means one hop, a number is the hop count). memoryRateLimitStore({ maxKeys }) caps the tracked keys (default 10 000, evicting the oldest-inserted).
Default key#
With trustProxy set, the X-Forwarded-For entry that many hops from the right (the trusted proxy's append), when it is a valid IPv4 or IPv6 address; otherwise request.ip, when it is a non-empty string; otherwise the middleware throws rateLimit: cannot identify the client... (there is no shared "global" fallback; use keyOf: () => "global" for a site-wide limit). Nothing in Rhythm sets request.ip: assign it in your Bun.serve fetch from server.requestIP(request), set trustProxy, or pass keyOf. X-Forwarded-For is never consulted without trustProxy, since the client controls it.
On refusal#
Status 429 with Retry-After (seconds to reset) and the JSON body { "success": false, "status": 429, "message": ... } (application/json; charset=utf-8). rateLimitWs is a standalone (ctx, next) middleware over RateLimitWsContext ({ request, response: Response | undefined }); on refusal it sets a 429 Response on ctx.response (bare application/json content type; RateLimit-* headers only on that refusal). It is not wired to the current RhythmWs, where the regular rateLimit works in use().

@rhythmjs/security/cors#

TypeScript
function cors(options?: CorsOptions): Middleware<RhythmHttpContext>

Returns middleware that sets Access-Control-* headers on every response. A preflight OPTIONS request is answered with 204 No Content directly; downstream middleware never runs for it.

origin?: string | string[] | ((origin: string) => string | null)#
Default "*". A non-wildcard string or array is echoed back only when the request origin matches; a function returns the origin to allow or null. Whenever origin is not "*", Vary: Origin is appended to the response.
allowMethods?: string[]#
Default GET,HEAD,PUT,POST,DELETE,PATCH,QUERY. Sent as Access-Control-Allow-Methods on preflight responses.
allowHeaders?: string[]#
Default: reflect the preflight's Access-Control-Request-Headers. When headers are sent, Vary: Access-Control-Request-Headers is appended to the preflight response.
exposeHeaders?: string[]#
Default none. Sent as Access-Control-Expose-Headers when non-empty.
maxAge?: number#
Default unset. Sent as Access-Control-Max-Age on preflight responses when provided.
credentials?: boolean#
Default unset. When true, sets Access-Control-Allow-Credentials: true.

The CorsOptions interface is also exported from the subpath.

@rhythmjs/security/csrf#

TypeScript
function csrf(options?: CsrfOptions): Middleware<RhythmHttpContext>

Returns middleware that rejects a request with 403 and the JSON body { "success": false, "status": 403, "message": "Forbidden" } (application/json; charset=utf-8) when all three conditions hold: the method is not GET or HEAD, the content type is a form type (application/x-www-form-urlencoded, multipart/form-data, or text/plain), and the Origin header is missing or not allowed. Every other request passes through.

origin?: string | string[] | ((origin: string) => boolean)#
Default: allow only the request URL's own origin. A string allows one origin, an array allows each listed origin, and a predicate returns whether the given origin is allowed.

The CsrfOptions interface is also exported from the subpath.

@rhythmjs/security/secure-headers#

TypeScript
function secureHeaders(options?: SecureHeadersOptions): Middleware<RhythmHttpContext>

Returns middleware that awaits next() and then sets the resolved headers on the response, so they apply after the handlers run. Every SecureHeadersOptions key accepts a string to override the header value or false to drop the header. The defaults are:

crossOriginOpenerPolicy#
Cross-Origin-Opener-Policy: same-origin
crossOriginResourcePolicy#
Cross-Origin-Resource-Policy: same-origin
referrerPolicy#
Referrer-Policy: no-referrer
strictTransportSecurity#
Strict-Transport-Security: max-age=15552000; includeSubDomains
xContentTypeOptions#
X-Content-Type-Options: nosniff
xDnsPrefetchControl#
X-DNS-Prefetch-Control: off
xDownloadOptions#
X-Download-Options: noopen
xFrameOptions#
X-Frame-Options: SAMEORIGIN
xPermittedCrossDomainPolicies#
X-Permitted-Cross-Domain-Policies: none
xXssProtection#
X-XSS-Protection: 0

contentSecurityPolicy (Content-Security-Policy) and crossOriginEmbedderPolicy (Cross-Origin-Embedder-Policy) default to off and are sent only when configured with a string value.

The SecureHeadersOptions interface is also exported from the subpath.