@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.
- package/.esdoc.json +10 -10
- package/.gitattributes +2 -2
- package/.prettierrc +7 -7
- package/.travis.yml +27 -27
- package/BREAKING_CHANGES.md +34 -34
- package/LICENSE +21 -21
- package/README.md +72 -72
- package/deploy.js +8 -8
- package/exports/browser/{client-Depp28gl.js → client-C0VVXIWm.js} +2 -2
- package/exports/browser/{index-DqPlTtAJ.js → index-CEwkDK9g.js} +5 -496
- package/exports/browser/{messages-RYLqPGkg.js → messages-BdevLRCA.js} +164 -164
- package/exports/browser/{peernet-B7TZP-Wg.js → peernet-DEIKLS2i.js} +185 -185
- package/exports/browser/peernet.js +1 -1
- package/exports/{messages-CRhtDipD.js → messages-BmpgEM4y.js} +163 -163
- package/exports/peernet.js +184 -184
- package/exports/src/prompts/password.js +3 -3
- package/index.html +19 -19
- package/package.json +71 -71
- package/rollup.config.js +63 -63
- package/src/dht/dht.ts +147 -147
- package/src/discovery/peer-discovery.js +75 -75
- package/src/errors/errors.js +12 -12
- package/src/handlers/data.js +15 -15
- package/src/handlers/message.js +34 -34
- package/src/identity.ts +104 -104
- package/src/messages/chat.js +13 -13
- package/src/messages/data-response.js +13 -13
- package/src/messages/data.js +17 -17
- package/src/messages/dht-response.js +13 -13
- package/src/messages/dht.js +21 -21
- package/src/messages/file-link.js +17 -17
- package/src/messages/file.js +17 -17
- package/src/messages/peer-response.js +13 -13
- package/src/messages/peer.js +13 -13
- package/src/messages/peernet.js +13 -13
- package/src/messages/ps.js +13 -13
- package/src/messages/request.js +13 -13
- package/src/messages/response.js +13 -13
- package/src/messages.js +13 -13
- package/src/peer-info.js +9 -9
- package/src/peernet.ts +817 -817
- package/src/prompts/password/browser.js +1 -1
- package/src/prompts/password/node.js +6 -6
- package/src/proto/chat-message.proto.js +6 -6
- package/src/proto/data-response.proto.js +4 -4
- package/src/proto/data.proto.js +4 -4
- package/src/proto/dht-response.proto.js +4 -4
- package/src/proto/dht.proto.js +4 -4
- package/src/proto/file-link.proto.js +5 -5
- package/src/proto/file.proto.js +5 -5
- package/src/proto/peer-response.proto.js +3 -3
- package/src/proto/peer.proto.js +3 -3
- package/src/proto/peernet.proto.js +7 -7
- package/src/proto/ps.proto.js +4 -4
- package/src/proto/request.proto.js +4 -4
- package/src/proto/response.proto.js +3 -3
- package/src/types.ts +25 -25
- package/src/utils/utils.js +77 -77
- package/test/client.js +14 -14
- package/test/codec.js +56 -56
- package/test/hash.js +13 -13
- package/test/index.js +3 -3
- package/test/lastBlock.js +7 -7
- package/test/messages.js +26 -26
- package/test/peernet.js +17 -17
- package/test/peernet.test.js +159 -159
- package/test.js +62 -62
- package/test2.js +13 -13
- package/test3.js +15 -15
- package/test4.js +7 -7
- package/tsconfig.json +11 -11
package/test/peernet.test.js
CHANGED
|
@@ -1,159 +1,159 @@
|
|
|
1
|
-
import Peernet from '../exports/peernet.js'
|
|
2
|
-
import Identity from '../exports/identity.js'
|
|
3
|
-
import { expect } from 'chai'
|
|
4
|
-
import sinon from 'sinon'
|
|
5
|
-
import networks from '@leofcoin/networks'
|
|
6
|
-
|
|
7
|
-
const network = networks.leofcoin.peach
|
|
8
|
-
|
|
9
|
-
let options
|
|
10
|
-
let password
|
|
11
|
-
options = {
|
|
12
|
-
network: 'leofcoin:peach',
|
|
13
|
-
stars: network.stars,
|
|
14
|
-
root: '.testnet',
|
|
15
|
-
version: '1.0.0',
|
|
16
|
-
storePrefix: 'test'
|
|
17
|
-
}
|
|
18
|
-
password = 'password'
|
|
19
|
-
const peernet = await new Peernet(options, password)
|
|
20
|
-
|
|
21
|
-
describe('Peernet', async () => {
|
|
22
|
-
// beforeEach(async () => {
|
|
23
|
-
// })
|
|
24
|
-
|
|
25
|
-
it('should initialize with the correct network', () => {
|
|
26
|
-
expect(peernet.network).to.equal('leofcoin:peach')
|
|
27
|
-
})
|
|
28
|
-
|
|
29
|
-
it('should initialize with the correct version', () => {
|
|
30
|
-
expect(peernet.version).to.equal('1.0.0')
|
|
31
|
-
})
|
|
32
|
-
|
|
33
|
-
it('should initialize with the correct store prefix', () => {
|
|
34
|
-
expect(peernet.storePrefix).to.equal('test')
|
|
35
|
-
})
|
|
36
|
-
|
|
37
|
-
it('should have an identity instance', () => {
|
|
38
|
-
expect(peernet.identity).to.be.instanceOf(Identity)
|
|
39
|
-
})
|
|
40
|
-
|
|
41
|
-
it('should have a DHT instance', () => {
|
|
42
|
-
expect(peernet.dht).to.not.be.undefined
|
|
43
|
-
})
|
|
44
|
-
|
|
45
|
-
it('should start the client', async () => {
|
|
46
|
-
const startSpy = sinon.spy(peernet, 'start')
|
|
47
|
-
await peernet.start()
|
|
48
|
-
expect(startSpy.calledOnce).to.be.true
|
|
49
|
-
})
|
|
50
|
-
|
|
51
|
-
it('should add a proto', () => {
|
|
52
|
-
const protoName = 'test-proto'
|
|
53
|
-
const proto = {}
|
|
54
|
-
peernet.addProto(protoName, proto)
|
|
55
|
-
expect(globalThis.peernet.protos[protoName]).to.equal(proto)
|
|
56
|
-
})
|
|
57
|
-
|
|
58
|
-
it('should add a codec', () => {
|
|
59
|
-
const codec = { name: 'test-codec' }
|
|
60
|
-
const addCodecSpy = sinon.spy(peernet, 'addCodec')
|
|
61
|
-
peernet.addCodec(codec)
|
|
62
|
-
expect(addCodecSpy.calledOnceWith(codec)).to.be.true
|
|
63
|
-
})
|
|
64
|
-
|
|
65
|
-
it('should select an account', async () => {
|
|
66
|
-
const account = 'test-account'
|
|
67
|
-
const selectAccountSpy = sinon.spy(peernet.identity, 'selectAccount')
|
|
68
|
-
await peernet.selectAccount(account)
|
|
69
|
-
expect(selectAccountSpy.calledOnceWith(account)).to.be.true
|
|
70
|
-
})
|
|
71
|
-
|
|
72
|
-
it('should prepare a message', () => {
|
|
73
|
-
const data = { message: 'test' }
|
|
74
|
-
const prepareMessageSpy = sinon.spy(peernet._messageHandler, 'prepareMessage')
|
|
75
|
-
peernet.prepareMessage(data)
|
|
76
|
-
expect(prepareMessageSpy.calledOnceWith(data)).to.be.true
|
|
77
|
-
})
|
|
78
|
-
|
|
79
|
-
it('should get peers', () => {
|
|
80
|
-
const peers = peernet.peers
|
|
81
|
-
expect(peers).to.be.an('array')
|
|
82
|
-
})
|
|
83
|
-
|
|
84
|
-
it('should get connections', () => {
|
|
85
|
-
const connections = peernet.connections
|
|
86
|
-
expect(connections).to.be.an('object')
|
|
87
|
-
})
|
|
88
|
-
|
|
89
|
-
it('should get a connection by id', () => {
|
|
90
|
-
const id = 'test-id'
|
|
91
|
-
const connection = peernet.getConnection(id)
|
|
92
|
-
expect(connection).to.be.undefined // Assuming no connections are established in the test
|
|
93
|
-
})
|
|
94
|
-
|
|
95
|
-
// it('should handle DHT', async () => {
|
|
96
|
-
// const peer = {}
|
|
97
|
-
// const id = 'test-id'
|
|
98
|
-
// const proto = { decoded: { hash: 'test-hash', store: 'test-store' } }
|
|
99
|
-
// const handleDHTSpy = sinon.spy(peernet, 'handleDHT')
|
|
100
|
-
// await peernet.handleDHT(peer, id, proto)
|
|
101
|
-
// expect(handleDHTSpy.calledOnceWith(peer, id, proto)).to.be.true
|
|
102
|
-
// })
|
|
103
|
-
|
|
104
|
-
it('should handle data', async () => {
|
|
105
|
-
const peer = {}
|
|
106
|
-
const id = 'test-id'
|
|
107
|
-
const proto = { decoded: { hash: 'test-hash', store: 'test-store' } }
|
|
108
|
-
const handleDataSpy = sinon.spy(peernet, 'handleData')
|
|
109
|
-
await peernet.handleData(peer, id, proto)
|
|
110
|
-
expect(handleDataSpy.calledOnceWith(peer, id, proto)).to.be.true
|
|
111
|
-
})
|
|
112
|
-
|
|
113
|
-
it('should handle request', async () => {
|
|
114
|
-
const peer = {}
|
|
115
|
-
const id = 'test-id'
|
|
116
|
-
const proto = { decoded: { request: 'test-request' } }
|
|
117
|
-
const handleRequestSpy = sinon.spy(peernet, 'handleRequest')
|
|
118
|
-
await peernet.handleRequest(peer, id, proto)
|
|
119
|
-
expect(handleRequestSpy.calledOnceWith(peer, id, proto)).to.be.true
|
|
120
|
-
})
|
|
121
|
-
|
|
122
|
-
it('should walk the network', async () => {
|
|
123
|
-
const hash = 'test-hash'
|
|
124
|
-
const walkSpy = sinon.spy(peernet, 'walk')
|
|
125
|
-
await peernet.walk(hash)
|
|
126
|
-
expect(walkSpy.calledOnceWith(hash)).to.be.true
|
|
127
|
-
})
|
|
128
|
-
|
|
129
|
-
// it('should find providers for a hash', async () => {
|
|
130
|
-
// const hash = 'test-hash'
|
|
131
|
-
// const providers = await peernet.providersFor(hash)
|
|
132
|
-
// expect(providers).to.be.undefined // Assuming no providers are found in the test
|
|
133
|
-
// })
|
|
134
|
-
|
|
135
|
-
// it('should request data', async () => {
|
|
136
|
-
// const hash = 'test-hash'
|
|
137
|
-
// const requestDataSpy = sinon.spy(peernet, 'requestData')
|
|
138
|
-
// await peernet.requestData(hash, 'data')
|
|
139
|
-
// expect(requestDataSpy.calledOnceWith(hash, 'data')).to.be.true
|
|
140
|
-
// })
|
|
141
|
-
|
|
142
|
-
it('should publish data', async () => {
|
|
143
|
-
const topic = 'test-topic'
|
|
144
|
-
const data = 'test-data'
|
|
145
|
-
const publishSpy = sinon.spy(peernet, 'publish')
|
|
146
|
-
await peernet.publish(topic, data)
|
|
147
|
-
expect(publishSpy.calledOnceWith(topic, data)).to.be.true
|
|
148
|
-
})
|
|
149
|
-
|
|
150
|
-
it('should subscribe to a topic', async () => {
|
|
151
|
-
const topic = 'test-topic'
|
|
152
|
-
const callback = sinon.spy()
|
|
153
|
-
const subscribeSpy = sinon.spy(peernet, 'subscribe')
|
|
154
|
-
await peernet.subscribe(topic, callback)
|
|
155
|
-
expect(subscribeSpy.calledOnceWith(topic, callback)).to.be.true
|
|
156
|
-
|
|
157
|
-
process.exit()
|
|
158
|
-
})
|
|
159
|
-
})
|
|
1
|
+
import Peernet from '../exports/peernet.js'
|
|
2
|
+
import Identity from '../exports/identity.js'
|
|
3
|
+
import { expect } from 'chai'
|
|
4
|
+
import sinon from 'sinon'
|
|
5
|
+
import networks from '@leofcoin/networks'
|
|
6
|
+
|
|
7
|
+
const network = networks.leofcoin.peach
|
|
8
|
+
|
|
9
|
+
let options
|
|
10
|
+
let password
|
|
11
|
+
options = {
|
|
12
|
+
network: 'leofcoin:peach',
|
|
13
|
+
stars: network.stars,
|
|
14
|
+
root: '.testnet',
|
|
15
|
+
version: '1.0.0',
|
|
16
|
+
storePrefix: 'test'
|
|
17
|
+
}
|
|
18
|
+
password = 'password'
|
|
19
|
+
const peernet = await new Peernet(options, password)
|
|
20
|
+
|
|
21
|
+
describe('Peernet', async () => {
|
|
22
|
+
// beforeEach(async () => {
|
|
23
|
+
// })
|
|
24
|
+
|
|
25
|
+
it('should initialize with the correct network', () => {
|
|
26
|
+
expect(peernet.network).to.equal('leofcoin:peach')
|
|
27
|
+
})
|
|
28
|
+
|
|
29
|
+
it('should initialize with the correct version', () => {
|
|
30
|
+
expect(peernet.version).to.equal('1.0.0')
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
it('should initialize with the correct store prefix', () => {
|
|
34
|
+
expect(peernet.storePrefix).to.equal('test')
|
|
35
|
+
})
|
|
36
|
+
|
|
37
|
+
it('should have an identity instance', () => {
|
|
38
|
+
expect(peernet.identity).to.be.instanceOf(Identity)
|
|
39
|
+
})
|
|
40
|
+
|
|
41
|
+
it('should have a DHT instance', () => {
|
|
42
|
+
expect(peernet.dht).to.not.be.undefined
|
|
43
|
+
})
|
|
44
|
+
|
|
45
|
+
it('should start the client', async () => {
|
|
46
|
+
const startSpy = sinon.spy(peernet, 'start')
|
|
47
|
+
await peernet.start()
|
|
48
|
+
expect(startSpy.calledOnce).to.be.true
|
|
49
|
+
})
|
|
50
|
+
|
|
51
|
+
it('should add a proto', () => {
|
|
52
|
+
const protoName = 'test-proto'
|
|
53
|
+
const proto = {}
|
|
54
|
+
peernet.addProto(protoName, proto)
|
|
55
|
+
expect(globalThis.peernet.protos[protoName]).to.equal(proto)
|
|
56
|
+
})
|
|
57
|
+
|
|
58
|
+
it('should add a codec', () => {
|
|
59
|
+
const codec = { name: 'test-codec' }
|
|
60
|
+
const addCodecSpy = sinon.spy(peernet, 'addCodec')
|
|
61
|
+
peernet.addCodec(codec)
|
|
62
|
+
expect(addCodecSpy.calledOnceWith(codec)).to.be.true
|
|
63
|
+
})
|
|
64
|
+
|
|
65
|
+
it('should select an account', async () => {
|
|
66
|
+
const account = 'test-account'
|
|
67
|
+
const selectAccountSpy = sinon.spy(peernet.identity, 'selectAccount')
|
|
68
|
+
await peernet.selectAccount(account)
|
|
69
|
+
expect(selectAccountSpy.calledOnceWith(account)).to.be.true
|
|
70
|
+
})
|
|
71
|
+
|
|
72
|
+
it('should prepare a message', () => {
|
|
73
|
+
const data = { message: 'test' }
|
|
74
|
+
const prepareMessageSpy = sinon.spy(peernet._messageHandler, 'prepareMessage')
|
|
75
|
+
peernet.prepareMessage(data)
|
|
76
|
+
expect(prepareMessageSpy.calledOnceWith(data)).to.be.true
|
|
77
|
+
})
|
|
78
|
+
|
|
79
|
+
it('should get peers', () => {
|
|
80
|
+
const peers = peernet.peers
|
|
81
|
+
expect(peers).to.be.an('array')
|
|
82
|
+
})
|
|
83
|
+
|
|
84
|
+
it('should get connections', () => {
|
|
85
|
+
const connections = peernet.connections
|
|
86
|
+
expect(connections).to.be.an('object')
|
|
87
|
+
})
|
|
88
|
+
|
|
89
|
+
it('should get a connection by id', () => {
|
|
90
|
+
const id = 'test-id'
|
|
91
|
+
const connection = peernet.getConnection(id)
|
|
92
|
+
expect(connection).to.be.undefined // Assuming no connections are established in the test
|
|
93
|
+
})
|
|
94
|
+
|
|
95
|
+
// it('should handle DHT', async () => {
|
|
96
|
+
// const peer = {}
|
|
97
|
+
// const id = 'test-id'
|
|
98
|
+
// const proto = { decoded: { hash: 'test-hash', store: 'test-store' } }
|
|
99
|
+
// const handleDHTSpy = sinon.spy(peernet, 'handleDHT')
|
|
100
|
+
// await peernet.handleDHT(peer, id, proto)
|
|
101
|
+
// expect(handleDHTSpy.calledOnceWith(peer, id, proto)).to.be.true
|
|
102
|
+
// })
|
|
103
|
+
|
|
104
|
+
it('should handle data', async () => {
|
|
105
|
+
const peer = {}
|
|
106
|
+
const id = 'test-id'
|
|
107
|
+
const proto = { decoded: { hash: 'test-hash', store: 'test-store' } }
|
|
108
|
+
const handleDataSpy = sinon.spy(peernet, 'handleData')
|
|
109
|
+
await peernet.handleData(peer, id, proto)
|
|
110
|
+
expect(handleDataSpy.calledOnceWith(peer, id, proto)).to.be.true
|
|
111
|
+
})
|
|
112
|
+
|
|
113
|
+
it('should handle request', async () => {
|
|
114
|
+
const peer = {}
|
|
115
|
+
const id = 'test-id'
|
|
116
|
+
const proto = { decoded: { request: 'test-request' } }
|
|
117
|
+
const handleRequestSpy = sinon.spy(peernet, 'handleRequest')
|
|
118
|
+
await peernet.handleRequest(peer, id, proto)
|
|
119
|
+
expect(handleRequestSpy.calledOnceWith(peer, id, proto)).to.be.true
|
|
120
|
+
})
|
|
121
|
+
|
|
122
|
+
it('should walk the network', async () => {
|
|
123
|
+
const hash = 'test-hash'
|
|
124
|
+
const walkSpy = sinon.spy(peernet, 'walk')
|
|
125
|
+
await peernet.walk(hash)
|
|
126
|
+
expect(walkSpy.calledOnceWith(hash)).to.be.true
|
|
127
|
+
})
|
|
128
|
+
|
|
129
|
+
// it('should find providers for a hash', async () => {
|
|
130
|
+
// const hash = 'test-hash'
|
|
131
|
+
// const providers = await peernet.providersFor(hash)
|
|
132
|
+
// expect(providers).to.be.undefined // Assuming no providers are found in the test
|
|
133
|
+
// })
|
|
134
|
+
|
|
135
|
+
// it('should request data', async () => {
|
|
136
|
+
// const hash = 'test-hash'
|
|
137
|
+
// const requestDataSpy = sinon.spy(peernet, 'requestData')
|
|
138
|
+
// await peernet.requestData(hash, 'data')
|
|
139
|
+
// expect(requestDataSpy.calledOnceWith(hash, 'data')).to.be.true
|
|
140
|
+
// })
|
|
141
|
+
|
|
142
|
+
it('should publish data', async () => {
|
|
143
|
+
const topic = 'test-topic'
|
|
144
|
+
const data = 'test-data'
|
|
145
|
+
const publishSpy = sinon.spy(peernet, 'publish')
|
|
146
|
+
await peernet.publish(topic, data)
|
|
147
|
+
expect(publishSpy.calledOnceWith(topic, data)).to.be.true
|
|
148
|
+
})
|
|
149
|
+
|
|
150
|
+
it('should subscribe to a topic', async () => {
|
|
151
|
+
const topic = 'test-topic'
|
|
152
|
+
const callback = sinon.spy()
|
|
153
|
+
const subscribeSpy = sinon.spy(peernet, 'subscribe')
|
|
154
|
+
await peernet.subscribe(topic, callback)
|
|
155
|
+
expect(subscribeSpy.calledOnceWith(topic, callback)).to.be.true
|
|
156
|
+
|
|
157
|
+
process.exit()
|
|
158
|
+
})
|
|
159
|
+
})
|
package/test.js
CHANGED
|
@@ -1,62 +1,62 @@
|
|
|
1
|
-
import Client from './exports/peernet.js'
|
|
2
|
-
|
|
3
|
-
const client = await new Client({
|
|
4
|
-
network: 'leofcoin:peach',
|
|
5
|
-
networkVersion: 'peach',
|
|
6
|
-
stars: ['wss://star.leofcoin.org']
|
|
7
|
-
})
|
|
8
|
-
console.log(pubsub.subscribers)
|
|
9
|
-
pubsub.subscribe('peer:discovered', (data) => {
|
|
10
|
-
// console.log(data);
|
|
11
|
-
console.log(`discovered: ${Object.keys(peernet.client.discovered).length}`)
|
|
12
|
-
console.log(Object.keys(peernet.client.discovered))
|
|
13
|
-
})
|
|
14
|
-
pubsub.subscribe('peer:connected', (data) => {
|
|
15
|
-
// console.log(data);
|
|
16
|
-
console.log(`connections: ${peernet.connections.length}`)
|
|
17
|
-
})
|
|
18
|
-
pubsub.subscribe('peer:left', (data) => {
|
|
19
|
-
console.log(`connections: ${peernet.connections.length}`)
|
|
20
|
-
console.log(`discovered: ${Object.keys(peernet.client.discovered).length}`)
|
|
21
|
-
// console.log(data);
|
|
22
|
-
})
|
|
23
|
-
|
|
24
|
-
// peernet.addFolder([{
|
|
25
|
-
// path: 'assets/asset.png',
|
|
26
|
-
// content: Buffer.from('png')
|
|
27
|
-
// }, {
|
|
28
|
-
// path: 'index.html',
|
|
29
|
-
// content: Buffer.from('html')
|
|
30
|
-
// }]).then(hash => peernet.ls(hash).then(paths => peernet.cat(paths[0].hash).then(content => console.log(content))))
|
|
31
|
-
|
|
32
|
-
// pubsub.subscribe('peer:connected', async peer => {
|
|
33
|
-
// chainStore.put('localBlock', '00000')
|
|
34
|
-
// const request = await new globalThis.peernet.protos['peernet-request']({
|
|
35
|
-
// request:'lastBlock'
|
|
36
|
-
// })
|
|
37
|
-
// const to = peer.id
|
|
38
|
-
// await peernet.data.put('hello', 'hi')
|
|
39
|
-
// console.log({request});
|
|
40
|
-
// const node = await peernet.prepareMessage(request)
|
|
41
|
-
// console.log({node});
|
|
42
|
-
// let response = await peer.request(node.encoded)
|
|
43
|
-
// console.log({response});
|
|
44
|
-
// const keys = Object.keys(response)
|
|
45
|
-
// const uint8Array = new Uint8Array(keys.length)
|
|
46
|
-
// for (const key of keys) {
|
|
47
|
-
// uint8Array[Number(key)] = response[key]
|
|
48
|
-
// }
|
|
49
|
-
// // const proto = await new globalThis.peernet.protos['peernet-message'](uint8Array)
|
|
50
|
-
// // console.log(proto.decoded.data);
|
|
51
|
-
// response = await new globalThis.peernet.protos['peernet-response'](uint8Array)
|
|
52
|
-
// console.log({response});
|
|
53
|
-
|
|
54
|
-
// const block = new TextDecoder().decode(response.decoded.response)
|
|
55
|
-
// console.log(block);
|
|
56
|
-
// const task = () => setTimeout(() => {
|
|
57
|
-
// console.log(peernet.connections[0]?.connected);
|
|
58
|
-
// console.log(pubsub.subscribers);
|
|
59
|
-
// task()
|
|
60
|
-
// }, 5000);
|
|
61
|
-
// task()
|
|
62
|
-
// })
|
|
1
|
+
import Client from './exports/peernet.js'
|
|
2
|
+
|
|
3
|
+
const client = await new Client({
|
|
4
|
+
network: 'leofcoin:peach',
|
|
5
|
+
networkVersion: 'peach',
|
|
6
|
+
stars: ['wss://star.leofcoin.org']
|
|
7
|
+
})
|
|
8
|
+
console.log(pubsub.subscribers)
|
|
9
|
+
pubsub.subscribe('peer:discovered', (data) => {
|
|
10
|
+
// console.log(data);
|
|
11
|
+
console.log(`discovered: ${Object.keys(peernet.client.discovered).length}`)
|
|
12
|
+
console.log(Object.keys(peernet.client.discovered))
|
|
13
|
+
})
|
|
14
|
+
pubsub.subscribe('peer:connected', (data) => {
|
|
15
|
+
// console.log(data);
|
|
16
|
+
console.log(`connections: ${peernet.connections.length}`)
|
|
17
|
+
})
|
|
18
|
+
pubsub.subscribe('peer:left', (data) => {
|
|
19
|
+
console.log(`connections: ${peernet.connections.length}`)
|
|
20
|
+
console.log(`discovered: ${Object.keys(peernet.client.discovered).length}`)
|
|
21
|
+
// console.log(data);
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
// peernet.addFolder([{
|
|
25
|
+
// path: 'assets/asset.png',
|
|
26
|
+
// content: Buffer.from('png')
|
|
27
|
+
// }, {
|
|
28
|
+
// path: 'index.html',
|
|
29
|
+
// content: Buffer.from('html')
|
|
30
|
+
// }]).then(hash => peernet.ls(hash).then(paths => peernet.cat(paths[0].hash).then(content => console.log(content))))
|
|
31
|
+
|
|
32
|
+
// pubsub.subscribe('peer:connected', async peer => {
|
|
33
|
+
// chainStore.put('localBlock', '00000')
|
|
34
|
+
// const request = await new globalThis.peernet.protos['peernet-request']({
|
|
35
|
+
// request:'lastBlock'
|
|
36
|
+
// })
|
|
37
|
+
// const to = peer.id
|
|
38
|
+
// await peernet.data.put('hello', 'hi')
|
|
39
|
+
// console.log({request});
|
|
40
|
+
// const node = await peernet.prepareMessage(request)
|
|
41
|
+
// console.log({node});
|
|
42
|
+
// let response = await peer.request(node.encoded)
|
|
43
|
+
// console.log({response});
|
|
44
|
+
// const keys = Object.keys(response)
|
|
45
|
+
// const uint8Array = new Uint8Array(keys.length)
|
|
46
|
+
// for (const key of keys) {
|
|
47
|
+
// uint8Array[Number(key)] = response[key]
|
|
48
|
+
// }
|
|
49
|
+
// // const proto = await new globalThis.peernet.protos['peernet-message'](uint8Array)
|
|
50
|
+
// // console.log(proto.decoded.data);
|
|
51
|
+
// response = await new globalThis.peernet.protos['peernet-response'](uint8Array)
|
|
52
|
+
// console.log({response});
|
|
53
|
+
|
|
54
|
+
// const block = new TextDecoder().decode(response.decoded.response)
|
|
55
|
+
// console.log(block);
|
|
56
|
+
// const task = () => setTimeout(() => {
|
|
57
|
+
// console.log(peernet.connections[0]?.connected);
|
|
58
|
+
// console.log(pubsub.subscribers);
|
|
59
|
+
// task()
|
|
60
|
+
// }, 5000);
|
|
61
|
+
// task()
|
|
62
|
+
// })
|
package/test2.js
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
import Client from './exports/peernet.js'
|
|
2
|
-
;(async () => {
|
|
3
|
-
const client = await new Client({
|
|
4
|
-
root: '.peernet/test2',
|
|
5
|
-
network: 'peach',
|
|
6
|
-
networkVersion: 'peach',
|
|
7
|
-
stars: [['wss://star.leofcoin.org']]
|
|
8
|
-
})
|
|
9
|
-
const job = () => client.publish('socket-data', 'hello')
|
|
10
|
-
setTimeout(async () => {
|
|
11
|
-
job()
|
|
12
|
-
}, 10000)
|
|
13
|
-
})()
|
|
1
|
+
import Client from './exports/peernet.js'
|
|
2
|
+
;(async () => {
|
|
3
|
+
const client = await new Client({
|
|
4
|
+
root: '.peernet/test2',
|
|
5
|
+
network: 'peach',
|
|
6
|
+
networkVersion: 'peach',
|
|
7
|
+
stars: [['wss://star.leofcoin.org']]
|
|
8
|
+
})
|
|
9
|
+
const job = () => client.publish('socket-data', 'hello')
|
|
10
|
+
setTimeout(async () => {
|
|
11
|
+
job()
|
|
12
|
+
}, 10000)
|
|
13
|
+
})()
|
package/test3.js
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
|
-
const Client = require('./dist/commonjs/peernet.js')
|
|
2
|
-
const PeernetMessage = require('./dist/messages/peernet.js');
|
|
3
|
-
const DHTMessage = require('./dist/messages/dht.js');
|
|
4
|
-
|
|
5
|
-
globalThis.DEBUG = true;
|
|
6
|
-
const client = new Client({root: '.peernet/test3'})
|
|
7
|
-
|
|
8
|
-
pubsub.subscribe('peer:connected', async peer => setInterval(async () => {
|
|
9
|
-
// const dhtMessage = new DHTMessage({hash: 'hello'})
|
|
10
|
-
// console.log(dhtMessage.encoded);
|
|
11
|
-
// const message = new PeernetMessage({ from: client.id, to: peer.id, data: dhtMessage.encoded, signature: Buffer.from('pnsig') })
|
|
12
|
-
// console.log(message.encoded);
|
|
13
|
-
// console.log(await peer.request(message.encoded));
|
|
14
|
-
const data = await client.get('hello')
|
|
15
|
-
console.log(data.toString())
|
|
1
|
+
const Client = require('./dist/commonjs/peernet.js')
|
|
2
|
+
const PeernetMessage = require('./dist/messages/peernet.js');
|
|
3
|
+
const DHTMessage = require('./dist/messages/dht.js');
|
|
4
|
+
|
|
5
|
+
globalThis.DEBUG = true;
|
|
6
|
+
const client = new Client({root: '.peernet/test3'})
|
|
7
|
+
|
|
8
|
+
pubsub.subscribe('peer:connected', async peer => setInterval(async () => {
|
|
9
|
+
// const dhtMessage = new DHTMessage({hash: 'hello'})
|
|
10
|
+
// console.log(dhtMessage.encoded);
|
|
11
|
+
// const message = new PeernetMessage({ from: client.id, to: peer.id, data: dhtMessage.encoded, signature: Buffer.from('pnsig') })
|
|
12
|
+
// console.log(message.encoded);
|
|
13
|
+
// console.log(await peer.request(message.encoded));
|
|
14
|
+
const data = await client.get('hello')
|
|
15
|
+
console.log(data.toString())
|
|
16
16
|
}, 5000))
|
package/test4.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
const Client = require('./dist/commonjs/peernet.js')
|
|
2
|
-
|
|
3
|
-
const client = new Client({root: '.peernet/test4'})
|
|
4
|
-
|
|
5
|
-
pubsub.subscribe('peer:connected', async peer => {
|
|
6
|
-
await peernet.get("ba5xcadcnub27naa7zrcvtmyruviahaiwvupatfgjkvdgcehuif7clissf")
|
|
7
|
-
})
|
|
1
|
+
const Client = require('./dist/commonjs/peernet.js')
|
|
2
|
+
|
|
3
|
+
const client = new Client({root: '.peernet/test4'})
|
|
4
|
+
|
|
5
|
+
pubsub.subscribe('peer:connected', async peer => {
|
|
6
|
+
await peernet.get("ba5xcadcnub27naa7zrcvtmyruviahaiwvupatfgjkvdgcehuif7clissf")
|
|
7
|
+
})
|
package/tsconfig.json
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
{
|
|
2
|
-
"compilerOptions": {
|
|
3
|
-
"module": "NodeNext",
|
|
4
|
-
"target": "ESNext",
|
|
5
|
-
"declaration": true,
|
|
6
|
-
"outDir": "./exports",
|
|
7
|
-
"moduleResolution": "NodeNext",
|
|
8
|
-
"allowJs": true
|
|
9
|
-
},
|
|
10
|
-
"include": ["./src/**/*"]
|
|
11
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"module": "NodeNext",
|
|
4
|
+
"target": "ESNext",
|
|
5
|
+
"declaration": true,
|
|
6
|
+
"outDir": "./exports",
|
|
7
|
+
"moduleResolution": "NodeNext",
|
|
8
|
+
"allowJs": true
|
|
9
|
+
},
|
|
10
|
+
"include": ["./src/**/*"]
|
|
11
|
+
}
|