Skip to content

Commit 0845877

Browse files
authored
fix: validator select should return if no callback (#15)
1 parent 1df8f62 commit 0845877

File tree

3 files changed

+30
-3
lines changed

3 files changed

+30
-3
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,11 +120,11 @@ const ipns = require('ipns')
120120
const validator = ipns.validator
121121
```
122122

123-
Contains an object with `validate (marshalledData, key, callback)` and `select (dataA, dataB, callback)` functions.
123+
Contains an object with `validate (marshalledData, key, callback)` and `select (dataA, dataB, [callback])` functions.
124124

125125
The `validate` function aims to verify if an IPNS record is valid. First the record is unmarshalled, then the public key is obtained and finally the record is validated (signature and validity are verified).
126126

127-
The `select` function is responsible for deciding which ipns record is the best (newer) between two records. Both records are unmarshalled and their sequence numbers are compared. If the first record provided is the newer, the operation result will be `0`, otherwise the operation result will be `1`.
127+
The `select` function is responsible for deciding which ipns record is the best (newer) between two records. Both records are unmarshalled and their sequence numbers are compared. If the first record provided is the newer, the operation result will be `0`, otherwise the operation result will be `1`. If a callback is not provided, the response is returned.
128128

129129
## API
130130

src/index.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,13 @@ const validator = {
311311
const entryA = unmarshal(dataA)
312312
const entryB = unmarshal(dataB)
313313

314-
callback(null, entryA.sequence > entryB.sequence ? 0 : 1)
314+
const index = entryA.sequence > entryB.sequence ? 0 : 1
315+
316+
if (typeof callback !== 'function') {
317+
return index
318+
}
319+
320+
callback(null, index)
315321
}
316322
}
317323

test/index.spec.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,27 @@ describe('ipns', function () {
323323
})
324324
})
325325

326+
it('should use validator.select to select the first record because it is newer without using callback', (done) => {
327+
const sequence = 0
328+
const validity = 1000000
329+
330+
ipns.create(rsa, cid, sequence, validity, (err, entry) => {
331+
expect(err).to.not.exist()
332+
333+
ipns.create(rsa, cid, (sequence + 1), validity, (err, newEntry) => {
334+
expect(err).to.not.exist()
335+
336+
const marshalledData = ipns.marshal(entry)
337+
const marshalledNewData = ipns.marshal(newEntry)
338+
339+
const valid = ipns.validator.select(marshalledNewData, marshalledData)
340+
341+
expect(valid).to.equal(0) // new data is the selected one
342+
done()
343+
})
344+
})
345+
})
346+
326347
it('should use validator.select to select the second record because it is newer', (done) => {
327348
const sequence = 0
328349
const validity = 1000000

0 commit comments

Comments
 (0)