All files / chart-source/src bmson-extra-fields.ts

100% Statements 42/42
100% Branches 38/38
100% Functions 5/5
100% Lines 32/32

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 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106          613x                                                                                               613x       244454x 821x 821x         156537x 134242x 280526x       402453x 270256x 270256x       613x     3897x 2705x 2496x 2492x 2490x   2490x 1030x 1030x 1534x 1534x 1024x   520x   1460x 1460x 1542x 1542x 1535x   1453x   1973x 1973x    
/** Opaque JSON annotations: data only, never executable format semantics. */
export type JSONValue = null | boolean | number | string | JSONValue[] | { [key: string]: JSONValue };
export type JSONFields = { [key: string]: JSONValue };
export type BmsonFieldScope = keyof typeof BMSON_FIELDS;
 
const BMSON_FIELDS = {
  root: [
    'version',
    'info',
    'lines',
    'resolution',
    'bpm_events',
    'stop_events',
    'scroll_events',
    'sound_channels',
    'key_channels',
    'mine_channels',
    'bga',
  ],
  info: [
    'title',
    'subtitle',
    'artist',
    'genre',
    'subartists',
    'chart_name',
    'level',
    'init_bpm',
    'resolution',
    'mode_hint',
    'judge_rank',
    'total',
    'ln_type',
    'back_image',
    'eyecatch_image',
    'title_image',
    'banner_image',
    'preview_music',
  ],
  channel: ['name', 'notes'],
  note: ['x', 'y', 'l', 'c', 't', 'up'],
  keyNote: ['x', 'y'],
  mineNote: ['x', 'y', 'damage'],
  line: ['y'],
  bpm: ['y', 'bpm'],
  stop: ['y', 'duration'],
  scroll: ['y', 'rate'],
  bga: ['bga_header', 'bga_events', 'layer_events', 'poor_events', 'bga_sequence'],
  bgaHeader: ['id', 'name'],
  bgaEvent: ['y', 'id', 'id_set', 'idSet', 'condition', 'interval'],
  bgaSequence: ['id', 'sequence'],
  bgaFrame: ['time', 'id'],
} as const;
const INVALID = Symbol('invalid-json');
 
/** Deep copy a nonempty JSON object; reject non-JSON values and cycles without retaining aliases. */
export function cloneJSONFields(input: unknown): JSONFields | undefined {
  if (!isRecord(input) || Object.keys(input).length === 0) return undefined;
  const copy = cloneValue(input, new Set<object>());
  return copy === INVALID ? undefined : (copy as JSONFields);
}
 
/** Retain only fields without supported semantics at this external bmson node; known fields can never be overridden. */
export function collectBmsonExtraFields(input: unknown, scope: BmsonFieldScope): JSONFields | undefined {
  if (!isRecord(input)) return undefined;
  const known: readonly string[] = BMSON_FIELDS[scope];
  return cloneJSONFields(Object.fromEntries(Object.entries(input).filter(([key]) => !known.includes(key))));
}
 
function isRecord(input: unknown): input is Record<string, unknown> {
  if (!input || typeof input !== 'object' || Array.isArray(input)) return false;
  const prototype = Object.getPrototypeOf(input);
  return prototype === Object.prototype || prototype === null;
}
 
/** Deeper opaque values are invalid, so a hostile document cannot exhaust the stack of the recursive copy. */
const MAX_JSON_DEPTH = 256;
 
function cloneValue(input: unknown, ancestors: Set<object>): JSONValue | typeof INVALID {
  if (input === null || typeof input === 'boolean' || typeof input === 'string') return input;
  if (typeof input === 'number') return Number.isFinite(input) ? input : INVALID;
  if (typeof input !== 'object' || (!Array.isArray(input) && !isRecord(input)) || ancestors.has(input)) return INVALID;
  if (ancestors.size >= MAX_JSON_DEPTH) return INVALID;
  ancestors.add(input);
  let result: JSONValue;
  if (Array.isArray(input)) {
    const values: JSONValue[] = [];
    for (const item of input) {
      const value = cloneValue(item, ancestors);
      if (value === INVALID) return INVALID;
      values.push(value);
    }
    result = values;
  } else {
    const entries: Array<[string, JSONValue]> = [];
    for (const [key, item] of Object.entries(input)) {
      const value = cloneValue(item, ancestors);
      if (value === INVALID) return INVALID;
      entries.push([key, value]);
    }
    result = Object.fromEntries(entries);
  }
  ancestors.delete(input);
  return result;
}