()
| 83 | } |
| 84 | |
| 85 | async function findFromCache(): Promise<CacheLookupResult> { |
| 86 | // 1) Puppeteer's managed cache — where `npx @puppeteer/browsers install |
| 87 | // chrome-headless-shell` lands, and where `puppeteer install` from a project |
| 88 | // depending on full `puppeteer` (not `puppeteer-core`) lands. The engine's |
| 89 | // `resolveHeadlessShellPath` reads from here and selects newest-version- |
| 90 | // first; the CLI must match that semantic or it will silently hand the |
| 91 | // engine an older binary than the engine itself would pick. |
| 92 | // |
| 93 | // We intentionally check puppeteer BEFORE the hyperframes-managed cache: |
| 94 | // the HF cache is pinned to `CHROME_VERSION` (above) which lags behind |
| 95 | // upstream Chrome by many releases. If a user installed chrome-headless-shell |
| 96 | // separately (via `@puppeteer/browsers install`) we want to use that |
| 97 | // newer binary, not the pinned-stale fallback. |
| 98 | const fromPuppeteer = findFromPuppeteerCache(); |
| 99 | if (fromPuppeteer) { |
| 100 | return { result: fromPuppeteer }; |
| 101 | } |
| 102 | |
| 103 | // 2) Hyperframes-managed cache (populated by `ensureBrowser` below as a |
| 104 | // download-of-last-resort). This is the fallback path: only reached when |
| 105 | // no puppeteer-cache binary exists. |
| 106 | if (existsSync(CACHE_DIR)) { |
| 107 | const { Browser, getInstalledBrowsers } = await loadPuppeteerBrowsers(); |
| 108 | // A corrupt cache (stub file where a browser dir is expected, malformed |
| 109 | // metadata) makes getInstalledBrowsers throw. Treat that as "no cached |
| 110 | // browser" so resolution falls through to system/download instead of |
| 111 | // crashing every caller. |
| 112 | let installed: Awaited<ReturnType<typeof getInstalledBrowsers>>; |
| 113 | try { |
| 114 | installed = await getInstalledBrowsers({ cacheDir: CACHE_DIR }); |
| 115 | } catch (err) { |
| 116 | const code = (err as NodeJS.ErrnoException | undefined)?.code; |
| 117 | const suffix = code ? ` (${code})` : ""; |
| 118 | console.warn( |
| 119 | `[hyperframes] Browser cache read failed${suffix}: ${normalizeErrorMessage(err)}. Falling back to system Chrome or a fresh download.`, |
| 120 | ); |
| 121 | installed = []; |
| 122 | } |
| 123 | const match = installed.find((b) => b.browser === Browser.CHROMEHEADLESSSHELL); |
| 124 | if (match && existsSync(match.executablePath)) { |
| 125 | return { result: { executablePath: match.executablePath, source: "cache" } }; |
| 126 | } |
| 127 | if (match) { |
| 128 | return { staleHyperframesCachePath: match.executablePath }; |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | return {}; |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Parse a puppeteer-cache version directory name (`linux-148.0.7778.97`, |
no test coverage detected