egos-transfer 0.0.9 → 0.1.0

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.
package/dist/index.d.ts CHANGED
@@ -53,7 +53,7 @@ export declare abstract class PeerTransfer {
53
53
  getDeviceStatus(peerId: string): Promise<IDeviceStatus | undefined>;
54
54
  connectToPeer(deviceId: string): Promise<DataConnection | undefined>;
55
55
  ping(peerId: string): Promise<boolean>;
56
- onConnect(conn: DataConnection, positive?: boolean): Promise<DataConnection | undefined>;
56
+ onConnect(conn: DataConnection): Promise<DataConnection | undefined>;
57
57
  request(peerId: string, payload: PeerRequestPayload): Promise<PeerMessage>;
58
58
  reply(conn: DataConnection, oldMessage: PeerMessage, message: {
59
59
  body?: Record<string, any>;
package/dist/index.js CHANGED
@@ -66,7 +66,7 @@ class PeerTransfer {
66
66
  });
67
67
  });
68
68
  peer.on('connection', (conn) => {
69
- this.onConnect(conn, false);
69
+ this.onConnect(conn);
70
70
  });
71
71
  peer.on('disconnected', () => {
72
72
  if (!this.isPaused) {
@@ -83,9 +83,7 @@ class PeerTransfer {
83
83
  return;
84
84
  }
85
85
  setTimeout(() => {
86
- if (!this.isPaused) {
87
- this.reconnect();
88
- }
86
+ this.reconnect();
89
87
  }, constant_1.CONNECTION_RETRY);
90
88
  });
91
89
  peer.on('error', (err) => {
@@ -119,6 +117,7 @@ class PeerTransfer {
119
117
  socket.on('close', () => {
120
118
  setTimeout(() => {
121
119
  this.destroy();
120
+ this.reconnect();
122
121
  }, constant_1.CONNECTION_RETRY);
123
122
  });
124
123
  this.onHeartbeat({ type: 'HEARTBEAT' });
@@ -156,7 +155,7 @@ class PeerTransfer {
156
155
  return;
157
156
  }
158
157
  if (((_a = message.payload) === null || _a === void 0 ? void 0 : _a.msg) === constant_1.CONNECTION_PEER_CONFLICT) {
159
- this.isPaused = true;
158
+ this.stop();
160
159
  this.getStore().dispatch({
161
160
  type: 'global/updateNetworkState',
162
161
  payload: {
@@ -189,8 +188,8 @@ class PeerTransfer {
189
188
  return;
190
189
  }
191
190
  const deviceId = message.payload.deviceId;
192
- this.removeConnection(deviceId);
193
191
  this.offline(deviceId);
192
+ this.ping(deviceId);
194
193
  }
195
194
  removeConnection(peerId) {
196
195
  var _a, _b;
@@ -200,6 +199,7 @@ class PeerTransfer {
200
199
  clearInterval((_b = conn.metadata) === null || _b === void 0 ? void 0 : _b.intervalId);
201
200
  }
202
201
  this.connectPool.delete(peerId);
202
+ this.offline(peerId);
203
203
  }
204
204
  }
205
205
  addConnection(conn) {
@@ -327,6 +327,9 @@ class PeerTransfer {
327
327
  }
328
328
  ping(peerId) {
329
329
  return __awaiter(this, void 0, void 0, function* () {
330
+ if (this.deviceId === peerId) {
331
+ return;
332
+ }
330
333
  const conn = this.connectPool.get(peerId);
331
334
  if (!conn) {
332
335
  return;
@@ -344,8 +347,8 @@ class PeerTransfer {
344
347
  return true;
345
348
  });
346
349
  }
347
- onConnect(conn_1) {
348
- return __awaiter(this, arguments, void 0, function* (conn, positive = true) {
350
+ onConnect(conn) {
351
+ return __awaiter(this, void 0, void 0, function* () {
349
352
  if (!conn) {
350
353
  return;
351
354
  }
@@ -470,8 +473,12 @@ class PeerTransfer {
470
473
  }
471
474
  reconnect() {
472
475
  return __awaiter(this, void 0, void 0, function* () {
476
+ if (this.isPaused) {
477
+ return;
478
+ }
479
+ console.log('peer reconnecting...');
473
480
  if (this.reconnectingPromise) {
474
- return this.reconnectingPromise;
481
+ return yield this.reconnectingPromise;
475
482
  }
476
483
  this.reconnectingPromise = this.doReconnect();
477
484
  return this.reconnectingPromise.finally(() => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "egos-transfer",
3
- "version": "0.0.9",
3
+ "version": "0.1.0",
4
4
  "description": "1234",
5
5
  "homepage": "https://github.com/superbogy/peer-transfer#readme",
6
6
  "bugs": {
package/src/index.ts CHANGED
@@ -102,7 +102,7 @@ export abstract class PeerTransfer {
102
102
  });
103
103
  });
104
104
  peer.on('connection', (conn) => {
105
- this.onConnect(conn, false);
105
+ this.onConnect(conn);
106
106
  });
107
107
  peer.on('disconnected', () => {
108
108
  if (!this.isPaused) {
@@ -119,9 +119,7 @@ export abstract class PeerTransfer {
119
119
  return;
120
120
  }
121
121
  setTimeout(() => {
122
- if (!this.isPaused) {
123
- this.reconnect();
124
- }
122
+ this.reconnect();
125
123
  }, CONNECTION_RETRY);
126
124
  });
127
125
  peer.on('error', (err) => {
@@ -155,6 +153,7 @@ export abstract class PeerTransfer {
155
153
  socket.on('close', () => {
156
154
  setTimeout(() => {
157
155
  this.destroy();
156
+ this.reconnect();
158
157
  }, CONNECTION_RETRY);
159
158
  });
160
159
  this.onHeartbeat({ type: 'HEARTBEAT' } as IMessage);
@@ -193,7 +192,7 @@ export abstract class PeerTransfer {
193
192
  return;
194
193
  }
195
194
  if (message.payload?.msg === CONNECTION_PEER_CONFLICT) {
196
- this.isPaused = true;
195
+ this.stop();
197
196
  this.getStore().dispatch({
198
197
  type: 'global/updateNetworkState',
199
198
  payload: {
@@ -227,8 +226,8 @@ export abstract class PeerTransfer {
227
226
  return;
228
227
  }
229
228
  const deviceId = message.payload.deviceId;
230
- this.removeConnection(deviceId);
231
229
  this.offline(deviceId);
230
+ this.ping(deviceId);
232
231
  }
233
232
 
234
233
  removeConnection(peerId: string) {
@@ -238,6 +237,7 @@ export abstract class PeerTransfer {
238
237
  clearInterval(conn.metadata?.intervalId);
239
238
  }
240
239
  this.connectPool.delete(peerId);
240
+ this.offline(peerId);
241
241
  }
242
242
  }
243
243
 
@@ -361,6 +361,9 @@ export abstract class PeerTransfer {
361
361
  }
362
362
 
363
363
  async ping(peerId: string) {
364
+ if (this.deviceId === peerId) {
365
+ return;
366
+ }
364
367
  const conn = this.connectPool.get(peerId);
365
368
  if (!conn) {
366
369
  return;
@@ -378,7 +381,7 @@ export abstract class PeerTransfer {
378
381
  return true;
379
382
  }
380
383
 
381
- async onConnect(conn: DataConnection, positive = true): Promise<DataConnection | undefined> {
384
+ async onConnect(conn: DataConnection): Promise<DataConnection | undefined> {
382
385
  if (!conn) {
383
386
  return;
384
387
  }
@@ -535,8 +538,12 @@ export abstract class PeerTransfer {
535
538
  }
536
539
 
537
540
  async reconnect(): Promise<Peer | undefined> {
541
+ if (this.isPaused) {
542
+ return;
543
+ }
544
+ console.log('peer reconnecting...');
538
545
  if (this.reconnectingPromise) {
539
- return this.reconnectingPromise;
546
+ return await this.reconnectingPromise;
540
547
  }
541
548
 
542
549
  this.reconnectingPromise = this.doReconnect();