|
| 1 | +import * as wasmBindings from "./wasm/tunshell_client_bg.js"; |
| 2 | + |
| 3 | +export interface TerminalEmulator { |
| 4 | + data: () => Promise<string>; |
| 5 | + resize: () => Promise<Uint16Array>; |
| 6 | + write: (data: string | Uint8Array) => Promise<void>; |
| 7 | + size: () => Uint16Array; |
| 8 | + clone: () => TerminalEmulator; |
| 9 | +} |
| 10 | + |
| 11 | +type WasmBindingModule = typeof import("./wasm/tunshell_client_bg.js"); |
| 12 | +type WasmExports = WebAssembly.Exports & { |
| 13 | + __wbindgen_start: () => void; |
| 14 | +}; |
| 15 | + |
| 16 | +export interface TunshellClientModule { |
| 17 | + BrowserConfig: WasmBindingModule["BrowserConfig"]; |
| 18 | + tunshell_init_client: WasmBindingModule["tunshell_init_client"]; |
| 19 | +} |
| 20 | + |
| 21 | +let modulePromise: Promise<TunshellClientModule> | undefined; |
| 22 | + |
| 23 | +export const loadTunshellClientModule = async (): Promise<TunshellClientModule> => { |
| 24 | + if (!modulePromise) { |
| 25 | + modulePromise = (async () => { |
| 26 | + const response = await fetch("/wasm/tunshell_client_bg.wasm"); |
| 27 | + if (!response.ok) { |
| 28 | + throw new Error(`Failed to load the tunshell wasm module: ${response.status}`); |
| 29 | + } |
| 30 | + |
| 31 | + const source = await response.arrayBuffer(); |
| 32 | + const { instance } = await WebAssembly.instantiate(source, { |
| 33 | + "./tunshell_client_bg.js": wasmBindings, |
| 34 | + }); |
| 35 | + |
| 36 | + const wasm = instance.exports as WasmExports; |
| 37 | + wasmBindings.__wbg_set_wasm(wasm); |
| 38 | + wasm.__wbindgen_start(); |
| 39 | + |
| 40 | + return { |
| 41 | + BrowserConfig: wasmBindings.BrowserConfig, |
| 42 | + tunshell_init_client: wasmBindings.tunshell_init_client, |
| 43 | + }; |
| 44 | + })(); |
| 45 | + } |
| 46 | + |
| 47 | + return modulePromise; |
| 48 | +}; |
0 commit comments