egos-transfer 0.1.0 → 0.1.3

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.
@@ -1,8 +1,8 @@
1
1
  export declare const REQUEST_TIMEOUT = 18000;
2
- export declare const HEARTBEAT_INTERVAL = 15000;
2
+ export declare const HEARTBEAT_INTERVAL = 12000;
3
3
  export declare const TRANSFER_TIMEOUT = 300000;
4
4
  export declare const PING_TIMEOUT = 5000;
5
- export declare const CONNECTION_TIMEOUT = 6000;
5
+ export declare const CONNECTION_TIMEOUT = 12000;
6
6
  export declare const LOCKER_TIMEOUT = 15000;
7
7
  export declare const RECONNECT_TIMEOUT = 5000;
8
8
  export declare const PEER_OPEN_TIMEOUT = 15000;
package/dist/constant.js CHANGED
@@ -2,10 +2,10 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.UNAUTHORIZED = exports.CONNECTION_DUPLICATED = exports.CONNECTION_LIMIT_EXCEED = exports.CONNECTION_PEER_CONFLICT = exports.CONNECTION_RETRY = exports.PEER_OPEN_TIMEOUT = exports.RECONNECT_TIMEOUT = exports.LOCKER_TIMEOUT = exports.CONNECTION_TIMEOUT = exports.PING_TIMEOUT = exports.TRANSFER_TIMEOUT = exports.HEARTBEAT_INTERVAL = exports.REQUEST_TIMEOUT = void 0;
4
4
  exports.REQUEST_TIMEOUT = 18000;
5
- exports.HEARTBEAT_INTERVAL = 15000;
5
+ exports.HEARTBEAT_INTERVAL = 12000;
6
6
  exports.TRANSFER_TIMEOUT = 300000;
7
7
  exports.PING_TIMEOUT = 5000;
8
- exports.CONNECTION_TIMEOUT = 6000;
8
+ exports.CONNECTION_TIMEOUT = 12000;
9
9
  exports.LOCKER_TIMEOUT = 15000;
10
10
  exports.RECONNECT_TIMEOUT = 5000;
11
11
  exports.PEER_OPEN_TIMEOUT = 15000;
package/dist/index.d.ts CHANGED
@@ -7,6 +7,9 @@ export interface ContentPayload {
7
7
  parent: string;
8
8
  }
9
9
  type ResponseCb<T = any> = (args: T) => void;
10
+ type PeerTransferOptions = PeerOptions & {
11
+ turnExpiredAt: number;
12
+ };
10
13
  export declare abstract class PeerTransfer {
11
14
  protected peer: Peer | undefined;
12
15
  protected peerPool: Map<string, Peer>;
@@ -18,18 +21,17 @@ export declare abstract class PeerTransfer {
18
21
  reject: (reason?: any) => void;
19
22
  }>;
20
23
  protected retry: number;
21
- protected peerConfig: PeerOptions | undefined;
24
+ protected peerConfig: PeerTransferOptions | undefined;
22
25
  deviceId: string;
23
26
  protected requests: Map<string, ResponseCb>;
24
27
  protected timeout: number;
25
28
  protected router: PeerRouter;
26
29
  protected booted: Promise<string> | undefined;
27
30
  protected reconnectingPromise: Promise<Peer | undefined> | undefined;
28
- protected timeoutId: ReturnType<typeof setTimeout>;
29
31
  protected checkerId: ReturnType<typeof setInterval>;
30
32
  isPaused: boolean;
31
33
  peerId: string;
32
- constructor(deviceId: string, config: PeerOptions, isRadomPeerId?: boolean);
34
+ constructor(deviceId: string, config: PeerTransferOptions, isRadomPeerId?: boolean);
33
35
  abstract getStore(): any;
34
36
  abstract online(deviceId: string): void;
35
37
  abstract offline(deviceId: string): void;
@@ -37,8 +39,9 @@ export declare abstract class PeerTransfer {
37
39
  abstract getApi(deviceId: string): Promise<InternalApi | undefined>;
38
40
  abstract discoverDeviceById(deviceId: string): Promise<void>;
39
41
  abstract onDestroy(): void;
40
- abstract requireAuth(): void;
42
+ abstract requireAuth(): Promise<void>;
41
43
  abstract onConnectLimit(): void;
44
+ abstract reloadConfig(config?: Record<string, any>): Promise<void>;
42
45
  initialize(peer: Peer): Promise<string>;
43
46
  handleSocketMessage(message: IMessage): void;
44
47
  onHeartbeat(message: IMessage): void;
package/dist/index.js CHANGED
@@ -30,9 +30,9 @@ exports.PeerTransfer = void 0;
30
30
  const constant_1 = require("./constant");
31
31
  const peerjs_1 = require("peerjs");
32
32
  const entity_1 = require("./entity");
33
- const helper_1 = require("./helper");
34
33
  const router_1 = require("./router");
35
34
  const axios_1 = __importDefault(require("axios"));
35
+ const helper_1 = require("./helper");
36
36
  class PeerTransfer {
37
37
  constructor(deviceId, config, isRadomPeerId = false) {
38
38
  this.peerIdMap = new Map();
@@ -54,7 +54,6 @@ class PeerTransfer {
54
54
  const timer = setTimeout(() => {
55
55
  var _a;
56
56
  if (!((_a = this.peer) === null || _a === void 0 ? void 0 : _a.open)) {
57
- this.destroy();
58
57
  this.createPeer(true);
59
58
  }
60
59
  // reject('peer open timeout');
@@ -86,7 +85,7 @@ class PeerTransfer {
86
85
  this.reconnect();
87
86
  }, constant_1.CONNECTION_RETRY);
88
87
  });
89
- peer.on('error', (err) => {
88
+ peer.on('error', (err) => __awaiter(this, void 0, void 0, function* () {
90
89
  if (err.message === constant_1.CONNECTION_PEER_CONFLICT) {
91
90
  this.stop();
92
91
  }
@@ -95,28 +94,30 @@ class PeerTransfer {
95
94
  this.onConnectLimit();
96
95
  }
97
96
  if (err.message == constant_1.UNAUTHORIZED) {
98
- this.stop();
99
- this.requireAuth();
97
+ yield this.requireAuth();
100
98
  }
101
99
  if (err.type === 'network') {
102
- this.destroy();
103
100
  this.reconnect();
104
101
  }
105
102
  if (err.type === 'server-error') {
106
103
  if (err.message === constant_1.UNAUTHORIZED) {
107
- return this.requireAuth();
104
+ yield this.requireAuth();
105
+ }
106
+ else {
107
+ this.reconnect();
108
108
  }
109
- this.destroy();
110
- this.reconnect();
111
109
  }
112
- });
110
+ }));
113
111
  const socket = peer.socket;
114
112
  socket === null || socket === void 0 ? void 0 : socket.on('message', (message) => {
115
113
  this.handleSocketMessage(message);
116
114
  });
115
+ socket === null || socket === void 0 ? void 0 : socket.on('error', (err) => {
116
+ console.log('socket@error', err);
117
+ });
117
118
  socket.on('close', () => {
119
+ console.log('socket@close');
118
120
  setTimeout(() => {
119
- this.destroy();
120
121
  this.reconnect();
121
122
  }, constant_1.CONNECTION_RETRY);
122
123
  });
@@ -145,9 +146,6 @@ class PeerTransfer {
145
146
  if (this.isPaused) {
146
147
  return;
147
148
  }
148
- if (this.timeoutId) {
149
- clearTimeout(this.timeoutId);
150
- }
151
149
  }
152
150
  onError(message) {
153
151
  var _a;
@@ -164,7 +162,6 @@ class PeerTransfer {
164
162
  message: 'peer conflict',
165
163
  },
166
164
  });
167
- this.destroy();
168
165
  return;
169
166
  }
170
167
  this.reconnect();
@@ -210,7 +207,20 @@ class PeerTransfer {
210
207
  }
211
208
  }
212
209
  checkConnection() {
213
- this.checkerId = setInterval(() => {
210
+ if (this.checkerId) {
211
+ return;
212
+ }
213
+ this.checkerId = setInterval(() => __awaiter(this, void 0, void 0, function* () {
214
+ var _a;
215
+ if (!((_a = this.peer) === null || _a === void 0 ? void 0 : _a.open)) {
216
+ yield this.reconnect();
217
+ }
218
+ // turn credential expires
219
+ if (this.peerConfig.turnExpiredAt && Date.now() - 30000 > this.peerConfig.turnExpiredAt) {
220
+ yield this.createPeer(true);
221
+ this.connectPool.clear();
222
+ return;
223
+ }
214
224
  const deviceIds = this.connectPool.keys();
215
225
  for (const deviceId of deviceIds) {
216
226
  const conn = this.connectPool.get(deviceId);
@@ -218,7 +228,7 @@ class PeerTransfer {
218
228
  this.ping(conn.peer);
219
229
  }
220
230
  }
221
- }, constant_1.HEARTBEAT_INTERVAL);
231
+ }), constant_1.HEARTBEAT_INTERVAL);
222
232
  }
223
233
  getApiHost() {
224
234
  var _a, _b, _c;
@@ -425,7 +435,7 @@ class PeerTransfer {
425
435
  clearTimeout(timeout);
426
436
  const now = Date.now();
427
437
  if (now - message.createdAt > 3000) {
428
- console.log('[egos] slow request', message.route, now - message.createdAt);
438
+ console.warn('[egos] slow peer request:', message.route, now - message.createdAt);
429
439
  }
430
440
  resolve(message);
431
441
  });
@@ -476,12 +486,11 @@ class PeerTransfer {
476
486
  if (this.isPaused) {
477
487
  return;
478
488
  }
479
- console.log('peer reconnecting...');
480
489
  if (this.reconnectingPromise) {
481
490
  return yield this.reconnectingPromise;
482
491
  }
483
492
  this.reconnectingPromise = this.doReconnect();
484
- return this.reconnectingPromise.finally(() => {
493
+ return yield this.reconnectingPromise.finally(() => {
485
494
  this.reconnectingPromise = undefined;
486
495
  });
487
496
  });
@@ -496,19 +505,21 @@ class PeerTransfer {
496
505
  if ((_a = this.peer) === null || _a === void 0 ? void 0 : _a.open) {
497
506
  return this.peer;
498
507
  }
508
+ console.log('reconnecting~~', Date.now());
499
509
  if ((_b = this.peer) === null || _b === void 0 ? void 0 : _b.disconnected) {
500
510
  this.peer.reconnect();
501
511
  return;
502
512
  }
503
513
  else {
504
- setTimeout(() => {
505
- this.createPeer();
506
- }, constant_1.RECONNECT_TIMEOUT);
507
- return;
514
+ yield new Promise((resolve) => {
515
+ setTimeout(() => {
516
+ resolve(undefined);
517
+ }, constant_1.RECONNECT_TIMEOUT);
518
+ });
519
+ return this.createPeer();
508
520
  }
509
521
  }
510
522
  catch (err) {
511
- this.destroy();
512
523
  return this.createPeer();
513
524
  }
514
525
  });
@@ -530,6 +541,7 @@ class PeerTransfer {
530
541
  return this.peer;
531
542
  }
532
543
  this.destroy();
544
+ yield this.reloadConfig();
533
545
  const peer = new peerjs_1.Peer(this.peerId, Object.assign(Object.assign({}, this.peerConfig), { pingInterval: 15000 }));
534
546
  this.peer = peer;
535
547
  if (this.checkerId) {
@@ -550,7 +562,6 @@ class PeerTransfer {
550
562
  (_a = this.peer) === null || _a === void 0 ? void 0 : _a.destroy();
551
563
  (_b = this.peer) === null || _b === void 0 ? void 0 : _b.disconnect();
552
564
  this.peerPool.clear();
553
- this.timeoutId = undefined;
554
565
  this.booted = undefined;
555
566
  this.peer = undefined;
556
567
  if (this.checkerId) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "egos-transfer",
3
- "version": "0.1.0",
3
+ "version": "0.1.3",
4
4
  "description": "1234",
5
5
  "homepage": "https://github.com/superbogy/peer-transfer#readme",
6
6
  "bugs": {
package/src/constant.ts CHANGED
@@ -1,8 +1,8 @@
1
1
  export const REQUEST_TIMEOUT = 18000;
2
- export const HEARTBEAT_INTERVAL = 15000;
2
+ export const HEARTBEAT_INTERVAL = 12000;
3
3
  export const TRANSFER_TIMEOUT = 300000;
4
4
  export const PING_TIMEOUT = 5000;
5
- export const CONNECTION_TIMEOUT = 6000;
5
+ export const CONNECTION_TIMEOUT = 12000;
6
6
  export const LOCKER_TIMEOUT = 15000;
7
7
  export const RECONNECT_TIMEOUT = 5000;
8
8
  export const PEER_OPEN_TIMEOUT = 15000;
package/src/index.ts CHANGED
@@ -19,10 +19,10 @@ import {
19
19
  RouteItem,
20
20
  } from './interfaces';
21
21
  import { InternalApi, PeerAction, PeerRoutes, PeerScope, REQUEST_TIMEOUT } from './entity';
22
- import { isRandomPeerId, purifyDeviceId } from './helper';
23
22
 
24
23
  import { PeerRouter } from './router';
25
24
  import axios from 'axios';
25
+ import { purifyDeviceId } from './helper';
26
26
 
27
27
  export interface ContentPayload {
28
28
  file: string;
@@ -30,6 +30,9 @@ export interface ContentPayload {
30
30
  }
31
31
 
32
32
  type ResponseCb<T = any> = (args: T) => void;
33
+ type PeerTransferOptions = PeerOptions & {
34
+ turnExpiredAt: number; // date time ms
35
+ };
33
36
 
34
37
  export abstract class PeerTransfer {
35
38
  protected peer: Peer | undefined;
@@ -49,19 +52,18 @@ export abstract class PeerTransfer {
49
52
 
50
53
  protected retry = 0;
51
54
 
52
- protected peerConfig: PeerOptions | undefined;
55
+ protected peerConfig: PeerTransferOptions | undefined;
53
56
  public deviceId: string;
54
57
  protected requests: Map<string, ResponseCb>;
55
58
  protected timeout: number;
56
59
  protected router: PeerRouter;
57
60
  protected booted: Promise<string> | undefined;
58
61
  protected reconnectingPromise: Promise<Peer | undefined> | undefined;
59
- protected timeoutId!: ReturnType<typeof setTimeout>;
60
62
  protected checkerId!: ReturnType<typeof setInterval>;
61
63
  public isPaused: boolean;
62
64
  peerId: string;
63
65
 
64
- constructor(deviceId: string, config: PeerOptions, isRadomPeerId = false) {
66
+ constructor(deviceId: string, config: PeerTransferOptions, isRadomPeerId = false) {
65
67
  this.connectPool = new Map<string, DataConnection>();
66
68
  this.peerConfig = {
67
69
  ...config,
@@ -83,14 +85,14 @@ export abstract class PeerTransfer {
83
85
  abstract getApi(deviceId: string): Promise<InternalApi | undefined>;
84
86
  abstract discoverDeviceById(deviceId: string): Promise<void>;
85
87
  abstract onDestroy(): void;
86
- abstract requireAuth(): void;
88
+ abstract requireAuth(): Promise<void>;
87
89
  abstract onConnectLimit(): void;
90
+ abstract reloadConfig(config?: Record<string, any>): Promise<void>;
88
91
  async initialize(peer: Peer) {
89
92
  try {
90
93
  this.booted = new Promise((resolve) => {
91
94
  const timer = setTimeout(() => {
92
95
  if (!this.peer?.open) {
93
- this.destroy();
94
96
  this.createPeer(true);
95
97
  }
96
98
  // reject('peer open timeout');
@@ -122,7 +124,7 @@ export abstract class PeerTransfer {
122
124
  this.reconnect();
123
125
  }, CONNECTION_RETRY);
124
126
  });
125
- peer.on('error', (err) => {
127
+ peer.on('error', async (err) => {
126
128
  if (err.message === CONNECTION_PEER_CONFLICT) {
127
129
  this.stop();
128
130
  }
@@ -131,28 +133,29 @@ export abstract class PeerTransfer {
131
133
  this.onConnectLimit();
132
134
  }
133
135
  if (err.message == UNAUTHORIZED) {
134
- this.stop();
135
- this.requireAuth();
136
+ await this.requireAuth();
136
137
  }
137
138
  if (err.type === 'network') {
138
- this.destroy();
139
139
  this.reconnect();
140
140
  }
141
141
  if (err.type === 'server-error') {
142
142
  if (err.message === UNAUTHORIZED) {
143
- return this.requireAuth();
143
+ await this.requireAuth();
144
+ } else {
145
+ this.reconnect();
144
146
  }
145
- this.destroy();
146
- this.reconnect();
147
147
  }
148
148
  });
149
149
  const socket = peer.socket;
150
150
  socket?.on('message', (message: IMessage) => {
151
151
  this.handleSocketMessage(message);
152
152
  });
153
+ socket?.on('error', (err) => {
154
+ console.log('socket@error', err);
155
+ });
153
156
  socket.on('close', () => {
157
+ console.log('socket@close');
154
158
  setTimeout(() => {
155
- this.destroy();
156
159
  this.reconnect();
157
160
  }, CONNECTION_RETRY);
158
161
  });
@@ -182,9 +185,6 @@ export abstract class PeerTransfer {
182
185
  if (this.isPaused) {
183
186
  return;
184
187
  }
185
- if (this.timeoutId) {
186
- clearTimeout(this.timeoutId);
187
- }
188
188
  }
189
189
 
190
190
  onError(message: IMessage) {
@@ -201,7 +201,6 @@ export abstract class PeerTransfer {
201
201
  message: 'peer conflict',
202
202
  },
203
203
  });
204
- this.destroy();
205
204
  return;
206
205
  }
207
206
  this.reconnect();
@@ -250,7 +249,19 @@ export abstract class PeerTransfer {
250
249
  }
251
250
 
252
251
  checkConnection() {
253
- this.checkerId = setInterval(() => {
252
+ if (this.checkerId) {
253
+ return;
254
+ }
255
+ this.checkerId = setInterval(async () => {
256
+ if (!this.peer?.open) {
257
+ await this.reconnect();
258
+ }
259
+ // turn credential expires
260
+ if (this.peerConfig.turnExpiredAt && Date.now() - 30000 > this.peerConfig.turnExpiredAt) {
261
+ await this.createPeer(true);
262
+ this.connectPool.clear();
263
+ return;
264
+ }
254
265
  const deviceIds = this.connectPool.keys();
255
266
  for (const deviceId of deviceIds) {
256
267
  const conn = this.connectPool.get(deviceId);
@@ -458,7 +469,7 @@ export abstract class PeerTransfer {
458
469
  clearTimeout(timeout);
459
470
  const now = Date.now();
460
471
  if (now - message.createdAt > 3000) {
461
- console.log('[egos] slow request', message.route, now - message.createdAt);
472
+ console.warn('[egos] slow peer request:', message.route, now - message.createdAt);
462
473
  }
463
474
  resolve(message);
464
475
  });
@@ -541,13 +552,12 @@ export abstract class PeerTransfer {
541
552
  if (this.isPaused) {
542
553
  return;
543
554
  }
544
- console.log('peer reconnecting...');
545
555
  if (this.reconnectingPromise) {
546
556
  return await this.reconnectingPromise;
547
557
  }
548
558
 
549
559
  this.reconnectingPromise = this.doReconnect();
550
- return this.reconnectingPromise.finally(() => {
560
+ return await this.reconnectingPromise.finally(() => {
551
561
  this.reconnectingPromise = undefined;
552
562
  });
553
563
  }
@@ -560,17 +570,20 @@ export abstract class PeerTransfer {
560
570
  if (this.peer?.open) {
561
571
  return this.peer;
562
572
  }
573
+ console.log('reconnecting~~', Date.now());
563
574
  if (this.peer?.disconnected) {
564
575
  this.peer.reconnect();
565
576
  return;
566
577
  } else {
567
- setTimeout(() => {
568
- this.createPeer();
569
- }, RECONNECT_TIMEOUT);
570
- return;
578
+ await new Promise((resolve) => {
579
+ setTimeout(() => {
580
+ resolve(undefined);
581
+ }, RECONNECT_TIMEOUT);
582
+ });
583
+
584
+ return this.createPeer();
571
585
  }
572
586
  } catch (err) {
573
- this.destroy();
574
587
  return this.createPeer();
575
588
  }
576
589
  }
@@ -591,6 +604,7 @@ export abstract class PeerTransfer {
591
604
  }
592
605
 
593
606
  this.destroy();
607
+ await this.reloadConfig();
594
608
  const peer = new Peer(this.peerId, {
595
609
  ...this.peerConfig,
596
610
  pingInterval: 15000,
@@ -613,7 +627,6 @@ export abstract class PeerTransfer {
613
627
  this.peer?.destroy();
614
628
  this.peer?.disconnect();
615
629
  this.peerPool.clear();
616
- this.timeoutId = undefined;
617
630
  this.booted = undefined;
618
631
  this.peer = undefined;
619
632
  if (this.checkerId) {