31 lines
1.1 KiB
JavaScript
31 lines
1.1 KiB
JavaScript
|
|
import { resolveCanonicalUserId } from './canonical-user.mjs';
|
|||
|
|
import { getActiveSnapshot } from './snapshot.mjs';
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* Session bootstrap payload for /auth/me (RFC: 5~10KB snapshot at session start).
|
|||
|
|
*
|
|||
|
|
* @param {import('mysql2/promise').Pool | null | undefined} pool
|
|||
|
|
* @param {string} userId
|
|||
|
|
* @param {string} [projection]
|
|||
|
|
*/
|
|||
|
|
export async function loadSessionUserModelSnapshot(pool, userId, projection = 'default') {
|
|||
|
|
if (!pool?.query || !userId) return null;
|
|||
|
|
const canonicalUserId = resolveCanonicalUserId(userId);
|
|||
|
|
const snap = await getActiveSnapshot(pool, canonicalUserId, projection);
|
|||
|
|
if (!snap) return null;
|
|||
|
|
return {
|
|||
|
|
snapshot_id: snap.snapshot_id,
|
|||
|
|
user_id: snap.user_id,
|
|||
|
|
projection: snap.projection,
|
|||
|
|
profile_version: snap.profile_version,
|
|||
|
|
fast_revision: snap.fast_revision,
|
|||
|
|
content_hash: snap.content_hash,
|
|||
|
|
byte_size: snap.byte_size,
|
|||
|
|
stale_after_sec: snap.stale_after_sec,
|
|||
|
|
loaded_at_hint: 'session_start',
|
|||
|
|
core: snap.core,
|
|||
|
|
meta: snap.meta,
|
|||
|
|
...(canonicalUserId !== userId ? { canonical_user_id: canonicalUserId } : {}),
|
|||
|
|
};
|
|||
|
|
}
|