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

Commit b633be9

Browse files
docs(api): first pass
1 parent d98d376 commit b633be9

File tree

6 files changed

+109
-127
lines changed

6 files changed

+109
-127
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,5 @@ build
3131
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git
3232
node_modules
3333

34-
lib
3534
dist
35+
docs

.npmignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,5 @@ build
3131
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git
3232
node_modules
3333

34-
test
34+
test
35+
docs

README.md

Lines changed: 0 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,6 @@ This is the [multihash](//github.com/multiformats/multihash) implementation in N
2323
- [Gotchas](#gotchas)
2424
- [Usage](#usage)
2525
- [API](#api)
26-
- [`decode(buf)`](#decodebuf)
27-
- [`encode(digest, hashfn[, length])`](#encodedigest-hashfn-length)
28-
- [`toHexString(multihash)`](#tohexstringmultihash)
29-
- [`fromHexString(string)`](#fromhexstringstring)
30-
- [`toB58String(multihash)`](#tob58stringmultihash)
31-
- [`fromB58String(string)`](#fromb58stringstring)
32-
- [`coerceCode(name)`](#coercecodename)
33-
- [`isAppCode(code)`](#isappcodecode)
34-
- [`isValidCode(code)`](#isvalidcodecode)
35-
- [`validate(multihash)`](#validatemultihash)
3626
- [Maintainers](#maintainers)
3727
- [Contribute](#contribute)
3828
- [License](#license)
@@ -87,96 +77,6 @@ You will need to use Node.js `Buffer` API compatible, if you are running inside
8777

8878
## API
8979

90-
### `decode(buf)`
91-
92-
- `buf: Buffer`
93-
94-
Decode a hash from the given multihash.
95-
96-
Returns an object of the form,
97-
98-
```js
99-
{
100-
code: Number,
101-
name: String,
102-
length: Number,
103-
digest: Buffer
104-
}
105-
```
106-
107-
### `encode(digest, hashfn[, length])`
108-
109-
- `digest: Buffer`
110-
- `hashfn: String|Number`
111-
- `length: Number` (optional)
112-
113-
Encode a hash digest along with the specified function code.
114-
115-
> Note: the length is derived from the length of the digest itself.
116-
117-
Returns a buffer.
118-
119-
### `toHexString(multihash)`
120-
121-
- `multihash: Buffer`
122-
123-
Convert the given multihash to a hex encoded string.
124-
125-
Returns a string.
126-
127-
### `fromHexString(string)`
128-
129-
- `string: String`
130-
131-
Convert the given hex encoded string to a multihash (a `Buffer`).
132-
133-
Returns a buffer.
134-
135-
### `toB58String(multihash)`
136-
137-
- `multihash: Buffer`
138-
139-
Convert the given multihash to a base58 encoded string.
140-
141-
Returns a string.
142-
143-
### `fromB58String(string)`
144-
145-
- `string: String`
146-
147-
Convert the given base58 encoded string to a multihash (a `Buffer`).
148-
149-
Returns a buffer.
150-
151-
### `coerceCode(name)`
152-
153-
- `name: String|Number`
154-
155-
Converts a given hash function into the matching code. If passed a number it will return the number if it's a valid code.
156-
157-
Returns a number.
158-
159-
### `isAppCode(code)`
160-
161-
- `code: Number`
162-
163-
Checks wether a code is part of the app range.
164-
165-
Returns a boolean.
166-
167-
### `isValidCode(code)`
168-
169-
- `code: Number`
170-
171-
Checks whether a multihash code is valid.
172-
173-
Returns a boolean.
174-
175-
### `validate(multihash)`
176-
177-
- `multihash: Buffer`
178-
179-
Check if the given buffer is a valid multihash. Throws an error if it is not valid.
18080

18181
## Maintainers
18282

example.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
'use strict'
2+
3+
const multihash = require('multihashes')
4+
const buf = new Buffer('0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33', 'hex')
5+
6+
const encoded = multihash.encode(buf, 'sha1')
7+
console.log(encoded)
8+
// => <Buffer 11 14 0b ee c7 b5 ea 3f 0f db c9 5d 0d d4 7f 3c 5b c2 75 da 8a 33>
9+
10+
multihash.decode(encoded)
11+
// => {
12+
// code: 17,
13+
// name: 'sha1',
14+
// length: 20,
15+
// digest: <Buffer 0b ee c7 b5 ea 3f 0f db c9 5d 0d d4 7f 3c 5b c2 75 da 8a 33>
16+
// }

package.json

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@
99
"test:browser": "aegir-test browser",
1010
"build": "aegir-build",
1111
"test": "aegir-test",
12-
"release": "aegir-release",
13-
"release-minor": "aegir-release --type minor",
14-
"release-major": "aegir-release --type major",
12+
"docs": "aegir-docs",
13+
"release": "aegir-release --docs",
14+
"release-minor": "aegir-release --type minor --docs",
15+
"release-major": "aegir-release --type major --docs",
1516
"coverage": "aegir-coverage",
1617
"coverage-publish": "aegir-coverage publish"
1718
},
@@ -37,13 +38,13 @@
3738
"url": "https://github.com/multiformats/js-multihash/issues"
3839
},
3940
"dependencies": {
40-
"bs58": "^3.0.0"
41+
"bs58": "^3.1.0"
4142
},
4243
"devDependencies": {
43-
"aegir": "^9.1.2",
44+
"aegir": "^9.3.0",
4445
"buffer-equal": "1.0.0",
4546
"chai": "^3.5.0",
46-
"pre-commit": "^1.1.2"
47+
"pre-commit": "^1.2.2"
4748
},
4849
"contributors": [
4950
"David Dias <[email protected]>",
@@ -56,4 +57,4 @@
5657
"nginnever <[email protected]>",
5758
"npm-to-cdn-bot (by Forbes Lindesay) <[email protected]>"
5859
]
59-
}
60+
}

src/index.js

Lines changed: 82 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,73 @@
1+
/**
2+
* Multihash implementation in JavaScript.
3+
*
4+
* @module multihash
5+
*/
16
'use strict'
27

38
const bs58 = require('bs58')
49

510
const cs = require('./constants')
611

7-
exports.toHexString = function toHexString (m) {
8-
if (!Buffer.isBuffer(m)) {
12+
/**
13+
* Convert the given multihash to a hex encoded string.
14+
*
15+
* @param {Buffer} hash
16+
* @returns {string}
17+
*/
18+
exports.toHexString = function toHexString (hash) {
19+
if (!Buffer.isBuffer(hash)) {
920
throw new Error('must be passed a buffer')
1021
}
1122

12-
return m.toString('hex')
23+
return hash.toString('hex')
1324
}
1425

15-
exports.fromHexString = function fromHexString (s) {
16-
return new Buffer(s, 'hex')
26+
/**
27+
* Convert the given hex encoded string to a multihash.
28+
*
29+
* @param {string} hash
30+
* @returns {Buffer}
31+
*/
32+
exports.fromHexString = function fromHexString (hash) {
33+
return new Buffer(hash, 'hex')
1734
}
1835

19-
exports.toB58String = function toB58String (m) {
20-
if (!Buffer.isBuffer(m)) {
36+
/**
37+
* Convert the given multihash to a base58 encoded string.
38+
*
39+
* @param {Buffer} hash
40+
* @returns {string}
41+
*/
42+
exports.toB58String = function toB58String (hash) {
43+
if (!Buffer.isBuffer(hash)) {
2144
throw new Error('must be passed a buffer')
2245
}
2346

24-
return bs58.encode(m)
47+
return bs58.encode(hash)
2548
}
2649

27-
exports.fromB58String = function fromB58String (s) {
28-
let encoded = s
29-
if (Buffer.isBuffer(s)) {
30-
encoded = s.toString()
50+
/**
51+
* Convert the given base58 encoded string to a multihash.
52+
*
53+
* @param {string|Buffer} hash
54+
* @returns {Buffer}
55+
*/
56+
exports.fromB58String = function fromB58String (hash) {
57+
let encoded = hash
58+
if (Buffer.isBuffer(hash)) {
59+
encoded = hash.toString()
3160
}
3261

3362
return new Buffer(bs58.decode(encoded))
3463
}
3564

36-
// Decode a hash from the given Multihash.
65+
/**
66+
* Decode a hash from the given multihash.
67+
*
68+
* @param {Buffer} buf
69+
* @returns {{code: number, name: string, length: number, digest: Buffer}} result
70+
*/
3771
exports.decode = function decode (buf) {
3872
exports.validate(buf)
3973

@@ -47,8 +81,16 @@ exports.decode = function decode (buf) {
4781
}
4882
}
4983

50-
// Encode a hash digest along with the specified function code.
51-
// Note: the length is derived from the length of the digest itself.
84+
/**
85+
* Encode a hash digest along with the specified function code.
86+
*
87+
* > **Note:** the length is derived from the length of the digest itself.
88+
*
89+
* @param {Buffer} digest
90+
* @param {string|number} code
91+
* @param {number} [length]
92+
* @returns {Buffer}
93+
*/
5294
exports.encode = function encode (digest, code, length) {
5395
if (!digest || !code) {
5496
throw new Error('multihash encode requires at least two args: digest, code')
@@ -76,7 +118,12 @@ exports.encode = function encode (digest, code, length) {
76118
return Buffer.concat([new Buffer([hashfn, length]), digest])
77119
}
78120

79-
// Converts a hashfn name into the matching code
121+
/**
122+
* Converts a hash function name into the matching code.
123+
* If passed a number it will return the number if it's a valid code.
124+
* @param {string|number} name
125+
* @returns {number}
126+
*/
80127
exports.coerceCode = function coerceCode (name) {
81128
let code = name
82129

@@ -98,12 +145,22 @@ exports.coerceCode = function coerceCode (name) {
98145
return code
99146
}
100147

101-
// Checks wether a code is part of the app range
148+
/**
149+
* Checks wether a code is part of the app range
150+
*
151+
* @param {number} code
152+
* @returns {boolean}
153+
*/
102154
exports.isAppCode = function appCode (code) {
103155
return code > 0 && code < 0x10
104156
}
105157

106-
// Checks whether a multihash code is valid.
158+
/**
159+
* Checks whether a multihash code is valid.
160+
*
161+
* @param {number} code
162+
* @returns {boolean}
163+
*/
107164
exports.isValidCode = function validCode (code) {
108165
if (exports.isAppCode(code)) {
109166
return true
@@ -116,6 +173,13 @@ exports.isValidCode = function validCode (code) {
116173
return false
117174
}
118175

176+
/**
177+
* Check if the given buffer is a valid multihash. Throws an error if it is not valid.
178+
*
179+
* @param {Buffer} multihash
180+
* @returns {undefined}
181+
* @throws {Error}
182+
*/
119183
exports.validate = function validate (multihash) {
120184
if (!(Buffer.isBuffer(multihash))) {
121185
throw new Error('multihash must be a Buffer')

0 commit comments

Comments
 (0)