Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01VDJVpPiGiWjJP1yVvvGhzQ
This commit is contained in:
+16
-15
@@ -1,4 +1,4 @@
|
||||
// 登入狀態存檔:COST_MODEL_DATA_DIR/sessions.json(新檔案,不進 Git)。部署重新啟動後,大家不用重新登入。
|
||||
// 登入狀態存檔:sessions.json(存在 COST_MODEL_DATA_DIR/leadboard.db 的 documents 表;STORAGE_BACKEND=json 時是同名檔案,不進 Git)。部署重新啟動後,大家不用重新登入。
|
||||
// - 檔案裡只存 session id 的 SHA-256,不存瀏覽器 cookie 的原始值;檔案權限 600。
|
||||
// - 只存登入需要的欄位;HR 名單、權限、成本等暫存(*Cache)不存。
|
||||
// - 過期的不載入、不存。
|
||||
@@ -6,8 +6,7 @@
|
||||
// 不寫進檔案、程式碼或 Git。沒有密鑰時不存檔(只放記憶體,重新部署要重新登入)。
|
||||
// - 密鑰換掉或檔案被改過:解不開的那筆直接略過(該使用者重新登入即可)。
|
||||
import { createCipheriv, createDecipheriv, createHash, hkdfSync, randomBytes } from 'node:crypto';
|
||||
import { mkdirSync, readFileSync, renameSync, writeFileSync } from 'node:fs';
|
||||
import { dirname } from 'node:path';
|
||||
import { bindFile } from './storage.js';
|
||||
|
||||
const KEPT = ['claims', 'expiresAt', 'idToken', 'accessToken', 'refreshToken', 'accessExpiresAt'];
|
||||
const hash = (id) => createHash('sha256').update(String(id)).digest('hex');
|
||||
@@ -35,11 +34,12 @@ function open(key, aad, sealed) {
|
||||
return JSON.parse(Buffer.concat([decipher.update(raw.subarray(28)), decipher.final()]).toString('utf8'));
|
||||
}
|
||||
|
||||
export function createSessionStore(path, { now = () => Date.now(), saveDelay = 500, secret } = {}) {
|
||||
export function createSessionStore(source, { now = () => Date.now(), saveDelay = 500, secret } = {}) {
|
||||
const key = sessionKey(secret);
|
||||
if (path && !key) {
|
||||
let file = bindFile(source);
|
||||
if (file && !key) {
|
||||
console.error('session store: no runtime secret, sessions are kept in memory only');
|
||||
path = null;
|
||||
file = null;
|
||||
}
|
||||
const map = new Map();
|
||||
let timer = null;
|
||||
@@ -59,29 +59,30 @@ export function createSessionStore(path, { now = () => Date.now(), saveDelay = 5
|
||||
// 同步寫檔:關機時也能寫完。內容沒變就不寫。
|
||||
function flush() {
|
||||
if (timer) { clearTimeout(timer); timer = null; }
|
||||
if (!path) return;
|
||||
if (!file) return;
|
||||
const records = current();
|
||||
const signature = createHash('sha256').update(JSON.stringify(records)).digest('hex');
|
||||
if (signature === lastWritten) return;
|
||||
const sessions = Object.fromEntries(Object.entries(records).map(([id, record]) => [id, { expiresAt: record.expiresAt, data: seal(key, id, record) }]));
|
||||
mkdirSync(dirname(path), { recursive: true });
|
||||
const temporary = `${path}.${process.pid}.tmp`;
|
||||
writeFileSync(temporary, `${JSON.stringify({ version: 2, sessions })}\n`, { mode: 0o600 });
|
||||
renameSync(temporary, path);
|
||||
file.write(`${JSON.stringify({ version: 2, sessions })}\n`);
|
||||
lastWritten = signature;
|
||||
}
|
||||
|
||||
function schedule() {
|
||||
if (!path || timer) return;
|
||||
if (!file || timer) return;
|
||||
timer = setTimeout(() => { timer = null; try { flush(); } catch (error) { console.error('session save failed', error.message); } }, saveDelay);
|
||||
timer.unref?.();
|
||||
}
|
||||
|
||||
function load() {
|
||||
if (!path) return 0;
|
||||
if (!file) return 0;
|
||||
let data;
|
||||
try { data = JSON.parse(readFileSync(path, 'utf8')); } catch (error) {
|
||||
if (error.code !== 'ENOENT') console.error('session load failed', error.message);
|
||||
try {
|
||||
const text = file.read();
|
||||
if (text === null) return 0;
|
||||
data = JSON.parse(text);
|
||||
} catch (error) {
|
||||
console.error('session load failed', error.message);
|
||||
return 0;
|
||||
}
|
||||
const time = now();
|
||||
|
||||
Reference in New Issue
Block a user