UNPKG

@theia/core

Version:

Theia is a cloud & desktop IDE framework implemented in TypeScript.

148 lines • 6.08 kB
/******************************************************************************** * Copyright (C) 2022 TypeFox and others. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v. 2.0 which is available at * http://www.eclipse.org/legal/epl-2.0. * * This Source Code may also be made available under the following Secondary * Licenses when the conditions for such availability set forth in the Eclipse * Public License v. 2.0 are satisfied: GNU General Public License, version 2 * with the GNU Classpath Exception which is available at * https://www.gnu.org/software/classpath/license.html. * * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 ********************************************************************************/ import { __decorate } from "tslib"; import { inject, injectable, postConstruct } from 'inversify'; import { BackendRequestService, RequestContext } from '@theia/request'; import { PreferenceService } from '../../common/index.js'; let AbstractBrowserRequestService = class AbstractBrowserRequestService { constructor() { this.configurePromise = Promise.resolve(); } init() { this.configurePromise = this.preferenceService.ready.then(() => { const proxyUrl = this.preferenceService.get('http.proxy'); const proxyAuthorization = this.preferenceService.get('http.proxyAuthorization'); const strictSSL = this.preferenceService.get('http.proxyStrictSSL'); return this.configure({ proxyUrl, proxyAuthorization, strictSSL }); }); this.preferenceService.onPreferencesChanged(e => { this.configurePromise.then(() => this.configure({ proxyUrl: this.preferenceService.get('http.proxy'), proxyAuthorization: this.preferenceService.get('http.proxyAuthorization'), strictSSL: this.preferenceService.get('http.proxyStrictSSL') })); }); } }; __decorate([ inject(PreferenceService) ], AbstractBrowserRequestService.prototype, "preferenceService", void 0); __decorate([ postConstruct() ], AbstractBrowserRequestService.prototype, "init", null); AbstractBrowserRequestService = __decorate([ injectable() ], AbstractBrowserRequestService); export { AbstractBrowserRequestService }; let ProxyingBrowserRequestService = class ProxyingBrowserRequestService extends AbstractBrowserRequestService { configure(config) { return this.backendRequestService.configure(config); } resolveProxy(url) { return this.backendRequestService.resolveProxy(url); } async request(options) { // Wait for both the preferences and the configuration of the backend service await this.configurePromise; const backendResult = await this.backendRequestService.request(options); return RequestContext.decompress(backendResult); } }; __decorate([ inject(BackendRequestService) ], ProxyingBrowserRequestService.prototype, "backendRequestService", void 0); ProxyingBrowserRequestService = __decorate([ injectable() ], ProxyingBrowserRequestService); export { ProxyingBrowserRequestService }; let XHRBrowserRequestService = class XHRBrowserRequestService extends ProxyingBrowserRequestService { async request(options, token) { try { const xhrResult = await this.xhrRequest(options, token); const statusCode = xhrResult.res.statusCode ?? 200; if (statusCode >= 400) { // We might've been blocked by the firewall // Try it through the backend request service return super.request(options); } return xhrResult; } catch { return super.request(options); } } xhrRequest(options, token) { const xhr = new XMLHttpRequest(); return new Promise((resolve, reject) => { xhr.open(options.type || 'GET', options.url || '', true, options.user, options.password); this.setRequestHeaders(xhr, options); xhr.responseType = 'arraybuffer'; xhr.onerror = () => reject(new Error(xhr.statusText && ('XHR failed: ' + xhr.statusText) || 'XHR failed')); xhr.onload = () => { resolve({ url: options.url, res: { statusCode: xhr.status, headers: this.getResponseHeaders(xhr) }, buffer: new Uint8Array(xhr.response) }); }; xhr.ontimeout = e => reject(new Error(`XHR timeout: ${options.timeout}ms`)); if (options.timeout) { xhr.timeout = options.timeout; } xhr.send(options.data); token?.onCancellationRequested(() => { xhr.abort(); reject(); }); }); } setRequestHeaders(xhr, options) { if (options.headers) { for (const k of Object.keys(options.headers)) { switch (k) { case 'User-Agent': case 'Accept-Encoding': case 'Content-Length': // unsafe headers continue; } xhr.setRequestHeader(k, options.headers[k]); } } } getResponseHeaders(xhr) { const headers = {}; for (const line of xhr.getAllResponseHeaders().split(/\r\n|\n|\r/g)) { if (line) { const idx = line.indexOf(':'); headers[line.substring(0, idx).trim().toLowerCase()] = line.substring(idx + 1).trim(); } } return headers; } }; XHRBrowserRequestService = __decorate([ injectable() ], XHRBrowserRequestService); export { XHRBrowserRequestService }; //# sourceMappingURL=browser-request-service.js.map