src/vs/platform/agentHost/node/agentHostRequestService.ts

197 LOC · 164 covered · 33 uncovered · 44 ranges · 11 concepts · 9 introducers · 6 tests

File neighbourhood

The centred file is linked to every concept that introduces one of its ranges, every test that runs code from the file, and the gray connector concepts standing between those tests and the file's own introducer concepts. Undirected links join concepts to every file where they introduce source and concepts to the tests they introduce; arrows show specialization between the displayed concepts and bridge only concepts omitted from this view. Concept colors match the source ranges below; connector concepts have no source color and are shown in gray.

Focused file, its introducer and connector concepts, their introduced files, and tests that run code from the file

In the embedded map, ordinary wheel input scrolls the page; use the visible controls to zoom and drag to pan. Open the full-screen map for canvas navigation: wheel pans, Ctrl/Command plus wheel zooms, and arrow keys pan when this region is focused. On touch screens, open the full-screen map to pan or pinch. If JavaScript or WebGL is unavailable, use the related-file, concept, and source links on this page.

Focused file, its introducer and connector concepts, their introduced files, and tests that run code from the filesrc/vs/platform/agentHost/common/agentHostClientProxyChannel.ts · 83 LOCcommon/agentHostClientPr…src/vs/platform/agentHost/node/networkDiagnosticsService.ts · 194 LOCnode/networkDiagnosticsS…agentHostRequestService.ts ×2 · 7 introduced LOCagentHostRequestService.…agentHostRequestService.ts ×4 · 10 introduced LOCagentHostRequestService.…agentHostRequestService.ts ×6 · 32 introduced LOCagentHostRequestService.…agentHostRequestService.ts ×2 · 4 introduced LOCagentHostRequestService.…agentHostRequestService.ts ×1 · 2 introduced LOCagentHostRequestService.…agentHostRequestService.ts ×1 · 2 introduced LOCagentHostRequestService.…agentHostRequestService.ts ×5 · 8 introduced LOCagentHostRequestService.…agentHostRequestService.ts ×12 · 46 introduced LOCagentHostRequestService.…networkDiagnosticsService.ts ×11 · 83 introduced LOCnetworkDiagnosticsServic…agentHostClientProxyChannel.ts ×4 · 26 introduced LOCagentHostClientProxyChan…agentHostRequestService.ts ×11 · 179 introduced LOCagentHostRequestService.…agentHostRequestService.test|title=AgentHostRequestService does not retry non-idempotent requests|occurrence=1 · introduced test · mocha:v1|namespace=vscode@05c208e9e28d8c1c723fa08f85e2b7a96092e8e5|file=vs/platform/agentHost/test/node/agentHostRequestService.test|title=AgentHostRequestService does not retry non-idempotent requests|occurrence=1agentHostRequestService.…agentHostRequestService.test|title=AgentHostRequestService forwards cancellation to resolver fetch|occurrence=1 · introduced test · mocha:v1|namespace=vscode@05c208e9e28d8c1c723fa08f85e2b7a96092e8e5|file=vs/platform/agentHost/test/node/agentHostRequestService.test|title=AgentHostRequestService forwards cancellation to resolver fetch|occurrence=1agentHostRequestService.…agentHostRequestService.test|title=AgentHostRequestService forwards proxy and authorization lookups through the client channel|occurrence=1 · introduced test · mocha:v1|namespace=vscode@05c208e9e28d8c1c723fa08f85e2b7a96092e8e5|file=vs/platform/agentHost/test/node/agentHostRequestService.test|title=AgentHostRequestService forwards proxy and authorization lookups through the client channel|occurrence=1agentHostRequestService.…agentHostRequestService.test|title=AgentHostRequestService retries idempotent requests on transient errors|occurrence=1 · introduced test · mocha:v1|namespace=vscode@05c208e9e28d8c1c723fa08f85e2b7a96092e8e5|file=vs/platform/agentHost/test/node/agentHostRequestService.test|title=AgentHostRequestService retries idempotent requests on transient errors|occurrence=1agentHostRequestService.…agentHostRequestService.test|title=AgentHostRequestService uses resolver fetch and streams the response|occurrence=1 · introduced test · mocha:v1|namespace=vscode@05c208e9e28d8c1c723fa08f85e2b7a96092e8e5|file=vs/platform/agentHost/test/node/agentHostRequestService.test|title=AgentHostRequestService uses resolver fetch and streams the response|occurrence=1agentHostRequestService.…agentHostRequestService.test|title=NetworkDiagnosticsService includes nested proxy response errors|occurrence=1 · introduced test · mocha:v1|namespace=vscode@05c208e9e28d8c1c723fa08f85e2b7a96092e8e5|file=vs/platform/agentHost/test/node/agentHostRequestService.test|title=NetworkDiagnosticsService includes nested proxy response errors|occurrence=1agentHostRequestService.…Focused file · src/vs/platform/agentHost/node/agentHostRequestService.ts · 197 LOCnode/agentHostRequestSer…

Graph controls are ready.

Interactive rendering requires JavaScript and WebGL. Use the related-file, concept, and source links on this page while the interactive map is unavailable.

1 > /*--------------------------------------------------------------------------------------------- agentHostRequestService.ts ×11
2 > * Copyright (c) Microsoft Corporation. All rights reserved.
3 > * Licensed under the MIT License. See License.txt in the project root for license information.
4 > *--------------------------------------------------------------------------------------------*/
5 >
6 > import { newWriteableBufferStream, VSBuffer, VSBufferWriteableStream } from '../../../base/common/buffer.js';
7 > import { timeout } from '../../../base/common/async.js';
8 > import { CancellationToken } from '../../../base/common/cancellation.js';
9 > import { CancellationError, isCancellationError } from '../../../base/common/errors.js';
10 > import { IDisposable } from '../../../base/common/lifecycle.js';
11 > import { IHeaders, IRequestContext, IRequestOptions } from '../../../base/parts/request/common/request.js';
12 > import { IConfigurationService } from '../../configuration/common/configuration.js';
13 > import { INativeEnvironmentService } from '../../environment/common/environment.js';
14 > import { ILogService } from '../../log/common/log.js';
15 > import { RequestService } from '../../request/node/requestService.js';
16 > import { IAgentHostProxyResolver } from './agentHostProxyResolver.js';
17 >
18 > const TRANSIENT_ERROR_CODES = new Set([
19 > 'EAI_AGAIN',
20 > 'ECONNREFUSED',
21 > 'EHOSTDOWN',
22 > 'EHOSTUNREACH',
23 > 'ENETDOWN',
24 > 'ENETUNREACH',
25 > 'EPROTO',
26 > ]);
27 >
28 > const IDEMPOTENT_HTTP_METHODS_REGEX = /^(GET|HEAD|OPTIONS)$/i;
29 >
30 > function isTransientError(error: unknown): boolean { agentHostRequestService.ts ×2
31 > if (error instanceof Error) {
32 > const code = (error as NodeJS.ErrnoException).code;
33 > return !!code && TRANSIENT_ERROR_CODES.has(code);
34 > }
35 return false;
36 }
38 > /**
39 > * Request service implemented on the agent host's `@vscode/proxy-agent`
40 > * patched fetch, including renderer-backed system/PAC resolution and VS Code's
41 > * certificate settings. The base {@link RequestService} remains unchanged for
42 > * all other Node consumers.
43 > */
44 > export class AgentHostRequestService extends RequestService {
45 >
46 > constructor(
47 > @IConfigurationService configurationService: IConfigurationService, agentHostRequestService.ts ×12
48 > @INativeEnvironmentService environmentService: INativeEnvironmentService,
49 > @ILogService logService: ILogService,
50 > @IAgentHostProxyResolver private readonly _proxyResolver: IAgentHostProxyResolver,
51 > ) {
52 > super('local', configurationService, environmentService, logService);
53 > }
55 > override request(options: IRequestOptions, token: CancellationToken): Promise<IRequestContext> {
56 > return this.logAndRequest(options, () => this._request(options, token)); agentHostRequestService.ts ×12
57 > }
59 > override resolveProxy(url: string): Promise<string | undefined> {
60 return this._proxyResolver.resolveProxy(url);
61 }
63 > private async _request(options: IRequestOptions, token: CancellationToken): Promise<IRequestContext> {
64 > const maxRetries = 3; agentHostRequestService.ts ×12
65 > let lastError: Error | undefined;
66 > const isIdempotent = IDEMPOTENT_HTTP_METHODS_REGEX.test(options.type || 'GET');
67 >
68 > for (let attempt = 1; attempt <= maxRetries; attempt++) {
69 > try {
70 > return await this._requestAttempt(options, token);
71 > } catch (error) {
72 > lastError = error as Error; agentHostRequestService.ts ×5
73 > if (isCancellationError(error)) {
74 > throw error; agentHostRequestService.ts ×2
75 > }
76 > if (!isIdempotent || !isTransientError(error) || attempt === maxRetries) { agentHostRequestService.ts ×5
77 > throw error; agentHostRequestService.ts ×1
78 > }
79 > await timeout(100 * attempt, token); agentHostRequestService.ts ×2
80 > }
82
83 throw lastError;
86 > private async _requestAttempt(options: IRequestOptions, token: CancellationToken): Promise<IRequestContext> {
87 > if (token.isCancellationRequested) { agentHostRequestService.ts ×12
88 throw new CancellationError();
89 }
91 > const cancellation = new AbortController();
92 > const cancellationListener = token.onCancellationRequested(() => cancellation.abort());
93 > const signal = options.timeout
94 ? AbortSignal.any([cancellation.signal, AbortSignal.timeout(options.timeout)])
95 > : cancellation.signal; agentHostRequestService.ts ×12
96 >
97 > try {
98 > const response = await this._proxyResolver.fetch(options.url || '', {
99 > method: options.type || 'GET',
100 > headers: getRequestHeaders(options),
101 > body: options.data,
102 > signal,
103 > cache: options.disableCache ? 'no-store' : undefined,
104 > });
105 > const stream = response.body agentHostRequestService.ts ×6
106 > ? responseBodyToStream(response.body, cancellation, cancellationListener)
107 : emptyResponseStream(cancellationListener);
109 > res: {
110 > statusCode: response.status,
111 > headers: getResponseHeaders(response),
112 > },
113 > stream,
114 > };
115 > } catch (error) {
116 > cancellationListener.dispose(); agentHostRequestService.ts ×5
117 > if (error instanceof Error && error.name === 'AbortError') {
118 > throw new CancellationError(); agentHostRequestService.ts ×2
119 > }
120 > if (error instanceof Error && error.name === 'TimeoutError') { agentHostRequestService.ts ×5
121 throw new Error(`Fetch timeout: ${options.timeout}ms`);
122 }
123 > throw error; agentHostRequestService.ts ×1
124 > }
127 >
128 > function getRequestHeaders(options: IRequestOptions): Headers | undefined { agentHostRequestService.ts ×12
129 > if (!options.headers && !options.user && !options.password && !options.proxyAuthorization) {
130 > return undefined; agentHostRequestService.ts ×5
131 > }
132 > const headers = new Headers(); agentHostRequestService.ts ×4
133 > for (const key in options.headers) {
134 > const value = options.headers[key];
135 > if (typeof value === 'string') {
136 > headers.set(key, value);
137 > } else if (Array.isArray(value)) {
138 for (const item of value) {
139 headers.append(key, item);
140 }
141 }
143 > if (options.user || options.password) { agentHostRequestService.ts ×12
144 headers.set('Authorization', `Basic ${btoa(`${options.user || ''}:${options.password || ''}`)}`);
145 }
146 > if (options.proxyAuthorization) { agentHostRequestService.ts ×4
147 headers.set('Proxy-Authorization', options.proxyAuthorization);
148 }
149 > return headers; agentHostRequestService.ts ×4
150 > }
152 > function getResponseHeaders(response: Response): IHeaders { agentHostRequestService.ts ×6
153 > const headers: IHeaders = Object.create(null);
154 > response.headers.forEach((value, key) => {
155 > headers[key] = value;
156 > });
157 > return headers;
158 > }
160 function emptyResponseStream(cancellationListener: IDisposable): VSBufferWriteableStream {
161 const stream = newWriteableBufferStream();
162 stream.end();
163 cancellationListener.dispose();
164 return stream;
165 }
167 > function responseBodyToStream(body: ReadableStream<Uint8Array>, cancellation: AbortController, cancellationListener: IDisposable): VSBufferWriteableStream { agentHostRequestService.ts ×6
168 > const reader = body.getReader();
169 > const stream = newWriteableBufferStream({ highWaterMark: 16 });
170 > const destroy = stream.destroy.bind(stream);
171 > stream.destroy = () => {
172 cancellation.abort();
173 void reader.cancel();
174 cancellationListener.dispose();
175 destroy();
176 };
177 > void pumpResponseBody(reader, stream, cancellationListener); agentHostRequestService.ts ×6
178 > return stream;
179 > }
181 > async function pumpResponseBody(reader: ReadableStreamDefaultReader<Uint8Array>, stream: VSBufferWriteableStream, cancellationListener: IDisposable): Promise<void> { agentHostRequestService.ts ×6
182 > try {
183 > while (true) {
184 > const { done, value } = await reader.read();
185 > if (done) {
186 > break;
187 > }
188 > await stream.write(VSBuffer.wrap(value));
189 > }
190 > stream.end();
191 > } catch (error) {
192 stream.error(error instanceof Error ? error : new Error(String(error)));
193 stream.end();
194 > } finally { agentHostRequestService.ts ×6
195 > cancellationListener.dispose();
196 > }
197 > }