@leofcoin/peernet 0.11.10 → 0.11.13

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.
@@ -83727,10 +83727,6 @@ const expected = (expected, actual) => {
83727
83727
  ${entries.join('\n\t')}`;
83728
83728
  }
83729
83729
 
83730
- const utils_debug = (log) => {
83731
- if (globalThis.DEBUG || globalThis.debug) console.log(`%c ${log}`, 'color: #0080ff;')
83732
- }
83733
-
83734
83730
  const protoFor = (data) => {
83735
83731
  if (!utils_Buffer.isBuffer(data)) data = utils_Buffer.from(data)
83736
83732
  const codec = new PeernetCodec(data)
@@ -84530,6 +84526,7 @@ const nothingFoundError = (hash) => {
84530
84526
 
84531
84527
 
84532
84528
 
84529
+
84533
84530
 
84534
84531
 
84535
84532
  globalThis.leofcoin = globalThis.leofcoin || {}
@@ -84629,6 +84626,13 @@ class Peernet {
84629
84626
  return Object.entries(this.client.connections)
84630
84627
  }
84631
84628
 
84629
+ /**
84630
+ * @return {String} id - peerId
84631
+ */
84632
+ getConnection(id) {
84633
+ return this.client.connections[id]
84634
+ }
84635
+
84632
84636
  /**
84633
84637
  * @private
84634
84638
  *
@@ -84648,7 +84652,6 @@ class Peernet {
84648
84652
  * @type {Map}
84649
84653
  * @property {Object} peer Instance of Peer
84650
84654
  */
84651
- this.peerMap = new Map()
84652
84655
  this.stores = []
84653
84656
  this.requestProtos = {}
84654
84657
  this.storePrefix = options.storePrefix
@@ -84748,14 +84751,6 @@ class Peernet {
84748
84751
  return this
84749
84752
  }
84750
84753
 
84751
- _getPeerId(id) {
84752
- for (const entry of [...this.peerMap.entries()]) {
84753
- for (const _id of entry[1]) {
84754
- if (_id === id) return entry[0]
84755
- }
84756
- }
84757
- }
84758
-
84759
84754
  addRequestHandler(name, method) {
84760
84755
  this.requestProtos[name] = method
84761
84756
  }
@@ -84928,7 +84923,7 @@ class Peernet {
84928
84923
  async requestData(hash, store) {
84929
84924
  const providers = await this.providersFor(hash)
84930
84925
  if (!providers || providers.size === 0) throw nothingFoundError(hash)
84931
- utils_debug(`found ${providers.size} provider(s) for ${hash}`)
84926
+ debug(`found ${providers.size} provider(s) for ${hash}`)
84932
84927
  // get closest peer on earth
84933
84928
  const closestPeer = await this.dht.closestPeer(providers)
84934
84929
  // get peer instance by id
@@ -84970,7 +84965,7 @@ class Peernet {
84970
84965
  * @param {String} hash
84971
84966
  */
84972
84967
  get: async (hash) => {
84973
- utils_debug(`get message ${hash}`)
84968
+ debug(`get message ${hash}`)
84974
84969
  const message = await messageStore.has(hash)
84975
84970
  if (message) return await messageStore.get(hash)
84976
84971
  return this.requestData(hash, 'message')
@@ -84998,7 +84993,7 @@ class Peernet {
84998
84993
  * @param {String} hash
84999
84994
  */
85000
84995
  get: async (hash) => {
85001
- utils_debug(`get data ${hash}`)
84996
+ debug(`get data ${hash}`)
85002
84997
  const data = await dataStore.has(hash)
85003
84998
  if (data) return await dataStore.get(hash)
85004
84999
  return this.requestData(hash, 'data')
@@ -85041,7 +85036,7 @@ class Peernet {
85041
85036
  * @param {String} store - storeName to access
85042
85037
  */
85043
85038
  async get(hash, store) {
85044
- utils_debug(`get ${hash}`)
85039
+ debug(`get ${hash}`)
85045
85040
  let data
85046
85041
  if (store) store = globalThis[`${store}Store`]
85047
85042
  if (!store) store = await this.whichStore([...this.stores], hash)
@@ -10,7 +10,7 @@ function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'defau
10
10
  var PubSub__default = /*#__PURE__*/_interopDefaultLegacy(PubSub);
11
11
  var Koa__default = /*#__PURE__*/_interopDefaultLegacy(Koa);
12
12
 
13
- var version = "0.11.9";
13
+ var version = "0.11.12";
14
14
 
15
15
  var api$1 = {
16
16
  version: ({send}) => send({client: '@peernet/api/http', version}),
@@ -776,10 +776,6 @@ class ChatMessage extends codecFormatInterface {
776
776
  }
777
777
  }
778
778
 
779
- const debug$1 = (log) => {
780
- if (globalThis.DEBUG || globalThis.debug) console.log(`%c ${log}`, 'color: #0080ff;');
781
- };
782
-
783
779
  const protoFor = (data) => {
784
780
  if (!Buffer.isBuffer(data)) data = Buffer.from(data);
785
781
  const codec$1 = new codec.PeernetCodec(data);
@@ -1576,6 +1572,13 @@ class Peernet {
1576
1572
  return Object.entries(this.client.connections)
1577
1573
  }
1578
1574
 
1575
+ /**
1576
+ * @return {String} id - peerId
1577
+ */
1578
+ getConnection(id) {
1579
+ return this.client.connections[id]
1580
+ }
1581
+
1579
1582
  /**
1580
1583
  * @private
1581
1584
  *
@@ -1595,7 +1598,6 @@ class Peernet {
1595
1598
  * @type {Map}
1596
1599
  * @property {Object} peer Instance of Peer
1597
1600
  */
1598
- this.peerMap = new Map();
1599
1601
  this.stores = [];
1600
1602
  this.requestProtos = {};
1601
1603
  this.storePrefix = options.storePrefix;
@@ -1638,7 +1640,7 @@ class Peernet {
1638
1640
  protocol: 'peernet-v0.1.0', host: '127.0.0.1', port: options.port
1639
1641
  });
1640
1642
  } else {
1641
- const http = await Promise.resolve().then(function () { return require('./http-e330b910.js'); });
1643
+ const http = await Promise.resolve().then(function () { return require('./http-2bb2eb4b.js'); });
1642
1644
  if (environment !== 'browser') http.default(options);
1643
1645
  }
1644
1646
 
@@ -1702,14 +1704,6 @@ class Peernet {
1702
1704
  return this
1703
1705
  }
1704
1706
 
1705
- _getPeerId(id) {
1706
- for (const entry of [...this.peerMap.entries()]) {
1707
- for (const _id of entry[1]) {
1708
- if (_id === id) return entry[0]
1709
- }
1710
- }
1711
- }
1712
-
1713
1707
  addRequestHandler(name, method) {
1714
1708
  this.requestProtos[name] = method;
1715
1709
  }
@@ -1880,7 +1874,7 @@ class Peernet {
1880
1874
  async requestData(hash, store) {
1881
1875
  const providers = await this.providersFor(hash);
1882
1876
  if (!providers || providers.size === 0) throw nothingFoundError(hash)
1883
- debug$1(`found ${providers.size} provider(s) for ${hash}`);
1877
+ debug(`found ${providers.size} provider(s) for ${hash}`);
1884
1878
  // get closest peer on earth
1885
1879
  const closestPeer = await this.dht.closestPeer(providers);
1886
1880
  // get peer instance by id
@@ -1922,7 +1916,7 @@ class Peernet {
1922
1916
  * @param {String} hash
1923
1917
  */
1924
1918
  get: async (hash) => {
1925
- debug$1(`get message ${hash}`);
1919
+ debug(`get message ${hash}`);
1926
1920
  const message = await messageStore.has(hash);
1927
1921
  if (message) return await messageStore.get(hash)
1928
1922
  return this.requestData(hash, 'message')
@@ -1950,7 +1944,7 @@ class Peernet {
1950
1944
  * @param {String} hash
1951
1945
  */
1952
1946
  get: async (hash) => {
1953
- debug$1(`get data ${hash}`);
1947
+ debug(`get data ${hash}`);
1954
1948
  const data = await dataStore.has(hash);
1955
1949
  if (data) return await dataStore.get(hash)
1956
1950
  return this.requestData(hash, 'data')
@@ -1992,7 +1986,7 @@ class Peernet {
1992
1986
  * @param {String} store - storeName to access
1993
1987
  */
1994
1988
  async get(hash, store) {
1995
- debug$1(`get ${hash}`);
1989
+ debug(`get ${hash}`);
1996
1990
  let data;
1997
1991
  if (store) store = globalThis[`${store}Store`];
1998
1992
  if (!store) store = await this.whichStore([...this.stores], hash);
@@ -1373,10 +1373,6 @@ class ChatMessage extends FormatInterface {
1373
1373
  }
1374
1374
  }
1375
1375
 
1376
- const debug$1 = (log) => {
1377
- if (globalThis.DEBUG || globalThis.debug) console.log(`%c ${log}`, 'color: #0080ff;');
1378
- };
1379
-
1380
1376
  const protoFor = (data) => {
1381
1377
  if (!Buffer.isBuffer(data)) data = Buffer.from(data);
1382
1378
  const codec = new PeernetCodec(data);
@@ -2173,6 +2169,13 @@ class Peernet {
2173
2169
  return Object.entries(this.client.connections)
2174
2170
  }
2175
2171
 
2172
+ /**
2173
+ * @return {String} id - peerId
2174
+ */
2175
+ getConnection(id) {
2176
+ return this.client.connections[id]
2177
+ }
2178
+
2176
2179
  /**
2177
2180
  * @private
2178
2181
  *
@@ -2192,7 +2195,6 @@ class Peernet {
2192
2195
  * @type {Map}
2193
2196
  * @property {Object} peer Instance of Peer
2194
2197
  */
2195
- this.peerMap = new Map();
2196
2198
  this.stores = [];
2197
2199
  this.requestProtos = {};
2198
2200
  this.storePrefix = options.storePrefix;
@@ -2291,14 +2293,6 @@ class Peernet {
2291
2293
  return this
2292
2294
  }
2293
2295
 
2294
- _getPeerId(id) {
2295
- for (const entry of [...this.peerMap.entries()]) {
2296
- for (const _id of entry[1]) {
2297
- if (_id === id) return entry[0]
2298
- }
2299
- }
2300
- }
2301
-
2302
2296
  addRequestHandler(name, method) {
2303
2297
  this.requestProtos[name] = method;
2304
2298
  }
@@ -2469,7 +2463,7 @@ class Peernet {
2469
2463
  async requestData(hash, store) {
2470
2464
  const providers = await this.providersFor(hash);
2471
2465
  if (!providers || providers.size === 0) throw nothingFoundError(hash)
2472
- debug$1(`found ${providers.size} provider(s) for ${hash}`);
2466
+ debug(`found ${providers.size} provider(s) for ${hash}`);
2473
2467
  // get closest peer on earth
2474
2468
  const closestPeer = await this.dht.closestPeer(providers);
2475
2469
  // get peer instance by id
@@ -2511,7 +2505,7 @@ class Peernet {
2511
2505
  * @param {String} hash
2512
2506
  */
2513
2507
  get: async (hash) => {
2514
- debug$1(`get message ${hash}`);
2508
+ debug(`get message ${hash}`);
2515
2509
  const message = await messageStore.has(hash);
2516
2510
  if (message) return await messageStore.get(hash)
2517
2511
  return this.requestData(hash, 'message')
@@ -2539,7 +2533,7 @@ class Peernet {
2539
2533
  * @param {String} hash
2540
2534
  */
2541
2535
  get: async (hash) => {
2542
- debug$1(`get data ${hash}`);
2536
+ debug(`get data ${hash}`);
2543
2537
  const data = await dataStore.has(hash);
2544
2538
  if (data) return await dataStore.get(hash)
2545
2539
  return this.requestData(hash, 'data')
@@ -2581,7 +2575,7 @@ class Peernet {
2581
2575
  * @param {String} store - storeName to access
2582
2576
  */
2583
2577
  async get(hash, store) {
2584
- debug$1(`get ${hash}`);
2578
+ debug(`get ${hash}`);
2585
2579
  let data;
2586
2580
  if (store) store = globalThis[`${store}Store`];
2587
2581
  if (!store) store = await this.whichStore([...this.stores], hash);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leofcoin/peernet",
3
- "version": "0.11.10",
3
+ "version": "0.11.13",
4
4
  "description": "",
5
5
  "main": "dist/commonjs/peernet.js",
6
6
  "module": "dist/module/peernet.js",
package/src/peernet.js CHANGED
@@ -1,3 +1,4 @@
1
+ import '@vandeurenglenn/debug'
1
2
  import Client from './../node_modules/@leofcoin/peernet-swarm/dist/es/client.js'
2
3
  import LeofcoinStorage from '@leofcoin/storage'
3
4
  import LeofcoinStorageClient from './http/client/storage.js'
@@ -16,7 +17,7 @@ import PeerDiscovery from './discovery/peer-discovery'
16
17
  import DHT from './dht/dht.js'
17
18
  import Hash from './hash/hash'
18
19
  import codecs from './codec/codecs'
19
- import { debug, protoFor, target } from './utils/utils.js'
20
+ import { protoFor, target } from './utils/utils.js'
20
21
  import generateAccount from '@leofcoin/generate-account'
21
22
  import MessageHandler from './handlers/message.js'
22
23
  import { encapsulatedError, dhtError,
@@ -119,6 +120,13 @@ export default class Peernet {
119
120
  return Object.entries(this.client.connections)
120
121
  }
121
122
 
123
+ /**
124
+ * @return {String} id - peerId
125
+ */
126
+ getConnection(id) {
127
+ return this.client.connections[id]
128
+ }
129
+
122
130
  /**
123
131
  * @private
124
132
  *
@@ -138,7 +146,6 @@ export default class Peernet {
138
146
  * @type {Map}
139
147
  * @property {Object} peer Instance of Peer
140
148
  */
141
- this.peerMap = new Map()
142
149
  this.stores = []
143
150
  this.requestProtos = {}
144
151
  this.storePrefix = options.storePrefix
@@ -238,14 +245,6 @@ export default class Peernet {
238
245
  return this
239
246
  }
240
247
 
241
- _getPeerId(id) {
242
- for (const entry of [...this.peerMap.entries()]) {
243
- for (const _id of entry[1]) {
244
- if (_id === id) return entry[0]
245
- }
246
- }
247
- }
248
-
249
248
  addRequestHandler(name, method) {
250
249
  this.requestProtos[name] = method
251
250
  }
@@ -16,10 +16,6 @@ export const expected = (expected, actual) => {
16
16
  ${entries.join('\n\t')}`;
17
17
  }
18
18
 
19
- export const debug = (log) => {
20
- if (globalThis.DEBUG || globalThis.debug) console.log(`%c ${log}`, 'color: #0080ff;')
21
- }
22
-
23
19
  export const protoFor = (data) => {
24
20
  if (!Buffer.isBuffer(data)) data = Buffer.from(data)
25
21
  const codec = new Codec(data)
package/src/client.js DELETED
@@ -1,75 +0,0 @@
1
- import Pubsub from '@vandeurenglenn/little-pubsub'
2
- import PeernetPeer from './peer.js'
3
- import sha256 from 'crypto-js/sha256'
4
-
5
- import P2P from 'p2pt'
6
-
7
-
8
- /**
9
- * Array of peers
10
- * @type {Array}
11
- * @property {PeernetPeer} peer Instance of Peer
12
- */
13
- globalThis.connections = new Map()
14
- globalThis.recentConnections = new Map()
15
- globalThis.pubsub = globalThis.pubsub || new Pubsub({verbose: false})
16
-
17
- export default class PeernetClient {
18
- constructor(options = {}) {
19
- if (!options.id) options.id = Buffer.from('00000000000000000000000000000000')
20
- if (!options.networkVersion) options.networkVersion = 'v0.1.0'
21
- if (!options.networkName) options.networkName = 'peernet'
22
- this.id = options.id
23
-
24
- this.topic = Buffer.from(sha256(`${options.networkName}-${options.networkVersion}`).toString())
25
-
26
- const trackers = [
27
- 'wss://star.leofcoin.org',
28
- 'wss://tracker.openwebtorrent.com',
29
- // 'wss://tracker.sloppyta.co:443/announce',
30
- ]
31
- this.p2p = new P2P(trackers, this.topic.slice(0, 20))
32
- this.p2p.on('peerconnect', (peer) => {
33
- peer = new PeernetPeer(peer.id, peer)
34
- connections.set(peer.id, peer)
35
- pubsub.publish('peer:discovered', peer)
36
- })
37
-
38
- this.p2p.on('peerclose', (peer) => {
39
- // TODO: close peernetPeer
40
- const peernetPeer = connections.get(peer.id)
41
- if (peernetPeer) {
42
- peernetPeer.close()
43
- }
44
- connections.delete(peer.id)
45
- pubsub.publish('peer:disconnected', peer)
46
- })
47
-
48
- this.p2p.start()
49
-
50
- if (globalThis.process) {
51
- process.on('SIGINT', async () => {
52
- console.log('Caught interrupt signal')
53
- this.close()
54
- setTimeout(async () => {
55
- process.exit();
56
- }, 100);
57
- })
58
- } else {
59
- globalThis.onbeforeunload = () => {
60
- this.close()
61
- }
62
- }
63
- //
64
- // this.sw.on('close', () => {
65
- // })
66
- }
67
-
68
- close() {
69
- return this.p2p.destroy()
70
- }
71
-
72
- _peers() {
73
- return this.p2p.getPeers()
74
- }
75
- }
package/src/peer.js DELETED
@@ -1,67 +0,0 @@
1
- export default class PeernetPeer {
2
- constructor(id, connection) {
3
- this._events = {}
4
- this.bw = {
5
- up: 0,
6
- down: 0,
7
- }
8
- this.id = id
9
- this.connection = connection
10
-
11
- this.connection.on('data', (message) => {
12
- this.bw.down += message.length
13
- pubsub.publish('peernet.data', JSON.parse(message.toString()))
14
- })
15
- }
16
-
17
- request(data) {
18
- return new Promise((resolve, reject) => {
19
- const id = Math.random().toString(36).slice(-12)
20
- data = Buffer.from(JSON.stringify({id, data}))
21
- const _onData = (message) => {
22
- if (message.id !== id) return
23
-
24
- resolve(message.data)
25
- }
26
-
27
- pubsub.subscribe('peernet.data', _onData)
28
-
29
- // cleanup subscriptions
30
- setTimeout(() => {
31
- pubsub.unsubscribe('peernet.data', _onData)
32
- }, 5000);
33
-
34
- this.write(data)
35
- });
36
- }
37
-
38
- write(data) {
39
- if (!Buffer.isBuffer(data)) data = Buffer.from(data)
40
-
41
- this.bw.up += data.length
42
- this.connection.write(data)
43
- }
44
-
45
- on(event = 'peernet.data', cb) {
46
- this._events[event] = cb
47
- pubsub.subscribe(event, cb)
48
- // this.connection.on(event, cb)
49
- }
50
-
51
- removeListener(event = 'data', cb) {
52
- delete this._events[event]
53
- pubsub.unsubscribe(event, cb)
54
- }
55
-
56
- close() {
57
- for (const event of Object.keys(this._events)) {
58
- pubsub.unsubscribe(event, this._events[event])
59
- }
60
- this._events = []
61
-
62
- for (const event of this.connection._events.data) {
63
- this.connection.removeListener('data', event)
64
- }
65
- this.connection.destroy()
66
- }
67
- }