http-cookie-file
v1.0.0
Published
Lightweight Node.js library for reading, parsing, and atomically saving HTTP cookie files
Maintainers
Readme
http-cookie-file is an atomic HTTP cookie jar supporting Netscape cookies.txt and JSON formats.
📦 Installation
npm install http-cookie-filepnpm
pnpm install http-cookie-fileyarn
yarn add http-cookie-file📖 Usage
Basic Usage
import { CookieFile } from 'http-cookie-file';
const cookies = new CookieFile('./cookies.txt');
// Load existing cookies
await cookies.load();
// Set cookie from raw Set-Cookie header
await cookies.setCookie(
'sid=abc123; Path=/; Secure; HttpOnly',
'https://example.com/login'
);
// Generate Cookie request header
const header = await cookies.getCookieHeader('https://example.com/api');
// -> "sid=abc123"JSON Format
Store cookies in JSON format instead of Netscape format.
const cookies = new CookieFile('./cookies.json', { format: 'json' });
await cookies.setCookie('session=xyz123', 'https://example.com');Response Header Ingestion
Ingest Set-Cookie headers directly from standard Response or Headers objects.
const response = await fetch('https://example.com/api/login');
// Extract and persist cookies from response
await cookies.setFromResponse(response, 'https://example.com/api/login');Structured Cookie Operations
Manage individual cookies with structured input, queries, and deletions.
// Add structured cookie
await cookies.set({
name: 'theme',
value: 'dark',
domain: 'example.com',
path: '/',
secure: true,
maxAge: 3600,
});
// Query matched cookies for URL
const list = await cookies.getCookies('https://example.com/dashboard');
// Delete specific cookie
await cookies.delete('theme', { domain: 'example.com', path: '/' });Session Persistence & Auto-Save
Configure automatic disk persistence and session cookie behavior.
const cookies = new CookieFile('./cookies.txt', {
autoSave: true, // Automatically persist changes to disk
persistSessionCookies: false, // Exclude session cookies from disk writes
});Utilities
Exposes low-level RFC 6265 and Netscape parser functions.
import {
parseNetscapeCookies,
parseSetCookie,
serializeNetscapeCookies,
} from 'http-cookie-file';
// Parse Netscape cookies format text
const list = parseNetscapeCookies(rawContent);
// Serialize cookie list to Netscape format
const content = serializeNetscapeCookies(list);📚 Documentation
For all configuration options, please see the API docs.
🤝 Contributing
Want to contribute? Awesome! To show your support is to star the project, or to raise issues on GitHub.
Thanks again for your support, it is much appreciated! 🙏
License
MIT © Shahrad Elahi and contributors.
