Files
OpenMAIC/lib/export/inline-assets-shared.ts
T
wyuc a8c745a791 feat(generation): self-host interactive scene libraries with robust CDN URL mapping (#1972)
Interactive scenes load KaTeX, Three.js (with addons), Chart.js and the Tailwind v4 browser runtime from the deployment (/vendor/interactive, configurable via the build-time INTERACTIVE_ASSET_BASE_URL) instead of public CDNs. KaTeX is injected only into pages with detectable math, synchronously at the start of head (same-origin and immutable-cached), with the #1966 fallback when the asset fails; reprocessing refreshes platform-injected KaTeX tags against the current asset base. A parser-based mapper rewrites compatible public CDN URLs for these libraries to the hosted copies, drops integrity, leaves unknown URLs untouched and is idempotent; stored scenes are reprocessed on open and before export, and exports fetch hosted assets directly. Legacy Tailwind v3 Play CDN is not mapped. @openmaic/generation 0.3.24. Closes #1971.
2026-10-21 22:46:51 +08:00

35 lines
1.2 KiB
TypeScript

export interface InlineReport {
inlined: string[];
failed: { url: string; reason: string }[];
}
export interface InlineOptions {
/** Deployment origin for resolving self-hosted interactive libraries outside a browser. */
baseUrl?: string;
fetchImpl?: typeof fetch;
maxAssetBytes?: number;
fetcher?: FetchAsset;
/** Keep original remote import-map entries as an online fallback. Default true. */
keepImportmapFallbacks?: boolean;
/**
* Leave audio and video sources (`<video src>`, `<audio src>` and their
* `<source src>` children) unfetched, for exports that carry no media.
*/
skipMedia?: boolean;
}
export type FetchAsset = (
url: string,
) => Promise<{ bytes: Uint8Array; contentType: string } | null>;
/** Encode bytes as a data: URI. */
export function toDataUri(bytes: Uint8Array, contentType: string): string {
let binary = '';
const CHUNK = 0x8000;
for (let i = 0; i < bytes.length; i += CHUNK) {
binary += String.fromCharCode.apply(null, bytes.subarray(i, i + CHUNK) as unknown as number[]);
}
const b64 = typeof btoa !== 'undefined' ? btoa(binary) : Buffer.from(bytes).toString('base64');
return `data:${contentType};base64,${b64}`;
}