-const compressedWrapper = `// Gzip-compressed single-file ESM wrapper around wasm-bindgen output.\n// - Inlines WASM as base64 gzip payload to reduce bundle size.\n// - Decompresses off-thread when possible to minimize main-thread work.\n// - Preserves async init() API shape.\n\nimport rawInit, { initSync as rawInitSync } from './raw/wasm_sdk.no_url.js';\n\nexport * from './raw/wasm_sdk.no_url.js';\nexport { initSync } from './raw/wasm_sdk.no_url.js';\n\nconst __WASM_COMPRESSED_BASE64 = '${wasmGzipBase64}';\nconst __WASM_COMPRESSION = 'gzip';\nconst isNode = typeof window === 'undefined' && typeof process !== 'undefined' && !!(process.versions && process.versions.node);\n\nfunction __decodeBase64(source) {\n if (typeof Buffer !== 'undefined' && typeof Buffer.from === 'function') {\n return Buffer.from(source, 'base64');\n }\n const atobFn = (typeof atob === 'function') ? atob : (s) => globalThis.atob(s);\n const bin = atobFn(source);\n const len = bin.length;\n const bytes = new Uint8Array(len);\n for (let i = 0; i < len; i++) bytes[i] = bin.charCodeAt(i);\n return bytes;\n}\n\nasync function __decompress(bytes) {\n if (!__WASM_COMPRESSION) {\n return bytes;\n }\n if (isNode) {\n const { gunzipSync } = await import(/* webpackIgnore: true */ 'node:zlib');\n const out = gunzipSync(bytes);\n return out instanceof Uint8Array\n ? out\n : new Uint8Array(out.buffer, out.byteOffset, out.byteLength);\n }\n if (typeof Blob === 'function' && typeof Response === 'function' && typeof DecompressionStream === 'function') {\n const res = new Response(\n new Blob([bytes]).stream().pipeThrough(new DecompressionStream(__WASM_COMPRESSION))\n );\n const buf = await res.arrayBuffer();\n return new Uint8Array(buf);\n }\n throw new Error('Gzip decompression not supported in this environment.');\n}\n\nasync function __wasmBytes(options = {}) {\n const { decompress = true } = options;\n if (!__WASM_COMPRESSION) {\n throw new Error('Compression metadata missing.');\n }\n const compressed = __decodeBase64(__WASM_COMPRESSED_BASE64);\n if (!decompress) {\n return compressed;\n }\n return __decompress(compressed);\n}\n\nfunction __supportsWorker() {\n return typeof Worker !== 'undefined' && typeof Blob !== 'undefined' && typeof URL !== 'undefined';\n}\n\nasync function __compileInWorker(compressedBytes) {\n const bytes = compressedBytes instanceof Uint8Array ? compressedBytes : new Uint8Array(compressedBytes);\n if (!__supportsWorker()) {\n const decompressed = await __decompress(bytes);\n return WebAssembly.compile(decompressed);\n }\n const src = "self.onmessage=async(event)=>{try{const data=event.data||{};let bytes=data.compressed;const compression=data.compression||null;if(!(bytes instanceof Uint8Array)){bytes=bytes?new Uint8Array(bytes):new Uint8Array();}if(compression){if(typeof Blob==='function'&&typeof Response==='function'&&typeof DecompressionStream==='function'){const res=new Response(new Blob([bytes]).stream().pipeThrough(new DecompressionStream(compression)));const buf=await res.arrayBuffer();bytes=new Uint8Array(buf);}else{throw new Error('DecompressionStream not available');}}const mod=await WebAssembly.compile(bytes);self.postMessage({ok:1,mod});}catch(err){self.postMessage({ok:0,err:String(err)})}}";\n const blob = new Blob([src], { type: 'application/javascript' });\n const url = URL.createObjectURL(blob);\n return new Promise((resolve, reject) => {\n const worker = new Worker(url);\n const cleanup = () => {\n URL.revokeObjectURL(url);\n worker.terminate();\n };\n worker.onmessage = (ev) => {\n const d = ev.data || {};\n if (d.ok && d.mod) {\n cleanup();\n resolve(d.mod);\n } else {\n cleanup();\n reject(new Error(d.err || 'Worker failed to compile WASM.'));\n }\n };\n worker.onerror = (err) => {\n cleanup();\n reject(err instanceof Error ? err : new Error(String(err && err.message ? err.message : err)));\n };\n try {\n worker.postMessage({ compressed: bytes, compression: __WASM_COMPRESSION });\n } catch (postErr) {\n cleanup();\n reject(postErr);\n }\n });\n}\n\nexport default async function init(moduleOrPath) {\n if (isNode) {\n if (typeof moduleOrPath === 'undefined') {\n const bytes = await __wasmBytes();\n return rawInitSync({ module: bytes });\n }\n return rawInit(moduleOrPath);\n }\n if (typeof moduleOrPath === 'undefined') {\n const compressedBytes = await __wasmBytes({ decompress: false });\n let mod;\n try {\n mod = await __compileInWorker(compressedBytes);\n } catch (_) {\n const decompressed = await __decompress(compressedBytes);\n mod = await WebAssembly.compile(decompressed);\n }\n return rawInit({ module_or_path: mod });\n }\n return rawInit(moduleOrPath);\n}\n`
0 commit comments