All files / chart-parser/src node-file.ts

94.73% Statements 18/19
92.3% Branches 12/13
100% Functions 5/5
94.11% Lines 16/17

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                                  201x 103x   103x 201x     201x 201x   4x 2x 2x 2x   199x 24x   175x 5x   170x     170x    
/**
 * Node runtime adapter: reading a chart from disk. The parser itself (`./core/`) only ever sees text and bytes, so it
 * runs unchanged in the browser; this module is the one place that reaches for `node:fs`, and it does so lazily, so
 * importing the package in a browser bundle never loads it.
 */
import type { BeMusicJSON } from '@be-music/chart-source';
import { decodeUTF8Text } from './core/bms-text-decode.ts';
import { parseBMSBytes } from './core/bms-bytes.ts';
import { parseN2SBytes } from './core/n2s.ts';
import { parseBmson, parseChart } from './core/parser.ts';
import { parseLRXBytes, resolveLRXHeaderPath, resolveLRXMoviePath } from './core/lrx.ts';
 
export interface ParseChartFileOptions {
  signal?: AbortSignal;
}
 
export async function parseChartFile(filePath: string, options: ParseChartFileOptions = {}): Promise<BeMusicJSON> {
  const [{ readFile }, { dirname, extname, join }] = await Promise.all([
    import('node:fs/promises'),
    import('node:path'),
  ]);
  const buffer = await readFile(filePath, {
    signal: options.signal,
  });
  const extension = extname(filePath).toLowerCase();
  if (extension === '.lrs') {
    // An LRX chart reads its `.lrh` header and the BGA file it names from beside it, when they are there.
    const readSibling = (path: string) => readFile(path, { signal: options.signal }).catch(() => undefined);
    const lrh = await readSibling(resolveLRXHeaderPath(filePath));
    const lrm = await readSibling(join(dirname(filePath), resolveLRXMoviePath(buffer, lrh).replace(/\\/g, '/')));
    return parseLRXBytes({ lrs: buffer, ...(lrh ? { lrh } : {}), ...(lrm ? { lrm } : {}) });
  }
  if (extension === '.bmson') {
    return parseBmson(decodeUTF8Text(buffer));
  }
  if (extension === '.json') {
    return parseChart(decodeUTF8Text(buffer), 'json');
  }
  Iif (extension === '.n2s') {
    return parseN2SBytes(buffer);
  }
  return parseBMSBytes(buffer);
}