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

Commit 03c24e1

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

File tree

5 files changed

+14
-27
lines changed

5 files changed

+14
-27
lines changed

package.json

+1-1
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",

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/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

0 commit comments

Comments
 (0)