@@ -2,6 +2,7 @@ import fs from 'fs'
2
2
import EmscriptenModule from '../itk-wasm-emscripten-module.js'
3
3
import { pathToFileURL } from 'url'
4
4
import { ZSTDDecoder } from '@thewtex/zstddec'
5
+ import pthreadSupportAvailable from '../pthread-support-available.js'
5
6
6
7
const zstdDecoder = new ZSTDDecoder ( )
7
8
await zstdDecoder . init ( )
@@ -19,9 +20,49 @@ async function loadEmscriptenModuleNode (
19
20
if ( modulePath . endsWith ( '.wasm.zst' ) ) {
20
21
modulePrefix = modulePath . substring ( 0 , modulePath . length - 9 )
21
22
}
22
- const compressedWasmBinaryPath = `${ modulePrefix } .wasm.zst`
23
- const compressedWasmBinary = fs . readFileSync ( compressedWasmBinaryPath )
24
- const wasmBinary = zstdDecoder . decode ( new Uint8Array ( compressedWasmBinary ) )
23
+
24
+ // Check for pthread support and use the appropriate WASM file
25
+ const hasPthreadSupport = pthreadSupportAvailable ( )
26
+ let wasmFileName = `${ modulePrefix } .wasm.zst`
27
+ let wasmBinary : Uint8Array
28
+
29
+ if ( hasPthreadSupport ) {
30
+ const threadsWasmPath = `${ modulePrefix } .threads.wasm.zst`
31
+ if ( fs . existsSync ( threadsWasmPath ) ) {
32
+ wasmFileName = threadsWasmPath
33
+ const compressedWasmBinary = fs . readFileSync ( wasmFileName )
34
+ wasmBinary = zstdDecoder . decode ( new Uint8Array ( compressedWasmBinary ) )
35
+ } else {
36
+ // Fall back to checking for compressed non-threaded version
37
+ if ( fs . existsSync ( wasmFileName ) ) {
38
+ const compressedWasmBinary = fs . readFileSync ( wasmFileName )
39
+ wasmBinary = zstdDecoder . decode ( new Uint8Array ( compressedWasmBinary ) )
40
+ } else {
41
+ // Fall back to uncompressed WASM file
42
+ const uncompressedWasmPath = `${ modulePrefix } .wasm`
43
+ if ( fs . existsSync ( uncompressedWasmPath ) ) {
44
+ wasmBinary = fs . readFileSync ( uncompressedWasmPath )
45
+ } else {
46
+ throw new Error ( `No WASM file found for module: ${ modulePrefix } ` )
47
+ }
48
+ }
49
+ }
50
+ } else {
51
+ // Check for compressed non-threaded version first
52
+ if ( fs . existsSync ( wasmFileName ) ) {
53
+ const compressedWasmBinary = fs . readFileSync ( wasmFileName )
54
+ wasmBinary = zstdDecoder . decode ( new Uint8Array ( compressedWasmBinary ) )
55
+ } else {
56
+ // Fall back to uncompressed WASM file
57
+ const uncompressedWasmPath = `${ modulePrefix } .wasm`
58
+ if ( fs . existsSync ( uncompressedWasmPath ) ) {
59
+ wasmBinary = fs . readFileSync ( uncompressedWasmPath )
60
+ } else {
61
+ throw new Error ( `No WASM file found for module: ${ modulePrefix } ` )
62
+ }
63
+ }
64
+ }
65
+
25
66
const fullModulePath = pathToFileURL ( `${ modulePrefix } .js` ) . href
26
67
const result = await import (
27
68
/* webpackIgnore: true */ /* @vite -ignore */ fullModulePath
0 commit comments