Documentation / @zerotal/session / RedisDriver
Class: RedisDriver
Defined in: session/src/drivers/RedisDriver.ts:59
Redis-backed session driver.
The cookie holds the session ID; all session data lives in Redis under
the key session:{id}. When a secret is provided the cookie value is
HMAC-signed (id.signature) so forged IDs are rejected before a Redis
lookup is ever attempted.
Security improvement over the unsigned version:
- Forged/tampered session IDs are detected server-side in constant time
via
safeEqual(@zerotal/core, backed by node:crypto'stimingSafeEqual) — no Redis round-trip required for rejection. - Uses
Bun.CryptoHasherwith BoringSSL — hardware-accelerated on supported CPUs, with zero npm packages.
Example
// Without signing (backwards compatible):
new RedisDriver(redis, 'session', 86400)
// With HMAC signing (recommended):
new RedisDriver(redis, 'session', 86400, Bun.env.SESSION_SECRET!)
See
SessionDriver — the interface this implements.
Implements
Constructors
Constructor
new RedisDriver(
redis,cookieName?,ttl?,secret?):RedisDriver
Defined in: session/src/drivers/RedisDriver.ts:69
Parameters
redis
RedisClient
Connected Bun RedisClient used for storage.
cookieName?
string = "session"
Name of the cookie carrying the session ID. Defaults
to "session".
ttl?
number = 86_400
Redis key TTL / cookie Max-Age in seconds. Defaults to
86400 (24h).
secret?
string
Optional HMAC secret; when set, cookie IDs are signed and verified before any Redis lookup. Omit to trust the raw cookie ID.
Returns
RedisDriver
Properties
cookieName
readonlycookieName:string="session"
Defined in: session/src/drivers/RedisDriver.ts:71
Name of the cookie carrying the session ID. Defaults
to "session".
Methods
loadFromRequest()
loadFromRequest(
request):Promise<SessionPayload>
Defined in: session/src/drivers/RedisDriver.ts:82
Read the (optionally signed) session ID from the cookie, then load its data
from Redis under session:{id}. A missing cookie, a failed signature, or an
unparseable value all yield a session with empty data.
Parameters
request
Request
Request carrying the session-ID cookie.
Returns
Promise<SessionPayload>
Implementation of
saveSession()
saveSession(
id,data,response):Promise<void>
Defined in: session/src/drivers/RedisDriver.ts:111
Write the session data to Redis (with TTL) and set the cookie to the ID —
signed when a secret is configured, raw otherwise.
Parameters
id
string
data
Record<string, unknown>
response
Response
Returns
Promise<void>
Implementation of
destroy()
destroy(
id):Promise<void>
Defined in: session/src/drivers/RedisDriver.ts:131
Delete a session's Redis record. Called by SessionMiddleware for IDs abandoned by SessionManager.regenerate so they cannot be replayed.
Parameters
id
string
Session ID whose session:{id} key is removed.
Returns
Promise<void>