1
1
'use strict'
2
2
3
- const { Record } = require ( 'libp2p-record' )
4
3
const { Key } = require ( 'interface-datastore' )
5
4
const { encodeBase32 } = require ( './utils' )
6
5
@@ -21,7 +20,7 @@ class DatastorePubsub {
21
20
* @param {Object } validator - validator functions.
22
21
* @param {function(record, peerId, callback) } validator.validate - function to validate a record.
23
22
* @param {function(received, current, callback) } validator.select - function to select the newest between two records.
24
- * @param {function(key, callback) } subscriptionKeyFn - function to manipulate the key topic received before processing it.
23
+ * @param {function(key, callback) } subscriptionKeyFn - optional function to manipulate the key topic received before processing it.
25
24
* @memberof DatastorePubsub
26
25
*/
27
26
constructor ( pubsub , datastore , peerId , validator , subscriptionKeyFn ) {
@@ -208,17 +207,8 @@ class DatastorePubsub {
208
207
209
208
// Verify if the record received through pubsub is valid and better than the one currently stored
210
209
_isBetter ( key , val , callback ) {
211
- let receivedRecord
212
-
213
- try {
214
- receivedRecord = Record . deserialize ( val )
215
- } catch ( err ) {
216
- log . error ( err )
217
- return callback ( err )
218
- }
219
-
220
210
// validate received record
221
- this . _validateRecord ( receivedRecord . value , key , ( err , valid ) => {
211
+ this . _validateRecord ( val , key , ( err , valid ) => {
222
212
// If not valid, it is not better than the one currently available
223
213
if ( err || ! valid ) {
224
214
const errMsg = 'record received through pubsub is not valid'
@@ -230,21 +220,19 @@ class DatastorePubsub {
230
220
// Get Local record
231
221
const dsKey = new Key ( key )
232
222
233
- this . _getLocal ( dsKey . toBuffer ( ) , ( err , res ) => {
223
+ this . _getLocal ( dsKey . toBuffer ( ) , ( err , currentRecord ) => {
234
224
// if the old one is invalid, the new one is *always* better
235
225
if ( err ) {
236
226
return callback ( null , true )
237
227
}
238
228
239
229
// if the same record, do not need to store
240
- if ( res . equals ( val ) ) {
230
+ if ( currentRecord . equals ( val ) ) {
241
231
return callback ( null , false )
242
232
}
243
233
244
- const currentRecord = Record . deserialize ( res )
245
-
246
234
// verify if the received record should replace the current one
247
- this . _selectRecord ( receivedRecord . value , currentRecord . value , callback )
235
+ this . _selectRecord ( val , currentRecord , callback )
248
236
} )
249
237
} )
250
238
}
0 commit comments