@leofcoin/peernet 0.11.10 → 0.11.11

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 || {}
@@ -84928,7 +84925,7 @@ class Peernet {
84928
84925
  async requestData(hash, store) {
84929
84926
  const providers = await this.providersFor(hash)
84930
84927
  if (!providers || providers.size === 0) throw nothingFoundError(hash)
84931
- utils_debug(`found ${providers.size} provider(s) for ${hash}`)
84928
+ debug(`found ${providers.size} provider(s) for ${hash}`)
84932
84929
  // get closest peer on earth
84933
84930
  const closestPeer = await this.dht.closestPeer(providers)
84934
84931
  // get peer instance by id
@@ -84970,7 +84967,7 @@ class Peernet {
84970
84967
  * @param {String} hash
84971
84968
  */
84972
84969
  get: async (hash) => {
84973
- utils_debug(`get message ${hash}`)
84970
+ debug(`get message ${hash}`)
84974
84971
  const message = await messageStore.has(hash)
84975
84972
  if (message) return await messageStore.get(hash)
84976
84973
  return this.requestData(hash, 'message')
@@ -84998,7 +84995,7 @@ class Peernet {
84998
84995
  * @param {String} hash
84999
84996
  */
85000
84997
  get: async (hash) => {
85001
- utils_debug(`get data ${hash}`)
84998
+ debug(`get data ${hash}`)
85002
84999
  const data = await dataStore.has(hash)
85003
85000
  if (data) return await dataStore.get(hash)
85004
85001
  return this.requestData(hash, 'data')
@@ -85041,7 +85038,7 @@ class Peernet {
85041
85038
  * @param {String} store - storeName to access
85042
85039
  */
85043
85040
  async get(hash, store) {
85044
- utils_debug(`get ${hash}`)
85041
+ debug(`get ${hash}`)
85045
85042
  let data
85046
85043
  if (store) store = globalThis[`${store}Store`]
85047
85044
  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.10";
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);
@@ -1638,7 +1634,7 @@ class Peernet {
1638
1634
  protocol: 'peernet-v0.1.0', host: '127.0.0.1', port: options.port
1639
1635
  });
1640
1636
  } else {
1641
- const http = await Promise.resolve().then(function () { return require('./http-e330b910.js'); });
1637
+ const http = await Promise.resolve().then(function () { return require('./http-1de9e08b.js'); });
1642
1638
  if (environment !== 'browser') http.default(options);
1643
1639
  }
1644
1640
 
@@ -1880,7 +1876,7 @@ class Peernet {
1880
1876
  async requestData(hash, store) {
1881
1877
  const providers = await this.providersFor(hash);
1882
1878
  if (!providers || providers.size === 0) throw nothingFoundError(hash)
1883
- debug$1(`found ${providers.size} provider(s) for ${hash}`);
1879
+ debug(`found ${providers.size} provider(s) for ${hash}`);
1884
1880
  // get closest peer on earth
1885
1881
  const closestPeer = await this.dht.closestPeer(providers);
1886
1882
  // get peer instance by id
@@ -1922,7 +1918,7 @@ class Peernet {
1922
1918
  * @param {String} hash
1923
1919
  */
1924
1920
  get: async (hash) => {
1925
- debug$1(`get message ${hash}`);
1921
+ debug(`get message ${hash}`);
1926
1922
  const message = await messageStore.has(hash);
1927
1923
  if (message) return await messageStore.get(hash)
1928
1924
  return this.requestData(hash, 'message')
@@ -1950,7 +1946,7 @@ class Peernet {
1950
1946
  * @param {String} hash
1951
1947
  */
1952
1948
  get: async (hash) => {
1953
- debug$1(`get data ${hash}`);
1949
+ debug(`get data ${hash}`);
1954
1950
  const data = await dataStore.has(hash);
1955
1951
  if (data) return await dataStore.get(hash)
1956
1952
  return this.requestData(hash, 'data')
@@ -1992,7 +1988,7 @@ class Peernet {
1992
1988
  * @param {String} store - storeName to access
1993
1989
  */
1994
1990
  async get(hash, store) {
1995
- debug$1(`get ${hash}`);
1991
+ debug(`get ${hash}`);
1996
1992
  let data;
1997
1993
  if (store) store = globalThis[`${store}Store`];
1998
1994
  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);
@@ -2469,7 +2465,7 @@ class Peernet {
2469
2465
  async requestData(hash, store) {
2470
2466
  const providers = await this.providersFor(hash);
2471
2467
  if (!providers || providers.size === 0) throw nothingFoundError(hash)
2472
- debug$1(`found ${providers.size} provider(s) for ${hash}`);
2468
+ debug(`found ${providers.size} provider(s) for ${hash}`);
2473
2469
  // get closest peer on earth
2474
2470
  const closestPeer = await this.dht.closestPeer(providers);
2475
2471
  // get peer instance by id
@@ -2511,7 +2507,7 @@ class Peernet {
2511
2507
  * @param {String} hash
2512
2508
  */
2513
2509
  get: async (hash) => {
2514
- debug$1(`get message ${hash}`);
2510
+ debug(`get message ${hash}`);
2515
2511
  const message = await messageStore.has(hash);
2516
2512
  if (message) return await messageStore.get(hash)
2517
2513
  return this.requestData(hash, 'message')
@@ -2539,7 +2535,7 @@ class Peernet {
2539
2535
  * @param {String} hash
2540
2536
  */
2541
2537
  get: async (hash) => {
2542
- debug$1(`get data ${hash}`);
2538
+ debug(`get data ${hash}`);
2543
2539
  const data = await dataStore.has(hash);
2544
2540
  if (data) return await dataStore.get(hash)
2545
2541
  return this.requestData(hash, 'data')
@@ -2581,7 +2577,7 @@ class Peernet {
2581
2577
  * @param {String} store - storeName to access
2582
2578
  */
2583
2579
  async get(hash, store) {
2584
- debug$1(`get ${hash}`);
2580
+ debug(`get ${hash}`);
2585
2581
  let data;
2586
2582
  if (store) store = globalThis[`${store}Store`];
2587
2583
  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.11",
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,
@@ -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
- }