Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 8x 8x 6x 6x 6x 6x 6x 6x 1x 1x 1x 1x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 4x 3x 3x 3x 4x 3x 3x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 3x 3x 3x 3x 3x 3x 1x 1x 5x 5x 1x 5x 5x 3x 3x 2x 2x 5x 5x 1x 1x 5x 5x 5x 5x 5x 5x 5x 5x | import { existsSync, readFileSync, writeFileSync } from 'node:fs';
import { join } from 'node:path';
export const SESSION_METADATA_FILE_NAME = 'session-metadata.json';
const RELEVANT_COOKIE_DOMAINS = [
'microsoft.com',
'microsoftonline.com',
'office.com',
'office365.com',
'live.com',
'sharepoint.com',
] as const;
export type SessionExpirySource = 'cookie-expiry' | 'session-cookie-only' | 'no-relevant-cookies';
export type SessionExpiryConfidence = 'high' | 'medium' | 'unknown';
export type SessionValidationResult = 'valid' | 'invalid' | 'unknown';
export interface SessionCookieLike {
name?: string;
domain?: string;
expires?: number;
}
export interface SessionMetadata {
createdAt: string;
updatedAt: string;
expiresAt: string | null;
expirySource: SessionExpirySource;
expiryConfidence: SessionExpiryConfidence;
lastValidatedAt: string | null;
lastValidatedResult: SessionValidationResult;
}
export interface SessionMetadataSummary {
expiresAt: string | null;
expirySource: SessionExpirySource | null;
expiryConfidence: SessionExpiryConfidence | null;
lastValidatedAt: string | null;
lastValidatedResult: SessionValidationResult | null;
}
export function isPersistedSessionStateFile(fileName: string): boolean {
return fileName !== SESSION_METADATA_FILE_NAME;
}
function isRelevantCookie(cookie: SessionCookieLike): boolean {
const domain = (cookie.domain?.toLowerCase() ?? '').replace(/^\.+/, '');
return RELEVANT_COOKIE_DOMAINS.some(
(fragment) => domain === fragment || domain.endsWith(`.${fragment}`),
);
}
function toIsoDate(expiresUnixSeconds: number): string {
return new Date(expiresUnixSeconds * 1000).toISOString();
}
export function buildSessionMetadataFromCookies(
cookies: SessionCookieLike[],
options?: {
createdAt?: string;
now?: string;
},
): SessionMetadata {
const now = options?.now ?? new Date().toISOString();
const createdAt = options?.createdAt ?? now;
const relevantCookies = cookies.filter(isRelevantCookie);
const finiteExpiries = relevantCookies
.map((cookie) => cookie.expires)
.filter(
(expires): expires is number =>
typeof expires === 'number' && Number.isFinite(expires) && expires > 0,
);
const hasSessionCookies = relevantCookies.some(
(cookie) =>
typeof cookie.expires !== 'number' || !Number.isFinite(cookie.expires) || cookie.expires <= 0,
);
if (finiteExpiries.length > 0) {
return {
createdAt,
updatedAt: now,
expiresAt: toIsoDate(Math.min(...finiteExpiries)),
expirySource: 'cookie-expiry',
expiryConfidence: hasSessionCookies ? 'medium' : 'high',
lastValidatedAt: null,
lastValidatedResult: 'unknown',
};
}
return {
createdAt,
updatedAt: now,
expiresAt: null,
expirySource: hasSessionCookies ? 'session-cookie-only' : 'no-relevant-cookies',
expiryConfidence: 'unknown',
lastValidatedAt: null,
lastValidatedResult: 'unknown',
};
}
export function withValidationResult(
metadata: SessionMetadata,
options: {
valid: boolean;
now?: string;
},
): SessionMetadata {
const now = options.now ?? new Date().toISOString();
return {
...metadata,
updatedAt: now,
lastValidatedAt: now,
lastValidatedResult: options.valid ? 'valid' : 'invalid',
};
}
export function getSessionMetadataPath(stateDir: string): string {
return join(stateDir, SESSION_METADATA_FILE_NAME);
}
export function readSessionMetadata(stateDir: string): SessionMetadata | null {
const path = getSessionMetadataPath(stateDir);
if (!existsSync(path)) {
return null;
}
try {
return JSON.parse(readFileSync(path, 'utf8')) as SessionMetadata;
} catch {
return null;
}
}
export function writeSessionMetadata(stateDir: string, metadata: SessionMetadata): void {
writeFileSync(getSessionMetadataPath(stateDir), JSON.stringify(metadata, null, 2));
}
export function toSessionMetadataSummary(metadata: SessionMetadata | null): SessionMetadataSummary {
return {
expiresAt: metadata?.expiresAt ?? null,
expirySource: metadata?.expirySource ?? null,
expiryConfidence: metadata?.expiryConfidence ?? null,
lastValidatedAt: metadata?.lastValidatedAt ?? null,
lastValidatedResult: metadata?.lastValidatedResult ?? null,
};
}
|