* Resolve the path to a WASM file in a Node.js or Bun environment. * Works for both ESM and CJS builds of the SDK.
(wasmFileName: string)
| 140 | * Works for both ESM and CJS builds of the SDK. |
| 141 | */ |
| 142 | function resolveWasmPath(wasmFileName: string): string { |
| 143 | const customWasmDirPath = getWasmDir() |
| 144 | if (customWasmDirPath) { |
| 145 | return path.join(customWasmDirPath, wasmFileName) |
| 146 | } |
| 147 | |
| 148 | // Try environment variable override |
| 149 | const envWasmDir = process.env.CODEBUFF_WASM_DIR |
| 150 | if (envWasmDir) { |
| 151 | return path.join(envWasmDir, wasmFileName) |
| 152 | } |
| 153 | |
| 154 | // Get the directory of this module |
| 155 | const moduleDir = (() => { |
| 156 | const dirname = getDirnameDynamically() |
| 157 | if (typeof dirname !== 'undefined') { |
| 158 | return dirname |
| 159 | } |
| 160 | // For ESM builds, we can't reliably get the module directory in all environments |
| 161 | // So we fall back to process.cwd() which works for our use case |
| 162 | return process.cwd() |
| 163 | })() |
| 164 | |
| 165 | // For bundled SDK: WASM files are in a shared wasm directory |
| 166 | const possiblePaths = [ |
| 167 | // Shared WASM directory (new approach to avoid duplication) |
| 168 | path.join(moduleDir, '..', 'wasm', wasmFileName), |
| 169 | // WASM files in the same directory as this module (for bundled builds) |
| 170 | path.join(moduleDir, 'wasm', wasmFileName), |
| 171 | // Development scenario - shared wasm directory in SDK dist |
| 172 | path.join(process.cwd(), 'dist', 'wasm', wasmFileName), |
| 173 | ] |
| 174 | |
| 175 | // Try each path and return the first one that exists (we'll fallback to package resolution if none work) |
| 176 | for (const wasmPath of possiblePaths) { |
| 177 | try { |
| 178 | // Don't actually check file existence here, let the Language.load() call handle it |
| 179 | // and fall back to package resolution if it fails |
| 180 | return wasmPath |
| 181 | } catch { |
| 182 | continue |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | // Default fallback |
| 187 | return possiblePaths[0] |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * Fallback: try to resolve from the original package for development |
no test coverage detected