This repository has been archived by the owner on Aug 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 57
Awesome API Documentation #23
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
|
@@ -9,9 +9,10 @@ | |
"test:browser": "aegir-test browser", | ||
"build": "aegir-build", | ||
"test": "aegir-test", | ||
"release": "aegir-release", | ||
"release-minor": "aegir-release --type minor", | ||
"release-major": "aegir-release --type major", | ||
"docs": "aegir-docs", | ||
"release": "aegir-release --docs", | ||
"release-minor": "aegir-release --type minor --docs", | ||
"release-major": "aegir-release --type major --docs", | ||
"coverage": "aegir-coverage", | ||
"coverage-publish": "aegir-coverage publish" | ||
}, | ||
|
@@ -37,13 +38,13 @@ | |
"url": "https://github.com/multiformats/js-multihash/issues" | ||
}, | ||
"dependencies": { | ||
"bs58": "^3.0.0" | ||
"bs58": "^3.1.0" | ||
}, | ||
"devDependencies": { | ||
"aegir": "^9.1.2", | ||
"aegir": "^9.2.2", | ||
"buffer-equal": "1.0.0", | ||
"chai": "^3.5.0", | ||
"pre-commit": "^1.1.2" | ||
"pre-commit": "^1.2.1" | ||
}, | ||
"contributors": [ | ||
"David Dias <[email protected]>", | ||
|
@@ -56,4 +57,4 @@ | |
"nginnever <[email protected]>", | ||
"npm-to-cdn-bot (by Forbes Lindesay) <[email protected]>" | ||
] | ||
} | ||
} |
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,9 +1,37 @@ | ||
/** | ||
* Multihash implementation in JavaScript. | ||
* | ||
* @module multihash | ||
* @example | ||
* const multihash = require('multihashes') | ||
* const buf = new Buffer('0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33', 'hex') | ||
* | ||
* const encoded = multihash.encode(buf, 'sha1') | ||
* console.log(encoded) | ||
* // => <Buffer 11 14 0b ee c7 b5 ea 3f 0f db c9 5d 0d d4 7f 3c 5b c2 75 da 8a 33> | ||
* | ||
* const decoded = multihash.decode(encoded) | ||
* console.log(decoded) | ||
* // => { | ||
* // code: 17, | ||
* // name: 'sha1', | ||
* // length: 20, | ||
* // digest: <Buffer 0b ee c7 b5 ea 3f 0f db c9 5d 0d d4 7f 3c 5b c2 75 da 8a 33> | ||
* // } | ||
* | ||
*/ | ||
'use strict' | ||
|
||
const bs58 = require('bs58') | ||
|
||
const cs = require('./constants') | ||
|
||
/** | ||
* Convert the given multihash to a hex encoded string. | ||
* | ||
* @param {Buffer} m | ||
* @returns {string} | ||
*/ | ||
exports.toHexString = function toHexString (m) { | ||
if (!Buffer.isBuffer(m)) { | ||
throw new Error('must be passed a buffer') | ||
|
@@ -12,10 +40,22 @@ exports.toHexString = function toHexString (m) { | |
return m.toString('hex') | ||
} | ||
|
||
/** | ||
* Convert the given hex encoded string to a multihash. | ||
* | ||
* @param {string} s | ||
* @returns {Buffer} | ||
*/ | ||
exports.fromHexString = function fromHexString (s) { | ||
return new Buffer(s, 'hex') | ||
} | ||
|
||
/** | ||
* Convert the given multihash to a base58 encoded string. | ||
* | ||
* @param {Buffer} m | ||
* @returns {string} | ||
*/ | ||
exports.toB58String = function toB58String (m) { | ||
if (!Buffer.isBuffer(m)) { | ||
throw new Error('must be passed a buffer') | ||
|
@@ -24,6 +64,12 @@ exports.toB58String = function toB58String (m) { | |
return bs58.encode(m) | ||
} | ||
|
||
/** | ||
* Convert the given base58 encoded string to a multihash. | ||
* | ||
* @param {string} s | ||
* @returns {Buffer} | ||
*/ | ||
exports.fromB58String = function fromB58String (s) { | ||
let encoded = s | ||
if (Buffer.isBuffer(s)) { | ||
|
@@ -33,7 +79,16 @@ exports.fromB58String = function fromB58String (s) { | |
return new Buffer(bs58.decode(encoded)) | ||
} | ||
|
||
// Decode a hash from the given Multihash. | ||
/** | ||
* Decode a hash from the given multihash. | ||
* | ||
* @param {Buffer} buf | ||
* @returns {Object} result | ||
* @returns {number} result.code | ||
* @returns {string} result.name | ||
* @returns {number} result.length | ||
* @returns {Buffer} result.digest | ||
*/ | ||
exports.decode = function decode (buf) { | ||
exports.validate(buf) | ||
|
||
|
@@ -47,8 +102,16 @@ exports.decode = function decode (buf) { | |
} | ||
} | ||
|
||
// Encode a hash digest along with the specified function code. | ||
// Note: the length is derived from the length of the digest itself. | ||
/** | ||
* Encode a hash digest along with the specified function code. | ||
* | ||
* > **Note:** the length is derived from the length of the digest itself. | ||
* | ||
* @param {Buffer} digest | ||
* @param {string|number} code | ||
* @param {number} [length] | ||
* @returns {Buffer} | ||
*/ | ||
exports.encode = function encode (digest, code, length) { | ||
if (!digest || !code) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Guess you can't use code |
||
throw new Error('multihash encode requires at least two args: digest, code') | ||
|
@@ -76,7 +139,12 @@ exports.encode = function encode (digest, code, length) { | |
return Buffer.concat([new Buffer([hashfn, length]), digest]) | ||
} | ||
|
||
// Converts a hashfn name into the matching code | ||
/** | ||
* Converts a hash function name into the matching code. | ||
* If passed a number it will return the number if it's a valid code. | ||
* @param {string|number} name | ||
* @returns {number} | ||
*/ | ||
exports.coerceCode = function coerceCode (name) { | ||
let code = name | ||
|
||
|
@@ -98,12 +166,22 @@ exports.coerceCode = function coerceCode (name) { | |
return code | ||
} | ||
|
||
// Checks wether a code is part of the app range | ||
/** | ||
* Checks wether a code is part of the app range | ||
* | ||
* @param {number} code | ||
* @returns {boolean} | ||
*/ | ||
exports.isAppCode = function appCode (code) { | ||
return code > 0 && code < 0x10 | ||
} | ||
|
||
// Checks whether a multihash code is valid. | ||
/** | ||
* Checks whether a multihash code is valid. | ||
* | ||
* @param {number} code | ||
* @returns {boolean} | ||
*/ | ||
exports.isValidCode = function validCode (code) { | ||
if (exports.isAppCode(code)) { | ||
return true | ||
|
@@ -116,6 +194,13 @@ exports.isValidCode = function validCode (code) { | |
return false | ||
} | ||
|
||
/** | ||
* Check if the given buffer is a valid multihash. Throws an error if it is not valid. | ||
* | ||
* @param {Buffer} multihash | ||
* @returns {undefined} | ||
* @throws {Error} | ||
*/ | ||
exports.validate = function validate (multihash) { | ||
if (!(Buffer.isBuffer(multihash))) { | ||
throw new Error('multihash must be a Buffer') | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should probably use the simple syntax as described in multiformats/js-multiaddr#31 (comment)