-
Notifications
You must be signed in to change notification settings - Fork 15
Workaround premature-close issue in 24.17.0 #191
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
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 |
|---|---|---|
| @@ -1,4 +1,63 @@ | ||
| /** | ||
| * Disclaimer: modules in _shims aren't intended to be imported by SDK users. | ||
| */ | ||
| export * from '../node-runtime'; | ||
| import { Readable } from 'node:stream'; | ||
| import { getRuntime as getNodeRuntime } from '../node-runtime'; | ||
| import { type Shims } from '../registry'; | ||
|
|
||
| type FetchInitWithAgent = { | ||
| agent?: unknown; | ||
| body?: unknown; | ||
| [key: string]: unknown; | ||
| }; | ||
|
|
||
| function usesNodeFetchOnlyFeatures(init: FetchInitWithAgent | undefined): boolean { | ||
| if (!init) return false; | ||
|
|
||
| // Node's native fetch does not use node:http Agent instances. Preserve | ||
| // historical behavior for callers who explicitly configure httpAgent. | ||
| return Boolean(init.agent); | ||
| } | ||
|
|
||
| function buildNativeFetchOptions(init: FetchInitWithAgent | undefined): FetchInitWithAgent | undefined { | ||
| if (!init) return init; | ||
|
|
||
| const { agent: _agent, ...fetchInit } = init; | ||
|
|
||
| // Node's native fetch requires `duplex: 'half'` when the body is a stream. | ||
| const body = fetchInit.body; | ||
| if (body instanceof Readable || typeof (body as any)?.pipe === 'function') { | ||
| return { duplex: 'half', ...fetchInit }; | ||
| } | ||
|
|
||
| return fetchInit; | ||
| } | ||
|
|
||
| export function getRuntime(): Shims { | ||
| const nodeRuntime = getNodeRuntime(); | ||
| const nativeFetch = (globalThis as any).fetch; | ||
|
|
||
| if (typeof nativeFetch !== 'function') { | ||
| return nodeRuntime; | ||
| } | ||
|
|
||
| return { | ||
| ...nodeRuntime, | ||
| fetch: (url: unknown, init?: FetchInitWithAgent) => { | ||
| if (usesNodeFetchOnlyFeatures(init)) { | ||
| return nodeRuntime.fetch(url, init); | ||
| } | ||
|
|
||
| return nativeFetch.call(undefined, url, buildNativeFetchOptions(init)); | ||
| }, | ||
| Request: | ||
| typeof (globalThis as any).Request !== 'undefined' ? (globalThis as any).Request : nodeRuntime.Request, | ||
| Response: | ||
| typeof (globalThis as any).Response !== 'undefined' ? | ||
| (globalThis as any).Response | ||
| : nodeRuntime.Response, | ||
| Headers: | ||
| typeof (globalThis as any).Headers !== 'undefined' ? (globalThis as any).Headers : nodeRuntime.Headers, | ||
| getDefaultAgent: () => undefined, | ||
| }; | ||
| } |
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
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
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,56 @@ | ||
| import { getRuntime } from '@browserbasehq/sdk/_shims/auto/runtime'; | ||
| import http from 'node:http'; | ||
| import { Readable } from 'node:stream'; | ||
|
|
||
| describe('auto node runtime', () => { | ||
| const originalFetch = (globalThis as any).fetch; | ||
|
|
||
| afterEach(() => { | ||
| (globalThis as any).fetch = originalFetch; | ||
| }); | ||
|
|
||
| test('prefers native fetch when available', async () => { | ||
| const response = { ok: true }; | ||
| const nativeFetch = jest.fn().mockResolvedValue(response); | ||
| (globalThis as any).fetch = nativeFetch; | ||
|
|
||
| const runtime = getRuntime(); | ||
|
|
||
| await expect(runtime.fetch('https://example.com', { method: 'post', body: '{}' })).resolves.toBe( | ||
| response, | ||
| ); | ||
| expect(nativeFetch).toHaveBeenCalledWith('https://example.com', { method: 'post', body: '{}' }); | ||
| expect(runtime.getDefaultAgent('https://example.com')).toBeUndefined(); | ||
| }); | ||
|
|
||
| test('uses native fetch for stream bodies with duplex enabled', async () => { | ||
| const response = { ok: true }; | ||
| const nativeFetch = jest.fn().mockResolvedValue(response); | ||
| const body = Readable.from(['Example data']); | ||
| (globalThis as any).fetch = nativeFetch; | ||
|
|
||
| const runtime = getRuntime(); | ||
|
|
||
| await expect(runtime.fetch('https://example.com', { method: 'post', body })).resolves.toBe(response); | ||
| expect(nativeFetch).toHaveBeenCalledWith('https://example.com', { | ||
| duplex: 'half', | ||
| method: 'post', | ||
| body, | ||
| }); | ||
| }); | ||
|
|
||
| test('uses node-fetch when an agent is provided', async () => { | ||
| const nativeFetch = jest.fn().mockRejectedValue(new Error('native fetch should not be called')); | ||
| (globalThis as any).fetch = nativeFetch; | ||
|
|
||
| const agent = new http.Agent({ keepAlive: false }); | ||
|
|
||
| try { | ||
| const runtime = getRuntime(); | ||
| await expect(runtime.fetch('not a url', { agent })).rejects.toThrow(); | ||
| expect(nativeFetch).not.toHaveBeenCalled(); | ||
| } finally { | ||
| agent.destroy(); | ||
| } | ||
| }); | ||
| }); | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could we add a test that confirms that when init.agent exists, we prefer node fetch to native
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah done in 78c0d18