@temelj/request
HTTP request utilities for working with cookies, including parsing, serialization, and encryption capabilities.Installation
Overview
The@temelj/request package provides comprehensive utilities for handling HTTP cookies with support for:
- Cookie parsing and serialization
- Cookie encryption and decryption with password-based security
- Cookie header manipulation
- Full support for cookie attributes (secure, httpOnly, sameSite, etc.)
Cookie interface
The coreCookie interface represents an HTTP cookie with all standard attributes:
Basic cookie operations
Parse a cookie
Parse a Set-Cookie header value into a Cookie object:parseCookie returns undefined if the cookie string is invalid.Serialize a cookie
Convert a Cookie object into a Set-Cookie header value:Cookie headers
Work with multiple cookies in Cookie headers:Cookie encryption
Encrypt cookie values with password-based encryption for secure transmission:Encrypt a cookie value
Decrypt a cookie value
decryptCookieValue returns undefined if decryption fails due to invalid signature or wrong password.Serialize and parse encrypted cookies
- Serialize encrypted
- Parse encrypted
Encryption options
Customize the encryption algorithm and parameters:Example with custom options
Complete example
Here’s a complete example showing cookie creation, encryption, and parsing:Type exports
Security considerations
Password requirements
Password requirements
Passwords must be at least 32 characters long to ensure adequate security. The package uses PBKDF2 for key derivation and HMAC for integrity checking.
Encryption algorithm
Encryption algorithm
The default encryption uses AES-CBC with 256-bit keys. The encrypted cookie value includes the IV, salt, encrypted data, and HMAC signature for integrity verification.
Timing attacks
Timing attacks
The package uses
timingSafeEqual for signature comparison to prevent timing attacks when verifying encrypted cookies.