@@ -2,6 +2,7 @@ import fs from 'fs'
22import EmscriptenModule from '../itk-wasm-emscripten-module.js'
33import { pathToFileURL } from 'url'
44import { ZSTDDecoder } from '@thewtex/zstddec'
5+ import pthreadSupportAvailable from '../pthread-support-available.js'
56
67const zstdDecoder = new ZSTDDecoder ( )
78await zstdDecoder . init ( )
@@ -19,9 +20,49 @@ async function loadEmscriptenModuleNode (
1920 if ( modulePath . endsWith ( '.wasm.zst' ) ) {
2021 modulePrefix = modulePath . substring ( 0 , modulePath . length - 9 )
2122 }
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+
2566 const fullModulePath = pathToFileURL ( `${ modulePrefix } .js` ) . href
2667 const result = await import (
2768 /* webpackIgnore: true */ /* @vite -ignore */ fullModulePath
0 commit comments