-
Notifications
You must be signed in to change notification settings - Fork 887
enable http2 for js sdk envd rpc/api traffic #1311
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
Open
matthewlouisbrockman
wants to merge
17
commits into
main
Choose a base branch
from
js-http2
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
4b0e913
Enable HTTP/2 for JS envd requests
matthewlouisbrockman 4ea49ea
changeset
matthewlouisbrockman 4bd1e6b
Refine JS HTTP/2 envd session handling
matthewlouisbrockman 6584e66
Test JS HTTP/2 envd fetch behavior
matthewlouisbrockman 4021a0b
Keep JS HTTP/2 streams active until body close
matthewlouisbrockman 759376c
Clean up HTTP/2 abort listener on request failure
matthewlouisbrockman 6b27786
cleaner changelog
matthewlouisbrockman 33fcacf
Avoid HTTP/2 stream accounting before request starts
matthewlouisbrockman 4ecf177
Follow redirects in JS HTTP/2 fetch
matthewlouisbrockman 85e9b63
Backpressure JS HTTP/2 response streams
matthewlouisbrockman b35cf5e
Derive HTTP/2 session origin from request URL
matthewlouisbrockman df40769
Format JS HTTP/2 fetch adapter
matthewlouisbrockman 754c360
Decode compressed JS HTTP/2 responses
matthewlouisbrockman 7123f5e
Use undici for JS HTTP/2 envd requests
matthewlouisbrockman a84426b
Mark JS HTTP/2 change as patch
matthewlouisbrockman c6ef2d7
Keep JS RPC streams on native fetch
matthewlouisbrockman 3f793cd
Use uncapped undici dispatcher for JS RPC streams
matthewlouisbrockman 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
Some comments aren't visible on the classic Files Changed page.
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 @@ | ||
| --- | ||
| 'e2b': patch | ||
| --- | ||
|
|
||
| Use HTTP/2 for JS SDK envd/api requests and require Node.js 20.18.1 or newer. |
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,103 @@ | ||
| import { dynamicRequire, runtime } from '../utils' | ||
|
|
||
| type Undici = typeof import('undici') | ||
| type UndiciDispatcher = InstanceType<Undici['Agent']> | ||
| type UndiciRequestInit = RequestInit & { | ||
| dispatcher: UndiciDispatcher | ||
| duplex?: 'half' | ||
| } | ||
| type EnvdFetchOptions = { | ||
| connectionLimit?: number | ||
| } | ||
|
|
||
| let envdFetch: typeof fetch | undefined | ||
| let envdRpcFetch: typeof fetch | undefined | ||
|
|
||
| export function createEnvdFetchForRuntime( | ||
| currentRuntime = runtime, | ||
| options: EnvdFetchOptions = { connectionLimit: 1 } | ||
| ): typeof fetch { | ||
| if (currentRuntime !== 'node') { | ||
| return fetch | ||
| } | ||
|
|
||
| const { Agent, fetch: undiciFetch } = dynamicRequire<Undici>('undici') | ||
| const dispatcherOptions: { allowH2: true; connections?: number } = { | ||
| allowH2: true, | ||
| } | ||
| if (options.connectionLimit !== undefined) { | ||
| dispatcherOptions.connections = options.connectionLimit | ||
| } | ||
| const dispatcher = new Agent(dispatcherOptions) | ||
| const fetchWithDispatcher = undiciFetch as unknown as ( | ||
| input: RequestInfo | URL, | ||
| init?: UndiciRequestInit | ||
| ) => Promise<Response> | ||
|
|
||
| return ((input, init) => { | ||
| const request = toRequestInput(input, init) | ||
|
|
||
| return fetchWithDispatcher(request.input, { | ||
| ...request.init, | ||
| dispatcher, | ||
| }) | ||
| }) as typeof fetch | ||
| } | ||
|
|
||
| export function createEnvdFetch(): typeof fetch { | ||
| if (envdFetch) { | ||
| return envdFetch | ||
| } | ||
|
|
||
| // Keep one origin connection for short envd REST calls. If ALPN falls back | ||
| // to h1, this favors connection pressure over per-sandbox throughput. | ||
| envdFetch = createEnvdFetchForRuntime(runtime) | ||
|
|
||
| return envdFetch | ||
| } | ||
|
|
||
| export function createEnvdRpcFetch(): typeof fetch { | ||
| if (envdRpcFetch) { | ||
| return envdRpcFetch | ||
| } | ||
|
|
||
| // RPC streams can stay open while follow-up RPCs run against the same | ||
| // sandbox, so they cannot share the REST client's single-connection cap. | ||
| envdRpcFetch = createEnvdFetchForRuntime(runtime, {}) | ||
|
|
||
| return envdRpcFetch | ||
| } | ||
|
|
||
| function toRequestInput( | ||
| input: RequestInfo | URL, | ||
| init?: RequestInit | ||
| ): { input: RequestInfo | URL; init?: RequestInit & { duplex?: 'half' } } { | ||
| if (!(input instanceof Request)) { | ||
| return { input, init } | ||
| } | ||
|
|
||
| const requestInit: RequestInit & { duplex?: 'half' } = { | ||
| body: input.body, | ||
| cache: input.cache, | ||
| credentials: input.credentials, | ||
| headers: input.headers, | ||
| integrity: input.integrity, | ||
| keepalive: input.keepalive, | ||
| method: input.method, | ||
| mode: input.mode, | ||
| redirect: input.redirect, | ||
| referrer: input.referrer, | ||
| referrerPolicy: input.referrerPolicy, | ||
| signal: input.signal, | ||
| ...init, | ||
| } | ||
|
|
||
| if (requestInit.body) { | ||
| requestInit.duplex = 'half' | ||
| } | ||
|
|
||
| return { | ||
| input: input.url, | ||
| init: requestInit, | ||
| } | ||
| } |
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,101 @@ | ||
| import { afterEach, expect, test, vi } from 'vitest' | ||
|
|
||
| afterEach(() => { | ||
| vi.restoreAllMocks() | ||
| vi.resetModules() | ||
| vi.doUnmock('../../src/utils') | ||
| }) | ||
|
|
||
| test('uses undici with HTTP/2 enabled in Node', async () => { | ||
| const agents: Array<{ allowH2?: boolean; connections?: number }> = [] | ||
| const requests: Array<{ init?: RequestInit & { dispatcher?: unknown } }> = [] | ||
|
|
||
| class Agent { | ||
| constructor(options: { allowH2?: boolean; connections?: number }) { | ||
| agents.push(options) | ||
| } | ||
| } | ||
|
|
||
| const undiciFetch = vi.fn((input, init) => { | ||
| requests.push({ init }) | ||
| return Promise.resolve(new Response('ok')) | ||
| }) | ||
|
|
||
| vi.doMock('../../src/utils', () => ({ | ||
| dynamicRequire: () => ({ Agent, fetch: undiciFetch }), | ||
| runtime: 'node', | ||
| })) | ||
| const { createEnvdFetchForRuntime } = await import('../../src/envd/http2') | ||
|
|
||
| const fetcher = createEnvdFetchForRuntime('node') | ||
| const res = await fetcher('https://example.com/status') | ||
|
|
||
| expect(await res.text()).toBe('ok') | ||
| expect(agents).toEqual([{ allowH2: true, connections: 1 }]) | ||
| expect(requests[0].init?.dispatcher).toBeInstanceOf(Agent) | ||
| }) | ||
|
|
||
| test('passes Request objects to undici as URL plus init', async () => { | ||
| const requests: Array<{ input: RequestInfo | URL; init?: RequestInit }> = [] | ||
|
|
||
| class Agent {} | ||
|
|
||
| const undiciFetch = vi.fn((input, init) => { | ||
| requests.push({ input, init }) | ||
| return Promise.resolve(new Response('ok')) | ||
| }) | ||
|
|
||
| vi.doMock('../../src/utils', () => ({ | ||
| dynamicRequire: () => ({ Agent, fetch: undiciFetch }), | ||
| runtime: 'node', | ||
| })) | ||
| const { createEnvdFetchForRuntime } = await import('../../src/envd/http2') | ||
|
|
||
| const fetcher = createEnvdFetchForRuntime('node') | ||
| const body = JSON.stringify({ ok: true }) | ||
| await fetcher( | ||
| new Request('https://example.com/rpc', { | ||
| body, | ||
| headers: { 'content-type': 'application/json' }, | ||
| method: 'POST', | ||
| }) | ||
| ) | ||
|
|
||
| expect(requests[0].input).toBe('https://example.com/rpc') | ||
| expect(requests[0].init?.method).toBe('POST') | ||
| expect(requests[0].init?.headers).toBeInstanceOf(Headers) | ||
| expect(requests[0].init?.body).toBeInstanceOf(ReadableStream) | ||
| }) | ||
|
|
||
| test('can create an uncapped dispatcher for RPC streams', async () => { | ||
| const agents: Array<{ allowH2?: boolean; connections?: number }> = [] | ||
|
|
||
| class Agent { | ||
| constructor(options: { allowH2?: boolean; connections?: number }) { | ||
| agents.push(options) | ||
| } | ||
| } | ||
|
|
||
| const undiciFetch = vi.fn(() => Promise.resolve(new Response('ok'))) | ||
|
|
||
| vi.doMock('../../src/utils', () => ({ | ||
| dynamicRequire: () => ({ Agent, fetch: undiciFetch }), | ||
| runtime: 'node', | ||
| })) | ||
| const { createEnvdFetchForRuntime } = await import('../../src/envd/http2') | ||
|
|
||
| const fetcher = createEnvdFetchForRuntime('node', {}) | ||
| await fetcher('https://example.com/rpc') | ||
|
|
||
| expect(agents).toEqual([{ allowH2: true }]) | ||
| }) | ||
|
|
||
| test('uses global fetch outside Node', async () => { | ||
| const fallbackFetch = vi.fn() as unknown as typeof fetch | ||
| vi.stubGlobal('fetch', fallbackFetch) | ||
|
|
||
| const { createEnvdFetchForRuntime } = await import('../../src/envd/http2') | ||
|
|
||
| expect(createEnvdFetchForRuntime('browser')).toBe(fallbackFetch) | ||
| expect(createEnvdFetchForRuntime('vercel-edge')).toBe(fallbackFetch) | ||
| }) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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.