@leofcoin/peernet 1.1.80 → 1.1.82

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.
Files changed (71) hide show
  1. package/.esdoc.json +10 -10
  2. package/.gitattributes +2 -2
  3. package/.prettierrc +7 -7
  4. package/.travis.yml +27 -27
  5. package/BREAKING_CHANGES.md +34 -34
  6. package/LICENSE +21 -21
  7. package/README.md +72 -72
  8. package/deploy.js +8 -8
  9. package/exports/browser/{client-Depp28gl.js → client-C0VVXIWm.js} +2 -2
  10. package/exports/browser/{index-DqPlTtAJ.js → index-CEwkDK9g.js} +5 -496
  11. package/exports/browser/{messages-RYLqPGkg.js → messages-BdevLRCA.js} +164 -164
  12. package/exports/browser/{peernet-B7TZP-Wg.js → peernet-DEIKLS2i.js} +185 -185
  13. package/exports/browser/peernet.js +1 -1
  14. package/exports/{messages-CRhtDipD.js → messages-BmpgEM4y.js} +163 -163
  15. package/exports/peernet.js +184 -184
  16. package/exports/src/prompts/password.js +3 -3
  17. package/index.html +19 -19
  18. package/package.json +71 -71
  19. package/rollup.config.js +63 -63
  20. package/src/dht/dht.ts +147 -147
  21. package/src/discovery/peer-discovery.js +75 -75
  22. package/src/errors/errors.js +12 -12
  23. package/src/handlers/data.js +15 -15
  24. package/src/handlers/message.js +34 -34
  25. package/src/identity.ts +104 -104
  26. package/src/messages/chat.js +13 -13
  27. package/src/messages/data-response.js +13 -13
  28. package/src/messages/data.js +17 -17
  29. package/src/messages/dht-response.js +13 -13
  30. package/src/messages/dht.js +21 -21
  31. package/src/messages/file-link.js +17 -17
  32. package/src/messages/file.js +17 -17
  33. package/src/messages/peer-response.js +13 -13
  34. package/src/messages/peer.js +13 -13
  35. package/src/messages/peernet.js +13 -13
  36. package/src/messages/ps.js +13 -13
  37. package/src/messages/request.js +13 -13
  38. package/src/messages/response.js +13 -13
  39. package/src/messages.js +13 -13
  40. package/src/peer-info.js +9 -9
  41. package/src/peernet.ts +817 -817
  42. package/src/prompts/password/browser.js +1 -1
  43. package/src/prompts/password/node.js +6 -6
  44. package/src/proto/chat-message.proto.js +6 -6
  45. package/src/proto/data-response.proto.js +4 -4
  46. package/src/proto/data.proto.js +4 -4
  47. package/src/proto/dht-response.proto.js +4 -4
  48. package/src/proto/dht.proto.js +4 -4
  49. package/src/proto/file-link.proto.js +5 -5
  50. package/src/proto/file.proto.js +5 -5
  51. package/src/proto/peer-response.proto.js +3 -3
  52. package/src/proto/peer.proto.js +3 -3
  53. package/src/proto/peernet.proto.js +7 -7
  54. package/src/proto/ps.proto.js +4 -4
  55. package/src/proto/request.proto.js +4 -4
  56. package/src/proto/response.proto.js +3 -3
  57. package/src/types.ts +25 -25
  58. package/src/utils/utils.js +77 -77
  59. package/test/client.js +14 -14
  60. package/test/codec.js +56 -56
  61. package/test/hash.js +13 -13
  62. package/test/index.js +3 -3
  63. package/test/lastBlock.js +7 -7
  64. package/test/messages.js +26 -26
  65. package/test/peernet.js +17 -17
  66. package/test/peernet.test.js +159 -159
  67. package/test.js +62 -62
  68. package/test2.js +13 -13
  69. package/test3.js +15 -15
  70. package/test4.js +7 -7
  71. package/tsconfig.json +11 -11
@@ -12098,140 +12098,140 @@ let FormatInterface$1 = class FormatInterface extends BasicInterface {
12098
12098
  const FormatInterface = FormatInterface$1;
12099
12099
  const Codec = Codec$1;
12100
12100
 
12101
- const BufferToUint8Array = (data) => {
12102
- if (data.type === 'Buffer') {
12103
- data = new Uint8Array(data.data);
12104
- }
12105
- return data
12106
- };
12107
-
12108
- const protoFor = (message) => {
12109
- const codec = new Codec(message);
12110
- if (!codec.name) throw new Error(`proto not found ${message}`)
12111
- const Proto = globalThis.peernet.protos[codec.name];
12112
- if (!Proto) throw new Error(`No proto defined for ${codec.name}`)
12113
- return new Proto(message)
12114
- };
12115
-
12116
- /**
12117
- * wether or not a peernet daemon is active
12118
- * @return {Boolean}
12119
- */
12120
- const hasDaemon = async () => {
12121
- try {
12122
- let response = await fetch('http://127.0.0.1:1000/api/version');
12123
- response = await response.json();
12124
- return Boolean(response.client === '@peernet/api/http')
12125
- } catch (e) {
12126
- return false
12127
- }
12128
- };
12129
-
12130
- const https = () => {
12131
- if (!globalThis.location) return false
12132
- return Boolean(globalThis.location.protocol === 'https:')
12133
- };
12134
-
12135
- /**
12136
- * Get current environment
12137
- * @return {String} current environment [node, electron, browser]
12138
- */
12139
- const environment = () => {
12140
- const _navigator = globalThis.navigator;
12141
- if (!_navigator) {
12142
- return 'node'
12143
- } else if (_navigator && /electron/i.test(_navigator.userAgent)) {
12144
- return 'electron'
12145
- } else {
12146
- return 'browser'
12147
- }
12148
- };
12149
-
12150
- /**
12151
- * * Get current environment
12152
- * @return {Object} result
12153
- * @property {Boolean} reult.daemon whether or not daemon is running
12154
- * @property {Boolean} reult.environment Current environment
12155
- */
12156
- const target = async () => {
12157
- let daemon = false;
12158
- if (!https()) daemon = await hasDaemon();
12159
-
12160
- return { daemon, environment: environment() }
12101
+ const BufferToUint8Array = (data) => {
12102
+ if (data.type === 'Buffer') {
12103
+ data = new Uint8Array(data.data);
12104
+ }
12105
+ return data
12161
12106
  };
12162
12107
 
12163
- class PeerDiscovery {
12164
- constructor(id) {
12165
- this.id = id;
12166
- }
12167
-
12168
- _getPeerId(id) {
12169
- if (!peernet.peerMap || (peernet.peerMap && peernet.peerMap.size === 0)) return false
12170
-
12171
- for (const entry of [...peernet.peerMap.entries()]) {
12172
- for (const _id of entry[1]) {
12173
- if (_id === id) return entry[0]
12174
- }
12175
- }
12176
- }
12177
-
12178
- async discover(peer) {
12179
- let id = this._getPeerId(peer.id);
12180
- if (id) return id
12181
- const data = await new peernet.protos['peernet-peer']({ id: this.id });
12182
- const node = await peernet.prepareMessage(peer.id, data.encoded);
12183
-
12184
- let response = await peer.request(node.encoded);
12185
- response = await protoFor(response);
12186
- response = await new peernet.protos['peernet-peer-response'](response.decoded.data);
12187
-
12188
- id = response.decoded.id;
12189
- if (id === this.id) return
12190
-
12191
- if (!peernet.peerMap.has(id)) peernet.peerMap.set(id, [peer.id]);
12192
- else {
12193
- const connections = peernet.peerMap.get(id);
12194
- if (connections.indexOf(peer.id) === -1) {
12195
- connections.push(peer.id);
12196
- peernet.peerMap.set(peer.id, connections);
12197
- }
12198
- }
12199
- return id
12200
- }
12201
-
12202
- async discoverHandler(message, peer) {
12203
- const { id, proto } = message;
12204
- // if (typeof message.data === 'string') message.data = Buffer.from(message.data)
12205
- if (proto.name === 'peernet-peer') {
12206
- const from = proto.decoded.id;
12207
- if (from === this.id) return
12208
-
12209
- if (!peernet.peerMap.has(from)) peernet.peerMap.set(from, [peer.id]);
12210
- else {
12211
- const connections = peernet.peerMap.get(from);
12212
- if (connections.indexOf(peer.id) === -1) {
12213
- connections.push(peer.id);
12214
- peernet.peerMap.set(from, connections);
12215
- }
12216
- }
12217
- const data = await new peernet.protos['peernet-peer-response']({ id: this.id });
12218
- const node = await peernet.prepareMessage(from, data.encoded);
12219
-
12220
- peer.write(Buffer.from(JSON.stringify({ id, data: node.encoded })));
12221
- } else if (proto.name === 'peernet-peer-response') {
12222
- const from = proto.decoded.id;
12223
- if (from === this.id) return
12224
-
12225
- if (!peernet.peerMap.has(from)) peernet.peerMap.set(from, [peer.id]);
12226
- else {
12227
- const connections = peernet.peerMap.get(from);
12228
- if (connections.indexOf(peer.id) === -1) {
12229
- connections.push(peer.id);
12230
- peernet.peerMap.set(from, connections);
12231
- }
12232
- }
12233
- }
12234
- }
12108
+ const protoFor = (message) => {
12109
+ const codec = new Codec(message);
12110
+ if (!codec.name) throw new Error(`proto not found ${message}`)
12111
+ const Proto = globalThis.peernet.protos[codec.name];
12112
+ if (!Proto) throw new Error(`No proto defined for ${codec.name}`)
12113
+ return new Proto(message)
12114
+ };
12115
+
12116
+ /**
12117
+ * wether or not a peernet daemon is active
12118
+ * @return {Boolean}
12119
+ */
12120
+ const hasDaemon = async () => {
12121
+ try {
12122
+ let response = await fetch('http://127.0.0.1:1000/api/version');
12123
+ response = await response.json();
12124
+ return Boolean(response.client === '@peernet/api/http')
12125
+ } catch (e) {
12126
+ return false
12127
+ }
12128
+ };
12129
+
12130
+ const https = () => {
12131
+ if (!globalThis.location) return false
12132
+ return Boolean(globalThis.location.protocol === 'https:')
12133
+ };
12134
+
12135
+ /**
12136
+ * Get current environment
12137
+ * @return {String} current environment [node, electron, browser]
12138
+ */
12139
+ const environment = () => {
12140
+ const _navigator = globalThis.navigator;
12141
+ if (!_navigator) {
12142
+ return 'node'
12143
+ } else if (_navigator && /electron/i.test(_navigator.userAgent)) {
12144
+ return 'electron'
12145
+ } else {
12146
+ return 'browser'
12147
+ }
12148
+ };
12149
+
12150
+ /**
12151
+ * * Get current environment
12152
+ * @return {Object} result
12153
+ * @property {Boolean} reult.daemon whether or not daemon is running
12154
+ * @property {Boolean} reult.environment Current environment
12155
+ */
12156
+ const target = async () => {
12157
+ let daemon = false;
12158
+ if (!https()) daemon = await hasDaemon();
12159
+
12160
+ return { daemon, environment: environment() }
12161
+ };
12162
+
12163
+ class PeerDiscovery {
12164
+ constructor(id) {
12165
+ this.id = id;
12166
+ }
12167
+
12168
+ _getPeerId(id) {
12169
+ if (!peernet.peerMap || (peernet.peerMap && peernet.peerMap.size === 0)) return false
12170
+
12171
+ for (const entry of [...peernet.peerMap.entries()]) {
12172
+ for (const _id of entry[1]) {
12173
+ if (_id === id) return entry[0]
12174
+ }
12175
+ }
12176
+ }
12177
+
12178
+ async discover(peer) {
12179
+ let id = this._getPeerId(peer.id);
12180
+ if (id) return id
12181
+ const data = await new peernet.protos['peernet-peer']({ id: this.id });
12182
+ const node = await peernet.prepareMessage(peer.id, data.encoded);
12183
+
12184
+ let response = await peer.request(node.encoded);
12185
+ response = await protoFor(response);
12186
+ response = await new peernet.protos['peernet-peer-response'](response.decoded.data);
12187
+
12188
+ id = response.decoded.id;
12189
+ if (id === this.id) return
12190
+
12191
+ if (!peernet.peerMap.has(id)) peernet.peerMap.set(id, [peer.id]);
12192
+ else {
12193
+ const connections = peernet.peerMap.get(id);
12194
+ if (connections.indexOf(peer.id) === -1) {
12195
+ connections.push(peer.id);
12196
+ peernet.peerMap.set(peer.id, connections);
12197
+ }
12198
+ }
12199
+ return id
12200
+ }
12201
+
12202
+ async discoverHandler(message, peer) {
12203
+ const { id, proto } = message;
12204
+ // if (typeof message.data === 'string') message.data = Buffer.from(message.data)
12205
+ if (proto.name === 'peernet-peer') {
12206
+ const from = proto.decoded.id;
12207
+ if (from === this.id) return
12208
+
12209
+ if (!peernet.peerMap.has(from)) peernet.peerMap.set(from, [peer.id]);
12210
+ else {
12211
+ const connections = peernet.peerMap.get(from);
12212
+ if (connections.indexOf(peer.id) === -1) {
12213
+ connections.push(peer.id);
12214
+ peernet.peerMap.set(from, connections);
12215
+ }
12216
+ }
12217
+ const data = await new peernet.protos['peernet-peer-response']({ id: this.id });
12218
+ const node = await peernet.prepareMessage(from, data.encoded);
12219
+
12220
+ peer.write(Buffer.from(JSON.stringify({ id, data: node.encoded })));
12221
+ } else if (proto.name === 'peernet-peer-response') {
12222
+ const from = proto.decoded.id;
12223
+ if (from === this.id) return
12224
+
12225
+ if (!peernet.peerMap.has(from)) peernet.peerMap.set(from, [peer.id]);
12226
+ else {
12227
+ const connections = peernet.peerMap.get(from);
12228
+ if (connections.indexOf(peer.id) === -1) {
12229
+ connections.push(peer.id);
12230
+ peernet.peerMap.set(from, connections);
12231
+ }
12232
+ }
12233
+ }
12234
+ }
12235
12235
  }
12236
12236
 
12237
12237
  /**
@@ -12343,60 +12343,60 @@ class DhtEarth {
12343
12343
  }
12344
12344
  }
12345
12345
 
12346
- class MessageHandler {
12347
- constructor(network) {
12348
- this.network = network;
12349
- }
12350
- /**
12351
- * hash and sign message
12352
- *
12353
- * @param {object} message
12354
- * @param {Buffer} message.from peer id
12355
- * @param {Buffer} message.to peer id
12356
- * @param {string} message.data Peernet message
12357
- * (PeernetMessage excluded) encoded as a string
12358
- * @return message
12359
- */
12360
- async hashAndSignMessage(message) {
12361
- const hash = await message.peernetHash;
12362
- message.decoded.signature = globalThis.identity.sign(hash.buffer);
12363
- return message
12364
- }
12365
-
12366
- /**
12367
- * @param {String} from - peer id
12368
- * @param {String} to - peer id
12369
- * @param {String|PeernetMessage} data - data encoded message string
12370
- * or the messageNode itself
12371
- */
12372
- async prepareMessage(message) {
12373
- if (message.keys.includes('signature')) {
12374
- message = await this.hashAndSignMessage(message);
12375
- }
12376
-
12377
- return message
12378
- }
12346
+ class MessageHandler {
12347
+ constructor(network) {
12348
+ this.network = network;
12349
+ }
12350
+ /**
12351
+ * hash and sign message
12352
+ *
12353
+ * @param {object} message
12354
+ * @param {Buffer} message.from peer id
12355
+ * @param {Buffer} message.to peer id
12356
+ * @param {string} message.data Peernet message
12357
+ * (PeernetMessage excluded) encoded as a string
12358
+ * @return message
12359
+ */
12360
+ async hashAndSignMessage(message) {
12361
+ const hash = await message.peernetHash;
12362
+ message.decoded.signature = globalThis.identity.sign(hash.buffer);
12363
+ return message
12364
+ }
12365
+
12366
+ /**
12367
+ * @param {String} from - peer id
12368
+ * @param {String} to - peer id
12369
+ * @param {String|PeernetMessage} data - data encoded message string
12370
+ * or the messageNode itself
12371
+ */
12372
+ async prepareMessage(message) {
12373
+ if (message.keys.includes('signature')) {
12374
+ message = await this.hashAndSignMessage(message);
12375
+ }
12376
+
12377
+ return message
12378
+ }
12379
12379
  }
12380
12380
 
12381
- const dataHandler = async (message) => {
12382
- if (!message) return
12383
-
12384
- try {
12385
- const { data, id, from, peer } = message;
12386
- const proto = await protoFor(data);
12387
- peernet._protoHandler({ id, proto }, peernet.connections[from] || peer, from);
12388
- } catch (error) {
12389
- console.error(error);
12390
- }
12381
+ const dataHandler = async (message) => {
12382
+ if (!message) return
12383
+
12384
+ try {
12385
+ const { data, id, from, peer } = message;
12386
+ const proto = await protoFor(data);
12387
+ peernet._protoHandler({ id, proto }, peernet.connections[from] || peer, from);
12388
+ } catch (error) {
12389
+ console.error(error);
12390
+ }
12391
12391
  };
12392
12392
 
12393
- const dhtError = (proto) => {
12394
- const text = `Received proto ${proto.name} expected peernet-dht-response`;
12395
- return new Error(`Routing error: ${text}`)
12396
- };
12397
-
12398
- const nothingFoundError = (hash) => {
12399
- return new Error(`nothing found for ${hash}`)
12393
+ const dhtError = (proto) => {
12394
+ const text = `Received proto ${proto.name} expected peernet-dht-response`;
12395
+ return new Error(`Routing error: ${text}`)
12396
+ };
12397
+
12398
+ const nothingFoundError = (hash) => {
12399
+ return new Error(`nothing found for ${hash}`)
12400
12400
  };
12401
12401
 
12402
12402
  const isBrowser = globalThis.process?.versions?.node ? false : true;
@@ -12617,7 +12617,7 @@ class Peernet {
12617
12617
  * @return {Array} peerId
12618
12618
  */
12619
12619
  get peers() {
12620
- return Object.entries(this.client?.connections);
12620
+ return Object.entries(this.client?.connections || {});
12621
12621
  }
12622
12622
  get connections() {
12623
12623
  return this.client?.connections || {};
@@ -12641,7 +12641,7 @@ class Peernet {
12641
12641
  this.root = options.root;
12642
12642
  const { RequestMessage, ResponseMessage, PeerMessage, PeerMessageResponse, PeernetMessage, DHTMessage, DHTMessageResponse, DataMessage, DataMessageResponse, PsMessage, ChatMessage, PeernetFile
12643
12643
  // FolderMessageResponse
12644
- } = await import(/* webpackChunkName: "messages" */ './messages-RYLqPGkg.js');
12644
+ } = await import(/* webpackChunkName: "messages" */ './messages-BdevLRCA.js');
12645
12645
  /**
12646
12646
  * proto Object containing protos
12647
12647
  * @type {Object}
@@ -12735,7 +12735,7 @@ class Peernet {
12735
12735
  if (this.#starting || this.#started)
12736
12736
  return;
12737
12737
  this.#starting = true;
12738
- const importee = await import('./client-Depp28gl.js');
12738
+ const importee = await import('./client-C0VVXIWm.js');
12739
12739
  /**
12740
12740
  * @access public
12741
12741
  * @type {PeernetClient}
@@ -1,3 +1,3 @@
1
- export { P as default } from './peernet-B7TZP-Wg.js';
1
+ export { P as default } from './peernet-DEIKLS2i.js';
2
2
  import './identity-CQ_ieRiz.js';
3
3
  import './value-C3vAp-wb.js';