UNPKG

4.16 kBJavaScriptView Raw
1"use strict";
2// *****************************************************************************
3// Copyright (C) 2017 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.Endpoint = void 0;
19const uri_1 = require("../common/uri");
20/**
21 * An endpoint provides URLs for http and ws, based on configuration and defaults.
22 */
23class Endpoint {
24 constructor(options = {}, location = self.location) {
25 this.options = options;
26 this.location = location;
27 }
28 getWebSocketUrl() {
29 return new uri_1.default(`${this.wsScheme}//${this.host}${this.pathname}${this.path}`);
30 }
31 getRestUrl() {
32 return new uri_1.default(`${this.httpScheme}//${this.host}${this.pathname}${this.path}`);
33 }
34 get pathname() {
35 if (this.location.protocol === Endpoint.PROTO_FILE) {
36 return '';
37 }
38 if (this.location.pathname === '/') {
39 return '';
40 }
41 if (this.location.pathname.endsWith('/')) {
42 return this.location.pathname.substring(0, this.location.pathname.length - 1);
43 }
44 return this.location.pathname;
45 }
46 get host() {
47 if (this.options.host) {
48 return this.options.host;
49 }
50 if (this.location.host) {
51 return this.location.host;
52 }
53 return 'localhost:' + this.port;
54 }
55 get origin() {
56 return `${this.httpScheme}//${this.host}`;
57 }
58 get port() {
59 return this.getSearchParam('port', '3000');
60 }
61 getSearchParam(name, defaultValue) {
62 const search = this.location.search;
63 if (!search) {
64 return defaultValue;
65 }
66 return search.substring(1).split('&')
67 .filter(value => value.startsWith(name + '='))
68 .map(value => {
69 const encoded = value.substring(name.length + 1);
70 return decodeURIComponent(encoded);
71 })[0] || defaultValue;
72 }
73 get wsScheme() {
74 if (this.options.wsScheme) {
75 return this.options.wsScheme;
76 }
77 return this.httpScheme === Endpoint.PROTO_HTTPS ? Endpoint.PROTO_WSS : Endpoint.PROTO_WS;
78 }
79 /**
80 * The HTTP/HTTPS scheme of the endpoint, or the user defined one.
81 * See: `Endpoint.Options.httpScheme`.
82 */
83 get httpScheme() {
84 if (this.options.httpScheme) {
85 return this.options.httpScheme;
86 }
87 if (this.location.protocol === Endpoint.PROTO_HTTP ||
88 this.location.protocol === Endpoint.PROTO_HTTPS) {
89 return this.location.protocol;
90 }
91 return Endpoint.PROTO_HTTP;
92 }
93 get path() {
94 if (this.options.path) {
95 if (this.options.path.startsWith('/')) {
96 return this.options.path;
97 }
98 else {
99 return '/' + this.options.path;
100 }
101 }
102 return '';
103 }
104}
105exports.Endpoint = Endpoint;
106Endpoint.PROTO_HTTPS = 'https:';
107Endpoint.PROTO_HTTP = 'http:';
108Endpoint.PROTO_WS = 'ws:';
109Endpoint.PROTO_WSS = 'wss:';
110Endpoint.PROTO_FILE = 'file:';
111(function (Endpoint) {
112 class Options {
113 }
114 Endpoint.Options = Options;
115 // Necessary for running tests with dependency on TS lib on node
116 // FIXME figure out how to mock with ts-node
117 class Location {
118 }
119 Endpoint.Location = Location;
120})(Endpoint = exports.Endpoint || (exports.Endpoint = {}));
121//# sourceMappingURL=endpoint.js.map
\No newline at end of file