@leofcoin/peernet 0.11.19 → 0.11.20
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/browser/peernet.js +91 -54
- package/dist/commonjs/{http-7f0fcdaf.js → http-62c5b155.js} +1 -1
- package/dist/commonjs/peernet.js +92 -55
- package/dist/module/peernet.js +91 -54
- package/package.json +2 -2
- package/src/handlers/data.js +15 -0
- package/src/peernet.js +8 -15
package/dist/browser/peernet.js
CHANGED
|
@@ -287,6 +287,8 @@ const socketRequestClient = (url, protocols = 'echo-protocol', options = { retry
|
|
|
287
287
|
});
|
|
288
288
|
};
|
|
289
289
|
|
|
290
|
+
const messageQue = {};
|
|
291
|
+
|
|
290
292
|
class Peer {
|
|
291
293
|
#connection
|
|
292
294
|
#ready = false
|
|
@@ -305,6 +307,7 @@ class Peer {
|
|
|
305
307
|
#iceCompleteTimer
|
|
306
308
|
#channel
|
|
307
309
|
#peerId
|
|
310
|
+
#chunkSize = 16384
|
|
308
311
|
|
|
309
312
|
get connection() {
|
|
310
313
|
return this.#connection
|
|
@@ -356,29 +359,51 @@ class Peer {
|
|
|
356
359
|
return this._socketClient
|
|
357
360
|
}
|
|
358
361
|
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
362
|
+
splitMessage(message) {
|
|
363
|
+
const chunks = [];
|
|
364
|
+
const size = message.byteLength || message.length;
|
|
365
|
+
let offset = 0;
|
|
366
|
+
return new Promise((resolve, reject) => {
|
|
367
|
+
const splitMessage = () => {
|
|
368
|
+
const chunk = message.slice(offset, offset + this.#chunkSize > size ? size : offset + this.#chunkSize);
|
|
369
|
+
offset += offset + this.#chunkSize;
|
|
370
|
+
chunks.push(chunk);
|
|
371
|
+
if (offset < size) return splitMessage()
|
|
372
|
+
else resolve({chunks, size});
|
|
373
|
+
};
|
|
374
|
+
|
|
375
|
+
splitMessage();
|
|
376
|
+
})
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
async send(message, id) {
|
|
380
|
+
const { chunks, size } = await this.splitMessage(message);
|
|
381
|
+
let offset = 0;
|
|
382
|
+
for (const chunk of chunks) {
|
|
383
|
+
const start = offset;
|
|
384
|
+
const end = offset + chunk.length;
|
|
385
|
+
const message = new TextEncoder().encode(JSON.stringify({ size, id, chunk, start, end }));
|
|
386
|
+
switch (this.channel?.readyState) {
|
|
387
|
+
case 'open':
|
|
388
|
+
this.bw.up += message.length || message.byteLength;
|
|
389
|
+
this.channel.send(message);
|
|
390
|
+
break;
|
|
391
|
+
case 'closed':
|
|
392
|
+
case 'closing':
|
|
393
|
+
debug('channel already closed, this usually means a bad implementation, try checking the readyState or check if the peer is connected before sending');
|
|
394
|
+
break;
|
|
395
|
+
case undefined:
|
|
396
|
+
debug(`trying to send before a channel is created`);
|
|
397
|
+
break;
|
|
398
|
+
}
|
|
399
|
+
}
|
|
373
400
|
}
|
|
374
401
|
|
|
375
402
|
request(data) {
|
|
376
403
|
return new Promise((resolve, reject) => {
|
|
377
404
|
const id = Math.random().toString(36).slice(-12);
|
|
378
|
-
|
|
379
|
-
data = new TextEncoder().encode(JSON.stringify({id, data}));
|
|
405
|
+
|
|
380
406
|
const _onData = message => {
|
|
381
|
-
message = JSON.parse(new TextDecoder().decode(message.data));
|
|
382
407
|
if (message.id === id) {
|
|
383
408
|
resolve(message.data);
|
|
384
409
|
pubsub.unsubscribe(`peer:data`, _onData);
|
|
@@ -392,8 +417,8 @@ class Peer {
|
|
|
392
417
|
// pubsub.unsubscribe(`peer:data-request-${id}`, _onData)
|
|
393
418
|
// }, 5000);
|
|
394
419
|
|
|
395
|
-
this.send(data);
|
|
396
|
-
})
|
|
420
|
+
this.send(data, id);
|
|
421
|
+
})
|
|
397
422
|
}
|
|
398
423
|
|
|
399
424
|
async #init() {
|
|
@@ -423,10 +448,7 @@ class Peer {
|
|
|
423
448
|
message.channel.onclose = () => this.close.bind(this);
|
|
424
449
|
|
|
425
450
|
message.channel.onmessage = (message) => {
|
|
426
|
-
|
|
427
|
-
debug(`incoming message from ${this.id}`);
|
|
428
|
-
debug(message);
|
|
429
|
-
this.bw.down += message.length || message.byteLength;
|
|
451
|
+
this._handleMessage(this.id, message);
|
|
430
452
|
};
|
|
431
453
|
this.channel = message.channel;
|
|
432
454
|
};
|
|
@@ -441,28 +463,39 @@ class Peer {
|
|
|
441
463
|
this.channel.onclose = () => this.close.bind(this);
|
|
442
464
|
|
|
443
465
|
this.channel.onmessage = (message) => {
|
|
444
|
-
|
|
445
|
-
debug(`incoming message from ${this.peerId}`);
|
|
446
|
-
this.bw.down += message.length || message.byteLength;
|
|
466
|
+
this._handleMessage(this.peerId, message);
|
|
447
467
|
};
|
|
448
468
|
|
|
449
469
|
const offer = await this.#connection.createOffer();
|
|
450
470
|
await this.#connection.setLocalDescription(offer);
|
|
451
471
|
|
|
452
472
|
this._sendMessage({'sdp': this.#connection.localDescription});
|
|
473
|
+
}
|
|
474
|
+
} catch (e) {
|
|
475
|
+
console.log(e);
|
|
476
|
+
}
|
|
477
|
+
}
|
|
453
478
|
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
await this.#connection.setLocalDescription(offer);
|
|
479
|
+
_handleMessage(peerId, message) {
|
|
480
|
+
debug(`incoming message from ${peerId}`);
|
|
457
481
|
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
482
|
+
message = JSON.parse(new TextDecoder().decode(message.data));
|
|
483
|
+
// allow sharding (multiple peers share data)
|
|
484
|
+
pubsub.publish('peernet:shard', message);
|
|
485
|
+
if (!messageQue[message.id]) messageQue[message.id] = [];
|
|
462
486
|
|
|
463
|
-
|
|
464
|
-
|
|
487
|
+
if (message.size > messageQue[message.id].length || message.size === messageQue[message.id].length) {
|
|
488
|
+
for (const value of Object.values(message.chunk)) {
|
|
489
|
+
messageQue[message.id].push(value);
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
if (message.size === messageQue[message.id].length) {
|
|
494
|
+
pubsub.publish('peer:data', {id: message.id, data: new Uint8Array(Object.values(messageQue[message.id]))});
|
|
495
|
+
delete messageQue[message.id];
|
|
465
496
|
}
|
|
497
|
+
this.bw.down += message.byteLength || message.length;
|
|
498
|
+
|
|
466
499
|
}
|
|
467
500
|
|
|
468
501
|
_sendMessage(message) {
|
|
@@ -626,7 +659,8 @@ class Client {
|
|
|
626
659
|
debug(`star ${id} left`);
|
|
627
660
|
}
|
|
628
661
|
|
|
629
|
-
peerLeft(
|
|
662
|
+
peerLeft(peer) {
|
|
663
|
+
const id = peer.peerId || peer;
|
|
630
664
|
if (this.#connections[id]) {
|
|
631
665
|
this.#connections[id].close();
|
|
632
666
|
delete this.#connections[id];
|
|
@@ -634,7 +668,8 @@ class Client {
|
|
|
634
668
|
debug(`peer ${id} left`);
|
|
635
669
|
}
|
|
636
670
|
|
|
637
|
-
peerJoined(
|
|
671
|
+
peerJoined(peer, signal) {
|
|
672
|
+
const id = peer.peerId || peer;
|
|
638
673
|
if (this.#connections[id]) {
|
|
639
674
|
if (this.#connections[id].connected) this.#connections[id].close();
|
|
640
675
|
delete this.#connections[id];
|
|
@@ -2267,6 +2302,18 @@ class MessageHandler {
|
|
|
2267
2302
|
}
|
|
2268
2303
|
}
|
|
2269
2304
|
|
|
2305
|
+
const dataHandler = async message => {
|
|
2306
|
+
if (!message) return
|
|
2307
|
+
|
|
2308
|
+
const {data, id} = message;
|
|
2309
|
+
|
|
2310
|
+
message = protoFor(data);
|
|
2311
|
+
const proto = protoFor(message.decoded.data);
|
|
2312
|
+
const from = message.decoded.from;
|
|
2313
|
+
|
|
2314
|
+
peernet._protoHandler({id, proto}, peernet.client.connections[from], from);
|
|
2315
|
+
};
|
|
2316
|
+
|
|
2270
2317
|
const encapsulatedError = () => {
|
|
2271
2318
|
return new Error('Nodes/Data should be send encapsulated by peernet-message')
|
|
2272
2319
|
};
|
|
@@ -2476,19 +2523,11 @@ class Peernet {
|
|
|
2476
2523
|
// })
|
|
2477
2524
|
});
|
|
2478
2525
|
|
|
2479
|
-
|
|
2480
|
-
|
|
2481
|
-
|
|
2482
|
-
|
|
2483
|
-
|
|
2484
|
-
uint8Array[i] = data[i];
|
|
2485
|
-
}
|
|
2486
|
-
message = new PeernetMessage(uint8Array);
|
|
2487
|
-
const proto = protoFor(message.decoded.data);
|
|
2488
|
-
|
|
2489
|
-
const from = message.decoded.from;
|
|
2490
|
-
this._protoHandler({id, proto}, this.client.connections[from], from);
|
|
2491
|
-
});
|
|
2526
|
+
/**
|
|
2527
|
+
* converts data -> message -> proto
|
|
2528
|
+
* @see DataHandler
|
|
2529
|
+
*/
|
|
2530
|
+
pubsub.subscribe('peer:data', dataHandler);
|
|
2492
2531
|
|
|
2493
2532
|
/**
|
|
2494
2533
|
* @access public
|
|
@@ -2507,11 +2546,9 @@ class Peernet {
|
|
|
2507
2546
|
|
|
2508
2547
|
sendMessage(peer, id, data) {
|
|
2509
2548
|
if (peer.readyState === 'open') {
|
|
2510
|
-
peer.send(
|
|
2549
|
+
peer.send(data, id);
|
|
2511
2550
|
this.bw.up += data.length;
|
|
2512
|
-
} else if (peer.readyState === 'closed')
|
|
2513
|
-
this.removePeer(peer);
|
|
2514
|
-
}
|
|
2551
|
+
} else if (peer.readyState === 'closed') ;
|
|
2515
2552
|
|
|
2516
2553
|
}
|
|
2517
2554
|
|
|
@@ -10,7 +10,7 @@ function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'defau
|
|
|
10
10
|
var PubSub__default = /*#__PURE__*/_interopDefaultLegacy(PubSub);
|
|
11
11
|
var Koa__default = /*#__PURE__*/_interopDefaultLegacy(Koa);
|
|
12
12
|
|
|
13
|
-
var version = "0.11.
|
|
13
|
+
var version = "0.11.19";
|
|
14
14
|
|
|
15
15
|
var api$1 = {
|
|
16
16
|
version: ({send}) => send({client: '@peernet/api/http', version}),
|
package/dist/commonjs/peernet.js
CHANGED
|
@@ -285,6 +285,8 @@ const socketRequestClient = (url, protocols = 'echo-protocol', options = { retry
|
|
|
285
285
|
});
|
|
286
286
|
};
|
|
287
287
|
|
|
288
|
+
const messageQue = {};
|
|
289
|
+
|
|
288
290
|
class Peer {
|
|
289
291
|
#connection
|
|
290
292
|
#ready = false
|
|
@@ -303,6 +305,7 @@ class Peer {
|
|
|
303
305
|
#iceCompleteTimer
|
|
304
306
|
#channel
|
|
305
307
|
#peerId
|
|
308
|
+
#chunkSize = 16384
|
|
306
309
|
|
|
307
310
|
get connection() {
|
|
308
311
|
return this.#connection
|
|
@@ -354,29 +357,51 @@ class Peer {
|
|
|
354
357
|
return this._socketClient
|
|
355
358
|
}
|
|
356
359
|
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
360
|
+
splitMessage(message) {
|
|
361
|
+
const chunks = [];
|
|
362
|
+
const size = message.byteLength || message.length;
|
|
363
|
+
let offset = 0;
|
|
364
|
+
return new Promise((resolve, reject) => {
|
|
365
|
+
const splitMessage = () => {
|
|
366
|
+
const chunk = message.slice(offset, offset + this.#chunkSize > size ? size : offset + this.#chunkSize);
|
|
367
|
+
offset += offset + this.#chunkSize;
|
|
368
|
+
chunks.push(chunk);
|
|
369
|
+
if (offset < size) return splitMessage()
|
|
370
|
+
else resolve({chunks, size});
|
|
371
|
+
};
|
|
372
|
+
|
|
373
|
+
splitMessage();
|
|
374
|
+
})
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
async send(message, id) {
|
|
378
|
+
const { chunks, size } = await this.splitMessage(message);
|
|
379
|
+
let offset = 0;
|
|
380
|
+
for (const chunk of chunks) {
|
|
381
|
+
const start = offset;
|
|
382
|
+
const end = offset + chunk.length;
|
|
383
|
+
const message = new TextEncoder().encode(JSON.stringify({ size, id, chunk, start, end }));
|
|
384
|
+
switch (this.channel?.readyState) {
|
|
385
|
+
case 'open':
|
|
386
|
+
this.bw.up += message.length || message.byteLength;
|
|
387
|
+
this.channel.send(message);
|
|
388
|
+
break;
|
|
389
|
+
case 'closed':
|
|
390
|
+
case 'closing':
|
|
391
|
+
debug('channel already closed, this usually means a bad implementation, try checking the readyState or check if the peer is connected before sending');
|
|
392
|
+
break;
|
|
393
|
+
case undefined:
|
|
394
|
+
debug(`trying to send before a channel is created`);
|
|
395
|
+
break;
|
|
396
|
+
}
|
|
397
|
+
}
|
|
371
398
|
}
|
|
372
399
|
|
|
373
400
|
request(data) {
|
|
374
401
|
return new Promise((resolve, reject) => {
|
|
375
402
|
const id = Math.random().toString(36).slice(-12);
|
|
376
|
-
|
|
377
|
-
data = new TextEncoder().encode(JSON.stringify({id, data}));
|
|
403
|
+
|
|
378
404
|
const _onData = message => {
|
|
379
|
-
message = JSON.parse(new TextDecoder().decode(message.data));
|
|
380
405
|
if (message.id === id) {
|
|
381
406
|
resolve(message.data);
|
|
382
407
|
pubsub.unsubscribe(`peer:data`, _onData);
|
|
@@ -390,8 +415,8 @@ class Peer {
|
|
|
390
415
|
// pubsub.unsubscribe(`peer:data-request-${id}`, _onData)
|
|
391
416
|
// }, 5000);
|
|
392
417
|
|
|
393
|
-
this.send(data);
|
|
394
|
-
})
|
|
418
|
+
this.send(data, id);
|
|
419
|
+
})
|
|
395
420
|
}
|
|
396
421
|
|
|
397
422
|
async #init() {
|
|
@@ -421,10 +446,7 @@ class Peer {
|
|
|
421
446
|
message.channel.onclose = () => this.close.bind(this);
|
|
422
447
|
|
|
423
448
|
message.channel.onmessage = (message) => {
|
|
424
|
-
|
|
425
|
-
debug(`incoming message from ${this.id}`);
|
|
426
|
-
debug(message);
|
|
427
|
-
this.bw.down += message.length || message.byteLength;
|
|
449
|
+
this._handleMessage(this.id, message);
|
|
428
450
|
};
|
|
429
451
|
this.channel = message.channel;
|
|
430
452
|
};
|
|
@@ -439,28 +461,39 @@ class Peer {
|
|
|
439
461
|
this.channel.onclose = () => this.close.bind(this);
|
|
440
462
|
|
|
441
463
|
this.channel.onmessage = (message) => {
|
|
442
|
-
|
|
443
|
-
debug(`incoming message from ${this.peerId}`);
|
|
444
|
-
this.bw.down += message.length || message.byteLength;
|
|
464
|
+
this._handleMessage(this.peerId, message);
|
|
445
465
|
};
|
|
446
466
|
|
|
447
467
|
const offer = await this.#connection.createOffer();
|
|
448
468
|
await this.#connection.setLocalDescription(offer);
|
|
449
469
|
|
|
450
470
|
this._sendMessage({'sdp': this.#connection.localDescription});
|
|
471
|
+
}
|
|
472
|
+
} catch (e) {
|
|
473
|
+
console.log(e);
|
|
474
|
+
}
|
|
475
|
+
}
|
|
451
476
|
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
await this.#connection.setLocalDescription(offer);
|
|
477
|
+
_handleMessage(peerId, message) {
|
|
478
|
+
debug(`incoming message from ${peerId}`);
|
|
455
479
|
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
480
|
+
message = JSON.parse(new TextDecoder().decode(message.data));
|
|
481
|
+
// allow sharding (multiple peers share data)
|
|
482
|
+
pubsub.publish('peernet:shard', message);
|
|
483
|
+
if (!messageQue[message.id]) messageQue[message.id] = [];
|
|
460
484
|
|
|
461
|
-
|
|
462
|
-
|
|
485
|
+
if (message.size > messageQue[message.id].length || message.size === messageQue[message.id].length) {
|
|
486
|
+
for (const value of Object.values(message.chunk)) {
|
|
487
|
+
messageQue[message.id].push(value);
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
if (message.size === messageQue[message.id].length) {
|
|
492
|
+
pubsub.publish('peer:data', {id: message.id, data: new Uint8Array(Object.values(messageQue[message.id]))});
|
|
493
|
+
delete messageQue[message.id];
|
|
463
494
|
}
|
|
495
|
+
this.bw.down += message.byteLength || message.length;
|
|
496
|
+
|
|
464
497
|
}
|
|
465
498
|
|
|
466
499
|
_sendMessage(message) {
|
|
@@ -624,7 +657,8 @@ class Client {
|
|
|
624
657
|
debug(`star ${id} left`);
|
|
625
658
|
}
|
|
626
659
|
|
|
627
|
-
peerLeft(
|
|
660
|
+
peerLeft(peer) {
|
|
661
|
+
const id = peer.peerId || peer;
|
|
628
662
|
if (this.#connections[id]) {
|
|
629
663
|
this.#connections[id].close();
|
|
630
664
|
delete this.#connections[id];
|
|
@@ -632,7 +666,8 @@ class Client {
|
|
|
632
666
|
debug(`peer ${id} left`);
|
|
633
667
|
}
|
|
634
668
|
|
|
635
|
-
peerJoined(
|
|
669
|
+
peerJoined(peer, signal) {
|
|
670
|
+
const id = peer.peerId || peer;
|
|
636
671
|
if (this.#connections[id]) {
|
|
637
672
|
if (this.#connections[id].connected) this.#connections[id].close();
|
|
638
673
|
delete this.#connections[id];
|
|
@@ -1625,6 +1660,18 @@ class MessageHandler {
|
|
|
1625
1660
|
}
|
|
1626
1661
|
}
|
|
1627
1662
|
|
|
1663
|
+
const dataHandler = async message => {
|
|
1664
|
+
if (!message) return
|
|
1665
|
+
|
|
1666
|
+
const {data, id} = message;
|
|
1667
|
+
|
|
1668
|
+
message = protoFor(data);
|
|
1669
|
+
const proto = protoFor(message.decoded.data);
|
|
1670
|
+
const from = message.decoded.from;
|
|
1671
|
+
|
|
1672
|
+
peernet._protoHandler({id, proto}, peernet.client.connections[from], from);
|
|
1673
|
+
};
|
|
1674
|
+
|
|
1628
1675
|
const encapsulatedError = () => {
|
|
1629
1676
|
return new Error('Nodes/Data should be send encapsulated by peernet-message')
|
|
1630
1677
|
};
|
|
@@ -1803,7 +1850,7 @@ class Peernet {
|
|
|
1803
1850
|
protocol: 'peernet-v0.1.0', host: '127.0.0.1', port: options.port
|
|
1804
1851
|
});
|
|
1805
1852
|
} else {
|
|
1806
|
-
const http = await Promise.resolve().then(function () { return require('./http-
|
|
1853
|
+
const http = await Promise.resolve().then(function () { return require('./http-62c5b155.js'); });
|
|
1807
1854
|
if (environment !== 'browser') http.default(options);
|
|
1808
1855
|
}
|
|
1809
1856
|
|
|
@@ -1842,19 +1889,11 @@ class Peernet {
|
|
|
1842
1889
|
// })
|
|
1843
1890
|
});
|
|
1844
1891
|
|
|
1845
|
-
|
|
1846
|
-
|
|
1847
|
-
|
|
1848
|
-
|
|
1849
|
-
|
|
1850
|
-
uint8Array[i] = data[i];
|
|
1851
|
-
}
|
|
1852
|
-
message = new peernetMessage(uint8Array);
|
|
1853
|
-
const proto = protoFor(message.decoded.data);
|
|
1854
|
-
|
|
1855
|
-
const from = message.decoded.from;
|
|
1856
|
-
this._protoHandler({id, proto}, this.client.connections[from], from);
|
|
1857
|
-
});
|
|
1892
|
+
/**
|
|
1893
|
+
* converts data -> message -> proto
|
|
1894
|
+
* @see DataHandler
|
|
1895
|
+
*/
|
|
1896
|
+
pubsub.subscribe('peer:data', dataHandler);
|
|
1858
1897
|
|
|
1859
1898
|
/**
|
|
1860
1899
|
* @access public
|
|
@@ -1873,11 +1912,9 @@ class Peernet {
|
|
|
1873
1912
|
|
|
1874
1913
|
sendMessage(peer, id, data) {
|
|
1875
1914
|
if (peer.readyState === 'open') {
|
|
1876
|
-
peer.send(
|
|
1915
|
+
peer.send(data, id);
|
|
1877
1916
|
this.bw.up += data.length;
|
|
1878
|
-
} else if (peer.readyState === 'closed')
|
|
1879
|
-
this.removePeer(peer);
|
|
1880
|
-
}
|
|
1917
|
+
} else if (peer.readyState === 'closed') ;
|
|
1881
1918
|
|
|
1882
1919
|
}
|
|
1883
1920
|
|
package/dist/module/peernet.js
CHANGED
|
@@ -241,6 +241,8 @@ const socketRequestClient = (url, protocols = 'echo-protocol', options = { retry
|
|
|
241
241
|
});
|
|
242
242
|
};
|
|
243
243
|
|
|
244
|
+
const messageQue = {};
|
|
245
|
+
|
|
244
246
|
class Peer {
|
|
245
247
|
#connection
|
|
246
248
|
#ready = false
|
|
@@ -259,6 +261,7 @@ class Peer {
|
|
|
259
261
|
#iceCompleteTimer
|
|
260
262
|
#channel
|
|
261
263
|
#peerId
|
|
264
|
+
#chunkSize = 16384
|
|
262
265
|
|
|
263
266
|
get connection() {
|
|
264
267
|
return this.#connection
|
|
@@ -310,29 +313,51 @@ class Peer {
|
|
|
310
313
|
return this._socketClient
|
|
311
314
|
}
|
|
312
315
|
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
316
|
+
splitMessage(message) {
|
|
317
|
+
const chunks = [];
|
|
318
|
+
const size = message.byteLength || message.length;
|
|
319
|
+
let offset = 0;
|
|
320
|
+
return new Promise((resolve, reject) => {
|
|
321
|
+
const splitMessage = () => {
|
|
322
|
+
const chunk = message.slice(offset, offset + this.#chunkSize > size ? size : offset + this.#chunkSize);
|
|
323
|
+
offset += offset + this.#chunkSize;
|
|
324
|
+
chunks.push(chunk);
|
|
325
|
+
if (offset < size) return splitMessage()
|
|
326
|
+
else resolve({chunks, size});
|
|
327
|
+
};
|
|
328
|
+
|
|
329
|
+
splitMessage();
|
|
330
|
+
})
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
async send(message, id) {
|
|
334
|
+
const { chunks, size } = await this.splitMessage(message);
|
|
335
|
+
let offset = 0;
|
|
336
|
+
for (const chunk of chunks) {
|
|
337
|
+
const start = offset;
|
|
338
|
+
const end = offset + chunk.length;
|
|
339
|
+
const message = new TextEncoder().encode(JSON.stringify({ size, id, chunk, start, end }));
|
|
340
|
+
switch (this.channel?.readyState) {
|
|
341
|
+
case 'open':
|
|
342
|
+
this.bw.up += message.length || message.byteLength;
|
|
343
|
+
this.channel.send(message);
|
|
344
|
+
break;
|
|
345
|
+
case 'closed':
|
|
346
|
+
case 'closing':
|
|
347
|
+
debug('channel already closed, this usually means a bad implementation, try checking the readyState or check if the peer is connected before sending');
|
|
348
|
+
break;
|
|
349
|
+
case undefined:
|
|
350
|
+
debug(`trying to send before a channel is created`);
|
|
351
|
+
break;
|
|
352
|
+
}
|
|
353
|
+
}
|
|
327
354
|
}
|
|
328
355
|
|
|
329
356
|
request(data) {
|
|
330
357
|
return new Promise((resolve, reject) => {
|
|
331
358
|
const id = Math.random().toString(36).slice(-12);
|
|
332
|
-
|
|
333
|
-
data = new TextEncoder().encode(JSON.stringify({id, data}));
|
|
359
|
+
|
|
334
360
|
const _onData = message => {
|
|
335
|
-
message = JSON.parse(new TextDecoder().decode(message.data));
|
|
336
361
|
if (message.id === id) {
|
|
337
362
|
resolve(message.data);
|
|
338
363
|
pubsub.unsubscribe(`peer:data`, _onData);
|
|
@@ -346,8 +371,8 @@ class Peer {
|
|
|
346
371
|
// pubsub.unsubscribe(`peer:data-request-${id}`, _onData)
|
|
347
372
|
// }, 5000);
|
|
348
373
|
|
|
349
|
-
this.send(data);
|
|
350
|
-
})
|
|
374
|
+
this.send(data, id);
|
|
375
|
+
})
|
|
351
376
|
}
|
|
352
377
|
|
|
353
378
|
async #init() {
|
|
@@ -377,10 +402,7 @@ class Peer {
|
|
|
377
402
|
message.channel.onclose = () => this.close.bind(this);
|
|
378
403
|
|
|
379
404
|
message.channel.onmessage = (message) => {
|
|
380
|
-
|
|
381
|
-
debug(`incoming message from ${this.id}`);
|
|
382
|
-
debug(message);
|
|
383
|
-
this.bw.down += message.length || message.byteLength;
|
|
405
|
+
this._handleMessage(this.id, message);
|
|
384
406
|
};
|
|
385
407
|
this.channel = message.channel;
|
|
386
408
|
};
|
|
@@ -395,28 +417,39 @@ class Peer {
|
|
|
395
417
|
this.channel.onclose = () => this.close.bind(this);
|
|
396
418
|
|
|
397
419
|
this.channel.onmessage = (message) => {
|
|
398
|
-
|
|
399
|
-
debug(`incoming message from ${this.peerId}`);
|
|
400
|
-
this.bw.down += message.length || message.byteLength;
|
|
420
|
+
this._handleMessage(this.peerId, message);
|
|
401
421
|
};
|
|
402
422
|
|
|
403
423
|
const offer = await this.#connection.createOffer();
|
|
404
424
|
await this.#connection.setLocalDescription(offer);
|
|
405
425
|
|
|
406
426
|
this._sendMessage({'sdp': this.#connection.localDescription});
|
|
427
|
+
}
|
|
428
|
+
} catch (e) {
|
|
429
|
+
console.log(e);
|
|
430
|
+
}
|
|
431
|
+
}
|
|
407
432
|
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
await this.#connection.setLocalDescription(offer);
|
|
433
|
+
_handleMessage(peerId, message) {
|
|
434
|
+
debug(`incoming message from ${peerId}`);
|
|
411
435
|
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
436
|
+
message = JSON.parse(new TextDecoder().decode(message.data));
|
|
437
|
+
// allow sharding (multiple peers share data)
|
|
438
|
+
pubsub.publish('peernet:shard', message);
|
|
439
|
+
if (!messageQue[message.id]) messageQue[message.id] = [];
|
|
416
440
|
|
|
417
|
-
|
|
418
|
-
|
|
441
|
+
if (message.size > messageQue[message.id].length || message.size === messageQue[message.id].length) {
|
|
442
|
+
for (const value of Object.values(message.chunk)) {
|
|
443
|
+
messageQue[message.id].push(value);
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
if (message.size === messageQue[message.id].length) {
|
|
448
|
+
pubsub.publish('peer:data', {id: message.id, data: new Uint8Array(Object.values(messageQue[message.id]))});
|
|
449
|
+
delete messageQue[message.id];
|
|
419
450
|
}
|
|
451
|
+
this.bw.down += message.byteLength || message.length;
|
|
452
|
+
|
|
420
453
|
}
|
|
421
454
|
|
|
422
455
|
_sendMessage(message) {
|
|
@@ -580,7 +613,8 @@ class Client {
|
|
|
580
613
|
debug(`star ${id} left`);
|
|
581
614
|
}
|
|
582
615
|
|
|
583
|
-
peerLeft(
|
|
616
|
+
peerLeft(peer) {
|
|
617
|
+
const id = peer.peerId || peer;
|
|
584
618
|
if (this.#connections[id]) {
|
|
585
619
|
this.#connections[id].close();
|
|
586
620
|
delete this.#connections[id];
|
|
@@ -588,7 +622,8 @@ class Client {
|
|
|
588
622
|
debug(`peer ${id} left`);
|
|
589
623
|
}
|
|
590
624
|
|
|
591
|
-
peerJoined(
|
|
625
|
+
peerJoined(peer, signal) {
|
|
626
|
+
const id = peer.peerId || peer;
|
|
592
627
|
if (this.#connections[id]) {
|
|
593
628
|
if (this.#connections[id].connected) this.#connections[id].close();
|
|
594
629
|
delete this.#connections[id];
|
|
@@ -2221,6 +2256,18 @@ class MessageHandler {
|
|
|
2221
2256
|
}
|
|
2222
2257
|
}
|
|
2223
2258
|
|
|
2259
|
+
const dataHandler = async message => {
|
|
2260
|
+
if (!message) return
|
|
2261
|
+
|
|
2262
|
+
const {data, id} = message;
|
|
2263
|
+
|
|
2264
|
+
message = protoFor(data);
|
|
2265
|
+
const proto = protoFor(message.decoded.data);
|
|
2266
|
+
const from = message.decoded.from;
|
|
2267
|
+
|
|
2268
|
+
peernet._protoHandler({id, proto}, peernet.client.connections[from], from);
|
|
2269
|
+
};
|
|
2270
|
+
|
|
2224
2271
|
const encapsulatedError = () => {
|
|
2225
2272
|
return new Error('Nodes/Data should be send encapsulated by peernet-message')
|
|
2226
2273
|
};
|
|
@@ -2430,19 +2477,11 @@ class Peernet {
|
|
|
2430
2477
|
// })
|
|
2431
2478
|
});
|
|
2432
2479
|
|
|
2433
|
-
|
|
2434
|
-
|
|
2435
|
-
|
|
2436
|
-
|
|
2437
|
-
|
|
2438
|
-
uint8Array[i] = data[i];
|
|
2439
|
-
}
|
|
2440
|
-
message = new PeernetMessage(uint8Array);
|
|
2441
|
-
const proto = protoFor(message.decoded.data);
|
|
2442
|
-
|
|
2443
|
-
const from = message.decoded.from;
|
|
2444
|
-
this._protoHandler({id, proto}, this.client.connections[from], from);
|
|
2445
|
-
});
|
|
2480
|
+
/**
|
|
2481
|
+
* converts data -> message -> proto
|
|
2482
|
+
* @see DataHandler
|
|
2483
|
+
*/
|
|
2484
|
+
pubsub.subscribe('peer:data', dataHandler);
|
|
2446
2485
|
|
|
2447
2486
|
/**
|
|
2448
2487
|
* @access public
|
|
@@ -2461,11 +2500,9 @@ class Peernet {
|
|
|
2461
2500
|
|
|
2462
2501
|
sendMessage(peer, id, data) {
|
|
2463
2502
|
if (peer.readyState === 'open') {
|
|
2464
|
-
peer.send(
|
|
2503
|
+
peer.send(data, id);
|
|
2465
2504
|
this.bw.up += data.length;
|
|
2466
|
-
} else if (peer.readyState === 'closed')
|
|
2467
|
-
this.removePeer(peer);
|
|
2468
|
-
}
|
|
2505
|
+
} else if (peer.readyState === 'closed') ;
|
|
2469
2506
|
|
|
2470
2507
|
}
|
|
2471
2508
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@leofcoin/peernet",
|
|
3
|
-
"version": "0.11.
|
|
3
|
+
"version": "0.11.20",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "dist/commonjs/peernet.js",
|
|
6
6
|
"module": "dist/module/peernet.js",
|
|
@@ -25,7 +25,7 @@
|
|
|
25
25
|
"dependencies": {
|
|
26
26
|
"@leofcoin/generate-account": "^1.0.2",
|
|
27
27
|
"@leofcoin/multi-wallet": "^2.1.2",
|
|
28
|
-
"@leofcoin/peernet-swarm": "^0.1.
|
|
28
|
+
"@leofcoin/peernet-swarm": "^0.1.21",
|
|
29
29
|
"@leofcoin/storage": "^2.3.0",
|
|
30
30
|
"@vandeurenglenn/base32": "^1.1.0",
|
|
31
31
|
"@vandeurenglenn/base58": "^1.1.0",
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { protoFor } from './../utils/utils.js'
|
|
2
|
+
|
|
3
|
+
const dataHandler = async message => {
|
|
4
|
+
if (!message) return
|
|
5
|
+
|
|
6
|
+
const {data, id} = message
|
|
7
|
+
|
|
8
|
+
message = protoFor(data)
|
|
9
|
+
const proto = protoFor(message.decoded.data)
|
|
10
|
+
const from = message.decoded.from
|
|
11
|
+
|
|
12
|
+
peernet._protoHandler({id, proto}, peernet.client.connections[from], from)
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export default dataHandler
|
package/src/peernet.js
CHANGED
|
@@ -20,6 +20,7 @@ import codecs from './codec/codecs'
|
|
|
20
20
|
import { protoFor, target } from './utils/utils.js'
|
|
21
21
|
import generateAccount from './../node_modules/@leofcoin/generate-account/dist/module/generate-account'
|
|
22
22
|
import MessageHandler from './handlers/message.js'
|
|
23
|
+
import dataHandler from './handlers/data.js'
|
|
23
24
|
import { encapsulatedError, dhtError,
|
|
24
25
|
nothingFoundError } from './errors/errors.js'
|
|
25
26
|
|
|
@@ -220,19 +221,11 @@ export default class Peernet {
|
|
|
220
221
|
// })
|
|
221
222
|
})
|
|
222
223
|
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
uint8Array[i] = data[i]
|
|
229
|
-
}
|
|
230
|
-
message = new PeernetMessage(uint8Array)
|
|
231
|
-
const proto = protoFor(message.decoded.data)
|
|
232
|
-
|
|
233
|
-
const from = message.decoded.from
|
|
234
|
-
this._protoHandler({id, proto}, this.client.connections[from], from)
|
|
235
|
-
})
|
|
224
|
+
/**
|
|
225
|
+
* converts data -> message -> proto
|
|
226
|
+
* @see DataHandler
|
|
227
|
+
*/
|
|
228
|
+
pubsub.subscribe('peer:data', dataHandler)
|
|
236
229
|
|
|
237
230
|
/**
|
|
238
231
|
* @access public
|
|
@@ -251,10 +244,10 @@ export default class Peernet {
|
|
|
251
244
|
|
|
252
245
|
sendMessage(peer, id, data) {
|
|
253
246
|
if (peer.readyState === 'open') {
|
|
254
|
-
peer.send(
|
|
247
|
+
peer.send(data, id)
|
|
255
248
|
this.bw.up += data.length
|
|
256
249
|
} else if (peer.readyState === 'closed') {
|
|
257
|
-
this.removePeer(peer)
|
|
250
|
+
// this.removePeer(peer)
|
|
258
251
|
}
|
|
259
252
|
|
|
260
253
|
}
|