egos-transfer 0.1.7 → 0.1.9

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
@@ -59,7 +59,7 @@ class PeerTransfer {
59
59
  }
60
60
  this.destroy();
61
61
  yield this.reloadConfig();
62
- const peer = new peerjs_1.Peer(this.peerId, Object.assign(Object.assign({}, this.peerConfig), { pingInterval: 15000 }));
62
+ const peer = new peerjs_1.Peer(this.peerId, Object.assign(Object.assign({}, this.peerConfig), { pingInterval: 9000 }));
63
63
  this.peer = peer;
64
64
  yield this.initialize(this.peer);
65
65
  this.checkConnection();
@@ -72,6 +72,9 @@ class PeerTransfer {
72
72
  if (this.checkerId) {
73
73
  clearInterval(this.checkerId);
74
74
  }
75
+ if (this.booted) {
76
+ return this.booted;
77
+ }
75
78
  this.booted = new Promise((resolve) => {
76
79
  const timer = setTimeout(() => {
77
80
  resolve(null);
@@ -119,7 +122,7 @@ class PeerTransfer {
119
122
  return this.reconnect();
120
123
  }
121
124
  done(null);
122
- return this.reconnect();
125
+ return this.createPeer(true);
123
126
  }));
124
127
  });
125
128
  peer.on('connection', (conn) => {
@@ -227,7 +230,7 @@ class PeerTransfer {
227
230
  }
228
231
  checkConnection() {
229
232
  if (this.checkerId) {
230
- return;
233
+ clearInterval(this.checkerId);
231
234
  }
232
235
  this.checkerId = setInterval(() => __awaiter(this, void 0, void 0, function* () {
233
236
  var _a;
@@ -235,21 +238,22 @@ class PeerTransfer {
235
238
  yield this.reconnect();
236
239
  }
237
240
  // turn credential expires
238
- if (this.peerConfig.turnExpiredAt && Date.now() - 30000 > this.peerConfig.turnExpiredAt) {
241
+ if (this.peerConfig.turnExpiredAt && this.peerConfig.turnExpiredAt - 30000 < Date.now()) {
239
242
  // this.connectPool.clear();
243
+ yield this.reloadConfig();
240
244
  yield this.createPeer(true);
241
245
  return;
242
246
  }
243
- // const deviceIds = this.connectPool.keys();
244
- // for (const deviceId of deviceIds) {
245
- // const conn = this.connectPool.get(deviceId);
246
- // if (!this.isValidConnection(conn)) {
247
- // this.removeConnection(conn.peer);
248
- // this.peer?._removeConnection(conn);
249
- // } else {
250
- // this.ping(conn.peer);
251
- // }
252
- // }
247
+ const deviceIds = this.connectPool.keys();
248
+ for (const deviceId of deviceIds) {
249
+ const conn = this.connectPool.get(deviceId);
250
+ if (!this.isValidConnection(conn)) {
251
+ this.removeConnection(conn.peer);
252
+ }
253
+ else {
254
+ // this.ping(conn.peer);
255
+ }
256
+ }
253
257
  }), constant_1.HEARTBEAT_INTERVAL);
254
258
  }
255
259
  getApiHost() {
@@ -281,7 +285,7 @@ class PeerTransfer {
281
285
  }
282
286
  const peerId = deviceId;
283
287
  const existingConn = this.connectPool.get(peerId);
284
- // console.log('connectToPeer', peerId, existingConn, this.connectPool);
288
+ console.log('connectToPeer', existingConn === null || existingConn === void 0 ? void 0 : existingConn.open, this.connectPool.size);
285
289
  if (existingConn) {
286
290
  if (this.isValidConnection(existingConn)) {
287
291
  return existingConn;
@@ -302,49 +306,56 @@ class PeerTransfer {
302
306
  }
303
307
  createConnection(peerId) {
304
308
  return __awaiter(this, void 0, void 0, function* () {
305
- var _a, _b;
306
309
  let resolve;
307
310
  let reject;
311
+ const q = PeerTransfer.connecting.get(peerId);
312
+ if (q) {
313
+ return q.promise;
314
+ }
308
315
  const connectionPromise = new Promise((res, rej) => {
309
316
  resolve = res;
310
317
  reject = rej;
311
318
  });
312
319
  PeerTransfer.connecting.set(peerId, { promise: connectionPromise, resolve, reject });
313
- try {
314
- for (let retry = 0; retry < 3; retry++) {
315
- if (!this.peer) {
316
- yield this.createPeer();
317
- }
318
- if (!((_a = this.peer) === null || _a === void 0 ? void 0 : _a.open)) {
319
- yield this.reconnect();
320
- }
321
- const cur = this.connectPool.get(peerId);
322
- if (this.isValidConnection(cur)) {
323
- resolve(cur);
324
- return cur;
325
- }
326
- const conn = (_b = this.peer) === null || _b === void 0 ? void 0 : _b.connect(peerId, {
327
- label: 'outgoing',
328
- metadata: { time: Date.now() },
329
- });
330
- if (!conn) {
331
- continue;
332
- }
333
- const connection = yield this.onConnect(conn).catch(() => { });
334
- if (connection) {
335
- resolve(conn);
336
- return conn;
320
+ const doConnect = () => __awaiter(this, void 0, void 0, function* () {
321
+ var _a, _b;
322
+ try {
323
+ for (let retry = 0; retry < 3; retry++) {
324
+ if (!this.peer) {
325
+ yield this.createPeer();
326
+ }
327
+ if (!((_a = this.peer) === null || _a === void 0 ? void 0 : _a.open)) {
328
+ yield this.reconnect();
329
+ }
330
+ const cur = this.connectPool.get(peerId);
331
+ if (this.isValidConnection(cur)) {
332
+ return resolve(cur);
333
+ }
334
+ const conn = (_b = this.peer) === null || _b === void 0 ? void 0 : _b.connect(peerId, {
335
+ label: 'outgoing',
336
+ metadata: { time: Date.now() },
337
+ });
338
+ if (!conn) {
339
+ continue;
340
+ }
341
+ const connection = yield this.onConnect(conn).catch(() => { });
342
+ if (connection) {
343
+ return resolve(connection);
344
+ }
337
345
  }
346
+ resolve(undefined);
347
+ return undefined;
338
348
  }
339
- resolve(undefined);
340
- return undefined;
341
- }
342
- catch (err) {
343
- reject(err);
344
- }
345
- finally {
346
- PeerTransfer.connecting.delete(peerId);
347
- }
349
+ catch (err) {
350
+ reject(err);
351
+ }
352
+ finally {
353
+ PeerTransfer.connecting.delete(peerId);
354
+ }
355
+ });
356
+ doConnect();
357
+ const conn = yield connectionPromise;
358
+ return conn;
348
359
  });
349
360
  }
350
361
  ping(peerId) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "egos-transfer",
3
- "version": "0.1.7",
3
+ "version": "0.1.9",
4
4
  "description": "1234",
5
5
  "homepage": "https://github.com/superbogy/peer-transfer#readme",
6
6
  "bugs": {
package/src/index.ts CHANGED
@@ -101,7 +101,7 @@ export abstract class PeerTransfer {
101
101
  await this.reloadConfig();
102
102
  const peer = new Peer(this.peerId, {
103
103
  ...this.peerConfig,
104
- pingInterval: 15000,
104
+ pingInterval: 9000,
105
105
  });
106
106
  this.peer = peer;
107
107
  await this.initialize(this.peer);
@@ -114,6 +114,10 @@ export abstract class PeerTransfer {
114
114
  clearInterval(this.checkerId);
115
115
  }
116
116
 
117
+ if (this.booted) {
118
+ return this.booted;
119
+ }
120
+
117
121
  this.booted = new Promise((resolve) => {
118
122
  const timer = setTimeout(() => {
119
123
  resolve(null);
@@ -161,7 +165,7 @@ export abstract class PeerTransfer {
161
165
  return this.reconnect();
162
166
  }
163
167
  done(null);
164
- return this.reconnect();
168
+ return this.createPeer(true);
165
169
  });
166
170
  });
167
171
  peer.on('connection', (conn: DataConnection) => {
@@ -272,28 +276,28 @@ export abstract class PeerTransfer {
272
276
 
273
277
  checkConnection() {
274
278
  if (this.checkerId) {
275
- return;
279
+ clearInterval(this.checkerId);
276
280
  }
277
281
  this.checkerId = setInterval(async () => {
278
282
  if (!this.peer?.open) {
279
283
  await this.reconnect();
280
284
  }
281
285
  // turn credential expires
282
- if (this.peerConfig.turnExpiredAt && Date.now() - 30000 > this.peerConfig.turnExpiredAt) {
286
+ if (this.peerConfig.turnExpiredAt && this.peerConfig.turnExpiredAt - 30000 < Date.now()) {
283
287
  // this.connectPool.clear();
288
+ await this.reloadConfig();
284
289
  await this.createPeer(true);
285
290
  return;
286
291
  }
287
- // const deviceIds = this.connectPool.keys();
288
- // for (const deviceId of deviceIds) {
289
- // const conn = this.connectPool.get(deviceId);
290
- // if (!this.isValidConnection(conn)) {
291
- // this.removeConnection(conn.peer);
292
- // this.peer?._removeConnection(conn);
293
- // } else {
294
- // this.ping(conn.peer);
295
- // }
296
- // }
292
+ const deviceIds = this.connectPool.keys();
293
+ for (const deviceId of deviceIds) {
294
+ const conn = this.connectPool.get(deviceId);
295
+ if (!this.isValidConnection(conn)) {
296
+ this.removeConnection(conn.peer);
297
+ } else {
298
+ // this.ping(conn.peer);
299
+ }
300
+ }
297
301
  }, HEARTBEAT_INTERVAL);
298
302
  }
299
303
 
@@ -323,7 +327,7 @@ export abstract class PeerTransfer {
323
327
 
324
328
  const peerId = deviceId;
325
329
  const existingConn = this.connectPool.get(peerId);
326
- // console.log('connectToPeer', peerId, existingConn, this.connectPool);
330
+ console.log('connectToPeer', existingConn?.open, this.connectPool.size);
327
331
  if (existingConn) {
328
332
  if (this.isValidConnection(existingConn)) {
329
333
  return existingConn;
@@ -346,48 +350,54 @@ export abstract class PeerTransfer {
346
350
  private async createConnection(peerId: string): Promise<DataConnection | undefined> {
347
351
  let resolve!: (value: DataConnection | undefined) => void;
348
352
  let reject!: (reason?: any) => void;
349
-
353
+ const q = PeerTransfer.connecting.get(peerId);
354
+ if (q) {
355
+ return q.promise;
356
+ }
350
357
  const connectionPromise = new Promise<DataConnection | undefined>((res, rej) => {
351
358
  resolve = res;
352
359
  reject = rej;
353
360
  });
354
361
 
355
362
  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
- }
363
+ const doConnect = async () => {
364
+ try {
365
+ for (let retry = 0; retry < 3; retry++) {
366
+ if (!this.peer) {
367
+ await this.createPeer();
368
+ }
369
+ if (!this.peer?.open) {
370
+ await this.reconnect();
371
+ }
364
372
 
365
- const cur = this.connectPool.get(peerId);
366
- if (this.isValidConnection(cur)) {
367
- resolve(cur);
368
- return cur;
369
- }
373
+ const cur = this.connectPool.get(peerId);
374
+ if (this.isValidConnection(cur)) {
375
+ return resolve(cur);
376
+ }
370
377
 
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;
378
+ const conn = this.peer?.connect(peerId, {
379
+ label: 'outgoing',
380
+ metadata: { time: Date.now() },
381
+ });
382
+ if (!conn) {
383
+ continue;
384
+ }
385
+ const connection = await this.onConnect(conn as DataConnection).catch(() => {});
386
+ if (connection) {
387
+ return resolve(connection);
388
+ }
382
389
  }
390
+ resolve(undefined);
391
+ return undefined;
392
+ } catch (err) {
393
+ reject(err);
394
+ } finally {
395
+ PeerTransfer.connecting.delete(peerId);
383
396
  }
384
- resolve(undefined);
385
- return undefined;
386
- } catch (err) {
387
- reject(err);
388
- } finally {
389
- PeerTransfer.connecting.delete(peerId);
390
- }
397
+ };
398
+ doConnect();
399
+ const conn = await connectionPromise;
400
+ return conn;
391
401
  }
392
402
 
393
403
  async ping(peerId: string) {