Files
openmaic/OpenMAIC/lib/export/inline-assets-shared.ts
2026-08-16 14:58:47 +08:00

28 lines
900 B
TypeScript

export interface InlineReport {
inlined: string[];
failed: { url: string; reason: string }[];
}
export interface InlineOptions {
fetchImpl?: typeof fetch;
maxAssetBytes?: number;
fetcher?: FetchAsset;
/** Keep original remote import-map entries as an online fallback. Default true. */
keepImportmapFallbacks?: 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}`;
}