Skip to content
Source
Sign inSign up
TanStack/router
Open History to see the last commit

session.ts

75 lines2.9 KB

1// TODO discuss do we want to copy this interface into Start we well?
2import type { CookieSerializeOptions } from 'cookie-es'
3​
4// TODO discuss do we want to copy this interface into Start as shown below?
5​
6/** Algorithm used for encryption and decryption. */
7type EncryptionAlgorithm = 'aes-128-ctr' | 'aes-256-cbc'
8/** Algorithm used for integrity verification. */
9type IntegrityAlgorithm = 'sha256'
10/** @internal */
11type _Algorithm = EncryptionAlgorithm | IntegrityAlgorithm
12/**
13 * Options for customizing the key derivation algorithm used to generate encryption and integrity verification keys as well as the algorithms and salt sizes used.
14 */
15type SealOptions = Readonly<{
16 /** Encryption step options. */
17 encryption: SealOptionsSub<EncryptionAlgorithm>
18 /** Integrity step options. */
19 integrity: SealOptionsSub<IntegrityAlgorithm>
20 /* Sealed object lifetime in milliseconds where 0 means forever. Defaults to 0. */
21 ttl: number
22 /** Number of seconds of permitted clock skew for incoming expirations. Defaults to 60 seconds. */
23 timestampSkewSec: number
24 /**
25 * Local clock time offset, expressed in number of milliseconds (positive or negative). Defaults to 0.
26 */
27 localtimeOffsetMsec: number
28}>
29/** `seal()` method options. */
30type SealOptionsSub<TAlgorithm extends _Algorithm = _Algorithm> = Readonly<{
31 /** The length of the salt (random buffer used to ensure that two identical objects will generate a different encrypted result). Defaults to 256. */
32 saltBits: number
33 /** The algorithm used. Defaults to 'aes-256-cbc' for encryption and 'sha256' for integrity. */
34 algorithm: TAlgorithm
35 /** The number of iterations used to derive a key from the password. Defaults to 1. */
36 iterations: number
37 /** Minimum password size. Defaults to 32. */
38 minPasswordlength: number
39}>
40/** Password secret string or buffer.*/
41​
42type SessionDataT = Record<string, any>
43export type SessionData<T extends SessionDataT = SessionDataT> = Partial<T>
44export interface Session<T extends SessionDataT = SessionDataT> {
45 id: string
46 createdAt: number
47 data: SessionData<T>
49​
50export type SessionUpdate<T extends SessionData = SessionData> =
51 | Partial<SessionData<T>>
52 | ((oldData: SessionData<T>) => Partial<SessionData<T>> | undefined)
53​
54export interface SessionManager<T extends SessionDataT = SessionDataT> {
55 readonly id: string | undefined
56 readonly data: SessionData<T>
57 update: (update: SessionUpdate<T>) => Promise<SessionManager<T>>
58 clear: () => Promise<SessionManager<T>>
60export interface SessionConfig {
61 /** Private key used to encrypt session tokens */
62 password: string
63 /** Session expiration time in seconds */
64 maxAge?: number
65 /** default is 'start' */
66 name?: string
67 /** Default is secure, httpOnly, / */
68 cookie?: false | CookieSerializeOptions
69 /** Default is x-start-session / x-{name}-session */
70 sessionHeader?: false | string
71 seal?: SealOptions
72 crypto?: Crypto
73 /** Default is Crypto.randomUUID */
74 generateId?: () => string