Registry / serialization / cookie-lite

cookie-lite

JSON →
library0.0.2jsnpmunverified

cookie-lite is a super lightweight JavaScript library designed for parsing and serializing HTTP cookie strings. It serves as a friendly fork of the `lightcookie` package, which was deprecated by its original author. The library focuses on efficiency and a minimal footprint, utilizing JavaScript regular expressions for its parsing logic, making it a smaller and faster alternative compared to more feature-rich cookie handling packages. Currently at version 0.0.2, it supports Node.js environments from version 18 onwards. Its primary differentiator is its minimal resource consumption and speed for fundamental cookie operations, suitable for applications where performance and size are critical. It offers straightforward methods for converting raw cookie strings into objects and vice versa, providing core functionality without extensive configuration or advanced features like signed cookies or complex attribute validation. Its simple API aims for ease of integration for basic cookie management tasks.

npm install cookie-lite
INSTALL
IMPORT
SIG · COOKIE-LITE
C
cookie-lite
serializationjavascriptv0.0.2
Install
—
Import
—
Disk
—
Pass rate
0/ 6
Env Coverage0 / 6
glibc
18–22
musl
18–22
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18–226 runs
build_error
glibc
node 18–226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

cookieLite
✓ import cookieLite from 'cookie-lite'
✗ const cookieLite = require('cookie-lite').default
The `cookie-lite` package provides a default object containing `parse` and `serialize` methods. This is the primary way to import the module in both ESM and CJS contexts.
parse
✓ import cookieLite from 'cookie-lite'; const parse = cookieLite.parse;
✗ import { parse } from 'cookie-lite'
The `parse` function is a method of the default `cookieLite` object. While some modern packages offer direct named exports, this approach ensures compatibility with the library's primary export structure. For CommonJS, use `require('cookie-lite').parse`.
serialize
✓ import cookieLite from 'cookie-lite'; const serialize = cookieLite.serialize;
✗ import { serialize } from 'cookie-lite'
The `serialize` function is a method of the default `cookieLite` object. This reflects the library's export pattern. For CommonJS, use `require('cookie-lite').serialize`.

Demonstrates how to parse a raw cookie string into an object and serialize an object into a cookie string, including common attributes like `HttpOnly`, `Expires`, and `SameSite`.

import { parse, serialize } from 'cookie-lite'; // Example 1: Parsing cookie strings const cookieString = 'foo=bar; copyright=%C2%A9; another=test'; const parsedCookies = parse(cookieString); console.log('Parsed cookies:', parsedCookies); // Expected: { foo: 'bar', copyright: '©', another: 'test' } // Example 2: Serializing objects to cookie strings const cookieObject = { name: 'Choco', HttpOnly: null, // Should serialize as just 'HttpOnly' flag Expires: new Date(Date.now() + 3600 * 1000).toUTCString(), // Add an Expires date MaxAge: 3600, Path: '/', Secure: true, SameSite: 'Lax', ValueWithSpace: 'hello world' // Test value with space }; const serializedCookie = serialize(cookieObject); console.log('Serialized cookie:', serializedCookie); // Expected: 'name=Choco;HttpOnly;Expires=...;Max-Age=3600;Path=/;Secure;SameSite=Lax;ValueWithSpace=hello%20world' // Demonstrate basic usage console.log('\nDemonstrating basic usage:'); const myCookie = serialize({ user: 'Alice', theme: 'dark', 'Path': '/' }); console.log('Set-Cookie header:', myCookie); const receivedCookies = parse(myCookie); console.log('Received cookies:', receivedCookies);
Debug
Known issues
breakingUsers migrating from the deprecated `ethan7g/lightcookie` package should be aware that `cookie-lite` is a new, independent fork. While it aims for functional compatibility, it's essential to perform thorough testing as behavioral nuances might exist. The original `lightcookie` is no longer maintained.
fix
Update all package imports to `cookie-lite` and conduct comprehensive regression testing. Review `cookie-lite`'s documentation for any specific changes or new features relative to `lightcookie`.
affects: >=0.0.1
gotcha`cookie-lite` focuses on basic parsing and serialization without implementing advanced security features like automatic cookie validation, signing, or comprehensive sanitization. Users are solely responsible for setting appropriate security attributes and handling untrusted input.
fix
Always validate and sanitize cookie data from untrusted sources. Explicitly set secure cookie attributes (`Secure`, `HttpOnly`, `SameSite='Lax'` or `'Strict'`) when creating cookies, especially for production or sensitive data.
affects: >=0.0.1
gotcha`cookie-lite` handles URI component encoding and decoding for cookie values. Be cautious with non-standard or double-encoded cookie values, as unexpected behavior may occur if encoding schemes conflict or are applied incorrectly at the application layer.
fix
Maintain consistent encoding practices for cookie values. Avoid double-encoding. If specific encoding requirements exist, ensure they are applied before serialization and processed after parsing, potentially outside of `cookie-lite`'s default handling.
affects: >=0.0.1
Upgrade
Version history
0.0.2latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
11 hits · last 30 days
node
10
OpenAI (training)
1
Resources
cookie-lite — npm install cookie-lite · libregistry