diff --git a/packages/cli/src/browser/pageNetwork.signal.fixture.ts b/packages/cli/src/browser/pageNetwork.signal.fixture.ts new file mode 100644 index 0000000000..0396354d00 --- /dev/null +++ b/packages/cli/src/browser/pageNetwork.signal.fixture.ts @@ -0,0 +1,26 @@ +// fallow-ignore-file unused-file +import { EventEmitter } from "node:events"; +import { writeFileSync } from "node:fs"; +import type { Browser } from "puppeteer-core"; +import { registerRootExitRequester } from "../utils/commandResult.js"; +import { onStopSignal } from "./launch.js"; +import { launchWithPageNetwork } from "./pageNetwork.js"; + +const [readyPath] = process.argv.slice(2); +if (!readyPath) throw new Error("Missing fixture arguments"); + +process.env.PRODUCER_RESTRICT_PAGE_NETWORK = "true"; +registerRootExitRequester((exitCode) => process.exit(exitCode)); +// A command's own Chrome launch installs the CLI's signal handling; this stands in for it. +onStopSignal(() => {}); + +const events = new EventEmitter(); +const connectedUntilStopped = { + connected: true, + once: (event: string, listener: () => void) => events.once(event, listener), +} as unknown as Browser; + +await launchWithPageNetwork("http://127.0.0.1:4000/", async (network) => { + writeFileSync(readyPath, JSON.stringify({ userDataDir: network.userDataDir })); + return connectedUntilStopped; +}); diff --git a/packages/cli/src/browser/pageNetwork.signal.test.ts b/packages/cli/src/browser/pageNetwork.signal.test.ts new file mode 100644 index 0000000000..2647ba24a3 --- /dev/null +++ b/packages/cli/src/browser/pageNetwork.signal.test.ts @@ -0,0 +1,37 @@ +import { spawn } from "node:child_process"; +import { existsSync, mkdtempSync, readFileSync, rmSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { dirname, join } from "node:path"; +import { fileURLToPath } from "node:url"; +import { expect, it } from "vitest"; +import { waitForTestCondition } from "../utils/processTestUtils.js"; + +const fixturePath = join(dirname(fileURLToPath(import.meta.url)), "pageNetwork.signal.fixture.ts"); + +// fails without the fix: the CLI's Ctrl-C handler exits without waiting for the profile's removal, so its +// hf-cli-profile- folder stays in the temp folder. +it.skipIf(process.platform === "win32")( + "removes a restricted browser's temp profile when the CLI stops on Ctrl-C", + async () => { + const testDir = mkdtempSync(join(tmpdir(), "hf-page-network-signal-")); + const readyPath = join(testDir, "ready.json"); + const child = spawn(process.execPath, ["--import", "tsx", fixturePath, readyPath], { + stdio: "ignore", + }); + try { + await waitForTestCondition(() => existsSync(readyPath), 20_000); + const { userDataDir } = JSON.parse(readFileSync(readyPath, "utf8")) as { + userDataDir: string; + }; + expect(existsSync(userDataDir)).toBe(true); + const exited = new Promise((resolve) => child.once("exit", resolve)); + child.kill("SIGINT"); + expect(await exited).toBe(130); + expect(existsSync(userDataDir)).toBe(false); + } finally { + child.kill("SIGKILL"); + rmSync(testDir, { recursive: true, force: true }); + } + }, + 30_000, +); diff --git a/packages/cli/src/browser/pageNetwork.ts b/packages/cli/src/browser/pageNetwork.ts index 532582968b..69196df509 100644 --- a/packages/cli/src/browser/pageNetwork.ts +++ b/packages/cli/src/browser/pageNetwork.ts @@ -1,4 +1,5 @@ import type { Browser } from "puppeteer-core"; +import { registerBrowserSessionCleanup } from "./launch.js"; export interface PageNetworkLaunch { args: string[]; @@ -20,11 +21,16 @@ export async function launchWithPageNetwork( const { rm } = await import("node:fs/promises"); const proxy = await startCaptureProxy(null, logRefusal); let userDataDir: string | undefined; - const release = async () => { - await proxy.close(); - if (userDataDir) await rm(userDataDir, { recursive: true, force: true }).catch(() => {}); - }; + let released: Promise | undefined; + let stopWatching = () => {}; + const release = () => + (released ??= (async () => { + stopWatching(); + await proxy.close(); + if (userDataDir) await rm(userDataDir, { recursive: true, force: true }).catch(() => {}); + })()); try { + stopWatching = registerBrowserSessionCleanup(release); userDataDir = await createPublicOnlyProfile("hf-cli-profile-"); const browser = await launch({ args: browserProxyArgs(proxy.url, new URL(pageUrl).host), diff --git a/packages/cli/src/browser/pageNetwork.wiring.test.ts b/packages/cli/src/browser/pageNetwork.wiring.test.ts new file mode 100644 index 0000000000..e6799df1da --- /dev/null +++ b/packages/cli/src/browser/pageNetwork.wiring.test.ts @@ -0,0 +1,91 @@ +import { existsSync, readdirSync, readFileSync } from "node:fs"; +import { join, relative, sep } from "node:path"; +import { fileURLToPath } from "node:url"; +import ts from "typescript"; +import { describe, expect, it } from "vitest"; + +const SRC = fileURLToPath(new URL("..", import.meta.url)); + +// Chrome launches that never open a composition page, so PRODUCER_RESTRICT_PAGE_NETWORK's fence doesn't apply. +const OPENS_NO_COMPOSITION: Record = { + "browser/launch.ts": "defines the launch, and its GPU probe opens a blank page", + "capture/launchCaptureBrowser.ts": "`capture` opens a website, not a composition", +}; + +function sources(dir: string): string[] { + return readdirSync(dir, { withFileTypes: true }).flatMap((entry) => { + const at = join(dir, entry.name); + if (entry.isDirectory()) return sources(at); + return /\.tsx?$/.test(entry.name) && !/\.(test|fixture|d)\.tsx?$/.test(entry.name) ? [at] : []; + }); +} + +const callee = (call: ts.CallExpression) => call.expression.getText(); + +/** + * The Chrome launches in `text` that skip the page-network fence: a `launchManagedBrowser` or puppeteer `.launch` + * not inside a `launchWithPageNetwork` call, or an engine `acquireBrowser` given no `pageServerUrl`. + */ +function unfencedLaunches(name: string, text: string): string[] { + const file = ts.createSourceFile(name, text, ts.ScriptTarget.Latest, true); + const found: string[] = []; + const at = (node: ts.Node) => + `${name}:${file.getLineAndCharacterOfPosition(node.getStart()).line + 1}`; + const fenced = (node: ts.Node): boolean => + !!node.parent && + ((ts.isCallExpression(node.parent) && callee(node.parent) === "launchWithPageNetwork") || + fenced(node.parent)); + const visit = (node: ts.Node) => { + if (ts.isCallExpression(node)) { + const name = callee(node); + if ((name === "launchManagedBrowser" || /puppeteer.*\.launch$/i.test(name)) && !fenced(node)) + found.push(at(node)); + if ( + name === "acquireBrowser" && + !node.arguments.some( + (arg) => + ts.isObjectLiteralExpression(arg) && + arg.properties.some((property) => property.name?.getText() === "pageServerUrl"), + ) + ) + found.push(at(node)); + } + ts.forEachChild(node, visit); + }; + visit(file); + return found; +} + +describe("the page-network fence", () => { + it("is found missing on a bare launch, and found on a fenced one", () => { + expect( + unfencedLaunches( + "bare.ts", + `const a = await launchManagedBrowser(puppeteer.default, { args }); +const b = await puppeteer.default.launch({ args }); +const c = await acquireBrowser(args, { forceScreenshot: true });`, + ), + ).toEqual(["bare.ts:1", "bare.ts:2", "bare.ts:3"]); + expect( + unfencedLaunches( + "fenced.ts", + `const a = await launchWithPageNetwork(url, (network) => + launchManagedBrowser(puppeteer.default, { args: [...args, ...network.args] }), +); +const c = await acquireBrowser(args, { forceScreenshot: true, pageServerUrl: url });`, + ), + ).toEqual([]); + }); + + // fails without the fix: layout, validate and the motion shot launched Chrome with launchManagedBrowser alone, and + // the Studio thumbnail browser called acquireBrowser with no page server; only the shared page-open path was tested. + it("covers every Chrome launch in the CLI that opens a composition", () => { + for (const file of Object.keys(OPENS_NO_COMPOSITION)) + expect(existsSync(join(SRC, file))).toBe(true); + const unfenced = sources(SRC) + .map((file) => relative(SRC, file).split(sep).join("/")) + .filter((file) => !(file in OPENS_NO_COMPOSITION)) + .flatMap((file) => unfencedLaunches(file, readFileSync(join(SRC, file), "utf8"))); + expect(unfenced).toEqual([]); + }); +}); diff --git a/packages/cli/src/server/studioServer.test.ts b/packages/cli/src/server/studioServer.test.ts index 6e8046b7cc..d4b8b24d82 100644 --- a/packages/cli/src/server/studioServer.test.ts +++ b/packages/cli/src/server/studioServer.test.ts @@ -747,6 +747,25 @@ describe("Studio thumbnail capture", () => { expect(screenshot).toHaveBeenCalledTimes(1); }); + // fails without the fix: the thumbnail browser launched with no page server, so with + // PRODUCER_RESTRICT_PAGE_NETWORK on its proxy refused the Studio's own preview and every thumbnail failed. + it("gives the thumbnail browser the preview's server, and a new browser for a preview on another host", async () => { + fakePageBrowser(); + const launch = vi.spyOn(engineState, "acquireBrowser"); + const dir = tmpProject(); + server = createStudioServer({ projectDir: dir }); + const at = (previewUrl: string) => ({ ...opts(dir), previewUrl }); + await server.adapter.generateThumbnail?.(at("http://127.0.0.1:4100/api/projects/demo/preview")); + await server.adapter.generateThumbnail?.(at("http://127.0.0.1:4100/api/projects/demo/preview")); + await server.adapter.generateThumbnail?.(at("http://localhost:4200/api/projects/demo/preview")); + expect( + launch.mock.calls.map((call) => (call[1] as { pageServerUrl?: string }).pageServerUrl), + ).toEqual([ + "http://127.0.0.1:4100/api/projects/demo/preview", + "http://localhost:4200/api/projects/demo/preview", + ]); + }); + it("undoes a row's isolation after its screenshot, since the page is reused", async () => { const { screenshot, evaluate } = fakePageBrowser(); const dir = tmpProject(); diff --git a/packages/cli/src/server/studioServer.ts b/packages/cli/src/server/studioServer.ts index a21ca25c4f..d3e589ca4e 100644 --- a/packages/cli/src/server/studioServer.ts +++ b/packages/cli/src/server/studioServer.ts @@ -267,6 +267,7 @@ let _thumbnailBrowserModes: { requested: BrowserGpuMode; resolved: ResolvedBrowserGpuMode; softwareWebGpu: boolean; + pageServerHost: string; } | null = null; interface ThumbnailBrowserSession { @@ -274,27 +275,35 @@ interface ThumbnailBrowserSession { requestedGpuMode: BrowserGpuMode; resolvedGpuMode: ResolvedBrowserGpuMode; softwareWebGpu: boolean; + pageServerHost: string; } +// The browser gets the preview's server as its page server: with PRODUCER_RESTRICT_PAGE_NETWORK on, that is the +// one host it reaches directly, so a preview on another host needs a new browser. async function getThumbnailBrowser( requestedGpuMode: BrowserGpuMode, + previewUrl: string, isShuttingDown: () => boolean, ): Promise { if (isShuttingDown()) return null; + const pageServerHost = new URL(previewUrl).host; if ( _thumbnailBrowserLease?.browser.connected && - _thumbnailBrowserModes?.requested === requestedGpuMode + _thumbnailBrowserModes?.requested === requestedGpuMode && + _thumbnailBrowserModes.pageServerHost === pageServerHost ) { return { browser: _thumbnailBrowserLease.browser, requestedGpuMode: _thumbnailBrowserModes.requested, resolvedGpuMode: _thumbnailBrowserModes.resolved, softwareWebGpu: _thumbnailBrowserModes.softwareWebGpu, + pageServerHost, }; } if (_thumbnailBrowserInitializing) { const session = await _thumbnailBrowserInitializing; - if (session?.requestedGpuMode === requestedGpuMode) return session; + if (session?.requestedGpuMode === requestedGpuMode && session.pageServerHost === pageServerHost) + return session; } if (_thumbnailBrowserLease) await closeThumbnailBrowser(); @@ -327,13 +336,14 @@ async function getThumbnailBrowser( }, gpuConfig, ), - { forceScreenshot: true }, + { forceScreenshot: true, pageServerUrl: previewUrl }, ); _thumbnailBrowserLease = acquired; _thumbnailBrowserModes = { requested: requestedGpuMode, resolved: resolvedGpuMode, softwareWebGpu, + pageServerHost, }; acquired.browser.on("disconnected", () => { if (_thumbnailBrowserLease !== acquired) return; @@ -341,7 +351,13 @@ async function getThumbnailBrowser( _thumbnailBrowserModes = null; _thumbnailBrowserInitializing = null; }); - return { browser: acquired.browser, requestedGpuMode, resolvedGpuMode, softwareWebGpu }; + return { + browser: acquired.browser, + requestedGpuMode, + resolvedGpuMode, + softwareWebGpu, + pageServerHost, + }; } catch (err) { console.warn( "[Studio] Failed to launch thumbnail browser:", @@ -791,7 +807,11 @@ export function createStudioServer(options: StudioServerOptions): StudioServer { }, async generateThumbnail(opts): Promise { - const session = await getThumbnailBrowser(browserGpuMode, () => shuttingDown); + const session = await getThumbnailBrowser( + browserGpuMode, + opts.previewUrl, + () => shuttingDown, + ); if (!session) { if (!shuttingDown) { console.warn("[Studio] Thumbnail: no browser available — Chrome may not be installed");