egos-transfer 0.0.10 → 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 +37 -30
- package/package.json +1 -1
- package/src/constant.ts +1 -1
- package/src/index.ts +35 -30
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');
|
|
@@ -83,12 +82,10 @@ class PeerTransfer {
|
|
|
83
82
|
return;
|
|
84
83
|
}
|
|
85
84
|
setTimeout(() => {
|
|
86
|
-
|
|
87
|
-
this.reconnect();
|
|
88
|
-
}
|
|
85
|
+
this.reconnect();
|
|
89
86
|
}, constant_1.CONNECTION_RETRY);
|
|
90
87
|
});
|
|
91
|
-
peer.on('error', (err) => {
|
|
88
|
+
peer.on('error', (err) => __awaiter(this, void 0, void 0, function* () {
|
|
92
89
|
if (err.message === constant_1.CONNECTION_PEER_CONFLICT) {
|
|
93
90
|
this.stop();
|
|
94
91
|
}
|
|
@@ -97,28 +94,31 @@ class PeerTransfer {
|
|
|
97
94
|
this.onConnectLimit();
|
|
98
95
|
}
|
|
99
96
|
if (err.message == constant_1.UNAUTHORIZED) {
|
|
100
|
-
this.
|
|
101
|
-
this.requireAuth();
|
|
97
|
+
yield this.requireAuth();
|
|
102
98
|
}
|
|
103
99
|
if (err.type === 'network') {
|
|
104
|
-
this.destroy();
|
|
105
100
|
this.reconnect();
|
|
106
101
|
}
|
|
107
102
|
if (err.type === 'server-error') {
|
|
108
103
|
if (err.message === constant_1.UNAUTHORIZED) {
|
|
109
|
-
|
|
104
|
+
yield this.requireAuth();
|
|
105
|
+
}
|
|
106
|
+
else {
|
|
107
|
+
this.reconnect();
|
|
110
108
|
}
|
|
111
|
-
this.destroy();
|
|
112
|
-
this.reconnect();
|
|
113
109
|
}
|
|
114
|
-
});
|
|
110
|
+
}));
|
|
115
111
|
const socket = peer.socket;
|
|
116
112
|
socket === null || socket === void 0 ? void 0 : socket.on('message', (message) => {
|
|
117
113
|
this.handleSocketMessage(message);
|
|
118
114
|
});
|
|
115
|
+
socket === null || socket === void 0 ? void 0 : socket.on('error', (err) => {
|
|
116
|
+
console.log('socket@error', err);
|
|
117
|
+
});
|
|
119
118
|
socket.on('close', () => {
|
|
119
|
+
console.log('socket@close');
|
|
120
120
|
setTimeout(() => {
|
|
121
|
-
this.
|
|
121
|
+
this.reconnect();
|
|
122
122
|
}, constant_1.CONNECTION_RETRY);
|
|
123
123
|
});
|
|
124
124
|
this.onHeartbeat({ type: 'HEARTBEAT' });
|
|
@@ -146,9 +146,6 @@ class PeerTransfer {
|
|
|
146
146
|
if (this.isPaused) {
|
|
147
147
|
return;
|
|
148
148
|
}
|
|
149
|
-
if (this.timeoutId) {
|
|
150
|
-
clearTimeout(this.timeoutId);
|
|
151
|
-
}
|
|
152
149
|
}
|
|
153
150
|
onError(message) {
|
|
154
151
|
var _a;
|
|
@@ -156,7 +153,7 @@ class PeerTransfer {
|
|
|
156
153
|
return;
|
|
157
154
|
}
|
|
158
155
|
if (((_a = message.payload) === null || _a === void 0 ? void 0 : _a.msg) === constant_1.CONNECTION_PEER_CONFLICT) {
|
|
159
|
-
this.
|
|
156
|
+
this.stop();
|
|
160
157
|
this.getStore().dispatch({
|
|
161
158
|
type: 'global/updateNetworkState',
|
|
162
159
|
payload: {
|
|
@@ -165,7 +162,6 @@ class PeerTransfer {
|
|
|
165
162
|
message: 'peer conflict',
|
|
166
163
|
},
|
|
167
164
|
});
|
|
168
|
-
this.destroy();
|
|
169
165
|
return;
|
|
170
166
|
}
|
|
171
167
|
this.reconnect();
|
|
@@ -211,7 +207,14 @@ class PeerTransfer {
|
|
|
211
207
|
}
|
|
212
208
|
}
|
|
213
209
|
checkConnection() {
|
|
214
|
-
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
|
+
}
|
|
215
218
|
const deviceIds = this.connectPool.keys();
|
|
216
219
|
for (const deviceId of deviceIds) {
|
|
217
220
|
const conn = this.connectPool.get(deviceId);
|
|
@@ -219,7 +222,7 @@ class PeerTransfer {
|
|
|
219
222
|
this.ping(conn.peer);
|
|
220
223
|
}
|
|
221
224
|
}
|
|
222
|
-
}, constant_1.HEARTBEAT_INTERVAL);
|
|
225
|
+
}), constant_1.HEARTBEAT_INTERVAL);
|
|
223
226
|
}
|
|
224
227
|
getApiHost() {
|
|
225
228
|
var _a, _b, _c;
|
|
@@ -426,7 +429,7 @@ class PeerTransfer {
|
|
|
426
429
|
clearTimeout(timeout);
|
|
427
430
|
const now = Date.now();
|
|
428
431
|
if (now - message.createdAt > 3000) {
|
|
429
|
-
console.
|
|
432
|
+
console.warn('[egos] slow peer request:', message.route, now - message.createdAt);
|
|
430
433
|
}
|
|
431
434
|
resolve(message);
|
|
432
435
|
});
|
|
@@ -474,11 +477,14 @@ class PeerTransfer {
|
|
|
474
477
|
}
|
|
475
478
|
reconnect() {
|
|
476
479
|
return __awaiter(this, void 0, void 0, function* () {
|
|
480
|
+
if (this.isPaused) {
|
|
481
|
+
return;
|
|
482
|
+
}
|
|
477
483
|
if (this.reconnectingPromise) {
|
|
478
|
-
return this.reconnectingPromise;
|
|
484
|
+
return yield this.reconnectingPromise;
|
|
479
485
|
}
|
|
480
486
|
this.reconnectingPromise = this.doReconnect();
|
|
481
|
-
return this.reconnectingPromise.finally(() => {
|
|
487
|
+
return yield this.reconnectingPromise.finally(() => {
|
|
482
488
|
this.reconnectingPromise = undefined;
|
|
483
489
|
});
|
|
484
490
|
});
|
|
@@ -493,19 +499,21 @@ class PeerTransfer {
|
|
|
493
499
|
if ((_a = this.peer) === null || _a === void 0 ? void 0 : _a.open) {
|
|
494
500
|
return this.peer;
|
|
495
501
|
}
|
|
502
|
+
console.log('reconnecting~~', Date.now());
|
|
496
503
|
if ((_b = this.peer) === null || _b === void 0 ? void 0 : _b.disconnected) {
|
|
497
504
|
this.peer.reconnect();
|
|
498
505
|
return;
|
|
499
506
|
}
|
|
500
507
|
else {
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
508
|
+
yield new Promise((resolve) => {
|
|
509
|
+
setTimeout(() => {
|
|
510
|
+
resolve(undefined);
|
|
511
|
+
}, constant_1.RECONNECT_TIMEOUT);
|
|
512
|
+
});
|
|
513
|
+
return this.createPeer();
|
|
505
514
|
}
|
|
506
515
|
}
|
|
507
516
|
catch (err) {
|
|
508
|
-
this.destroy();
|
|
509
517
|
return this.createPeer();
|
|
510
518
|
}
|
|
511
519
|
});
|
|
@@ -547,7 +555,6 @@ class PeerTransfer {
|
|
|
547
555
|
(_a = this.peer) === null || _a === void 0 ? void 0 : _a.destroy();
|
|
548
556
|
(_b = this.peer) === null || _b === void 0 ? void 0 : _b.disconnect();
|
|
549
557
|
this.peerPool.clear();
|
|
550
|
-
this.timeoutId = undefined;
|
|
551
558
|
this.booted = undefined;
|
|
552
559
|
this.peer = undefined;
|
|
553
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');
|
|
@@ -119,12 +117,10 @@ export abstract class PeerTransfer {
|
|
|
119
117
|
return;
|
|
120
118
|
}
|
|
121
119
|
setTimeout(() => {
|
|
122
|
-
|
|
123
|
-
this.reconnect();
|
|
124
|
-
}
|
|
120
|
+
this.reconnect();
|
|
125
121
|
}, CONNECTION_RETRY);
|
|
126
122
|
});
|
|
127
|
-
peer.on('error', (err) => {
|
|
123
|
+
peer.on('error', async (err) => {
|
|
128
124
|
if (err.message === CONNECTION_PEER_CONFLICT) {
|
|
129
125
|
this.stop();
|
|
130
126
|
}
|
|
@@ -133,28 +129,30 @@ export abstract class PeerTransfer {
|
|
|
133
129
|
this.onConnectLimit();
|
|
134
130
|
}
|
|
135
131
|
if (err.message == UNAUTHORIZED) {
|
|
136
|
-
this.
|
|
137
|
-
this.requireAuth();
|
|
132
|
+
await this.requireAuth();
|
|
138
133
|
}
|
|
139
134
|
if (err.type === 'network') {
|
|
140
|
-
this.destroy();
|
|
141
135
|
this.reconnect();
|
|
142
136
|
}
|
|
143
137
|
if (err.type === 'server-error') {
|
|
144
138
|
if (err.message === UNAUTHORIZED) {
|
|
145
|
-
|
|
139
|
+
await this.requireAuth();
|
|
140
|
+
} else {
|
|
141
|
+
this.reconnect();
|
|
146
142
|
}
|
|
147
|
-
this.destroy();
|
|
148
|
-
this.reconnect();
|
|
149
143
|
}
|
|
150
144
|
});
|
|
151
145
|
const socket = peer.socket;
|
|
152
146
|
socket?.on('message', (message: IMessage) => {
|
|
153
147
|
this.handleSocketMessage(message);
|
|
154
148
|
});
|
|
149
|
+
socket?.on('error', (err) => {
|
|
150
|
+
console.log('socket@error', err);
|
|
151
|
+
});
|
|
155
152
|
socket.on('close', () => {
|
|
153
|
+
console.log('socket@close');
|
|
156
154
|
setTimeout(() => {
|
|
157
|
-
this.
|
|
155
|
+
this.reconnect();
|
|
158
156
|
}, CONNECTION_RETRY);
|
|
159
157
|
});
|
|
160
158
|
this.onHeartbeat({ type: 'HEARTBEAT' } as IMessage);
|
|
@@ -183,9 +181,6 @@ export abstract class PeerTransfer {
|
|
|
183
181
|
if (this.isPaused) {
|
|
184
182
|
return;
|
|
185
183
|
}
|
|
186
|
-
if (this.timeoutId) {
|
|
187
|
-
clearTimeout(this.timeoutId);
|
|
188
|
-
}
|
|
189
184
|
}
|
|
190
185
|
|
|
191
186
|
onError(message: IMessage) {
|
|
@@ -193,7 +188,7 @@ export abstract class PeerTransfer {
|
|
|
193
188
|
return;
|
|
194
189
|
}
|
|
195
190
|
if (message.payload?.msg === CONNECTION_PEER_CONFLICT) {
|
|
196
|
-
this.
|
|
191
|
+
this.stop();
|
|
197
192
|
this.getStore().dispatch({
|
|
198
193
|
type: 'global/updateNetworkState',
|
|
199
194
|
payload: {
|
|
@@ -202,7 +197,6 @@ export abstract class PeerTransfer {
|
|
|
202
197
|
message: 'peer conflict',
|
|
203
198
|
},
|
|
204
199
|
});
|
|
205
|
-
this.destroy();
|
|
206
200
|
return;
|
|
207
201
|
}
|
|
208
202
|
this.reconnect();
|
|
@@ -251,7 +245,13 @@ export abstract class PeerTransfer {
|
|
|
251
245
|
}
|
|
252
246
|
|
|
253
247
|
checkConnection() {
|
|
254
|
-
this.checkerId
|
|
248
|
+
if (this.checkerId) {
|
|
249
|
+
return;
|
|
250
|
+
}
|
|
251
|
+
this.checkerId = setInterval(async () => {
|
|
252
|
+
if (!this.peer?.open) {
|
|
253
|
+
await this.reconnect();
|
|
254
|
+
}
|
|
255
255
|
const deviceIds = this.connectPool.keys();
|
|
256
256
|
for (const deviceId of deviceIds) {
|
|
257
257
|
const conn = this.connectPool.get(deviceId);
|
|
@@ -459,7 +459,7 @@ export abstract class PeerTransfer {
|
|
|
459
459
|
clearTimeout(timeout);
|
|
460
460
|
const now = Date.now();
|
|
461
461
|
if (now - message.createdAt > 3000) {
|
|
462
|
-
console.
|
|
462
|
+
console.warn('[egos] slow peer request:', message.route, now - message.createdAt);
|
|
463
463
|
}
|
|
464
464
|
resolve(message);
|
|
465
465
|
});
|
|
@@ -539,12 +539,15 @@ export abstract class PeerTransfer {
|
|
|
539
539
|
}
|
|
540
540
|
|
|
541
541
|
async reconnect(): Promise<Peer | undefined> {
|
|
542
|
+
if (this.isPaused) {
|
|
543
|
+
return;
|
|
544
|
+
}
|
|
542
545
|
if (this.reconnectingPromise) {
|
|
543
|
-
return this.reconnectingPromise;
|
|
546
|
+
return await this.reconnectingPromise;
|
|
544
547
|
}
|
|
545
548
|
|
|
546
549
|
this.reconnectingPromise = this.doReconnect();
|
|
547
|
-
return this.reconnectingPromise.finally(() => {
|
|
550
|
+
return await this.reconnectingPromise.finally(() => {
|
|
548
551
|
this.reconnectingPromise = undefined;
|
|
549
552
|
});
|
|
550
553
|
}
|
|
@@ -557,17 +560,20 @@ export abstract class PeerTransfer {
|
|
|
557
560
|
if (this.peer?.open) {
|
|
558
561
|
return this.peer;
|
|
559
562
|
}
|
|
563
|
+
console.log('reconnecting~~', Date.now());
|
|
560
564
|
if (this.peer?.disconnected) {
|
|
561
565
|
this.peer.reconnect();
|
|
562
566
|
return;
|
|
563
567
|
} else {
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
+
await new Promise((resolve) => {
|
|
569
|
+
setTimeout(() => {
|
|
570
|
+
resolve(undefined);
|
|
571
|
+
}, RECONNECT_TIMEOUT);
|
|
572
|
+
});
|
|
573
|
+
|
|
574
|
+
return this.createPeer();
|
|
568
575
|
}
|
|
569
576
|
} catch (err) {
|
|
570
|
-
this.destroy();
|
|
571
577
|
return this.createPeer();
|
|
572
578
|
}
|
|
573
579
|
}
|
|
@@ -610,7 +616,6 @@ export abstract class PeerTransfer {
|
|
|
610
616
|
this.peer?.destroy();
|
|
611
617
|
this.peer?.disconnect();
|
|
612
618
|
this.peerPool.clear();
|
|
613
|
-
this.timeoutId = undefined;
|
|
614
619
|
this.booted = undefined;
|
|
615
620
|
this.peer = undefined;
|
|
616
621
|
if (this.checkerId) {
|