Skip to content
This repository has been archived by the owner on Aug 11, 2021. It is now read-only.

Commit

Permalink
fix: issue #16 - Throw error on resolving non-root path (#43)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: non-root paths throw an error

Prior to this change a call to the resolver with a path like this:

    resolver.resolve(blob, '/a/b/c/d')

would just return the `blob` and an empty remainder path.

With this commit, it will throw an Error as there is no such path.
  • Loading branch information
chafey authored Feb 27, 2020
1 parent e9eb569 commit a5b53be
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
"greenkeeper[bot] <23040076+greenkeeper[bot]@users.noreply.github.com>",
"greenkeeper[bot] <greenkeeper[bot]@users.noreply.github.com>",
"kumavis <aaron@kumavis.me>",
"kumavis <kumavis@users.noreply.github.com>"
"kumavis <kumavis@users.noreply.github.com>",
"Chris Hafey <chafey@gmail.com>"
],
"license": "MIT",
"bugs": {
Expand Down
6 changes: 5 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,16 @@ module.exports = {
* Always returns the raw data as value without any remainderPath.
*
* @param {Buffer} binaryBlob - Binary representation of a PB block
* @param {string} [path='/'] - Path that should be resolved (that value is ignored)
* @param {string} [path='/'] - Path that should be resolved. Must be '/' or an exception is thrown
* @returns {Object} result - Result of the path it it was resolved successfully
* @returns {*} result.value - The raw data
* @returns {string} result.remainderPath - An empty string
*/
resolve: (binaryBlob, path) => {
if (path !== '/') {
throw new Error('Only the root path / may be resolved')
}

return {
value: binaryBlob,
remainderPath: ''
Expand Down
8 changes: 7 additions & 1 deletion test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,17 @@ describe('raw codec', () => {
})

it('resolver.resolve', () => {
const result = resolver.resolve(testBlob, 'a/b/c/d')
const result = resolver.resolve(testBlob, '/')
expect(result.value.toString('hex')).to.equal(testData.toString('hex'))
expect(result.remainderPath).to.equal('')
})

it('resolver.resolve throws on non-root path', () => {
expect(() => resolver.resolve(testBlob, 'a/b/c/d')).to.throw(
'Only the root path / may be resolved'
)
})

it('resolver.tree', () => {
const paths = resolver.tree(testBlob)
expect(paths.value).to.be.undefined()
Expand Down

0 comments on commit a5b53be

Please sign in to comment.