Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

1.9 KB
Raw Blame
import { loadCacheWasm } from '../wasm-module';
import {
  assertMatchingCacheWasmBuilds,
  type CacheWasmBuildInfo,
  type CacheWasmBuildInfoSource,
} from './wasm-build-compatibility';

type BrowserTestCacheWasmModule = CacheWasmBuildInfoSource & {
  default(input?: { module_or_path?: unknown }): Promise<unknown>;
  browserTestMakeNamespaceIncompatible(scope: string): Promise<void>;
  browserTestCorruptQueuePayload(scope: string): Promise<void>;
};

let modulePromise:
  | Promise<{
      module: BrowserTestCacheWasmModule;
      wasmUrl: string;
      buildInfo: CacheWasmBuildInfo;
    }>
  | undefined;

/** Loads and verifies the test/production artifact pair without touching OPFS. */
export function loadBrowserTestCacheWasm(): Promise<{
  module: BrowserTestCacheWasmModule;
  wasmUrl: string;
  buildInfo: CacheWasmBuildInfo;
}> {
  if (modulePromise) return modulePromise;
  const initialization = (async () => {
    const glueUrl = new URL(
      '../../wasm-browser-test/cache_wasm_browser_test_hooks.js',
      import.meta.url
    ).href;
    const wasmUrl = new URL(
      '../../wasm-browser-test/cache_wasm_browser_test_hooks_bg.wasm',
      import.meta.url
    ).href;
    const module = (await import(
      /* @vite-ignore */ glueUrl
    )) as BrowserTestCacheWasmModule;
    const response = await fetch(wasmUrl);
    if (!response.ok) {
      throw new Error(`browser-test WASM returned HTTP ${response.status}`);
    }
    const compiled = await WebAssembly.compile(await response.arrayBuffer());
    await module.default({ module_or_path: compiled });
    const production = await loadCacheWasm();
    const buildInfo = assertMatchingCacheWasmBuilds(production, module);
    return { module, wasmUrl, buildInfo };
  })();
  modulePromise = initialization;
  void initialization.catch(() => {
    if (modulePromise === initialization) modulePromise = undefined;
  });
  return initialization;
}