Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: validator select should return if no callback #15

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,11 @@ const ipns = require('ipns')
const validator = ipns.validator
```

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

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).

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`.
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.

## API

Expand Down
8 changes: 7 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,13 @@ const validator = {
const entryA = unmarshal(dataA)
const entryB = unmarshal(dataB)

callback(null, entryA.sequence > entryB.sequence ? 0 : 1)
const index = entryA.sequence > entryB.sequence ? 0 : 1

if (typeof callback !== 'function') {
return index
}

callback(null, index)
}
}

Expand Down
21 changes: 21 additions & 0 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,27 @@ describe('ipns', function () {
})
})

it('should use validator.select to select the first record because it is newer without using callback', (done) => {
const sequence = 0
const validity = 1000000

ipns.create(rsa, cid, sequence, validity, (err, entry) => {
expect(err).to.not.exist()

ipns.create(rsa, cid, (sequence + 1), validity, (err, newEntry) => {
expect(err).to.not.exist()

const marshalledData = ipns.marshal(entry)
const marshalledNewData = ipns.marshal(newEntry)

const valid = ipns.validator.select(marshalledNewData, marshalledData)

expect(valid).to.equal(0) // new data is the selected one
done()
})
})
})

it('should use validator.select to select the second record because it is newer', (done) => {
const sequence = 0
const validity = 1000000
Expand Down