@leofcoin/peernet 0.11.7 → 0.11.10
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/326.peernet.js +29 -1
- package/dist/browser/peernet.js +85137 -2
- package/dist/commonjs/{http-906864ff.js → http-e330b910.js} +1 -1
- package/dist/commonjs/peernet.js +72 -69
- package/dist/module/peernet.js +71 -68
- package/package.json +3 -2
- package/src/peernet.js +29 -28
- package/test.js +7 -0
- package/test4.js +7 -0
- package/webpack.config.js +1 -1
- package/dist/browser/peernet.js.LICENSE.txt +0 -40
package/dist/module/peernet.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import '@vandeurenglenn/debug';
|
|
1
2
|
import LeofcoinStorage from '@leofcoin/storage';
|
|
2
3
|
import protons from 'protons';
|
|
3
4
|
import bs32 from '@vandeurenglenn/base32';
|
|
@@ -264,6 +265,7 @@ class Peer {
|
|
|
264
265
|
#senderMap = new Map()
|
|
265
266
|
#iceCompleteTimer
|
|
266
267
|
#channel
|
|
268
|
+
#peerId
|
|
267
269
|
|
|
268
270
|
get connection() {
|
|
269
271
|
return this.#connection
|
|
@@ -290,12 +292,17 @@ constructor(options = {}) {
|
|
|
290
292
|
down: 0
|
|
291
293
|
};
|
|
292
294
|
|
|
293
|
-
this.channelName = options.channelName
|
|
294
|
-
|
|
295
|
+
this.channelName = options.channelName;
|
|
296
|
+
|
|
297
|
+
this.#peerId = options.peerId;
|
|
295
298
|
this.options = options;
|
|
296
299
|
this.#init();
|
|
297
300
|
}
|
|
298
301
|
|
|
302
|
+
get peerId() {
|
|
303
|
+
return this.#peerId
|
|
304
|
+
}
|
|
305
|
+
|
|
299
306
|
set socketClient(value) {
|
|
300
307
|
// this.socketClient?.pubsub.unsubscribe('signal', this._in)
|
|
301
308
|
this._socketClient = value;
|
|
@@ -307,28 +314,29 @@ constructor(options = {}) {
|
|
|
307
314
|
}
|
|
308
315
|
|
|
309
316
|
send(message) {
|
|
310
|
-
this.bw.up += message.length;
|
|
317
|
+
this.bw.up += message.length || message.byteLength;
|
|
311
318
|
this.channel.send(message);
|
|
312
319
|
}
|
|
313
320
|
|
|
314
321
|
request(data) {
|
|
315
322
|
return new Promise((resolve, reject) => {
|
|
316
323
|
const id = Math.random().toString(36).slice(-12);
|
|
324
|
+
// TODO: get rid of JSON
|
|
317
325
|
data = new TextEncoder().encode(JSON.stringify({id, data}));
|
|
318
326
|
const _onData = message => {
|
|
319
327
|
message = JSON.parse(new TextDecoder().decode(message.data));
|
|
320
328
|
if (message.id === id) {
|
|
321
329
|
resolve(message.data);
|
|
322
|
-
pubsub.unsubscribe(
|
|
330
|
+
pubsub.unsubscribe(`peer:data`, _onData);
|
|
323
331
|
}
|
|
324
332
|
};
|
|
325
333
|
|
|
326
|
-
pubsub.subscribe(
|
|
334
|
+
pubsub.subscribe(`peer:data`, _onData);
|
|
327
335
|
|
|
328
336
|
// cleanup subscriptions
|
|
329
|
-
setTimeout(() => {
|
|
330
|
-
|
|
331
|
-
}, 5000);
|
|
337
|
+
// setTimeout(() => {
|
|
338
|
+
// pubsub.unsubscribe(`peer:data-request-${id}`, _onData)
|
|
339
|
+
// }, 5000);
|
|
332
340
|
|
|
333
341
|
this.send(data);
|
|
334
342
|
});
|
|
@@ -357,12 +365,13 @@ constructor(options = {}) {
|
|
|
357
365
|
this.#connected = true;
|
|
358
366
|
pubsub.publish('peer:connected', this);
|
|
359
367
|
};
|
|
360
|
-
message.channel.onclose = () =>
|
|
368
|
+
message.channel.onclose = () => this.close.bind(this);
|
|
369
|
+
|
|
361
370
|
message.channel.onmessage = (message) => {
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
this.bw.down += message.length;
|
|
371
|
+
pubsub.publish('peer:data', message);
|
|
372
|
+
debug(`incoming message from ${this.id}`);
|
|
373
|
+
debug(message);
|
|
374
|
+
this.bw.down += message.length || message.byteLength;
|
|
366
375
|
};
|
|
367
376
|
this.channel = message.channel;
|
|
368
377
|
};
|
|
@@ -377,10 +386,10 @@ constructor(options = {}) {
|
|
|
377
386
|
this.channel.onclose = () => this.close.bind(this);
|
|
378
387
|
|
|
379
388
|
this.channel.onmessage = (message) => {
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
this.bw.down += message.length;
|
|
389
|
+
pubsub.publish('peer:data', message);
|
|
390
|
+
debug(`incoming message from ${this.peerId}`);
|
|
391
|
+
debug(message);
|
|
392
|
+
this.bw.down += message.length || message.byteLength;
|
|
384
393
|
};
|
|
385
394
|
|
|
386
395
|
const offer = await this.#connection.createOffer();
|
|
@@ -410,6 +419,8 @@ constructor(options = {}) {
|
|
|
410
419
|
// if (data.videocall) return this._startStream(true, false); // start video and audio stream
|
|
411
420
|
// if (data.call) return this._startStream(true, true); // start audio stream
|
|
412
421
|
if (message.candidate) {
|
|
422
|
+
debug(`incoming candidate ${this.channelName}`);
|
|
423
|
+
debug(message.candidate.candidate);
|
|
413
424
|
this.remoteAddress = message.candidate.address;
|
|
414
425
|
this.remotePort = message.candidate.port;
|
|
415
426
|
this.remoteProtocol = message.candidate.protocol;
|
|
@@ -419,12 +430,14 @@ constructor(options = {}) {
|
|
|
419
430
|
try {
|
|
420
431
|
if (message.sdp) {
|
|
421
432
|
if (message.sdp.type === 'offer') {
|
|
433
|
+
debug(`incoming offer ${this.channelName}`);
|
|
422
434
|
await this.#connection.setRemoteDescription(new wrtc.RTCSessionDescription(message.sdp));
|
|
423
435
|
const answer = await this.#connection.createAnswer();
|
|
424
436
|
await this.#connection.setLocalDescription(answer);
|
|
425
437
|
this._sendMessage({'sdp': this.#connection.localDescription});
|
|
426
438
|
}
|
|
427
439
|
if (message.sdp.type === 'answer') {
|
|
440
|
+
debug(`incoming answer ${this.channelName}`);
|
|
428
441
|
await this.#connection.setRemoteDescription(new wrtc.RTCSessionDescription(message.sdp));
|
|
429
442
|
}
|
|
430
443
|
}
|
|
@@ -434,6 +447,7 @@ constructor(options = {}) {
|
|
|
434
447
|
}
|
|
435
448
|
|
|
436
449
|
close() {
|
|
450
|
+
debug(`closing ${this.peerId}`);
|
|
437
451
|
this.channel?.close();
|
|
438
452
|
this.#connection?.close();
|
|
439
453
|
|
|
@@ -464,7 +478,6 @@ class Client {
|
|
|
464
478
|
async _init(identifiers, stars = []) {
|
|
465
479
|
if (stars.length === 0) {
|
|
466
480
|
stars.push('wss://star.leofcoin.org');
|
|
467
|
-
stars.push('ws://localhost:44444');
|
|
468
481
|
}
|
|
469
482
|
this.identifiers = identifiers;
|
|
470
483
|
this.starsConfig = stars;
|
|
@@ -483,17 +496,9 @@ class Client {
|
|
|
483
496
|
}
|
|
484
497
|
const peers = await this.socketClient.peernet.join({id: this.id});
|
|
485
498
|
for (const id of peers) {
|
|
486
|
-
if (id !== this.id && !this.#connections[id]) this.#connections[id] = new Peer({channelName: `${id}:${this.id}`, socketClient: this.socketClient, id: this.id, to: id});
|
|
499
|
+
if (id !== this.id && !this.#connections[id]) this.#connections[id] = new Peer({channelName: `${id}:${this.id}`, socketClient: this.socketClient, id: this.id, to: id, peerId: id});
|
|
487
500
|
}
|
|
488
501
|
this.setupListeners();
|
|
489
|
-
|
|
490
|
-
pubsub.subscribe('peer:connected', (peer) => {
|
|
491
|
-
// peer.send(JSON.stringify({data: 'hello', from: this.id, to: peer.to}))
|
|
492
|
-
console.log({peer: peer.to});
|
|
493
|
-
console.log({id: peer.id});
|
|
494
|
-
console.log({id: this.id});
|
|
495
|
-
});
|
|
496
|
-
// pubsub.subscribe('peer:data', (data) => console.log({data}))
|
|
497
502
|
}
|
|
498
503
|
|
|
499
504
|
setupListeners() {
|
|
@@ -542,7 +547,7 @@ class Client {
|
|
|
542
547
|
delete this.#connections[id];
|
|
543
548
|
}
|
|
544
549
|
// reconnect
|
|
545
|
-
if (id !== this.id) this.#connections[id] = new Peer({channelName: `${id}:${this.id}`, socketClient: this.socketClient, id: this.id, to: id});
|
|
550
|
+
if (id !== this.id) this.#connections[id] = new Peer({channelName: `${id}:${this.id}`, socketClient: this.socketClient, id: this.id, to: id, peerId: id});
|
|
546
551
|
}
|
|
547
552
|
|
|
548
553
|
}
|
|
@@ -552,10 +557,7 @@ class Client {
|
|
|
552
557
|
}
|
|
553
558
|
}
|
|
554
559
|
}
|
|
555
|
-
|
|
556
|
-
console.log(`star ${id} left`);
|
|
557
|
-
|
|
558
|
-
|
|
560
|
+
debug(`star ${id} left`);
|
|
559
561
|
}
|
|
560
562
|
|
|
561
563
|
peerLeft(id) {
|
|
@@ -563,7 +565,7 @@ class Client {
|
|
|
563
565
|
this.#connections[id].close();
|
|
564
566
|
delete this.#connections[id];
|
|
565
567
|
}
|
|
566
|
-
|
|
568
|
+
debug(`peer ${id} left`);
|
|
567
569
|
}
|
|
568
570
|
|
|
569
571
|
peerJoined(id, signal) {
|
|
@@ -572,8 +574,8 @@ class Client {
|
|
|
572
574
|
delete this.#connections[id];
|
|
573
575
|
}
|
|
574
576
|
// RTCPeerConnection
|
|
575
|
-
this.#connections[id] = new Peer({initiator: true, channelName: `${this.id}:${id}`, socketClient: this.socketClient, id: this.id, to: id});
|
|
576
|
-
|
|
577
|
+
this.#connections[id] = new Peer({initiator: true, channelName: `${this.id}:${id}`, socketClient: this.socketClient, id: this.id, to: id, peerId: id});
|
|
578
|
+
debug(`peer ${id} joined`);
|
|
577
579
|
}
|
|
578
580
|
|
|
579
581
|
|
|
@@ -1371,7 +1373,7 @@ class ChatMessage extends FormatInterface {
|
|
|
1371
1373
|
}
|
|
1372
1374
|
}
|
|
1373
1375
|
|
|
1374
|
-
const debug = (log) => {
|
|
1376
|
+
const debug$1 = (log) => {
|
|
1375
1377
|
if (globalThis.DEBUG || globalThis.debug) console.log(`%c ${log}`, 'color: #0080ff;');
|
|
1376
1378
|
};
|
|
1377
1379
|
|
|
@@ -2308,9 +2310,9 @@ class Peernet {
|
|
|
2308
2310
|
* @param {PeernetPeer} peer - peernet peer
|
|
2309
2311
|
*/
|
|
2310
2312
|
async _protoHandler(message, peer, from) {
|
|
2313
|
+
|
|
2311
2314
|
const {id, proto} = message;
|
|
2312
2315
|
this.bw.down += proto.encoded.length;
|
|
2313
|
-
|
|
2314
2316
|
if (proto.name === 'peernet-dht') {
|
|
2315
2317
|
let { hash, store } = proto.decoded;
|
|
2316
2318
|
let has;
|
|
@@ -2358,9 +2360,8 @@ class Peernet {
|
|
|
2358
2360
|
|
|
2359
2361
|
this.bw.up += node.encoded.length;
|
|
2360
2362
|
}
|
|
2361
|
-
} else if (proto.name === 'peernet-ps' &&
|
|
2362
|
-
|
|
2363
|
-
globalSub.publish(proto.decoded.topic.toString(), proto.decoded.data.toString());
|
|
2363
|
+
} else if (proto.name === 'peernet-ps' && peer.peerId !== this.id) {
|
|
2364
|
+
globalSub.publish(new TextDecoder().decode(proto.decoded.topic), proto.decoded.data);
|
|
2364
2365
|
}
|
|
2365
2366
|
// }
|
|
2366
2367
|
}
|
|
@@ -2374,17 +2375,14 @@ class Peernet {
|
|
|
2374
2375
|
if (!hash) throw new Error('hash expected, received undefined')
|
|
2375
2376
|
const data = new DHTMessage({hash});
|
|
2376
2377
|
this.client.id;
|
|
2377
|
-
|
|
2378
|
-
const node = await this.prepareMessage(peer.
|
|
2379
|
-
|
|
2380
|
-
|
|
2381
|
-
|
|
2382
|
-
let proto = protoFor(result.data);
|
|
2383
|
-
|
|
2378
|
+
const walk = async peer => {
|
|
2379
|
+
const node = await this.prepareMessage(peer.peerId, data.encoded);
|
|
2380
|
+
let result = await peer.request(node.encoded);
|
|
2381
|
+
result = new Uint8Array(Object.values(result));
|
|
2382
|
+
let proto = protoFor(result);
|
|
2384
2383
|
if (proto.name !== 'peernet-message') throw encapsulatedError()
|
|
2385
2384
|
const from = proto.decoded.from;
|
|
2386
2385
|
proto = protoFor(proto.decoded.data);
|
|
2387
|
-
|
|
2388
2386
|
if (proto.name !== 'peernet-dht-response') throw dhtError(proto.name)
|
|
2389
2387
|
|
|
2390
2388
|
// TODO: give ip and port (just used for location)
|
|
@@ -2402,8 +2400,14 @@ class Peernet {
|
|
|
2402
2400
|
};
|
|
2403
2401
|
|
|
2404
2402
|
if (proto.decoded.has) this.dht.addProvider(peerInfo, proto.decoded.hash);
|
|
2403
|
+
};
|
|
2404
|
+
let walks = [];
|
|
2405
|
+
for (const peer of this.connections) {
|
|
2406
|
+
if (peer.peerId !== this.id) {
|
|
2407
|
+
walks.push(walk(peer));
|
|
2408
|
+
}
|
|
2405
2409
|
}
|
|
2406
|
-
return
|
|
2410
|
+
return Promise.all(walks)
|
|
2407
2411
|
}
|
|
2408
2412
|
|
|
2409
2413
|
/**
|
|
@@ -2465,34 +2469,33 @@ class Peernet {
|
|
|
2465
2469
|
async requestData(hash, store) {
|
|
2466
2470
|
const providers = await this.providersFor(hash);
|
|
2467
2471
|
if (!providers || providers.size === 0) throw nothingFoundError(hash)
|
|
2468
|
-
debug(`found ${providers.size} provider(s) for ${hash}`);
|
|
2472
|
+
debug$1(`found ${providers.size} provider(s) for ${hash}`);
|
|
2469
2473
|
// get closest peer on earth
|
|
2470
2474
|
const closestPeer = await this.dht.closestPeer(providers);
|
|
2471
2475
|
// get peer instance by id
|
|
2472
|
-
if (!closestPeer || !closestPeer.id) return this.requestData(hash, store
|
|
2476
|
+
if (!closestPeer || !closestPeer.id) return this.requestData(hash, store?.name ? store?.name : store)
|
|
2473
2477
|
|
|
2474
|
-
const id = closestPeer.id
|
|
2478
|
+
const id = closestPeer.id;
|
|
2475
2479
|
if (this.connections) {
|
|
2476
2480
|
let closest = this.connections.filter((peer) => {
|
|
2477
|
-
if (peer.
|
|
2481
|
+
if (peer.peerId === id) return peer
|
|
2478
2482
|
});
|
|
2479
2483
|
|
|
2480
|
-
let data = new DataMessage({hash, store: store
|
|
2484
|
+
let data = new DataMessage({hash, store: store?.name ? store?.name : store});
|
|
2481
2485
|
|
|
2482
2486
|
const node = await this.prepareMessage(id, data.encoded);
|
|
2483
2487
|
if (closest[0]) data = await closest[0].request(node.encoded);
|
|
2484
2488
|
else {
|
|
2485
2489
|
closest = this.connections.filter((peer) => {
|
|
2486
|
-
if (peer.
|
|
2490
|
+
if (peer.peerId === id) return peer
|
|
2487
2491
|
});
|
|
2488
2492
|
if (closest[0]) data = await closest[0].request(node.encoded);
|
|
2489
2493
|
}
|
|
2490
|
-
|
|
2491
|
-
|
|
2492
|
-
|
|
2493
|
-
|
|
2494
|
-
|
|
2495
|
-
}
|
|
2494
|
+
data = new Uint8Array(Object.values(data));
|
|
2495
|
+
let proto = protoFor(data);
|
|
2496
|
+
proto = protoFor(proto.decoded.data);
|
|
2497
|
+
// TODO: store data automaticly or not
|
|
2498
|
+
return proto.decoded.data
|
|
2496
2499
|
|
|
2497
2500
|
// this.put(hash, proto.decoded.data)
|
|
2498
2501
|
}
|
|
@@ -2508,7 +2511,7 @@ class Peernet {
|
|
|
2508
2511
|
* @param {String} hash
|
|
2509
2512
|
*/
|
|
2510
2513
|
get: async (hash) => {
|
|
2511
|
-
debug(`get message ${hash}`);
|
|
2514
|
+
debug$1(`get message ${hash}`);
|
|
2512
2515
|
const message = await messageStore.has(hash);
|
|
2513
2516
|
if (message) return await messageStore.get(hash)
|
|
2514
2517
|
return this.requestData(hash, 'message')
|
|
@@ -2536,7 +2539,7 @@ class Peernet {
|
|
|
2536
2539
|
* @param {String} hash
|
|
2537
2540
|
*/
|
|
2538
2541
|
get: async (hash) => {
|
|
2539
|
-
debug(`get data ${hash}`);
|
|
2542
|
+
debug$1(`get data ${hash}`);
|
|
2540
2543
|
const data = await dataStore.has(hash);
|
|
2541
2544
|
if (data) return await dataStore.get(hash)
|
|
2542
2545
|
return this.requestData(hash, 'data')
|
|
@@ -2578,14 +2581,14 @@ class Peernet {
|
|
|
2578
2581
|
* @param {String} store - storeName to access
|
|
2579
2582
|
*/
|
|
2580
2583
|
async get(hash, store) {
|
|
2581
|
-
debug(`get ${hash}`);
|
|
2584
|
+
debug$1(`get ${hash}`);
|
|
2582
2585
|
let data;
|
|
2583
2586
|
if (store) store = globalThis[`${store}Store`];
|
|
2584
2587
|
if (!store) store = await this.whichStore([...this.stores], hash);
|
|
2585
2588
|
if (store && await store.has(hash)) data = await store.get(hash);
|
|
2586
2589
|
if (data) return data
|
|
2587
2590
|
|
|
2588
|
-
return this.requestData(hash, store
|
|
2591
|
+
return this.requestData(hash, store?.name ? store.name : store)
|
|
2589
2592
|
}
|
|
2590
2593
|
|
|
2591
2594
|
/**
|
|
@@ -2626,8 +2629,8 @@ class Peernet {
|
|
|
2626
2629
|
data = new PsMessage({data, topic});
|
|
2627
2630
|
for (const peer of this.connections) {
|
|
2628
2631
|
if (peer.connected) {
|
|
2629
|
-
if (peer.
|
|
2630
|
-
const node = await this.prepareMessage(peer.
|
|
2632
|
+
if (peer.peerId !== this.peerId) {
|
|
2633
|
+
const node = await this.prepareMessage(peer.peerId, data.encoded);
|
|
2631
2634
|
peer.send(new TextEncoder().encode(JSON.stringify({id, data: node.encoded})));
|
|
2632
2635
|
}
|
|
2633
2636
|
} else {
|
|
@@ -2652,7 +2655,7 @@ class Peernet {
|
|
|
2652
2655
|
}
|
|
2653
2656
|
|
|
2654
2657
|
async removePeer(peer) {
|
|
2655
|
-
delete this.client.connections[peer.
|
|
2658
|
+
delete this.client.connections[peer.peerId];
|
|
2656
2659
|
}
|
|
2657
2660
|
|
|
2658
2661
|
get Buffer() {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@leofcoin/peernet",
|
|
3
|
-
"version": "0.11.
|
|
3
|
+
"version": "0.11.10",
|
|
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.16",
|
|
29
29
|
"@leofcoin/storage": "^2.3.0",
|
|
30
30
|
"@vandeurenglenn/base32": "^1.1.0",
|
|
31
31
|
"@vandeurenglenn/base58": "^1.1.0",
|
|
@@ -46,6 +46,7 @@
|
|
|
46
46
|
"@rollup/plugin-eslint": "^8.0.1",
|
|
47
47
|
"@rollup/plugin-json": "^4.1.0",
|
|
48
48
|
"@rollup/plugin-node-resolve": "^13.0.4",
|
|
49
|
+
"@vandeurenglenn/debug": "^1.0.0",
|
|
49
50
|
"coveralls": "^3.1.1",
|
|
50
51
|
"esdoc": "^1.1.0",
|
|
51
52
|
"esdoc-ecmascript-proposal-plugin": "^1.0.0",
|
package/src/peernet.js
CHANGED
|
@@ -257,9 +257,9 @@ export default class Peernet {
|
|
|
257
257
|
* @param {PeernetPeer} peer - peernet peer
|
|
258
258
|
*/
|
|
259
259
|
async _protoHandler(message, peer, from) {
|
|
260
|
+
|
|
260
261
|
const {id, proto} = message
|
|
261
262
|
this.bw.down += proto.encoded.length
|
|
262
|
-
|
|
263
263
|
if (proto.name === 'peernet-dht') {
|
|
264
264
|
let { hash, store } = proto.decoded
|
|
265
265
|
let has;
|
|
@@ -309,9 +309,8 @@ export default class Peernet {
|
|
|
309
309
|
|
|
310
310
|
this.bw.up += node.encoded.length
|
|
311
311
|
}
|
|
312
|
-
} else if (proto.name === 'peernet-ps' &&
|
|
313
|
-
|
|
314
|
-
globalSub.publish(proto.decoded.topic.toString(), proto.decoded.data.toString())
|
|
312
|
+
} else if (proto.name === 'peernet-ps' && peer.peerId !== this.id) {
|
|
313
|
+
globalSub.publish(new TextDecoder().decode(proto.decoded.topic), proto.decoded.data)
|
|
315
314
|
}
|
|
316
315
|
// }
|
|
317
316
|
}
|
|
@@ -325,17 +324,14 @@ export default class Peernet {
|
|
|
325
324
|
if (!hash) throw new Error('hash expected, received undefined')
|
|
326
325
|
const data = new DHTMessage({hash})
|
|
327
326
|
const clientId = this.client.id
|
|
328
|
-
|
|
329
|
-
const node = await this.prepareMessage(peer.
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
let proto = protoFor(result.data)
|
|
334
|
-
|
|
327
|
+
const walk = async peer => {
|
|
328
|
+
const node = await this.prepareMessage(peer.peerId, data.encoded)
|
|
329
|
+
let result = await peer.request(node.encoded)
|
|
330
|
+
result = new Uint8Array(Object.values(result))
|
|
331
|
+
let proto = protoFor(result)
|
|
335
332
|
if (proto.name !== 'peernet-message') throw encapsulatedError()
|
|
336
333
|
const from = proto.decoded.from
|
|
337
334
|
proto = protoFor(proto.decoded.data)
|
|
338
|
-
|
|
339
335
|
if (proto.name !== 'peernet-dht-response') throw dhtError(proto.name)
|
|
340
336
|
|
|
341
337
|
// TODO: give ip and port (just used for location)
|
|
@@ -354,7 +350,13 @@ export default class Peernet {
|
|
|
354
350
|
|
|
355
351
|
if (proto.decoded.has) this.dht.addProvider(peerInfo, proto.decoded.hash)
|
|
356
352
|
}
|
|
357
|
-
|
|
353
|
+
let walks = []
|
|
354
|
+
for (const peer of this.connections) {
|
|
355
|
+
if (peer.peerId !== this.id) {
|
|
356
|
+
walks.push(walk(peer))
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
return Promise.all(walks)
|
|
358
360
|
}
|
|
359
361
|
|
|
360
362
|
/**
|
|
@@ -420,30 +422,29 @@ export default class Peernet {
|
|
|
420
422
|
// get closest peer on earth
|
|
421
423
|
const closestPeer = await this.dht.closestPeer(providers)
|
|
422
424
|
// get peer instance by id
|
|
423
|
-
if (!closestPeer || !closestPeer.id) return this.requestData(hash, store
|
|
425
|
+
if (!closestPeer || !closestPeer.id) return this.requestData(hash, store?.name ? store?.name : store)
|
|
424
426
|
|
|
425
|
-
const id = closestPeer.id
|
|
427
|
+
const id = closestPeer.id
|
|
426
428
|
if (this.connections) {
|
|
427
429
|
let closest = this.connections.filter((peer) => {
|
|
428
|
-
if (peer.
|
|
430
|
+
if (peer.peerId === id) return peer
|
|
429
431
|
})
|
|
430
432
|
|
|
431
|
-
let data = new DataMessage({hash, store: store
|
|
433
|
+
let data = new DataMessage({hash, store: store?.name ? store?.name : store});
|
|
432
434
|
|
|
433
435
|
const node = await this.prepareMessage(id, data.encoded)
|
|
434
436
|
if (closest[0]) data = await closest[0].request(node.encoded)
|
|
435
437
|
else {
|
|
436
438
|
closest = this.connections.filter((peer) => {
|
|
437
|
-
if (peer.
|
|
439
|
+
if (peer.peerId === id) return peer
|
|
438
440
|
})
|
|
439
441
|
if (closest[0]) data = await closest[0].request(node.encoded)
|
|
440
442
|
}
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
}
|
|
443
|
+
data = new Uint8Array(Object.values(data))
|
|
444
|
+
let proto = protoFor(data)
|
|
445
|
+
proto = protoFor(proto.decoded.data)
|
|
446
|
+
// TODO: store data automaticly or not
|
|
447
|
+
return proto.decoded.data
|
|
447
448
|
|
|
448
449
|
// this.put(hash, proto.decoded.data)
|
|
449
450
|
}
|
|
@@ -537,7 +538,7 @@ export default class Peernet {
|
|
|
537
538
|
if (store && await store.has(hash)) data = await store.get(hash)
|
|
538
539
|
if (data) return data
|
|
539
540
|
|
|
540
|
-
return this.requestData(hash, store
|
|
541
|
+
return this.requestData(hash, store?.name ? store.name : store)
|
|
541
542
|
}
|
|
542
543
|
|
|
543
544
|
/**
|
|
@@ -578,8 +579,8 @@ export default class Peernet {
|
|
|
578
579
|
data = new PsMessage({data, topic})
|
|
579
580
|
for (const peer of this.connections) {
|
|
580
581
|
if (peer.connected) {
|
|
581
|
-
if (peer.
|
|
582
|
-
const node = await this.prepareMessage(peer.
|
|
582
|
+
if (peer.peerId !== this.peerId) {
|
|
583
|
+
const node = await this.prepareMessage(peer.peerId, data.encoded)
|
|
583
584
|
peer.send(new TextEncoder().encode(JSON.stringify({id, data: node.encoded})))
|
|
584
585
|
}
|
|
585
586
|
} else {
|
|
@@ -604,7 +605,7 @@ export default class Peernet {
|
|
|
604
605
|
}
|
|
605
606
|
|
|
606
607
|
async removePeer(peer) {
|
|
607
|
-
delete this.client.connections[peer.
|
|
608
|
+
delete this.client.connections[peer.peerId]
|
|
608
609
|
}
|
|
609
610
|
|
|
610
611
|
get Buffer() {
|
package/test.js
CHANGED
|
@@ -8,6 +8,7 @@ pubsub.subscribe('peer:connected', async peer => {
|
|
|
8
8
|
request:'lastBlock'
|
|
9
9
|
})
|
|
10
10
|
const to = peer.id
|
|
11
|
+
await peernet.data.put('hello', 'hi')
|
|
11
12
|
console.log(request);
|
|
12
13
|
const node = await peernet.prepareMessage(to, request.encoded)
|
|
13
14
|
console.log({node});
|
|
@@ -25,4 +26,10 @@ pubsub.subscribe('peer:connected', async peer => {
|
|
|
25
26
|
|
|
26
27
|
const block = new TextDecoder().decode(response.decoded.response)
|
|
27
28
|
console.log(block);
|
|
29
|
+
const task = () => setTimeout(() => {
|
|
30
|
+
console.log(peernet.connections[0]?.connected);
|
|
31
|
+
console.log(pubsub.subscribers);
|
|
32
|
+
task()
|
|
33
|
+
}, 5000);
|
|
34
|
+
task()
|
|
28
35
|
})
|
package/test4.js
ADDED
package/webpack.config.js
CHANGED
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
/*!
|
|
2
|
-
* Determine if an object is a Buffer
|
|
3
|
-
*
|
|
4
|
-
* @author Feross Aboukhadijeh <https://feross.org>
|
|
5
|
-
* @license MIT
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
/*!
|
|
9
|
-
* The buffer module from node.js, for the browser.
|
|
10
|
-
*
|
|
11
|
-
* @author Feross Aboukhadijeh <https://feross.org>
|
|
12
|
-
* @license MIT
|
|
13
|
-
*/
|
|
14
|
-
|
|
15
|
-
/*!
|
|
16
|
-
* assert.js - assertions for javascript
|
|
17
|
-
* Copyright (c) 2018, Christopher Jeffrey (MIT License).
|
|
18
|
-
* https://github.com/chjj/bsert
|
|
19
|
-
*/
|
|
20
|
-
|
|
21
|
-
/*!
|
|
22
|
-
* base32.js - base32 for bcrypto
|
|
23
|
-
* Copyright (c) 2014-2019, Christopher Jeffrey (MIT License).
|
|
24
|
-
* https://github.com/bcoin-org/bcrypto
|
|
25
|
-
*
|
|
26
|
-
* Parts of this software are based on bitcoin/bitcoin:
|
|
27
|
-
* Copyright (c) 2009-2019, The Bitcoin Core Developers (MIT License).
|
|
28
|
-
* Copyright (c) 2009-2019, The Bitcoin Developers (MIT License).
|
|
29
|
-
* https://github.com/bitcoin/bitcoin
|
|
30
|
-
*
|
|
31
|
-
* Resources:
|
|
32
|
-
* https://tools.ietf.org/html/rfc4648
|
|
33
|
-
* https://github.com/bitcoin/bitcoin/blob/11d486d/src/utilstrencodings.cpp#L230
|
|
34
|
-
*/
|
|
35
|
-
|
|
36
|
-
/*! For license information please see browser.js.LICENSE.txt */
|
|
37
|
-
|
|
38
|
-
/*! ieee754. BSD-3-Clause License. Feross Aboukhadijeh <https://feross.org/opensource> */
|
|
39
|
-
|
|
40
|
-
/*! safe-buffer. MIT License. Feross Aboukhadijeh <https://feross.org/opensource> */
|