Skip to content

Commit 39fb4e6

Browse files
mpiannucciclaude
andcommitted
Fix Node.js compatibility for metadata inspector
- Add Node.js-specific WASM initialization to handle file:// URL limitation - Node.js fetch() cannot load file:// URLs, but Bun can - Solution: Use fs.readFileSync() to load WASM file in Node.js - Both Node.js and Bun now work correctly with the metadata inspector 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 0c1192d commit 39fb4e6

File tree

1 file changed

+28
-6
lines changed

1 file changed

+28
-6
lines changed

readap-wasm/examples/metadata-inspector.js

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,38 @@
1212
* - Basic dataset structure
1313
*/
1414

15+
import { readFileSync } from 'fs';
16+
import { fileURLToPath } from 'url';
17+
import { dirname, join } from 'path';
1518
import init, { ImmutableDataset, universalFetchText } from '../pkg/readap_wasm.js';
1619

1720
// Runtime detection
1821
const isNode = typeof process !== 'undefined' && process.versions && process.versions.node;
1922
const isBun = typeof Bun !== 'undefined';
2023

24+
/**
25+
* Initialize WASM with Node.js compatibility
26+
* Node.js cannot fetch file:// URLs, so we need to read the WASM file manually
27+
*/
28+
async function initializeWasm() {
29+
if (!isNode) {
30+
// For non-Node.js environments (Bun, browser), use default initialization
31+
await init();
32+
return;
33+
}
34+
35+
try {
36+
// For Node.js, manually load the WASM file
37+
const scriptDir = dirname(fileURLToPath(import.meta.url));
38+
const wasmPath = join(scriptDir, '..', 'pkg', 'readap_wasm_bg.wasm');
39+
const wasmBytes = readFileSync(wasmPath);
40+
await init(wasmBytes);
41+
} catch (error) {
42+
console.error('Failed to load WASM file. Make sure you are running from the examples directory.');
43+
throw error;
44+
}
45+
}
46+
2147
async function main() {
2248
// Check command line arguments
2349
if (process.argv.length < 3) {
@@ -37,16 +63,12 @@ async function main() {
3763
}
3864

3965
try {
40-
// Initialize WebAssembly
41-
await init();
66+
// Initialize WebAssembly with Node.js compatibility
67+
await initializeWasm();
4268

4369
console.log(`Runtime: ${isBun ? 'Bun' : isNode ? 'Node.js' : 'Unknown'}`);
4470
console.log(`Dataset URL: ${url}`);
4571

46-
if (isNode) {
47-
console.log('Note: Some OpenDAP servers may have networking issues with Node.js');
48-
console.log(' If you encounter fetch errors, try using Bun instead');
49-
}
5072
console.log('');
5173

5274
// Load dataset with metadata

0 commit comments

Comments
 (0)