All files / player-browser/src/collection file-lookup.ts

85.71% Statements 6/7
100% Branches 6/6
80% Functions 4/5
83.33% Lines 5/6

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 87 88 89                                                                                          6x                     31x                       168x               4x 2x                      
import {
  asLoadedFileEntryBytes,
  findCaseInsensitiveMapPath,
  isMaliciousAssetPath,
  loadFileEntryBytes,
  lookupCaseInsensitiveMapEntry,
} from '@be-music/shared-utils/core';
import { DeferredDroppedFile } from './dropped-file.ts';
import type { BrowserAssetEntry } from './types.ts';
 
export { isMaliciousAssetPath } from '@be-music/shared-utils/core';
 
/**
 * Shared case-insensitive lookup helpers for the file maps used by the song collection (`BrowserAssetSource.files`)
 * and the LR2 skin asset map (`LR2Skin.files`).
 *
 * Map values can be either:
 *
 * - `Uint8Array` — bytes already in memory (small files / images / charts / skins); returned directly to the
 *   synchronous caller.
 * - `File` — a lazy `File` reference for large audio assets that shouldn't be slurped into memory up-front. Callers
 *   that need bytes await {@link loadAssetBytes}; callers that are fine with either branch take the union.
 *
 * Why this exists: BMS / bmson chart files reference WAV / BMP / image assets by an arbitrary string (e.g. `#WAV01
 * kick.wav`). On case- sensitive filesystems / the `webkitRelativePath` map produced by a directory drop, the chart's
 * casing has to match the file's casing exactly — but real-world archives routinely mix `KICK.WAV` / `kick.WAV` /
 * `Kick.wav` etc. Same story for LR2 skins referencing `LR2files\Theme\LR2\Result\parts.tga` while the actual filename
 * on disk is `Parts.TGA`.
 *
 * Strategy: a per-source lazy-built `Map<lowerKey, originalKey>` index that lets the resolver fall back to a
 * case-insensitive match after the case-sensitive one misses. Keyed on the source map identity via `WeakMap` so the
 * cache vanishes the moment the source itself does (and we never re-scan the same source twice).
 */
 
/**
 * Returns the original key in `files` matching `candidate`, comparing exact-match first and falling back to a
 * case-insensitive match. Returns `undefined` when no key matches under either comparison.
 *
 * The two-stage approach matters: callers that pass in a key they know to be exact (e.g. iterating `files.keys()`
 * themselves) get the fast path with no Map allocation; only on a miss do we touch the lazy index.
 */
export function findCaseInsensitivePath(
  files: ReadonlyMap<string, BrowserAssetEntry>,
  candidate: string,
): string | undefined {
  return findCaseInsensitiveMapPath(files, candidate, { rejectCandidate: isMaliciousAssetPath });
}
 
/**
 * Convenience wrapper that returns the bytes (or `undefined`) directly, for call sites that don't need the original
 * key.
 */
export function lookupBytesCaseInsensitive(
  files: ReadonlyMap<string, BrowserAssetEntry>,
  candidate: string,
): BrowserAssetEntry | undefined {
  return lookupCaseInsensitiveMapEntry(files, candidate, { rejectCandidate: isMaliciousAssetPath });
}
 
/**
 * Forces a {@link BrowserAssetEntry} into a `Uint8Array`. If the entry is a `File` (a lazy audio reference) this
 * performs an `arrayBuffer()` read on demand. The result is NOT cached on the source map — callers that need to cache
 * (e.g. the gameplay sample decoder) should hold onto the returned buffer themselves.
 *
 * Returns `undefined` when the entry itself is undefined so the common pattern
 * `loadAssetBytes(lookupBytesCaseInsensitive(...))` stays readable without an extra null-guard.
 */
export async function loadAssetBytes(entry: BrowserAssetEntry | undefined): Promise<Uint8Array | undefined> {
  return loadFileEntryBytes(entry);
}
 
/**
 * The entry as something a media element can play without reading it: bytes already in memory as they are, and a lazy
 * file as its `File` (opening a deferred one), which stays on disk until the browser reads it.
 */
export async function loadAssetFile(entry: BrowserAssetEntry | undefined): Promise<Blob | Uint8Array | undefined> {
  if (entry === undefined || entry instanceof Uint8Array) return entry;
  return entry instanceof DeferredDroppedFile ? entry.getFile() : entry;
}
 
/**
 * Sync variant — returns the entry's bytes when they're already resolved, `undefined` when it's a lazy `File` reference
 * (the caller has to use {@link loadAssetBytes} to read those). Used by image / texture loaders that can't reasonably
 * go async at the call site.
 */
export function asLoadedBytes(entry: BrowserAssetEntry | undefined): Uint8Array | undefined {
  return asLoadedFileEntryBytes(entry);
}