egos-transfer 0.1.0 → 0.1.1
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/constant.d.ts +1 -1
- package/dist/constant.js +1 -1
- package/dist/index.d.ts +1 -2
- package/dist/index.js +30 -26
- package/package.json +1 -1
- package/src/constant.ts +1 -1
- package/src/index.ts +28 -26
package/dist/constant.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export declare const REQUEST_TIMEOUT = 18000;
|
|
2
|
-
export declare const HEARTBEAT_INTERVAL =
|
|
2
|
+
export declare const HEARTBEAT_INTERVAL = 12000;
|
|
3
3
|
export declare const TRANSFER_TIMEOUT = 300000;
|
|
4
4
|
export declare const PING_TIMEOUT = 5000;
|
|
5
5
|
export declare const CONNECTION_TIMEOUT = 6000;
|
package/dist/constant.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.UNAUTHORIZED = exports.CONNECTION_DUPLICATED = exports.CONNECTION_LIMIT_EXCEED = exports.CONNECTION_PEER_CONFLICT = exports.CONNECTION_RETRY = exports.PEER_OPEN_TIMEOUT = exports.RECONNECT_TIMEOUT = exports.LOCKER_TIMEOUT = exports.CONNECTION_TIMEOUT = exports.PING_TIMEOUT = exports.TRANSFER_TIMEOUT = exports.HEARTBEAT_INTERVAL = exports.REQUEST_TIMEOUT = void 0;
|
|
4
4
|
exports.REQUEST_TIMEOUT = 18000;
|
|
5
|
-
exports.HEARTBEAT_INTERVAL =
|
|
5
|
+
exports.HEARTBEAT_INTERVAL = 12000;
|
|
6
6
|
exports.TRANSFER_TIMEOUT = 300000;
|
|
7
7
|
exports.PING_TIMEOUT = 5000;
|
|
8
8
|
exports.CONNECTION_TIMEOUT = 6000;
|
package/dist/index.d.ts
CHANGED
|
@@ -25,7 +25,6 @@ export declare abstract class PeerTransfer {
|
|
|
25
25
|
protected router: PeerRouter;
|
|
26
26
|
protected booted: Promise<string> | undefined;
|
|
27
27
|
protected reconnectingPromise: Promise<Peer | undefined> | undefined;
|
|
28
|
-
protected timeoutId: ReturnType<typeof setTimeout>;
|
|
29
28
|
protected checkerId: ReturnType<typeof setInterval>;
|
|
30
29
|
isPaused: boolean;
|
|
31
30
|
peerId: string;
|
|
@@ -37,7 +36,7 @@ export declare abstract class PeerTransfer {
|
|
|
37
36
|
abstract getApi(deviceId: string): Promise<InternalApi | undefined>;
|
|
38
37
|
abstract discoverDeviceById(deviceId: string): Promise<void>;
|
|
39
38
|
abstract onDestroy(): void;
|
|
40
|
-
abstract requireAuth(): void
|
|
39
|
+
abstract requireAuth(): Promise<void>;
|
|
41
40
|
abstract onConnectLimit(): void;
|
|
42
41
|
initialize(peer: Peer): Promise<string>;
|
|
43
42
|
handleSocketMessage(message: IMessage): void;
|
package/dist/index.js
CHANGED
|
@@ -30,9 +30,9 @@ exports.PeerTransfer = void 0;
|
|
|
30
30
|
const constant_1 = require("./constant");
|
|
31
31
|
const peerjs_1 = require("peerjs");
|
|
32
32
|
const entity_1 = require("./entity");
|
|
33
|
-
const helper_1 = require("./helper");
|
|
34
33
|
const router_1 = require("./router");
|
|
35
34
|
const axios_1 = __importDefault(require("axios"));
|
|
35
|
+
const helper_1 = require("./helper");
|
|
36
36
|
class PeerTransfer {
|
|
37
37
|
constructor(deviceId, config, isRadomPeerId = false) {
|
|
38
38
|
this.peerIdMap = new Map();
|
|
@@ -54,7 +54,6 @@ class PeerTransfer {
|
|
|
54
54
|
const timer = setTimeout(() => {
|
|
55
55
|
var _a;
|
|
56
56
|
if (!((_a = this.peer) === null || _a === void 0 ? void 0 : _a.open)) {
|
|
57
|
-
this.destroy();
|
|
58
57
|
this.createPeer(true);
|
|
59
58
|
}
|
|
60
59
|
// reject('peer open timeout');
|
|
@@ -86,7 +85,7 @@ class PeerTransfer {
|
|
|
86
85
|
this.reconnect();
|
|
87
86
|
}, constant_1.CONNECTION_RETRY);
|
|
88
87
|
});
|
|
89
|
-
peer.on('error', (err) => {
|
|
88
|
+
peer.on('error', (err) => __awaiter(this, void 0, void 0, function* () {
|
|
90
89
|
if (err.message === constant_1.CONNECTION_PEER_CONFLICT) {
|
|
91
90
|
this.stop();
|
|
92
91
|
}
|
|
@@ -95,28 +94,30 @@ class PeerTransfer {
|
|
|
95
94
|
this.onConnectLimit();
|
|
96
95
|
}
|
|
97
96
|
if (err.message == constant_1.UNAUTHORIZED) {
|
|
98
|
-
this.
|
|
99
|
-
this.requireAuth();
|
|
97
|
+
yield this.requireAuth();
|
|
100
98
|
}
|
|
101
99
|
if (err.type === 'network') {
|
|
102
|
-
this.destroy();
|
|
103
100
|
this.reconnect();
|
|
104
101
|
}
|
|
105
102
|
if (err.type === 'server-error') {
|
|
106
103
|
if (err.message === constant_1.UNAUTHORIZED) {
|
|
107
|
-
|
|
104
|
+
yield this.requireAuth();
|
|
105
|
+
}
|
|
106
|
+
else {
|
|
107
|
+
this.reconnect();
|
|
108
108
|
}
|
|
109
|
-
this.destroy();
|
|
110
|
-
this.reconnect();
|
|
111
109
|
}
|
|
112
|
-
});
|
|
110
|
+
}));
|
|
113
111
|
const socket = peer.socket;
|
|
114
112
|
socket === null || socket === void 0 ? void 0 : socket.on('message', (message) => {
|
|
115
113
|
this.handleSocketMessage(message);
|
|
116
114
|
});
|
|
115
|
+
socket === null || socket === void 0 ? void 0 : socket.on('error', (err) => {
|
|
116
|
+
console.log('socket@error', err);
|
|
117
|
+
});
|
|
117
118
|
socket.on('close', () => {
|
|
119
|
+
console.log('socket@close');
|
|
118
120
|
setTimeout(() => {
|
|
119
|
-
this.destroy();
|
|
120
121
|
this.reconnect();
|
|
121
122
|
}, constant_1.CONNECTION_RETRY);
|
|
122
123
|
});
|
|
@@ -145,9 +146,6 @@ class PeerTransfer {
|
|
|
145
146
|
if (this.isPaused) {
|
|
146
147
|
return;
|
|
147
148
|
}
|
|
148
|
-
if (this.timeoutId) {
|
|
149
|
-
clearTimeout(this.timeoutId);
|
|
150
|
-
}
|
|
151
149
|
}
|
|
152
150
|
onError(message) {
|
|
153
151
|
var _a;
|
|
@@ -164,7 +162,6 @@ class PeerTransfer {
|
|
|
164
162
|
message: 'peer conflict',
|
|
165
163
|
},
|
|
166
164
|
});
|
|
167
|
-
this.destroy();
|
|
168
165
|
return;
|
|
169
166
|
}
|
|
170
167
|
this.reconnect();
|
|
@@ -210,7 +207,14 @@ class PeerTransfer {
|
|
|
210
207
|
}
|
|
211
208
|
}
|
|
212
209
|
checkConnection() {
|
|
213
|
-
this.checkerId
|
|
210
|
+
if (this.checkerId) {
|
|
211
|
+
return;
|
|
212
|
+
}
|
|
213
|
+
this.checkerId = setInterval(() => __awaiter(this, void 0, void 0, function* () {
|
|
214
|
+
var _a;
|
|
215
|
+
if (!((_a = this.peer) === null || _a === void 0 ? void 0 : _a.open)) {
|
|
216
|
+
yield this.reconnect();
|
|
217
|
+
}
|
|
214
218
|
const deviceIds = this.connectPool.keys();
|
|
215
219
|
for (const deviceId of deviceIds) {
|
|
216
220
|
const conn = this.connectPool.get(deviceId);
|
|
@@ -218,7 +222,7 @@ class PeerTransfer {
|
|
|
218
222
|
this.ping(conn.peer);
|
|
219
223
|
}
|
|
220
224
|
}
|
|
221
|
-
}, constant_1.HEARTBEAT_INTERVAL);
|
|
225
|
+
}), constant_1.HEARTBEAT_INTERVAL);
|
|
222
226
|
}
|
|
223
227
|
getApiHost() {
|
|
224
228
|
var _a, _b, _c;
|
|
@@ -425,7 +429,7 @@ class PeerTransfer {
|
|
|
425
429
|
clearTimeout(timeout);
|
|
426
430
|
const now = Date.now();
|
|
427
431
|
if (now - message.createdAt > 3000) {
|
|
428
|
-
console.
|
|
432
|
+
console.warn('[egos] slow peer request:', message.route, now - message.createdAt);
|
|
429
433
|
}
|
|
430
434
|
resolve(message);
|
|
431
435
|
});
|
|
@@ -476,12 +480,11 @@ class PeerTransfer {
|
|
|
476
480
|
if (this.isPaused) {
|
|
477
481
|
return;
|
|
478
482
|
}
|
|
479
|
-
console.log('peer reconnecting...');
|
|
480
483
|
if (this.reconnectingPromise) {
|
|
481
484
|
return yield this.reconnectingPromise;
|
|
482
485
|
}
|
|
483
486
|
this.reconnectingPromise = this.doReconnect();
|
|
484
|
-
return this.reconnectingPromise.finally(() => {
|
|
487
|
+
return yield this.reconnectingPromise.finally(() => {
|
|
485
488
|
this.reconnectingPromise = undefined;
|
|
486
489
|
});
|
|
487
490
|
});
|
|
@@ -496,19 +499,21 @@ class PeerTransfer {
|
|
|
496
499
|
if ((_a = this.peer) === null || _a === void 0 ? void 0 : _a.open) {
|
|
497
500
|
return this.peer;
|
|
498
501
|
}
|
|
502
|
+
console.log('reconnecting~~', Date.now());
|
|
499
503
|
if ((_b = this.peer) === null || _b === void 0 ? void 0 : _b.disconnected) {
|
|
500
504
|
this.peer.reconnect();
|
|
501
505
|
return;
|
|
502
506
|
}
|
|
503
507
|
else {
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
+
yield new Promise((resolve) => {
|
|
509
|
+
setTimeout(() => {
|
|
510
|
+
resolve(undefined);
|
|
511
|
+
}, constant_1.RECONNECT_TIMEOUT);
|
|
512
|
+
});
|
|
513
|
+
return this.createPeer();
|
|
508
514
|
}
|
|
509
515
|
}
|
|
510
516
|
catch (err) {
|
|
511
|
-
this.destroy();
|
|
512
517
|
return this.createPeer();
|
|
513
518
|
}
|
|
514
519
|
});
|
|
@@ -550,7 +555,6 @@ class PeerTransfer {
|
|
|
550
555
|
(_a = this.peer) === null || _a === void 0 ? void 0 : _a.destroy();
|
|
551
556
|
(_b = this.peer) === null || _b === void 0 ? void 0 : _b.disconnect();
|
|
552
557
|
this.peerPool.clear();
|
|
553
|
-
this.timeoutId = undefined;
|
|
554
558
|
this.booted = undefined;
|
|
555
559
|
this.peer = undefined;
|
|
556
560
|
if (this.checkerId) {
|
package/package.json
CHANGED
package/src/constant.ts
CHANGED
package/src/index.ts
CHANGED
|
@@ -19,10 +19,10 @@ import {
|
|
|
19
19
|
RouteItem,
|
|
20
20
|
} from './interfaces';
|
|
21
21
|
import { InternalApi, PeerAction, PeerRoutes, PeerScope, REQUEST_TIMEOUT } from './entity';
|
|
22
|
-
import { isRandomPeerId, purifyDeviceId } from './helper';
|
|
23
22
|
|
|
24
23
|
import { PeerRouter } from './router';
|
|
25
24
|
import axios from 'axios';
|
|
25
|
+
import { purifyDeviceId } from './helper';
|
|
26
26
|
|
|
27
27
|
export interface ContentPayload {
|
|
28
28
|
file: string;
|
|
@@ -56,7 +56,6 @@ export abstract class PeerTransfer {
|
|
|
56
56
|
protected router: PeerRouter;
|
|
57
57
|
protected booted: Promise<string> | undefined;
|
|
58
58
|
protected reconnectingPromise: Promise<Peer | undefined> | undefined;
|
|
59
|
-
protected timeoutId!: ReturnType<typeof setTimeout>;
|
|
60
59
|
protected checkerId!: ReturnType<typeof setInterval>;
|
|
61
60
|
public isPaused: boolean;
|
|
62
61
|
peerId: string;
|
|
@@ -83,14 +82,13 @@ export abstract class PeerTransfer {
|
|
|
83
82
|
abstract getApi(deviceId: string): Promise<InternalApi | undefined>;
|
|
84
83
|
abstract discoverDeviceById(deviceId: string): Promise<void>;
|
|
85
84
|
abstract onDestroy(): void;
|
|
86
|
-
abstract requireAuth(): void
|
|
85
|
+
abstract requireAuth(): Promise<void>;
|
|
87
86
|
abstract onConnectLimit(): void;
|
|
88
87
|
async initialize(peer: Peer) {
|
|
89
88
|
try {
|
|
90
89
|
this.booted = new Promise((resolve) => {
|
|
91
90
|
const timer = setTimeout(() => {
|
|
92
91
|
if (!this.peer?.open) {
|
|
93
|
-
this.destroy();
|
|
94
92
|
this.createPeer(true);
|
|
95
93
|
}
|
|
96
94
|
// reject('peer open timeout');
|
|
@@ -122,7 +120,7 @@ export abstract class PeerTransfer {
|
|
|
122
120
|
this.reconnect();
|
|
123
121
|
}, CONNECTION_RETRY);
|
|
124
122
|
});
|
|
125
|
-
peer.on('error', (err) => {
|
|
123
|
+
peer.on('error', async (err) => {
|
|
126
124
|
if (err.message === CONNECTION_PEER_CONFLICT) {
|
|
127
125
|
this.stop();
|
|
128
126
|
}
|
|
@@ -131,28 +129,29 @@ export abstract class PeerTransfer {
|
|
|
131
129
|
this.onConnectLimit();
|
|
132
130
|
}
|
|
133
131
|
if (err.message == UNAUTHORIZED) {
|
|
134
|
-
this.
|
|
135
|
-
this.requireAuth();
|
|
132
|
+
await this.requireAuth();
|
|
136
133
|
}
|
|
137
134
|
if (err.type === 'network') {
|
|
138
|
-
this.destroy();
|
|
139
135
|
this.reconnect();
|
|
140
136
|
}
|
|
141
137
|
if (err.type === 'server-error') {
|
|
142
138
|
if (err.message === UNAUTHORIZED) {
|
|
143
|
-
|
|
139
|
+
await this.requireAuth();
|
|
140
|
+
} else {
|
|
141
|
+
this.reconnect();
|
|
144
142
|
}
|
|
145
|
-
this.destroy();
|
|
146
|
-
this.reconnect();
|
|
147
143
|
}
|
|
148
144
|
});
|
|
149
145
|
const socket = peer.socket;
|
|
150
146
|
socket?.on('message', (message: IMessage) => {
|
|
151
147
|
this.handleSocketMessage(message);
|
|
152
148
|
});
|
|
149
|
+
socket?.on('error', (err) => {
|
|
150
|
+
console.log('socket@error', err);
|
|
151
|
+
});
|
|
153
152
|
socket.on('close', () => {
|
|
153
|
+
console.log('socket@close');
|
|
154
154
|
setTimeout(() => {
|
|
155
|
-
this.destroy();
|
|
156
155
|
this.reconnect();
|
|
157
156
|
}, CONNECTION_RETRY);
|
|
158
157
|
});
|
|
@@ -182,9 +181,6 @@ export abstract class PeerTransfer {
|
|
|
182
181
|
if (this.isPaused) {
|
|
183
182
|
return;
|
|
184
183
|
}
|
|
185
|
-
if (this.timeoutId) {
|
|
186
|
-
clearTimeout(this.timeoutId);
|
|
187
|
-
}
|
|
188
184
|
}
|
|
189
185
|
|
|
190
186
|
onError(message: IMessage) {
|
|
@@ -201,7 +197,6 @@ export abstract class PeerTransfer {
|
|
|
201
197
|
message: 'peer conflict',
|
|
202
198
|
},
|
|
203
199
|
});
|
|
204
|
-
this.destroy();
|
|
205
200
|
return;
|
|
206
201
|
}
|
|
207
202
|
this.reconnect();
|
|
@@ -250,7 +245,13 @@ export abstract class PeerTransfer {
|
|
|
250
245
|
}
|
|
251
246
|
|
|
252
247
|
checkConnection() {
|
|
253
|
-
this.checkerId
|
|
248
|
+
if (this.checkerId) {
|
|
249
|
+
return;
|
|
250
|
+
}
|
|
251
|
+
this.checkerId = setInterval(async () => {
|
|
252
|
+
if (!this.peer?.open) {
|
|
253
|
+
await this.reconnect();
|
|
254
|
+
}
|
|
254
255
|
const deviceIds = this.connectPool.keys();
|
|
255
256
|
for (const deviceId of deviceIds) {
|
|
256
257
|
const conn = this.connectPool.get(deviceId);
|
|
@@ -458,7 +459,7 @@ export abstract class PeerTransfer {
|
|
|
458
459
|
clearTimeout(timeout);
|
|
459
460
|
const now = Date.now();
|
|
460
461
|
if (now - message.createdAt > 3000) {
|
|
461
|
-
console.
|
|
462
|
+
console.warn('[egos] slow peer request:', message.route, now - message.createdAt);
|
|
462
463
|
}
|
|
463
464
|
resolve(message);
|
|
464
465
|
});
|
|
@@ -541,13 +542,12 @@ export abstract class PeerTransfer {
|
|
|
541
542
|
if (this.isPaused) {
|
|
542
543
|
return;
|
|
543
544
|
}
|
|
544
|
-
console.log('peer reconnecting...');
|
|
545
545
|
if (this.reconnectingPromise) {
|
|
546
546
|
return await this.reconnectingPromise;
|
|
547
547
|
}
|
|
548
548
|
|
|
549
549
|
this.reconnectingPromise = this.doReconnect();
|
|
550
|
-
return this.reconnectingPromise.finally(() => {
|
|
550
|
+
return await this.reconnectingPromise.finally(() => {
|
|
551
551
|
this.reconnectingPromise = undefined;
|
|
552
552
|
});
|
|
553
553
|
}
|
|
@@ -560,17 +560,20 @@ export abstract class PeerTransfer {
|
|
|
560
560
|
if (this.peer?.open) {
|
|
561
561
|
return this.peer;
|
|
562
562
|
}
|
|
563
|
+
console.log('reconnecting~~', Date.now());
|
|
563
564
|
if (this.peer?.disconnected) {
|
|
564
565
|
this.peer.reconnect();
|
|
565
566
|
return;
|
|
566
567
|
} else {
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
568
|
+
await new Promise((resolve) => {
|
|
569
|
+
setTimeout(() => {
|
|
570
|
+
resolve(undefined);
|
|
571
|
+
}, RECONNECT_TIMEOUT);
|
|
572
|
+
});
|
|
573
|
+
|
|
574
|
+
return this.createPeer();
|
|
571
575
|
}
|
|
572
576
|
} catch (err) {
|
|
573
|
-
this.destroy();
|
|
574
577
|
return this.createPeer();
|
|
575
578
|
}
|
|
576
579
|
}
|
|
@@ -613,7 +616,6 @@ export abstract class PeerTransfer {
|
|
|
613
616
|
this.peer?.destroy();
|
|
614
617
|
this.peer?.disconnect();
|
|
615
618
|
this.peerPool.clear();
|
|
616
|
-
this.timeoutId = undefined;
|
|
617
619
|
this.booted = undefined;
|
|
618
620
|
this.peer = undefined;
|
|
619
621
|
if (this.checkerId) {
|