Skip to content

Commit 00c91ef

Browse files
committed
refactor: move node browser routing rollout to env
Remove the public browser routing constructor knobs and read direct-to-VM subresource rollout from KERNEL_BROWSER_ROUTING_SUBRESOURCES instead, defaulting to curl while leaving browser.fetch cache-backed.
1 parent fdd3adf commit 00c91ef

5 files changed

Lines changed: 182 additions & 144 deletions

File tree

examples/browser-routing.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,9 @@
11
import Kernel from '@onkernel/sdk';
22

33
async function main() {
4-
const kernel = new Kernel({
5-
browserRouting: {
6-
enabled: true,
7-
subresources: ['computer'],
8-
},
9-
});
4+
const kernel = new Kernel();
105

116
const browser = await kernel.browsers.create({});
12-
await kernel.browsers.computer.clickMouse(browser.session_id, { x: 10, y: 10 });
137
const response = await kernel.browsers.fetch(browser.session_id, 'https://example.com', { method: 'GET' });
148
console.log('status', response.status);
159

src/client.ts

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@ import * as Uploads from './core/uploads';
2020
import * as API from './resources/index';
2121
import { APIPromise } from './core/api-promise';
2222
import { AppListParams, AppListResponse, AppListResponsesOffsetPagination, Apps } from './resources/apps';
23-
import { BrowserRouteCache, createRoutingFetch, type BrowserRoutingOptions } from './lib/browser-routing';
23+
import {
24+
BrowserRouteCache,
25+
browserRoutingSubresourcesFromEnv,
26+
createRoutingFetch,
27+
} from './lib/browser-routing';
2428
import {
2529
BrowserPool,
2630
BrowserPoolAcquireParams,
@@ -195,11 +199,6 @@ export interface ClientOptions {
195199
*/
196200
fetch?: Fetch | undefined;
197201

198-
/**
199-
* Configure direct-to-VM routing for browser subresource requests.
200-
*/
201-
browserRouting?: BrowserRoutingOptions | undefined;
202-
203202
/**
204203
* The maximum number of times that the client will retry a request in case of a
205204
* temporary failure, like a network error or a 5XX error from the server.
@@ -321,15 +320,12 @@ export class Kernel {
321320
this.fetchOptions = options.fetchOptions;
322321
this.maxRetries = options.maxRetries ?? 2;
323322
this.rawFetch = options.fetch ?? Shims.getDefaultFetch();
324-
this.browserRouteCache = options.browserRouting?.cache ?? new BrowserRouteCache();
325-
this.fetch =
326-
options.browserRouting?.enabled ?
327-
createRoutingFetch(this.rawFetch, {
328-
apiBaseURL: this.baseURL,
329-
subresources: options.browserRouting.subresources ?? [],
330-
cache: this.browserRouteCache,
331-
})
332-
: this.rawFetch;
323+
this.browserRouteCache = new BrowserRouteCache();
324+
this.fetch = createRoutingFetch(this.rawFetch, {
325+
apiBaseURL: this.baseURL,
326+
subresources: browserRoutingSubresourcesFromEnv(),
327+
cache: this.browserRouteCache,
328+
});
333329
this.#encoder = Opts.FallbackEncoder;
334330

335331
this._options = options;
@@ -341,16 +337,6 @@ export class Kernel {
341337
* Create a new client instance re-using the same options given to the current client with optional overriding.
342338
*/
343339
withOptions(options: Partial<ClientOptions>): this {
344-
const currentRouting = this._options.browserRouting;
345-
const nextBrowserRouting = options.browserRouting === undefined ? currentRouting : options.browserRouting;
346-
const sharedBrowserRouting =
347-
nextBrowserRouting ?
348-
{
349-
...nextBrowserRouting,
350-
cache: nextBrowserRouting.cache ?? this.browserRouteCache,
351-
}
352-
: undefined;
353-
354340
const client = new (this.constructor as any as new (props: ClientOptions) => typeof this)({
355341
...this._options,
356342
environment: options.environment ? options.environment : undefined,
@@ -363,7 +349,12 @@ export class Kernel {
363349
fetchOptions: this.fetchOptions,
364350
apiKey: this.apiKey,
365351
...options,
366-
browserRouting: sharedBrowserRouting,
352+
});
353+
client.browserRouteCache = this.browserRouteCache;
354+
client.fetch = createRoutingFetch(client.rawFetch, {
355+
apiBaseURL: client.baseURL,
356+
subresources: browserRoutingSubresourcesFromEnv(),
357+
cache: client.browserRouteCache,
367358
});
368359
return client;
369360
}

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export { type Uploadable, toFile } from './core/uploads';
66
export { APIPromise } from './core/api-promise';
77
export { Kernel, type ClientOptions } from './client';
88
export { type BrowserFetchInit } from './lib/browser-fetch';
9-
export { BrowserRouteCache, type BrowserRoute, type BrowserRoutingOptions } from './lib/browser-routing';
9+
export { BrowserRouteCache, type BrowserRoute } from './lib/browser-routing';
1010
export { PagePromise } from './core/pagination';
1111
export {
1212
KernelError,

src/lib/browser-routing.ts

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,6 @@ export type BrowserRoute = {
66
jwt: string;
77
};
88

9-
export interface BrowserRoutingOptions {
10-
enabled?: boolean;
11-
subresources?: string[] | undefined;
12-
cache?: BrowserRouteCache | undefined;
13-
}
14-
159
export class BrowserRouteCache {
1610
private entries = new Map<string, BrowserRoute>();
1711

@@ -32,6 +26,25 @@ export class BrowserRouteCache {
3226
}
3327
}
3428

29+
const BROWSER_ROUTING_SUBRESOURCES_ENV = 'KERNEL_BROWSER_ROUTING_SUBRESOURCES';
30+
const DEFAULT_BROWSER_ROUTING_SUBRESOURCES = ['curl'];
31+
32+
export function browserRoutingSubresourcesFromEnv(): string[] {
33+
const raw = readBrowserRoutingSubresourcesEnv();
34+
if (raw === undefined) {
35+
return [...DEFAULT_BROWSER_ROUTING_SUBRESOURCES];
36+
}
37+
38+
if (raw.trim() === '') {
39+
return [];
40+
}
41+
42+
return raw
43+
.split(',')
44+
.map((value) => value.trim())
45+
.filter(Boolean);
46+
}
47+
3548
export function createRoutingFetch(
3649
innerFetch: Fetch,
3750
{
@@ -186,3 +199,17 @@ function parseJwtFromCdpWsUrl(cdpWsUrl: string | undefined): string | undefined
186199
return undefined;
187200
}
188201
}
202+
203+
function readBrowserRoutingSubresourcesEnv(): string | undefined {
204+
if (typeof (globalThis as any).process !== 'undefined') {
205+
const value = (globalThis as any).process.env?.[BROWSER_ROUTING_SUBRESOURCES_ENV];
206+
return typeof value === 'string' ? value : undefined;
207+
}
208+
209+
if (typeof (globalThis as any).Deno !== 'undefined') {
210+
const value = (globalThis as any).Deno.env?.get?.(BROWSER_ROUTING_SUBRESOURCES_ENV);
211+
return typeof value === 'string' ? value : undefined;
212+
}
213+
214+
return undefined;
215+
}

0 commit comments

Comments
 (0)