Something went wrong. Try again.
Something went wrong. Try again.
1234567891011121314151617181920export const SESSION_COOKIE = 'shard_session';export const SECURE_SESSION_COOKIE = '__Host-shard_session';export interface SessionCookieOptions { maxAgeSeconds?: number; secure?: boolean; }export function issueSessionCookie(value: string, options: SessionCookieOptions = {}): string { if (!/^[A-Za-z0-9._~-]+$/.test(value)) throw new Error('invalid cookie value'); const secure = options.secure === true; const name = secure ? SECURE_SESSION_COOKIE : SESSION_COOKIE; const maxAge = options.maxAgeSeconds === undefined ? '' : `; Max-Age=${Math.max(0, Math.floor(options.maxAgeSeconds))}`; return `${name}=${value}; Path=/; HttpOnly;${secure ? ' Secure;' : ''} SameSite=Strict${maxAge}`;}export function parseSessionCookie(header: string | undefined): string | undefined { if (!header) return undefined; for (const part of header.split(';')) { const [name, ...rest] = part.trim().split('='); if (name === SESSION_COOKIE || name === SECURE_SESSION_COOKIE) { const value = rest.join('='); return /^[A-Za-z0-9._~-]+$/.test(value) ? value : undefined; } } return undefined;}/** True when the cookie header carries the `Secure`/`__Host-` form of the session cookie. */export function hasSecureSessionCookie(header: string | undefined): boolean { return !!header && header.split(';').some((part) => part.trim().startsWith(`${SECURE_SESSION_COOKIE}=`));}