1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | Object.defineProperty(exports, "__esModule", { value: true });
|
18 | exports.Endpoint = void 0;
|
19 | const uri_1 = require("../common/uri");
|
20 |
|
21 |
|
22 |
|
23 | class 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 |
|
81 |
|
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 | }
|
105 | exports.Endpoint = Endpoint;
|
106 | Endpoint.PROTO_HTTPS = 'https:';
|
107 | Endpoint.PROTO_HTTP = 'http:';
|
108 | Endpoint.PROTO_WS = 'ws:';
|
109 | Endpoint.PROTO_WSS = 'wss:';
|
110 | Endpoint.PROTO_FILE = 'file:';
|
111 | (function (Endpoint) {
|
112 | class Options {
|
113 | }
|
114 | Endpoint.Options = Options;
|
115 |
|
116 |
|
117 | class Location {
|
118 | }
|
119 | Endpoint.Location = Location;
|
120 | })(Endpoint = exports.Endpoint || (exports.Endpoint = {}));
|
121 |
|
\ | No newline at end of file |