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.
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.
/*---------------------------------------------------------------------------------------------
agentHostRequestService.ts ×11
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { newWriteableBufferStream, VSBuffer, VSBufferWriteableStream } from '../../../base/common/buffer.js';
import { timeout } from '../../../base/common/async.js';
import { CancellationToken } from '../../../base/common/cancellation.js';
import { CancellationError, isCancellationError } from '../../../base/common/errors.js';
import { IDisposable } from '../../../base/common/lifecycle.js';
import { IHeaders, IRequestContext, IRequestOptions } from '../../../base/parts/request/common/request.js';
import { IConfigurationService } from '../../configuration/common/configuration.js';
import { INativeEnvironmentService } from '../../environment/common/environment.js';
import { ILogService } from '../../log/common/log.js';
import { RequestService } from '../../request/node/requestService.js';
import { IAgentHostProxyResolver } from './agentHostProxyResolver.js';
const TRANSIENT_ERROR_CODES = new Set([
'EAI_AGAIN',
'ECONNREFUSED',
'EHOSTDOWN',
'EHOSTUNREACH',
'ENETDOWN',
'ENETUNREACH',
'EPROTO',
]);
const IDEMPOTENT_HTTP_METHODS_REGEX = /^(GET|HEAD|OPTIONS)$/i;
if (error instanceof Error) {
const code = (error as NodeJS.ErrnoException).code;
return !!code && TRANSIENT_ERROR_CODES.has(code);
}
return false;
}
/**
* Request service implemented on the agent host's `@vscode/proxy-agent`
* patched fetch, including renderer-backed system/PAC resolution and VS Code's
* certificate settings. The base {@link RequestService} remains unchanged for
* all other Node consumers.
*/
export class AgentHostRequestService extends RequestService {
constructor(
@IConfigurationService configurationService: IConfigurationService,
agentHostRequestService.ts ×12
@INativeEnvironmentService environmentService: INativeEnvironmentService,
@ILogService logService: ILogService,
@IAgentHostProxyResolver private readonly _proxyResolver: IAgentHostProxyResolver,
) {
super('local', configurationService, environmentService, logService);
}
override request(options: IRequestOptions, token: CancellationToken): Promise<IRequestContext> {
return this.logAndRequest(options, () => this._request(options, token));
agentHostRequestService.ts ×12
}
override resolveProxy(url: string): Promise<string | undefined> {
return this._proxyResolver.resolveProxy(url);
}
private async _request(options: IRequestOptions, token: CancellationToken): Promise<IRequestContext> {
let lastError: Error | undefined;
const isIdempotent = IDEMPOTENT_HTTP_METHODS_REGEX.test(options.type || 'GET');
for (let attempt = 1; attempt <= maxRetries; attempt++) {
try {
return await this._requestAttempt(options, token);
} catch (error) {
if (isCancellationError(error)) {
}
if (!isIdempotent || !isTransientError(error) || attempt === maxRetries) {
agentHostRequestService.ts ×5
}
}
throw lastError;
private async _requestAttempt(options: IRequestOptions, token: CancellationToken): Promise<IRequestContext> {
throw new CancellationError();
}
const cancellation = new AbortController();
const cancellationListener = token.onCancellationRequested(() => cancellation.abort());
const signal = options.timeout
? AbortSignal.any([cancellation.signal, AbortSignal.timeout(options.timeout)])
try {
const response = await this._proxyResolver.fetch(options.url || '', {
method: options.type || 'GET',
headers: getRequestHeaders(options),
body: options.data,
signal,
cache: options.disableCache ? 'no-store' : undefined,
});
? responseBodyToStream(response.body, cancellation, cancellationListener)
: emptyResponseStream(cancellationListener);
res: {
statusCode: response.status,
headers: getResponseHeaders(response),
},
stream,
};
} catch (error) {
if (error instanceof Error && error.name === 'AbortError') {
}
throw new Error(`Fetch timeout: ${options.timeout}ms`);
}
}
function getRequestHeaders(options: IRequestOptions): Headers | undefined {
agentHostRequestService.ts ×12
if (!options.headers && !options.user && !options.password && !options.proxyAuthorization) {
}
for (const key in options.headers) {
const value = options.headers[key];
if (typeof value === 'string') {
headers.set(key, value);
} else if (Array.isArray(value)) {
for (const item of value) {
headers.append(key, item);
}
}
headers.set('Authorization', `Basic ${btoa(`${options.user || ''}:${options.password || ''}`)}`);
}
headers.set('Proxy-Authorization', options.proxyAuthorization);
}
}
const headers: IHeaders = Object.create(null);
response.headers.forEach((value, key) => {
headers[key] = value;
});
return headers;
}
function emptyResponseStream(cancellationListener: IDisposable): VSBufferWriteableStream {
const stream = newWriteableBufferStream();
stream.end();
cancellationListener.dispose();
return stream;
}
function responseBodyToStream(body: ReadableStream<Uint8Array>, cancellation: AbortController, cancellationListener: IDisposable): VSBufferWriteableStream {
agentHostRequestService.ts ×6
const reader = body.getReader();
const stream = newWriteableBufferStream({ highWaterMark: 16 });
const destroy = stream.destroy.bind(stream);
stream.destroy = () => {
cancellation.abort();
void reader.cancel();
cancellationListener.dispose();
destroy();
};
return stream;
}
async function pumpResponseBody(reader: ReadableStreamDefaultReader<Uint8Array>, stream: VSBufferWriteableStream, cancellationListener: IDisposable): Promise<void> {
agentHostRequestService.ts ×6
try {
while (true) {
const { done, value } = await reader.read();
if (done) {
break;
}
await stream.write(VSBuffer.wrap(value));
}
stream.end();
} catch (error) {
stream.error(error instanceof Error ? error : new Error(String(error)));
stream.end();
cancellationListener.dispose();
}
}