egos-transfer 0.1.1 → 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.
@@ -2,7 +2,7 @@ export declare const REQUEST_TIMEOUT = 18000;
2
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
@@ -5,7 +5,7 @@ exports.REQUEST_TIMEOUT = 18000;
5
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;
@@ -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
@@ -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,7 +21,7 @@ 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;
@@ -28,7 +31,7 @@ export declare abstract class PeerTransfer {
28
31
  protected checkerId: ReturnType<typeof setInterval>;
29
32
  isPaused: boolean;
30
33
  peerId: string;
31
- constructor(deviceId: string, config: PeerOptions, isRadomPeerId?: boolean);
34
+ constructor(deviceId: string, config: PeerTransferOptions, isRadomPeerId?: boolean);
32
35
  abstract getStore(): any;
33
36
  abstract online(deviceId: string): void;
34
37
  abstract offline(deviceId: string): void;
@@ -38,6 +41,7 @@ export declare abstract class PeerTransfer {
38
41
  abstract onDestroy(): void;
39
42
  abstract requireAuth(): Promise<void>;
40
43
  abstract onConnectLimit(): void;
44
+ abstract reloadConfig(config?: Record<string, any>): Promise<void>;
41
45
  initialize(peer: Peer): Promise<string>;
42
46
  handleSocketMessage(message: IMessage): void;
43
47
  onHeartbeat(message: IMessage): void;
@@ -51,6 +55,7 @@ export declare abstract class PeerTransfer {
51
55
  getApiHost(): string;
52
56
  getDeviceStatus(peerId: string): Promise<IDeviceStatus | undefined>;
53
57
  connectToPeer(deviceId: string): Promise<DataConnection | undefined>;
58
+ private createConnection;
54
59
  ping(peerId: string): Promise<boolean>;
55
60
  onConnect(conn: DataConnection): Promise<DataConnection | undefined>;
56
61
  request(peerId: string, payload: PeerRequestPayload): Promise<PeerMessage>;
package/dist/index.js CHANGED
@@ -215,6 +215,12 @@ class PeerTransfer {
215
215
  if (!((_a = this.peer) === null || _a === void 0 ? void 0 : _a.open)) {
216
216
  yield this.reconnect();
217
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
+ }
218
224
  const deviceIds = this.connectPool.keys();
219
225
  for (const deviceId of deviceIds) {
220
226
  const conn = this.connectPool.get(deviceId);
@@ -264,69 +270,63 @@ class PeerTransfer {
264
270
  peerId = status.deviceId;
265
271
  this.peerIdMap.set(pdId, peerId);
266
272
  }
267
- if (this.connectPool.has(peerId)) {
268
- const conn = this.connectPool.get(peerId);
269
- if (conn === null || conn === void 0 ? void 0 : conn.open) {
270
- return conn;
271
- }
273
+ const existingConn = this.connectPool.get(peerId);
274
+ if (existingConn === null || existingConn === void 0 ? void 0 : existingConn.open) {
275
+ return existingConn;
272
276
  }
273
- // Check if there's already a connection attempt for this peerId
274
277
  if (PeerTransfer.connecting.has(peerId)) {
275
- // Wait for the existing connection attempt to complete
276
- const { promise } = yield PeerTransfer.connecting.get(peerId);
277
- 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
+ }
278
283
  }
279
- // Create a new connection promise and store it in connecting map
280
- 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;
281
292
  const connectionPromise = new Promise((res, rej) => {
282
293
  resolve = res;
283
294
  reject = rej;
284
295
  });
285
- // Store the promise in connecting map
286
296
  PeerTransfer.connecting.set(peerId, { promise: connectionPromise, resolve, reject });
287
- const buildConnFn = () => __awaiter(this, void 0, void 0, function* () {
288
- var _a, _b;
289
- try {
290
- let retry = 0;
291
- while (retry < 3) {
292
- if (!this.peer) {
293
- yield this.createPeer();
294
- }
295
- if (!((_a = this.peer) === null || _a === void 0 ? void 0 : _a.open)) {
296
- yield this.reconnect();
297
- }
298
- const cur = this.connectPool.get(peerId);
299
- if (cur === null || cur === void 0 ? void 0 : cur.open) {
300
- this.online(peerId);
301
- resolve(cur);
302
- return cur;
303
- }
304
- const conn = (_b = this.peer) === null || _b === void 0 ? void 0 : _b.connect(peerId, { reliable: true });
305
- if (!conn) {
306
- retry++;
307
- continue;
308
- }
309
- const connection = yield this.onConnect(conn).catch((err) => { });
310
- if (connection) {
311
- this.online(peerId);
312
- return resolve(conn);
313
- }
314
- 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;
315
318
  }
316
- resolve(null);
317
- }
318
- catch (err) {
319
- reject(err);
320
- }
321
- finally {
322
- // Remove from connecting map regardless of success or failure
323
- PeerTransfer.connecting.delete(peerId);
324
319
  }
325
- });
326
- buildConnFn();
327
- // Wait for the connection to complete
328
- const conn = yield connectionPromise;
329
- 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
+ }
330
330
  });
331
331
  }
332
332
  ping(peerId) {
@@ -379,8 +379,10 @@ class PeerTransfer {
379
379
  conn.on('data', (message) => {
380
380
  switch (message.scope) {
381
381
  case entity_1.PeerScope.REQUEST:
382
+ // console.log('peer@onRequest', message.route);
382
383
  return this.onRequest(conn, message);
383
384
  case entity_1.PeerScope.RESPONSE:
385
+ // console.log('peer@onResponse', message.route);
384
386
  return this.onResponse(conn, message);
385
387
  default:
386
388
  return this.responseFail(conn, message, 'unknown scope');
@@ -412,6 +414,7 @@ class PeerTransfer {
412
414
  request(peerId, payload) {
413
415
  return __awaiter(this, void 0, void 0, function* () {
414
416
  const conn = yield this.connectToPeer(peerId);
417
+ // console.log('connected@peerId', peerId, payload.route);
415
418
  if (!conn) {
416
419
  return Object.assign(Object.assign({}, payload), { body: {}, error: { message: 'build connection failed', code: -1 } });
417
420
  }
@@ -499,7 +502,7 @@ class PeerTransfer {
499
502
  if ((_a = this.peer) === null || _a === void 0 ? void 0 : _a.open) {
500
503
  return this.peer;
501
504
  }
502
- console.log('reconnecting~~', Date.now());
505
+ console.log('peer reconnecting....', Date.now());
503
506
  if ((_b = this.peer) === null || _b === void 0 ? void 0 : _b.disconnected) {
504
507
  this.peer.reconnect();
505
508
  return;
@@ -535,6 +538,7 @@ class PeerTransfer {
535
538
  return this.peer;
536
539
  }
537
540
  this.destroy();
541
+ yield this.reloadConfig();
538
542
  const peer = new peerjs_1.Peer(this.peerId, Object.assign(Object.assign({}, this.peerConfig), { pingInterval: 15000 }));
539
543
  this.peer = peer;
540
544
  if (this.checkerId) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "egos-transfer",
3
- "version": "0.1.1",
3
+ "version": "0.1.4",
4
4
  "description": "1234",
5
5
  "homepage": "https://github.com/superbogy/peer-transfer#readme",
6
6
  "bugs": {
package/src/constant.ts CHANGED
@@ -2,7 +2,7 @@ export const REQUEST_TIMEOUT = 18000;
2
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;
@@ -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
@@ -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,7 +52,7 @@ 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;
@@ -60,7 +63,7 @@ export abstract class PeerTransfer {
60
63
  public isPaused: boolean;
61
64
  peerId: string;
62
65
 
63
- constructor(deviceId: string, config: PeerOptions, isRadomPeerId = false) {
66
+ constructor(deviceId: string, config: PeerTransferOptions, isRadomPeerId = false) {
64
67
  this.connectPool = new Map<string, DataConnection>();
65
68
  this.peerConfig = {
66
69
  ...config,
@@ -84,6 +87,7 @@ export abstract class PeerTransfer {
84
87
  abstract onDestroy(): void;
85
88
  abstract requireAuth(): Promise<void>;
86
89
  abstract onConnectLimit(): void;
90
+ abstract reloadConfig(config?: Record<string, any>): Promise<void>;
87
91
  async initialize(peer: Peer) {
88
92
  try {
89
93
  this.booted = new Promise((resolve) => {
@@ -252,6 +256,12 @@ export abstract class PeerTransfer {
252
256
  if (!this.peer?.open) {
253
257
  await this.reconnect();
254
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
+ }
255
265
  const deviceIds = this.connectPool.keys();
256
266
  for (const deviceId of deviceIds) {
257
267
  const conn = this.connectPool.get(deviceId);
@@ -285,8 +295,10 @@ export abstract class PeerTransfer {
285
295
  if (deviceId === this.deviceId) {
286
296
  return;
287
297
  }
298
+
288
299
  let peerId = deviceId;
289
300
  const pdId = purifyDeviceId(deviceId);
301
+
290
302
  if (this.peerIdMap.has(pdId)) {
291
303
  peerId = this.peerIdMap.get(pdId) || '';
292
304
  } else {
@@ -297,68 +309,68 @@ export abstract class PeerTransfer {
297
309
  peerId = status.deviceId;
298
310
  this.peerIdMap.set(pdId, peerId);
299
311
  }
300
- if (this.connectPool.has(peerId)) {
301
- const conn = this.connectPool.get(peerId);
302
- if (conn?.open) {
303
- return conn;
304
- }
312
+
313
+ const existingConn = this.connectPool.get(peerId);
314
+ if (existingConn?.open) {
315
+ return existingConn;
305
316
  }
306
- // Check if there's already a connection attempt for this peerId
317
+
307
318
  if (PeerTransfer.connecting.has(peerId)) {
308
- // Wait for the existing connection attempt to complete
309
- const { promise } = await PeerTransfer.connecting.get(peerId);
310
- return await promise;
319
+ const entry = PeerTransfer.connecting.get(peerId);
320
+ const conn = await entry?.promise;
321
+ if (conn) {
322
+ return conn;
323
+ }
311
324
  }
312
325
 
313
- // Create a new connection promise and store it in connecting map
314
- 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
+
315
333
  const connectionPromise = new Promise<DataConnection | undefined>((res, rej) => {
316
334
  resolve = res;
317
335
  reject = rej;
318
336
  });
319
- // Store the promise in connecting map
337
+
320
338
  PeerTransfer.connecting.set(peerId, { promise: connectionPromise, resolve, reject });
321
- const buildConnFn = async () => {
322
- try {
323
- let retry = 0;
324
- while (retry < 3) {
325
- if (!this.peer) {
326
- await this.createPeer();
327
- }
328
- if (!this.peer?.open) {
329
- await this.reconnect();
330
- }
331
- const cur = this.connectPool.get(peerId);
332
- if (cur?.open) {
333
- this.online(peerId);
334
- resolve(cur);
335
- return cur;
336
- }
337
- const conn = this.peer?.connect(peerId, { reliable: true });
338
- if (!conn) {
339
- retry++;
340
- continue;
341
- }
342
- const connection = await this.onConnect(conn as DataConnection).catch((err) => {});
343
- if (connection) {
344
- this.online(peerId);
345
- return resolve(conn);
346
- }
347
- 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();
348
347
  }
349
- resolve(null);
350
- } catch (err) {
351
- reject(err);
352
- } finally {
353
- // Remove from connecting map regardless of success or failure
354
- PeerTransfer.connecting.delete(peerId);
355
- }
356
- };
357
348
 
358
- buildConnFn();
359
- // Wait for the connection to complete
360
- const conn = await connectionPromise;
361
- 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
+ }
362
374
  }
363
375
 
364
376
  async ping(peerId: string) {
@@ -408,8 +420,10 @@ export abstract class PeerTransfer {
408
420
  conn.on('data', (message: any) => {
409
421
  switch (message.scope) {
410
422
  case PeerScope.REQUEST:
423
+ // console.log('peer@onRequest', message.route);
411
424
  return this.onRequest(conn, message);
412
425
  case PeerScope.RESPONSE:
426
+ // console.log('peer@onResponse', message.route);
413
427
  return this.onResponse(conn, message);
414
428
  default:
415
429
  return this.responseFail(conn, message, 'unknown scope');
@@ -438,6 +452,7 @@ export abstract class PeerTransfer {
438
452
 
439
453
  async request(peerId: string, payload: PeerRequestPayload): Promise<PeerMessage> {
440
454
  const conn = await this.connectToPeer(peerId);
455
+ // console.log('connected@peerId', peerId, payload.route);
441
456
  if (!conn) {
442
457
  return {
443
458
  ...payload,
@@ -560,7 +575,7 @@ export abstract class PeerTransfer {
560
575
  if (this.peer?.open) {
561
576
  return this.peer;
562
577
  }
563
- console.log('reconnecting~~', Date.now());
578
+ console.log('peer reconnecting....', Date.now());
564
579
  if (this.peer?.disconnected) {
565
580
  this.peer.reconnect();
566
581
  return;
@@ -594,6 +609,7 @@ export abstract class PeerTransfer {
594
609
  }
595
610
 
596
611
  this.destroy();
612
+ await this.reloadConfig();
597
613
  const peer = new Peer(this.peerId, {
598
614
  ...this.peerConfig,
599
615
  pingInterval: 15000,