npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@uppercut-labs/security-headers

v0.1.0

Published

Build Content-Security-Policy strings from directive maps, merge policies, set the usual response security headers on a Response, and edit Cloudflare Pages _headers files. No dependencies.

Readme

@uppercut-labs/security-headers

Build a Content-Security-Policy from a directive map, derive per-route policies from one base, set the usual security headers on a Response, and edit a Cloudflare Pages _headers file. No dependencies. It runs in Cloudflare Workers, Deno, Bun, browsers and Node 18+.

npm install @uppercut-labs/security-headers
import { applySecurityHeaders, buildCsp, cspOrigin, extendCsp } from '@uppercut-labs/security-headers';

const app = {
  'default-src': "'none'",
  'script-src': "'self'",
  'style-src': "'self'",
  'img-src': "'self'",
  'connect-src': ["'self'", cspOrigin(env.API_BASE_URL)], // blank adds nothing
  'base-uri': "'none'",
  'frame-ancestors': "'none'",
};
const withCovers = extendCsp(app, { 'img-src': 'https://img.example.test', 'frame-src': 'https://player.example.test' });

export default {
  async fetch(request: Request, env: Env) {
    const response = await handle(request, env);
    return applySecurityHeaders(response, { csp: buildCsp(isMediaPage(request) ? withCovers : app) });
  },
};

Policies

  • buildCsp(directives): { 'img-src': ["'self'", 'data:'] } becomes img-src 'self' data:. Directives keep the order written. A string source is split on spaces and duplicates are dropped. true writes a bare directive (upgrade-insecure-requests); false, null and undefined leave a directive out. A source containing ;, ,, whitespace or a control character throws SecurityHeadersError, so a configured value cannot end a directive or add a second policy.
  • extendCsp(base, additions): add sources to a base policy (a directive map or a policy string). Each addition is appended to the same directive or added when the base has none; 'none' is dropped from a directive that gains sources. Returns a directive map for buildCsp.
  • parseCsp(policy): a policy string as { directive: sources[] }. Names are lower-cased and the first of a repeated directive wins, as in browsers.
  • cspOrigin(url): the origin of an http(s) URL for use as a source. A blank value gives ''; anything else that is not a URL throws.
  • escapeCspSource(url): percent-encodes ;, , and whitespace in a URL you must use as a source.

Response headers

securityHeaders(options) returns the headers as an object; applySecurityHeaders(response, options, { overwrite? }) returns a copy of the response with them set, passing status, status text and body through. A header the response already has is kept unless overwrite is true, so a route's own page policy survives.

| Option | Default | Meaning | | --- | --- | --- | | csp | none | Policy string or directive map; false for none | | contentTypeOptions | nosniff | false to omit | | referrerPolicy | no-referrer | A value, or false to omit | | permissionsPolicy | camera=(), microphone=(), geolocation=() | A string, or { camera: [], fullscreen: ['self'] }, or false | | frameOptions | DENY | 'SAMEORIGIN', or false for a site that relies on frame-ancestors | | hsts | off | { maxAgeSeconds, includeSubDomains?, preload? }; set it only for a host that is HTTPS-only | | crossOriginOpenerPolicy, crossOriginResourcePolicy | off | Any value | | extra | none | Other headers, set as given |

A value that could not be sent in a header (a line break, a non-ASCII character) throws.

_headers files

import { buildHeadersFile, setHeaderInHeadersFile } from '@uppercut-labs/security-headers';

await writeFile('dist/_headers', buildHeadersFile([
  { path: '/*', headers: { 'X-Content-Type-Options': 'nosniff', 'Content-Security-Policy': buildCsp(app) } },
  { path: '/assets/*', headers: { 'Cache-Control': 'public, max-age=31536000, immutable' } },
]));

// At build time, swap one value in a file the framework already wrote:
const text = await readFile('dist/_headers', 'utf8');
await writeFile('dist/_headers', setHeaderInHeadersFile(text, 'Content-Security-Policy', policy));

setHeaderInHeadersFile(text, name, value, { path? }) replaces the header in every rule, or only the rule for path, matching names without regard to case. It throws when the header is not there, so a build cannot quietly ship a file without it. Path lines, names and values may not contain a line break.

Tests

The suite runs reference copies of the policy builders, header wrapper and _headers rewrite step this package replaces, with synthetic origins, and compares their output byte for byte; where it differs from a hand-spliced string (directive order in one derived policy) it compares the parsed sources instead.

License

MIT