egos-transfer 0.1.9 → 0.2.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.
Files changed (3) hide show
  1. package/dist/index.js +30 -11
  2. package/package.json +2 -1
  3. package/src/index.ts +29 -11
package/dist/index.js CHANGED
@@ -32,6 +32,7 @@ const peerjs_1 = require("peerjs");
32
32
  const entity_1 = require("./entity");
33
33
  const router_1 = require("./router");
34
34
  const axios_1 = __importDefault(require("axios"));
35
+ const delay_1 = __importDefault(require("delay"));
35
36
  class PeerTransfer {
36
37
  constructor(deviceId, config, isRadomPeerId = false) {
37
38
  this.retry = 0;
@@ -58,6 +59,7 @@ class PeerTransfer {
58
59
  return this.peer;
59
60
  }
60
61
  this.destroy();
62
+ yield (0, delay_1.default)(1500);
61
63
  yield this.reloadConfig();
62
64
  const peer = new peerjs_1.Peer(this.peerId, Object.assign(Object.assign({}, this.peerConfig), { pingInterval: 9000 }));
63
65
  this.peer = peer;
@@ -75,6 +77,7 @@ class PeerTransfer {
75
77
  if (this.booted) {
76
78
  return this.booted;
77
79
  }
80
+ console.log('create peer@@@', Date.now());
78
81
  this.booted = new Promise((resolve) => {
79
82
  const timer = setTimeout(() => {
80
83
  resolve(null);
@@ -84,6 +87,7 @@ class PeerTransfer {
84
87
  resolve(peer);
85
88
  };
86
89
  peer.on('open', (id) => {
90
+ console.log('peer@open', Date.now());
87
91
  clearTimeout(timer);
88
92
  done(peer);
89
93
  });
@@ -202,7 +206,8 @@ class PeerTransfer {
202
206
  this.ping(deviceId);
203
207
  }
204
208
  removeConnection(peerId, connectionId) {
205
- var _a;
209
+ var _a, _b, _c;
210
+ // console.log('removeConnection@', peerId, connectionId);
206
211
  const conn = this.connectPool.get(peerId);
207
212
  if (conn) {
208
213
  if (connectionId && conn.connectionId !== connectionId) {
@@ -219,6 +224,17 @@ class PeerTransfer {
219
224
  conn.close();
220
225
  (_a = this.peer) === null || _a === void 0 ? void 0 : _a._removeConnection(conn);
221
226
  }
227
+ else {
228
+ const connections = ((_b = this.peer) === null || _b === void 0 ? void 0 : _b.connections) || {};
229
+ const conns = connections[peerId] || [];
230
+ if (conns.length > 0) {
231
+ for (const conn of conns) {
232
+ conn.removeAllListeners();
233
+ conn.close();
234
+ (_c = this.peer) === null || _c === void 0 ? void 0 : _c._removeConnection(conn);
235
+ }
236
+ }
237
+ }
222
238
  }
223
239
  addConnection(conn) {
224
240
  // console.log('addConnection@', conn.label);
@@ -248,10 +264,10 @@ class PeerTransfer {
248
264
  for (const deviceId of deviceIds) {
249
265
  const conn = this.connectPool.get(deviceId);
250
266
  if (!this.isValidConnection(conn)) {
251
- this.removeConnection(conn.peer);
267
+ this.removeConnection(conn.peer, conn.connectionId);
252
268
  }
253
269
  else {
254
- // this.ping(conn.peer);
270
+ this.ping(conn.peer);
255
271
  }
256
272
  }
257
273
  }), constant_1.HEARTBEAT_INTERVAL);
@@ -291,7 +307,7 @@ class PeerTransfer {
291
307
  return existingConn;
292
308
  }
293
309
  else {
294
- this.removeConnection(peerId);
310
+ this.removeConnection(existingConn.peer, existingConn.connectionId);
295
311
  }
296
312
  }
297
313
  if (PeerTransfer.connecting.has(peerId)) {
@@ -365,7 +381,7 @@ class PeerTransfer {
365
381
  }
366
382
  const conn = this.connectPool.get(peerId);
367
383
  if (!this.isValidConnection(conn)) {
368
- this.removeConnection(conn === null || conn === void 0 ? void 0 : conn.peer);
384
+ this.removeConnection(conn === null || conn === void 0 ? void 0 : conn.peer, conn === null || conn === void 0 ? void 0 : conn.connectionId);
369
385
  return;
370
386
  }
371
387
  const res = yield this.request(conn.peer, {
@@ -375,7 +391,7 @@ class PeerTransfer {
375
391
  timeout: constant_1.PING_TIMEOUT,
376
392
  });
377
393
  if (!res || res.error) {
378
- this.removeConnection(conn.peer);
394
+ this.removeConnection(conn.peer, conn.connectionId);
379
395
  return false;
380
396
  }
381
397
  return true;
@@ -390,13 +406,13 @@ class PeerTransfer {
390
406
  // console.log('onConnect', conn.label, exists);
391
407
  if (exists) {
392
408
  if (exists.metadata.time > conn.metadata.time) {
393
- this.removeConnection(conn.peer);
409
+ this.removeConnection(conn.peer, conn.connectionId);
394
410
  if (this.isValidConnection(exists)) {
395
411
  return exists;
396
412
  }
397
413
  }
398
414
  else {
399
- this.removeConnection(exists.peer);
415
+ this.removeConnection(exists.peer, exists.connectionId);
400
416
  }
401
417
  }
402
418
  return new Promise((resolve) => {
@@ -413,8 +429,7 @@ class PeerTransfer {
413
429
  done(conn);
414
430
  }
415
431
  else {
416
- conn.close();
417
- this.removeConnection(conn.peer);
432
+ this.removeConnection(conn.peer, conn.connectionId);
418
433
  done(undefined);
419
434
  }
420
435
  }), constant_1.CONNECTION_TIMEOUT);
@@ -436,6 +451,8 @@ class PeerTransfer {
436
451
  done(null);
437
452
  }));
438
453
  conn.on('close', () => __awaiter(this, void 0, void 0, function* () {
454
+ console.log('close------------>', conn.label);
455
+ clearTimeout(timer);
439
456
  this.removeConnection(conn.peer, conn.connectionId);
440
457
  // if (!this.peer?.open) {
441
458
  // await this.reconnect();
@@ -465,7 +482,7 @@ class PeerTransfer {
465
482
  body: {},
466
483
  error: { message: 'request timeout' },
467
484
  };
468
- this.removeConnection(conn.peer);
485
+ this.removeConnection(conn.peer, conn.connectionId);
469
486
  resolve(msg);
470
487
  }, payload.timeout || entity_1.REQUEST_TIMEOUT);
471
488
  this.inRequest.set(conn.connectionId, {
@@ -560,6 +577,8 @@ class PeerTransfer {
560
577
  if (((_a = this.peer) === null || _a === void 0 ? void 0 : _a.open) && this.peerConfig.token === ((_b = this.peer) === null || _b === void 0 ? void 0 : _b.options.token)) {
561
578
  return this.peer;
562
579
  }
580
+ yield this.reloadConfig();
581
+ return this.createPeer();
563
582
  // return this.createPeer();
564
583
  if (((_c = this.peer) === null || _c === void 0 ? void 0 : _c.disconnected) && ((_d = this.peer) === null || _d === void 0 ? void 0 : _d.options.token) === this.peerConfig.token) {
565
584
  this.peer.reconnect();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "egos-transfer",
3
- "version": "0.1.9",
3
+ "version": "0.2.0",
4
4
  "description": "1234",
5
5
  "homepage": "https://github.com/superbogy/peer-transfer#readme",
6
6
  "bugs": {
@@ -23,6 +23,7 @@
23
23
  "await-lock": "^2.2.2",
24
24
  "axios": "^1.13.2",
25
25
  "bson-objectid": "^2.0.4",
26
+ "delay": "^7.0.0",
26
27
  "path-to-regexp": "^8.2.0",
27
28
  "peerjs": "^1.5.4",
28
29
  "uuid": "^11.1.0"
package/src/index.ts CHANGED
@@ -22,6 +22,7 @@ import { InternalApi, PeerAction, PeerRoutes, PeerScope, REQUEST_TIMEOUT } from
22
22
 
23
23
  import { PeerRouter } from './router';
24
24
  import axios from 'axios';
25
+ import delay from 'delay';
25
26
 
26
27
  export interface ContentPayload {
27
28
  file: string;
@@ -98,6 +99,7 @@ export abstract class PeerTransfer {
98
99
  return this.peer;
99
100
  }
100
101
  this.destroy();
102
+ await delay(1500);
101
103
  await this.reloadConfig();
102
104
  const peer = new Peer(this.peerId, {
103
105
  ...this.peerConfig,
@@ -117,7 +119,7 @@ export abstract class PeerTransfer {
117
119
  if (this.booted) {
118
120
  return this.booted;
119
121
  }
120
-
122
+ console.log('create peer@@@', Date.now());
121
123
  this.booted = new Promise((resolve) => {
122
124
  const timer = setTimeout(() => {
123
125
  resolve(null);
@@ -127,6 +129,7 @@ export abstract class PeerTransfer {
127
129
  resolve(peer);
128
130
  };
129
131
  peer.on('open', (id) => {
132
+ console.log('peer@open', Date.now());
130
133
  clearTimeout(timer);
131
134
  done(peer);
132
135
  });
@@ -246,6 +249,7 @@ export abstract class PeerTransfer {
246
249
  }
247
250
 
248
251
  removeConnection(peerId: string, connectionId?: string) {
252
+ // console.log('removeConnection@', peerId, connectionId);
249
253
  const conn = this.connectPool.get(peerId);
250
254
  if (conn) {
251
255
  if (connectionId && conn.connectionId !== connectionId) {
@@ -262,6 +266,16 @@ export abstract class PeerTransfer {
262
266
  conn.removeAllListeners();
263
267
  conn.close();
264
268
  this.peer?._removeConnection(conn);
269
+ } else {
270
+ const connections = this.peer?.connections || {};
271
+ const conns = connections[peerId] || [];
272
+ if (conns.length > 0) {
273
+ for (const conn of conns) {
274
+ conn.removeAllListeners();
275
+ conn.close();
276
+ this.peer?._removeConnection(conn);
277
+ }
278
+ }
265
279
  }
266
280
  }
267
281
 
@@ -293,9 +307,9 @@ export abstract class PeerTransfer {
293
307
  for (const deviceId of deviceIds) {
294
308
  const conn = this.connectPool.get(deviceId);
295
309
  if (!this.isValidConnection(conn)) {
296
- this.removeConnection(conn.peer);
310
+ this.removeConnection(conn.peer, conn.connectionId);
297
311
  } else {
298
- // this.ping(conn.peer);
312
+ this.ping(conn.peer);
299
313
  }
300
314
  }
301
315
  }, HEARTBEAT_INTERVAL);
@@ -332,7 +346,7 @@ export abstract class PeerTransfer {
332
346
  if (this.isValidConnection(existingConn)) {
333
347
  return existingConn;
334
348
  } else {
335
- this.removeConnection(peerId);
349
+ this.removeConnection(existingConn.peer, existingConn.connectionId);
336
350
  }
337
351
  }
338
352
 
@@ -406,7 +420,7 @@ export abstract class PeerTransfer {
406
420
  }
407
421
  const conn = this.connectPool.get(peerId);
408
422
  if (!this.isValidConnection(conn)) {
409
- this.removeConnection(conn?.peer);
423
+ this.removeConnection(conn?.peer, conn?.connectionId);
410
424
  return;
411
425
  }
412
426
  const res = await this.request(conn.peer, {
@@ -416,7 +430,7 @@ export abstract class PeerTransfer {
416
430
  timeout: PING_TIMEOUT,
417
431
  });
418
432
  if (!res || res.error) {
419
- this.removeConnection(conn.peer);
433
+ this.removeConnection(conn.peer, conn.connectionId);
420
434
  return false;
421
435
  }
422
436
  return true;
@@ -430,12 +444,12 @@ export abstract class PeerTransfer {
430
444
  // console.log('onConnect', conn.label, exists);
431
445
  if (exists) {
432
446
  if (exists.metadata.time > conn.metadata.time) {
433
- this.removeConnection(conn.peer);
447
+ this.removeConnection(conn.peer, conn.connectionId);
434
448
  if (this.isValidConnection(exists)) {
435
449
  return exists;
436
450
  }
437
451
  } else {
438
- this.removeConnection(exists.peer);
452
+ this.removeConnection(exists.peer, exists.connectionId);
439
453
  }
440
454
  }
441
455
  return new Promise((resolve) => {
@@ -451,8 +465,7 @@ export abstract class PeerTransfer {
451
465
  this.addConnection(conn);
452
466
  done(conn);
453
467
  } else {
454
- conn.close();
455
- this.removeConnection(conn.peer);
468
+ this.removeConnection(conn.peer, conn.connectionId);
456
469
  done(undefined);
457
470
  }
458
471
  }, CONNECTION_TIMEOUT);
@@ -474,6 +487,8 @@ export abstract class PeerTransfer {
474
487
  done(null);
475
488
  });
476
489
  conn.on('close', async () => {
490
+ console.log('close------------>', conn.label);
491
+ clearTimeout(timer);
477
492
  this.removeConnection(conn.peer, conn.connectionId);
478
493
  // if (!this.peer?.open) {
479
494
  // await this.reconnect();
@@ -507,7 +522,7 @@ export abstract class PeerTransfer {
507
522
  body: {},
508
523
  error: { message: 'request timeout' },
509
524
  };
510
- this.removeConnection(conn.peer);
525
+ this.removeConnection(conn.peer, conn.connectionId);
511
526
  resolve(msg);
512
527
  }, payload.timeout || REQUEST_TIMEOUT);
513
528
  this.inRequest.set(conn.connectionId, {
@@ -635,6 +650,9 @@ export abstract class PeerTransfer {
635
650
  if (this.peer?.open && this.peerConfig.token === this.peer?.options.token) {
636
651
  return this.peer;
637
652
  }
653
+
654
+ await this.reloadConfig();
655
+ return this.createPeer();
638
656
  // return this.createPeer();
639
657
  if (this.peer?.disconnected && this.peer?.options.token === this.peerConfig.token) {
640
658
  this.peer.reconnect();