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.
Maintainers
Readme
🍪 parse-cookie-lite-ts
Ultra-fast (< 1 KB), zero-dependency HTTP
Cookieheader parser andSet-Cookieserializer for Node.js, Express, Next.js, and edge runtimes.
💡 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.cookieor standard Fetchrequest.headers.get('cookie')with automatic URI decoding and quoted-string unwrapping. - 🔒 Modern Security Flags — Supports all standard
Set-Cookiedirectives:HttpOnly,Secure,SameSite(Strict,Lax,None),Domain,Path,Max-Age, andPartitioned(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 withExpires=Thu, 01 Jan 1970 00:00:00 GMTandMax-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
