egos-transfer 0.1.3 → 0.1.4

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.
@@ -302,6 +302,7 @@ class FileTransfer {
302
302
  }
303
303
  if (processed.length === totalChunk) {
304
304
  const checksum = yield this.taskService.verifyFile(task.transId);
305
+ console.log('file verify', checksum);
305
306
  if (checksum) {
306
307
  yield this.done(task);
307
308
  yield this.updateTask(task.transId, {
package/dist/index.d.ts CHANGED
@@ -55,6 +55,7 @@ export declare abstract class PeerTransfer {
55
55
  getApiHost(): string;
56
56
  getDeviceStatus(peerId: string): Promise<IDeviceStatus | undefined>;
57
57
  connectToPeer(deviceId: string): Promise<DataConnection | undefined>;
58
+ private createConnection;
58
59
  ping(peerId: string): Promise<boolean>;
59
60
  onConnect(conn: DataConnection): Promise<DataConnection | undefined>;
60
61
  request(peerId: string, payload: PeerRequestPayload): Promise<PeerMessage>;
package/dist/index.js CHANGED
@@ -270,69 +270,63 @@ class PeerTransfer {
270
270
  peerId = status.deviceId;
271
271
  this.peerIdMap.set(pdId, peerId);
272
272
  }
273
- if (this.connectPool.has(peerId)) {
274
- const conn = this.connectPool.get(peerId);
275
- if (conn === null || conn === void 0 ? void 0 : conn.open) {
276
- return conn;
277
- }
273
+ const existingConn = this.connectPool.get(peerId);
274
+ if (existingConn === null || existingConn === void 0 ? void 0 : existingConn.open) {
275
+ return existingConn;
278
276
  }
279
- // Check if there's already a connection attempt for this peerId
280
277
  if (PeerTransfer.connecting.has(peerId)) {
281
- // Wait for the existing connection attempt to complete
282
- const { promise } = yield PeerTransfer.connecting.get(peerId);
283
- return yield promise;
278
+ const entry = PeerTransfer.connecting.get(peerId);
279
+ const conn = yield (entry === null || entry === void 0 ? void 0 : entry.promise);
280
+ if (conn) {
281
+ return conn;
282
+ }
284
283
  }
285
- // Create a new connection promise and store it in connecting map
286
- let resolve, reject;
284
+ return this.createConnection(peerId);
285
+ });
286
+ }
287
+ createConnection(peerId) {
288
+ return __awaiter(this, void 0, void 0, function* () {
289
+ var _a, _b;
290
+ let resolve;
291
+ let reject;
287
292
  const connectionPromise = new Promise((res, rej) => {
288
293
  resolve = res;
289
294
  reject = rej;
290
295
  });
291
- // Store the promise in connecting map
292
296
  PeerTransfer.connecting.set(peerId, { promise: connectionPromise, resolve, reject });
293
- const buildConnFn = () => __awaiter(this, void 0, void 0, function* () {
294
- var _a, _b;
295
- try {
296
- let retry = 0;
297
- while (retry < 3) {
298
- if (!this.peer) {
299
- yield this.createPeer();
300
- }
301
- if (!((_a = this.peer) === null || _a === void 0 ? void 0 : _a.open)) {
302
- yield this.reconnect();
303
- }
304
- const cur = this.connectPool.get(peerId);
305
- if (cur === null || cur === void 0 ? void 0 : cur.open) {
306
- this.online(peerId);
307
- resolve(cur);
308
- return cur;
309
- }
310
- const conn = (_b = this.peer) === null || _b === void 0 ? void 0 : _b.connect(peerId, { reliable: true });
311
- if (!conn) {
312
- retry++;
313
- continue;
314
- }
315
- const connection = yield this.onConnect(conn).catch((err) => { });
316
- if (connection) {
317
- this.online(peerId);
318
- return resolve(conn);
319
- }
320
- retry++;
297
+ try {
298
+ for (let retry = 0; retry < 3; retry++) {
299
+ if (!this.peer) {
300
+ yield this.createPeer();
301
+ }
302
+ if (!((_a = this.peer) === null || _a === void 0 ? void 0 : _a.open)) {
303
+ yield this.reconnect();
304
+ }
305
+ const cur = this.connectPool.get(peerId);
306
+ if (cur === null || cur === void 0 ? void 0 : cur.open) {
307
+ resolve(cur);
308
+ return cur;
309
+ }
310
+ const conn = (_b = this.peer) === null || _b === void 0 ? void 0 : _b.connect(peerId);
311
+ if (!conn) {
312
+ continue;
313
+ }
314
+ const connection = yield this.onConnect(conn).catch(() => { });
315
+ if (connection) {
316
+ resolve(conn);
317
+ return conn;
321
318
  }
322
- resolve(null);
323
- }
324
- catch (err) {
325
- reject(err);
326
- }
327
- finally {
328
- // Remove from connecting map regardless of success or failure
329
- PeerTransfer.connecting.delete(peerId);
330
319
  }
331
- });
332
- buildConnFn();
333
- // Wait for the connection to complete
334
- const conn = yield connectionPromise;
335
- return conn;
320
+ resolve(undefined);
321
+ return undefined;
322
+ }
323
+ catch (err) {
324
+ reject(err);
325
+ throw err;
326
+ }
327
+ finally {
328
+ PeerTransfer.connecting.delete(peerId);
329
+ }
336
330
  });
337
331
  }
338
332
  ping(peerId) {
@@ -385,8 +379,10 @@ class PeerTransfer {
385
379
  conn.on('data', (message) => {
386
380
  switch (message.scope) {
387
381
  case entity_1.PeerScope.REQUEST:
382
+ // console.log('peer@onRequest', message.route);
388
383
  return this.onRequest(conn, message);
389
384
  case entity_1.PeerScope.RESPONSE:
385
+ // console.log('peer@onResponse', message.route);
390
386
  return this.onResponse(conn, message);
391
387
  default:
392
388
  return this.responseFail(conn, message, 'unknown scope');
@@ -418,6 +414,7 @@ class PeerTransfer {
418
414
  request(peerId, payload) {
419
415
  return __awaiter(this, void 0, void 0, function* () {
420
416
  const conn = yield this.connectToPeer(peerId);
417
+ // console.log('connected@peerId', peerId, payload.route);
421
418
  if (!conn) {
422
419
  return Object.assign(Object.assign({}, payload), { body: {}, error: { message: 'build connection failed', code: -1 } });
423
420
  }
@@ -505,7 +502,7 @@ class PeerTransfer {
505
502
  if ((_a = this.peer) === null || _a === void 0 ? void 0 : _a.open) {
506
503
  return this.peer;
507
504
  }
508
- console.log('reconnecting~~', Date.now());
505
+ console.log('peer reconnecting....', Date.now());
509
506
  if ((_b = this.peer) === null || _b === void 0 ? void 0 : _b.disconnected) {
510
507
  this.peer.reconnect();
511
508
  return;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "egos-transfer",
3
- "version": "0.1.3",
3
+ "version": "0.1.4",
4
4
  "description": "1234",
5
5
  "homepage": "https://github.com/superbogy/peer-transfer#readme",
6
6
  "bugs": {
@@ -346,6 +346,7 @@ export class FileTransfer {
346
346
  }
347
347
  if (processed.length === totalChunk) {
348
348
  const checksum = await this.taskService.verifyFile(task.transId);
349
+ console.log('file verify', checksum);
349
350
  if (checksum) {
350
351
  await this.done(task);
351
352
  await this.updateTask(task.transId, {
package/src/index.ts CHANGED
@@ -295,8 +295,10 @@ export abstract class PeerTransfer {
295
295
  if (deviceId === this.deviceId) {
296
296
  return;
297
297
  }
298
+
298
299
  let peerId = deviceId;
299
300
  const pdId = purifyDeviceId(deviceId);
301
+
300
302
  if (this.peerIdMap.has(pdId)) {
301
303
  peerId = this.peerIdMap.get(pdId) || '';
302
304
  } else {
@@ -307,68 +309,68 @@ export abstract class PeerTransfer {
307
309
  peerId = status.deviceId;
308
310
  this.peerIdMap.set(pdId, peerId);
309
311
  }
310
- if (this.connectPool.has(peerId)) {
311
- const conn = this.connectPool.get(peerId);
312
- if (conn?.open) {
313
- return conn;
314
- }
312
+
313
+ const existingConn = this.connectPool.get(peerId);
314
+ if (existingConn?.open) {
315
+ return existingConn;
315
316
  }
316
- // Check if there's already a connection attempt for this peerId
317
+
317
318
  if (PeerTransfer.connecting.has(peerId)) {
318
- // Wait for the existing connection attempt to complete
319
- const { promise } = await PeerTransfer.connecting.get(peerId);
320
- return await promise;
319
+ const entry = PeerTransfer.connecting.get(peerId);
320
+ const conn = await entry?.promise;
321
+ if (conn) {
322
+ return conn;
323
+ }
321
324
  }
322
325
 
323
- // Create a new connection promise and store it in connecting map
324
- let resolve, reject;
326
+ return this.createConnection(peerId);
327
+ }
328
+
329
+ private async createConnection(peerId: string): Promise<DataConnection | undefined> {
330
+ let resolve!: (value: DataConnection | undefined) => void;
331
+ let reject!: (reason?: any) => void;
332
+
325
333
  const connectionPromise = new Promise<DataConnection | undefined>((res, rej) => {
326
334
  resolve = res;
327
335
  reject = rej;
328
336
  });
329
- // Store the promise in connecting map
337
+
330
338
  PeerTransfer.connecting.set(peerId, { promise: connectionPromise, resolve, reject });
331
- const buildConnFn = async () => {
332
- try {
333
- let retry = 0;
334
- while (retry < 3) {
335
- if (!this.peer) {
336
- await this.createPeer();
337
- }
338
- if (!this.peer?.open) {
339
- await this.reconnect();
340
- }
341
- const cur = this.connectPool.get(peerId);
342
- if (cur?.open) {
343
- this.online(peerId);
344
- resolve(cur);
345
- return cur;
346
- }
347
- const conn = this.peer?.connect(peerId, { reliable: true });
348
- if (!conn) {
349
- retry++;
350
- continue;
351
- }
352
- const connection = await this.onConnect(conn as DataConnection).catch((err) => {});
353
- if (connection) {
354
- this.online(peerId);
355
- return resolve(conn);
356
- }
357
- retry++;
339
+
340
+ try {
341
+ for (let retry = 0; retry < 3; retry++) {
342
+ if (!this.peer) {
343
+ await this.createPeer();
344
+ }
345
+ if (!this.peer?.open) {
346
+ await this.reconnect();
358
347
  }
359
- resolve(null);
360
- } catch (err) {
361
- reject(err);
362
- } finally {
363
- // Remove from connecting map regardless of success or failure
364
- PeerTransfer.connecting.delete(peerId);
365
- }
366
- };
367
348
 
368
- buildConnFn();
369
- // Wait for the connection to complete
370
- const conn = await connectionPromise;
371
- return conn;
349
+ const cur = this.connectPool.get(peerId);
350
+ if (cur?.open) {
351
+ resolve(cur);
352
+ return cur;
353
+ }
354
+
355
+ const conn = this.peer?.connect(peerId);
356
+ if (!conn) {
357
+ continue;
358
+ }
359
+
360
+ const connection = await this.onConnect(conn as DataConnection).catch(() => {});
361
+ if (connection) {
362
+ resolve(conn);
363
+ return conn;
364
+ }
365
+ }
366
+ resolve(undefined);
367
+ return undefined;
368
+ } catch (err) {
369
+ reject(err);
370
+ throw err;
371
+ } finally {
372
+ PeerTransfer.connecting.delete(peerId);
373
+ }
372
374
  }
373
375
 
374
376
  async ping(peerId: string) {
@@ -418,8 +420,10 @@ export abstract class PeerTransfer {
418
420
  conn.on('data', (message: any) => {
419
421
  switch (message.scope) {
420
422
  case PeerScope.REQUEST:
423
+ // console.log('peer@onRequest', message.route);
421
424
  return this.onRequest(conn, message);
422
425
  case PeerScope.RESPONSE:
426
+ // console.log('peer@onResponse', message.route);
423
427
  return this.onResponse(conn, message);
424
428
  default:
425
429
  return this.responseFail(conn, message, 'unknown scope');
@@ -448,6 +452,7 @@ export abstract class PeerTransfer {
448
452
 
449
453
  async request(peerId: string, payload: PeerRequestPayload): Promise<PeerMessage> {
450
454
  const conn = await this.connectToPeer(peerId);
455
+ // console.log('connected@peerId', peerId, payload.route);
451
456
  if (!conn) {
452
457
  return {
453
458
  ...payload,
@@ -570,7 +575,7 @@ export abstract class PeerTransfer {
570
575
  if (this.peer?.open) {
571
576
  return this.peer;
572
577
  }
573
- console.log('reconnecting~~', Date.now());
578
+ console.log('peer reconnecting....', Date.now());
574
579
  if (this.peer?.disconnected) {
575
580
  this.peer.reconnect();
576
581
  return;