helia-coord 1.1.0

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 (57) hide show
  1. package/.on-save.json +8 -0
  2. package/PEDIGREE.md +3 -0
  3. package/README.md +3 -0
  4. package/config/bootstrap-circuit-relays.js +48 -0
  5. package/examples/start-external.js +56 -0
  6. package/index.js +129 -0
  7. package/lib/adapters/bch-adapter.js +94 -0
  8. package/lib/adapters/encryption-adapter.js +123 -0
  9. package/lib/adapters/gist.js +42 -0
  10. package/lib/adapters/index.js +66 -0
  11. package/lib/adapters/ipfs-adapter.js +263 -0
  12. package/lib/adapters/logs-adapter.js +44 -0
  13. package/lib/adapters/pubsub-adapter/README.md +86 -0
  14. package/lib/adapters/pubsub-adapter/about-adapter.js +117 -0
  15. package/lib/adapters/pubsub-adapter/index.js +275 -0
  16. package/lib/adapters/pubsub-adapter/messaging.js +389 -0
  17. package/lib/adapters/pubsub-adapter/msg-router.js +58 -0
  18. package/lib/adapters/pubsub-adapter/resend-msg.js +58 -0
  19. package/lib/controllers/index.js +24 -0
  20. package/lib/controllers/timer-controller.js +417 -0
  21. package/lib/entities/this-node-entity.js +102 -0
  22. package/lib/use-cases/index.js +36 -0
  23. package/lib/use-cases/peer-use-cases.js +146 -0
  24. package/lib/use-cases/pubsub-use-cases.js +56 -0
  25. package/lib/use-cases/relay-use-cases.js +479 -0
  26. package/lib/use-cases/schema.js +158 -0
  27. package/lib/use-cases/this-node-use-cases.js +443 -0
  28. package/lib/util/utils.js +12 -0
  29. package/package.json +52 -0
  30. package/test/mocks/adapter-mock.js +119 -0
  31. package/test/mocks/circuit-relay-mocks.js +67 -0
  32. package/test/mocks/ipfs-mock.js +46 -0
  33. package/test/mocks/peers-mock.js +75 -0
  34. package/test/mocks/pubsub-mocks.js +37 -0
  35. package/test/mocks/thisnode-mocks.js +82 -0
  36. package/test/mocks/use-case-mocks.js +24 -0
  37. package/test/unit/adapters/bch-adapter-unit.js +96 -0
  38. package/test/unit/adapters/encryption-adapter-unit.js +129 -0
  39. package/test/unit/adapters/gist.unit.adapters.js +58 -0
  40. package/test/unit/adapters/index-adapters-unit.js +79 -0
  41. package/test/unit/adapters/ipfs-adapter-unit.js +215 -0
  42. package/test/unit/adapters/logs-adapter-unit.js +55 -0
  43. package/test/unit/adapters/pubsub/about-adapter-unit.js +129 -0
  44. package/test/unit/adapters/pubsub/messaging-adapter-unit.js +576 -0
  45. package/test/unit/adapters/pubsub/pubsub-adapter-unit.js +367 -0
  46. package/test/unit/adapters/pubsub/resend-msg-adapter-unit.js +58 -0
  47. package/test/unit/controllers/controllers-index-unit.js +30 -0
  48. package/test/unit/controllers/timer-controller-unit.js +261 -0
  49. package/test/unit/entities/this-node.unit.entity.js +157 -0
  50. package/test/unit/index-unit.js +160 -0
  51. package/test/unit/use-cases/peer.unit.use-cases.js +186 -0
  52. package/test/unit/use-cases/pubsub.unit.use-cases.js +114 -0
  53. package/test/unit/use-cases/relay-use-cases-unit.js +658 -0
  54. package/test/unit/use-cases/schema-use-case-unit.js +101 -0
  55. package/test/unit/use-cases/this-node-use-cases-unit.js +427 -0
  56. package/test/unit/use-cases/use-cases-index-unit.js +47 -0
  57. package/test/unit/util/utils-unit.js +31 -0
@@ -0,0 +1,58 @@
1
+ /*
2
+ Unit tests for the schema.js library.
3
+ */
4
+
5
+ // npm libraries
6
+ import { assert } from 'chai'
7
+ import sinon from 'sinon'
8
+
9
+ // local libraries
10
+ import Gist from '../../../lib/adapters/gist.js'
11
+
12
+ describe('#Adapter-Gist', () => {
13
+ let sandbox
14
+ let uut
15
+
16
+ beforeEach(() => {
17
+ // Restore the sandbox before each test.
18
+ sandbox = sinon.createSandbox()
19
+
20
+ uut = new Gist()
21
+ })
22
+
23
+ afterEach(() => sandbox.restore())
24
+
25
+ describe('#getCRList', () => {
26
+ it('should get data from GitHub', async () => {
27
+ // Mock network dependencies
28
+ sandbox.stub(uut.axios, 'get').resolves({
29
+ data: {
30
+ files: {
31
+ 'psf-public-circuit-relays.json': {
32
+ content: JSON.stringify({ key: 'value' })
33
+ }
34
+ }
35
+ }
36
+ })
37
+
38
+ const result = await uut.getCRList()
39
+ // console.log('result: ', result)
40
+
41
+ assert.property(result, 'key')
42
+ assert.equal(result.key, 'value')
43
+ })
44
+
45
+ it('should catch and throw errors', async () => {
46
+ try {
47
+ // Force desired code path
48
+ sandbox.stub(uut.axios, 'get').rejects(new Error('test error'))
49
+
50
+ await uut.getCRList()
51
+
52
+ assert.fail('Unexpected result')
53
+ } catch (err) {
54
+ assert.include(err.message, 'test error')
55
+ }
56
+ })
57
+ })
58
+ })
@@ -0,0 +1,79 @@
1
+ /*
2
+ Unit tests for the main Adapters index.js file.
3
+ */
4
+
5
+ // npm libraries
6
+ import { assert } from 'chai'
7
+ import SlpWallet from 'minimal-slp-wallet'
8
+
9
+ // Local libraries
10
+ import Adapters from '../../../lib/adapters/index.js'
11
+ import ipfs from '../../mocks/ipfs-mock.js'
12
+
13
+ // const Adapters = require('../../../lib/adapters')
14
+ // const BCHJS = require('@psf/bch-js')
15
+ // const bchjs = new BCHJS()
16
+ // const ipfs = require('../../mocks/ipfs-mock')
17
+ // const wallet = new SlpWallet()
18
+
19
+ describe('#Adapters - index.js', () => {
20
+ let uut, wallet
21
+
22
+ beforeEach(async () => {
23
+ wallet = new SlpWallet()
24
+ await wallet.walletInfoPromise
25
+ })
26
+
27
+ describe('#constructor', () => {
28
+ it('should throw an error if ipfs is not included', () => {
29
+ try {
30
+ uut = new Adapters()
31
+
32
+ assert.fail('Unexpected code path')
33
+ } catch (err) {
34
+ assert.include(
35
+ err.message,
36
+ 'An instance of IPFS must be passed when instantiating the Adapters library.'
37
+ )
38
+ }
39
+ })
40
+
41
+ it('should throw an error if minimal-slp-wallet is not included', () => {
42
+ try {
43
+ uut = new Adapters({ ipfs: {} })
44
+
45
+ assert.fail('Unexpected code path')
46
+ } catch (err) {
47
+ assert.include(
48
+ err.message,
49
+ 'An instance of minimal-slp-wallet must be passed when instantiating the Adapters library.'
50
+ )
51
+ }
52
+ })
53
+
54
+ it('should throw an error if node type is not specified', () => {
55
+ try {
56
+ uut = new Adapters({ ipfs: {}, wallet: {} })
57
+
58
+ assert.fail('Unexpected code path')
59
+ } catch (err) {
60
+ assert.include(
61
+ err.message,
62
+ 'The type of IPFS node (browser or node.js) must be specified.'
63
+ )
64
+ }
65
+ })
66
+
67
+ it('should instantiate other adapter libraries', () => {
68
+ uut = new Adapters({
69
+ ipfs,
70
+ wallet,
71
+ type: 'node.js',
72
+ statusLog: () => {},
73
+ privateLog: () => {}
74
+ })
75
+
76
+ assert.property(uut, 'log')
77
+ })
78
+ })
79
+ })
@@ -0,0 +1,215 @@
1
+ /*
2
+ Unit tests for the ipfs-adapters.js library
3
+ */
4
+
5
+ // npm libraries
6
+ import { assert } from 'chai'
7
+ import sinon from 'sinon'
8
+
9
+ // local libraries
10
+ import IPFSAdapter from '../../../lib/adapters/ipfs-adapter.js'
11
+ import ipfs from '../../mocks/ipfs-mock.js'
12
+
13
+ describe('#Adapter - IPFS', () => {
14
+ let sandbox
15
+ let uut
16
+
17
+ beforeEach(() => {
18
+ // Restore the sandbox before each test.
19
+ sandbox = sinon.createSandbox()
20
+
21
+ const log = {
22
+ statusLog: () => {}
23
+ }
24
+ uut = new IPFSAdapter({ ipfs, log })
25
+ })
26
+
27
+ afterEach(() => sandbox.restore())
28
+
29
+ describe('#constructor', () => {
30
+ it('should throw an error if ipfs instance is not passed in', () => {
31
+ try {
32
+ uut = new IPFSAdapter({})
33
+ } catch (err) {
34
+ assert.include(
35
+ err.message,
36
+ 'An instance of IPFS must be passed when instantiating the IPFS adapter library.'
37
+ )
38
+ }
39
+ })
40
+
41
+ it('should throw an error if log instance is not passed in', () => {
42
+ try {
43
+ uut = new IPFSAdapter({ ipfs })
44
+ } catch (err) {
45
+ assert.include(
46
+ err.message,
47
+ 'A status log handler must be specified when instantiating IPFS adapter library.'
48
+ )
49
+ }
50
+ })
51
+
52
+ it('should overwrite default ports when user specifies them at run time', async () => {
53
+ const log = {
54
+ statusLog: () => {}
55
+ }
56
+ uut = new IPFSAdapter({ ipfs, log, tcpPort: 6000, wsPort: 6001 })
57
+
58
+ assert.equal(uut.tcpPort, 6000)
59
+ assert.equal(uut.wsPort, 6001)
60
+ })
61
+ })
62
+
63
+ describe('#start', () => {
64
+ it('should populate ID and multiaddrs', async () => {
65
+ await uut.start()
66
+
67
+ // console.log('uut: ', uut)
68
+
69
+ assert.property(uut, 'ipfsPeerId')
70
+ assert.property(uut, 'ipfsMultiaddrs')
71
+ })
72
+
73
+ it('should configure an exteranl go-ipfs node', async () => {
74
+ const log = {
75
+ statusLog: () => {}
76
+ }
77
+ uut = new IPFSAdapter({ ipfs, log, nodeType: 'external' })
78
+
79
+ await uut.start()
80
+
81
+ // console.log('uut: ', uut)
82
+
83
+ assert.equal(uut.nodeType, 'external')
84
+ })
85
+
86
+ it('should catch and throw an error', async () => {
87
+ try {
88
+ // Force an error
89
+ // sandbox.stub(uut.ipfs, 'id').rejects(new Error('test error'))
90
+ sandbox.stub(uut.ipfs.libp2p.peerId, 'toString').throws(new Error('test error'))
91
+
92
+ await uut.start()
93
+
94
+ assert.fail('Unexpected code path')
95
+ } catch (err) {
96
+ assert.include(err.message, 'test error')
97
+ }
98
+ })
99
+ })
100
+
101
+ describe('#connectToPeer', () => {
102
+ it('should return true after connecting to peer', async () => {
103
+ // Mock dependencies
104
+ sandbox.stub(uut.ipfs.libp2p, 'dial').resolves()
105
+ sandbox.stub(uut, 'multiaddr').returns()
106
+
107
+ const result = await uut.connectToPeer('fakeId')
108
+
109
+ assert.equal(result, true)
110
+ })
111
+
112
+ it('should return false when issues connecting to peer', async () => {
113
+ // Force an error
114
+ sandbox.stub(uut.ipfs.swarm, 'connect').rejects(new Error('test error'))
115
+
116
+ const result = await uut.connectToPeer('fakeId')
117
+
118
+ assert.equal(result, false)
119
+ })
120
+
121
+ it('should report connection errors at debugLevel 1', async () => {
122
+ uut.debugLevel = 1
123
+
124
+ // Force an error
125
+ sandbox.stub(uut.ipfs.swarm, 'connect').rejects(new Error('test error'))
126
+
127
+ const result = await uut.connectToPeer('fakeId')
128
+
129
+ assert.equal(result, false)
130
+ })
131
+
132
+ it('should report full errors at debugLevel 2', async () => {
133
+ uut.debugLevel = 2
134
+
135
+ // Force an error
136
+ sandbox.stub(uut.ipfs.swarm, 'connect').rejects(new Error('test error'))
137
+
138
+ const result = await uut.connectToPeer('fakeId')
139
+
140
+ assert.equal(result, false)
141
+ })
142
+ })
143
+
144
+ describe('#getPeers', () => {
145
+ it('should return an array of peers', async () => {
146
+ const result = await uut.getPeers()
147
+
148
+ assert.isArray(result)
149
+ })
150
+
151
+ it('should catch and throw an error', async () => {
152
+ try {
153
+ // Force an error
154
+ sandbox.stub(uut.ipfs.libp2p, 'getPeers').rejects(new Error('test error'))
155
+
156
+ await uut.getPeers()
157
+
158
+ assert.fail('Unexpected code path')
159
+ } catch (err) {
160
+ assert.include(err.message, 'test error')
161
+ }
162
+ })
163
+ })
164
+
165
+ describe('#disconnectFromPeer', () => {
166
+ it('should return true if thisNode is not connected to the peer', async () => {
167
+ // Mock dependencies
168
+ sandbox.stub(uut, 'getPeers').resolves([])
169
+
170
+ const result = await uut.disconnectFromPeer('testId')
171
+
172
+ assert.equal(result, true)
173
+ })
174
+
175
+ it('should disconnect if thisNode is connected to the peer', async () => {
176
+ // Mock dependencies
177
+ sandbox.stub(uut, 'getPeers').resolves([{ peer: 'testId' }])
178
+
179
+ const result = await uut.disconnectFromPeer('testId')
180
+
181
+ assert.equal(result, true)
182
+ })
183
+
184
+ it('should return false on error', async () => {
185
+ // Force an error
186
+ sandbox.stub(uut, 'getPeers').rejects(new Error('test error'))
187
+
188
+ const result = await uut.disconnectFromPeer('testId')
189
+
190
+ assert.equal(result, false)
191
+ })
192
+ })
193
+
194
+ describe('#disconnectFromMultiaddr', () => {
195
+ it('should return true when disconnect succeeds', async () => {
196
+ // Mock dependencies
197
+ sandbox.stub(uut.ipfs.swarm, 'disconnect').resolves()
198
+
199
+ const result = await uut.disconnectFromMultiaddr()
200
+
201
+ assert.equal(result, true)
202
+ })
203
+
204
+ it('should return false on error', async () => {
205
+ // Mock dependencies
206
+ sandbox
207
+ .stub(uut.ipfs.swarm, 'disconnect')
208
+ .rejects(new Error('test error'))
209
+
210
+ const result = await uut.disconnectFromMultiaddr()
211
+
212
+ assert.equal(result, false)
213
+ })
214
+ })
215
+ })
@@ -0,0 +1,55 @@
1
+ /*
2
+ Unit tests for log adapter.
3
+ */
4
+
5
+ // npm libraries
6
+ import { assert } from 'chai'
7
+ import sinon from 'sinon'
8
+
9
+ // local libraries
10
+ import LogsAdapter from '../../../lib/adapters/logs-adapter.js'
11
+
12
+ describe('#Adapters - Logs', () => {
13
+ let uut
14
+ let sandbox
15
+
16
+ beforeEach(() => {
17
+ // Restore the sandbox before each test.
18
+ sandbox = sinon.createSandbox()
19
+
20
+ const config = {
21
+ debugLevel: 2,
22
+ statusLog: () => {}
23
+ }
24
+
25
+ // Instantiate the library under test. Must instantiate dependencies first.
26
+ uut = new LogsAdapter(config)
27
+ })
28
+
29
+ afterEach(() => sandbox.restore())
30
+
31
+ describe('#constructor', () => {
32
+ it('should throw an error if log handler is specified', async () => {
33
+ try {
34
+ uut = new LogsAdapter()
35
+
36
+ assert.fail('Unexpected code path')
37
+ } catch (err) {
38
+ assert.include(
39
+ err.message,
40
+ 'statusLog must be specified when instantiating Logs adapter library.'
41
+ )
42
+ }
43
+ })
44
+ })
45
+
46
+ describe('#statusLog', () => {
47
+ it('should include object if defined', () => {
48
+ uut.statusLog(1, 'test string', { message: 'obj message' })
49
+ })
50
+
51
+ it('should exclude object if undefined', () => {
52
+ uut.statusLog(1, 'test string')
53
+ })
54
+ })
55
+ })
@@ -0,0 +1,129 @@
1
+ /*
2
+ Unit tests for the about adapter library.
3
+ */
4
+
5
+ // npm libraries
6
+ import { assert } from 'chai'
7
+ import sinon from 'sinon'
8
+
9
+ // local libraries
10
+ import AboutAdapter from '../../../../lib/adapters/pubsub-adapter/about-adapter.js'
11
+
12
+ describe('#About-adapter', () => {
13
+ let uut
14
+ let sandbox
15
+
16
+ beforeEach(() => {
17
+ // Restore the sandbox before each test.
18
+ sandbox = sinon.createSandbox()
19
+
20
+ // Instantiate the library under test. Must instantiate dependencies first.
21
+ uut = new AboutAdapter()
22
+ })
23
+
24
+ afterEach(() => sandbox.restore())
25
+
26
+ describe('#sendRPC', () => {
27
+ it('should return false if response is not recieved in time', async () => {
28
+ // Prep test data.
29
+ uut.waitPeriod = 1
30
+ const ipfsId = 'testId'
31
+ const cmdStr = 'fakeCmd'
32
+ const id = 1
33
+ const thisNode = {
34
+ useCases: {
35
+ peer: {
36
+ sendPrivateMessage: async () => {
37
+ },
38
+ adapters: {
39
+ bch: {
40
+ bchjs: {
41
+ Util: {
42
+ sleep: () => {
43
+ }
44
+ }
45
+ }
46
+ }
47
+ }
48
+ }
49
+ }
50
+ }
51
+
52
+ const result = await uut.sendRPC(ipfsId, cmdStr, id, thisNode)
53
+ // console.log('result: ', result)
54
+
55
+ assert.equal(result, false)
56
+ })
57
+
58
+ it('should return the result of the RPC call', async () => {
59
+ // Prep test data.
60
+ uut.waitPeriod = 2000
61
+ const ipfsId = 'testId'
62
+ const cmdStr = 'fakeCmd'
63
+ const id = 1
64
+ const thisNode = {
65
+ useCases: {
66
+ peer: {
67
+ sendPrivateMessage: async () => {
68
+ },
69
+ adapters: {
70
+ bch: {
71
+ bchjs: {
72
+ Util: {
73
+ sleep: () => {
74
+ }
75
+ }
76
+ }
77
+ }
78
+ }
79
+ }
80
+ }
81
+ }
82
+
83
+ // Force positive code path.
84
+ uut.incomingData = '{"id": 1}'
85
+
86
+ const result = await uut.sendRPC(ipfsId, cmdStr, id, thisNode)
87
+ // console.log('result: ', result)
88
+
89
+ assert.equal(result, true)
90
+ })
91
+
92
+ it('should catch and throw errors', async () => {
93
+ try {
94
+ await uut.sendRPC()
95
+
96
+ assert.fail('Unexpected code path')
97
+ } catch (err) {
98
+ // console.log(err)
99
+ assert.include(err.message, 'Cannot read')
100
+ }
101
+ })
102
+ })
103
+
104
+ describe('#queryAbout', () => {
105
+ it('should return true after peer responds to RPC', async () => {
106
+ // Mock dependencies
107
+ sandbox.stub(uut, 'sendRPC').resolves(true)
108
+
109
+ const result = await uut.queryAbout()
110
+ assert.equal(result, true)
111
+ })
112
+
113
+ it('should return false if peer never responds to RPC', async () => {
114
+ // Mock dependencies
115
+ sandbox.stub(uut, 'sendRPC').resolves(false)
116
+
117
+ const result = await uut.queryAbout()
118
+ assert.equal(result, false)
119
+ })
120
+
121
+ it('should return false when there is an error', async () => {
122
+ // Mock dependencies
123
+ sandbox.stub(uut, 'sendRPC').rejects(new Error('test error'))
124
+
125
+ const result = await uut.queryAbout()
126
+ assert.equal(result, false)
127
+ })
128
+ })
129
+ })