Skip to content

Commit 9fc8a2e

Browse files
committed
feat(server): allow a host-installed browser for previews
1 parent 30eb7e7 commit 9fc8a2e

3 files changed

Lines changed: 115 additions & 1 deletion

File tree

‎apps/server/src/preview/PreviewBrowser.test.ts‎

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ const makeHarness = Effect.fn("test.makePreviewBrowser")(function* (
7878
readonly body?: Stream.Stream<Uint8Array>;
7979
readonly wait?: Duration.Input;
8080
readonly unsupported?: boolean;
81+
readonly env?: NodeJS.ProcessEnv;
8182
} = {},
8283
) {
8384
const fs = yield* FileSystem.FileSystem;
@@ -103,6 +104,7 @@ const makeHarness = Effect.fn("test.makePreviewBrowser")(function* (
103104
}).pipe(
104105
Effect.provideService(HostProcess.Platform, hostPlatform),
105106
Effect.provideService(HostProcess.Architecture, "x64"),
107+
Effect.provideService(HostProcess.Environment, options.env ?? {}),
106108
Effect.provideService(
107109
HttpClient.HttpClient,
108110
HttpClient.make((request) =>
@@ -121,6 +123,67 @@ const makeHarness = Effect.fn("test.makePreviewBrowser")(function* (
121123
});
122124

123125
it.layer(NodeServices.layer)("PreviewBrowser", (it) => {
126+
it.effect("uses a configured browser without downloading the bundled shell", () =>
127+
Effect.gen(function* () {
128+
const fs = yield* FileSystem.FileSystem;
129+
const path = yield* Path.Path;
130+
const directory = yield* fs.makeTempDirectoryScoped({ prefix: "t3-custom-browser-test-" });
131+
const configured = path.join(directory, "custom Chrome");
132+
yield* fs.writeFileString(configured, "#!/bin/sh\n");
133+
yield* fs.chmod(configured, 0o755);
134+
const { browser, requests } = yield* makeHarness({
135+
env: { T3CODE_SERVER_BROWSER_EXECUTABLE: ` ${configured} ` },
136+
});
137+
138+
expect(yield* browser.installed).toEqual(Option.some(configured));
139+
expect(yield* browser.executable).toBe(configured);
140+
expect(requests).toEqual([]);
141+
}),
142+
);
143+
144+
it.effect.each([
145+
{ name: "missing" },
146+
{ name: "directory" },
147+
{ name: "relative" },
148+
...(hostPlatform === "win32" ? [] : [{ name: "non-executable" }]),
149+
])("rejects a $name browser override without downloading a fallback", (testCase) =>
150+
Effect.gen(function* () {
151+
const fs = yield* FileSystem.FileSystem;
152+
const path = yield* Path.Path;
153+
const directory = yield* fs.makeTempDirectoryScoped({ prefix: "t3-custom-browser-test-" });
154+
const configured =
155+
testCase.name === "relative"
156+
? "relative-browser"
157+
: testCase.name === "directory"
158+
? directory
159+
: path.join(directory, "missing-browser");
160+
if (testCase.name === "non-executable") {
161+
yield* fs.writeFileString(configured, "#!/bin/sh\n");
162+
yield* fs.chmod(configured, 0o644);
163+
}
164+
const { browser, requests } = yield* makeHarness({
165+
env: { T3CODE_SERVER_BROWSER_EXECUTABLE: configured },
166+
});
167+
168+
expect(yield* browser.installed).toEqual(Option.none());
169+
const error = yield* browser.executable.pipe(Effect.flip);
170+
expect(error._tag).toBe("PreviewBrowserInstallError");
171+
expect(error.message).toContain("T3CODE_SERVER_BROWSER_EXECUTABLE");
172+
expect(requests).toEqual([]);
173+
}),
174+
);
175+
176+
it.effect("treats a blank override as the bundled browser", () =>
177+
Effect.gen(function* () {
178+
const { browser, requests } = yield* makeHarness({
179+
env: { T3CODE_SERVER_BROWSER_EXECUTABLE: " " },
180+
});
181+
182+
expect(yield* browser.executable).toContain(executableName);
183+
expect(requests).toHaveLength(1);
184+
}),
185+
);
186+
124187
it.effect("installs a verified download with its file modes and then reuses it", () =>
125188
Effect.gen(function* () {
126189
const { browser, fs, path, installRoot, requests } = yield* makeHarness();

‎apps/server/src/preview/PreviewBrowser.ts‎

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,10 +162,41 @@ export const makePreviewBrowser = Effect.fn("PreviewBrowser.make")(function* (
162162
) {
163163
const fs = yield* FileSystem.FileSystem;
164164
const path = yield* Path.Path;
165+
const platform = yield* HostProcess.Platform;
166+
const configured = (yield* HostProcess.Environment).T3CODE_SERVER_BROWSER_EXECUTABLE?.trim();
167+
if (configured) {
168+
const installed = path.isAbsolute(configured)
169+
? fs.stat(configured).pipe(
170+
Effect.map((info) =>
171+
info.type === "File" && (platform === "win32" || (info.mode & 0o111) !== 0)
172+
? Option.some(configured)
173+
: Option.none<string>(),
174+
),
175+
Effect.orElseSucceed(() => Option.none<string>()),
176+
)
177+
: Effect.succeed(Option.none<string>());
178+
return PreviewBrowser.of({
179+
installed,
180+
executable: installed.pipe(
181+
Effect.flatMap(
182+
Option.match({
183+
onSome: Effect.succeed,
184+
onNone: () =>
185+
Effect.fail(
186+
new PreviewBrowserInstallError({
187+
detail:
188+
"T3CODE_SERVER_BROWSER_EXECUTABLE must be an absolute path to an executable file.",
189+
}),
190+
),
191+
}),
192+
),
193+
Effect.withSpan("PreviewBrowser.executable"),
194+
),
195+
});
196+
}
165197
const http = yield* HttpClient.HttpClient;
166198
// Installs belong to the service, so they finish even when no caller is still waiting.
167199
const serviceScope = yield* Effect.scope;
168-
const platform = yield* HostProcess.Platform;
169200
const arch = yield* HostProcess.Architecture;
170201
const release =
171202
options.release === undefined ? previewBrowserRelease(platform, arch) : options.release;

‎docs/user/remote-access.md‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,26 @@ is the same browser [HTML renders](html-renders.md) use, so a host downloads it
225225
only once. Some Linux hosts need [setup](#browser-host-setup) before it can
226226
start.
227227

228+
To use a Chromium-compatible browser installed on the server, set
229+
`T3CODE_SERVER_BROWSER_EXECUTABLE` to its absolute executable path before starting
230+
T3. Shared browser tabs and HTML renders use that executable instead of downloading
231+
the bundled browser:
232+
233+
```sh
234+
T3CODE_SERVER_BROWSER_EXECUTABLE=/opt/google/chrome/chrome t3 serve
235+
```
236+
237+
For a background service, put the variable in `service.env` inside the server's
238+
T3 home, normally `~/.t3/service.env`, then restart the service. Set the path on
239+
the machine running the server, including when you connect from another device.
240+
An unset or blank variable keeps the bundled browser. A relative, missing,
241+
non-file or non-executable path produces an error rather than downloading a fallback.
242+
243+
Profiles stay in T3's data folder. You maintain the selected browser and its
244+
required libraries. This option does not change sandbox settings; on Linux,
245+
your administrator may need to allow the executable in AppArmor. Sites can still
246+
refuse sign-in, so selecting another browser does not guarantee account access.
247+
228248
Agent tabs have separate storage and share a Chromium process. Take control before
229249
typing into an agent's tab, then release control when you want the agent to
230250
continue. Read-only connections can watch without changing the page.

0 commit comments

Comments
 (0)