Cookies
@t-req/core includes a cookie jar for automatic cookie handling across requests, with RFC 6265 compliance via tough-cookie.
Basic Usage
Section titled “Basic Usage”Enable cookie handling by creating a cookie jar:
import { createClient } from '@t-req/core';import { createNodeIO } from '@t-req/core/runtime';import { createCookieJar } from '@t-req/core/cookies';
const jar = createCookieJar();
const client = createClient({ io: createNodeIO(), cookieJar: jar,});
// Login response sets cookies automaticallyawait client.run('./auth/login.http');
// Subsequent requests include cookiesconst profile = await client.run('./users/profile.http');Manual Cookie Management
Section titled “Manual Cookie Management”Set Cookies
Section titled “Set Cookies”const jar = createCookieJar();
// Set a cookie from a Set-Cookie header valuejar.setCookieSync('session=abc123; Path=/', 'https://example.com/');
// Set multiple cookiesjar.setCookieSync('theme=dark; Path=/', 'https://example.com/');jar.setCookieSync('lang=en; Path=/', 'https://example.com/');Read Cookies
Section titled “Read Cookies”// Get all cookies for a URLconst cookies = jar.getCookiesSync('https://example.com/api');console.log(cookies.map((c) => `${c.key}=${c.value}`));
// Get the Cookie header stringconst cookieHeader = jar.getCookieStringSync('https://example.com/api');// "session=abc123; theme=dark; lang=en"Persistence
Section titled “Persistence”Save and restore cookies across sessions:
Save Cookies
Section titled “Save Cookies”const snapshot = jar.serializeSync();
// Bunawait Bun.write('./cookies.json', JSON.stringify(snapshot, null, 2));
// Node.jsimport { writeFile } from 'node:fs/promises';await writeFile('./cookies.json', JSON.stringify(snapshot, null, 2), 'utf8');Restore Cookies
Section titled “Restore Cookies”import { CookieJar } from '@t-req/core/cookies';
// Bunconst loaded = JSON.parse(await Bun.file('./cookies.json').text());
// Node.jsimport { readFile } from 'node:fs/promises';const loaded = JSON.parse(await readFile('./cookies.json', 'utf8'));
// Restore into a new jarconst jar = CookieJar.deserializeSync(loaded);Security Features
Section titled “Security Features”The cookie jar enforces security best practices:
Domain Scope Validation
Section titled “Domain Scope Validation”Cookies can only be set for the request domain or its parent domains:
// Request to api.example.com can set cookies for:// - api.example.com ✓// - example.com ✓// - .com ✗ (public suffix)Public Suffix Protection
Section titled “Public Suffix Protection”By default, cookies for public suffixes like .com, .co.uk, .github.io are rejected:
// This will be rejectedjar.setCookieSync('evil=value; Domain=.com', 'https://example.com/');Secure Cookie Enforcement
Section titled “Secure Cookie Enforcement”Secure cookies are only accepted from HTTPS and only sent over HTTPS:
// Only works over HTTPSjar.setCookieSync('token=secret; Secure', 'https://example.com/');RFC 6265 Ordering
Section titled “RFC 6265 Ordering”Cookies are sorted by path length (longest first), then by creation time.
Configuration Options
Section titled “Configuration Options”Disable Public Suffix Protection
Section titled “Disable Public Suffix Protection”For compatibility with servers that incorrectly set cookies (not recommended):
const jar = createCookieJar({ rejectPublicSuffixes: false });Sharing Cookies Between Clients
Section titled “Sharing Cookies Between Clients”Multiple clients can share a cookie jar:
const sharedJar = createCookieJar();
const client1 = createClient({ io: createNodeIO(), cookieJar: sharedJar,});
const client2 = createClient({ io: createNodeIO(), cookieJar: sharedJar,});
// Login with client1await client1.run('./auth/login.http');
// client2 now has the session cookieawait client2.run('./api/data.http');Clearing Cookies
Section titled “Clearing Cookies”Create a new jar to clear all cookies:
const client = createClient({ io: createNodeIO(), cookieJar: createCookieJar(), // Fresh jar, no cookies});Or for domain-specific clearing, iterate and remove:
const cookies = jar.getCookiesSync('https://example.com/');for (const cookie of cookies) { jar.removeCookieSync(cookie.domain, cookie.path, cookie.key);}