This repository was archived by the owner on Feb 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathname-pubsub.js
191 lines (150 loc) · 5.16 KB
/
name-pubsub.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/* eslint max-nested-callbacks: ["error", 6] */
/* eslint-env mocha */
'use strict'
const hat = require('hat')
const { expect } = require('interface-ipfs-core/src/utils/mocha')
const base64url = require('base64url')
const { fromB58String } = require('multihashes')
const peerId = require('peer-id')
const isNode = require('detect-node')
const ipns = require('ipns')
const IPFS = require('../../src')
const waitFor = require('../utils/wait-for')
const delay = require('delay')
const DaemonFactory = require('ipfsd-ctl')
const df = DaemonFactory.create({
type: 'proc',
IpfsClient: require('ipfs-http-client')
})
const namespace = '/record/'
const ipfsRef = '/ipfs/QmPFVLPmp9zv5Z5KUqLhe2EivAGccQW2r7M7jhVJGLZoZU'
describe('name-pubsub', function () {
if (!isNode) {
return
}
let nodes
let nodeA
let nodeB
let idA
let idB
const createNode = () => df.spawn({
exec: IPFS,
args: [`--pass ${hat()}`, '--enable-namesys-pubsub'],
config: {
Bootstrap: [],
Discovery: {
MDNS: {
Enabled: false
},
webRTCStar: {
Enabled: false
}
}
},
preload: { enabled: false }
})
before(async function () {
this.timeout(40 * 1000)
nodes = await Promise.all([
createNode(),
createNode()
])
nodeA = nodes[0].api
nodeB = nodes[1].api
const ids = await Promise.all([
nodeA.id(),
nodeB.id()
])
idA = ids[0]
idB = ids[1]
await nodeA.swarm.connect(idB.addresses[0])
})
after(() => Promise.all(nodes.map((node) => node.stop())))
it('should publish and then resolve correctly', async function () {
this.timeout(80 * 1000)
let subscribed = false
function checkMessage (msg) {
subscribed = true
}
const alreadySubscribed = () => {
return subscribed === true
}
// Wait until a peer subscribes a topic
const waitForPeerToSubscribe = async (node, topic) => {
for (let i = 0; i < 5; i++) {
const res = await node.pubsub.peers(topic)
if (res && res.length) {
return
}
await delay(2000)
}
throw new Error(`Could not find subscription for topic ${topic}`)
}
const keys = ipns.getIdKeys(fromB58String(idA.id))
const topic = `${namespace}${base64url.encode(keys.routingKey.toBuffer())}`
await expect(nodeB.name.resolve(idA.id))
.to.eventually.be.rejected()
.and.to.have.property('code', 'ERR_NO_RECORD_FOUND')
await waitForPeerToSubscribe(nodeA, topic)
await nodeB.pubsub.subscribe(topic, checkMessage)
await nodeA.name.publish(ipfsRef, { resolve: false })
await waitFor(alreadySubscribed)
await delay(1000) // guarantee record is written
const res = await nodeB.name.resolve(idA.id)
expect(res).to.equal(ipfsRef)
})
it('should self resolve, publish and then resolve correctly', async function () {
this.timeout(6000)
const emptyDirCid = '/ipfs/QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn'
const [{ path }] = await nodeA.add(Buffer.from('pubsub records'))
const resolvesEmpty = await nodeB.name.resolve(idB.id)
expect(resolvesEmpty).to.be.eq(emptyDirCid)
await expect(nodeA.name.resolve(idB.id))
.to.eventually.be.rejected()
.and.to.have.property('code', 'ERR_NO_RECORD_FOUND')
const publish = await nodeB.name.publish(path)
expect(publish).to.be.eql({
name: idB.id,
value: `/ipfs/${path}`
})
const resolveB = await nodeB.name.resolve(idB.id)
expect(resolveB).to.be.eq(`/ipfs/${path}`)
await delay(5000)
const resolveA = await nodeA.name.resolve(idB.id)
expect(resolveA).to.be.eq(`/ipfs/${path}`)
})
it('should handle event on publish correctly', async function () {
this.timeout(80 * 1000)
const testAccountName = 'test-account'
let publishedMessage = null
let publishedMessageData = null
let publishedMessageDataValue = null
function checkMessage (msg) {
publishedMessage = msg
publishedMessageData = ipns.unmarshal(msg.data)
publishedMessageDataValue = publishedMessageData.value.toString('utf8')
}
const alreadySubscribed = () => {
return publishedMessage !== null
}
// Create account for publish
const testAccount = await nodeA.key.gen(testAccountName, {
type: 'rsa',
size: 2048
})
const keys = ipns.getIdKeys(fromB58String(testAccount.id))
const topic = `${namespace}${base64url.encode(keys.routingKey.toBuffer())}`
await nodeB.pubsub.subscribe(topic, checkMessage)
await nodeA.name.publish(ipfsRef, { resolve: false, key: testAccountName })
await waitFor(alreadySubscribed)
const messageKey = await peerId.createFromPubKey(publishedMessage.key)
const pubKeyPeerId = await peerId.createFromPubKey(publishedMessageData.pubKey)
expect(pubKeyPeerId.toB58String()).not.to.equal(messageKey.toB58String())
expect(pubKeyPeerId.toB58String()).to.equal(testAccount.id)
expect(publishedMessage.from).to.equal(idA.id)
expect(messageKey.toB58String()).to.equal(idA.id)
expect(publishedMessageDataValue).to.equal(ipfsRef)
// Verify the signature
await ipns.validate(pubKeyPeerId._pubKey, publishedMessageData)
})
})