@leofcoin/chain 1.5.39 → 1.5.40

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.
@@ -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 };
@@ -1,4 +1,4 @@
1
- import { M as MultiWallet, e as encrypt, b as base58$1 } from './node-browser-e7da3b14.js';
1
+ import { M as MultiWallet, e as encrypt, b as base58$1 } from './node-browser-688295ca.js';
2
2
  import './index-526fd466.js';
3
3
 
4
4
  /**
@@ -1,4 +1,4 @@
1
- import { F as FormatInterface } from './node-browser-e7da3b14.js';
1
+ import { F as FormatInterface } from './node-browser-688295ca.js';
2
2
  import './index-526fd466.js';
3
3
 
4
4
  var proto$b = {
@@ -27178,7 +27178,7 @@ class Identity {
27178
27178
  this.selectedAccount = new TextDecoder().decode(selected);
27179
27179
  }
27180
27180
  else {
27181
- const importee = await import(/* webpackChunkName: "generate-account" */ './index-34c93510-db72e006.js');
27181
+ const importee = await import(/* webpackChunkName: "generate-account" */ './index-1d597e6b-2cb0f7af.js');
27182
27182
  const { identity, accounts } = await importee.default(password, this.network);
27183
27183
  await globalThis.accountStore.put('public', JSON.stringify({ walletId: identity.walletId }));
27184
27184
  await globalThis.walletStore.put('version', String(1));
@@ -27370,7 +27370,7 @@ class Peernet {
27370
27370
  this.root = options.root;
27371
27371
  const { RequestMessage, ResponseMessage, PeerMessage, PeerMessageResponse, PeernetMessage, DHTMessage, DHTMessageResponse, DataMessage, DataMessageResponse, PsMessage, ChatMessage, PeernetFile
27372
27372
  // FolderMessageResponse
27373
- } = await import(/* webpackChunkName: "messages" */ './messages-b7f41cba-61105b71.js');
27373
+ } = await import(/* webpackChunkName: "messages" */ './messages-67e5efa0-a40dcae6.js');
27374
27374
  /**
27375
27375
  * proto Object containing protos
27376
27376
  * @type {Object}
@@ -27461,12 +27461,228 @@ class Peernet {
27461
27461
  if (this.#starting || this.#started)
27462
27462
  return;
27463
27463
  this.#starting = true;
27464
- const importee = await import('./client-493cbd83-493cbd83.js');
27464
+ const importee = class Client {
27465
+ #peerId;
27466
+ #connections = {};
27467
+ #stars = {};
27468
+ id;
27469
+ networkVersion;
27470
+ starsConfig;
27471
+ socketClient;
27472
+ messageSize = 262144;
27473
+ version;
27474
+ #messagesToHandle = {};
27475
+ get peerId() {
27476
+ return this.#peerId;
27477
+ }
27478
+ get connections() {
27479
+ return { ...this.#connections };
27480
+ }
27481
+ get peers() {
27482
+ return Object.entries(this.#connections);
27483
+ }
27484
+ getPeer(peerId) {
27485
+ return this.#connections[peerId];
27486
+ }
27487
+ constructor(peerId, networkVersion = 'peach', version, stars = ['wss://peach.leofcoin.org']) {
27488
+ this.#peerId = peerId;
27489
+ this.networkVersion = networkVersion;
27490
+ this.version = version;
27491
+ this._init(stars);
27492
+ }
27493
+ async _init(stars = []) {
27494
+ this.starsConfig = stars;
27495
+ // reconnectJob()
27496
+ if (!globalThis.RTCPeerConnection)
27497
+ globalThis.wrtc = (await import('./browser-2c73e2ef-2c73e2ef.js').then(function (n) { return n.b; })).default;
27498
+ for (const star of stars) {
27499
+ try {
27500
+ const client = new SocketRequestClient(star, this.networkVersion);
27501
+ this.#stars[star] = await client.init();
27502
+ this.setupStarListeners(this.#stars[star]);
27503
+ this.#stars[star].send({
27504
+ url: 'join',
27505
+ params: { version: this.version, peerId: this.peerId }
27506
+ });
27507
+ }
27508
+ catch (e) {
27509
+ if (stars.indexOf(star) === stars.length - 1 && !this.socketClient)
27510
+ throw new Error(`No star available to connect`);
27511
+ }
27512
+ }
27513
+ if (globalThis.navigator) {
27514
+ globalThis.addEventListener('beforeunload', async () => this.close());
27515
+ }
27516
+ else {
27517
+ process.on('SIGINT', async () => {
27518
+ process.stdin.resume();
27519
+ await this.close();
27520
+ process.exit();
27521
+ });
27522
+ }
27523
+ }
27524
+ setupStarListeners(star) {
27525
+ star.pubsub.subscribe('peer:joined', (id) => this.#peerJoined(id, star));
27526
+ star.pubsub.subscribe('peer:left', (id) => this.#peerLeft(id, star));
27527
+ star.pubsub.subscribe('star:joined', this.#starJoined);
27528
+ star.pubsub.subscribe('star:left', this.#starLeft);
27529
+ star.pubsub.subscribe('signal', (message) => this.#inComingSignal(message, star));
27530
+ }
27531
+ #starJoined = (id) => {
27532
+ if (this.#stars[id]) {
27533
+ this.#stars[id].close(0);
27534
+ delete this.#stars[id];
27535
+ }
27536
+ console.log(`star ${id} joined`);
27537
+ };
27538
+ #starLeft = async (id) => {
27539
+ if (this.#stars[id]) {
27540
+ this.#stars[id].close(0);
27541
+ delete this.#stars[id];
27542
+ }
27543
+ if (Object.keys(this.#stars).length === 0) {
27544
+ for (const star of this.starsConfig) {
27545
+ try {
27546
+ const socketClient = await new SocketRequestClient(star, this.networkVersion).init();
27547
+ if (!socketClient?.client?.OPEN)
27548
+ return;
27549
+ this.#stars[star] = socketClient;
27550
+ this.#stars[star].send({
27551
+ url: 'join',
27552
+ params: { peerId: this.peerId, version: this.version }
27553
+ });
27554
+ this.setupStarListeners(socketClient);
27555
+ }
27556
+ catch (e) {
27557
+ if (this.starsConfig.indexOf(star) === this.starsConfig.length - 1)
27558
+ throw new Error(`No star available to connect`);
27559
+ }
27560
+ }
27561
+ }
27562
+ globalThis.debug(`star ${id} left`);
27563
+ };
27564
+ #peerLeft(peer, star) {
27565
+ const id = peer.peerId || peer;
27566
+ if (this.#connections[id]) {
27567
+ this.#connections[id].destroy();
27568
+ delete this.#connections[id];
27569
+ }
27570
+ globalThis.debug(`peer ${id} left`);
27571
+ }
27572
+ #createRTCPeerConnection = (peerId, star, version, initiator = false) => {
27573
+ const peer = new Peer({
27574
+ initiator: initiator,
27575
+ from: this.peerId,
27576
+ to: peerId,
27577
+ version
27578
+ });
27579
+ peer.on('signal', (signal) => this.#peerSignal(peer, signal, star));
27580
+ peer.on('connect', () => this.#peerConnect(peer));
27581
+ peer.on('close', () => this.#peerClose(peer));
27582
+ peer.on('data', (data) => this.#peerData(peer, data));
27583
+ peer.on('error', (error) => this.#peerError(peer, error));
27584
+ this.#connections[peerId] = peer;
27585
+ };
27586
+ #peerJoined = async ({ peerId, version }, star) => {
27587
+ // check if peer rejoined before the previous connection closed
27588
+ if (this.#connections[peerId]) {
27589
+ if (this.#connections[peerId].connected)
27590
+ this.#connections[peerId].destroy();
27591
+ delete this.#connections[peerId];
27592
+ }
27593
+ // RTCPeerConnection
27594
+ this.#createRTCPeerConnection(peerId, star, version, true);
27595
+ globalThis.debug(`peer ${peerId} joined`);
27596
+ };
27597
+ #inComingSignal = async ({ from, signal, channelName, version }, star) => {
27598
+ if (version !== this.version) {
27599
+ console.warn(`${from} joined using the wrong version.\nexpected: ${this.version} but got:${version}`);
27600
+ return;
27601
+ }
27602
+ let peer = this.#connections[from];
27603
+ if (!peer) {
27604
+ this.#createRTCPeerConnection(from, star, version);
27605
+ peer = this.#connections[from];
27606
+ }
27607
+ if (String(peer.channelName) !== String(channelName))
27608
+ console.warn(`channelNames don't match: got ${peer.channelName}, expected: ${channelName}`);
27609
+ peer.signal(signal);
27610
+ };
27611
+ #peerSignal = (peer, signal, star) => {
27612
+ let client = this.#stars[star];
27613
+ if (!client)
27614
+ client = this.#stars[Object.keys(this.#stars)[0]];
27615
+ client.send({
27616
+ url: 'signal',
27617
+ params: {
27618
+ from: this.peerId,
27619
+ to: peer.peerId,
27620
+ channelName: peer.channelName,
27621
+ version: this.version,
27622
+ signal
27623
+ }
27624
+ });
27625
+ };
27626
+ #peerClose = (peer) => {
27627
+ if (this.#connections[peer.peerId]) {
27628
+ this.#connections[peer.peerId].destroy();
27629
+ delete this.#connections[peer.peerId];
27630
+ }
27631
+ globalThis.debug(`closed ${peer.peerId}'s connection`);
27632
+ };
27633
+ #peerConnect = (peer) => {
27634
+ globalThis.debug(`${peer.peerId} connected`);
27635
+ globalThis.pubsub.publish('peer:connected', peer.peerId);
27636
+ };
27637
+ #noticeMessage = (message, id) => {
27638
+ if (globalThis.pubsub.subscribers[id]) {
27639
+ globalThis.pubsub.publish(id, new Uint8Array(message));
27640
+ }
27641
+ else {
27642
+ globalThis.pubsub.publish('peer:data', new Uint8Array(message));
27643
+ }
27644
+ };
27645
+ #peerData = (peer, data) => {
27646
+ const { id, size, chunk } = JSON.parse(new TextDecoder().decode(data));
27647
+ peer.bw.down += size;
27648
+ if (size <= MAX_MESSAGE_SIZE) {
27649
+ this.#noticeMessage(chunk, id);
27650
+ }
27651
+ else {
27652
+ if (!this.#messagesToHandle[id])
27653
+ this.#messagesToHandle[id] = [];
27654
+ this.#messagesToHandle[id] = [
27655
+ ...this.#messagesToHandle[id],
27656
+ ...Object.values(chunk)
27657
+ ];
27658
+ if (this.#messagesToHandle[id].length === Number(size)) {
27659
+ this.#noticeMessage(this.#messagesToHandle[id], id);
27660
+ delete this.#messagesToHandle[id];
27661
+ }
27662
+ }
27663
+ };
27664
+ #peerError = (peer, error) => {
27665
+ console.warn(`Connection error: ${error.message}`);
27666
+ peer.destroy();
27667
+ };
27668
+ async close() {
27669
+ for (const star in this.#stars) {
27670
+ if (this.#stars[star].connectionState() === 'open')
27671
+ await this.#stars[star].send({ url: 'leave', params: this.peerId });
27672
+ }
27673
+ const promises = [
27674
+ Object.values(this.#connections).map((connection) => connection.destroy()),
27675
+ Object.values(this.#stars).map((connection) => connection.close(0))
27676
+ ];
27677
+ await Promise.allSettled(promises);
27678
+ }
27679
+ };
27465
27680
  /**
27466
27681
  * @access public
27467
27682
  * @type {PeernetClient}
27468
27683
  */
27469
- this.client = new importee.default(this.id, this.networkVersion, this.stars);
27684
+ console.log(this.stars);
27685
+ this.client = new importee.default(this.id, this.networkVersion, this.version, this.stars);
27470
27686
  this.#started = true;
27471
27687
  this.#starting = false;
27472
27688
  }
@@ -1,2 +1,2 @@
1
- export { N as default } from './node-browser-e7da3b14.js';
1
+ export { N as default } from './node-browser-688295ca.js';
2
2
  import './index-526fd466.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leofcoin/chain",
3
- "version": "1.5.39",
3
+ "version": "1.5.40",
4
4
  "description": "Official javascript implementation",
5
5
  "exports": {
6
6
  "./node": {