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

Awesome API Documentation #23

Merged
merged 3 commits into from
Dec 14, 2016
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@ build
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git
node_modules

lib
dist
docs
3 changes: 2 additions & 1 deletion .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,5 @@ build
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git
node_modules

test
test
docs
100 changes: 0 additions & 100 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,6 @@ This is the [multihash](//github.com/multiformats/multihash) implementation in N
- [Gotchas](#gotchas)
- [Usage](#usage)
- [API](#api)
- [`decode(buf)`](#decodebuf)
- [`encode(digest, hashfn[, length])`](#encodedigest-hashfn-length)
- [`toHexString(multihash)`](#tohexstringmultihash)
- [`fromHexString(string)`](#fromhexstringstring)
- [`toB58String(multihash)`](#tob58stringmultihash)
- [`fromB58String(string)`](#fromb58stringstring)
- [`coerceCode(name)`](#coercecodename)
- [`isAppCode(code)`](#isappcodecode)
- [`isValidCode(code)`](#isvalidcodecode)
- [`validate(multihash)`](#validatemultihash)
- [Maintainers](#maintainers)
- [Contribute](#contribute)
- [License](#license)
Expand Down Expand Up @@ -87,96 +77,6 @@ You will need to use Node.js `Buffer` API compatible, if you are running inside

## API

### `decode(buf)`

- `buf: Buffer`

Decode a hash from the given multihash.

Returns an object of the form,

```js
{
code: Number,
name: String,
length: Number,
digest: Buffer
}
```

### `encode(digest, hashfn[, length])`

- `digest: Buffer`
- `hashfn: String|Number`
- `length: Number` (optional)

Encode a hash digest along with the specified function code.

> Note: the length is derived from the length of the digest itself.

Returns a buffer.

### `toHexString(multihash)`

- `multihash: Buffer`

Convert the given multihash to a hex encoded string.

Returns a string.

### `fromHexString(string)`

- `string: String`

Convert the given hex encoded string to a multihash (a `Buffer`).

Returns a buffer.

### `toB58String(multihash)`

- `multihash: Buffer`

Convert the given multihash to a base58 encoded string.

Returns a string.

### `fromB58String(string)`

- `string: String`

Convert the given base58 encoded string to a multihash (a `Buffer`).

Returns a buffer.

### `coerceCode(name)`

- `name: String|Number`

Converts a given hash function into the matching code. If passed a number it will return the number if it's a valid code.

Returns a number.

### `isAppCode(code)`

- `code: Number`

Checks wether a code is part of the app range.

Returns a boolean.

### `isValidCode(code)`

- `code: Number`

Checks whether a multihash code is valid.

Returns a boolean.

### `validate(multihash)`

- `multihash: Buffer`

Check if the given buffer is a valid multihash. Throws an error if it is not valid.

## Maintainers

Expand Down
15 changes: 8 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand All @@ -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]>",
Expand All @@ -56,4 +57,4 @@
"nginnever <[email protected]>",
"npm-to-cdn-bot (by Forbes Lindesay) <[email protected]>"
]
}
}
97 changes: 91 additions & 6 deletions src/index.js
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')
Expand All @@ -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')
Expand All @@ -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)) {
Expand All @@ -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
Copy link
Member

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)

* @returns {number} result.code
* @returns {string} result.name
* @returns {number} result.length
* @returns {Buffer} result.digest
*/
exports.decode = function decode (buf) {
exports.validate(buf)

Expand All @@ -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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Guess you can't use code 0 here?

throw new Error('multihash encode requires at least two args: digest, code')
Expand Down Expand Up @@ -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

Expand All @@ -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
Expand All @@ -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')
Expand Down