-
Notifications
You must be signed in to change notification settings - Fork 0
fix: stabilize browser launcher requests #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "daegari": minor | ||
| --- | ||
|
|
||
| Report typed browser-launch failures and return explicit Host and browser request outcomes from Target opening. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,7 @@ on: | |
| push: | ||
| branches: [main] | ||
| pull_request: | ||
| workflow_dispatch: | ||
|
|
||
| permissions: | ||
| contents: read | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,7 @@ on: | |
| push: | ||
| branches: [main] | ||
| pull_request: | ||
| workflow_dispatch: | ||
|
|
||
| permissions: | ||
| contents: read | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
packages/daegari/src/core/internal/browser-launcher.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| import { EventEmitter } from "node:events"; | ||
| import { describe, expect, test, vi } from "vitest"; | ||
|
|
||
| import { requestBrowser } from "./browser-launcher.js"; | ||
|
|
||
| function fakeProcess() { | ||
| return Object.assign(new EventEmitter(), { unref: vi.fn() }); | ||
| } | ||
|
|
||
| describe("browser launcher", () => { | ||
| test.each([ | ||
| ["darwin", "http://127.0.0.1:5173/", "open", ["http://127.0.0.1:5173/"]], | ||
| ["linux", "http://127.0.0.1:5173/", "xdg-open", ["http://127.0.0.1:5173/"]], | ||
| [ | ||
| "win32", | ||
| "http://127.0.0.1:5173/?target=alpha&view=details#token=secret", | ||
| "rundll32.exe", | ||
| [ | ||
| "url.dll,FileProtocolHandler", | ||
| "http://127.0.0.1:5173/?target=alpha&view=details#token=secret", | ||
| ], | ||
| ], | ||
| ] as const)( | ||
| "dispatches through the %s platform launcher", | ||
| async (platform, url, command, args) => { | ||
| const child = fakeProcess(); | ||
| const spawn = vi.fn(() => { | ||
| queueMicrotask(() => { | ||
| child.emit("spawn"); | ||
| child.emit("close", 0, null); | ||
| }); | ||
| return child; | ||
| }); | ||
|
|
||
| await requestBrowser(url, { platform, spawn }); | ||
|
|
||
| expect(spawn).toHaveBeenCalledWith(command, args, { | ||
| detached: true, | ||
| stdio: "ignore", | ||
| windowsHide: true, | ||
| }); | ||
| expect(child.unref).toHaveBeenCalledOnce(); | ||
| }, | ||
| ); | ||
|
|
||
| test("turns a spawn failure into a typed Target error", async () => { | ||
| const child = fakeProcess(); | ||
| const cause = Object.assign(new Error("spawn xdg-open ENOENT"), { code: "ENOENT" }); | ||
| const spawn = vi.fn(() => { | ||
| queueMicrotask(() => child.emit("error", cause)); | ||
| return child; | ||
| }); | ||
|
|
||
| await expect( | ||
| requestBrowser("http://127.0.0.1:5173/#token=secret", { | ||
| handoffTimeoutMs: 10, | ||
| platform: "linux", | ||
| spawn, | ||
| }), | ||
| ).rejects.toMatchObject({ | ||
| cause, | ||
| code: "DAEGARI_APP_TARGET_BROWSER_LAUNCH", | ||
| name: "App.Target.BrowserLaunchError", | ||
| }); | ||
| expect(child.unref).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| test("reports a launcher that rejects the request immediately", async () => { | ||
| const child = fakeProcess(); | ||
| const spawn = vi.fn(() => { | ||
| queueMicrotask(() => { | ||
| child.emit("spawn"); | ||
| child.emit("close", 4, null); | ||
| }); | ||
| return child; | ||
| }); | ||
|
|
||
| await expect( | ||
| requestBrowser("http://127.0.0.1:5173/#token=secret", { | ||
| handoffTimeoutMs: 10, | ||
| platform: "linux", | ||
| spawn, | ||
| }), | ||
| ).rejects.toMatchObject({ | ||
| code: "DAEGARI_APP_TARGET_BROWSER_LAUNCH", | ||
| name: "App.Target.BrowserLaunchError", | ||
| }); | ||
| expect(child.unref).toHaveBeenCalledOnce(); | ||
| }); | ||
|
|
||
| test("stops waiting after the bounded handoff window", async () => { | ||
| vi.useFakeTimers(); | ||
| try { | ||
| const child = fakeProcess(); | ||
| const request = requestBrowser("http://127.0.0.1:5173/", { | ||
| handoffTimeoutMs: 10, | ||
| platform: "linux", | ||
| spawn: () => child, | ||
| }); | ||
|
|
||
| child.emit("spawn"); | ||
| await vi.advanceTimersByTimeAsync(10); | ||
|
|
||
| await expect(request).resolves.toBeUndefined(); | ||
| expect(child.unref).toHaveBeenCalledOnce(); | ||
| } finally { | ||
| vi.useRealTimers(); | ||
| } | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| import { spawn as spawnProcess } from "node:child_process"; | ||
|
|
||
| import { BrowserLaunchError } from "../../app/target.js"; | ||
|
|
||
| type BrowserProcess = { | ||
| off(event: string | symbol, listener: (...args: unknown[]) => void): unknown; | ||
| once(event: string | symbol, listener: (...args: unknown[]) => void): unknown; | ||
| unref(): void; | ||
| }; | ||
|
|
||
| type SpawnBrowser = ( | ||
| command: string, | ||
| args: string[], | ||
| options: { detached: true; stdio: "ignore"; windowsHide: true }, | ||
| ) => BrowserProcess; | ||
|
|
||
| type RequestBrowserOptions = { | ||
| handoffTimeoutMs?: number; | ||
| platform?: NodeJS.Platform; | ||
| spawn?: SpawnBrowser; | ||
| }; | ||
|
|
||
| const defaultHandoffTimeoutMs = 1_000; | ||
|
|
||
| function launcher(platform: NodeJS.Platform, url: string) { | ||
| if (platform === "darwin") return { args: [url], command: "open" }; | ||
| if (platform === "win32") { | ||
| return { args: ["url.dll,FileProtocolHandler", url], command: "rundll32.exe" }; | ||
| } | ||
| return { args: [url], command: "xdg-open" }; | ||
|
2wheeh marked this conversation as resolved.
|
||
| } | ||
|
|
||
| function launchError(cause: unknown) { | ||
| return new BrowserLaunchError("Failed to request the platform browser", { cause }); | ||
| } | ||
|
|
||
| export function requestBrowser(url: string, options: RequestBrowserOptions = {}): Promise<void> { | ||
| const spawn = | ||
| options.spawn ?? | ||
| ((command, args, spawnOptions) => spawnProcess(command, args, spawnOptions) as BrowserProcess); | ||
| const { args, command } = launcher(options.platform ?? process.platform, url); | ||
|
|
||
| return new Promise((resolve, reject) => { | ||
| let child: ReturnType<SpawnBrowser>; | ||
| try { | ||
| child = spawn(command, args, { detached: true, stdio: "ignore", windowsHide: true }); | ||
| } catch (error) { | ||
| reject(launchError(error)); | ||
| return; | ||
| } | ||
|
|
||
| let timer: NodeJS.Timeout | undefined; | ||
| let settled = false; | ||
|
|
||
| const finish = (result?: BrowserLaunchError) => { | ||
| if (settled) return; | ||
| settled = true; | ||
| if (timer) clearTimeout(timer); | ||
| child.off("error", onError); | ||
| child.off("close", onClose); | ||
| child.off("spawn", onSpawn); | ||
| if (result) reject(result); | ||
| else resolve(); | ||
| }; | ||
|
2wheeh marked this conversation as resolved.
|
||
| const onError = (error: unknown) => finish(launchError(error)); | ||
| const onClose = (code: unknown, signal: unknown) => { | ||
| if (code === 0) { | ||
| finish(); | ||
| return; | ||
| } | ||
| const outcome = code === null ? `signal ${String(signal)}` : `exit code ${String(code)}`; | ||
| finish(launchError(new Error(`Browser launcher "${command}" failed with ${outcome}`))); | ||
| }; | ||
| const onSpawn = () => { | ||
| if (settled) return; | ||
| child.unref(); | ||
| child.once("close", onClose); | ||
| timer = setTimeout(finish, options.handoffTimeoutMs ?? defaultHandoffTimeoutMs); | ||
| }; | ||
|
|
||
| child.once("error", onError); | ||
| child.once("spawn", onSpawn); | ||
| }); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.