UNPKG

2.77 kBJavaScriptView Raw
1"use strict";
2// *****************************************************************************
3// Copyright (C) 2019 TypeFox and others.
4//
5// This program and the accompanying materials are made available under the
6// terms of the Eclipse Public License v. 2.0 which is available at
7// http://www.eclipse.org/legal/epl-2.0.
8//
9// This Source Code may also be made available under the following Secondary
10// Licenses when the conditions for such availability set forth in the Eclipse
11// Public License v. 2.0 are satisfied: GNU General Public License, version 2
12// with the GNU Classpath Exception which is available at
13// https://www.gnu.org/software/classpath/license.html.
14//
15// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
16// *****************************************************************************
17Object.defineProperty(exports, "__esModule", { value: true });
18exports.ExternalUriService = void 0;
19const tslib_1 = require("tslib");
20const environment_1 = require("@theia/application-package/lib/environment");
21const inversify_1 = require("inversify");
22const endpoint_1 = require("./endpoint");
23let ExternalUriService = class ExternalUriService {
24 /**
25 * Maps local to remote URLs.
26 * Should be no-op if the given URL is not a localhost URL.
27 *
28 * By default maps to an origin serving Theia.
29 *
30 * Use `parseLocalhost` to retrieve localhost address and port information.
31 */
32 resolve(uri) {
33 const address = this.parseLocalhost(uri);
34 if (address) {
35 return this.toRemoteUrl(uri, address);
36 }
37 return uri;
38 }
39 parseLocalhost(uri) {
40 if (uri.scheme !== 'http' && uri.scheme !== 'https') {
41 return;
42 }
43 const localhostMatch = /^(localhost|127\.0\.0\.1|0\.0\.0\.0):(\d+)$/.exec(uri.authority);
44 if (!localhostMatch) {
45 return;
46 }
47 return {
48 address: localhostMatch[1],
49 port: +localhostMatch[2],
50 };
51 }
52 toRemoteUrl(uri, address) {
53 return new endpoint_1.Endpoint({ host: this.toRemoteHost(address) })
54 .getRestUrl()
55 .withPath(uri.path)
56 .withFragment(uri.fragment)
57 .withQuery(uri.query);
58 }
59 toRemoteHost(address) {
60 return `${this.getRemoteHost()}:${address.port}`;
61 }
62 /**
63 * @returns The remote host (where the backend is running).
64 */
65 getRemoteHost() {
66 return environment_1.environment.electron.is() ? 'localhost' : window.location.hostname;
67 }
68};
69ExternalUriService = (0, tslib_1.__decorate)([
70 (0, inversify_1.injectable)()
71], ExternalUriService);
72exports.ExternalUriService = ExternalUriService;
73//# sourceMappingURL=external-uri-service.js.map
\No newline at end of file