egos-transfer 0.1.8 → 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.
package/dist/index.d.ts CHANGED
@@ -42,7 +42,7 @@ export declare abstract class PeerTransfer {
42
42
  abstract onConnectLimit(): void;
43
43
  abstract reloadConfig(config?: Record<string, any>): Promise<void>;
44
44
  createPeer(force?: boolean): Promise<Peer | undefined>;
45
- initialize(peer: Peer): Promise<void>;
45
+ initialize(peer: Peer): Promise<Peer>;
46
46
  handleSocketMessage(message: IMessage): void;
47
47
  onHeartbeat(message: IMessage): void;
48
48
  onError(message: IMessage): void;
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,8 +59,9 @@ 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
- const peer = new peerjs_1.Peer(this.peerId, Object.assign(Object.assign({}, this.peerConfig), { pingInterval: 15000 }));
64
+ const peer = new peerjs_1.Peer(this.peerId, Object.assign(Object.assign({}, this.peerConfig), { pingInterval: 9000 }));
63
65
  this.peer = peer;
64
66
  yield this.initialize(this.peer);
65
67
  this.checkConnection();
@@ -72,6 +74,10 @@ class PeerTransfer {
72
74
  if (this.checkerId) {
73
75
  clearInterval(this.checkerId);
74
76
  }
77
+ if (this.booted) {
78
+ return this.booted;
79
+ }
80
+ console.log('create peer@@@', Date.now());
75
81
  this.booted = new Promise((resolve) => {
76
82
  const timer = setTimeout(() => {
77
83
  resolve(null);
@@ -81,6 +87,7 @@ class PeerTransfer {
81
87
  resolve(peer);
82
88
  };
83
89
  peer.on('open', (id) => {
90
+ console.log('peer@open', Date.now());
84
91
  clearTimeout(timer);
85
92
  done(peer);
86
93
  });
@@ -199,7 +206,8 @@ class PeerTransfer {
199
206
  this.ping(deviceId);
200
207
  }
201
208
  removeConnection(peerId, connectionId) {
202
- var _a;
209
+ var _a, _b, _c;
210
+ // console.log('removeConnection@', peerId, connectionId);
203
211
  const conn = this.connectPool.get(peerId);
204
212
  if (conn) {
205
213
  if (connectionId && conn.connectionId !== connectionId) {
@@ -216,6 +224,17 @@ class PeerTransfer {
216
224
  conn.close();
217
225
  (_a = this.peer) === null || _a === void 0 ? void 0 : _a._removeConnection(conn);
218
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
+ }
219
238
  }
220
239
  addConnection(conn) {
221
240
  // console.log('addConnection@', conn.label);
@@ -245,7 +264,7 @@ class PeerTransfer {
245
264
  for (const deviceId of deviceIds) {
246
265
  const conn = this.connectPool.get(deviceId);
247
266
  if (!this.isValidConnection(conn)) {
248
- this.removeConnection(conn.peer);
267
+ this.removeConnection(conn.peer, conn.connectionId);
249
268
  }
250
269
  else {
251
270
  this.ping(conn.peer);
@@ -282,13 +301,13 @@ class PeerTransfer {
282
301
  }
283
302
  const peerId = deviceId;
284
303
  const existingConn = this.connectPool.get(peerId);
285
- // console.log('connectToPeer', peerId, existingConn, this.connectPool);
304
+ console.log('connectToPeer', existingConn === null || existingConn === void 0 ? void 0 : existingConn.open, this.connectPool.size);
286
305
  if (existingConn) {
287
306
  if (this.isValidConnection(existingConn)) {
288
307
  return existingConn;
289
308
  }
290
309
  else {
291
- this.removeConnection(peerId);
310
+ this.removeConnection(existingConn.peer, existingConn.connectionId);
292
311
  }
293
312
  }
294
313
  if (PeerTransfer.connecting.has(peerId)) {
@@ -303,49 +322,56 @@ class PeerTransfer {
303
322
  }
304
323
  createConnection(peerId) {
305
324
  return __awaiter(this, void 0, void 0, function* () {
306
- var _a, _b;
307
325
  let resolve;
308
326
  let reject;
327
+ const q = PeerTransfer.connecting.get(peerId);
328
+ if (q) {
329
+ return q.promise;
330
+ }
309
331
  const connectionPromise = new Promise((res, rej) => {
310
332
  resolve = res;
311
333
  reject = rej;
312
334
  });
313
335
  PeerTransfer.connecting.set(peerId, { promise: connectionPromise, resolve, reject });
314
- try {
315
- for (let retry = 0; retry < 3; retry++) {
316
- if (!this.peer) {
317
- yield this.createPeer();
318
- }
319
- if (!((_a = this.peer) === null || _a === void 0 ? void 0 : _a.open)) {
320
- yield this.reconnect();
321
- }
322
- const cur = this.connectPool.get(peerId);
323
- if (this.isValidConnection(cur)) {
324
- resolve(cur);
325
- return cur;
326
- }
327
- const conn = (_b = this.peer) === null || _b === void 0 ? void 0 : _b.connect(peerId, {
328
- label: 'outgoing',
329
- metadata: { time: Date.now() },
330
- });
331
- if (!conn) {
332
- continue;
333
- }
334
- const connection = yield this.onConnect(conn).catch(() => { });
335
- if (connection) {
336
- resolve(conn);
337
- return conn;
336
+ const doConnect = () => __awaiter(this, void 0, void 0, function* () {
337
+ var _a, _b;
338
+ try {
339
+ for (let retry = 0; retry < 3; retry++) {
340
+ if (!this.peer) {
341
+ yield this.createPeer();
342
+ }
343
+ if (!((_a = this.peer) === null || _a === void 0 ? void 0 : _a.open)) {
344
+ yield this.reconnect();
345
+ }
346
+ const cur = this.connectPool.get(peerId);
347
+ if (this.isValidConnection(cur)) {
348
+ return resolve(cur);
349
+ }
350
+ const conn = (_b = this.peer) === null || _b === void 0 ? void 0 : _b.connect(peerId, {
351
+ label: 'outgoing',
352
+ metadata: { time: Date.now() },
353
+ });
354
+ if (!conn) {
355
+ continue;
356
+ }
357
+ const connection = yield this.onConnect(conn).catch(() => { });
358
+ if (connection) {
359
+ return resolve(connection);
360
+ }
338
361
  }
362
+ resolve(undefined);
363
+ return undefined;
339
364
  }
340
- resolve(undefined);
341
- return undefined;
342
- }
343
- catch (err) {
344
- reject(err);
345
- }
346
- finally {
347
- PeerTransfer.connecting.delete(peerId);
348
- }
365
+ catch (err) {
366
+ reject(err);
367
+ }
368
+ finally {
369
+ PeerTransfer.connecting.delete(peerId);
370
+ }
371
+ });
372
+ doConnect();
373
+ const conn = yield connectionPromise;
374
+ return conn;
349
375
  });
350
376
  }
351
377
  ping(peerId) {
@@ -355,7 +381,7 @@ class PeerTransfer {
355
381
  }
356
382
  const conn = this.connectPool.get(peerId);
357
383
  if (!this.isValidConnection(conn)) {
358
- 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);
359
385
  return;
360
386
  }
361
387
  const res = yield this.request(conn.peer, {
@@ -365,7 +391,7 @@ class PeerTransfer {
365
391
  timeout: constant_1.PING_TIMEOUT,
366
392
  });
367
393
  if (!res || res.error) {
368
- this.removeConnection(conn.peer);
394
+ this.removeConnection(conn.peer, conn.connectionId);
369
395
  return false;
370
396
  }
371
397
  return true;
@@ -380,13 +406,13 @@ class PeerTransfer {
380
406
  // console.log('onConnect', conn.label, exists);
381
407
  if (exists) {
382
408
  if (exists.metadata.time > conn.metadata.time) {
383
- this.removeConnection(conn.peer);
409
+ this.removeConnection(conn.peer, conn.connectionId);
384
410
  if (this.isValidConnection(exists)) {
385
411
  return exists;
386
412
  }
387
413
  }
388
414
  else {
389
- this.removeConnection(exists.peer);
415
+ this.removeConnection(exists.peer, exists.connectionId);
390
416
  }
391
417
  }
392
418
  return new Promise((resolve) => {
@@ -403,8 +429,7 @@ class PeerTransfer {
403
429
  done(conn);
404
430
  }
405
431
  else {
406
- conn.close();
407
- this.removeConnection(conn.peer);
432
+ this.removeConnection(conn.peer, conn.connectionId);
408
433
  done(undefined);
409
434
  }
410
435
  }), constant_1.CONNECTION_TIMEOUT);
@@ -426,6 +451,8 @@ class PeerTransfer {
426
451
  done(null);
427
452
  }));
428
453
  conn.on('close', () => __awaiter(this, void 0, void 0, function* () {
454
+ console.log('close------------>', conn.label);
455
+ clearTimeout(timer);
429
456
  this.removeConnection(conn.peer, conn.connectionId);
430
457
  // if (!this.peer?.open) {
431
458
  // await this.reconnect();
@@ -455,7 +482,7 @@ class PeerTransfer {
455
482
  body: {},
456
483
  error: { message: 'request timeout' },
457
484
  };
458
- this.removeConnection(conn.peer);
485
+ this.removeConnection(conn.peer, conn.connectionId);
459
486
  resolve(msg);
460
487
  }, payload.timeout || entity_1.REQUEST_TIMEOUT);
461
488
  this.inRequest.set(conn.connectionId, {
@@ -550,6 +577,8 @@ class PeerTransfer {
550
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)) {
551
578
  return this.peer;
552
579
  }
580
+ yield this.reloadConfig();
581
+ return this.createPeer();
553
582
  // return this.createPeer();
554
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) {
555
584
  this.peer.reconnect();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "egos-transfer",
3
- "version": "0.1.8",
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,10 +99,11 @@ 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,
104
- pingInterval: 15000,
106
+ pingInterval: 9000,
105
107
  });
106
108
  this.peer = peer;
107
109
  await this.initialize(this.peer);
@@ -114,6 +116,10 @@ export abstract class PeerTransfer {
114
116
  clearInterval(this.checkerId);
115
117
  }
116
118
 
119
+ if (this.booted) {
120
+ return this.booted;
121
+ }
122
+ console.log('create peer@@@', Date.now());
117
123
  this.booted = new Promise((resolve) => {
118
124
  const timer = setTimeout(() => {
119
125
  resolve(null);
@@ -123,6 +129,7 @@ export abstract class PeerTransfer {
123
129
  resolve(peer);
124
130
  };
125
131
  peer.on('open', (id) => {
132
+ console.log('peer@open', Date.now());
126
133
  clearTimeout(timer);
127
134
  done(peer);
128
135
  });
@@ -242,6 +249,7 @@ export abstract class PeerTransfer {
242
249
  }
243
250
 
244
251
  removeConnection(peerId: string, connectionId?: string) {
252
+ // console.log('removeConnection@', peerId, connectionId);
245
253
  const conn = this.connectPool.get(peerId);
246
254
  if (conn) {
247
255
  if (connectionId && conn.connectionId !== connectionId) {
@@ -258,6 +266,16 @@ export abstract class PeerTransfer {
258
266
  conn.removeAllListeners();
259
267
  conn.close();
260
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
+ }
261
279
  }
262
280
  }
263
281
 
@@ -289,7 +307,7 @@ export abstract class PeerTransfer {
289
307
  for (const deviceId of deviceIds) {
290
308
  const conn = this.connectPool.get(deviceId);
291
309
  if (!this.isValidConnection(conn)) {
292
- this.removeConnection(conn.peer);
310
+ this.removeConnection(conn.peer, conn.connectionId);
293
311
  } else {
294
312
  this.ping(conn.peer);
295
313
  }
@@ -323,12 +341,12 @@ export abstract class PeerTransfer {
323
341
 
324
342
  const peerId = deviceId;
325
343
  const existingConn = this.connectPool.get(peerId);
326
- // console.log('connectToPeer', peerId, existingConn, this.connectPool);
344
+ console.log('connectToPeer', existingConn?.open, this.connectPool.size);
327
345
  if (existingConn) {
328
346
  if (this.isValidConnection(existingConn)) {
329
347
  return existingConn;
330
348
  } else {
331
- this.removeConnection(peerId);
349
+ this.removeConnection(existingConn.peer, existingConn.connectionId);
332
350
  }
333
351
  }
334
352
 
@@ -346,48 +364,54 @@ export abstract class PeerTransfer {
346
364
  private async createConnection(peerId: string): Promise<DataConnection | undefined> {
347
365
  let resolve!: (value: DataConnection | undefined) => void;
348
366
  let reject!: (reason?: any) => void;
349
-
367
+ const q = PeerTransfer.connecting.get(peerId);
368
+ if (q) {
369
+ return q.promise;
370
+ }
350
371
  const connectionPromise = new Promise<DataConnection | undefined>((res, rej) => {
351
372
  resolve = res;
352
373
  reject = rej;
353
374
  });
354
375
 
355
376
  PeerTransfer.connecting.set(peerId, { promise: connectionPromise, resolve, reject });
356
- try {
357
- for (let retry = 0; retry < 3; retry++) {
358
- if (!this.peer) {
359
- await this.createPeer();
360
- }
361
- if (!this.peer?.open) {
362
- await this.reconnect();
363
- }
377
+ const doConnect = async () => {
378
+ try {
379
+ for (let retry = 0; retry < 3; retry++) {
380
+ if (!this.peer) {
381
+ await this.createPeer();
382
+ }
383
+ if (!this.peer?.open) {
384
+ await this.reconnect();
385
+ }
364
386
 
365
- const cur = this.connectPool.get(peerId);
366
- if (this.isValidConnection(cur)) {
367
- resolve(cur);
368
- return cur;
369
- }
387
+ const cur = this.connectPool.get(peerId);
388
+ if (this.isValidConnection(cur)) {
389
+ return resolve(cur);
390
+ }
370
391
 
371
- const conn = this.peer?.connect(peerId, {
372
- label: 'outgoing',
373
- metadata: { time: Date.now() },
374
- });
375
- if (!conn) {
376
- continue;
377
- }
378
- const connection = await this.onConnect(conn as DataConnection).catch(() => {});
379
- if (connection) {
380
- resolve(conn);
381
- return conn;
392
+ const conn = this.peer?.connect(peerId, {
393
+ label: 'outgoing',
394
+ metadata: { time: Date.now() },
395
+ });
396
+ if (!conn) {
397
+ continue;
398
+ }
399
+ const connection = await this.onConnect(conn as DataConnection).catch(() => {});
400
+ if (connection) {
401
+ return resolve(connection);
402
+ }
382
403
  }
404
+ resolve(undefined);
405
+ return undefined;
406
+ } catch (err) {
407
+ reject(err);
408
+ } finally {
409
+ PeerTransfer.connecting.delete(peerId);
383
410
  }
384
- resolve(undefined);
385
- return undefined;
386
- } catch (err) {
387
- reject(err);
388
- } finally {
389
- PeerTransfer.connecting.delete(peerId);
390
- }
411
+ };
412
+ doConnect();
413
+ const conn = await connectionPromise;
414
+ return conn;
391
415
  }
392
416
 
393
417
  async ping(peerId: string) {
@@ -396,7 +420,7 @@ export abstract class PeerTransfer {
396
420
  }
397
421
  const conn = this.connectPool.get(peerId);
398
422
  if (!this.isValidConnection(conn)) {
399
- this.removeConnection(conn?.peer);
423
+ this.removeConnection(conn?.peer, conn?.connectionId);
400
424
  return;
401
425
  }
402
426
  const res = await this.request(conn.peer, {
@@ -406,7 +430,7 @@ export abstract class PeerTransfer {
406
430
  timeout: PING_TIMEOUT,
407
431
  });
408
432
  if (!res || res.error) {
409
- this.removeConnection(conn.peer);
433
+ this.removeConnection(conn.peer, conn.connectionId);
410
434
  return false;
411
435
  }
412
436
  return true;
@@ -420,12 +444,12 @@ export abstract class PeerTransfer {
420
444
  // console.log('onConnect', conn.label, exists);
421
445
  if (exists) {
422
446
  if (exists.metadata.time > conn.metadata.time) {
423
- this.removeConnection(conn.peer);
447
+ this.removeConnection(conn.peer, conn.connectionId);
424
448
  if (this.isValidConnection(exists)) {
425
449
  return exists;
426
450
  }
427
451
  } else {
428
- this.removeConnection(exists.peer);
452
+ this.removeConnection(exists.peer, exists.connectionId);
429
453
  }
430
454
  }
431
455
  return new Promise((resolve) => {
@@ -441,8 +465,7 @@ export abstract class PeerTransfer {
441
465
  this.addConnection(conn);
442
466
  done(conn);
443
467
  } else {
444
- conn.close();
445
- this.removeConnection(conn.peer);
468
+ this.removeConnection(conn.peer, conn.connectionId);
446
469
  done(undefined);
447
470
  }
448
471
  }, CONNECTION_TIMEOUT);
@@ -464,6 +487,8 @@ export abstract class PeerTransfer {
464
487
  done(null);
465
488
  });
466
489
  conn.on('close', async () => {
490
+ console.log('close------------>', conn.label);
491
+ clearTimeout(timer);
467
492
  this.removeConnection(conn.peer, conn.connectionId);
468
493
  // if (!this.peer?.open) {
469
494
  // await this.reconnect();
@@ -497,7 +522,7 @@ export abstract class PeerTransfer {
497
522
  body: {},
498
523
  error: { message: 'request timeout' },
499
524
  };
500
- this.removeConnection(conn.peer);
525
+ this.removeConnection(conn.peer, conn.connectionId);
501
526
  resolve(msg);
502
527
  }, payload.timeout || REQUEST_TIMEOUT);
503
528
  this.inRequest.set(conn.connectionId, {
@@ -625,6 +650,9 @@ export abstract class PeerTransfer {
625
650
  if (this.peer?.open && this.peerConfig.token === this.peer?.options.token) {
626
651
  return this.peer;
627
652
  }
653
+
654
+ await this.reloadConfig();
655
+ return this.createPeer();
628
656
  // return this.createPeer();
629
657
  if (this.peer?.disconnected && this.peer?.options.token === this.peerConfig.token) {
630
658
  this.peer.reconnect();