-
Notifications
You must be signed in to change notification settings - Fork 124
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
fix: fix socket info in fetch response #555
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { | ||
Agent, | ||
Dispatcher, | ||
} from 'undici'; | ||
import { AsyncLocalStorage } from 'node:async_hooks'; | ||
import { FetchOpaque } from './FetchOpaqueInterceptor.js'; | ||
|
||
export interface BaseAgentOptions extends Agent.Options { | ||
opaqueLocalStorage?: AsyncLocalStorage<FetchOpaque>; | ||
} | ||
|
||
export class BaseAgent extends Agent { | ||
#opaqueLocalStorage?: AsyncLocalStorage<FetchOpaque>; | ||
|
||
constructor(options: BaseAgentOptions) { | ||
super(options); | ||
this.#opaqueLocalStorage = options.opaqueLocalStorage; | ||
} | ||
|
||
dispatch(options: Agent.DispatchOptions, handler: Dispatcher.DispatchHandler): boolean { | ||
const opaque = this.#opaqueLocalStorage?.getStore(); | ||
if (opaque) { | ||
(handler as any).opaque = opaque; | ||
} | ||
return super.dispatch(options, handler); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,6 +46,7 @@ | |
assert(requestDiagnosticsMessage!.request); | ||
assert(responseDiagnosticsMessage!.request); | ||
assert(responseDiagnosticsMessage!.response); | ||
assert.equal(responseDiagnosticsMessage!.response.socket.localAddress, '127.0.0.1'); | ||
Check failure on line 49 in test/fetch.test.ts GitHub Actions / Node.js / Test (macos-latest, 18)test/fetch.test.ts > fetch.test.ts > fetch should work
Check failure on line 49 in test/fetch.test.ts GitHub Actions / Node.js / Test (macos-latest, 18.19.0)test/fetch.test.ts > fetch.test.ts > fetch should work
Check failure on line 49 in test/fetch.test.ts GitHub Actions / Node.js / Test (macos-latest, 20)test/fetch.test.ts > fetch.test.ts > fetch should work
Check failure on line 49 in test/fetch.test.ts GitHub Actions / Node.js / Test (macos-latest, 22)test/fetch.test.ts > fetch.test.ts > fetch should work
Check failure on line 49 in test/fetch.test.ts GitHub Actions / Node.js / Test (macos-latest, 23)test/fetch.test.ts > fetch.test.ts > fetch should work
Check failure on line 49 in test/fetch.test.ts GitHub Actions / Node.js / Test (ubuntu-latest, 18)test/fetch.test.ts > fetch.test.ts > fetch should work
Check failure on line 49 in test/fetch.test.ts GitHub Actions / Node.js / Test (ubuntu-latest, 18.19.0)test/fetch.test.ts > fetch.test.ts > fetch should work
Check failure on line 49 in test/fetch.test.ts GitHub Actions / Node.js / Test (ubuntu-latest, 20)test/fetch.test.ts > fetch.test.ts > fetch should work
Check failure on line 49 in test/fetch.test.ts GitHub Actions / Node.js / Test (ubuntu-latest, 22)test/fetch.test.ts > fetch.test.ts > fetch should work
Check failure on line 49 in test/fetch.test.ts GitHub Actions / Node.js / Test (ubuntu-latest, 23)test/fetch.test.ts > fetch.test.ts > fetch should work
Check failure on line 49 in test/fetch.test.ts GitHub Actions / Node.js / Test (windows-latest, 18)test/fetch.test.ts > fetch.test.ts > fetch should work
Check failure on line 49 in test/fetch.test.ts GitHub Actions / Node.js / Test (windows-latest, 18.19.0)test/fetch.test.ts > fetch.test.ts > fetch should work
Check failure on line 49 in test/fetch.test.ts GitHub Actions / Node.js / Test (windows-latest, 20)test/fetch.test.ts > fetch.test.ts > fetch should work
Check failure on line 49 in test/fetch.test.ts GitHub Actions / Node.js / Test (windows-latest, 22)test/fetch.test.ts > fetch.test.ts > fetch should work
Check failure on line 49 in test/fetch.test.ts GitHub Actions / Node.js / Test (windows-latest, 23)test/fetch.test.ts > fetch.test.ts > fetch should work
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix Test Assertion to Support IPv6 Loopback Address The test is failing because the Apply this diff to update the assertion: assert.equal(responseDiagnosticsMessage!.response.socket.localAddress, '127.0.0.1');
+ assert(['127.0.0.1', '::1'].includes(responseDiagnosticsMessage!.response.socket.localAddress)); Alternatively, use a regular expression to match any loopback address: + assert(/^(127\.0\.0\.1|::1)$/.test(responseDiagnosticsMessage!.response.socket.localAddress)); This adjustment ensures the test passes regardless of whether the environment uses IPv4 or IPv6 for loopback addresses.
🧰 Tools🪛 GitHub Check: Node.js / Test (ubuntu-latest, 23)[failure] 49-49: test/fetch.test.ts > fetch.test.ts > fetch should work '::1' !== '127.0.0.1' Expected: "127.0.0.1" ❯ test/fetch.test.ts:49:12 🪛 GitHub Check: Node.js / Test (ubuntu-latest, 22)[failure] 49-49: test/fetch.test.ts > fetch.test.ts > fetch should work
Expected: "127.0.0.1" ❯ test/fetch.test.ts:49:12 🪛 GitHub Check: Node.js / Test (ubuntu-latest, 20)[failure] 49-49: test/fetch.test.ts > fetch.test.ts > fetch should work
Expected: "127.0.0.1" ❯ test/fetch.test.ts:49:12 🪛 GitHub Check: Node.js / Test (macos-latest, 22)[failure] 49-49: test/fetch.test.ts > fetch.test.ts > fetch should work
Expected: "127.0.0.1" ❯ test/fetch.test.ts:49:12 |
||
|
||
assert(fetchDiagnosticsMessage!.fetch); | ||
assert(fetchResponseDiagnosticsMessage!.fetch); | ||
|
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.
🛠️ Refactor suggestion
Improve type safety and error handling in dispatch
The current implementation has several areas for improvement:
any
type assertion is unsafeConsider this safer implementation:
📝 Committable suggestion