/** * Copyright (c) Meta Platforms, Inc. and affiliates. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * * @format * @oncall memory_lab */ import fs from 'fs'; import path from 'path'; import {getSnapshotMetadata} from './heap-state.js'; import {resolveRoundDir} from './round-index.js'; import {lastRunJsonRead} from './run-manifest.js'; export const DEFAULT_CDP_PORT = 9222; export const CDP_PORT_DESCRIPTION = "Chrome remote-debugging port. Default: MEMLAB_CDP_PORT, else the CDP port the round's run.json records (`run_dir`, else the run this server read last, else the active snapshot's), else 9222. The reply names the page and where the port came from."; export const CDP_RUN_DIR_DESCRIPTION = "A leak-hunt round's directory, or a round name in the round index: attach to the browser its run.json records (`config.cdp_port`, `config.url_filter`)."; export interface CdpTarget { port: number; urlFilter: string; /** Where the port came from, printed with every live result. */ source: string; } function roundConfig( file: string, ): {port: number | null; urlFilter: string | null} | null { try { const raw = JSON.parse(fs.readFileSync(file, 'utf8')) as { config?: {cdp_port?: unknown; url_filter?: unknown}; }; const port = raw.config?.cdp_port; const filter = raw.config?.url_filter; return { port: typeof port === 'number' && Number.isInteger(port) && port > 0 ? port : null, urlFilter: typeof filter === 'string' && filter !== '' ? filter : null, }; } catch { return null; } } /** run.json of the round the active snapshot belongs to, if any. */ function activeRoundJson(): string | null { const file = getSnapshotMetadata()?.filePath; if (file == null) return null; const dir = path.dirname(file); for (const d of [dir, path.dirname(dir)]) { const p = path.join(d, 'run.json'); if (fs.existsSync(p)) return p; } return null; } const short = (file: string) => path.join(path.basename(path.dirname(file)), path.basename(file)); /** * The browser a live tool attaches to: an explicit `port`, else * `MEMLAB_CDP_PORT`, else the CDP port the round's run.json records (the * `run_dir` passed, the run this server read last, or the active snapshot's), * else 9222. 9222 is often some other Chrome, so the source is always shown. * A round's `url_filter` applies when its port is the one used and the * caller passed none (`url_filter: ""` means any page). */ export function cdpTarget(args: { port?: number; url_filter?: string; run_dir?: string; }): CdpTarget { const given = args.url_filter ?? ''; let runDir = args.run_dir != null && args.run_dir !== '' ? args.run_dir : null; if (runDir != null) { try { runDir = resolveRoundDir(runDir); } catch { // Not a path and not one indexed round: read as given, and say so below. } } const files = [ runDir != null ? runDir.endsWith('.json') ? runDir : path.join(runDir, 'run.json') : null, lastRunJsonRead(), activeRoundJson(), ].filter((f): f is string => f != null); let round: {file: string; port: number; urlFilter: string | null} | null = null; let unusedRunDir: string | null = null; for (const file of files) { const cfg = roundConfig(file); if (cfg?.port != null) { round = {file, port: cfg.port, urlFilter: cfg.urlFilter}; break; } // An explicit run_dir that records no port must not fall through to // another round's. if (file === files[0] && runDir != null) { unusedRunDir = `\`run_dir\` ${short(file)} ${cfg == null ? 'could not be read' : 'records no `config.cdp_port`'}`; break; } } const filterFor = (port: number) => args.url_filter != null || round == null || round.port !== port ? given : (round.urlFilter ?? ''); if (args.port != null) { return {port: args.port, urlFilter: filterFor(args.port), source: '`port`'}; } const env = Number((process.env.MEMLAB_CDP_PORT ?? '').trim()); if (Number.isInteger(env) && env > 0) { return {port: env, urlFilter: filterFor(env), source: 'MEMLAB_CDP_PORT'}; } if (round != null) { return { port: round.port, urlFilter: filterFor(round.port), source: short(round.file), }; } return { port: DEFAULT_CDP_PORT, urlFilter: given, source: unusedRunDir != null ? `the default, because ${unusedRunDir}; pass \`port\` or set MEMLAB_CDP_PORT` : 'the default; pass `port`, set MEMLAB_CDP_PORT, or pass the round’s `run_dir`', }; } /** "port 9444 (from r493ab/run.json)". */ export function describeCdpTarget(t: CdpTarget): string { return `port ${t.port} (from ${t.source})`; }