Skip to content

Commit

Permalink
fix: fix socket info if fetch failed (#556)
Browse files Browse the repository at this point in the history
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

## Summary by CodeRabbit

- **New Features**
- Enhanced error handling in the fetch functionality for better
diagnostics and tracking of request timing.
- Improved compatibility with Node.js 16 by ensuring the presence of
global objects.

- **Bug Fixes**
- Added robust error handling for socket information retrieval during
fetch operations.

- **Tests**
- Introduced a new test case for the fetch function to validate error
handling and ensure diagnostic messages include socket information.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
  • Loading branch information
killagu authored Dec 4, 2024
1 parent f2610d5 commit e9f4258
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,8 @@ export class FetchFactory {
res = await UndiciFetch(input, init);
});
} catch (e: any) {
updateSocketInfo(socketInfo, internalOpaque /* , rawError */);
urllibResponse.rt = performanceTime(requestStartTime);
channels.fetchResponse.publish({
fetch: fetchMeta,
error: e,
Expand Down
1 change: 1 addition & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ export function isReadable(stream: any) {

export function updateSocketInfo(socketInfo: SocketInfo, internalOpaque: any, err?: any) {
const socket = internalOpaque[symbols.kRequestSocket] ?? err?.[symbols.kErrorSocket];

if (socket) {
socketInfo.id = socket[symbols.kSocketId];
socketInfo.handledRequests = socket[symbols.kHandledRequests];
Expand Down
40 changes: 40 additions & 0 deletions test/fetch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,44 @@ describe('fetch.test.ts', () => {
assert(stats);
assert(Object.keys(stats).length > 0);
});

it('fetch error should has socket info', async () => {
let requestDiagnosticsMessage: RequestDiagnosticsMessage;
let responseDiagnosticsMessage: ResponseDiagnosticsMessage;
let fetchDiagnosticsMessage: FetchDiagnosticsMessage;
let fetchResponseDiagnosticsMessage: FetchResponseDiagnosticsMessage;
diagnosticsChannel.subscribe('urllib:request', msg => {
requestDiagnosticsMessage = msg as RequestDiagnosticsMessage;
});
diagnosticsChannel.subscribe('urllib:response', msg => {
responseDiagnosticsMessage = msg as ResponseDiagnosticsMessage;
});
diagnosticsChannel.subscribe('urllib:fetch:request', msg => {
fetchDiagnosticsMessage = msg as FetchDiagnosticsMessage;
});
diagnosticsChannel.subscribe('urllib:fetch:response', msg => {
fetchResponseDiagnosticsMessage = msg as FetchResponseDiagnosticsMessage;
});
FetchFactory.setClientOptions({});

try {
await fetch(`${_url}html?timeout=9999`, {
signal: AbortSignal.timeout(100),
});
} catch (error) {
console.log(error);
}

assert(requestDiagnosticsMessage!.request);
assert(responseDiagnosticsMessage!.request);
assert(responseDiagnosticsMessage!.response);
assert([ '127.0.0.1', '::1' ].includes(responseDiagnosticsMessage!.response.socket.localAddress));

assert(fetchDiagnosticsMessage!.fetch);
assert(fetchResponseDiagnosticsMessage!.fetch);

const stats = FetchFactory.getDispatcherPoolStats();
assert(stats);
assert(Object.keys(stats).length > 0);
});
});

0 comments on commit e9f4258

Please sign in to comment.