egos-transfer 0.0.8 → 0.0.10

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) {
@@ -77,12 +77,8 @@ class PeerTransfer {
77
77
  }, constant_1.CONNECTION_RETRY);
78
78
  }
79
79
  });
80
- peer.on('call', (mConn) => {
81
- console.log('on call1');
82
- // @todo
83
- });
80
+ peer.on('call', (mConn) => { });
84
81
  peer.on('close', (...args) => {
85
- console.log('peer-transfer@onClose', this.isPaused);
86
82
  if (this.isPaused) {
87
83
  return;
88
84
  }
@@ -93,7 +89,6 @@ class PeerTransfer {
93
89
  }, constant_1.CONNECTION_RETRY);
94
90
  });
95
91
  peer.on('error', (err) => {
96
- console.log('peer-transfer@onError>>', err, err.type, err.message, 123123);
97
92
  if (err.message === constant_1.CONNECTION_PEER_CONFLICT) {
98
93
  this.stop();
99
94
  }
@@ -122,7 +117,6 @@ class PeerTransfer {
122
117
  this.handleSocketMessage(message);
123
118
  });
124
119
  socket.on('close', () => {
125
- console.log('peer-transfer@onSocketClose', 'socket close', this.isPaused);
126
120
  setTimeout(() => {
127
121
  this.destroy();
128
122
  }, constant_1.CONNECTION_RETRY);
@@ -155,29 +149,12 @@ class PeerTransfer {
155
149
  if (this.timeoutId) {
156
150
  clearTimeout(this.timeoutId);
157
151
  }
158
- // this.timeoutId = setTimeout(() => {
159
- // // two heartbeats
160
- // if (Date.now() - this.heartbeat > HEARTBEAT_INTERVAL) {
161
- // if (this.isPaused) {
162
- // clearTimeout(this.timeoutId);
163
- // return;
164
- // }
165
- // console.log(
166
- // 'peer-transfer@onHeartbeatTimeout',
167
- // 'heartbeat timeout',
168
- // Date.now() - this.heartbeat,
169
- // );
170
- // this.reconnect();
171
- // }
172
- // }, HEARTBEAT_INTERVAL);
173
152
  }
174
153
  onError(message) {
175
154
  var _a;
176
155
  if (message.type !== 'ERROR') {
177
156
  return;
178
157
  }
179
- console.log('onPeerError', message);
180
- // this.destroy();
181
158
  if (((_a = message.payload) === null || _a === void 0 ? void 0 : _a.msg) === constant_1.CONNECTION_PEER_CONFLICT) {
182
159
  this.isPaused = true;
183
160
  this.getStore().dispatch({
@@ -212,8 +189,8 @@ class PeerTransfer {
212
189
  return;
213
190
  }
214
191
  const deviceId = message.payload.deviceId;
215
- this.removeConnection(deviceId);
216
192
  this.offline(deviceId);
193
+ this.ping(deviceId);
217
194
  }
218
195
  removeConnection(peerId) {
219
196
  var _a, _b;
@@ -223,6 +200,7 @@ class PeerTransfer {
223
200
  clearInterval((_b = conn.metadata) === null || _b === void 0 ? void 0 : _b.intervalId);
224
201
  }
225
202
  this.connectPool.delete(peerId);
203
+ this.offline(peerId);
226
204
  }
227
205
  }
228
206
  addConnection(conn) {
@@ -286,13 +264,11 @@ class PeerTransfer {
286
264
  if (this.connectPool.has(peerId)) {
287
265
  const conn = this.connectPool.get(peerId);
288
266
  if (conn === null || conn === void 0 ? void 0 : conn.open) {
289
- console.log('reused connection', conn.peer);
290
267
  return conn;
291
268
  }
292
269
  }
293
270
  // Check if there's already a connection attempt for this peerId
294
271
  if (PeerTransfer.connecting.has(peerId)) {
295
- console.log('connectToPeer@connecting for existing connection', peerId);
296
272
  // Wait for the existing connection attempt to complete
297
273
  const { promise } = yield PeerTransfer.connecting.get(peerId);
298
274
  return yield promise;
@@ -313,11 +289,8 @@ class PeerTransfer {
313
289
  if (!this.peer) {
314
290
  yield this.createPeer();
315
291
  }
316
- if (!this.peer) {
317
- return;
318
- }
319
292
  if (!((_a = this.peer) === null || _a === void 0 ? void 0 : _a.open)) {
320
- this.reconnect();
293
+ yield this.reconnect();
321
294
  }
322
295
  const cur = this.connectPool.get(peerId);
323
296
  if (cur === null || cur === void 0 ? void 0 : cur.open) {
@@ -337,9 +310,9 @@ class PeerTransfer {
337
310
  }
338
311
  retry++;
339
312
  }
313
+ resolve(null);
340
314
  }
341
315
  catch (err) {
342
- console.log('connectToPeer@err', err);
343
316
  reject(err);
344
317
  }
345
318
  finally {
@@ -355,6 +328,9 @@ class PeerTransfer {
355
328
  }
356
329
  ping(peerId) {
357
330
  return __awaiter(this, void 0, void 0, function* () {
331
+ if (this.deviceId === peerId) {
332
+ return;
333
+ }
358
334
  const conn = this.connectPool.get(peerId);
359
335
  if (!conn) {
360
336
  return;
@@ -372,8 +348,8 @@ class PeerTransfer {
372
348
  return true;
373
349
  });
374
350
  }
375
- onConnect(conn_1) {
376
- return __awaiter(this, arguments, void 0, function* (conn, positive = true) {
351
+ onConnect(conn) {
352
+ return __awaiter(this, void 0, void 0, function* () {
377
353
  if (!conn) {
378
354
  return;
379
355
  }
@@ -409,7 +385,6 @@ class PeerTransfer {
409
385
  });
410
386
  conn.on('error', (err) => __awaiter(this, void 0, void 0, function* () {
411
387
  var _a;
412
- console.log('@!!!!!!connection error', err);
413
388
  this.removeConnection(conn.peer);
414
389
  if (!((_a = this.peer) === null || _a === void 0 ? void 0 : _a.open)) {
415
390
  yield this.reconnect();
@@ -483,7 +458,6 @@ class PeerTransfer {
483
458
  connection = con;
484
459
  }
485
460
  const requestId = message.requestId || (yield this.genRequestId());
486
- console.log('send@message', message.route);
487
461
  const sent = yield connection.send(Object.assign(Object.assign({}, message), { requestId, headers: Object.assign(Object.assign({}, message.headers), { ['x-device-token']: internalApi === null || internalApi === void 0 ? void 0 : internalApi.token }), scope: entity_1.PeerScope.REQUEST, action: entity_1.PeerAction.SEND, src: this.peerId, dest: conn.peer, createdAt: Date.now() }), false);
488
462
  return sent;
489
463
  });
@@ -511,16 +485,15 @@ class PeerTransfer {
511
485
  }
512
486
  doReconnect() {
513
487
  return __awaiter(this, void 0, void 0, function* () {
514
- var _a, _b, _c, _d;
488
+ var _a, _b;
515
489
  try {
516
- console.log('doReconnect@', this.isPaused, (_a = this.peer) === null || _a === void 0 ? void 0 : _a.open, (_b = this.peer) === null || _b === void 0 ? void 0 : _b.disconnected);
517
490
  if (this.isPaused) {
518
491
  return;
519
492
  }
520
- if ((_c = this.peer) === null || _c === void 0 ? void 0 : _c.open) {
493
+ if ((_a = this.peer) === null || _a === void 0 ? void 0 : _a.open) {
521
494
  return this.peer;
522
495
  }
523
- if ((_d = this.peer) === null || _d === void 0 ? void 0 : _d.disconnected) {
496
+ if ((_b = this.peer) === null || _b === void 0 ? void 0 : _b.disconnected) {
524
497
  this.peer.reconnect();
525
498
  return;
526
499
  }
@@ -532,7 +505,6 @@ class PeerTransfer {
532
505
  }
533
506
  }
534
507
  catch (err) {
535
- console.log('###reconnect@error', err);
536
508
  this.destroy();
537
509
  return this.createPeer();
538
510
  }
@@ -591,11 +563,9 @@ class PeerTransfer {
591
563
  return __awaiter(this, void 0, void 0, function* () {
592
564
  try {
593
565
  // @todo default folder
594
- console.log('onRequest@----++++message', message.route);
595
566
  yield this.router.run(conn, message);
596
567
  }
597
568
  catch (err) {
598
- console.log('onRequest@error', err);
599
569
  this.responseFail(conn, message, { message: err.message, code: err.code });
600
570
  }
601
571
  });
@@ -603,15 +573,12 @@ class PeerTransfer {
603
573
  onResponse(conn, message) {
604
574
  return __awaiter(this, void 0, void 0, function* () {
605
575
  // @todo default folder
606
- console.log('onResponse@----++++message', message.route);
607
576
  const cb = this.requests.get(message.requestId);
608
577
  if (cb) {
609
578
  cb.apply(this, [message]);
610
579
  this.requests.delete(message.requestId);
611
580
  }
612
- if (message.error) {
613
- console.log('response@error>>', message);
614
- }
581
+ // @todo throw err;
615
582
  });
616
583
  }
617
584
  responseOk(conn, message) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "egos-transfer",
3
- "version": "0.0.8",
3
+ "version": "0.0.10",
4
4
  "description": "1234",
5
5
  "homepage": "https://github.com/superbogy/peer-transfer#readme",
6
6
  "bugs": {
@@ -143,7 +143,6 @@ export class FileTransfer {
143
143
  if (attempt === maxRetries) {
144
144
  throw lastError;
145
145
  }
146
-
147
146
  const delay = baseDelay * Math.pow(2, attempt);
148
147
  console.log(`Retry attempt ${attempt + 1}/${maxRetries} after ${delay}ms`);
149
148
  }
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) {
@@ -113,12 +113,8 @@ export abstract class PeerTransfer {
113
113
  }, CONNECTION_RETRY);
114
114
  }
115
115
  });
116
- peer.on('call', (mConn: MediaConnection) => {
117
- console.log('on call1');
118
- // @todo
119
- });
116
+ peer.on('call', (mConn: MediaConnection) => {});
120
117
  peer.on('close', (...args) => {
121
- console.log('peer-transfer@onClose', this.isPaused);
122
118
  if (this.isPaused) {
123
119
  return;
124
120
  }
@@ -129,7 +125,6 @@ export abstract class PeerTransfer {
129
125
  }, CONNECTION_RETRY);
130
126
  });
131
127
  peer.on('error', (err) => {
132
- console.log('peer-transfer@onError>>', err, err.type, err.message, 123123);
133
128
  if (err.message === CONNECTION_PEER_CONFLICT) {
134
129
  this.stop();
135
130
  }
@@ -158,7 +153,6 @@ export abstract class PeerTransfer {
158
153
  this.handleSocketMessage(message);
159
154
  });
160
155
  socket.on('close', () => {
161
- console.log('peer-transfer@onSocketClose', 'socket close', this.isPaused);
162
156
  setTimeout(() => {
163
157
  this.destroy();
164
158
  }, CONNECTION_RETRY);
@@ -192,29 +186,12 @@ export abstract class PeerTransfer {
192
186
  if (this.timeoutId) {
193
187
  clearTimeout(this.timeoutId);
194
188
  }
195
- // this.timeoutId = setTimeout(() => {
196
- // // two heartbeats
197
- // if (Date.now() - this.heartbeat > HEARTBEAT_INTERVAL) {
198
- // if (this.isPaused) {
199
- // clearTimeout(this.timeoutId);
200
- // return;
201
- // }
202
- // console.log(
203
- // 'peer-transfer@onHeartbeatTimeout',
204
- // 'heartbeat timeout',
205
- // Date.now() - this.heartbeat,
206
- // );
207
- // this.reconnect();
208
- // }
209
- // }, HEARTBEAT_INTERVAL);
210
189
  }
211
190
 
212
191
  onError(message: IMessage) {
213
192
  if (message.type !== 'ERROR') {
214
193
  return;
215
194
  }
216
- console.log('onPeerError', message);
217
- // this.destroy();
218
195
  if (message.payload?.msg === CONNECTION_PEER_CONFLICT) {
219
196
  this.isPaused = true;
220
197
  this.getStore().dispatch({
@@ -250,8 +227,8 @@ export abstract class PeerTransfer {
250
227
  return;
251
228
  }
252
229
  const deviceId = message.payload.deviceId;
253
- this.removeConnection(deviceId);
254
230
  this.offline(deviceId);
231
+ this.ping(deviceId);
255
232
  }
256
233
 
257
234
  removeConnection(peerId: string) {
@@ -261,6 +238,7 @@ export abstract class PeerTransfer {
261
238
  clearInterval(conn.metadata?.intervalId);
262
239
  }
263
240
  this.connectPool.delete(peerId);
241
+ this.offline(peerId);
264
242
  }
265
243
  }
266
244
 
@@ -322,13 +300,11 @@ export abstract class PeerTransfer {
322
300
  if (this.connectPool.has(peerId)) {
323
301
  const conn = this.connectPool.get(peerId);
324
302
  if (conn?.open) {
325
- console.log('reused connection', conn.peer);
326
303
  return conn;
327
304
  }
328
305
  }
329
306
  // Check if there's already a connection attempt for this peerId
330
307
  if (PeerTransfer.connecting.has(peerId)) {
331
- console.log('connectToPeer@connecting for existing connection', peerId);
332
308
  // Wait for the existing connection attempt to complete
333
309
  const { promise } = await PeerTransfer.connecting.get(peerId);
334
310
  return await promise;
@@ -349,11 +325,8 @@ export abstract class PeerTransfer {
349
325
  if (!this.peer) {
350
326
  await this.createPeer();
351
327
  }
352
- if (!this.peer) {
353
- return;
354
- }
355
328
  if (!this.peer?.open) {
356
- this.reconnect();
329
+ await this.reconnect();
357
330
  }
358
331
  const cur = this.connectPool.get(peerId);
359
332
  if (cur?.open) {
@@ -373,8 +346,8 @@ export abstract class PeerTransfer {
373
346
  }
374
347
  retry++;
375
348
  }
349
+ resolve(null);
376
350
  } catch (err) {
377
- console.log('connectToPeer@err', err);
378
351
  reject(err);
379
352
  } finally {
380
353
  // Remove from connecting map regardless of success or failure
@@ -389,6 +362,9 @@ export abstract class PeerTransfer {
389
362
  }
390
363
 
391
364
  async ping(peerId: string) {
365
+ if (this.deviceId === peerId) {
366
+ return;
367
+ }
392
368
  const conn = this.connectPool.get(peerId);
393
369
  if (!conn) {
394
370
  return;
@@ -406,7 +382,7 @@ export abstract class PeerTransfer {
406
382
  return true;
407
383
  }
408
384
 
409
- async onConnect(conn: DataConnection, positive = true): Promise<DataConnection | undefined> {
385
+ async onConnect(conn: DataConnection): Promise<DataConnection | undefined> {
410
386
  if (!conn) {
411
387
  return;
412
388
  }
@@ -440,7 +416,6 @@ export abstract class PeerTransfer {
440
416
  }
441
417
  });
442
418
  conn.on('error', async (err) => {
443
- console.log('@!!!!!!connection error', err);
444
419
  this.removeConnection(conn.peer);
445
420
  if (!this.peer?.open) {
446
421
  await this.reconnect();
@@ -533,7 +508,6 @@ export abstract class PeerTransfer {
533
508
  connection = con;
534
509
  }
535
510
  const requestId = message.requestId || (await this.genRequestId());
536
- console.log('send@message', message.route);
537
511
  const sent = await connection.send(
538
512
  {
539
513
  ...message,
@@ -577,7 +551,6 @@ export abstract class PeerTransfer {
577
551
 
578
552
  private async doReconnect(): Promise<Peer | undefined> {
579
553
  try {
580
- console.log('doReconnect@', this.isPaused, this.peer?.open, this.peer?.disconnected);
581
554
  if (this.isPaused) {
582
555
  return;
583
556
  }
@@ -594,7 +567,6 @@ export abstract class PeerTransfer {
594
567
  return;
595
568
  }
596
569
  } catch (err) {
597
- console.log('###reconnect@error', err);
598
570
  this.destroy();
599
571
  return this.createPeer();
600
572
  }
@@ -655,25 +627,20 @@ export abstract class PeerTransfer {
655
627
  async onRequest(conn: DataConnection, message: PeerMessage) {
656
628
  try {
657
629
  // @todo default folder
658
- console.log('onRequest@----++++message', message.route);
659
630
  await this.router.run(conn, message);
660
631
  } catch (err: any) {
661
- console.log('onRequest@error', err);
662
632
  this.responseFail(conn, message, { message: err.message, code: err.code });
663
633
  }
664
634
  }
665
635
 
666
636
  async onResponse(conn: DataConnection, message: PeerMessage) {
667
637
  // @todo default folder
668
- console.log('onResponse@----++++message', message.route);
669
638
  const cb = this.requests.get(message.requestId);
670
639
  if (cb) {
671
640
  cb.apply(this, [message]);
672
641
  this.requests.delete(message.requestId);
673
642
  }
674
- if (message.error) {
675
- console.log('response@error>>', message);
676
- }
643
+ // @todo throw err;
677
644
  }
678
645
 
679
646
  async responseOk(conn: DataConnection, message: PeerMessage) {