feat: simplify asset serving and enhance bootstrap response handling

This commit is contained in:
shuaiplus
2026-03-19 00:52:58 +08:00
parent facd0ea5f7
commit 5ff322d809
5 changed files with 43 additions and 43 deletions
+34 -11
View File
@@ -92,6 +92,35 @@ function readWindowBootstrap(): WebBootstrapResponse {
return raw && typeof raw === 'object' ? raw : {};
}
function normalizeBootstrapResponse(boot: WebBootstrapResponse): Pick<InitialAppBootstrapState, 'defaultKdfIterations' | 'jwtWarning'> {
const defaultKdfIterations = Number(boot.defaultKdfIterations || 600000);
const jwtUnsafeReason = boot.jwtUnsafeReason || null;
const jwtWarning = jwtUnsafeReason
? {
reason: jwtUnsafeReason,
minLength: Number(boot.jwtSecretMinLength || 32),
}
: null;
return {
defaultKdfIterations,
jwtWarning,
};
}
async function fetchBootstrapConfig(): Promise<WebBootstrapResponse> {
try {
const resp = await fetch('/api/web-bootstrap', {
method: 'GET',
headers: { Accept: 'application/json' },
});
if (!resp.ok) return {};
return ((await resp.json()) as WebBootstrapResponse) || {};
} catch {
return {};
}
}
interface AccessTokenClaims {
sub?: string;
email?: string;
@@ -129,15 +158,7 @@ function buildTransientProfile(token: TokenSuccess, email: string): Profile {
}
export function readInitialAppBootstrapState(): InitialAppBootstrapState {
const boot = readWindowBootstrap();
const defaultKdfIterations = Number(boot.defaultKdfIterations || 600000);
const jwtUnsafeReason = boot.jwtUnsafeReason || null;
const jwtWarning = jwtUnsafeReason
? {
reason: jwtUnsafeReason,
minLength: Number(boot.jwtSecretMinLength || 32),
}
: null;
const { defaultKdfIterations, jwtWarning } = normalizeBootstrapResponse(readWindowBootstrap());
const session = loadSession();
const hasInviteCode = !!readInviteCodeFromUrl();
@@ -150,8 +171,10 @@ export function readInitialAppBootstrapState(): InitialAppBootstrapState {
}
export async function bootstrapAppSession(initial: InitialAppBootstrapState = readInitialAppBootstrapState()): Promise<BootstrapAppResult> {
const defaultKdfIterations = initial.defaultKdfIterations;
const jwtWarning = initial.jwtWarning;
const remoteBoot = await fetchBootstrapConfig();
const normalizedBoot = normalizeBootstrapResponse(remoteBoot);
const defaultKdfIterations = normalizedBoot.defaultKdfIterations || initial.defaultKdfIterations;
const jwtWarning = normalizedBoot.jwtWarning ?? initial.jwtWarning;
if (jwtWarning) {
return {