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

parse-cookie-lite-ts

v1.0.0

Published

Ultra-fast (<1 KB), zero-dependency HTTP Cookie header parser and Set-Cookie serializer for Node.js, Express, Next.js, and edge workers.

Readme

🍪 parse-cookie-lite-ts

Ultra-fast (< 1 KB), zero-dependency HTTP Cookie header parser and Set-Cookie serializer for Node.js, Express, Next.js, and edge runtimes.

npm version License: MIT Sponsor TypeScript Size


💡 Why parse-cookie-lite-ts?

Managing HTTP session cookies in edge workers (Cloudflare, Vercel Edge), microservices, or custom HTTP servers often relies on standard cookie npm packages which lack native modern ESM exports or add bloat.

parse-cookie-lite-ts provides:

  • ⚡ Zero Dependencies — Minified to less than 1 KB, pure native TypeScript.
  • 🍪 Universal Cookie Parsing — Parses incoming request req.headers.cookie or standard Fetch request.headers.get('cookie') with automatic URI decoding and quoted-string unwrapping.
  • 🔒 Modern Security Flags — Supports all standard Set-Cookie directives: HttpOnly, Secure, SameSite (Strict, Lax, None), Domain, Path, Max-Age, and Partitioned (CHIPS).
  • 📦 Dual ESM & CommonJS — Native compatibility across Node.js, Next.js, Fastify, Express, Bun, Deno, and Edge Workers.

📥 Installation

# npm
npm install parse-cookie-lite-ts

# pnpm
pnpm add parse-cookie-lite-ts

# yarn
yarn add parse-cookie-lite-ts

# bun
bun add parse-cookie-lite-ts

🚀 Usage & Real-World Examples

1. Parsing Incoming Request Cookies

import { parseCookie } from 'parse-cookie-lite-ts';

// Incoming HTTP request header:
const cookieHeader = 'sessionId=s%3Aabc123; userTheme=dark; consent=true';

const cookies = parseCookie(cookieHeader);

console.log(cookies.sessionId); // "s:abc123"
console.log(cookies.userTheme); // "dark"
console.log(cookies.consent);   // "true"

2. Creating Secure Set-Cookie Headers

import { serializeCookie } from 'parse-cookie-lite-ts';

// Create a secure 7-day authentication cookie:
const authCookie = serializeCookie('session_token', 'jwt_secret_token_123', {
  maxAge: 7 * 24 * 3600, // 7 days in seconds
  httpOnly: true,        // Prevent JavaScript access (mitigates XSS)
  secure: true,          // HTTPS only
  sameSite: 'lax',       // CSRF mitigation
  path: '/',
});

console.log(authCookie);
// "session_token=jwt_secret_token_123; Max-Age=604800; Path=/; HttpOnly; Secure; SameSite=Lax"

3. Next.js App Router / Edge Middleware Handler

import { parseCookie, serializeCookie } from 'parse-cookie-lite-ts';

export function handleRequest(request: Request): Response {
  const cookieHeader = request.headers.get('cookie') || '';
  const cookies = parseCookie(cookieHeader);

  const preferredLanguage = cookies.lang || 'en';

  const response = new Response(JSON.stringify({ lang: preferredLanguage }), {
    headers: { 'Content-Type': 'application/json' },
  });

  // Set response cookie if missing:
  if (!cookies.lang) {
    const newCookie = serializeCookie('lang', 'en', {
      maxAge: 30 * 86400,
      path: '/',
    });
    response.headers.set('Set-Cookie', newCookie);
  }

  return response;
}

📖 API Reference

parseCookie(header)

Parses an HTTP Cookie header string into key-value pairs.

  • Parameters:
    • header (string): Raw header string.
  • Returns: Record<string, string>.

serializeCookie(name, value, options?)

Serializes a cookie into an RFC 6265 compliant Set-Cookie string.

  • Parameters:
    • name (string): Cookie key name.
    • value (string): Cookie string value.
    • options (CookieSerializeOptions, optional): Configuration attributes.
  • Returns: string.

Options (CookieSerializeOptions):

| Property | Type | Default | Description | |---|---|---|---| | maxAge | number | undefined | Relative expiration duration in seconds | | expires | Date | undefined | Absolute expiration date | | domain | string | undefined | Domain scope for the cookie | | path | string | '/' | Path scope for the cookie | | httpOnly | boolean | false | Forbids client-side script access | | secure | boolean | false | Restricts transmission over HTTPS only | | sameSite | 'strict' \| 'lax' \| 'none' \| boolean | undefined | Controls cross-site cookie transmission | | partitioned | boolean | false | Opts in to partitioned storage (CHIPS) |


clearCookie(name, options?)

Helper to generate a Set-Cookie header that immediately expires and clears a cookie.

  • Parameters:
    • name (string): Cookie key name to delete.
    • options (CookieSerializeOptions, optional): Path and domain scope.
  • Returns: string (Set-Cookie string with Expires=Thu, 01 Jan 1970 00:00:00 GMT and Max-Age=0).
import { clearCookie } from 'parse-cookie-lite-ts';

res.setHeader('Set-Cookie', clearCookie('auth_token', { path: '/' }));

🧪 Testing

100% test coverage verified with Vitest:

npm test

💖 Support the Project

If parse-cookie-lite-ts streamlined cookie handling across your backend and edge services, please consider sponsoring:

👉 Sponsor @Piratha97 on GitHub Sponsors


📄 License

MIT © 2026 Pirapaakaran Pirathapan