@leofcoin/peernet 1.1.99 → 1.1.100

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.
@@ -1,4 +1,4 @@
1
- import { L as LittlePubSub } from './peernet-BQUef8u4.js';
1
+ import { L as LittlePubSub } from './peernet-CP4375lb.js';
2
2
  import './identity-Cn0iQbY3.js';
3
3
  import './value-C3vAp-wb.js';
4
4
 
@@ -341,16 +341,16 @@ class Peer extends SimplePeer {
341
341
  channelName;
342
342
  version;
343
343
  bw = { up: 0, down: 0 };
344
+ get connected() {
345
+ return super.connected;
346
+ }
344
347
  constructor(options) {
345
- const { from, to, trickle, config, version } = options;
346
- // Canonical initiator and channel label based on sorted IDs
347
- const [a, b] = [from, to].sort();
348
- const channelName = `${a}:${b}`;
349
- const initiator = from === a;
348
+ const { from, to, initiator, trickle, config, version } = options;
349
+ const channelName = initiator ? `${from}:${to}` : `${to}:${from}`;
350
350
  super({
351
351
  channelName,
352
352
  initiator,
353
- trickle: trickle ?? true,
353
+ trickle: trickle || true,
354
354
  config: { iceServers, ...config },
355
355
  wrtc: globalThis.wrtc
356
356
  });
@@ -661,7 +661,7 @@ class Client {
661
661
  }
662
662
  debug(`peer ${id} left`);
663
663
  };
664
- connect(peerId, star) {
664
+ connect(peerId, star, initiator = true) {
665
665
  if (this.#connections[peerId]) {
666
666
  debug(`peer ${peerId} already connected`);
667
667
  return;
@@ -670,15 +670,16 @@ class Client {
670
670
  console.warn(`Star ${star} is not connected, cannot reconnect to peer ${peerId}`);
671
671
  return;
672
672
  }
673
- this.#createRTCPeerConnection(peerId, star, this.version);
673
+ this.#createRTCPeerConnection(peerId, star, this.version, initiator);
674
674
  }
675
- reconnect(peerId, star) {
675
+ reconnect(peerId, star, initiator = false) {
676
676
  delete this.#connections[peerId];
677
677
  debug(`reconnecting to peer ${peerId}`);
678
- return this.connect(peerId, star);
678
+ return this.connect(peerId, star, initiator);
679
679
  }
680
- #createRTCPeerConnection = (peerId, star, version) => {
680
+ #createRTCPeerConnection = (peerId, star, version, initiator = false) => {
681
681
  const peer = new Peer({
682
+ initiator: initiator,
682
683
  from: this.peerId,
683
684
  to: peerId,
684
685
  version
@@ -697,7 +698,7 @@ class Client {
697
698
  delete this.#connections[peerId];
698
699
  }
699
700
  if (this.peerId !== peerId)
700
- this.#createRTCPeerConnection(peerId, star, version);
701
+ this.#createRTCPeerConnection(peerId, star, version, true);
701
702
  debug(`peer ${peerId} joined`);
702
703
  };
703
704
  #inComingSignal = async ({ from, signal, channelName, version }, star) => {
@@ -0,0 +1,10 @@
1
+ /**
2
+ * @extends {CodecFormat}
3
+ */
4
+ export default class PeernetFileLink {
5
+ /**
6
+ * @param {Buffer|String|Object|DataMessage} data - The data needed to create the DataMessage
7
+ */
8
+ constructor(data: Buffer | string | any | DataMessage);
9
+ get messageName(): string;
10
+ }
@@ -0,0 +1,206 @@
1
+ import { F as FormatInterface } from './peernet-CP4375lb.js';
2
+ import './identity-Cn0iQbY3.js';
3
+ import './value-C3vAp-wb.js';
4
+
5
+ var proto$b = {
6
+ data: new Uint8Array(),
7
+ signature: new Uint8Array(),
8
+ 'from?': String(),
9
+ 'to?': String(),
10
+ 'id?': String()
11
+ };
12
+
13
+ class PeernetMessage extends FormatInterface {
14
+ get messageName() {
15
+ return 'PeernetMessage';
16
+ }
17
+ constructor(buffer) {
18
+ const name = 'peernet-message';
19
+ super(buffer, proto$b, { name });
20
+ }
21
+ }
22
+
23
+ var proto$a = {
24
+ hash: String(),
25
+ 'store?': String()
26
+ };
27
+
28
+ /**
29
+ * @example `
30
+ new DHTMessage(hash, store)
31
+ // store = optional if not set, peernet checks every store
32
+ let message = new DHTMessage('hashmvbs124xcfd...', 'transaction')
33
+ message = new DHTMessage('hashmvbs124xcfd...', 'block')
34
+ `
35
+ */
36
+ class DHTMessage extends FormatInterface {
37
+ get messageName() {
38
+ return 'PeernetDHTMessage';
39
+ }
40
+ constructor(data) {
41
+ const name = 'peernet-dht';
42
+ super(data, proto$a, { name });
43
+ }
44
+ }
45
+
46
+ var proto$9 = {
47
+ hash: String(),
48
+ has: Boolean()
49
+ };
50
+
51
+ class DHTMessageResponse extends FormatInterface {
52
+ get messageName() {
53
+ return 'PeernetDHTMessageResponse';
54
+ }
55
+ constructor(data) {
56
+ const name = 'peernet-dht-response';
57
+ super(data, proto$9, { name });
58
+ }
59
+ }
60
+
61
+ var proto$8 = {
62
+ hash: String(),
63
+ 'store?': String()
64
+ };
65
+
66
+ /**
67
+ * @extends {CodecFormat}
68
+ */
69
+ class DataMessage extends FormatInterface {
70
+ get messageName() {
71
+ return 'PeernetDataMessage';
72
+ }
73
+ /**
74
+ * @param {Buffer|String|Object|DataMessage} data - The data needed to create the DataMessage
75
+ */
76
+ constructor(data) {
77
+ super(data, proto$8, { name: 'peernet-data' });
78
+ }
79
+ }
80
+
81
+ var proto$7 = {
82
+ data: new Uint8Array(),
83
+ topic: new Uint8Array()
84
+ };
85
+
86
+ class PsMessage extends FormatInterface {
87
+ get messageName() {
88
+ return 'PsMessage';
89
+ }
90
+ constructor(buffer) {
91
+ const name = 'peernet-ps';
92
+ super(buffer, proto$7, { name });
93
+ }
94
+ }
95
+
96
+ var proto$6 = {
97
+ id: String()
98
+ };
99
+
100
+ class PeerMessage extends FormatInterface {
101
+ get messageName() {
102
+ return 'PeernetPeerMessage';
103
+ }
104
+ constructor(data) {
105
+ const name = 'peernet-peer';
106
+ super(data, proto$6, { name });
107
+ }
108
+ }
109
+
110
+ var proto$5 = {
111
+ request: String(),
112
+ 'requested?': new Uint8Array()
113
+ };
114
+
115
+ class RequestMessage extends FormatInterface {
116
+ get messageName() {
117
+ return 'PeernetRequestMessage';
118
+ }
119
+ constructor(data) {
120
+ const name = 'peernet-request';
121
+ super(data, proto$5, { name });
122
+ }
123
+ }
124
+
125
+ var proto$4 = {
126
+ response: {}
127
+ };
128
+
129
+ class ResponseMessage extends FormatInterface {
130
+ get messageName() {
131
+ return 'PeernetResponseMessage';
132
+ }
133
+ constructor(data) {
134
+ const name = 'peernet-response';
135
+ super(data, proto$4, { name });
136
+ }
137
+ }
138
+
139
+ var proto$3 = {
140
+ id: String()
141
+ };
142
+
143
+ class PeerMessageResponse extends FormatInterface {
144
+ get messageName() {
145
+ return 'PeernetPeerMessageResponse';
146
+ }
147
+ constructor(data) {
148
+ const name = 'peernet-peer-response';
149
+ super(data, proto$3, { name });
150
+ }
151
+ }
152
+
153
+ var proto$2 = {
154
+ hash: String(),
155
+ data: new Uint8Array()
156
+ };
157
+
158
+ class DataMessageResponse extends FormatInterface {
159
+ get messageName() {
160
+ return 'PeernetDataMessageResponse';
161
+ }
162
+ constructor(data) {
163
+ const name = 'peernet-data-response';
164
+ super(data, proto$2, { name });
165
+ }
166
+ }
167
+
168
+ var proto$1 = {
169
+ value: String(),
170
+ author: String(),
171
+ timestamp: Number(),
172
+ files: [String()]
173
+ };
174
+
175
+ class ChatMessage extends FormatInterface {
176
+ get messageName() {
177
+ return 'ChatMessage';
178
+ }
179
+ constructor(buffer) {
180
+ const name = 'chat-message';
181
+ super(buffer, proto$1, { name });
182
+ }
183
+ }
184
+
185
+ var proto = {
186
+ path: String(),
187
+ 'content?': '',
188
+ 'links?': []
189
+ };
190
+
191
+ /**
192
+ * @extends {CodecFormat}
193
+ */
194
+ class PeernetFile extends FormatInterface {
195
+ get messageName() {
196
+ return 'PeernetFile';
197
+ }
198
+ /**
199
+ * @param {Buffer|String|Object|DataMessage} data - The data needed to create the DataMessage
200
+ */
201
+ constructor(data) {
202
+ super(data, proto, { name: 'peernet-file' });
203
+ }
204
+ }
205
+
206
+ export { ChatMessage, DHTMessage, DHTMessageResponse, DataMessage, DataMessageResponse, PeerMessage, PeerMessageResponse, PeernetFile, PeernetMessage, PsMessage, RequestMessage, ResponseMessage };
@@ -0,0 +1,4 @@
1
+ export default class PeerInfo {
2
+ constructor(data: any, options: any);
3
+ keys: string[];
4
+ }
@@ -7820,54 +7820,55 @@ const FormatInterface = FormatInterface$1;
7820
7820
  const Codec = Codec$1;
7821
7821
 
7822
7822
  const BufferToUint8Array = (data) => {
7823
- if (data.type === 'Buffer') {
7824
- data = new Uint8Array(data.data);
7825
- }
7826
- return data
7823
+ if (data.type === 'Buffer') {
7824
+ data = new Uint8Array(data.data);
7825
+ }
7826
+ return data;
7827
7827
  };
7828
-
7829
7828
  const protoFor = (message) => {
7830
- const codec = new Codec(message);
7831
- if (!codec.name) throw new Error(`proto not found ${message}`)
7832
- const Proto = globalThis.peernet.protos[codec.name];
7833
- if (!Proto) throw new Error(`No proto defined for ${codec.name}`)
7834
- return new Proto(message)
7829
+ const codec = new Codec(message);
7830
+ if (!codec.name)
7831
+ throw new Error(`proto not found ${message}`);
7832
+ const Proto = globalThis.peernet.protos[codec.name];
7833
+ if (!Proto)
7834
+ throw new Error(`No proto defined for ${codec.name}`);
7835
+ return new Proto(message);
7835
7836
  };
7836
-
7837
7837
  /**
7838
7838
  * wether or not a peernet daemon is active
7839
7839
  * @return {Boolean}
7840
7840
  */
7841
7841
  const hasDaemon = async () => {
7842
- try {
7843
- let response = await fetch('http://127.0.0.1:1000/api/version');
7844
- response = await response.json();
7845
- return Boolean(response.client === '@peernet/api/http')
7846
- } catch (e) {
7847
- return false
7848
- }
7842
+ try {
7843
+ let response = await fetch('http://127.0.0.1:1000/api/version');
7844
+ response = await response.json();
7845
+ return Boolean(response.client === '@peernet/api/http');
7846
+ }
7847
+ catch (e) {
7848
+ return false;
7849
+ }
7849
7850
  };
7850
-
7851
7851
  const https = () => {
7852
- if (!globalThis.location) return false
7853
- return Boolean(globalThis.location.protocol === 'https:')
7852
+ if (!globalThis.location)
7853
+ return false;
7854
+ return Boolean(globalThis.location.protocol === 'https:');
7854
7855
  };
7855
-
7856
7856
  /**
7857
7857
  * Get current environment
7858
7858
  * @return {String} current environment [node, electron, browser]
7859
7859
  */
7860
7860
  const environment = () => {
7861
- const _navigator = globalThis.navigator;
7862
- if (!_navigator) {
7863
- return 'node'
7864
- } else if (_navigator && /electron/i.test(_navigator.userAgent)) {
7865
- return 'electron'
7866
- } else {
7867
- return 'browser'
7868
- }
7861
+ const _navigator = globalThis.navigator;
7862
+ if (!_navigator) {
7863
+ return 'node';
7864
+ }
7865
+ else if (_navigator && /electron/i.test(_navigator.userAgent)) {
7866
+ return 'electron';
7867
+ }
7868
+ else {
7869
+ return 'browser';
7870
+ }
7869
7871
  };
7870
-
7871
7872
  /**
7872
7873
  * * Get current environment
7873
7874
  * @return {Object} result
@@ -7875,84 +7876,84 @@ const environment = () => {
7875
7876
  * @property {Boolean} reult.environment Current environment
7876
7877
  */
7877
7878
  const target = async () => {
7878
- let daemon = false;
7879
- if (!https()) daemon = await hasDaemon();
7880
-
7881
- return { daemon, environment: environment() }
7879
+ let daemon = false;
7880
+ if (!https())
7881
+ daemon = await hasDaemon();
7882
+ return { daemon, environment: environment() };
7882
7883
  };
7883
7884
 
7884
7885
  class PeerDiscovery {
7885
- constructor(id) {
7886
- this.id = id;
7887
- }
7888
-
7889
- _getPeerId(id) {
7890
- if (!peernet.peerMap || (peernet.peerMap && peernet.peerMap.size === 0)) return false
7891
-
7892
- for (const entry of [...peernet.peerMap.entries()]) {
7893
- for (const _id of entry[1]) {
7894
- if (_id === id) return entry[0]
7895
- }
7886
+ constructor(id) {
7887
+ this.id = id;
7896
7888
  }
7897
- }
7898
-
7899
- async discover(peer) {
7900
- let id = this._getPeerId(peer.id);
7901
- if (id) return id
7902
- const data = await new peernet.protos['peernet-peer']({ id: this.id });
7903
- const node = await peernet.prepareMessage(peer.id, data.encoded);
7904
-
7905
- let response = await peer.request(node.encoded);
7906
- response = await protoFor(response);
7907
- response = await new peernet.protos['peernet-peer-response'](response.decoded.data);
7908
-
7909
- id = response.decoded.id;
7910
- if (id === this.id) return
7911
-
7912
- if (!peernet.peerMap.has(id)) peernet.peerMap.set(id, [peer.id]);
7913
- else {
7914
- const connections = peernet.peerMap.get(id);
7915
- if (connections.indexOf(peer.id) === -1) {
7916
- connections.push(peer.id);
7917
- peernet.peerMap.set(peer.id, connections);
7918
- }
7889
+ _getPeerId(id) {
7890
+ if (!peernet.peerMap || (peernet.peerMap && peernet.peerMap.size === 0))
7891
+ return false;
7892
+ for (const entry of [...peernet.peerMap.entries()]) {
7893
+ for (const _id of entry[1]) {
7894
+ if (_id === id)
7895
+ return entry[0];
7896
+ }
7897
+ }
7919
7898
  }
7920
- return id
7921
- }
7922
-
7923
- async discoverHandler(message, peer) {
7924
- const { id, proto } = message;
7925
- // if (typeof message.data === 'string') message.data = Buffer.from(message.data)
7926
- if (proto.name === 'peernet-peer') {
7927
- const from = proto.decoded.id;
7928
- if (from === this.id) return
7929
-
7930
- if (!peernet.peerMap.has(from)) peernet.peerMap.set(from, [peer.id]);
7931
- else {
7932
- const connections = peernet.peerMap.get(from);
7933
- if (connections.indexOf(peer.id) === -1) {
7934
- connections.push(peer.id);
7935
- peernet.peerMap.set(from, connections);
7899
+ async discover(peer) {
7900
+ let id = this._getPeerId(peer.id);
7901
+ if (id)
7902
+ return id;
7903
+ const data = await new peernet.protos['peernet-peer']({ id: this.id });
7904
+ const node = await peernet.prepareMessage(peer.id, data.encoded);
7905
+ let response = await peer.request(node.encoded);
7906
+ response = await protoFor(response);
7907
+ response = await new peernet.protos['peernet-peer-response'](response.decoded.data);
7908
+ id = response.decoded.id;
7909
+ if (id === this.id)
7910
+ return;
7911
+ if (!peernet.peerMap.has(id))
7912
+ peernet.peerMap.set(id, [peer.id]);
7913
+ else {
7914
+ const connections = peernet.peerMap.get(id);
7915
+ if (connections.indexOf(peer.id) === -1) {
7916
+ connections.push(peer.id);
7917
+ peernet.peerMap.set(peer.id, connections);
7918
+ }
7936
7919
  }
7937
- }
7938
- const data = await new peernet.protos['peernet-peer-response']({ id: this.id });
7939
- const node = await peernet.prepareMessage(from, data.encoded);
7940
-
7941
- peer.write(Buffer.from(JSON.stringify({ id, data: node.encoded })));
7942
- } else if (proto.name === 'peernet-peer-response') {
7943
- const from = proto.decoded.id;
7944
- if (from === this.id) return
7945
-
7946
- if (!peernet.peerMap.has(from)) peernet.peerMap.set(from, [peer.id]);
7947
- else {
7948
- const connections = peernet.peerMap.get(from);
7949
- if (connections.indexOf(peer.id) === -1) {
7950
- connections.push(peer.id);
7951
- peernet.peerMap.set(from, connections);
7920
+ return id;
7921
+ }
7922
+ async discoverHandler(message, peer) {
7923
+ const { id, proto } = message;
7924
+ // if (typeof message.data === 'string') message.data = Buffer.from(message.data)
7925
+ if (proto.name === 'peernet-peer') {
7926
+ const from = proto.decoded.id;
7927
+ if (from === this.id)
7928
+ return;
7929
+ if (!peernet.peerMap.has(from))
7930
+ peernet.peerMap.set(from, [peer.id]);
7931
+ else {
7932
+ const connections = peernet.peerMap.get(from);
7933
+ if (connections.indexOf(peer.id) === -1) {
7934
+ connections.push(peer.id);
7935
+ peernet.peerMap.set(from, connections);
7936
+ }
7937
+ }
7938
+ const data = await new peernet.protos['peernet-peer-response']({ id: this.id });
7939
+ const node = await peernet.prepareMessage(from, data.encoded);
7940
+ peer.write(Buffer.from(JSON.stringify({ id, data: node.encoded })));
7941
+ }
7942
+ else if (proto.name === 'peernet-peer-response') {
7943
+ const from = proto.decoded.id;
7944
+ if (from === this.id)
7945
+ return;
7946
+ if (!peernet.peerMap.has(from))
7947
+ peernet.peerMap.set(from, [peer.id]);
7948
+ else {
7949
+ const connections = peernet.peerMap.get(from);
7950
+ if (connections.indexOf(peer.id) === -1) {
7951
+ connections.push(peer.id);
7952
+ peernet.peerMap.set(from, connections);
7953
+ }
7954
+ }
7952
7955
  }
7953
- }
7954
7956
  }
7955
- }
7956
7957
  }
7957
7958
 
7958
7959
  /**
@@ -8054,59 +8055,57 @@ class DhtEarth {
8054
8055
  }
8055
8056
 
8056
8057
  class MessageHandler {
8057
- constructor(network) {
8058
- this.network = network;
8059
- }
8060
- /**
8061
- * hash and sign message
8062
- *
8063
- * @param {object} message
8064
- * @param {Buffer} message.from peer id
8065
- * @param {Buffer} message.to peer id
8066
- * @param {string} message.data Peernet message
8067
- * (PeernetMessage excluded) encoded as a string
8068
- * @return message
8069
- */
8070
- async hashAndSignMessage(message) {
8071
- const hash = await message.peernetHash;
8072
- message.decoded.signature = globalThis.identity.sign(hash.buffer);
8073
- return message
8074
- }
8075
-
8076
- /**
8077
- * @param {String} from - peer id
8078
- * @param {String} to - peer id
8079
- * @param {String|PeernetMessage} data - data encoded message string
8080
- * or the messageNode itself
8081
- */
8082
- async prepareMessage(message) {
8083
- if (message.keys.includes('signature')) {
8084
- message = await this.hashAndSignMessage(message);
8058
+ constructor(network) {
8059
+ this.network = network;
8060
+ }
8061
+ /**
8062
+ * hash and sign message
8063
+ *
8064
+ * @param {object} message
8065
+ * @param {Buffer} message.from peer id
8066
+ * @param {Buffer} message.to peer id
8067
+ * @param {string} message.data Peernet message
8068
+ * (PeernetMessage excluded) encoded as a string
8069
+ * @return message
8070
+ */
8071
+ async hashAndSignMessage(message) {
8072
+ const hash = await message.peernetHash;
8073
+ message.decoded.signature = globalThis.identity.sign(hash.buffer);
8074
+ return message;
8075
+ }
8076
+ /**
8077
+ * @param {String} from - peer id
8078
+ * @param {String} to - peer id
8079
+ * @param {String|PeernetMessage} data - data encoded message string
8080
+ * or the messageNode itself
8081
+ */
8082
+ async prepareMessage(message) {
8083
+ if (message.keys.includes('signature')) {
8084
+ message = await this.hashAndSignMessage(message);
8085
+ }
8086
+ return message;
8085
8087
  }
8086
-
8087
- return message
8088
- }
8089
8088
  }
8090
8089
 
8091
8090
  const dataHandler = async (message) => {
8092
- if (!message) return
8093
-
8094
- try {
8095
- const { data, id, from, peer } = message;
8096
- const proto = await protoFor(data);
8097
- peernet._protoHandler({ id, proto }, peernet.connections[from] || peer, from);
8098
- } catch (error) {
8099
- console.error(error);
8100
- }
8091
+ if (!message)
8092
+ return;
8093
+ try {
8094
+ const { data, id, from, peer } = message;
8095
+ const proto = await protoFor(data);
8096
+ peernet._protoHandler({ id, proto }, peernet.connections[from] || peer, from);
8097
+ }
8098
+ catch (error) {
8099
+ console.error(error);
8100
+ }
8101
8101
  };
8102
8102
 
8103
8103
  const dhtError = (proto) => {
8104
- const text = `Received proto ${proto.name} expected peernet-dht-response`;
8105
- return new Error(`Routing error: ${text}`)
8104
+ const text = `Received proto ${proto.name} expected peernet-dht-response`;
8105
+ return new Error(`Routing error: ${text}`);
8106
8106
  };
8107
-
8108
8107
  const nothingFoundError = (hash) => {
8109
- return new Error(`nothing found for ${hash}`)
8108
+ return new Error(`nothing found for ${hash}`);
8110
8109
  };
8111
8110
 
8112
8111
  const isBrowser = globalThis.process?.versions?.node ? false : true;
@@ -8367,7 +8366,7 @@ class Peernet {
8367
8366
  this.root = options.root;
8368
8367
  const { RequestMessage, ResponseMessage, PeerMessage, PeerMessageResponse, PeernetMessage, DHTMessage, DHTMessageResponse, DataMessage, DataMessageResponse, PsMessage, ChatMessage, PeernetFile
8369
8368
  // FolderMessageResponse
8370
- } = await import(/* webpackChunkName: "messages" */ './messages-B2WVXv2a.js');
8369
+ } = await import(/* webpackChunkName: "messages" */ './messages-Bx4w-Pqo.js');
8371
8370
  /**
8372
8371
  * proto Object containing protos
8373
8372
  * @type {Object}
@@ -8461,7 +8460,7 @@ class Peernet {
8461
8460
  if (this.#starting || this.#started)
8462
8461
  return;
8463
8462
  this.#starting = true;
8464
- const importee = await import('./client-BfWOZz0W.js');
8463
+ const importee = await import('./client-DgmVTCEc.js');
8465
8464
  /**
8466
8465
  * @access public
8467
8466
  * @type {PeernetClient}
@@ -1,3 +1,3 @@
1
- export { P as default } from './peernet-BQUef8u4.js';
1
+ export { P as default } from './peernet-CP4375lb.js';
2
2
  import './identity-Cn0iQbY3.js';
3
3
  import './value-C3vAp-wb.js';
@@ -0,0 +1,2 @@
1
+ declare function _default(): Promise<string>;
2
+ export default _default;
@@ -0,0 +1,2 @@
1
+ declare function _default(): Promise<any>;
2
+ export default _default;
@@ -0,0 +1,6 @@
1
+ declare const _default: {
2
+ hash: string;
3
+ path: string;
4
+ 'size?': number;
5
+ };
6
+ export default _default;