Zero-dependency Web-Standards cookie parser & serializer for Node.js, Bun, Deno, Cloudflare Workers, and Next.js Edge Runtime.
- ⚡ Web-Standards Native: Directly accepts
RequestorHeadersobjects from the Fetch API. - 🌐 Cross-Runtime: Zero Node.js-specific dependencies (
http/net). - 🛡️ Modern Security: Full support for
Partitioned(CHIPS),SameSite,HttpOnly, andSecureattributes. - 📦 Microscopic Footprint: Dual ESM + CommonJS builds with TypeScript declaration maps included out of the box.
| Runtime | Support |
|---|---|
| Node.js | >= 18.0.0 |
| Bun | >= 1.0.0 |
| Deno | >= 1.30.0 |
| Cloudflare Workers / Edge | ✅ Native |
| Next.js Edge Middleware | ✅ Native |
npm install @maganya/cross-cookie
Or using alternative package managers:
pnpm add @maganya/cross-cookie
# or
yarn add @maganya/cross-cookie
# or
bun add @maganya/cross-cookie
You can parse cookies directly from standard Web Request objects, Headers maps, or raw string headers:
import { parseCookies } from '@maganya/cross-cookie';
// From Next.js Edge Middleware / Cloudflare Workers
export async function middleware(request: Request) {
const cookies = parseCookies(request);
console.log(cookies.session); // 'abc12345'
}
// From a standard Fetch API Headers object
const headers = new Headers({ cookie: 'theme=dark; lang=en' });
const cookiesFromHeaders = parseCookies(headers);
// From a raw string
const cookiesFromString = parseCookies('theme=dark; session=abc12345');Format Set-Cookie header strings with modern security flags:
import { serializeCookie } from '@maganya/cross-cookie';
const cookieHeader = serializeCookie('session_id', 'abc12345', {
httpOnly: true,
secure: true,
sameSite: 'Strict',
partitioned: true, // CHIPS (Cookies Having Independent Partitioned State)
path: '/',
maxAge: 3600
});
// Output:
// session_id=abc12345; Max-Age=3600; Path=/; HttpOnly; Secure; SameSite=Strict; PartitionedParses incoming cookie headers into a key-value record object.
-
Parameters:
-
input:string | Request | Headers -
Returns:
Record<string, string>
Serializes a cookie name and value into a valid Set-Cookie header value.
-
Parameters:
-
name:string -
value:string -
options:CookieOptions(optional) -
CookieOptionsProperties: -
path?: string -
domain?: string -
maxAge?: number -
expires?: Date -
secure?: boolean -
httpOnly?: boolean -
sameSite?: 'Strict' | 'Lax' | 'None' -
partitioned?: boolean
MIT © Owiny Livingstone Maganya