Merge main and fix lazy image export loading

This commit is contained in:
Bohdan Triapitsyn
2026-08-02 20:17:37 +03:00
1656 changed files with 159739 additions and 72292 deletions
@@ -0,0 +1,28 @@
import { describe, expect, test } from 'bun:test';
import {
getPreviewTargetErrorCode,
getPreviewTargetRecoveryAction,
PREVIEW_TARGET_ERROR_HEADER,
} from './proxy-response';
describe('preview proxy response classification', () => {
test('recognizes proxy-owned target failures', () => {
for (const code of ['missing', 'expired', 'invalid-token'] as const) {
const headers = new Headers({ [PREVIEW_TARGET_ERROR_HEADER]: code });
expect(getPreviewTargetErrorCode(headers)).toBe(code);
}
});
test('does not classify ordinary upstream responses as target failures', () => {
expect(getPreviewTargetErrorCode(new Headers())).toBeNull();
expect(getPreviewTargetErrorCode(new Headers({ [PREVIEW_TARGET_ERROR_HEADER]: 'unknown' }))).toBeNull();
expect(getPreviewTargetRecoveryAction(new Headers(), false)).toBe('none');
});
test('bounds automatic target recovery to one registration retry', () => {
const headers = new Headers({ [PREVIEW_TARGET_ERROR_HEADER]: 'expired' });
expect(getPreviewTargetRecoveryAction(headers, false)).toBe('retry-registration');
expect(getPreviewTargetRecoveryAction(headers, true)).toBe('stop-retrying');
});
});
@@ -0,0 +1,18 @@
export const PREVIEW_TARGET_ERROR_HEADER = 'x-openchamber-preview-target-error';
type PreviewTargetErrorCode = 'missing' | 'expired' | 'invalid-token';
export const getPreviewTargetErrorCode = (headers: Pick<Headers, 'get'>): PreviewTargetErrorCode | null => {
const value = headers.get(PREVIEW_TARGET_ERROR_HEADER);
return value === 'missing' || value === 'expired' || value === 'invalid-token'
? value
: null;
};
export const getPreviewTargetRecoveryAction = (
headers: Pick<Headers, 'get'>,
recoveryAttempted: boolean,
): 'none' | 'retry-registration' | 'stop-retrying' => {
if (!getPreviewTargetErrorCode(headers)) return 'none';
return recoveryAttempted ? 'stop-retrying' : 'retry-registration';
};
@@ -196,8 +196,9 @@ const TRANSPARENT_IMAGE_PLACEHOLDER = 'data:image/png;base64,iVBORw0KGgoAAAANSUh
// switches, but intentionally does NOT survive a full page reload: the server
// holds the target map in memory and the auth cookie is HttpOnly + scoped to
// the proxy id, so a stale persisted entry would 404 after a server restart.
// Entries are evicted on registration error (refetched) or when the upstream
// returns 403 (cookie expired) / 404 (target unknown) at iframe load time.
// Entries are evicted on registration error or when the preview proxy marks a
// target as missing, expired, or unauthorized. Upstream 4xx responses are not
// cache failures.
export type CachedProxyTarget = { proxyBasePath: string; previewToken?: string; expiresAt: number };
export const previewProxyTargetCache = new Map<string, CachedProxyTarget>();
const previewProxyTargetRequests = new Map<string, Promise<CachedProxyTarget | null>>();