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 +1 -1
- package/dist/index.js +47 -37
- package/package.json +1 -1
- package/src/index.ts +45 -35
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<
|
|
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:
|
|
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
|
-
|
|
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
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
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
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
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
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:
|
|
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
|
-
|
|
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
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
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
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
}
|
|
373
|
+
const cur = this.connectPool.get(peerId);
|
|
374
|
+
if (this.isValidConnection(cur)) {
|
|
375
|
+
return resolve(cur);
|
|
376
|
+
}
|
|
370
377
|
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
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
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
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) {
|