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.
- package/dist/file-transfer.js +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +51 -54
- package/package.json +1 -1
- package/src/file-transfer.ts +1 -0
- package/src/index.ts +57 -52
package/dist/file-transfer.js
CHANGED
|
@@ -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
|
-
|
|
274
|
-
|
|
275
|
-
|
|
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
|
-
|
|
282
|
-
const
|
|
283
|
-
|
|
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
|
-
|
|
286
|
-
|
|
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
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
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
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
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
|
|
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
package/src/file-transfer.ts
CHANGED
|
@@ -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
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
}
|
|
312
|
+
|
|
313
|
+
const existingConn = this.connectPool.get(peerId);
|
|
314
|
+
if (existingConn?.open) {
|
|
315
|
+
return existingConn;
|
|
315
316
|
}
|
|
316
|
-
|
|
317
|
+
|
|
317
318
|
if (PeerTransfer.connecting.has(peerId)) {
|
|
318
|
-
|
|
319
|
-
const
|
|
320
|
-
|
|
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
|
-
|
|
324
|
-
|
|
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
|
-
|
|
337
|
+
|
|
330
338
|
PeerTransfer.connecting.set(peerId, { promise: connectionPromise, resolve, reject });
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
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
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
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
|
|
578
|
+
console.log('peer reconnecting....', Date.now());
|
|
574
579
|
if (this.peer?.disconnected) {
|
|
575
580
|
this.peer.reconnect();
|
|
576
581
|
return;
|