Skip to main content

@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.)
The core Cookie interface represents an HTTP cookie with all standard attributes:
Parse a Set-Cookie header value into a Cookie object:
parseCookie returns undefined if the cookie string is invalid.
Convert a Cookie object into a Set-Cookie header value:
Work with multiple cookies in Cookie headers:
Encrypt cookie values with password-based encryption for secure transmission:
The password must be at least 32 characters long. An error will be thrown if the password is shorter.
decryptCookieValue returns undefined if decryption fails due to invalid signature or wrong password.

Serialize and parse encrypted cookies

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

Passwords must be at least 32 characters long to ensure adequate security. The package uses PBKDF2 for key derivation and HMAC for integrity checking.
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.
The package uses timingSafeEqual for signature comparison to prevent timing attacks when verifying encrypted cookies.
Always use HTTPS when transmitting cookies with the secure: true flag to prevent interception.