egos-transfer 0.1.3 → 0.1.6
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/file-transfer.js +1 -0
- package/dist/index.d.ts +7 -6
- package/dist/index.js +205 -192
- package/dist/router.js +1 -1
- package/package.json +1 -1
- package/src/constant.ts +1 -1
- package/src/file-transfer.ts +1 -0
- package/src/index.ts +221 -202
- package/src/router.ts +1 -3
package/dist/index.js
CHANGED
|
@@ -32,100 +32,112 @@ const peerjs_1 = require("peerjs");
|
|
|
32
32
|
const entity_1 = require("./entity");
|
|
33
33
|
const router_1 = require("./router");
|
|
34
34
|
const axios_1 = __importDefault(require("axios"));
|
|
35
|
-
const helper_1 = require("./helper");
|
|
36
35
|
class PeerTransfer {
|
|
37
36
|
constructor(deviceId, config, isRadomPeerId = false) {
|
|
38
|
-
this.peerIdMap = new Map();
|
|
39
37
|
this.retry = 0;
|
|
38
|
+
this.inRequest = new Map();
|
|
40
39
|
this.connectPool = new Map();
|
|
41
40
|
this.peerConfig = Object.assign(Object.assign({}, config), { key: String(Date.now()) });
|
|
42
41
|
this.requests = new Map();
|
|
43
42
|
this.timeout = entity_1.REQUEST_TIMEOUT;
|
|
44
43
|
this.router = new router_1.PeerRouter();
|
|
45
|
-
this.peerPool = new Map();
|
|
46
44
|
this.isPaused = false;
|
|
47
45
|
this.peerId = isRadomPeerId ? deviceId + '_' + Date.now() : deviceId;
|
|
48
46
|
this.deviceId = deviceId;
|
|
49
47
|
}
|
|
48
|
+
createPeer() {
|
|
49
|
+
return __awaiter(this, arguments, void 0, function* (force = false) {
|
|
50
|
+
var _a;
|
|
51
|
+
if (this.isPaused) {
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
if (this.booted) {
|
|
55
|
+
return this.booted;
|
|
56
|
+
}
|
|
57
|
+
if (((_a = this.peer) === null || _a === void 0 ? void 0 : _a.open) && !force) {
|
|
58
|
+
return this.peer;
|
|
59
|
+
}
|
|
60
|
+
this.destroy();
|
|
61
|
+
yield this.reloadConfig();
|
|
62
|
+
const peer = new peerjs_1.Peer(this.peerId, Object.assign(Object.assign({}, this.peerConfig), { pingInterval: 15000 }));
|
|
63
|
+
this.peer = peer;
|
|
64
|
+
yield this.initialize(this.peer);
|
|
65
|
+
this.checkConnection();
|
|
66
|
+
return this.peer;
|
|
67
|
+
});
|
|
68
|
+
}
|
|
50
69
|
initialize(peer) {
|
|
51
70
|
return __awaiter(this, void 0, void 0, function* () {
|
|
52
71
|
try {
|
|
72
|
+
if (this.checkerId) {
|
|
73
|
+
clearInterval(this.checkerId);
|
|
74
|
+
}
|
|
53
75
|
this.booted = new Promise((resolve) => {
|
|
54
76
|
const timer = setTimeout(() => {
|
|
55
|
-
|
|
56
|
-
if (!((_a = this.peer) === null || _a === void 0 ? void 0 : _a.open)) {
|
|
57
|
-
this.createPeer(true);
|
|
58
|
-
}
|
|
59
|
-
// reject('peer open timeout');
|
|
77
|
+
resolve(null);
|
|
60
78
|
}, constant_1.PEER_OPEN_TIMEOUT);
|
|
79
|
+
const done = (peer) => {
|
|
80
|
+
clearTimeout(timer);
|
|
81
|
+
resolve(peer);
|
|
82
|
+
};
|
|
61
83
|
peer.on('open', (id) => {
|
|
62
|
-
this.booted = undefined;
|
|
63
84
|
clearTimeout(timer);
|
|
64
|
-
|
|
85
|
+
done(peer);
|
|
65
86
|
});
|
|
87
|
+
peer.on('disconnected', () => {
|
|
88
|
+
if (!this.isPaused) {
|
|
89
|
+
setTimeout(() => {
|
|
90
|
+
if (!this.isPaused) {
|
|
91
|
+
this.reconnect();
|
|
92
|
+
}
|
|
93
|
+
}, constant_1.CONNECTION_RETRY);
|
|
94
|
+
}
|
|
95
|
+
});
|
|
96
|
+
// duplicated with close event
|
|
97
|
+
peer.on('close', (...args) => { });
|
|
98
|
+
peer.on('error', (err) => __awaiter(this, void 0, void 0, function* () {
|
|
99
|
+
if (err.message === constant_1.CONNECTION_PEER_CONFLICT) {
|
|
100
|
+
this.stop();
|
|
101
|
+
done(null);
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
if (err.message === constant_1.CONNECTION_LIMIT_EXCEED) {
|
|
105
|
+
this.stop();
|
|
106
|
+
this.onConnectLimit();
|
|
107
|
+
return done(null);
|
|
108
|
+
}
|
|
109
|
+
if (err.message == constant_1.UNAUTHORIZED) {
|
|
110
|
+
done(null);
|
|
111
|
+
return this.requireAuth();
|
|
112
|
+
}
|
|
113
|
+
if (err.type === 'network') {
|
|
114
|
+
done(null);
|
|
115
|
+
return this.reconnect();
|
|
116
|
+
}
|
|
117
|
+
if (err.type === 'server-error') {
|
|
118
|
+
done(null);
|
|
119
|
+
return this.reconnect();
|
|
120
|
+
}
|
|
121
|
+
done(null);
|
|
122
|
+
return this.reconnect();
|
|
123
|
+
}));
|
|
66
124
|
});
|
|
67
125
|
peer.on('connection', (conn) => {
|
|
126
|
+
conn.label = 'incoming';
|
|
68
127
|
this.onConnect(conn);
|
|
69
128
|
});
|
|
70
|
-
peer.on('disconnected', () => {
|
|
71
|
-
if (!this.isPaused) {
|
|
72
|
-
setTimeout(() => {
|
|
73
|
-
if (!this.isPaused) {
|
|
74
|
-
this.reconnect();
|
|
75
|
-
}
|
|
76
|
-
}, constant_1.CONNECTION_RETRY);
|
|
77
|
-
}
|
|
78
|
-
});
|
|
79
|
-
peer.on('call', (mConn) => { });
|
|
80
|
-
peer.on('close', (...args) => {
|
|
81
|
-
if (this.isPaused) {
|
|
82
|
-
return;
|
|
83
|
-
}
|
|
84
|
-
setTimeout(() => {
|
|
85
|
-
this.reconnect();
|
|
86
|
-
}, constant_1.CONNECTION_RETRY);
|
|
87
|
-
});
|
|
88
|
-
peer.on('error', (err) => __awaiter(this, void 0, void 0, function* () {
|
|
89
|
-
if (err.message === constant_1.CONNECTION_PEER_CONFLICT) {
|
|
90
|
-
this.stop();
|
|
91
|
-
}
|
|
92
|
-
if (err.message === constant_1.CONNECTION_LIMIT_EXCEED) {
|
|
93
|
-
this.stop();
|
|
94
|
-
this.onConnectLimit();
|
|
95
|
-
}
|
|
96
|
-
if (err.message == constant_1.UNAUTHORIZED) {
|
|
97
|
-
yield this.requireAuth();
|
|
98
|
-
}
|
|
99
|
-
if (err.type === 'network') {
|
|
100
|
-
this.reconnect();
|
|
101
|
-
}
|
|
102
|
-
if (err.type === 'server-error') {
|
|
103
|
-
if (err.message === constant_1.UNAUTHORIZED) {
|
|
104
|
-
yield this.requireAuth();
|
|
105
|
-
}
|
|
106
|
-
else {
|
|
107
|
-
this.reconnect();
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
}));
|
|
111
129
|
const socket = peer.socket;
|
|
112
130
|
socket === null || socket === void 0 ? void 0 : socket.on('message', (message) => {
|
|
113
131
|
this.handleSocketMessage(message);
|
|
114
132
|
});
|
|
115
|
-
socket === null || socket === void 0 ? void 0 : socket.on('error', (err) => {
|
|
116
|
-
console.log('socket@error', err);
|
|
117
|
-
});
|
|
118
|
-
socket.on('close', () => {
|
|
119
|
-
console.log('socket@close');
|
|
120
|
-
setTimeout(() => {
|
|
121
|
-
this.reconnect();
|
|
122
|
-
}, constant_1.CONNECTION_RETRY);
|
|
123
|
-
});
|
|
124
133
|
this.onHeartbeat({ type: 'HEARTBEAT' });
|
|
125
|
-
|
|
134
|
+
yield this.booted;
|
|
126
135
|
}
|
|
127
136
|
catch (err) {
|
|
128
|
-
|
|
137
|
+
this.booted = undefined;
|
|
138
|
+
}
|
|
139
|
+
finally {
|
|
140
|
+
this.booted = undefined;
|
|
129
141
|
}
|
|
130
142
|
});
|
|
131
143
|
}
|
|
@@ -164,7 +176,6 @@ class PeerTransfer {
|
|
|
164
176
|
});
|
|
165
177
|
return;
|
|
166
178
|
}
|
|
167
|
-
this.reconnect();
|
|
168
179
|
}
|
|
169
180
|
onOpen(message) {
|
|
170
181
|
if (message.type !== 'OPEN') {
|
|
@@ -185,21 +196,29 @@ class PeerTransfer {
|
|
|
185
196
|
return;
|
|
186
197
|
}
|
|
187
198
|
const deviceId = message.payload.deviceId;
|
|
188
|
-
this.offline(deviceId);
|
|
189
199
|
this.ping(deviceId);
|
|
190
200
|
}
|
|
191
|
-
removeConnection(peerId) {
|
|
192
|
-
var _a
|
|
201
|
+
removeConnection(peerId, connectionId) {
|
|
202
|
+
var _a;
|
|
193
203
|
const conn = this.connectPool.get(peerId);
|
|
194
204
|
if (conn) {
|
|
195
|
-
if (
|
|
196
|
-
|
|
205
|
+
if (connectionId && conn.connectionId !== connectionId) {
|
|
206
|
+
return;
|
|
207
|
+
}
|
|
208
|
+
const sending = this.inRequest.get(conn.connectionId);
|
|
209
|
+
if (sending) {
|
|
210
|
+
// 提前结束request,避免关闭连接时触发反馈
|
|
211
|
+
sending.feedback();
|
|
212
|
+
this.inRequest.delete(conn.connectionId);
|
|
197
213
|
}
|
|
198
214
|
this.connectPool.delete(peerId);
|
|
199
|
-
|
|
215
|
+
conn.removeAllListeners();
|
|
216
|
+
conn.close();
|
|
217
|
+
(_a = this.peer) === null || _a === void 0 ? void 0 : _a._removeConnection(conn);
|
|
200
218
|
}
|
|
201
219
|
}
|
|
202
220
|
addConnection(conn) {
|
|
221
|
+
// console.log('addConnection@', conn.label);
|
|
203
222
|
this.connectPool.set(conn.peer, conn);
|
|
204
223
|
const connecting = PeerTransfer.connecting.get(conn.peer);
|
|
205
224
|
if (connecting) {
|
|
@@ -217,17 +236,20 @@ class PeerTransfer {
|
|
|
217
236
|
}
|
|
218
237
|
// turn credential expires
|
|
219
238
|
if (this.peerConfig.turnExpiredAt && Date.now() - 30000 > this.peerConfig.turnExpiredAt) {
|
|
239
|
+
// this.connectPool.clear();
|
|
220
240
|
yield this.createPeer(true);
|
|
221
|
-
this.connectPool.clear();
|
|
222
241
|
return;
|
|
223
242
|
}
|
|
224
|
-
const deviceIds = this.connectPool.keys();
|
|
225
|
-
for (const deviceId of deviceIds) {
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
}
|
|
243
|
+
// const deviceIds = this.connectPool.keys();
|
|
244
|
+
// for (const deviceId of deviceIds) {
|
|
245
|
+
// const conn = this.connectPool.get(deviceId);
|
|
246
|
+
// if (!this.isValidConnection(conn)) {
|
|
247
|
+
// this.removeConnection(conn.peer);
|
|
248
|
+
// this.peer?._removeConnection(conn);
|
|
249
|
+
// } else {
|
|
250
|
+
// this.ping(conn.peer);
|
|
251
|
+
// }
|
|
252
|
+
// }
|
|
231
253
|
}), constant_1.HEARTBEAT_INTERVAL);
|
|
232
254
|
}
|
|
233
255
|
getApiHost() {
|
|
@@ -257,82 +279,72 @@ class PeerTransfer {
|
|
|
257
279
|
if (deviceId === this.deviceId) {
|
|
258
280
|
return;
|
|
259
281
|
}
|
|
260
|
-
|
|
261
|
-
const
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
const status = yield this.getDeviceStatus(deviceId);
|
|
267
|
-
if (!status || !status.isOnline) {
|
|
268
|
-
return;
|
|
282
|
+
const peerId = deviceId;
|
|
283
|
+
const existingConn = this.connectPool.get(peerId);
|
|
284
|
+
// console.log('connectToPeer', peerId, existingConn, this.connectPool);
|
|
285
|
+
if (existingConn) {
|
|
286
|
+
if (this.isValidConnection(existingConn)) {
|
|
287
|
+
return existingConn;
|
|
269
288
|
}
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
}
|
|
273
|
-
if (this.connectPool.has(peerId)) {
|
|
274
|
-
const conn = this.connectPool.get(peerId);
|
|
275
|
-
if (conn === null || conn === void 0 ? void 0 : conn.open) {
|
|
276
|
-
return conn;
|
|
289
|
+
else {
|
|
290
|
+
this.removeConnection(peerId);
|
|
277
291
|
}
|
|
278
292
|
}
|
|
279
|
-
// Check if there's already a connection attempt for this peerId
|
|
280
293
|
if (PeerTransfer.connecting.has(peerId)) {
|
|
281
|
-
|
|
282
|
-
const
|
|
283
|
-
|
|
294
|
+
const entry = PeerTransfer.connecting.get(peerId);
|
|
295
|
+
const conn = yield (entry === null || entry === void 0 ? void 0 : entry.promise);
|
|
296
|
+
if (conn) {
|
|
297
|
+
return conn;
|
|
298
|
+
}
|
|
284
299
|
}
|
|
285
|
-
|
|
286
|
-
|
|
300
|
+
return this.createConnection(peerId);
|
|
301
|
+
});
|
|
302
|
+
}
|
|
303
|
+
createConnection(peerId) {
|
|
304
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
305
|
+
var _a, _b;
|
|
306
|
+
let resolve;
|
|
307
|
+
let reject;
|
|
287
308
|
const connectionPromise = new Promise((res, rej) => {
|
|
288
309
|
resolve = res;
|
|
289
310
|
reject = rej;
|
|
290
311
|
});
|
|
291
|
-
// Store the promise in connecting map
|
|
292
312
|
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
|
-
|
|
316
|
-
|
|
317
|
-
this.online(peerId);
|
|
318
|
-
return resolve(conn);
|
|
319
|
-
}
|
|
320
|
-
retry++;
|
|
313
|
+
try {
|
|
314
|
+
for (let retry = 0; retry < 3; retry++) {
|
|
315
|
+
if (!this.peer) {
|
|
316
|
+
yield this.createPeer();
|
|
317
|
+
}
|
|
318
|
+
if (!((_a = this.peer) === null || _a === void 0 ? void 0 : _a.open)) {
|
|
319
|
+
yield this.reconnect();
|
|
320
|
+
}
|
|
321
|
+
const cur = this.connectPool.get(peerId);
|
|
322
|
+
if (this.isValidConnection(cur)) {
|
|
323
|
+
resolve(cur);
|
|
324
|
+
return cur;
|
|
325
|
+
}
|
|
326
|
+
const conn = (_b = this.peer) === null || _b === void 0 ? void 0 : _b.connect(peerId, {
|
|
327
|
+
label: 'outgoing',
|
|
328
|
+
metadata: { time: Date.now() },
|
|
329
|
+
});
|
|
330
|
+
if (!conn) {
|
|
331
|
+
continue;
|
|
332
|
+
}
|
|
333
|
+
const connection = yield this.onConnect(conn).catch(() => { });
|
|
334
|
+
if (connection) {
|
|
335
|
+
resolve(conn);
|
|
336
|
+
return conn;
|
|
321
337
|
}
|
|
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
338
|
}
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
339
|
+
resolve(undefined);
|
|
340
|
+
return undefined;
|
|
341
|
+
}
|
|
342
|
+
catch (err) {
|
|
343
|
+
reject(err);
|
|
344
|
+
}
|
|
345
|
+
finally {
|
|
346
|
+
PeerTransfer.connecting.delete(peerId);
|
|
347
|
+
}
|
|
336
348
|
});
|
|
337
349
|
}
|
|
338
350
|
ping(peerId) {
|
|
@@ -341,7 +353,8 @@ class PeerTransfer {
|
|
|
341
353
|
return;
|
|
342
354
|
}
|
|
343
355
|
const conn = this.connectPool.get(peerId);
|
|
344
|
-
if (!conn) {
|
|
356
|
+
if (!this.isValidConnection(conn)) {
|
|
357
|
+
this.removeConnection(conn === null || conn === void 0 ? void 0 : conn.peer);
|
|
345
358
|
return;
|
|
346
359
|
}
|
|
347
360
|
const res = yield this.request(conn.peer, {
|
|
@@ -362,6 +375,19 @@ class PeerTransfer {
|
|
|
362
375
|
if (!conn) {
|
|
363
376
|
return;
|
|
364
377
|
}
|
|
378
|
+
const exists = this.connectPool.get(conn.peer);
|
|
379
|
+
// console.log('onConnect', conn.label, exists);
|
|
380
|
+
if (exists) {
|
|
381
|
+
if (exists.metadata.time > conn.metadata.time) {
|
|
382
|
+
this.removeConnection(conn.peer);
|
|
383
|
+
if (this.isValidConnection(exists)) {
|
|
384
|
+
return exists;
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
else {
|
|
388
|
+
this.removeConnection(exists.peer);
|
|
389
|
+
}
|
|
390
|
+
}
|
|
365
391
|
return new Promise((resolve) => {
|
|
366
392
|
const done = (connection) => {
|
|
367
393
|
if (connection) {
|
|
@@ -371,14 +397,13 @@ class PeerTransfer {
|
|
|
371
397
|
resolve(connection);
|
|
372
398
|
};
|
|
373
399
|
const timer = setTimeout(() => __awaiter(this, void 0, void 0, function* () {
|
|
374
|
-
if (conn
|
|
400
|
+
if (this.isValidConnection(conn)) {
|
|
375
401
|
this.addConnection(conn);
|
|
376
402
|
done(conn);
|
|
377
403
|
}
|
|
378
404
|
else {
|
|
379
405
|
conn.close();
|
|
380
406
|
this.removeConnection(conn.peer);
|
|
381
|
-
yield this.createPeer();
|
|
382
407
|
done(undefined);
|
|
383
408
|
}
|
|
384
409
|
}), constant_1.CONNECTION_TIMEOUT);
|
|
@@ -394,31 +419,34 @@ class PeerTransfer {
|
|
|
394
419
|
});
|
|
395
420
|
conn.on('error', (err) => __awaiter(this, void 0, void 0, function* () {
|
|
396
421
|
var _a;
|
|
397
|
-
this.removeConnection(conn.peer);
|
|
422
|
+
this.removeConnection(conn.peer, conn.connectionId);
|
|
398
423
|
if (!((_a = this.peer) === null || _a === void 0 ? void 0 : _a.open)) {
|
|
399
424
|
yield this.reconnect();
|
|
400
425
|
}
|
|
426
|
+
done(null);
|
|
401
427
|
}));
|
|
402
428
|
conn.on('close', () => __awaiter(this, void 0, void 0, function* () {
|
|
403
429
|
var _a;
|
|
404
|
-
this.removeConnection(conn.peer);
|
|
430
|
+
this.removeConnection(conn.peer, conn.connectionId);
|
|
405
431
|
if (!((_a = this.peer) === null || _a === void 0 ? void 0 : _a.open)) {
|
|
406
432
|
yield this.reconnect();
|
|
407
433
|
}
|
|
434
|
+
done(null);
|
|
408
435
|
}));
|
|
409
436
|
conn.on('open', () => __awaiter(this, void 0, void 0, function* () {
|
|
410
|
-
this.addConnection(conn);
|
|
411
437
|
clearTimeout(timer);
|
|
412
438
|
done(conn);
|
|
413
439
|
}));
|
|
414
|
-
|
|
440
|
+
if (this.isValidConnection(conn)) {
|
|
441
|
+
done(conn);
|
|
442
|
+
}
|
|
415
443
|
});
|
|
416
444
|
});
|
|
417
445
|
}
|
|
418
446
|
request(peerId, payload) {
|
|
419
447
|
return __awaiter(this, void 0, void 0, function* () {
|
|
420
448
|
const conn = yield this.connectToPeer(peerId);
|
|
421
|
-
if (!conn) {
|
|
449
|
+
if (!this.isValidConnection(conn)) {
|
|
422
450
|
return Object.assign(Object.assign({}, payload), { body: {}, error: { message: 'build connection failed', code: -1 } });
|
|
423
451
|
}
|
|
424
452
|
const requestId = payload.requestId || (yield this.genRequestId());
|
|
@@ -428,15 +456,24 @@ class PeerTransfer {
|
|
|
428
456
|
body: {},
|
|
429
457
|
error: { message: 'request timeout' },
|
|
430
458
|
};
|
|
431
|
-
this.removeConnection(
|
|
459
|
+
this.removeConnection(conn.peer);
|
|
432
460
|
resolve(msg);
|
|
433
461
|
}, payload.timeout || entity_1.REQUEST_TIMEOUT);
|
|
462
|
+
this.inRequest.set(conn.connectionId, {
|
|
463
|
+
conn,
|
|
464
|
+
message: payload,
|
|
465
|
+
feedback: () => resolve({
|
|
466
|
+
body: {},
|
|
467
|
+
error: { message: 'send failed' },
|
|
468
|
+
}),
|
|
469
|
+
});
|
|
434
470
|
this.addRequest(requestId, (message) => {
|
|
435
471
|
clearTimeout(timeout);
|
|
436
472
|
const now = Date.now();
|
|
437
473
|
if (now - message.createdAt > 3000) {
|
|
438
474
|
console.warn('[egos] slow peer request:', message.route, now - message.createdAt);
|
|
439
475
|
}
|
|
476
|
+
this.inRequest.delete(conn.connectionId);
|
|
440
477
|
resolve(message);
|
|
441
478
|
});
|
|
442
479
|
});
|
|
@@ -448,7 +485,7 @@ class PeerTransfer {
|
|
|
448
485
|
reply(conn, oldMessage, message) {
|
|
449
486
|
return __awaiter(this, void 0, void 0, function* () {
|
|
450
487
|
const data = Object.assign(Object.assign(Object.assign({}, this.inheritMessage(oldMessage)), message), { dest: conn.peer, scope: entity_1.PeerScope.RESPONSE, updatedAt: Date.now() });
|
|
451
|
-
if (!conn
|
|
488
|
+
if (!this.isValidConnection(conn)) {
|
|
452
489
|
const connection = yield this.connectToPeer(conn.peer);
|
|
453
490
|
return connection === null || connection === void 0 ? void 0 : connection.send(data);
|
|
454
491
|
}
|
|
@@ -459,7 +496,7 @@ class PeerTransfer {
|
|
|
459
496
|
return __awaiter(this, void 0, void 0, function* () {
|
|
460
497
|
const internalApi = yield this.getApi(conn.peer);
|
|
461
498
|
let connection = conn;
|
|
462
|
-
if (!
|
|
499
|
+
if (!this.isValidConnection) {
|
|
463
500
|
const con = yield this.connectToPeer(conn.peer);
|
|
464
501
|
if (!con) {
|
|
465
502
|
return this.responseFail(conn, message, 'send with invalid connection');
|
|
@@ -495,27 +532,32 @@ class PeerTransfer {
|
|
|
495
532
|
});
|
|
496
533
|
});
|
|
497
534
|
}
|
|
535
|
+
isValidConnection(conn) {
|
|
536
|
+
if (!(conn === null || conn === void 0 ? void 0 : conn.open)) {
|
|
537
|
+
return false;
|
|
538
|
+
}
|
|
539
|
+
if (!(conn === null || conn === void 0 ? void 0 : conn.peerConnection)) {
|
|
540
|
+
return false;
|
|
541
|
+
}
|
|
542
|
+
return ['connected', 'completed'].includes(conn.peerConnection.connectionState);
|
|
543
|
+
}
|
|
498
544
|
doReconnect() {
|
|
499
545
|
return __awaiter(this, void 0, void 0, function* () {
|
|
500
|
-
var _a, _b;
|
|
546
|
+
var _a, _b, _c, _d;
|
|
501
547
|
try {
|
|
502
548
|
if (this.isPaused) {
|
|
503
549
|
return;
|
|
504
550
|
}
|
|
505
|
-
if ((_a = this.peer) === null || _a === void 0 ? void 0 : _a.open) {
|
|
551
|
+
if (((_a = this.peer) === null || _a === void 0 ? void 0 : _a.open) && this.peerConfig.token === ((_b = this.peer) === null || _b === void 0 ? void 0 : _b.options.token)) {
|
|
506
552
|
return this.peer;
|
|
507
553
|
}
|
|
508
|
-
|
|
509
|
-
if ((
|
|
554
|
+
// return this.createPeer();
|
|
555
|
+
if (((_c = this.peer) === null || _c === void 0 ? void 0 : _c.disconnected) && ((_d = this.peer) === null || _d === void 0 ? void 0 : _d.options.token) === this.peerConfig.token) {
|
|
510
556
|
this.peer.reconnect();
|
|
511
|
-
return;
|
|
557
|
+
return this.peer;
|
|
512
558
|
}
|
|
513
559
|
else {
|
|
514
|
-
yield
|
|
515
|
-
setTimeout(() => {
|
|
516
|
-
resolve(undefined);
|
|
517
|
-
}, constant_1.RECONNECT_TIMEOUT);
|
|
518
|
-
});
|
|
560
|
+
yield this.reloadConfig();
|
|
519
561
|
return this.createPeer();
|
|
520
562
|
}
|
|
521
563
|
}
|
|
@@ -524,34 +566,6 @@ class PeerTransfer {
|
|
|
524
566
|
}
|
|
525
567
|
});
|
|
526
568
|
}
|
|
527
|
-
createPeer() {
|
|
528
|
-
return __awaiter(this, arguments, void 0, function* (force = false) {
|
|
529
|
-
var _a, _b;
|
|
530
|
-
if (this.isPaused) {
|
|
531
|
-
return;
|
|
532
|
-
}
|
|
533
|
-
if (this.booted) {
|
|
534
|
-
yield this.booted;
|
|
535
|
-
if ((_a = this.peer) === null || _a === void 0 ? void 0 : _a.open) {
|
|
536
|
-
return this.peer;
|
|
537
|
-
}
|
|
538
|
-
this.booted = undefined;
|
|
539
|
-
}
|
|
540
|
-
if (((_b = this.peer) === null || _b === void 0 ? void 0 : _b.open) && !force) {
|
|
541
|
-
return this.peer;
|
|
542
|
-
}
|
|
543
|
-
this.destroy();
|
|
544
|
-
yield this.reloadConfig();
|
|
545
|
-
const peer = new peerjs_1.Peer(this.peerId, Object.assign(Object.assign({}, this.peerConfig), { pingInterval: 15000 }));
|
|
546
|
-
this.peer = peer;
|
|
547
|
-
if (this.checkerId) {
|
|
548
|
-
clearInterval(this.checkerId);
|
|
549
|
-
}
|
|
550
|
-
this.checkConnection();
|
|
551
|
-
yield this.initialize(peer);
|
|
552
|
-
return this.peer;
|
|
553
|
-
});
|
|
554
|
-
}
|
|
555
569
|
clearInstance() {
|
|
556
570
|
var _a, _b;
|
|
557
571
|
PeerTransfer.connecting.clear();
|
|
@@ -560,8 +574,7 @@ class PeerTransfer {
|
|
|
560
574
|
this.removeConnection(pId);
|
|
561
575
|
}
|
|
562
576
|
(_a = this.peer) === null || _a === void 0 ? void 0 : _a.destroy();
|
|
563
|
-
(_b = this.peer) === null || _b === void 0 ? void 0 : _b.
|
|
564
|
-
this.peerPool.clear();
|
|
577
|
+
(_b = this.peer) === null || _b === void 0 ? void 0 : _b.removeAllListeners();
|
|
565
578
|
this.booted = undefined;
|
|
566
579
|
this.peer = undefined;
|
|
567
580
|
if (this.checkerId) {
|
package/dist/router.js
CHANGED
|
@@ -118,7 +118,7 @@ class PeerRouter {
|
|
|
118
118
|
.sort((a, b) => (b.priority || 0) - (a.priority || 0));
|
|
119
119
|
const res = yield this.compose(stack)(conn, message);
|
|
120
120
|
// Execute route handler with timeout
|
|
121
|
-
const routePromise = Promise.resolve(route.callback.apply(
|
|
121
|
+
const routePromise = Promise.resolve(route.callback.apply(null, [conn, message]));
|
|
122
122
|
const timeoutPromise = new Promise((_, reject) => {
|
|
123
123
|
setTimeout(() => reject(new Error('Request timeout')), route.timeout || 30000);
|
|
124
124
|
});
|
package/package.json
CHANGED
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 =
|
|
5
|
+
export const CONNECTION_TIMEOUT = 6000;
|
|
6
6
|
export const LOCKER_TIMEOUT = 15000;
|
|
7
7
|
export const RECONNECT_TIMEOUT = 5000;
|
|
8
8
|
export const PEER_OPEN_TIMEOUT = 15000;
|
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, {
|