egos-transfer 0.1.8 → 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);
@@ -248,7 +251,7 @@ class PeerTransfer {
248
251
  this.removeConnection(conn.peer);
249
252
  }
250
253
  else {
251
- this.ping(conn.peer);
254
+ // this.ping(conn.peer);
252
255
  }
253
256
  }
254
257
  }), constant_1.HEARTBEAT_INTERVAL);
@@ -282,7 +285,7 @@ class PeerTransfer {
282
285
  }
283
286
  const peerId = deviceId;
284
287
  const existingConn = this.connectPool.get(peerId);
285
- // console.log('connectToPeer', peerId, existingConn, this.connectPool);
288
+ console.log('connectToPeer', existingConn === null || existingConn === void 0 ? void 0 : existingConn.open, this.connectPool.size);
286
289
  if (existingConn) {
287
290
  if (this.isValidConnection(existingConn)) {
288
291
  return existingConn;
@@ -303,49 +306,56 @@ class PeerTransfer {
303
306
  }
304
307
  createConnection(peerId) {
305
308
  return __awaiter(this, void 0, void 0, function* () {
306
- var _a, _b;
307
309
  let resolve;
308
310
  let reject;
311
+ const q = PeerTransfer.connecting.get(peerId);
312
+ if (q) {
313
+ return q.promise;
314
+ }
309
315
  const connectionPromise = new Promise((res, rej) => {
310
316
  resolve = res;
311
317
  reject = rej;
312
318
  });
313
319
  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;
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
+ }
338
345
  }
346
+ resolve(undefined);
347
+ return undefined;
339
348
  }
340
- resolve(undefined);
341
- return undefined;
342
- }
343
- catch (err) {
344
- reject(err);
345
- }
346
- finally {
347
- PeerTransfer.connecting.delete(peerId);
348
- }
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;
349
359
  });
350
360
  }
351
361
  ping(peerId) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "egos-transfer",
3
- "version": "0.1.8",
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);
@@ -291,7 +295,7 @@ export abstract class PeerTransfer {
291
295
  if (!this.isValidConnection(conn)) {
292
296
  this.removeConnection(conn.peer);
293
297
  } else {
294
- this.ping(conn.peer);
298
+ // this.ping(conn.peer);
295
299
  }
296
300
  }
297
301
  }, HEARTBEAT_INTERVAL);
@@ -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) {