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 3
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
3 changed files
with
83 additions
and
89 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,68 @@ | ||
'use strict' | ||
const CID = require('cids') | ||
const multihash = require('multihashing-async') | ||
const multihashing = require('multihashing-async') | ||
const multicodec = require('multicodec') | ||
|
||
// binary resolver | ||
module.exports = { | ||
codec: multicodec.RAW, | ||
defaultHashAlg: multicodec.SHA2_256, | ||
resolver: { | ||
multicodec: 'raw', | ||
defaultHashAlg: 'sha2-256', | ||
resolve: (binaryBlob, path, callback) => { | ||
callback(null, { | ||
/** | ||
* Resolves a path within a Raw block. | ||
* | ||
* 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) | ||
* @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) => { | ||
return { | ||
value: binaryBlob, | ||
remainderPath: '' | ||
}) | ||
} | ||
}, | ||
tree: (binaryBlob, options, callback) => { | ||
if (typeof options === 'function') { | ||
callback = options | ||
/** | ||
* Return all available paths of a block. | ||
* | ||
* @generator | ||
* @param {Buffer} binaryBlob - The raw data | ||
* @returns {Object} - Finished generator with `done: true` | ||
*/ | ||
tree: (binaryBlob) => { | ||
return { | ||
done: true | ||
} | ||
callback(null, []) | ||
} | ||
}, | ||
util: { | ||
deserialize: (data, cb) => { | ||
cb(null, data) | ||
deserialize: (data) => { | ||
return data | ||
}, | ||
serialize: (data, cb) => { | ||
cb(null, data) | ||
serialize: (data) => { | ||
return data | ||
}, | ||
cid: (data, options, cb) => { | ||
if (typeof options === 'function') { | ||
cb = options | ||
options = {} | ||
} | ||
options = options || {} | ||
const hashAlg = options.hashAlg || 'sha2-256' | ||
const version = typeof options.version === 'undefined' ? 1 : options.version | ||
multihash(data, hashAlg, (err, mh) => { | ||
if (err) return cb(err) | ||
cb(null, new CID(version, 'raw', mh)) | ||
}) | ||
/** | ||
* Calculate the CID of the binary blob. | ||
* | ||
* @param {Object} binaryBlob - Encoded IPLD Node | ||
* @param {Object} [userOptions] - Options to create the CID | ||
* @param {number} [userOptions.cidVersion=1] - CID version number | ||
* @param {string} [UserOptions.hashAlg] - Defaults to the defaultHashAlg of the format | ||
* @returns {Promise.<CID>} | ||
*/ | ||
cid: async (binaryBlob, userOptions) => { | ||
const defaultOptions = { cidVersion: 1, hashAlg: module.exports.defaultHashAlg } | ||
const options = Object.assign(defaultOptions, userOptions) | ||
|
||
const multihash = await multihashing(binaryBlob, options.hashAlg) | ||
const codecName = multicodec.print[module.exports.codec] | ||
const cid = new CID(options.cidVersion, codecName, multihash) | ||
|
||
return cid | ||
} | ||
} | ||
} |
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