withSecurityHeaders function is a utility for adding a small set of security-related HTTP headers to a Response. It's fully opt-in, nothing else in @arkyn/server calls it automatically, you wrap the responses you want it applied to.ts
import { withSecurityHeaders } from "@arkyn/server/withSecurityHeaders";
response (required)Response object to add headers to. Its headers are mutated in place, the same instance is returned.Responseoverrides (optional)null removes it (useful to opt a default header back out).Record<string, string | null>X-Content-Type-Options: nosniffX-Frame-Options: DENYReferrer-Policy: strict-origin-when-cross-originContent-Security-Policy and Strict-Transport-Security are deliberately not set by default, both are too app-specific to guess safely: a wrong CSP can break an app's own scripts, and HSTS assumes HTTPS is already correctly configured elsewhere. Add either through overrides if your app needs them.Response instance it was given, with headers added.Responsetypescript
import { Success } from "@arkyn/server/success";import { withSecurityHeaders } from "@arkyn/server/withSecurityHeaders";// Apply the defaultsreturn withSecurityHeaders(new Success("OK", { data }).toResponse());// Add a Content-Security-Policy, and opt out of X-Frame-Optionsreturn withSecurityHeaders(new Success("OK", { data }).toResponse(), {"Content-Security-Policy": "default-src 'self'","X-Frame-Options": null,});
Response instance passed in, it does not clone the response. Existing headers, body, status, and statusText are all preserved.