| 97 | } |
| 98 | |
| 99 | private static async loadFromPath(path: string): Promise<Ghostty> { |
| 100 | let wasmBytes: ArrayBuffer | undefined; |
| 101 | |
| 102 | // Try Bun.file first (for Bun environments) |
| 103 | if (typeof Bun !== 'undefined' && typeof Bun.file === 'function') { |
| 104 | try { |
| 105 | const file = Bun.file(path); |
| 106 | if (await file.exists()) { |
| 107 | wasmBytes = await file.arrayBuffer(); |
| 108 | } |
| 109 | } catch { |
| 110 | // Bun.file failed, try next method |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | // Try Node.js fs module if Bun.file didn't work |
| 115 | if (!wasmBytes) { |
| 116 | try { |
| 117 | const fs = await import('fs/promises'); |
| 118 | const buffer = await fs.readFile(path); |
| 119 | wasmBytes = buffer.buffer.slice(buffer.byteOffset, buffer.byteOffset + buffer.byteLength); |
| 120 | } catch { |
| 121 | // fs failed, try fetch |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | // Fall back to fetch (for browser environments) |
| 126 | if (!wasmBytes) { |
| 127 | const response = await fetch(path); |
| 128 | if (!response.ok) { |
| 129 | throw new Error(`Failed to fetch WASM: ${response.status} ${response.statusText}`); |
| 130 | } |
| 131 | wasmBytes = await response.arrayBuffer(); |
| 132 | if (wasmBytes.byteLength === 0) { |
| 133 | throw new Error(`WASM file is empty (0 bytes). Check path: ${path}`); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | if (!wasmBytes) { |
| 138 | throw new Error(`Could not load WASM from path: ${path}`); |
| 139 | } |
| 140 | |
| 141 | const wasmModule = await WebAssembly.compile(wasmBytes); |
| 142 | const wasmInstance = await WebAssembly.instantiate(wasmModule, { |
| 143 | env: { |
| 144 | log: (ptr: number, len: number) => { |
| 145 | const bytes = new Uint8Array( |
| 146 | (wasmInstance.exports as GhosttyWasmExports).memory.buffer, |
| 147 | ptr, |
| 148 | len |
| 149 | ); |
| 150 | console.log('[ghostty-vt]', new TextDecoder().decode(bytes)); |
| 151 | }, |
| 152 | }, |
| 153 | }); |
| 154 | return new Ghostty(wasmInstance); |
| 155 | } |
| 156 | } |