egos-transfer 0.1.9 → 0.2.1

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 +34 -14
  2. package/package.json +2 -1
  3. package/src/index.ts +33 -14
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;
@@ -48,7 +49,7 @@ class PeerTransfer {
48
49
  createPeer() {
49
50
  return __awaiter(this, arguments, void 0, function* (force = false) {
50
51
  var _a;
51
- if (this.isPaused) {
52
+ if (this.isPaused && !force) {
52
53
  return;
53
54
  }
54
55
  if (this.booted) {
@@ -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;
@@ -84,6 +86,7 @@ class PeerTransfer {
84
86
  resolve(peer);
85
87
  };
86
88
  peer.on('open', (id) => {
89
+ console.log('peer@open', Date.now());
87
90
  clearTimeout(timer);
88
91
  done(peer);
89
92
  });
@@ -202,7 +205,8 @@ class PeerTransfer {
202
205
  this.ping(deviceId);
203
206
  }
204
207
  removeConnection(peerId, connectionId) {
205
- var _a;
208
+ var _a, _b, _c;
209
+ // console.log('removeConnection@', peerId, connectionId);
206
210
  const conn = this.connectPool.get(peerId);
207
211
  if (conn) {
208
212
  if (connectionId && conn.connectionId !== connectionId) {
@@ -219,6 +223,17 @@ class PeerTransfer {
219
223
  conn.close();
220
224
  (_a = this.peer) === null || _a === void 0 ? void 0 : _a._removeConnection(conn);
221
225
  }
226
+ else {
227
+ const connections = ((_b = this.peer) === null || _b === void 0 ? void 0 : _b.connections) || {};
228
+ const conns = connections[peerId] || [];
229
+ if (conns.length > 0) {
230
+ for (const conn of conns) {
231
+ conn.removeAllListeners();
232
+ conn.close();
233
+ (_c = this.peer) === null || _c === void 0 ? void 0 : _c._removeConnection(conn);
234
+ }
235
+ }
236
+ }
222
237
  }
223
238
  addConnection(conn) {
224
239
  // console.log('addConnection@', conn.label);
@@ -248,10 +263,10 @@ class PeerTransfer {
248
263
  for (const deviceId of deviceIds) {
249
264
  const conn = this.connectPool.get(deviceId);
250
265
  if (!this.isValidConnection(conn)) {
251
- this.removeConnection(conn.peer);
266
+ this.removeConnection(conn.peer, conn.connectionId);
252
267
  }
253
268
  else {
254
- // this.ping(conn.peer);
269
+ this.ping(conn.peer);
255
270
  }
256
271
  }
257
272
  }), constant_1.HEARTBEAT_INTERVAL);
@@ -285,13 +300,13 @@ class PeerTransfer {
285
300
  }
286
301
  const peerId = deviceId;
287
302
  const existingConn = this.connectPool.get(peerId);
288
- console.log('connectToPeer', existingConn === null || existingConn === void 0 ? void 0 : existingConn.open, this.connectPool.size);
303
+ console.log('connectToPeer', this.isPaused, this.connectPool.size);
289
304
  if (existingConn) {
290
305
  if (this.isValidConnection(existingConn)) {
291
306
  return existingConn;
292
307
  }
293
308
  else {
294
- this.removeConnection(peerId);
309
+ this.removeConnection(existingConn.peer, existingConn.connectionId);
295
310
  }
296
311
  }
297
312
  if (PeerTransfer.connecting.has(peerId)) {
@@ -365,7 +380,7 @@ class PeerTransfer {
365
380
  }
366
381
  const conn = this.connectPool.get(peerId);
367
382
  if (!this.isValidConnection(conn)) {
368
- this.removeConnection(conn === null || conn === void 0 ? void 0 : conn.peer);
383
+ this.removeConnection(conn === null || conn === void 0 ? void 0 : conn.peer, conn === null || conn === void 0 ? void 0 : conn.connectionId);
369
384
  return;
370
385
  }
371
386
  const res = yield this.request(conn.peer, {
@@ -375,7 +390,7 @@ class PeerTransfer {
375
390
  timeout: constant_1.PING_TIMEOUT,
376
391
  });
377
392
  if (!res || res.error) {
378
- this.removeConnection(conn.peer);
393
+ this.removeConnection(conn.peer, conn.connectionId);
379
394
  return false;
380
395
  }
381
396
  return true;
@@ -390,13 +405,13 @@ class PeerTransfer {
390
405
  // console.log('onConnect', conn.label, exists);
391
406
  if (exists) {
392
407
  if (exists.metadata.time > conn.metadata.time) {
393
- this.removeConnection(conn.peer);
408
+ this.removeConnection(conn.peer, conn.connectionId);
394
409
  if (this.isValidConnection(exists)) {
395
410
  return exists;
396
411
  }
397
412
  }
398
413
  else {
399
- this.removeConnection(exists.peer);
414
+ this.removeConnection(exists.peer, exists.connectionId);
400
415
  }
401
416
  }
402
417
  return new Promise((resolve) => {
@@ -413,8 +428,7 @@ class PeerTransfer {
413
428
  done(conn);
414
429
  }
415
430
  else {
416
- conn.close();
417
- this.removeConnection(conn.peer);
431
+ this.removeConnection(conn.peer, conn.connectionId);
418
432
  done(undefined);
419
433
  }
420
434
  }), constant_1.CONNECTION_TIMEOUT);
@@ -436,6 +450,8 @@ class PeerTransfer {
436
450
  done(null);
437
451
  }));
438
452
  conn.on('close', () => __awaiter(this, void 0, void 0, function* () {
453
+ console.log('close------------>', conn.label);
454
+ clearTimeout(timer);
439
455
  this.removeConnection(conn.peer, conn.connectionId);
440
456
  // if (!this.peer?.open) {
441
457
  // await this.reconnect();
@@ -465,7 +481,7 @@ class PeerTransfer {
465
481
  body: {},
466
482
  error: { message: 'request timeout' },
467
483
  };
468
- this.removeConnection(conn.peer);
484
+ this.removeConnection(conn.peer, conn.connectionId);
469
485
  resolve(msg);
470
486
  }, payload.timeout || entity_1.REQUEST_TIMEOUT);
471
487
  this.inRequest.set(conn.connectionId, {
@@ -560,6 +576,8 @@ class PeerTransfer {
560
576
  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
577
  return this.peer;
562
578
  }
579
+ yield this.reloadConfig();
580
+ return this.createPeer();
563
581
  // return this.createPeer();
564
582
  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
583
  this.peer.reconnect();
@@ -598,6 +616,8 @@ class PeerTransfer {
598
616
  onRequest(conn, message) {
599
617
  return __awaiter(this, void 0, void 0, function* () {
600
618
  try {
619
+ // @todo default folder
620
+ // console.log('onRequest-->', message.route);
601
621
  // @todo default folder
602
622
  yield this.router.run(conn, message);
603
623
  }
@@ -652,7 +672,7 @@ class PeerTransfer {
652
672
  }
653
673
  resume() {
654
674
  this.isPaused = false;
655
- return this.createPeer();
675
+ return this.createPeer(true);
656
676
  }
657
677
  }
658
678
  exports.PeerTransfer = PeerTransfer;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "egos-transfer",
3
- "version": "0.1.9",
3
+ "version": "0.2.1",
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;
@@ -88,7 +89,7 @@ export abstract class PeerTransfer {
88
89
  abstract reloadConfig(config?: Record<string, any>): Promise<void>;
89
90
 
90
91
  async createPeer(force = false): Promise<Peer | undefined> {
91
- if (this.isPaused) {
92
+ if (this.isPaused && !force) {
92
93
  return;
93
94
  }
94
95
  if (this.booted) {
@@ -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,6 @@ export abstract class PeerTransfer {
117
119
  if (this.booted) {
118
120
  return this.booted;
119
121
  }
120
-
121
122
  this.booted = new Promise((resolve) => {
122
123
  const timer = setTimeout(() => {
123
124
  resolve(null);
@@ -127,6 +128,7 @@ export abstract class PeerTransfer {
127
128
  resolve(peer);
128
129
  };
129
130
  peer.on('open', (id) => {
131
+ console.log('peer@open', Date.now());
130
132
  clearTimeout(timer);
131
133
  done(peer);
132
134
  });
@@ -246,6 +248,7 @@ export abstract class PeerTransfer {
246
248
  }
247
249
 
248
250
  removeConnection(peerId: string, connectionId?: string) {
251
+ // console.log('removeConnection@', peerId, connectionId);
249
252
  const conn = this.connectPool.get(peerId);
250
253
  if (conn) {
251
254
  if (connectionId && conn.connectionId !== connectionId) {
@@ -262,6 +265,16 @@ export abstract class PeerTransfer {
262
265
  conn.removeAllListeners();
263
266
  conn.close();
264
267
  this.peer?._removeConnection(conn);
268
+ } else {
269
+ const connections = this.peer?.connections || {};
270
+ const conns = connections[peerId] || [];
271
+ if (conns.length > 0) {
272
+ for (const conn of conns) {
273
+ conn.removeAllListeners();
274
+ conn.close();
275
+ this.peer?._removeConnection(conn);
276
+ }
277
+ }
265
278
  }
266
279
  }
267
280
 
@@ -293,9 +306,9 @@ export abstract class PeerTransfer {
293
306
  for (const deviceId of deviceIds) {
294
307
  const conn = this.connectPool.get(deviceId);
295
308
  if (!this.isValidConnection(conn)) {
296
- this.removeConnection(conn.peer);
309
+ this.removeConnection(conn.peer, conn.connectionId);
297
310
  } else {
298
- // this.ping(conn.peer);
311
+ this.ping(conn.peer);
299
312
  }
300
313
  }
301
314
  }, HEARTBEAT_INTERVAL);
@@ -327,12 +340,12 @@ export abstract class PeerTransfer {
327
340
 
328
341
  const peerId = deviceId;
329
342
  const existingConn = this.connectPool.get(peerId);
330
- console.log('connectToPeer', existingConn?.open, this.connectPool.size);
343
+ console.log('connectToPeer', this.isPaused, this.connectPool.size);
331
344
  if (existingConn) {
332
345
  if (this.isValidConnection(existingConn)) {
333
346
  return existingConn;
334
347
  } else {
335
- this.removeConnection(peerId);
348
+ this.removeConnection(existingConn.peer, existingConn.connectionId);
336
349
  }
337
350
  }
338
351
 
@@ -406,7 +419,7 @@ export abstract class PeerTransfer {
406
419
  }
407
420
  const conn = this.connectPool.get(peerId);
408
421
  if (!this.isValidConnection(conn)) {
409
- this.removeConnection(conn?.peer);
422
+ this.removeConnection(conn?.peer, conn?.connectionId);
410
423
  return;
411
424
  }
412
425
  const res = await this.request(conn.peer, {
@@ -416,7 +429,7 @@ export abstract class PeerTransfer {
416
429
  timeout: PING_TIMEOUT,
417
430
  });
418
431
  if (!res || res.error) {
419
- this.removeConnection(conn.peer);
432
+ this.removeConnection(conn.peer, conn.connectionId);
420
433
  return false;
421
434
  }
422
435
  return true;
@@ -430,12 +443,12 @@ export abstract class PeerTransfer {
430
443
  // console.log('onConnect', conn.label, exists);
431
444
  if (exists) {
432
445
  if (exists.metadata.time > conn.metadata.time) {
433
- this.removeConnection(conn.peer);
446
+ this.removeConnection(conn.peer, conn.connectionId);
434
447
  if (this.isValidConnection(exists)) {
435
448
  return exists;
436
449
  }
437
450
  } else {
438
- this.removeConnection(exists.peer);
451
+ this.removeConnection(exists.peer, exists.connectionId);
439
452
  }
440
453
  }
441
454
  return new Promise((resolve) => {
@@ -451,8 +464,7 @@ export abstract class PeerTransfer {
451
464
  this.addConnection(conn);
452
465
  done(conn);
453
466
  } else {
454
- conn.close();
455
- this.removeConnection(conn.peer);
467
+ this.removeConnection(conn.peer, conn.connectionId);
456
468
  done(undefined);
457
469
  }
458
470
  }, CONNECTION_TIMEOUT);
@@ -474,6 +486,8 @@ export abstract class PeerTransfer {
474
486
  done(null);
475
487
  });
476
488
  conn.on('close', async () => {
489
+ console.log('close------------>', conn.label);
490
+ clearTimeout(timer);
477
491
  this.removeConnection(conn.peer, conn.connectionId);
478
492
  // if (!this.peer?.open) {
479
493
  // await this.reconnect();
@@ -507,7 +521,7 @@ export abstract class PeerTransfer {
507
521
  body: {},
508
522
  error: { message: 'request timeout' },
509
523
  };
510
- this.removeConnection(conn.peer);
524
+ this.removeConnection(conn.peer, conn.connectionId);
511
525
  resolve(msg);
512
526
  }, payload.timeout || REQUEST_TIMEOUT);
513
527
  this.inRequest.set(conn.connectionId, {
@@ -635,6 +649,9 @@ export abstract class PeerTransfer {
635
649
  if (this.peer?.open && this.peerConfig.token === this.peer?.options.token) {
636
650
  return this.peer;
637
651
  }
652
+
653
+ await this.reloadConfig();
654
+ return this.createPeer();
638
655
  // return this.createPeer();
639
656
  if (this.peer?.disconnected && this.peer?.options.token === this.peerConfig.token) {
640
657
  this.peer.reconnect();
@@ -671,6 +688,8 @@ export abstract class PeerTransfer {
671
688
 
672
689
  async onRequest(conn: DataConnection, message: PeerMessage) {
673
690
  try {
691
+ // @todo default folder
692
+ // console.log('onRequest-->', message.route);
674
693
  // @todo default folder
675
694
  await this.router.run(conn, message);
676
695
  } catch (err: any) {
@@ -729,7 +748,7 @@ export abstract class PeerTransfer {
729
748
 
730
749
  resume() {
731
750
  this.isPaused = false;
732
- return this.createPeer();
751
+ return this.createPeer(true);
733
752
  }
734
753
  }
735
754