@leofcoin/peernet 0.11.8 → 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.
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
- }