@leofcoin/peernet 1.1.66 → 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/browser-2c73e2ef.js +25 -0
- package/exports/browser/{index-34c93510.js → index-e6b55b51.js} +1 -1
- package/exports/browser/{messages-b7f41cba.js → messages-606d459f.js} +1 -1
- package/exports/browser/{peernet-72528dd9.js → peernet-b93a3e33.js} +306 -63
- package/exports/browser/peernet.d.ts +11 -6
- package/exports/browser/peernet.js +1 -1
- package/exports/peernet.js +74 -62
- package/exports/types/peernet.d.ts +11 -6
- package/package.json +3 -2
- package/rollup.config.js +35 -41
- package/src/peernet.ts +83 -68
- package/test.js +60 -62
- package/test2.js +8 -4
- package/exports/browser/client-493cbd83.js +0 -10783
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
var browser$1 = {};
|
|
2
|
+
|
|
3
|
+
browser$1.MediaStream = window.MediaStream;
|
|
4
|
+
browser$1.MediaStreamTrack = window.MediaStreamTrack;
|
|
5
|
+
browser$1.RTCDataChannel = window.RTCDataChannel;
|
|
6
|
+
browser$1.RTCDataChannelEvent = window.RTCDataChannelEvent;
|
|
7
|
+
browser$1.RTCDtlsTransport = window.RTCDtlsTransport;
|
|
8
|
+
browser$1.RTCIceCandidate = window.RTCIceCandidate;
|
|
9
|
+
browser$1.RTCIceTransport = window.RTCIceTransport;
|
|
10
|
+
browser$1.RTCPeerConnection = window.RTCPeerConnection;
|
|
11
|
+
browser$1.RTCPeerConnectionIceEvent = window.RTCPeerConnectionIceEvent;
|
|
12
|
+
browser$1.RTCRtpReceiver = window.RTCRtpReceiver;
|
|
13
|
+
browser$1.RTCRtpSender = window.RTCRtpSender;
|
|
14
|
+
browser$1.RTCRtpTransceiver = window.RTCRtpTransceiver;
|
|
15
|
+
browser$1.RTCSctpTransport = window.RTCSctpTransport;
|
|
16
|
+
browser$1.RTCSessionDescription = window.RTCSessionDescription;
|
|
17
|
+
browser$1.getUserMedia = window.getUserMedia;
|
|
18
|
+
browser$1.mediaDevices = navigator.mediaDevices;
|
|
19
|
+
|
|
20
|
+
var browser = /*#__PURE__*/Object.freeze({
|
|
21
|
+
__proto__: null,
|
|
22
|
+
default: browser$1
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
export { browser as b };
|
|
@@ -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,80 +27316,321 @@ 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);
|
|
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
|
+
// }
|
|
27371
|
+
if (this.autoStart)
|
|
27372
|
+
await this.start();
|
|
27373
|
+
return this;
|
|
27374
|
+
}
|
|
27375
|
+
async start() {
|
|
27376
|
+
if (this.#starting || this.#started)
|
|
27377
|
+
return;
|
|
27378
|
+
this.#starting = true;
|
|
27379
|
+
const importee = class Client {
|
|
27380
|
+
#peerId;
|
|
27381
|
+
#connections = {};
|
|
27382
|
+
#stars = {};
|
|
27383
|
+
#connectEvent = 'peer:connected';
|
|
27384
|
+
id;
|
|
27385
|
+
networkVersion;
|
|
27386
|
+
starsConfig;
|
|
27387
|
+
socketClient;
|
|
27388
|
+
messageSize = 262144;
|
|
27389
|
+
version;
|
|
27390
|
+
#messagesToHandle = {};
|
|
27391
|
+
get peerId() {
|
|
27392
|
+
return this.#peerId;
|
|
27393
|
+
}
|
|
27394
|
+
get connections() {
|
|
27395
|
+
return { ...this.#connections };
|
|
27396
|
+
}
|
|
27397
|
+
get peers() {
|
|
27398
|
+
return Object.entries(this.#connections);
|
|
27399
|
+
}
|
|
27400
|
+
getPeer(peerId) {
|
|
27401
|
+
return this.#connections[peerId];
|
|
27402
|
+
}
|
|
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
|
+
};
|
|
27417
|
+
this.#peerId = peerId;
|
|
27418
|
+
this.networkVersion = networkVersion;
|
|
27419
|
+
this.version = version;
|
|
27420
|
+
this.#connectEvent = connectEvent;
|
|
27421
|
+
this.starsConfig = stars;
|
|
27422
|
+
this._init();
|
|
27423
|
+
}
|
|
27424
|
+
async _init() {
|
|
27425
|
+
// reconnectJob()
|
|
27426
|
+
if (!globalThis.RTCPeerConnection)
|
|
27427
|
+
globalThis.wrtc = (await import('./browser-2c73e2ef.js').then(function (n) { return n.b; })).default;
|
|
27428
|
+
for (const star of this.starsConfig) {
|
|
27429
|
+
try {
|
|
27430
|
+
const client = new SocketRequestClient(star, this.networkVersion);
|
|
27431
|
+
this.#stars[star] = await client.init();
|
|
27432
|
+
this.setupStarListeners(this.#stars[star]);
|
|
27433
|
+
this.#stars[star].send({
|
|
27434
|
+
url: 'join',
|
|
27435
|
+
params: { version: this.version, peerId: this.peerId }
|
|
27436
|
+
});
|
|
27437
|
+
}
|
|
27438
|
+
catch (e) {
|
|
27439
|
+
if (this.starsConfig.indexOf(star) === this.starsConfig.length - 1 &&
|
|
27440
|
+
!this.socketClient)
|
|
27441
|
+
throw new Error(`No star available to connect`);
|
|
27442
|
+
}
|
|
27443
|
+
}
|
|
27350
27444
|
if (globalThis.navigator) {
|
|
27351
|
-
globalThis.addEventListener('beforeunload', async () => this.
|
|
27445
|
+
globalThis.addEventListener('beforeunload', async () => this.close());
|
|
27352
27446
|
}
|
|
27353
27447
|
else {
|
|
27354
|
-
process.on('
|
|
27448
|
+
process.on('SIGINT', async () => {
|
|
27355
27449
|
process.stdin.resume();
|
|
27356
|
-
|
|
27357
|
-
await this.client.destroy();
|
|
27358
|
-
}
|
|
27359
|
-
catch (error) {
|
|
27360
|
-
// @ts-ignore
|
|
27361
|
-
await this.client.close();
|
|
27362
|
-
}
|
|
27450
|
+
await this.close();
|
|
27363
27451
|
process.exit();
|
|
27364
27452
|
});
|
|
27365
27453
|
}
|
|
27366
|
-
if (this.autoStart)
|
|
27367
|
-
await this.start();
|
|
27368
|
-
return this;
|
|
27369
27454
|
}
|
|
27370
|
-
|
|
27371
|
-
|
|
27455
|
+
setupStarListeners(star) {
|
|
27456
|
+
star.pubsub.subscribe('peer:joined', (id) => this.#peerJoined(id, star));
|
|
27457
|
+
star.pubsub.subscribe('peer:left', (id) => this.#peerLeft(id, star));
|
|
27458
|
+
star.pubsub.subscribe('star:joined', this.#starJoined);
|
|
27459
|
+
star.pubsub.subscribe('star:left', this.#starLeft);
|
|
27460
|
+
star.pubsub.subscribe('signal', (message) => this.#inComingSignal(message, star));
|
|
27461
|
+
}
|
|
27462
|
+
#starJoined = (id) => {
|
|
27463
|
+
if (this.#stars[id]) {
|
|
27464
|
+
this.#stars[id].close(0);
|
|
27465
|
+
delete this.#stars[id];
|
|
27466
|
+
}
|
|
27467
|
+
console.log(`star ${id} joined`);
|
|
27468
|
+
};
|
|
27469
|
+
#starLeft = async (id) => {
|
|
27470
|
+
if (this.#stars[id]) {
|
|
27471
|
+
this.#stars[id].close(0);
|
|
27472
|
+
delete this.#stars[id];
|
|
27473
|
+
}
|
|
27474
|
+
if (Object.keys(this.#stars).length === 0) {
|
|
27475
|
+
for (const star of this.starsConfig) {
|
|
27476
|
+
try {
|
|
27477
|
+
const socketClient = await new SocketRequestClient(star, this.networkVersion).init();
|
|
27478
|
+
if (!socketClient?.client?.OPEN)
|
|
27479
|
+
return;
|
|
27480
|
+
this.#stars[star] = socketClient;
|
|
27481
|
+
this.#stars[star].send({
|
|
27482
|
+
url: 'join',
|
|
27483
|
+
params: { peerId: this.peerId, version: this.version }
|
|
27484
|
+
});
|
|
27485
|
+
this.setupStarListeners(socketClient);
|
|
27486
|
+
}
|
|
27487
|
+
catch (e) {
|
|
27488
|
+
if (this.starsConfig.indexOf(star) === this.starsConfig.length - 1)
|
|
27489
|
+
throw new Error(`No star available to connect`);
|
|
27490
|
+
}
|
|
27491
|
+
}
|
|
27492
|
+
}
|
|
27493
|
+
globalThis.debug(`star ${id} left`);
|
|
27494
|
+
};
|
|
27495
|
+
#peerLeft(peer, star) {
|
|
27496
|
+
const id = peer.peerId || peer;
|
|
27497
|
+
if (this.#connections[id]) {
|
|
27498
|
+
this.#connections[id].destroy();
|
|
27499
|
+
delete this.#connections[id];
|
|
27500
|
+
}
|
|
27501
|
+
globalThis.debug(`peer ${id} left`);
|
|
27502
|
+
}
|
|
27503
|
+
#createRTCPeerConnection = (peerId, star, version, initiator = false) => {
|
|
27504
|
+
const peer = new Peer({
|
|
27505
|
+
initiator: initiator,
|
|
27506
|
+
from: this.peerId,
|
|
27507
|
+
to: peerId,
|
|
27508
|
+
version
|
|
27509
|
+
});
|
|
27510
|
+
peer.on('signal', (signal) => this.#peerSignal(peer, signal, star));
|
|
27511
|
+
peer.on('connect', () => this.#peerConnect(peer));
|
|
27512
|
+
peer.on('close', () => this.#peerClose(peer));
|
|
27513
|
+
peer.on('data', (data) => this.#peerData(peer, data));
|
|
27514
|
+
peer.on('error', (error) => this.#peerError(peer, error));
|
|
27515
|
+
this.#connections[peerId] = peer;
|
|
27516
|
+
};
|
|
27517
|
+
#peerJoined = async ({ peerId, version }, star) => {
|
|
27518
|
+
// check if peer rejoined before the previous connection closed
|
|
27519
|
+
if (this.#connections[peerId]) {
|
|
27520
|
+
if (this.#connections[peerId].connected)
|
|
27521
|
+
this.#connections[peerId].destroy();
|
|
27522
|
+
delete this.#connections[peerId];
|
|
27523
|
+
}
|
|
27524
|
+
// RTCPeerConnection
|
|
27525
|
+
this.#createRTCPeerConnection(peerId, star, version, true);
|
|
27526
|
+
globalThis.debug(`peer ${peerId} joined`);
|
|
27527
|
+
};
|
|
27528
|
+
#inComingSignal = async ({ from, signal, channelName, version }, star) => {
|
|
27529
|
+
if (version !== this.version) {
|
|
27530
|
+
console.warn(`${from} joined using the wrong version.\nexpected: ${this.version} but got:${version}`);
|
|
27372
27531
|
return;
|
|
27373
|
-
|
|
27374
|
-
|
|
27532
|
+
}
|
|
27533
|
+
let peer = this.#connections[from];
|
|
27534
|
+
if (!peer) {
|
|
27535
|
+
this.#createRTCPeerConnection(from, star, version);
|
|
27536
|
+
peer = this.#connections[from];
|
|
27537
|
+
}
|
|
27538
|
+
if (String(peer.channelName) !== String(channelName))
|
|
27539
|
+
console.warn(`channelNames don't match: got ${peer.channelName}, expected: ${channelName}`);
|
|
27540
|
+
peer.signal(signal);
|
|
27541
|
+
};
|
|
27542
|
+
#peerSignal = (peer, signal, star) => {
|
|
27543
|
+
let client = this.#stars[star];
|
|
27544
|
+
if (!client)
|
|
27545
|
+
client = this.#stars[Object.keys(this.#stars)[0]];
|
|
27546
|
+
client.send({
|
|
27547
|
+
url: 'signal',
|
|
27548
|
+
params: {
|
|
27549
|
+
from: this.peerId,
|
|
27550
|
+
to: peer.peerId,
|
|
27551
|
+
channelName: peer.channelName,
|
|
27552
|
+
version: this.version,
|
|
27553
|
+
signal
|
|
27554
|
+
}
|
|
27555
|
+
});
|
|
27556
|
+
};
|
|
27557
|
+
#peerClose = (peer) => {
|
|
27558
|
+
if (this.#connections[peer.peerId]) {
|
|
27559
|
+
this.#connections[peer.peerId].destroy();
|
|
27560
|
+
delete this.#connections[peer.peerId];
|
|
27561
|
+
}
|
|
27562
|
+
globalThis.debug(`closed ${peer.peerId}'s connection`);
|
|
27563
|
+
};
|
|
27564
|
+
#peerConnect = (peer) => {
|
|
27565
|
+
globalThis.debug(`${peer.peerId} connected`);
|
|
27566
|
+
globalThis.pubsub.publish(this.#connectEvent, peer.peerId);
|
|
27567
|
+
};
|
|
27568
|
+
#noticeMessage = (message, id) => {
|
|
27569
|
+
if (globalThis.pubsub.subscribers[id]) {
|
|
27570
|
+
globalThis.pubsub.publish(id, new Uint8Array(message));
|
|
27571
|
+
}
|
|
27572
|
+
else {
|
|
27573
|
+
globalThis.pubsub.publish('peer:data', new Uint8Array(message));
|
|
27574
|
+
}
|
|
27575
|
+
};
|
|
27576
|
+
#peerData = (peer, data) => {
|
|
27577
|
+
const { id, size, chunk } = JSON.parse(new TextDecoder().decode(data));
|
|
27578
|
+
peer.bw.down += size;
|
|
27579
|
+
if (size <= MAX_MESSAGE_SIZE) {
|
|
27580
|
+
this.#noticeMessage(chunk, id);
|
|
27581
|
+
}
|
|
27582
|
+
else {
|
|
27583
|
+
if (!this.#messagesToHandle[id])
|
|
27584
|
+
this.#messagesToHandle[id] = [];
|
|
27585
|
+
this.#messagesToHandle[id] = [
|
|
27586
|
+
...this.#messagesToHandle[id],
|
|
27587
|
+
...Object.values(chunk)
|
|
27588
|
+
];
|
|
27589
|
+
if (this.#messagesToHandle[id].length === Number(size)) {
|
|
27590
|
+
this.#noticeMessage(this.#messagesToHandle[id], id);
|
|
27591
|
+
delete this.#messagesToHandle[id];
|
|
27592
|
+
}
|
|
27593
|
+
}
|
|
27594
|
+
};
|
|
27595
|
+
#peerError = (peer, error) => {
|
|
27596
|
+
console.warn(`Connection error: ${error.message}`);
|
|
27597
|
+
peer.destroy();
|
|
27598
|
+
};
|
|
27599
|
+
async close() {
|
|
27600
|
+
for (const star in this.#stars) {
|
|
27601
|
+
if (this.#stars[star].connectionState() === 'open')
|
|
27602
|
+
await this.#stars[star].send({ url: 'leave', params: this.peerId });
|
|
27603
|
+
}
|
|
27604
|
+
const promises = [
|
|
27605
|
+
Object.values(this.#connections).map((connection) => connection.destroy()),
|
|
27606
|
+
Object.values(this.#stars).map((connection) => connection.close(0))
|
|
27607
|
+
];
|
|
27608
|
+
await Promise.allSettled(promises);
|
|
27609
|
+
}
|
|
27610
|
+
};
|
|
27375
27611
|
/**
|
|
27376
27612
|
* @access public
|
|
27377
27613
|
* @type {PeernetClient}
|
|
27378
27614
|
*/
|
|
27379
|
-
|
|
27615
|
+
console.log(this.stars);
|
|
27616
|
+
this.client = new importee.default({
|
|
27617
|
+
peerId: this.id,
|
|
27618
|
+
networkVersion: this.networkVersion,
|
|
27619
|
+
version: this.version,
|
|
27620
|
+
stars: this.stars
|
|
27621
|
+
});
|
|
27380
27622
|
this.#started = true;
|
|
27381
27623
|
this.#starting = false;
|
|
27382
27624
|
}
|
|
27383
|
-
|
|
27384
|
-
|
|
27385
|
-
|
|
27386
|
-
|
|
27387
|
-
|
|
27388
|
-
|
|
27389
|
-
|
|
27390
|
-
}
|
|
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
|
+
// }
|
|
27391
27634
|
addRequestHandler(name, method) {
|
|
27392
27635
|
this.requestProtos[name] = method;
|
|
27393
27636
|
}
|
|
@@ -27491,7 +27734,7 @@ class Peernet {
|
|
|
27491
27734
|
this.dht.addProvider(peerInfo, proto.decoded.hash);
|
|
27492
27735
|
};
|
|
27493
27736
|
let walks = [];
|
|
27494
|
-
for (const [peerId, peer] of Object.entries(this
|
|
27737
|
+
for (const [peerId, peer] of Object.entries(this.connections)) {
|
|
27495
27738
|
if (peerId !== this.id) {
|
|
27496
27739
|
walks.push(walk(peer, peerId));
|
|
27497
27740
|
}
|
|
@@ -27558,7 +27801,7 @@ class Peernet {
|
|
|
27558
27801
|
if (!closestPeer || !closestPeer.id)
|
|
27559
27802
|
return this.requestData(hash, store?.name || store);
|
|
27560
27803
|
const id = closestPeer.id;
|
|
27561
|
-
const peer = this
|
|
27804
|
+
const peer = this.connections[id];
|
|
27562
27805
|
if (peer?.connected) {
|
|
27563
27806
|
let data = await new globalThis.peernet.protos['peernet-data']({
|
|
27564
27807
|
hash,
|
|
@@ -27572,7 +27815,7 @@ class Peernet {
|
|
|
27572
27815
|
const promises = [];
|
|
27573
27816
|
const providers = await this.providersFor(hash, store);
|
|
27574
27817
|
for (const provider of Object.values(providers)) {
|
|
27575
|
-
const peer = this
|
|
27818
|
+
const peer = this.connections[provider.id];
|
|
27576
27819
|
if (peer)
|
|
27577
27820
|
promises.push(peer.request(node.encoded));
|
|
27578
27821
|
}
|
|
@@ -27786,7 +28029,7 @@ class Peernet {
|
|
|
27786
28029
|
// globalSub.publish(topic, data)
|
|
27787
28030
|
const id = Math.random().toString(36).slice(-12);
|
|
27788
28031
|
data = await new globalThis.peernet.protos['peernet-ps']({ data, topic });
|
|
27789
|
-
for (const [peerId, peer] of Object.entries(this
|
|
28032
|
+
for (const [peerId, peer] of Object.entries(this.connections)) {
|
|
27790
28033
|
if (peerId !== this.id) {
|
|
27791
28034
|
const node = await this.prepareMessage(data);
|
|
27792
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';
|