Skip to content
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

Change the global fetch available in local extension hosts to be the one from Electron #228476

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/vs/platform/request/common/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,13 @@ function registerProxyConfigurations(scope: ConfigurationScope): void {
default: false,
description: localize('systemCertificatesV2', "Controls whether experimental loading of CA certificates from the OS should be enabled. This uses a more general approach than the default implemenation."),
restricted: true
},
'http.experimental.electronFetch': {
type: 'boolean',
tags: ['experimental'],
default: true,
description: localize('electronFetch', "Controls whether experimental use of Electron's fetch implementation instead of Node.js' should be enabled. All local extensions will get Electron's fetch implementation for the global fetch API."),
restricted: true
}
}
};
Expand Down
2 changes: 1 addition & 1 deletion src/vs/workbench/api/node/extHostExtensionService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export class ExtHostExtensionService extends AbstractExtHostExtensionService {

// Do this when extension service exists, but extensions are not being activated yet.
const configProvider = await this._extHostConfiguration.getConfigProvider();
await connectProxyResolver(this._extHostWorkspace, configProvider, this, this._logService, this._mainThreadTelemetryProxy, this._initData);
await connectProxyResolver(this._extHostWorkspace, configProvider, this, this._logService, this._mainThreadTelemetryProxy, this._initData, this._store);
performance.mark('code/extHost/didInitProxyResolver');
}

Expand Down
28 changes: 28 additions & 0 deletions src/vs/workbench/api/node/proxyResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { ILogService, LogLevel as LogServiceLevel } from '../../../platform/log/
import { IExtensionDescription } from '../../../platform/extensions/common/extensions.js';
import { LogLevel, createHttpPatch, createProxyResolver, createTlsPatch, ProxySupportSetting, ProxyAgentParams, createNetPatch, loadSystemCertificates } from '@vscode/proxy-agent';
import { AuthInfo } from '../../../platform/request/common/request.js';
import { DisposableStore } from '../../../base/common/lifecycle.js';

// ESM-uncomment-begin
import { createRequire } from 'node:module';
Expand All @@ -31,6 +32,7 @@ const net = require('net');
// ESM-uncomment-end

const systemCertificatesV2Default = false;
const useElectronFetchDefault = true;

export function connectProxyResolver(
extHostWorkspace: IExtHostWorkspaceProvider,
Expand All @@ -39,7 +41,11 @@ export function connectProxyResolver(
extHostLogService: ILogService,
mainThreadTelemetry: MainThreadTelemetryShape,
initData: IExtensionHostInitData,
disposables: DisposableStore,
) {

patchGlobalFetch(configProvider, initData, disposables);

const useHostProxy = initData.environment.useHostProxy;
const doUseHostProxy = typeof useHostProxy === 'boolean' ? useHostProxy : !initData.remote.isRemote;
const params: ProxyAgentParams = {
Expand Down Expand Up @@ -94,6 +100,28 @@ export function connectProxyResolver(
return configureModuleLoading(extensionService, lookup);
}

function patchGlobalFetch(configProvider: ExtHostConfigProvider, initData: IExtensionHostInitData, disposables: DisposableStore) {
if (!initData.remote.isRemote && !(globalThis as any).__originalFetch) {
const originalFetch = globalThis.fetch;
(globalThis as any).__originalFetch = originalFetch;
let useElectronFetch = configProvider.getConfiguration('http').get<boolean>('experimental.electronFetch', useElectronFetchDefault);
disposables.add(configProvider.onDidChangeConfiguration(e => {
if (e.affectsConfiguration('http.experimental.electronFetch')) {
useElectronFetch = configProvider.getConfiguration('http').get<boolean>('experimental.electronFetch', useElectronFetchDefault);
}
}));
const electron = require('electron');
globalThis.fetch = function fetch(input: any /* RequestInfo */ | URL, init?: RequestInit) {
if (!useElectronFetch) {
return originalFetch(input, init);
}
// Support for URL: https://github.com/electron/electron/issues/43712
const electronInput = input instanceof URL ? input.toString() : input;
return electron.net.fetch(electronInput, init);
};
}
}

function createPatchedModules(params: ProxyAgentParams, resolveProxy: ReturnType<typeof createProxyResolver>) {

function mergeModules(module: any, patch: any) {
Expand Down
Loading