/** * Desktop WebGPU for the command-line scripts, via the optional `webgpu` * package (prebuilt Google Dawn). Installs Dawn under the globals the * host code expects (navigator.gpu, GPUBufferUsage, ...) so everything * under src/ runs here unchanged. */ export const errMsg = (e: unknown): string => e instanceof Error ? e.message : String(e); export async function installWebGpu(): Promise { const specifier = 'webgpu'; let mod: { create: (flags: string[]) => GPU; globals: Record }; try { mod = await import(specifier); } catch (e) { const detail = errMsg(e); if (/Cannot find (package|module) '?webgpu'?/.test(detail)) { throw new Error( 'desktop WebGPU needs the optional `webgpu` package (prebuilt Google Dawn):\n' + ' npm install webgpu', ); } throw new Error(`the \`webgpu\` package is installed but did not load:\n ${detail}`); } Object.assign(globalThis, mod.globals); const dawnFlags = process.env.DAWN_FLAGS?.split(';').filter(Boolean) ?? []; Object.defineProperty(globalThis, 'navigator', { value: { gpu: mod.create(dawnFlags) }, configurable: true, writable: true, }); const { version } = await import(`${specifier}/package.json`, { with: { type: 'json' }, }).then( (m) => m.default as { version: string }, () => ({ version: '?' }), ); return `node-webgpu ${version} (Google Dawn)`; }