Skip to content
This repository was archived by the owner on Feb 12, 2024. It is now read-only.

Commit 7f07aa2

Browse files
committed
fix: remove libp2p-record for pubsub
1 parent 664f595 commit 7f07aa2

File tree

7 files changed

+39
-31
lines changed

7 files changed

+39
-31
lines changed

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@
9090
"cids": "~0.5.5",
9191
"class-is": "^1.1.0",
9292
"datastore-core": "~0.6.0",
93-
"datastore-pubsub": "ipfs/js-datastore-pubsub#feat/encode-record-store-keys",
93+
"datastore-pubsub": "~0.1.1",
9494
"debug": "^4.1.0",
9595
"deep-extend": "~0.6.0",
9696
"err-code": "^1.1.2",
@@ -120,7 +120,7 @@
120120
"ipld-ethereum": "^2.0.1",
121121
"ipld-git": "~0.2.2",
122122
"ipld-zcash": "~0.1.6",
123-
"ipns": "~0.3.0",
123+
"ipns": "~0.4.2",
124124
"is-ipfs": "~0.4.7",
125125
"is-pull-stream": "~0.0.0",
126126
"is-stream": "^1.1.0",

src/core/components/pubsub.js

+8-6
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ const errPubsubDisabled = () => {
88
return errCode(new Error('pubsub experiment is not enabled'), 'ERR_PUBSUB_DISABLED')
99
}
1010

11+
const pubsubEnabled = (options) => options.EXPERIMENTAL.pubsub || options.EXPERIMENTAL.ipnsPubsub
12+
1113
module.exports = function pubsub (self) {
1214
return {
1315
subscribe: (topic, handler, options, callback) => {
@@ -16,7 +18,7 @@ module.exports = function pubsub (self) {
1618
options = {}
1719
}
1820

19-
if (!self._options.EXPERIMENTAL.pubsub) {
21+
if (!pubsubEnabled(self._options)) {
2022
return callback
2123
? setImmediate(() => callback(errPubsubDisabled()))
2224
: Promise.reject(errPubsubDisabled())
@@ -37,7 +39,7 @@ module.exports = function pubsub (self) {
3739
},
3840

3941
unsubscribe: (topic, handler, callback) => {
40-
if (!self._options.EXPERIMENTAL.pubsub) {
42+
if (!pubsubEnabled(self._options)) {
4143
return callback
4244
? setImmediate(() => callback(errPubsubDisabled()))
4345
: Promise.reject(errPubsubDisabled())
@@ -53,28 +55,28 @@ module.exports = function pubsub (self) {
5355
},
5456

5557
publish: promisify((topic, data, callback) => {
56-
if (!self._options.EXPERIMENTAL.pubsub) {
58+
if (!pubsubEnabled(self._options)) {
5759
return setImmediate(() => callback(errPubsubDisabled()))
5860
}
5961
self._libp2pNode.pubsub.publish(topic, data, callback)
6062
}),
6163

6264
ls: promisify((callback) => {
63-
if (!self._options.EXPERIMENTAL.pubsub) {
65+
if (!pubsubEnabled(self._options)) {
6466
return setImmediate(() => callback(errPubsubDisabled()))
6567
}
6668
self._libp2pNode.pubsub.ls(callback)
6769
}),
6870

6971
peers: promisify((topic, callback) => {
70-
if (!self._options.EXPERIMENTAL.pubsub) {
72+
if (!pubsubEnabled(self._options)) {
7173
return setImmediate(() => callback(errPubsubDisabled()))
7274
}
7375
self._libp2pNode.pubsub.peers(topic, callback)
7476
}),
7577

7678
setMaxListeners (n) {
77-
if (!self._options.EXPERIMENTAL.pubsub) {
79+
if (!pubsubEnabled(self._options)) {
7880
throw errPubsubDisabled()
7981
}
8082
self._libp2pNode.pubsub.setMaxListeners(n)

src/core/ipns/publisher.js

+4-16
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use strict'
22

33
const PeerId = require('peer-id')
4-
const Record = require('libp2p-record').Record
54
const { Key } = require('interface-datastore')
65
const series = require('async/series')
76
const errcode = require('err-code')
@@ -97,19 +96,17 @@ class IpnsPublisher {
9796
return callback(errcode(new Error(errMsg), 'ERR_INVALID_DATASTORE_KEY'))
9897
}
9998

100-
let rec
99+
let entryData
101100
try {
102101
// Marshal record
103-
const entryData = ipns.marshal(entry)
104-
// Marshal to libp2p record
105-
rec = new Record(key.toBuffer(), entryData)
102+
entryData = ipns.marshal(entry)
106103
} catch (err) {
107104
log.error(err)
108105
return callback(err)
109106
}
110107

111108
// Add record to routing (buffer key)
112-
this._routing.put(key.toBuffer(), rec.serialize(), (err, res) => {
109+
this._routing.put(key.toBuffer(), entryData, (err, res) => {
113110
if (err) {
114111
const errMsg = `ipns record for ${key.toString()} could not be stored in the routing`
115112

@@ -137,17 +134,8 @@ class IpnsPublisher {
137134
return callback(errcode(new Error(errMsg), 'ERR_UNDEFINED_PARAMETER'))
138135
}
139136

140-
let rec
141-
try {
142-
// Marshal to libp2p record
143-
rec = new Record(key.toBuffer(), publicKey.bytes)
144-
} catch (err) {
145-
log.error(err)
146-
return callback(err)
147-
}
148-
149137
// Add public key to routing (buffer key)
150-
this._routing.put(key.toBuffer(), rec.serialize(), (err, res) => {
138+
this._routing.put(key.toBuffer(), publicKey.bytes, (err, res) => {
151139
if (err) {
152140
const errMsg = `public key for ${key.toString()} could not be stored in the routing`
153141

src/core/ipns/resolver.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use strict'
22

33
const ipns = require('ipns')
4-
const Record = require('libp2p-record').Record
54
const PeerId = require('peer-id')
65
const errcode = require('err-code')
76

@@ -119,8 +118,7 @@ class IpnsResolver {
119118

120119
let ipnsEntry
121120
try {
122-
const record = Record.deserialize(res)
123-
ipnsEntry = ipns.unmarshal(record.value)
121+
ipnsEntry = ipns.unmarshal(res)
124122
} catch (err) {
125123
const errMsg = `found ipns record that we couldn't convert to a value`
126124

src/core/ipns/routing/offline-datastore.js

+21-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict'
22

33
const { Key } = require('interface-datastore')
4+
const Record = require('libp2p-record').Record
45
const { encodeBase32 } = require('./utils')
56

67
const errcode = require('err-code')
@@ -48,7 +49,10 @@ class OfflineDatastore {
4849
return callback(errcode(new Error(errMsg), 'ERR_GENERATING_ROUTING_KEY'))
4950
}
5051

51-
this._repo.datastore.put(routingKey, value, callback)
52+
// Marshal to libp2p record as the DHT does
53+
let record = new Record(key, value)
54+
55+
this._repo.datastore.put(routingKey, record.serialize(), callback)
5256
}
5357

5458
/**
@@ -76,7 +80,22 @@ class OfflineDatastore {
7680
return callback(errcode(new Error(errMsg), 'ERR_GENERATING_ROUTING_KEY'))
7781
}
7882

79-
this._repo.datastore.get(routingKey, callback)
83+
this._repo.datastore.get(routingKey, (err, res) => {
84+
if (err) {
85+
return callback(err)
86+
}
87+
88+
// Unmarshal libp2p record as the DHT does
89+
let record
90+
try {
91+
record = Record.deserialize(res)
92+
} catch (err) {
93+
log.error(err)
94+
return callback(err)
95+
}
96+
97+
callback(null, record.value)
98+
})
8099
}
81100

82101
// encode key properly - base32(/ipns/{cid})

src/core/ipns/routing/pubsub-datastore.js

-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,6 @@ class IpnsPubsubDatastore {
8989
}
9090

9191
// Modify subscription key to have a proper encoding
92-
// Without this, the utf-8 encoding gets the key broken
9392
_handleSubscriptionKey (key, callback) {
9493
const subscriber = this._subscriptions[key]
9594

src/core/ipns/routing/utils.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,7 @@
33
const multibase = require('multibase')
44

55
module.exports.encodeBase32 = (buf) => {
6-
return multibase.encode('base32', buf).slice(1) // slice off multibase codec
6+
const m = multibase.encode('base32', buf).slice(1)
7+
8+
return m.toString().toUpperCase() // slice off multibase codec
79
}

0 commit comments

Comments
 (0)