This repository has been archived by the owner on Aug 11, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BREAKING CHANGE: The API is now async/await based There are numerous changes, the most significant one is that the API is no longer callback based, but it using async/await. For the full new API please see the [IPLD Formats spec]. [IPLD Formats spec]: https://github.com/ipld/interface-ipld-format
- Loading branch information
Showing
9 changed files
with
306 additions
and
543 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,135 +1,70 @@ | ||
'use strict' | ||
|
||
const util = require('./util') | ||
const traverse = require('traverse') | ||
const CID = require('cids') | ||
|
||
exports = module.exports | ||
|
||
exports.multicodec = 'dag-cbor' | ||
exports.defaultHashAlg = 'sha2-256' | ||
const util = require('./util') | ||
|
||
/* | ||
* resolve: receives a path and a binary blob and returns the value on path, | ||
* throw if not possible. `binaryBlob` is CBOR encoded data. | ||
/** | ||
* Resolves a path within a CBOR block. | ||
* | ||
* Returns the value or a link and the partial mising path. This way the | ||
* IPLD Resolver can fetch the link and continue to resolve. | ||
* | ||
* @param {Buffer} binaryBlob - Binary representation of a CBOR block | ||
* @param {string} [path='/'] - Path that should be resolved | ||
* @returns {Object} result - Result of the path it it was resolved successfully | ||
* @returns {*} result.value - Value the path resolves to | ||
* @returns {string} result.remainderPath - If the path resolves half-way to a | ||
* link, then the `remainderPath` is the part after the link that can be used | ||
* for further resolving | ||
*/ | ||
exports.resolve = (binaryBlob, path, callback) => { | ||
if (typeof path === 'function') { | ||
callback = path | ||
path = undefined | ||
} | ||
|
||
util.deserialize(binaryBlob, (err, node) => { | ||
if (err) { | ||
return callback(err) | ||
exports.resolve = (binaryBlob, path) => { | ||
let node = util.deserialize(binaryBlob) | ||
|
||
const parts = path.split('/').filter(Boolean) | ||
while (parts.length) { | ||
const key = parts.shift() | ||
if (node[key] === undefined) { | ||
throw new Error(`Object has no property '${key}'`) | ||
} | ||
|
||
// root | ||
|
||
if (!path || path === '/') { | ||
return callback(null, { | ||
node = node[key] | ||
if (CID.isCID(node)) { | ||
return { | ||
value: node, | ||
remainderPath: '' | ||
}) | ||
} | ||
|
||
// within scope | ||
|
||
const parts = path.split('/') | ||
const val = traverse(node).get(parts) | ||
|
||
if (val !== undefined) { | ||
return callback(null, { | ||
value: val, | ||
remainderPath: '' | ||
}) | ||
} | ||
|
||
// out of scope | ||
let value | ||
const len = parts.length | ||
|
||
for (let i = 0; i < len; i++) { | ||
const partialPath = parts.shift() | ||
|
||
if (Array.isArray(node) && !Buffer.isBuffer(node)) { | ||
value = node[Number(partialPath)] | ||
} if (node[partialPath]) { | ||
value = node[partialPath] | ||
} else { | ||
// can't traverse more | ||
if (!value) { | ||
return callback(new Error('path not available at root')) | ||
} else { | ||
parts.unshift(partialPath) | ||
return callback(null, { | ||
value: value, | ||
remainderPath: parts.join('/') | ||
}) | ||
} | ||
remainderPath: parts.join('/') | ||
} | ||
node = value | ||
} | ||
}) | ||
} | ||
|
||
function flattenObject (obj, delimiter) { | ||
delimiter = delimiter || '/' | ||
|
||
if (Object.keys(obj).length === 0) { | ||
return [] | ||
} | ||
|
||
return traverse(obj).reduce(function (acc, x) { | ||
if (CID.isCID(x)) { | ||
this.update(undefined) | ||
} | ||
const path = this.path.join(delimiter) | ||
|
||
if (path !== '') { | ||
acc.push({ path: path, value: x }) | ||
} | ||
return acc | ||
}, []) | ||
return { | ||
value: node, | ||
remainderPath: '' | ||
} | ||
} | ||
|
||
/* | ||
* tree: returns a flattened array with paths: values of the project. options | ||
* are option (i.e. nestness) | ||
*/ | ||
exports.tree = (binaryBlob, options, callback) => { | ||
if (typeof options === 'function') { | ||
callback = options | ||
options = undefined | ||
const traverse = function * (node, path) { | ||
// Traverse only objects and arrays | ||
if (Buffer.isBuffer(node) || CID.isCID(node) || typeof node === 'string' || | ||
node === null) { | ||
return | ||
} | ||
for (const item of Object.keys(node)) { | ||
const nextpath = path === undefined ? item : path + '/' + item | ||
yield nextpath | ||
yield * traverse(node[item], nextpath) | ||
} | ||
|
||
options = options || {} | ||
|
||
util.deserialize(binaryBlob, (err, node) => { | ||
if (err) { | ||
return callback(err) | ||
} | ||
const flat = flattenObject(node) | ||
const paths = flat.map((el) => el.path) | ||
|
||
callback(null, paths) | ||
}) | ||
} | ||
|
||
exports.isLink = (binaryBlob, path, callback) => { | ||
exports.resolve(binaryBlob, path, (err, result) => { | ||
if (err) { | ||
return callback(err) | ||
} | ||
|
||
if (result.remainderPath.length > 0) { | ||
return callback(new Error('path out of scope')) | ||
} | ||
/** | ||
* Return all available paths of a block. | ||
* | ||
* @generator | ||
* @param {Buffer} binaryBlob - Binary representation of a CBOR block | ||
* @yields {string} - A single path | ||
*/ | ||
exports.tree = function * (binaryBlob) { | ||
const node = util.deserialize(binaryBlob) | ||
|
||
if (CID.isCID(result.value)) { | ||
callback(null, result.value) | ||
} else { | ||
callback(null, false) | ||
} | ||
}) | ||
yield * traverse(node) | ||
} |
Oops, something went wrong.