|
| 1 | +/** |
| 2 | + * WebAssembly-based UTF-8 string processing using js-string-builtins. |
| 3 | + * |
| 4 | + * Environment variables: |
| 5 | + * - MSGPACK_WASM=force: Force wasm mode, throw error if wasm fails to load |
| 6 | + * - MSGPACK_WASM=never: Disable wasm, always use pure JS |
| 7 | + * |
| 8 | + * Three-tier fallback: |
| 9 | + * 1. Native js-string-builtins (Chrome 130+, Firefox 134+) |
| 10 | + * 2. Wasm + polyfill (older browsers with WebAssembly) |
| 11 | + * 3. Pure JS (no WebAssembly support) |
| 12 | + */ |
| 13 | + |
| 14 | +import { wasmBinary } from "./utf8-wasm-binary.ts"; |
| 15 | + |
| 16 | +// Check environment variable for wasm mode |
| 17 | +declare const process: { env?: Record<string, string | undefined> } | undefined; |
| 18 | + |
| 19 | +function getWasmMode(): "force" | "never" | "auto" { |
| 20 | + try { |
| 21 | + if (process?.env) { |
| 22 | + const mode = process.env["MSGPACK_WASM"]; |
| 23 | + if (mode) { |
| 24 | + switch (mode.toLowerCase()) { |
| 25 | + case "force": |
| 26 | + return "force"; |
| 27 | + case "never": |
| 28 | + return "never"; |
| 29 | + default: |
| 30 | + return "auto"; |
| 31 | + } |
| 32 | + } |
| 33 | + } |
| 34 | + } catch { |
| 35 | + // process may not be defined in browser |
| 36 | + } |
| 37 | + return "auto"; |
| 38 | +} |
| 39 | + |
| 40 | +const WASM_MODE = getWasmMode(); |
| 41 | + |
| 42 | +interface WasmExports { |
| 43 | + memory: WebAssembly.Memory; |
| 44 | + utf8Count(str: string): number; |
| 45 | + utf8Encode(str: string, offset: number): number; |
| 46 | + utf8Decode(offset: number, length: number): string; |
| 47 | +} |
| 48 | + |
| 49 | +let wasmInstance: WasmExports | null = null; |
| 50 | +let wasmInitError: Error | null = null; |
| 51 | + |
| 52 | +function base64ToBytes(base64: string): Uint8Array { |
| 53 | + if (typeof atob === "function") { |
| 54 | + const binary = atob(base64); |
| 55 | + const bytes = new Uint8Array(binary.length); |
| 56 | + for (let i = 0; i < binary.length; i++) { |
| 57 | + bytes[i] = binary.charCodeAt(i); |
| 58 | + } |
| 59 | + return bytes; |
| 60 | + } |
| 61 | + // Node.js fallback |
| 62 | + return new Uint8Array(Buffer.from(base64, "base64")); |
| 63 | +} |
| 64 | + |
| 65 | +// Polyfill for js-string-builtins (used when native builtins unavailable) |
| 66 | +const jsStringPolyfill = { |
| 67 | + // eslint-disable-next-line @typescript-eslint/naming-convention |
| 68 | + "wasm:js-string": { |
| 69 | + length: (s: string) => s.length, |
| 70 | + charCodeAt: (s: string, i: number) => s.charCodeAt(i), |
| 71 | + fromCharCode: (code: number) => String.fromCharCode(code), |
| 72 | + concat: (a: string, b: string) => a + b, |
| 73 | + }, |
| 74 | +}; |
| 75 | + |
| 76 | +function tryInitWasm(): void { |
| 77 | + if (wasmInstance !== null || wasmInitError !== null) { |
| 78 | + return; // Already initialized or failed |
| 79 | + } |
| 80 | + |
| 81 | + if (WASM_MODE === "never") { |
| 82 | + wasmInitError = new Error("MSGPACK_WASM=never: wasm disabled"); |
| 83 | + return; |
| 84 | + } |
| 85 | + |
| 86 | + try { |
| 87 | + if (typeof WebAssembly === "undefined") { |
| 88 | + throw new Error("WebAssembly not supported"); |
| 89 | + } |
| 90 | + |
| 91 | + const bytes = base64ToBytes(wasmBinary); |
| 92 | + |
| 93 | + // Try with builtins option (native support) |
| 94 | + // If builtins not supported, option is ignored and polyfill is used |
| 95 | + |
| 96 | + |
| 97 | + const module: WebAssembly.Module = new (WebAssembly.Module as any)(bytes, { builtins: ["js-string"] }); |
| 98 | + |
| 99 | + |
| 100 | + const instance = new (WebAssembly.Instance)(module, jsStringPolyfill); |
| 101 | + wasmInstance = instance.exports as unknown as WasmExports; |
| 102 | + } catch (e) { |
| 103 | + wasmInitError = e instanceof Error ? e : new Error(String(e)); |
| 104 | + |
| 105 | + if (WASM_MODE === "force") { |
| 106 | + throw new Error(`MSGPACK_WASM=force but wasm failed to load: ${wasmInitError.message}`); |
| 107 | + } |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +// Initialize on module load |
| 112 | +tryInitWasm(); |
| 113 | + |
| 114 | +/** |
| 115 | + * Whether wasm is available and initialized. |
| 116 | + */ |
| 117 | +// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition |
| 118 | +export const WASM_AVAILABLE = (wasmInstance !== null); |
| 119 | + |
| 120 | +/** |
| 121 | + * Get the wasm initialization error, if any. |
| 122 | + */ |
| 123 | +export function getWasmError(): Error | null { |
| 124 | + return wasmInitError; |
| 125 | +} |
| 126 | + |
| 127 | +/** |
| 128 | + * Get the raw wasm exports for advanced usage. |
| 129 | + */ |
| 130 | +export function getWasmExports(): WasmExports | null { |
| 131 | + return wasmInstance; |
| 132 | +} |
| 133 | + |
| 134 | +/** |
| 135 | + * Count UTF-8 byte length of a string. |
| 136 | + */ |
| 137 | +export function utf8CountWasm(str: string): number { |
| 138 | + if (wasmInstance === null) { |
| 139 | + throw new Error("wasm not initialized"); |
| 140 | + } |
| 141 | + return wasmInstance.utf8Count(str); |
| 142 | +} |
| 143 | + |
| 144 | +/** |
| 145 | + * Encode string to UTF-8 bytes in the provided buffer. |
| 146 | + * Returns the number of bytes written. |
| 147 | + */ |
| 148 | +export function utf8EncodeWasm(str: string, output: Uint8Array, outputOffset: number): number { |
| 149 | + if (wasmInstance === null) { |
| 150 | + throw new Error("wasm not initialized"); |
| 151 | + } |
| 152 | + |
| 153 | + // Ensure wasm memory is large enough |
| 154 | + const byteLength = wasmInstance.utf8Count(str); |
| 155 | + const requiredPages = Math.ceil((outputOffset + byteLength) / 65536); |
| 156 | + const currentPages = wasmInstance.memory.buffer.byteLength / 65536; |
| 157 | + |
| 158 | + if (requiredPages > currentPages) { |
| 159 | + wasmInstance.memory.grow(requiredPages - currentPages); |
| 160 | + } |
| 161 | + |
| 162 | + // Encode to wasm memory |
| 163 | + const bytesWritten = wasmInstance.utf8Encode(str, 0); |
| 164 | + |
| 165 | + // Copy from wasm memory to output buffer |
| 166 | + const wasmBytes = new Uint8Array(wasmInstance.memory.buffer, 0, bytesWritten); |
| 167 | + output.set(wasmBytes, outputOffset); |
| 168 | + |
| 169 | + return bytesWritten; |
| 170 | +} |
| 171 | + |
| 172 | +/** |
| 173 | + * Decode UTF-8 bytes to string. |
| 174 | + */ |
| 175 | +export function utf8DecodeWasm(bytes: Uint8Array, inputOffset: number, byteLength: number): string { |
| 176 | + if (wasmInstance === null) { |
| 177 | + throw new Error("wasm not initialized"); |
| 178 | + } |
| 179 | + |
| 180 | + // Ensure wasm memory is large enough |
| 181 | + const requiredPages = Math.ceil(byteLength / 65536); |
| 182 | + const currentPages = wasmInstance.memory.buffer.byteLength / 65536; |
| 183 | + |
| 184 | + if (requiredPages > currentPages) { |
| 185 | + wasmInstance.memory.grow(requiredPages - currentPages); |
| 186 | + } |
| 187 | + |
| 188 | + // Copy bytes to wasm memory |
| 189 | + const wasmBytes = new Uint8Array(wasmInstance.memory.buffer, 0, byteLength); |
| 190 | + wasmBytes.set(bytes.subarray(inputOffset, inputOffset + byteLength)); |
| 191 | + |
| 192 | + // Decode from wasm memory |
| 193 | + const result = wasmInstance.utf8Decode(0, byteLength); |
| 194 | + |
| 195 | + // Remove leading NUL character (artifact of wasm implementation) |
| 196 | + return result.length > 0 && result.charCodeAt(0) === 0 ? result.slice(1) : result; |
| 197 | +} |
0 commit comments