@leofcoin/peernet 1.1.67 → 1.1.68
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/exports/browser/{index-1d597e6b.js → index-e6b55b51.js} +1 -1
- package/exports/browser/{messages-67e5efa0.js → messages-606d459f.js} +1 -1
- package/exports/browser/{peernet-55ce42c7.js → peernet-b93a3e33.js} +97 -70
- package/exports/browser/peernet.d.ts +11 -6
- package/exports/browser/peernet.js +1 -1
- package/exports/peernet.js +72 -61
- package/exports/types/peernet.d.ts +11 -6
- package/package.json +2 -2
- package/src/peernet.ts +80 -67
|
@@ -27088,7 +27088,7 @@ class Identity {
|
|
|
27088
27088
|
this.selectedAccount = new TextDecoder().decode(selected);
|
|
27089
27089
|
}
|
|
27090
27090
|
else {
|
|
27091
|
-
const importee = await import(/* webpackChunkName: "generate-account" */ './index-
|
|
27091
|
+
const importee = await import(/* webpackChunkName: "generate-account" */ './index-e6b55b51.js');
|
|
27092
27092
|
const { identity, accounts } = await importee.default(password, this.network);
|
|
27093
27093
|
await globalThis.accountStore.put('public', JSON.stringify({ walletId: identity.walletId }));
|
|
27094
27094
|
await globalThis.walletStore.put('version', String(1));
|
|
@@ -27164,17 +27164,18 @@ class Peernet {
|
|
|
27164
27164
|
autoStart = true;
|
|
27165
27165
|
#starting = false;
|
|
27166
27166
|
#started = false;
|
|
27167
|
-
#connections = {};
|
|
27168
27167
|
requestProtos = {};
|
|
27169
27168
|
_messageHandler;
|
|
27170
27169
|
_peerHandler;
|
|
27171
27170
|
protos;
|
|
27171
|
+
version;
|
|
27172
27172
|
/**
|
|
27173
27173
|
* @access public
|
|
27174
27174
|
* @param {Object} options
|
|
27175
27175
|
* @param {String} options.network - desired network
|
|
27176
27176
|
* @param {String} options.stars - star list for selected network (these should match, don't mix networks)
|
|
27177
27177
|
* @param {String} options.root - path to root directory
|
|
27178
|
+
* @param {String} options.version - path to root directory
|
|
27178
27179
|
* @param {String} options.storePrefix - prefix for datatores (lfc)
|
|
27179
27180
|
*
|
|
27180
27181
|
* @return {Promise} instance of Peernet
|
|
@@ -27189,6 +27190,7 @@ class Peernet {
|
|
|
27189
27190
|
this.network = options.network || 'leofcoin';
|
|
27190
27191
|
this.autoStart = options.autoStart === undefined ? true : options.autoStart;
|
|
27191
27192
|
this.stars = options.stars;
|
|
27193
|
+
this.version = options.version;
|
|
27192
27194
|
const parts = this.network.split(':');
|
|
27193
27195
|
this.networkVersion = options.networkVersion || parts.length > 1 ? parts[1] : 'mainnet';
|
|
27194
27196
|
if (!options.storePrefix)
|
|
@@ -27253,13 +27255,13 @@ class Peernet {
|
|
|
27253
27255
|
* @return {Array} peerId
|
|
27254
27256
|
*/
|
|
27255
27257
|
get peers() {
|
|
27256
|
-
return Object.entries(this
|
|
27258
|
+
return Object.entries(this.client.connections);
|
|
27257
27259
|
}
|
|
27258
27260
|
get connections() {
|
|
27259
|
-
return
|
|
27261
|
+
return this.client.connections;
|
|
27260
27262
|
}
|
|
27261
27263
|
get peerEntries() {
|
|
27262
|
-
return Object.values(this
|
|
27264
|
+
return Object.values(this.client.connections);
|
|
27263
27265
|
}
|
|
27264
27266
|
/**
|
|
27265
27267
|
* @return {String} id - peerId
|
|
@@ -27280,7 +27282,7 @@ class Peernet {
|
|
|
27280
27282
|
this.root = options.root;
|
|
27281
27283
|
const { RequestMessage, ResponseMessage, PeerMessage, PeerMessageResponse, PeernetMessage, DHTMessage, DHTMessageResponse, DataMessage, DataMessageResponse, PsMessage, ChatMessage, PeernetFile
|
|
27282
27284
|
// FolderMessageResponse
|
|
27283
|
-
} = await import(/* webpackChunkName: "messages" */ './messages-
|
|
27285
|
+
} = await import(/* webpackChunkName: "messages" */ './messages-606d459f.js');
|
|
27284
27286
|
/**
|
|
27285
27287
|
* proto Object containing protos
|
|
27286
27288
|
* @type {Object}
|
|
@@ -27314,55 +27316,58 @@ class Peernet {
|
|
|
27314
27316
|
await this.identity.load(password);
|
|
27315
27317
|
this._peerHandler = new PeerDiscovery(this.id);
|
|
27316
27318
|
this.peerId = this.id;
|
|
27317
|
-
this.addRequestHandler('handshake', () => {
|
|
27318
|
-
|
|
27319
|
-
|
|
27320
|
-
|
|
27321
|
-
})
|
|
27322
|
-
pubsub.subscribe('peer:discovered', async (peer) => {
|
|
27323
|
-
|
|
27324
|
-
|
|
27325
|
-
|
|
27326
|
-
|
|
27327
|
-
|
|
27328
|
-
|
|
27329
|
-
|
|
27330
|
-
|
|
27331
|
-
|
|
27332
|
-
|
|
27333
|
-
|
|
27334
|
-
|
|
27335
|
-
|
|
27336
|
-
|
|
27337
|
-
|
|
27338
|
-
|
|
27339
|
-
|
|
27340
|
-
|
|
27341
|
-
|
|
27342
|
-
|
|
27343
|
-
}
|
|
27344
|
-
|
|
27319
|
+
// this.addRequestHandler('handshake', () => {
|
|
27320
|
+
// return new peernet.protos['peernet-response']({
|
|
27321
|
+
// response: { peerId: this.id }
|
|
27322
|
+
// })
|
|
27323
|
+
// })
|
|
27324
|
+
// pubsub.subscribe('peer:discovered', async (peer) => {
|
|
27325
|
+
// // console.log(peer);
|
|
27326
|
+
// if (this.requestProtos['version'] && !peer.version) {
|
|
27327
|
+
// let data = await new globalThis.peernet.protos['peernet-request']({
|
|
27328
|
+
// request: 'version'
|
|
27329
|
+
// })
|
|
27330
|
+
// let node = await globalThis.peernet.prepareMessage(data)
|
|
27331
|
+
// let response = await peer.request(node.encoded)
|
|
27332
|
+
// response = await new globalThis.peernet.protos['peernet-response'](new Uint8Array(Object.values(response)))
|
|
27333
|
+
// peer.version = response.decoded.response.version
|
|
27334
|
+
// }
|
|
27335
|
+
// if (!peer.peerId) {
|
|
27336
|
+
// let data = await new globalThis.peernet.protos['peernet-request']({
|
|
27337
|
+
// request: 'handshake'
|
|
27338
|
+
// })
|
|
27339
|
+
// let node = await globalThis.peernet.prepareMessage(data)
|
|
27340
|
+
// let response = await peer.request(node.encoded)
|
|
27341
|
+
// response = await new globalThis.peernet.protos['peernet-response'](new Uint8Array(Object.values(response)))
|
|
27342
|
+
// // todo: response.decoded should be the response and not response.peerId
|
|
27343
|
+
// // todo: ignore above and remove discover completly
|
|
27344
|
+
// response.decoded.response.peerId
|
|
27345
|
+
// }
|
|
27346
|
+
// this.connections[peer.peerId] = peer
|
|
27347
|
+
// pubsub.publish('peer:connected', peer)
|
|
27348
|
+
// todo: cleanup discovered
|
|
27349
|
+
// })
|
|
27350
|
+
// pubsub.subscribe('peer:left', this.#peerLeft.bind(this))
|
|
27345
27351
|
/**
|
|
27346
27352
|
* converts data -> message -> proto
|
|
27347
27353
|
* @see DataHandler
|
|
27348
27354
|
*/
|
|
27349
27355
|
pubsub.subscribe('peer:data', dataHandler);
|
|
27350
|
-
|
|
27351
|
-
|
|
27352
|
-
|
|
27353
|
-
else {
|
|
27354
|
-
|
|
27355
|
-
|
|
27356
|
-
|
|
27357
|
-
|
|
27358
|
-
|
|
27359
|
-
|
|
27360
|
-
|
|
27361
|
-
|
|
27362
|
-
|
|
27363
|
-
|
|
27364
|
-
|
|
27365
|
-
}
|
|
27356
|
+
// // todo: remove below, already handles in the swarm
|
|
27357
|
+
// if (globalThis.navigator) {
|
|
27358
|
+
// globalThis.addEventListener('beforeunload', async () => this.client.close())
|
|
27359
|
+
// } else {
|
|
27360
|
+
// process.on('SIGTERM', async () => {
|
|
27361
|
+
// process.stdin.resume()
|
|
27362
|
+
// try {
|
|
27363
|
+
// await this.client.close()
|
|
27364
|
+
// } catch (error) {
|
|
27365
|
+
// // @ts-ignore
|
|
27366
|
+
// await this.client.close()
|
|
27367
|
+
// }
|
|
27368
|
+
// process.exit()
|
|
27369
|
+
// })
|
|
27370
|
+
// }
|
|
27366
27371
|
if (this.autoStart)
|
|
27367
27372
|
await this.start();
|
|
27368
27373
|
return this;
|
|
@@ -27375,6 +27380,7 @@ class Peernet {
|
|
|
27375
27380
|
#peerId;
|
|
27376
27381
|
#connections = {};
|
|
27377
27382
|
#stars = {};
|
|
27383
|
+
#connectEvent = 'peer:connected';
|
|
27378
27384
|
id;
|
|
27379
27385
|
networkVersion;
|
|
27380
27386
|
starsConfig;
|
|
@@ -27394,18 +27400,32 @@ class Peernet {
|
|
|
27394
27400
|
getPeer(peerId) {
|
|
27395
27401
|
return this.#connections[peerId];
|
|
27396
27402
|
}
|
|
27397
|
-
|
|
27403
|
+
/**
|
|
27404
|
+
*
|
|
27405
|
+
* @param options {object}
|
|
27406
|
+
* @param options.peerId {string}
|
|
27407
|
+
* @param options.networkVersion {string}
|
|
27408
|
+
* @param options.version {string}
|
|
27409
|
+
* @param options.stars {string[]}
|
|
27410
|
+
* @param options.connectEvent {string} defaults to peer:connected, can be renamed to handle different protocols, like peer:discovered (setup peer props before fireing the connect event)
|
|
27411
|
+
*/
|
|
27412
|
+
constructor(options) {
|
|
27413
|
+
const { peerId, networkVersion, version, connectEvent, stars } = {
|
|
27414
|
+
...defaultOptions,
|
|
27415
|
+
...options
|
|
27416
|
+
};
|
|
27398
27417
|
this.#peerId = peerId;
|
|
27399
27418
|
this.networkVersion = networkVersion;
|
|
27400
27419
|
this.version = version;
|
|
27401
|
-
this
|
|
27402
|
-
}
|
|
27403
|
-
async _init(stars = []) {
|
|
27420
|
+
this.#connectEvent = connectEvent;
|
|
27404
27421
|
this.starsConfig = stars;
|
|
27422
|
+
this._init();
|
|
27423
|
+
}
|
|
27424
|
+
async _init() {
|
|
27405
27425
|
// reconnectJob()
|
|
27406
27426
|
if (!globalThis.RTCPeerConnection)
|
|
27407
27427
|
globalThis.wrtc = (await import('./browser-2c73e2ef.js').then(function (n) { return n.b; })).default;
|
|
27408
|
-
for (const star of
|
|
27428
|
+
for (const star of this.starsConfig) {
|
|
27409
27429
|
try {
|
|
27410
27430
|
const client = new SocketRequestClient(star, this.networkVersion);
|
|
27411
27431
|
this.#stars[star] = await client.init();
|
|
@@ -27416,7 +27436,8 @@ class Peernet {
|
|
|
27416
27436
|
});
|
|
27417
27437
|
}
|
|
27418
27438
|
catch (e) {
|
|
27419
|
-
if (
|
|
27439
|
+
if (this.starsConfig.indexOf(star) === this.starsConfig.length - 1 &&
|
|
27440
|
+
!this.socketClient)
|
|
27420
27441
|
throw new Error(`No star available to connect`);
|
|
27421
27442
|
}
|
|
27422
27443
|
}
|
|
@@ -27542,7 +27563,7 @@ class Peernet {
|
|
|
27542
27563
|
};
|
|
27543
27564
|
#peerConnect = (peer) => {
|
|
27544
27565
|
globalThis.debug(`${peer.peerId} connected`);
|
|
27545
|
-
globalThis.pubsub.publish(
|
|
27566
|
+
globalThis.pubsub.publish(this.#connectEvent, peer.peerId);
|
|
27546
27567
|
};
|
|
27547
27568
|
#noticeMessage = (message, id) => {
|
|
27548
27569
|
if (globalThis.pubsub.subscribers[id]) {
|
|
@@ -27592,18 +27613,24 @@ class Peernet {
|
|
|
27592
27613
|
* @type {PeernetClient}
|
|
27593
27614
|
*/
|
|
27594
27615
|
console.log(this.stars);
|
|
27595
|
-
this.client = new importee.default(
|
|
27616
|
+
this.client = new importee.default({
|
|
27617
|
+
peerId: this.id,
|
|
27618
|
+
networkVersion: this.networkVersion,
|
|
27619
|
+
version: this.version,
|
|
27620
|
+
stars: this.stars
|
|
27621
|
+
});
|
|
27596
27622
|
this.#started = true;
|
|
27597
27623
|
this.#starting = false;
|
|
27598
27624
|
}
|
|
27599
|
-
|
|
27600
|
-
|
|
27601
|
-
|
|
27602
|
-
|
|
27603
|
-
|
|
27604
|
-
|
|
27605
|
-
|
|
27606
|
-
}
|
|
27625
|
+
// todo: remove, handled in swarm now
|
|
27626
|
+
// #peerLeft(peer: SwarmPeer) {
|
|
27627
|
+
// for (const [id, _peer] of Object.entries(this.connections)) {
|
|
27628
|
+
// if (_peer.id === peer.id && this.connections[id] && !this.connections[id].connected) {
|
|
27629
|
+
// delete this.connections[id]
|
|
27630
|
+
// this.removePeer(_peer)
|
|
27631
|
+
// }
|
|
27632
|
+
// }
|
|
27633
|
+
// }
|
|
27607
27634
|
addRequestHandler(name, method) {
|
|
27608
27635
|
this.requestProtos[name] = method;
|
|
27609
27636
|
}
|
|
@@ -27707,7 +27734,7 @@ class Peernet {
|
|
|
27707
27734
|
this.dht.addProvider(peerInfo, proto.decoded.hash);
|
|
27708
27735
|
};
|
|
27709
27736
|
let walks = [];
|
|
27710
|
-
for (const [peerId, peer] of Object.entries(this
|
|
27737
|
+
for (const [peerId, peer] of Object.entries(this.connections)) {
|
|
27711
27738
|
if (peerId !== this.id) {
|
|
27712
27739
|
walks.push(walk(peer, peerId));
|
|
27713
27740
|
}
|
|
@@ -27774,7 +27801,7 @@ class Peernet {
|
|
|
27774
27801
|
if (!closestPeer || !closestPeer.id)
|
|
27775
27802
|
return this.requestData(hash, store?.name || store);
|
|
27776
27803
|
const id = closestPeer.id;
|
|
27777
|
-
const peer = this
|
|
27804
|
+
const peer = this.connections[id];
|
|
27778
27805
|
if (peer?.connected) {
|
|
27779
27806
|
let data = await new globalThis.peernet.protos['peernet-data']({
|
|
27780
27807
|
hash,
|
|
@@ -27788,7 +27815,7 @@ class Peernet {
|
|
|
27788
27815
|
const promises = [];
|
|
27789
27816
|
const providers = await this.providersFor(hash, store);
|
|
27790
27817
|
for (const provider of Object.values(providers)) {
|
|
27791
|
-
const peer = this
|
|
27818
|
+
const peer = this.connections[provider.id];
|
|
27792
27819
|
if (peer)
|
|
27793
27820
|
promises.push(peer.request(node.encoded));
|
|
27794
27821
|
}
|
|
@@ -28002,7 +28029,7 @@ class Peernet {
|
|
|
28002
28029
|
// globalSub.publish(topic, data)
|
|
28003
28030
|
const id = Math.random().toString(36).slice(-12);
|
|
28004
28031
|
data = await new globalThis.peernet.protos['peernet-ps']({ data, topic });
|
|
28005
|
-
for (const [peerId, peer] of Object.entries(this
|
|
28032
|
+
for (const [peerId, peer] of Object.entries(this.connections)) {
|
|
28006
28033
|
if (peerId !== this.id) {
|
|
28007
28034
|
const node = await this.prepareMessage(data);
|
|
28008
28035
|
this.sendMessage(peer, id, node.encoded);
|
|
@@ -6,7 +6,8 @@ import DHT from './dht/dht.js';
|
|
|
6
6
|
import MessageHandler from './handlers/message.js';
|
|
7
7
|
import { Storage as LeofcoinStorageClass } from '@leofcoin/storage';
|
|
8
8
|
import Identity from './identity.js';
|
|
9
|
-
import swarm from '@netpeer/
|
|
9
|
+
import swarm from '@netpeer/swarm/client';
|
|
10
|
+
import SwarmPeer from '@netpeer/swarm/peer';
|
|
10
11
|
declare global {
|
|
11
12
|
var LeofcoinStorage: typeof LeofcoinStorageClass;
|
|
12
13
|
var peernet: Peernet;
|
|
@@ -51,12 +52,14 @@ export default class Peernet {
|
|
|
51
52
|
_messageHandler: MessageHandler;
|
|
52
53
|
_peerHandler: PeerDiscovery;
|
|
53
54
|
protos: {};
|
|
55
|
+
version: any;
|
|
54
56
|
/**
|
|
55
57
|
* @access public
|
|
56
58
|
* @param {Object} options
|
|
57
59
|
* @param {String} options.network - desired network
|
|
58
60
|
* @param {String} options.stars - star list for selected network (these should match, don't mix networks)
|
|
59
61
|
* @param {String} options.root - path to root directory
|
|
62
|
+
* @param {String} options.version - path to root directory
|
|
60
63
|
* @param {String} options.storePrefix - prefix for datatores (lfc)
|
|
61
64
|
*
|
|
62
65
|
* @return {Promise} instance of Peernet
|
|
@@ -82,13 +85,15 @@ export default class Peernet {
|
|
|
82
85
|
*
|
|
83
86
|
* @return {Array} peerId
|
|
84
87
|
*/
|
|
85
|
-
get peers(): [string,
|
|
86
|
-
get connections():
|
|
87
|
-
|
|
88
|
+
get peers(): [string, SwarmPeer][];
|
|
89
|
+
get connections(): {
|
|
90
|
+
[x: string]: SwarmPeer;
|
|
91
|
+
};
|
|
92
|
+
get peerEntries(): SwarmPeer[];
|
|
88
93
|
/**
|
|
89
94
|
* @return {String} id - peerId
|
|
90
95
|
*/
|
|
91
|
-
getConnection(id: any):
|
|
96
|
+
getConnection(id: any): SwarmPeer;
|
|
92
97
|
/**
|
|
93
98
|
* @private
|
|
94
99
|
*
|
|
@@ -238,6 +243,6 @@ export default class Peernet {
|
|
|
238
243
|
* @param {Method} cb
|
|
239
244
|
*/
|
|
240
245
|
subscribe(topic: string, callback: Function): Promise<void>;
|
|
241
|
-
removePeer(peer: any): Promise<
|
|
246
|
+
removePeer(peer: any): Promise<any>;
|
|
242
247
|
get Buffer(): BufferConstructor;
|
|
243
248
|
}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export { P as default } from './peernet-
|
|
1
|
+
export { P as default } from './peernet-b93a3e33.js';
|
|
2
2
|
import './value-4e80eeeb.js';
|
package/exports/peernet.js
CHANGED
|
@@ -425,17 +425,18 @@ class Peernet {
|
|
|
425
425
|
autoStart = true;
|
|
426
426
|
#starting = false;
|
|
427
427
|
#started = false;
|
|
428
|
-
#connections = {};
|
|
429
428
|
requestProtos = {};
|
|
430
429
|
_messageHandler;
|
|
431
430
|
_peerHandler;
|
|
432
431
|
protos;
|
|
432
|
+
version;
|
|
433
433
|
/**
|
|
434
434
|
* @access public
|
|
435
435
|
* @param {Object} options
|
|
436
436
|
* @param {String} options.network - desired network
|
|
437
437
|
* @param {String} options.stars - star list for selected network (these should match, don't mix networks)
|
|
438
438
|
* @param {String} options.root - path to root directory
|
|
439
|
+
* @param {String} options.version - path to root directory
|
|
439
440
|
* @param {String} options.storePrefix - prefix for datatores (lfc)
|
|
440
441
|
*
|
|
441
442
|
* @return {Promise} instance of Peernet
|
|
@@ -450,6 +451,7 @@ class Peernet {
|
|
|
450
451
|
this.network = options.network || 'leofcoin';
|
|
451
452
|
this.autoStart = options.autoStart === undefined ? true : options.autoStart;
|
|
452
453
|
this.stars = options.stars;
|
|
454
|
+
this.version = options.version;
|
|
453
455
|
const parts = this.network.split(':');
|
|
454
456
|
this.networkVersion = options.networkVersion || parts.length > 1 ? parts[1] : 'mainnet';
|
|
455
457
|
if (!options.storePrefix)
|
|
@@ -514,13 +516,13 @@ class Peernet {
|
|
|
514
516
|
* @return {Array} peerId
|
|
515
517
|
*/
|
|
516
518
|
get peers() {
|
|
517
|
-
return Object.entries(this
|
|
519
|
+
return Object.entries(this.client.connections);
|
|
518
520
|
}
|
|
519
521
|
get connections() {
|
|
520
|
-
return
|
|
522
|
+
return this.client.connections;
|
|
521
523
|
}
|
|
522
524
|
get peerEntries() {
|
|
523
|
-
return Object.values(this
|
|
525
|
+
return Object.values(this.client.connections);
|
|
524
526
|
}
|
|
525
527
|
/**
|
|
526
528
|
* @return {String} id - peerId
|
|
@@ -575,55 +577,58 @@ class Peernet {
|
|
|
575
577
|
await this.identity.load(password);
|
|
576
578
|
this._peerHandler = new PeerDiscovery(this.id);
|
|
577
579
|
this.peerId = this.id;
|
|
578
|
-
this.addRequestHandler('handshake', () => {
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
})
|
|
583
|
-
pubsub.subscribe('peer:discovered', async (peer) => {
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
}
|
|
605
|
-
|
|
580
|
+
// this.addRequestHandler('handshake', () => {
|
|
581
|
+
// return new peernet.protos['peernet-response']({
|
|
582
|
+
// response: { peerId: this.id }
|
|
583
|
+
// })
|
|
584
|
+
// })
|
|
585
|
+
// pubsub.subscribe('peer:discovered', async (peer) => {
|
|
586
|
+
// // console.log(peer);
|
|
587
|
+
// if (this.requestProtos['version'] && !peer.version) {
|
|
588
|
+
// let data = await new globalThis.peernet.protos['peernet-request']({
|
|
589
|
+
// request: 'version'
|
|
590
|
+
// })
|
|
591
|
+
// let node = await globalThis.peernet.prepareMessage(data)
|
|
592
|
+
// let response = await peer.request(node.encoded)
|
|
593
|
+
// response = await new globalThis.peernet.protos['peernet-response'](new Uint8Array(Object.values(response)))
|
|
594
|
+
// peer.version = response.decoded.response.version
|
|
595
|
+
// }
|
|
596
|
+
// if (!peer.peerId) {
|
|
597
|
+
// let data = await new globalThis.peernet.protos['peernet-request']({
|
|
598
|
+
// request: 'handshake'
|
|
599
|
+
// })
|
|
600
|
+
// let node = await globalThis.peernet.prepareMessage(data)
|
|
601
|
+
// let response = await peer.request(node.encoded)
|
|
602
|
+
// response = await new globalThis.peernet.protos['peernet-response'](new Uint8Array(Object.values(response)))
|
|
603
|
+
// // todo: response.decoded should be the response and not response.peerId
|
|
604
|
+
// // todo: ignore above and remove discover completly
|
|
605
|
+
// response.decoded.response.peerId
|
|
606
|
+
// }
|
|
607
|
+
// this.connections[peer.peerId] = peer
|
|
608
|
+
// pubsub.publish('peer:connected', peer)
|
|
609
|
+
// todo: cleanup discovered
|
|
610
|
+
// })
|
|
611
|
+
// pubsub.subscribe('peer:left', this.#peerLeft.bind(this))
|
|
606
612
|
/**
|
|
607
613
|
* converts data -> message -> proto
|
|
608
614
|
* @see DataHandler
|
|
609
615
|
*/
|
|
610
616
|
pubsub.subscribe('peer:data', dataHandler);
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
else {
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
}
|
|
617
|
+
// // todo: remove below, already handles in the swarm
|
|
618
|
+
// if (globalThis.navigator) {
|
|
619
|
+
// globalThis.addEventListener('beforeunload', async () => this.client.close())
|
|
620
|
+
// } else {
|
|
621
|
+
// process.on('SIGTERM', async () => {
|
|
622
|
+
// process.stdin.resume()
|
|
623
|
+
// try {
|
|
624
|
+
// await this.client.close()
|
|
625
|
+
// } catch (error) {
|
|
626
|
+
// // @ts-ignore
|
|
627
|
+
// await this.client.close()
|
|
628
|
+
// }
|
|
629
|
+
// process.exit()
|
|
630
|
+
// })
|
|
631
|
+
// }
|
|
627
632
|
if (this.autoStart)
|
|
628
633
|
await this.start();
|
|
629
634
|
return this;
|
|
@@ -638,18 +643,24 @@ class Peernet {
|
|
|
638
643
|
* @type {PeernetClient}
|
|
639
644
|
*/
|
|
640
645
|
console.log(this.stars);
|
|
641
|
-
this.client = new importee.default(
|
|
646
|
+
this.client = new importee.default({
|
|
647
|
+
peerId: this.id,
|
|
648
|
+
networkVersion: this.networkVersion,
|
|
649
|
+
version: this.version,
|
|
650
|
+
stars: this.stars
|
|
651
|
+
});
|
|
642
652
|
this.#started = true;
|
|
643
653
|
this.#starting = false;
|
|
644
654
|
}
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
}
|
|
655
|
+
// todo: remove, handled in swarm now
|
|
656
|
+
// #peerLeft(peer: SwarmPeer) {
|
|
657
|
+
// for (const [id, _peer] of Object.entries(this.connections)) {
|
|
658
|
+
// if (_peer.id === peer.id && this.connections[id] && !this.connections[id].connected) {
|
|
659
|
+
// delete this.connections[id]
|
|
660
|
+
// this.removePeer(_peer)
|
|
661
|
+
// }
|
|
662
|
+
// }
|
|
663
|
+
// }
|
|
653
664
|
addRequestHandler(name, method) {
|
|
654
665
|
this.requestProtos[name] = method;
|
|
655
666
|
}
|
|
@@ -753,7 +764,7 @@ class Peernet {
|
|
|
753
764
|
this.dht.addProvider(peerInfo, proto.decoded.hash);
|
|
754
765
|
};
|
|
755
766
|
let walks = [];
|
|
756
|
-
for (const [peerId, peer] of Object.entries(this
|
|
767
|
+
for (const [peerId, peer] of Object.entries(this.connections)) {
|
|
757
768
|
if (peerId !== this.id) {
|
|
758
769
|
walks.push(walk(peer, peerId));
|
|
759
770
|
}
|
|
@@ -820,7 +831,7 @@ class Peernet {
|
|
|
820
831
|
if (!closestPeer || !closestPeer.id)
|
|
821
832
|
return this.requestData(hash, store?.name || store);
|
|
822
833
|
const id = closestPeer.id;
|
|
823
|
-
const peer = this
|
|
834
|
+
const peer = this.connections[id];
|
|
824
835
|
if (peer?.connected) {
|
|
825
836
|
let data = await new globalThis.peernet.protos['peernet-data']({
|
|
826
837
|
hash,
|
|
@@ -834,7 +845,7 @@ class Peernet {
|
|
|
834
845
|
const promises = [];
|
|
835
846
|
const providers = await this.providersFor(hash, store);
|
|
836
847
|
for (const provider of Object.values(providers)) {
|
|
837
|
-
const peer = this
|
|
848
|
+
const peer = this.connections[provider.id];
|
|
838
849
|
if (peer)
|
|
839
850
|
promises.push(peer.request(node.encoded));
|
|
840
851
|
}
|
|
@@ -1048,7 +1059,7 @@ class Peernet {
|
|
|
1048
1059
|
// globalSub.publish(topic, data)
|
|
1049
1060
|
const id = Math.random().toString(36).slice(-12);
|
|
1050
1061
|
data = await new globalThis.peernet.protos['peernet-ps']({ data, topic });
|
|
1051
|
-
for (const [peerId, peer] of Object.entries(this
|
|
1062
|
+
for (const [peerId, peer] of Object.entries(this.connections)) {
|
|
1052
1063
|
if (peerId !== this.id) {
|
|
1053
1064
|
const node = await this.prepareMessage(data);
|
|
1054
1065
|
this.sendMessage(peer, id, node.encoded);
|
|
@@ -6,7 +6,8 @@ import DHT from './dht/dht.js';
|
|
|
6
6
|
import MessageHandler from './handlers/message.js';
|
|
7
7
|
import { Storage as LeofcoinStorageClass } from '@leofcoin/storage';
|
|
8
8
|
import Identity from './identity.js';
|
|
9
|
-
import swarm from '@netpeer/
|
|
9
|
+
import swarm from '@netpeer/swarm/client';
|
|
10
|
+
import SwarmPeer from '@netpeer/swarm/peer';
|
|
10
11
|
declare global {
|
|
11
12
|
var LeofcoinStorage: typeof LeofcoinStorageClass;
|
|
12
13
|
var peernet: Peernet;
|
|
@@ -51,12 +52,14 @@ export default class Peernet {
|
|
|
51
52
|
_messageHandler: MessageHandler;
|
|
52
53
|
_peerHandler: PeerDiscovery;
|
|
53
54
|
protos: {};
|
|
55
|
+
version: any;
|
|
54
56
|
/**
|
|
55
57
|
* @access public
|
|
56
58
|
* @param {Object} options
|
|
57
59
|
* @param {String} options.network - desired network
|
|
58
60
|
* @param {String} options.stars - star list for selected network (these should match, don't mix networks)
|
|
59
61
|
* @param {String} options.root - path to root directory
|
|
62
|
+
* @param {String} options.version - path to root directory
|
|
60
63
|
* @param {String} options.storePrefix - prefix for datatores (lfc)
|
|
61
64
|
*
|
|
62
65
|
* @return {Promise} instance of Peernet
|
|
@@ -82,13 +85,15 @@ export default class Peernet {
|
|
|
82
85
|
*
|
|
83
86
|
* @return {Array} peerId
|
|
84
87
|
*/
|
|
85
|
-
get peers(): [string,
|
|
86
|
-
get connections():
|
|
87
|
-
|
|
88
|
+
get peers(): [string, SwarmPeer][];
|
|
89
|
+
get connections(): {
|
|
90
|
+
[x: string]: SwarmPeer;
|
|
91
|
+
};
|
|
92
|
+
get peerEntries(): SwarmPeer[];
|
|
88
93
|
/**
|
|
89
94
|
* @return {String} id - peerId
|
|
90
95
|
*/
|
|
91
|
-
getConnection(id: any):
|
|
96
|
+
getConnection(id: any): SwarmPeer;
|
|
92
97
|
/**
|
|
93
98
|
* @private
|
|
94
99
|
*
|
|
@@ -238,6 +243,6 @@ export default class Peernet {
|
|
|
238
243
|
* @param {Method} cb
|
|
239
244
|
*/
|
|
240
245
|
subscribe(topic: string, callback: Function): Promise<void>;
|
|
241
|
-
removePeer(peer: any): Promise<
|
|
246
|
+
removePeer(peer: any): Promise<any>;
|
|
242
247
|
get Buffer(): BufferConstructor;
|
|
243
248
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@leofcoin/peernet",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.68",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "src/peernet.js",
|
|
6
6
|
"exports": {
|
|
@@ -37,7 +37,7 @@
|
|
|
37
37
|
"@leofcoin/peernet-swarm": "^1.1.0",
|
|
38
38
|
"@leofcoin/storage": "^3.0.0",
|
|
39
39
|
"@netpeer/p2pt-swarm": "^1.3.5",
|
|
40
|
-
"@netpeer/swarm": "^0.
|
|
40
|
+
"@netpeer/swarm": "^0.8.1",
|
|
41
41
|
"@types/node": "^18.11.18",
|
|
42
42
|
"@vandeurenglenn/base32": "^1.1.0",
|
|
43
43
|
"@vandeurenglenn/base58": "^1.1.0",
|
package/src/peernet.ts
CHANGED
|
@@ -9,8 +9,8 @@ import { dhtError, nothingFoundError } from './errors/errors.js'
|
|
|
9
9
|
import { Storage as LeofcoinStorageClass } from '@leofcoin/storage'
|
|
10
10
|
import { utils as codecUtils } from '@leofcoin/codecs'
|
|
11
11
|
import Identity from './identity.js'
|
|
12
|
-
import swarm
|
|
13
|
-
import
|
|
12
|
+
import swarm from '@netpeer/swarm/client'
|
|
13
|
+
import SwarmPeer from '@netpeer/swarm/peer'
|
|
14
14
|
|
|
15
15
|
globalThis.LeofcoinStorage = LeofcoinStorageClass
|
|
16
16
|
|
|
@@ -60,11 +60,11 @@ export default class Peernet {
|
|
|
60
60
|
autoStart: boolean = true
|
|
61
61
|
#starting: boolean = false
|
|
62
62
|
#started: boolean = false
|
|
63
|
-
#connections: { [index: PeerId]: P2PTPeer } = {}
|
|
64
63
|
requestProtos = {}
|
|
65
64
|
_messageHandler: MessageHandler
|
|
66
65
|
_peerHandler: PeerDiscovery
|
|
67
66
|
protos: {}
|
|
67
|
+
version
|
|
68
68
|
|
|
69
69
|
/**
|
|
70
70
|
* @access public
|
|
@@ -72,6 +72,7 @@ export default class Peernet {
|
|
|
72
72
|
* @param {String} options.network - desired network
|
|
73
73
|
* @param {String} options.stars - star list for selected network (these should match, don't mix networks)
|
|
74
74
|
* @param {String} options.root - path to root directory
|
|
75
|
+
* @param {String} options.version - path to root directory
|
|
75
76
|
* @param {String} options.storePrefix - prefix for datatores (lfc)
|
|
76
77
|
*
|
|
77
78
|
* @return {Promise} instance of Peernet
|
|
@@ -86,6 +87,7 @@ export default class Peernet {
|
|
|
86
87
|
this.network = options.network || 'leofcoin'
|
|
87
88
|
this.autoStart = options.autoStart === undefined ? true : options.autoStart
|
|
88
89
|
this.stars = options.stars
|
|
90
|
+
this.version = options.version
|
|
89
91
|
const parts = this.network.split(':')
|
|
90
92
|
this.networkVersion = options.networkVersion || parts.length > 1 ? parts[1] : 'mainnet'
|
|
91
93
|
|
|
@@ -162,15 +164,15 @@ export default class Peernet {
|
|
|
162
164
|
* @return {Array} peerId
|
|
163
165
|
*/
|
|
164
166
|
get peers() {
|
|
165
|
-
return Object.entries(this
|
|
167
|
+
return Object.entries(this.client.connections)
|
|
166
168
|
}
|
|
167
169
|
|
|
168
170
|
get connections() {
|
|
169
|
-
return
|
|
171
|
+
return this.client.connections
|
|
170
172
|
}
|
|
171
173
|
|
|
172
174
|
get peerEntries() {
|
|
173
|
-
return Object.values(this
|
|
175
|
+
return Object.values(this.client.connections)
|
|
174
176
|
}
|
|
175
177
|
|
|
176
178
|
/**
|
|
@@ -248,40 +250,44 @@ export default class Peernet {
|
|
|
248
250
|
this._peerHandler = new PeerDiscovery(this.id)
|
|
249
251
|
this.peerId = this.id
|
|
250
252
|
|
|
251
|
-
this.addRequestHandler('handshake', () => {
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
})
|
|
256
|
-
|
|
257
|
-
pubsub.subscribe('peer:discovered', async (peer) => {
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
pubsub.
|
|
253
|
+
// this.addRequestHandler('handshake', () => {
|
|
254
|
+
// return new peernet.protos['peernet-response']({
|
|
255
|
+
// response: { peerId: this.id }
|
|
256
|
+
// })
|
|
257
|
+
// })
|
|
258
|
+
|
|
259
|
+
// pubsub.subscribe('peer:discovered', async (peer) => {
|
|
260
|
+
// // console.log(peer);
|
|
261
|
+
|
|
262
|
+
// if (this.requestProtos['version'] && !peer.version) {
|
|
263
|
+
// let data = await new globalThis.peernet.protos['peernet-request']({
|
|
264
|
+
// request: 'version'
|
|
265
|
+
// })
|
|
266
|
+
// let node = await globalThis.peernet.prepareMessage(data)
|
|
267
|
+
// let response = await peer.request(node.encoded)
|
|
268
|
+
// response = await new globalThis.peernet.protos['peernet-response'](new Uint8Array(Object.values(response)))
|
|
269
|
+
// peer.version = response.decoded.response.version
|
|
270
|
+
// }
|
|
271
|
+
|
|
272
|
+
// if (!peer.peerId) {
|
|
273
|
+
// let data = await new globalThis.peernet.protos['peernet-request']({
|
|
274
|
+
// request: 'handshake'
|
|
275
|
+
// })
|
|
276
|
+
// let node = await globalThis.peernet.prepareMessage(data)
|
|
277
|
+
// let response = await peer.request(node.encoded)
|
|
278
|
+
|
|
279
|
+
// response = await new globalThis.peernet.protos['peernet-response'](new Uint8Array(Object.values(response)))
|
|
280
|
+
// // todo: response.decoded should be the response and not response.peerId
|
|
281
|
+
// // todo: ignore above and remove discover completly
|
|
282
|
+
// response.decoded.response.peerId
|
|
283
|
+
// }
|
|
284
|
+
|
|
285
|
+
// this.connections[peer.peerId] = peer
|
|
286
|
+
// pubsub.publish('peer:connected', peer)
|
|
287
|
+
// todo: cleanup discovered
|
|
288
|
+
// })
|
|
289
|
+
|
|
290
|
+
// pubsub.subscribe('peer:left', this.#peerLeft.bind(this))
|
|
285
291
|
|
|
286
292
|
/**
|
|
287
293
|
* converts data -> message -> proto
|
|
@@ -289,20 +295,21 @@ export default class Peernet {
|
|
|
289
295
|
*/
|
|
290
296
|
pubsub.subscribe('peer:data', dataHandler)
|
|
291
297
|
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
}
|
|
298
|
+
// // todo: remove below, already handles in the swarm
|
|
299
|
+
// if (globalThis.navigator) {
|
|
300
|
+
// globalThis.addEventListener('beforeunload', async () => this.client.close())
|
|
301
|
+
// } else {
|
|
302
|
+
// process.on('SIGTERM', async () => {
|
|
303
|
+
// process.stdin.resume()
|
|
304
|
+
// try {
|
|
305
|
+
// await this.client.close()
|
|
306
|
+
// } catch (error) {
|
|
307
|
+
// // @ts-ignore
|
|
308
|
+
// await this.client.close()
|
|
309
|
+
// }
|
|
310
|
+
// process.exit()
|
|
311
|
+
// })
|
|
312
|
+
// }
|
|
306
313
|
if (this.autoStart) await this.start()
|
|
307
314
|
return this
|
|
308
315
|
}
|
|
@@ -318,19 +325,25 @@ export default class Peernet {
|
|
|
318
325
|
*/
|
|
319
326
|
console.log(this.stars)
|
|
320
327
|
|
|
321
|
-
this.client = new importee.default(
|
|
328
|
+
this.client = new importee.default({
|
|
329
|
+
peerId: this.id,
|
|
330
|
+
networkVersion: this.networkVersion,
|
|
331
|
+
version: this.version,
|
|
332
|
+
stars: this.stars
|
|
333
|
+
})
|
|
322
334
|
this.#started = true
|
|
323
335
|
this.#starting = false
|
|
324
336
|
}
|
|
325
337
|
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
}
|
|
338
|
+
// todo: remove, handled in swarm now
|
|
339
|
+
// #peerLeft(peer: SwarmPeer) {
|
|
340
|
+
// for (const [id, _peer] of Object.entries(this.connections)) {
|
|
341
|
+
// if (_peer.id === peer.id && this.connections[id] && !this.connections[id].connected) {
|
|
342
|
+
// delete this.connections[id]
|
|
343
|
+
// this.removePeer(_peer)
|
|
344
|
+
// }
|
|
345
|
+
// }
|
|
346
|
+
// }
|
|
334
347
|
|
|
335
348
|
addRequestHandler(name, method) {
|
|
336
349
|
this.requestProtos[name] = method
|
|
@@ -447,7 +460,7 @@ export default class Peernet {
|
|
|
447
460
|
if (proto.decoded.has) this.dht.addProvider(peerInfo, proto.decoded.hash)
|
|
448
461
|
}
|
|
449
462
|
let walks = []
|
|
450
|
-
for (const [peerId, peer] of Object.entries(this
|
|
463
|
+
for (const [peerId, peer] of Object.entries(this.connections)) {
|
|
451
464
|
if (peerId !== this.id) {
|
|
452
465
|
walks.push(walk(peer, peerId))
|
|
453
466
|
}
|
|
@@ -514,7 +527,7 @@ export default class Peernet {
|
|
|
514
527
|
if (!closestPeer || !closestPeer.id) return this.requestData(hash, store?.name || store)
|
|
515
528
|
|
|
516
529
|
const id = closestPeer.id
|
|
517
|
-
const peer = this
|
|
530
|
+
const peer = this.connections[id]
|
|
518
531
|
|
|
519
532
|
if (peer?.connected) {
|
|
520
533
|
let data = await new globalThis.peernet.protos['peernet-data']({
|
|
@@ -530,7 +543,7 @@ export default class Peernet {
|
|
|
530
543
|
const promises = []
|
|
531
544
|
const providers = await this.providersFor(hash, store)
|
|
532
545
|
for (const provider of Object.values(providers)) {
|
|
533
|
-
const peer = this
|
|
546
|
+
const peer = this.connections[provider.id]
|
|
534
547
|
|
|
535
548
|
if (peer) promises.push(peer.request(node.encoded))
|
|
536
549
|
}
|
|
@@ -745,7 +758,7 @@ export default class Peernet {
|
|
|
745
758
|
// globalSub.publish(topic, data)
|
|
746
759
|
const id = Math.random().toString(36).slice(-12)
|
|
747
760
|
data = await new globalThis.peernet.protos['peernet-ps']({ data, topic })
|
|
748
|
-
for (const [peerId, peer] of Object.entries(this
|
|
761
|
+
for (const [peerId, peer] of Object.entries(this.connections)) {
|
|
749
762
|
if (peerId !== this.id) {
|
|
750
763
|
const node = await this.prepareMessage(data)
|
|
751
764
|
this.sendMessage(peer, id, node.encoded)
|