bdy 1.9.3-dev → 1.9.4-dev

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "bdy",
3
3
  "preferGlobal": false,
4
- "version": "1.9.3-dev",
4
+ "version": "1.9.4-dev",
5
5
  "type": "commonjs",
6
6
  "license": "MIT",
7
7
  "scripts": {
@@ -19,6 +19,7 @@ class Agent extends events_1.default {
19
19
  this.service = service;
20
20
  this.target = target;
21
21
  this.tunnels = [];
22
+ this.tunnelsUpdating = false;
22
23
  this.started = false;
23
24
  this.api = api;
24
25
  this.manager = null;
@@ -31,6 +32,18 @@ class Agent extends events_1.default {
31
32
  this.socket.on(utils_js_1.SOCKET_IO_EVENT_TUNNEL, (tunnel, action) => this._onSocketTunnel(tunnel, action));
32
33
  this.socket.on(utils_js_1.SOCKET_IO_EVENT_AGENT, (agent, action) => this._onSocketAgent(agent, action));
33
34
  }
35
+ destroy() {
36
+ if (this.socket) {
37
+ this.socket.removeAllListeners();
38
+ this.socket.destroy();
39
+ this.socket = null;
40
+ }
41
+ this.manager = null;
42
+ this.onRefresh = null;
43
+ this.api = null;
44
+ // musza byc manualnie wczesniej zniszczone to tylko czysci obiekt
45
+ this.tunnels = null;
46
+ }
34
47
  _onSocketTunnel(data, action) {
35
48
  if (action === 'DEL') {
36
49
  this.destroyTunnel(data.id);
@@ -173,33 +186,44 @@ class Agent extends events_1.default {
173
186
  }
174
187
  }
175
188
  refreshTunnels(tunnels) {
176
- const tt = this.tunnels;
177
- const hasTunnels = tt.length > 0;
178
- const haveTunnels = tunnels.length > 0;
179
- if (hasTunnels) {
180
- tt.forEach((tunnel) => {
181
- if (!tunnels.find((t) => t.id === tunnel.id)) {
182
- this.destroyTunnel(tunnel.id);
183
- }
184
- });
189
+ if (this.tunnelsUpdating)
190
+ return;
191
+ this.tunnelsUpdating = true;
192
+ let hasTunnels = false;
193
+ let haveTunnels = false;
194
+ try {
195
+ const tt = [...this.tunnels];
196
+ hasTunnels = tt.length > 0;
197
+ haveTunnels = tunnels.length > 0;
198
+ if (hasTunnels) {
199
+ tt.forEach((tunnel) => {
200
+ if (!tunnels.find((t) => t.id === tunnel.id)) {
201
+ this.destroyTunnel(tunnel.id);
202
+ }
203
+ });
204
+ }
205
+ if (haveTunnels) {
206
+ tunnels.forEach((data) => {
207
+ let sshHostKey;
208
+ if (this.manager)
209
+ sshHostKey = this.manager.sshHostKey;
210
+ const tunnel = tt.find((tunnel) => data.id === tunnel.id);
211
+ if (!tunnel) {
212
+ this.addTunnel(new tunnel_js_1.default({
213
+ ...data,
214
+ sshHostKey,
215
+ }));
216
+ }
217
+ else if (tunnel.hasChanged(data)) {
218
+ tunnel.recreate(data);
219
+ }
220
+ });
221
+ }
185
222
  }
186
- if (haveTunnels) {
187
- tunnels.forEach((data) => {
188
- let sshHostKey;
189
- if (this.manager)
190
- sshHostKey = this.manager.sshHostKey;
191
- const tunnel = tt.find((tunnel) => data.id === tunnel.id);
192
- if (!tunnel) {
193
- this.addTunnel(new tunnel_js_1.default({
194
- ...data,
195
- sshHostKey,
196
- }));
197
- }
198
- else if (tunnel.hasChanged(data)) {
199
- tunnel.recreate(data);
200
- }
201
- });
223
+ catch {
224
+ // do nothing
202
225
  }
226
+ this.tunnelsUpdating = false;
203
227
  if (!this.service && hasTunnels && !haveTunnels) {
204
228
  output_js_1.default.exitError(texts_js_1.ERR_TUNNEL_REMOVED);
205
229
  }
@@ -76,14 +76,24 @@ class ApiSocketClass extends events_1.default {
76
76
  // do nothing
77
77
  }
78
78
  }
79
- connect() {
80
- setTimeout(() => {
81
- this.socket.io.open((err) => {
82
- if (err) {
83
- this.connect();
84
- }
85
- });
86
- }, 3000);
79
+ connect(atOnce) {
80
+ logger_js_1.default.debug('socket connect');
81
+ if (atOnce && this.socket && !this.socket.connected) {
82
+ this.socket.connect();
83
+ }
84
+ else {
85
+ setTimeout(() => {
86
+ if (this.socket && !this.socket.connected)
87
+ this.socket.connect();
88
+ }, 3000);
89
+ }
90
+ }
91
+ destroy() {
92
+ if (this.socket) {
93
+ this.socket.removeAllListeners();
94
+ this.socket.disconnect();
95
+ this.socket = null;
96
+ }
87
97
  }
88
98
  constructor(host, id, token) {
89
99
  super();
@@ -129,6 +139,10 @@ class ApiSocketClass extends events_1.default {
129
139
  this.emit(utils_1.SOCKET_IO_EVENT_DISCONNECTED);
130
140
  this.connect();
131
141
  });
142
+ this.socket.on('connect_error', () => {
143
+ logger_js_1.default.debug('socket error');
144
+ this.connect();
145
+ });
132
146
  this.socket.on('fetchTunnelAgentSuccess', (data) => {
133
147
  logger_js_1.default.debug('Socket fetch success');
134
148
  this.emit(utils_1.SOCKET_IO_EVENT_FETCH_SUCCESS, data);
@@ -138,7 +152,7 @@ class ApiSocketClass extends events_1.default {
138
152
  this.emit(utils_1.SOCKET_IO_EVENT_FETCH_FAILED, (0, utils_1.apiErrorCodeToClass)(code, host));
139
153
  });
140
154
  this.socket.on('sync', (data) => this.onSync(data));
141
- this.connect();
155
+ this.connect(true);
142
156
  }
143
157
  }
144
158
  exports.default = ApiSocketClass;
@@ -82,6 +82,8 @@ commandAgentInstall.action(async (options) => {
82
82
  });
83
83
  id = agent.id;
84
84
  token = agent.token;
85
+ agent.destroy();
86
+ agent = null;
85
87
  }
86
88
  const saved = manager_js_1.default.system.saveSystemConfig(id, host, token, port);
87
89
  if (!saved) {
@@ -66,6 +66,9 @@ class Logger {
66
66
  debug(...args) {
67
67
  this.getPino().debug(...args);
68
68
  }
69
+ trace() {
70
+ this.getPino().trace();
71
+ }
69
72
  fatal(...args) {
70
73
  this.getPino().fatal(...args);
71
74
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "bdy",
3
3
  "preferGlobal": false,
4
- "version": "1.9.3-dev",
4
+ "version": "1.9.4-dev",
5
5
  "type": "commonjs",
6
6
  "license": "MIT",
7
7
  "scripts": {