/** * Lexical facts about package-runner cache paths, shared by doctor's PATH * listing and setup's ownership of saved launchers so both read one layout. */ /** libnpmexec keeps the first 16 hex digits of the sha512 spec hash. */ const NPX_CACHE_KEY_HEX_DIGITS = 16; const HEX_DIGITS = /^[a-f0-9]+$/u; export const hexDigits = ( segment: string | undefined, width: number, ): boolean => segment !== undefined && segment.length === width && HEX_DIGITS.test(segment); export const segmentAt = ( segments: readonly string[], index: number, ): string | undefined => (index >= 0 ? segments[index] : undefined); /** * Split a path as written, before symlink resolution, because the cache marker * is on that spelling. Windows folds case and treats `\` as a separator. POSIX * keeps the spelling: a backslash can belong to a name. */ export const packageRunnerSegments = ( path: string, platform: NodeJS.Platform, ): readonly string[] => (platform === "win32" ? path.replace(/\\/gu, "/").toLowerCase() : path) .split("/") .filter((segment) => segment.length > 0); /** Whether the `node_modules` segment at `modules` sits in `_npx/<16 hex>`. */ export const npxCache = ( segments: readonly string[], modules: number, ): boolean => segmentAt(segments, modules - 2) === "_npx" && hexDigits(segmentAt(segments, modules - 1), NPX_CACHE_KEY_HEX_DIGITS);