#!/bin/sh # Run an Emscripten-generated helper under Node during a cross build. # Node's fetch rejects absolute filesystem paths; generated Emscripten loaders # otherwise have a correct synchronous filesystem fallback, so adapt fetch to # a minimal Response for local .wasm files. set -eu script="$1" shift exec node -e ' const fs = require("fs"); const originalFetch = global.fetch; global.fetch = async (input, init) => { const value = typeof input === "string" ? input : input && input.url; if (typeof value === "string" && value.startsWith("/")) { const data = await fs.promises.readFile(value); return new Response(data, { status: 200 }); } return originalFetch(input, init); }; const moduleObject = require(process.argv[1]); // Emscripten starts instantiateWasm asynchronously. Keep the helper process // alive until its main function has actually run, otherwise Node can exit // after the fetch promise while the generated file is still pending. let ticks = 0; const waitForMain = setInterval(() => { if (moduleObject.calledRun) { const name = require("path").basename(process.argv[1]); const entry = name === "vtkH5detect.js" ? moduleObject._h5detect_entry : name === "vtkH5make_libsettings.js" ? moduleObject._h5make_libsettings_entry : null; if (entry) { try { entry(); } catch (error) { if (!error || error.name !== "ExitStatus") throw error; } } clearInterval(waitForMain); } else if (++ticks > 600) clearInterval(waitForMain); }, 10); ' "$script" "$@"