All files cache-key.ts

100% Statements 13/13
100% Branches 8/8
100% Functions 6/6
100% Lines 11/11

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                                                                12x 6x   6x 2x   4x 4x 4x     2x                     2x 2x 2x      
// @fitness-ignore-file unbounded-memory -- reads a single language manifest (go.mod / Cargo.toml / pom.xml / pyproject.toml); bounded by standard project metadata
/**
 * Shared config-fingerprint helpers for the tree-sitter adapters'
 * `cacheKey`.
 *
 * `hashConfig(configPathAbs)` is byte-identical in graph-go / graph-java /
 * graph-rust: it returns the literal `no-config` when no anchor exists,
 * `missing:<path>` / `unreadable:<path>` sentinels on fs failure, and the
 * first 16 hex chars of the sha256 of the manifest content otherwise. Per
 * contract invariant I-6 it is a pure function of the config content.
 *
 * `makeConfigCacheKey({ prefix })` returns the trivial
 * `cacheKey(input) => `${prefix}-${hashConfig(...)}`` used by go/java/rust
 * (with prefixes `go-` / `java-` / `rs-`). Python keeps its own
 * `cache-key.ts` but imports `hashConfig` from here and layers its
 * `requires-python` extraction on top (DEC-4).
 */
 
import { createHash } from 'node:crypto';
import { existsSync, readFileSync } from 'node:fs';
 
import type { CacheKeyInput } from '@opensip-tools/graph';
 
/**
 * Fingerprint a language config file's content.
 *
 *   - `undefined` / empty path → `'no-config'`
 *   - path does not exist      → `'missing:<path>'`
 *   - read fails               → `'unreadable:<path>'`
 *   - otherwise                → first 16 hex of sha256(content)
 */
export function hashConfig(configPathAbs: string | undefined): string {
  if (configPathAbs === undefined || configPathAbs.length === 0) {
    return 'no-config';
  }
  if (!existsSync(configPathAbs)) {
    return `missing:${configPathAbs}`;
  }
  try {
    const content = readFileSync(configPathAbs, 'utf8');
    return createHash('sha256').update(content).digest('hex').slice(0, 16);
  } catch {
    /* v8 ignore next */
    return `unreadable:${configPathAbs}`;
  }
}
 
/**
 * Builds a `cacheKey` that emits `${prefix}-${hashConfig(configPathAbs)}`.
 * Per I-8 the prefix must be distinct per adapter (`go-`, `java-`, `rs-`).
 */
export function makeConfigCacheKey(options: {
  readonly prefix: string;
}): (input: CacheKeyInput) => string {
  const { prefix } = options;
  return function cacheKey(input: CacheKeyInput): string {
    return `${prefix}-${hashConfig(input.configPathAbs)}`;
  };
}