All files / player-terminal/src/node sea-embedded-modules.ts

11.76% Statements 4/34
5.88% Branches 1/17
25% Functions 1/4
11.76% Lines 4/34

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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86                  3x 3x                                 3x 3x                                                                                                                  
import { existsSync } from 'node:fs';
import { mkdir, rename, rm, writeFile } from 'node:fs/promises';
import { homedir } from 'node:os';
import { dirname, join } from 'node:path';
import { getAsset } from 'node:sea';
import { OPTIONAL_NODE_MODULE_DIR_ENV } from '@be-music/shared-utils/optional-node-module';
import { isSEARuntime } from './sea-worker.ts';
 
// Asset names kept in sync with scripts/build-sea.ts.
const MANIFEST_ASSET_NAME = 'embedded-node-modules.json';
const MODULE_ASSET_PREFIX = 'embedded-node-modules/';
 
interface EmbeddedModulesManifest {
  cacheKey: string;
  files: string[];
}
 
/**
 * Extract the optional node modules embedded as SEA assets (native addons cannot be loaded from memory, so
 * they must exist on disk) into a content-addressed cache directory under `~/.be-music`, and expose that
 * directory to `loadOptionalNodeModule` through {@link OPTIONAL_NODE_MODULE_DIR_ENV}, which worker threads
 * inherit through the environment.
 *
 * Returns the base directory containing the extracted `node_modules`, or `undefined` when not running as a
 * SEA or when the binary has no embedded modules.
 */
export async function ensureSEAEmbeddedNodeModules(): Promise<string | undefined> {
  Eif (!isSEARuntime()) {
    return undefined;
  }
  const manifest = readEmbeddedModulesManifest();
  if (!manifest) {
    return undefined;
  }
  const baseDir = join(homedir(), '.be-music', 'sea-embedded-modules', manifest.cacheKey);
  if (!existsSync(completeMarkerPath(baseDir))) {
    await extractEmbeddedModules(manifest, baseDir);
  }
  process.env[OPTIONAL_NODE_MODULE_DIR_ENV] ??= baseDir;
  return baseDir;
}
 
function completeMarkerPath(baseDir: string): string {
  return join(baseDir, '.complete');
}
 
function readEmbeddedModulesManifest(): EmbeddedModulesManifest | undefined {
  try {
    const manifest = JSON.parse(getAsset(MANIFEST_ASSET_NAME, 'utf8')) as EmbeddedModulesManifest;
    if (typeof manifest.cacheKey !== 'string' || manifest.cacheKey.length === 0 || !Array.isArray(manifest.files)) {
      return undefined;
    }
    return manifest;
  } catch {
    // The binary was built without embedded modules.
    return undefined;
  }
}
 
async function extractEmbeddedModules(manifest: EmbeddedModulesManifest, baseDir: string): Promise<void> {
  // A base directory without the completion marker is a leftover from an interrupted extraction.
  if (existsSync(baseDir)) {
    await rm(baseDir, { recursive: true, force: true });
  }
  // Stage into a per-process directory and rename at the end so a concurrently launched player never sees a
  // half-written cache; losing the rename race to another process is fine because the content is identical.
  const stagingDir = `${baseDir}.staging-${process.pid}`;
  try {
    for (const relativePath of manifest.files) {
      if (relativePath.split('/').includes('..')) {
        continue;
      }
      const filePath = join(stagingDir, 'node_modules', ...relativePath.split('/'));
      await mkdir(dirname(filePath), { recursive: true });
      await writeFile(filePath, Buffer.from(getAsset(`${MODULE_ASSET_PREFIX}${relativePath}`)));
    }
    await writeFile(completeMarkerPath(stagingDir), '');
    await rename(stagingDir, baseDir);
  } catch (error) {
    await rm(stagingDir, { recursive: true, force: true });
    if (!existsSync(completeMarkerPath(baseDir))) {
      throw error;
    }
  }
}