@theia/core 1.78.0-next.0 → 1.78.0-next.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -21,12 +21,34 @@ import { ILogger } from '../../common/index.js';
21
21
 
22
22
  decorate(injectable(), NodeRequestService);
23
23
 
24
+ type ProxyResponseHandler = (result: string | undefined) => void;
25
+
24
26
  @injectable()
25
27
  export class ElectronBackendRequestService extends NodeRequestService {
26
28
 
27
29
  @inject(ILogger) @named('core:ElectronBackendRequestService')
28
30
  protected readonly logger: ILogger;
29
31
 
32
+ private static readonly pendingProxyRequests = new Map<string, ProxyResponseHandler>();
33
+ private static proxyResponseListenerAttached = false;
34
+
35
+ private static attachProxyResponseListener(): void {
36
+ if (this.proxyResponseListenerAttached || !process.send) {
37
+ return;
38
+ }
39
+ this.proxyResponseListenerAttached = true;
40
+ process.on('message', (msg: unknown) => {
41
+ const response = msg as { type: string, id: string, result: string };
42
+ if (response?.type === 'resolve-proxy-response' && response.id) {
43
+ const handler = this.pendingProxyRequests.get(response.id);
44
+ if (handler) {
45
+ this.pendingProxyRequests.delete(response.id);
46
+ handler(response.result);
47
+ }
48
+ }
49
+ });
50
+ }
51
+
30
52
  override async getProxyUrl(url: string): Promise<string | undefined> {
31
53
  if (this.proxyUrl) {
32
54
  return this.proxyUrl;
@@ -34,8 +56,15 @@ export class ElectronBackendRequestService extends NodeRequestService {
34
56
  try {
35
57
  const proxy = await this.resolveProxy(url);
36
58
  if (proxy && proxy !== 'DIRECT') {
37
- const proxyHost = proxy.split(' ')[1];
38
- return this.buildProxyUrl(url, proxyHost);
59
+ // Handle fallback proxies separated by semicolons, e.g., "PROXY proxy:8080; DIRECT"
60
+ const firstProxy = proxy.split(';')[0].trim();
61
+ const proxyType = firstProxy.split(' ')[0]; // e.g., "PROXY", "HTTPS"
62
+ const proxyHost = firstProxy.split(' ')[1];
63
+ if (!proxyHost) {
64
+ // Malformed proxy string, fall back
65
+ return super.getProxyUrl(url);
66
+ }
67
+ return this.buildProxyUrl(proxyHost, proxyType);
39
68
  }
40
69
  } catch (e) {
41
70
  this.logger.error('Could not resolve electron proxy.', e);
@@ -44,20 +73,39 @@ export class ElectronBackendRequestService extends NodeRequestService {
44
73
  }
45
74
 
46
75
  override async resolveProxy(url: string): Promise<string | undefined> {
47
- // TODO: Implement IPC to the backend to access the Electron proxy resolver
48
- return undefined;
76
+ if (!process.send) {
77
+ // Running in --no-cluster mode (in the Electron main process): call session directly.
78
+ const { session } = await import('../../../electron-shared/electron/index.cjs');
79
+ return session.defaultSession.resolveProxy(url);
80
+ }
81
+ // Running as a forked child process: ask the main process via Node IPC.
82
+ return new Promise<string | undefined>(resolve => {
83
+ const id = Math.random().toString(36).slice(2);
84
+ ElectronBackendRequestService.attachProxyResponseListener();
85
+ const timer = setTimeout(() => {
86
+ ElectronBackendRequestService.pendingProxyRequests.delete(id);
87
+ resolve(undefined);
88
+ }, 5000);
89
+ ElectronBackendRequestService.pendingProxyRequests.set(id, (result: string | undefined) => {
90
+ clearTimeout(timer);
91
+ resolve(result);
92
+ });
93
+ process.send!({ type: 'resolve-proxy', id, url });
94
+ });
49
95
  }
50
96
 
51
- protected buildProxyUrl(url: string, proxyHost: string): string {
97
+ protected buildProxyUrl(proxyHost: string, proxyType: string): string | undefined {
98
+ if (proxyType !== 'HTTPS' && proxyType !== 'PROXY') {
99
+ return undefined;
100
+ }
101
+
52
102
  if (proxyHost.startsWith('http://') || proxyHost.startsWith('https://')) {
53
103
  return proxyHost;
54
104
  }
55
- if (url.startsWith('http://')) {
56
- return 'http://' + proxyHost;
57
- } else if (url.startsWith('https://')) {
58
- return 'https://' + proxyHost;
59
- }
60
- return proxyHost;
105
+
106
+ return proxyType === 'HTTPS'
107
+ ? 'https://' + proxyHost
108
+ : 'http://' + proxyHost;
61
109
  }
62
110
 
63
111
  protected override async processOptions(options: NodeRequestOptions): Promise<NodeRequestOptions> {