-
Notifications
You must be signed in to change notification settings - Fork 2
feat: add browser routing cache #99
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
19 commits
Select commit
Hold shift + click to select a range
94228be
feat: add browser-scoped session client
rgarcia ae9a739
fix: require base_url for browser-scoped routing
rgarcia d835f69
fix: enforce browser base_url routing
rgarcia e730af8
feat: generate browser-scoped session bindings
rgarcia 2f12277
refactor: replace browser-scoped client with browser routing cache
rgarcia 2082705
refactor: simplify browser routing cache
rgarcia 7030d96
refactor: rename browser routing subresources config
rgarcia 0d9ddce
docs: restore raw http example in browser routing demo
rgarcia b09434e
feat: restore node browser fetch helper
rgarcia 2f16386
fix: drop node browser routing branch churn
rgarcia 7a56ab6
fix: clean up node browser routing lint drift
rgarcia fdd3adf
fix: simplify node browser routing helpers
rgarcia 00c91ef
refactor: move node browser routing rollout to env
rgarcia 9b24280
fix: preserve browser routing fetch options
rgarcia 0e0e88f
fix: limit browser route cache sniffing
rgarcia 2d0056e
fix: evict deleted browser routes
rgarcia a76f7ae
fix: keep browser routing helpers out of generated code
rgarcia a7ff9bc
fix: restore generated types formatting
rgarcia 81e47ca
fix: handle browser pool route cache updates
rgarcia 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,13 @@ | ||
| import Kernel from '@onkernel/sdk'; | ||
|
|
||
| async function main() { | ||
| const kernel = new Kernel(); | ||
|
|
||
| const browser = await kernel.browsers.create({}); | ||
| const response = await kernel.browsers.fetch(browser.session_id, 'https://example.com', { method: 'GET' }); | ||
| console.log('status', response.status); | ||
|
|
||
| await kernel.browsers.deleteByID(browser.session_id); | ||
| } | ||
|
|
||
| void main(); |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,180 @@ | ||
| import type { RequestInfo, RequestInit } from '../internal/builtin-types'; | ||
| import { KernelError } from '../core/error'; | ||
| import { buildHeaders } from '../internal/headers'; | ||
| import type { FinalRequestOptions, RequestOptions } from '../internal/request-options'; | ||
| import type { HTTPMethod } from '../internal/types'; | ||
| import { joinURL } from './join-url'; | ||
| import type { Kernel } from '../client'; | ||
|
|
||
| export interface BrowserFetchInit extends RequestInit { | ||
| timeout_ms?: number; | ||
| } | ||
|
|
||
| export async function browserFetch( | ||
| client: Kernel, | ||
| sessionId: string, | ||
| input: RequestInfo | URL, | ||
| init?: BrowserFetchInit, | ||
| ): Promise<Response> { | ||
| const route = client.browserRouteCache.get(sessionId); | ||
| if (!route) { | ||
| throw new KernelError( | ||
| `browser route cache does not contain session ${sessionId}; create, retrieve, or list the browser before calling browser.fetch`, | ||
| ); | ||
| } | ||
|
|
||
| const { url: targetURL, method, headers, body, signal, duplex, timeout_ms } = splitFetchArgs(input, init); | ||
| assertHTTPURL(targetURL); | ||
|
|
||
| const query: Record<string, unknown> = { url: targetURL, jwt: route.jwt }; | ||
| if (timeout_ms !== undefined) { | ||
| query['timeout_ms'] = timeout_ms; | ||
| } | ||
|
|
||
| const accept = headers.get('accept'); | ||
| const requestOptions: FinalRequestOptions = { | ||
| method: normalizeMethod(method), | ||
| path: joinURL(route.baseURL, '/curl/raw'), | ||
| query, | ||
| body: body as RequestOptions['body'], | ||
| headers: buildHeaders([ | ||
| { Authorization: null }, | ||
| accept ? { Accept: accept } : { Accept: '*/*' }, | ||
| headersToRequestOptionsHeaders(headers), | ||
| ]), | ||
| signal: signal ?? null, | ||
| __binaryResponse: true, | ||
| }; | ||
| if (duplex) { | ||
| requestOptions.fetchOptions = { duplex } as NonNullable<RequestOptions['fetchOptions']>; | ||
| } | ||
|
|
||
| return client.request(requestOptions).asResponse(); | ||
| } | ||
|
|
||
| function normalizeMethod(method: string): HTTPMethod { | ||
| const methodLower = method.toLowerCase(); | ||
| const allowed = new Set(['get', 'post', 'put', 'patch', 'delete']); | ||
| if (!allowed.has(methodLower)) { | ||
| throw new KernelError(`browser.fetch unsupported HTTP method: ${method}`); | ||
| } | ||
| return methodLower as HTTPMethod; | ||
| } | ||
|
|
||
| function splitFetchArgs( | ||
| input: RequestInfo | URL, | ||
| init?: BrowserFetchInit, | ||
| ): { | ||
| url: string; | ||
| method: string; | ||
| headers: Headers; | ||
| body?: RequestInit['body']; | ||
| signal?: AbortSignal | null; | ||
| duplex?: RequestInit['duplex']; | ||
| timeout_ms?: number; | ||
| } { | ||
| const timeoutFromInit = init && 'timeout_ms' in init ? init['timeout_ms'] : undefined; | ||
|
|
||
| if (input instanceof Request) { | ||
| const headers = new Headers(input.headers); | ||
| if (init?.headers) { | ||
| const extra = new Headers(init.headers); | ||
| extra.forEach((value, key) => { | ||
| headers.set(key, value); | ||
| }); | ||
| } | ||
|
|
||
| const out: { | ||
| url: string; | ||
| method: string; | ||
| headers: Headers; | ||
| body?: RequestInit['body']; | ||
| signal?: AbortSignal | null; | ||
| duplex?: RequestInit['duplex']; | ||
| timeout_ms?: number; | ||
| } = { | ||
| url: input.url, | ||
| method: (init?.method ?? input.method)?.toUpperCase() || 'GET', | ||
| headers, | ||
| }; | ||
| const body = init?.body ?? input.body; | ||
| if (body !== undefined && body !== null) { | ||
| out.body = body; | ||
| } | ||
| const signal = init?.signal ?? input.signal; | ||
| if (signal !== undefined) { | ||
| out.signal = signal; | ||
| } | ||
| if (init?.duplex !== undefined) { | ||
| out.duplex = init.duplex; | ||
| } | ||
| if (timeoutFromInit !== undefined) { | ||
| out.timeout_ms = timeoutFromInit; | ||
| } | ||
| return out; | ||
| } | ||
|
|
||
| const out: { | ||
| url: string; | ||
| method: string; | ||
| headers: Headers; | ||
| body?: RequestInit['body']; | ||
| signal?: AbortSignal | null; | ||
| duplex?: RequestInit['duplex']; | ||
| timeout_ms?: number; | ||
| } = { | ||
| url: input instanceof URL ? input.href : String(input), | ||
| method: (init?.method ?? 'GET').toUpperCase(), | ||
| headers: new Headers(init?.headers), | ||
| }; | ||
| if (init?.body !== undefined) { | ||
| out.body = init.body; | ||
| } | ||
| if (init?.signal !== undefined) { | ||
| out.signal = init.signal; | ||
| } | ||
| if (init?.duplex !== undefined) { | ||
| out.duplex = init.duplex; | ||
| } | ||
| if (timeoutFromInit !== undefined) { | ||
| out.timeout_ms = timeoutFromInit; | ||
| } | ||
| return out; | ||
| } | ||
|
|
||
| function assertHTTPURL(url: string): void { | ||
| let parsed: URL; | ||
| try { | ||
| parsed = new URL(url); | ||
| } catch { | ||
| throw new KernelError(`browser.fetch target must be an absolute URL; received: ${url}`); | ||
| } | ||
|
|
||
| if (parsed.protocol !== 'http:' && parsed.protocol !== 'https:') { | ||
| throw new KernelError(`browser.fetch only supports http(s) URLs; received: ${parsed.protocol}`); | ||
| } | ||
| } | ||
|
|
||
| function headersToRequestOptionsHeaders(headers: Headers): Record<string, string | null | undefined> { | ||
| const out: Record<string, string | null | undefined> = {}; | ||
|
|
||
| headers.forEach((value, key) => { | ||
| switch (key.toLowerCase()) { | ||
| case 'accept': | ||
| case 'content-length': | ||
| case 'connection': | ||
| case 'keep-alive': | ||
| case 'proxy-authenticate': | ||
| case 'proxy-authorization': | ||
| case 'te': | ||
| case 'trailers': | ||
| case 'transfer-encoding': | ||
| case 'upgrade': | ||
| return; | ||
| default: | ||
| out[key] = value; | ||
| } | ||
| }); | ||
|
|
||
| return out; | ||
| } | ||
Oops, something went wrong.
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.