Add a packaged-client runtime boundary so the shared UI can talk to local, desktop, remote, and VS Code runtimes through the right transport instead of assuming one same-origin web server. Centralize OpenChamber-owned API access behind RuntimeAPIs, runtimeFetch, and runtime URL helpers, while keeping official OpenCode traffic on the SDK path. Support runtime switching, remote host selection, desktop client credentials, and headless connection links for pairing packaged clients with remote OpenChamber servers. Harden the new auth model by moving long-lived client tokens out of browser URLs, introducing short-lived scoped URL tokens for browser-owned transports, restricting URL-token access to explicit readable/realtime routes, and making client-token management session-scoped or self-scoped as appropriate. Update browser-owned assets and preview proxy flows to work with the split runtime model, including authenticated project icons, preview token propagation, CSP-safe preview bridge injection, and preview proxy auth that survives short-lived URL-token expiry. Tighten Electron security boundaries for packaged clients by gating privileged preload state to trusted origins and requiring explicit confirmation before connect deep-links import or switch remote runtimes. Also refresh agent guidance and project skills so future runtime/API, auth, preview, UI, CLI, settings, locale, and drag-to-reorder work follows the new architecture.
82 lines
2.7 KiB
TypeScript
82 lines
2.7 KiB
TypeScript
export const encodeBase64 = (bytes: Uint8Array): string => {
|
|
const CHUNK = 0x8000;
|
|
let binary = '';
|
|
for (let i = 0; i < bytes.length; i += CHUNK) {
|
|
binary += String.fromCharCode(...bytes.subarray(i, i + CHUNK));
|
|
}
|
|
return btoa(binary);
|
|
};
|
|
|
|
export const hasInitBody = (init: RequestInit | undefined): boolean => init?.body !== undefined && init.body !== null;
|
|
|
|
export const readBodyBytes = async (body: BodyInit): Promise<Uint8Array> => {
|
|
if (typeof body === 'string') {
|
|
return new TextEncoder().encode(body);
|
|
}
|
|
|
|
if (body instanceof URLSearchParams) {
|
|
return new TextEncoder().encode(body.toString());
|
|
}
|
|
|
|
if (body instanceof Blob) {
|
|
return new Uint8Array(await body.arrayBuffer());
|
|
}
|
|
|
|
if (body instanceof ArrayBuffer) {
|
|
return new Uint8Array(body);
|
|
}
|
|
|
|
if (ArrayBuffer.isView(body)) {
|
|
return new Uint8Array(body.buffer, body.byteOffset, body.byteLength);
|
|
}
|
|
|
|
if (body instanceof FormData) {
|
|
return new Uint8Array(await new Request('https://openchamber.local/body', { method: 'POST', body }).arrayBuffer());
|
|
}
|
|
|
|
throw new Error('Unsupported request body type');
|
|
};
|
|
|
|
export const readBodyText = async (body: BodyInit): Promise<string> => {
|
|
if (typeof body === 'string') return body;
|
|
if (body instanceof URLSearchParams) return body.toString();
|
|
if (body instanceof Blob) return await body.text();
|
|
return new TextDecoder().decode(await readBodyBytes(body));
|
|
};
|
|
|
|
export const extractBodyBase64 = async (input: RequestInfo | URL, init: RequestInit | undefined, method: string): Promise<string | undefined> => {
|
|
if (method === 'GET' || method === 'HEAD') return undefined;
|
|
|
|
if (input instanceof Request && !hasInitBody(init)) {
|
|
const cloned = input.clone();
|
|
const buffer = await cloned.arrayBuffer();
|
|
const bytes = new Uint8Array(buffer);
|
|
return bytes.length > 0 ? encodeBase64(bytes) : undefined;
|
|
}
|
|
|
|
const body = init?.body;
|
|
if (!body) return undefined;
|
|
|
|
const bytes = await readBodyBytes(body);
|
|
return bytes.length > 0 ? encodeBase64(bytes) : undefined;
|
|
};
|
|
|
|
export const extractBodyText = async (input: RequestInfo | URL, init: RequestInit | undefined, method: string): Promise<string> => {
|
|
if (method === 'GET' || method === 'HEAD') return '';
|
|
|
|
if (input instanceof Request && !hasInitBody(init)) {
|
|
const cloned = input.clone();
|
|
return await cloned.text();
|
|
}
|
|
|
|
const body = init?.body;
|
|
if (!body) return '';
|
|
|
|
return readBodyText(body);
|
|
};
|
|
|
|
export const extractJsonBody = async (input: RequestInfo | URL, init: RequestInit | undefined, method: string): Promise<Record<string, unknown>> => {
|
|
const bodyText = await extractBodyText(input, init, method);
|
|
return bodyText ? JSON.parse(bodyText) as Record<string, unknown> : {};
|
|
};
|