From 4b642e57e5e108a9374d3d474bbc40d5ee8256bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?walkdoggo=2Eyeh=5F=E8=91=89=E5=82=B3=E8=80=80?= Date: Mon, 23 Nov 2026 08:04:03 +0000 Subject: [PATCH] =?UTF-8?q?=E7=99=BB=E5=85=A5=E7=8B=80=E6=85=8B=E5=AD=98?= =?UTF-8?q?=E6=AA=94=E6=94=B9=E6=8E=A5=E5=85=B1=E7=94=A8=E5=AD=98=E6=AA=94?= =?UTF-8?q?=E4=BB=8B=E9=9D=A2=EF=BC=88storage.js=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_01VDJVpPiGiWjJP1yVvvGhzQ --- session-store.js | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/session-store.js b/session-store.js index ab51fd9..25fdbbb 100644 --- a/session-store.js +++ b/session-store.js @@ -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();