|
1 | 1 | import ModuleFactory from './encrypt_web.js'; |
| 2 | +import wasmAsset from './encrypt_web.wasm'; |
2 | 3 |
|
3 | 4 | let ModulePromise = null; |
4 | 5 |
|
| 6 | +function getRuntimeBaseUrl() { |
| 7 | + if (typeof self !== 'undefined' && self.location && self.location.href) { |
| 8 | + return self.location.href; |
| 9 | + } |
| 10 | + if (typeof document !== 'undefined' && document.baseURI) { |
| 11 | + return document.baseURI; |
| 12 | + } |
| 13 | + return null; |
| 14 | +} |
| 15 | + |
| 16 | +function toAbsoluteAssetUrl(filename) { |
| 17 | + if (typeof filename !== 'string' || filename.length === 0) { |
| 18 | + throw new TypeError('filename must be a non-empty string'); |
| 19 | + } |
| 20 | + |
| 21 | + const baseUrl = getRuntimeBaseUrl(); |
| 22 | + if (!baseUrl) { |
| 23 | + return filename; |
| 24 | + } |
| 25 | + |
| 26 | + try { |
| 27 | + return new URL(filename, baseUrl).href; |
| 28 | + } catch { |
| 29 | + return filename; |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +function getWasmModuleCandidate() { |
| 34 | + if (typeof WebAssembly === 'undefined' || typeof WebAssembly.Module === 'undefined') { |
| 35 | + return null; |
| 36 | + } |
| 37 | + |
| 38 | + if (wasmAsset instanceof WebAssembly.Module) { |
| 39 | + return wasmAsset; |
| 40 | + } |
| 41 | + |
| 42 | + if (wasmAsset && wasmAsset.default instanceof WebAssembly.Module) { |
| 43 | + return wasmAsset.default; |
| 44 | + } |
| 45 | + |
| 46 | + try { |
| 47 | + if (wasmAsset instanceof Uint8Array || wasmAsset instanceof ArrayBuffer) { |
| 48 | + return new WebAssembly.Module(wasmAsset); |
| 49 | + } |
| 50 | + |
| 51 | + if (wasmAsset && (wasmAsset.default instanceof Uint8Array || wasmAsset.default instanceof ArrayBuffer)) { |
| 52 | + return new WebAssembly.Module(wasmAsset.default); |
| 53 | + } |
| 54 | + } catch { |
| 55 | + // Fall through to URL-based loading below. |
| 56 | + } |
| 57 | + |
| 58 | + return null; |
| 59 | +} |
| 60 | + |
| 61 | +function getWasmUrlCandidate() { |
| 62 | + if (typeof wasmAsset === 'string' && wasmAsset.length > 0) { |
| 63 | + return wasmAsset; |
| 64 | + } |
| 65 | + |
| 66 | + if (wasmAsset && typeof wasmAsset.default === 'string' && wasmAsset.default.length > 0) { |
| 67 | + return wasmAsset.default; |
| 68 | + } |
| 69 | + |
| 70 | + if (wasmAsset && typeof wasmAsset.href === 'string' && wasmAsset.href.length > 0) { |
| 71 | + return wasmAsset.href; |
| 72 | + } |
| 73 | + |
| 74 | + return null; |
| 75 | +} |
| 76 | + |
5 | 77 | function getModule() { |
6 | 78 | if (!ModulePromise) { |
7 | | - const wasmUrl = new URL('encrypt_web.wasm', import.meta.url).href; |
8 | | - ModulePromise = ModuleFactory({ |
9 | | - locateFile: (path) => { |
10 | | - if (path.endsWith('.wasm')) { |
| 79 | + const moduleOptions = {}; |
| 80 | + const wasmModule = getWasmModuleCandidate(); |
| 81 | + const wasmUrl = getWasmUrlCandidate(); |
| 82 | + |
| 83 | + if (wasmModule) { |
| 84 | + moduleOptions.instantiateWasm = (imports, receiveInstance) => { |
| 85 | + const instance = new WebAssembly.Instance(wasmModule, imports); |
| 86 | + receiveInstance(instance, wasmModule); |
| 87 | + return instance.exports; |
| 88 | + }; |
| 89 | + } |
| 90 | + |
| 91 | + if (wasmUrl) { |
| 92 | + moduleOptions.locateFile = (filename) => { |
| 93 | + if (filename.endsWith('.wasm')) { |
11 | 94 | return wasmUrl; |
12 | 95 | } |
13 | | - return path; |
14 | | - } |
15 | | - }); |
| 96 | + |
| 97 | + return toAbsoluteAssetUrl(filename); |
| 98 | + }; |
| 99 | + } else { |
| 100 | + moduleOptions.locateFile = (filename) => { |
| 101 | + return toAbsoluteAssetUrl(filename); |
| 102 | + }; |
| 103 | + } |
| 104 | + |
| 105 | + ModulePromise = ModuleFactory(moduleOptions); |
16 | 106 | } |
17 | 107 | return ModulePromise; |
18 | 108 | } |
|
0 commit comments