All files / shared-utils/src optional-node-module.ts

100% Statements 13/13
100% Branches 2/2
100% Functions 2/2
100% Lines 13/13

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52                  21x                                 27x 27x   3x         3x 3x 3x 3x 3x 5x 5x 5x               1x    
// Lazy-load Node built-ins so the module remains importable from a browser bundle (same pattern as
// `./log.ts`); the fallback only runs after the caller's own dynamic import already failed.
 
/**
 * Environment variable naming an extra base directory whose `node_modules` is consulted by
 * {@link loadOptionalNodeModule}. SEA binaries point it at the directory where their embedded modules were
 * extracted (see `player-tui/src/node/sea-embedded-modules.ts`); it propagates to worker threads through
 * the inherited environment.
 */
export const OPTIONAL_NODE_MODULE_DIR_ENV = 'BE_MUSIC_OPTIONAL_NODE_MODULE_DIR';
 
/**
 * Load an optional dependency that may be missing at runtime (native addons, large wasm packages).
 *
 * In a Node single executable application (SEA), `import()` of a bare specifier always fails with
 * `ERR_UNKNOWN_BUILTIN_MODULE` — the SEA module system never consults the filesystem. When the caller's
 * import fails, retry through `createRequire` anchored next to the executable, then the directory named by
 * {@link OPTIONAL_NODE_MODULE_DIR_ENV}, then the working directory, so a SEA binary can load these modules
 * from a `node_modules` directory shipped alongside it or extracted from its embedded assets.
 *
 * Returns `undefined` when the module cannot be loaded from any location.
 */
export async function loadOptionalNodeModule<T>(
  specifier: string,
  importModule: () => Promise<T>,
): Promise<T | undefined> {
  try {
    return await importModule();
  } catch {
    return await requireFromRuntimeDirectories<T>(specifier);
  }
}
 
async function requireFromRuntimeDirectories<T>(specifier: string): Promise<T | undefined> {
  try {
    const [{ createRequire }, { dirname, join }] = await Promise.all([import('node:module'), import('node:path')]);
    const envBaseDir = process.env[OPTIONAL_NODE_MODULE_DIR_ENV];
    const baseDirs = [dirname(process.execPath), ...(envBaseDir ? [envBaseDir] : []), process.cwd()];
    for (const baseDir of baseDirs) {
      try {
        const requireFromBase = createRequire(join(baseDir, '__optional-node-module-resolver__.cjs'));
        return requireFromBase(specifier) as T;
      } catch {
        // Keep probing the remaining base directories.
      }
    }
  } catch {
    // node:module / node:path are unavailable (non-Node runtime); report the module as missing.
  }
  return undefined;
}