UNPKG

7.24 kBJavaScriptView Raw
1"use strict";
2// *****************************************************************************
3// Copyright (C) 2018 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// *****************************************************************************
17var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
18 var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
19 if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
20 else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
21 return c > 3 && r && Object.defineProperty(target, key, r), r;
22};
23var __metadata = (this && this.__metadata) || function (k, v) {
24 if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
25};
26var WebSocketConnectionProvider_1;
27Object.defineProperty(exports, "__esModule", { value: true });
28exports.WebSocketConnectionProvider = exports.LocalWebSocketConnectionProvider = void 0;
29const inversify_1 = require("inversify");
30const common_1 = require("../../common");
31const endpoint_1 = require("../endpoint");
32const abstract_connection_provider_1 = require("../../common/messaging/abstract-connection-provider");
33const socket_io_client_1 = require("socket.io-client");
34const web_socket_channel_1 = require("../../common/messaging/web-socket-channel");
35(0, inversify_1.decorate)((0, inversify_1.injectable)(), common_1.RpcProxyFactory);
36(0, inversify_1.decorate)((0, inversify_1.unmanaged)(), common_1.RpcProxyFactory, 0);
37exports.LocalWebSocketConnectionProvider = Symbol('LocalWebSocketConnectionProvider');
38let WebSocketConnectionProvider = WebSocketConnectionProvider_1 = class WebSocketConnectionProvider extends abstract_connection_provider_1.AbstractConnectionProvider {
39 constructor() {
40 super();
41 this.onSocketDidOpenEmitter = new common_1.Emitter();
42 this.onSocketDidCloseEmitter = new common_1.Emitter();
43 const url = this.createWebSocketUrl(web_socket_channel_1.WebSocketChannel.wsPath);
44 this.socket = this.createWebSocket(url);
45 this.socket.on('connect', () => {
46 this.initializeMultiplexer();
47 if (this.reconnectChannelOpeners.length > 0) {
48 this.reconnectChannelOpeners.forEach(opener => opener());
49 this.reconnectChannelOpeners = [];
50 }
51 this.socket.on('disconnect', () => this.fireSocketDidClose());
52 this.socket.on('message', () => this.onIncomingMessageActivityEmitter.fire(undefined));
53 this.fireSocketDidOpen();
54 });
55 this.socket.connect();
56 }
57 get onSocketDidOpen() {
58 return this.onSocketDidOpenEmitter.event;
59 }
60 get onSocketDidClose() {
61 return this.onSocketDidCloseEmitter.event;
62 }
63 static createProxy(container, path, arg) {
64 return container.get(WebSocketConnectionProvider_1).createProxy(path, arg);
65 }
66 static createLocalProxy(container, path, arg) {
67 return container.get(exports.LocalWebSocketConnectionProvider).createProxy(path, arg);
68 }
69 static createHandler(container, path, arg) {
70 const remote = container.get(WebSocketConnectionProvider_1);
71 const local = container.get(exports.LocalWebSocketConnectionProvider);
72 remote.createProxy(path, arg);
73 if (remote !== local) {
74 local.createProxy(path, arg);
75 }
76 }
77 createMainChannel() {
78 return new web_socket_channel_1.WebSocketChannel(this.toIWebSocket(this.socket));
79 }
80 toIWebSocket(socket) {
81 return {
82 close: () => {
83 socket.removeAllListeners('disconnect');
84 socket.removeAllListeners('error');
85 socket.removeAllListeners('message');
86 },
87 isConnected: () => socket.connected,
88 onClose: cb => socket.on('disconnect', reason => cb(reason)),
89 onError: cb => socket.on('error', reason => cb(reason)),
90 onMessage: cb => socket.on('message', data => cb(data)),
91 send: message => socket.emit('message', message)
92 };
93 }
94 async openChannel(path, handler, options) {
95 if (this.socket.connected) {
96 return super.openChannel(path, handler, options);
97 }
98 else {
99 const openChannel = () => {
100 this.socket.off('connect', openChannel);
101 this.openChannel(path, handler, options);
102 };
103 this.socket.on('connect', openChannel);
104 }
105 }
106 /**
107 * @param path The handler to reach in the backend.
108 */
109 createWebSocketUrl(path) {
110 // Since we are using Socket.io, the path should look like the following:
111 // proto://domain.com/{path}
112 return this.createEndpoint(path).getWebSocketUrl().withPath(path).toString();
113 }
114 createHttpWebSocketUrl(path) {
115 return this.createEndpoint(path).getRestUrl().toString();
116 }
117 createEndpoint(path) {
118 return new endpoint_1.Endpoint({ path });
119 }
120 /**
121 * Creates a web socket for the given url
122 */
123 createWebSocket(url) {
124 return (0, socket_io_client_1.io)(url, {
125 path: this.createSocketIoPath(url),
126 reconnection: true,
127 reconnectionDelay: 1000,
128 reconnectionDelayMax: 10000,
129 reconnectionAttempts: Infinity,
130 extraHeaders: {
131 // Socket.io strips the `origin` header
132 // We need to provide our own for validation
133 'fix-origin': window.location.origin
134 }
135 });
136 }
137 /**
138 * Path for Socket.io to make its requests to.
139 */
140 createSocketIoPath(url) {
141 if (location.protocol === endpoint_1.Endpoint.PROTO_FILE) {
142 return '/socket.io';
143 }
144 let { pathname } = location;
145 if (!pathname.endsWith('/')) {
146 pathname += '/';
147 }
148 return pathname + 'socket.io';
149 }
150 fireSocketDidOpen() {
151 this.onSocketDidOpenEmitter.fire(undefined);
152 }
153 fireSocketDidClose() {
154 this.onSocketDidCloseEmitter.fire(undefined);
155 }
156};
157WebSocketConnectionProvider = WebSocketConnectionProvider_1 = __decorate([
158 (0, inversify_1.injectable)(),
159 __metadata("design:paramtypes", [])
160], WebSocketConnectionProvider);
161exports.WebSocketConnectionProvider = WebSocketConnectionProvider;
162//# sourceMappingURL=ws-connection-provider.js.map
\No newline at end of file