diff --git a/.gitignore b/.gitignore index 254988d..ff1fec8 100644 --- a/.gitignore +++ b/.gitignore @@ -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 diff --git a/.npmignore b/.npmignore index 59335fd..8495fc5 100644 --- a/.npmignore +++ b/.npmignore @@ -32,3 +32,4 @@ build node_modules test +docs diff --git a/.travis.yml b/.travis.yml index 5c9015c..b78922d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,11 +1,17 @@ sudo: false language: node_js -node_js: - - 4 - - 5 - - stable -# Make sure we have new NPM. +matrix: + include: + - node_js: 4 + env: CXX=g++-4.8 + - node_js: 6 + env: + - SAUCE=true + - CXX=g++-4.8 + - node_js: stable + env: CXX=g++-4.8 + before_install: - npm install -g npm @@ -22,4 +28,9 @@ after_success: - npm run coverage-publish addons: - firefox: 'latest' + firefox: latest + apt: + sources: + - ubuntu-toolchain-r-test + packages: + - g++-4.8 diff --git a/example.js b/example.js new file mode 100644 index 0000000..c0cd870 --- /dev/null +++ b/example.js @@ -0,0 +1,9 @@ +'use strict' + +const multibase = require('multibase') + +const encodedBuf = multibase.encode('base58btc', new Buffer('hey, how is it going')) + +const decodedBuf = multibase.decode(encodedBuf) +console.log(decodedBuf.toString()) +// => hey, how it going diff --git a/package.json b/package.json index 04b61c6..5b8e1fd 100644 --- a/package.json +++ b/package.json @@ -2,17 +2,17 @@ "name": "multibase", "version": "0.2.0", "description": "JavaScript implementation of the multibase specification", - "main": "lib/index.js", - "jsnext:main": "src/index.js", + "main": "src/index.js", "scripts": { "test:node": "aegir-test node", "lint": "aegir-lint", "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" }, @@ -33,9 +33,9 @@ "formats" ], "devDependencies": { - "aegir": "^6.0.1", + "aegir": "^9.3.0", "chai": "^3.5.0", - "pre-commit": "^1.1.3" + "pre-commit": "^1.2.2" }, "author": "David Dias ", "license": "MIT", @@ -44,7 +44,7 @@ }, "homepage": "https://github.com/multiformats/js-multibase#readme", "dependencies": { - "base-x": "1.0.4" + "base-x": "1.1.0" }, "contributors": [ "David Dias ", diff --git a/src/Base.js b/src/base.js similarity index 100% rename from src/Base.js rename to src/base.js diff --git a/src/constants.js b/src/constants.js index 43fce6b..eebc55a 100644 --- a/src/constants.js +++ b/src/constants.js @@ -1,7 +1,8 @@ 'use strict' -const Base = require('./Base.js') +const Base = require('./base.js') const baseX = require('base-x') + // name, code, implementation, alphabet const constants = [ ['base1', '1', '', '1'], diff --git a/src/index.js b/src/index.js index 0b7d4b6..d43f330 100644 --- a/src/index.js +++ b/src/index.js @@ -1,3 +1,136 @@ +/** + * Implementation of the [multibase](https://github.com/multiformats/multibase) specification. + * @module Multibase + */ 'use strict' -exports = module.exports = require('./multibase') +const constants = require('./constants') + +exports = module.exports = multibase +exports.encode = encode +exports.decode = decode +exports.isEncoded = isEncoded + +const errNotSupported = new Error('Unsupported encoding') + +/** + * Create a new buffer with the multibase varint+code. + * + * @param {string|number} nameOrCode - The multibase name or code number. + * @param {Buffer} buf - The data to be prefixed with multibase. + * @memberof Multibase + * @returns {Buffer} + */ +function multibase (nameOrCode, buf) { + if (!buf) { + throw new Error('requires an encoded buffer') + } + const base = getBase(nameOrCode) + const codeBuf = new Buffer(base.code) + + const name = base.name + validEncode(name, buf) + return Buffer.concat([codeBuf, buf]) +} + +/** + * Encode data with the specified base and add the multibase prefix. + * + * @param {string|number} nameOrCode - The multibase name or code number. + * @param {Buffer} buf - The data to be encoded. + * @returns {Buffer} + * @memberof Multibase + */ +function encode (nameOrCode, buf) { + const base = getBase(nameOrCode) + const name = base.name + + return multibase(name, new Buffer(base.encode(buf))) +} + +/** + * + * Takes a buffer or string encoded with multibase header + * decodes it and returns an object with the decoded buffer + * and the encoded type { base: , data: } + * + * from @theobat : This is not what the multibase.spec.js test is waiting for, + * hence the return decodeObject.data + * + * @param {Buffer|string} bufOrString + * @returns {Object} result + * @returns {string} result.base + * @returns {Buffer} result.data + * @memberof Multibase + * + */ +function decode (bufOrString) { + if (Buffer.isBuffer(bufOrString)) { + bufOrString = bufOrString.toString() + } + + const code = bufOrString.substring(0, 1) + bufOrString = bufOrString.substring(1, bufOrString.length) + + if (typeof bufOrString === 'string') { + bufOrString = new Buffer(bufOrString) + } + + const base = getBase(code) + + const decodeObject = { + base: base.name, + data: new Buffer(base.decode(bufOrString.toString())) + } + return decodeObject.data +} + +/** + * Is the given data multibase encoded? + * + * @param {Buffer|string} bufOrString + * @returns {boolean} + * @memberof Multibase + */ +function isEncoded (bufOrString) { + if (Buffer.isBuffer(bufOrString)) { + bufOrString = bufOrString.toString() + } + + const code = bufOrString.substring(0, 1) + try { + const base = getBase(code) + return base.name + } catch (err) { + return false + } +} + +/** + * @param {string} name + * @param {Buffer} buf + * @private + * @returns {undefined} + */ +function validEncode (name, buf) { + const base = getBase(name) + base.decode(buf.toString()) +} + +function getBase (nameOrCode) { + let base + + if (constants.names[nameOrCode]) { + base = constants.names[nameOrCode] + } else if (constants.codes[nameOrCode]) { + base = constants.codes[nameOrCode] + } else { + throw errNotSupported + } + + if (!base.isImplemented()) { + throw new Error('Base ' + nameOrCode + ' is not implemented yet') + } + + return base +} diff --git a/src/multibase.js b/src/multibase.js deleted file mode 100644 index 59c714d..0000000 --- a/src/multibase.js +++ /dev/null @@ -1,94 +0,0 @@ -'use strict' - -const constants = require('./constants') - -exports = module.exports = multibase -exports.encode = encode -exports.decode = decode -exports.isEncoded = isEncoded - -const errNotSupported = new Error('Unsupported encoding') - -// returns a new buffer with the multibase varint+code` -function multibase (nameOrCode, buf) { - if (!buf) { - throw new Error('requires an encoded buffer') - } - const base = getBase(nameOrCode) - const codeBuf = new Buffer(base.code) - - const name = base.name - validEncode(name, buf) - return Buffer.concat([codeBuf, buf]) -} - -function encode (nameOrCode, buf) { - const base = getBase(nameOrCode) - const name = base.name - - return multibase(name, new Buffer(base.encode(buf))) -} - -// receives a buffer or string encoded with multibase header -// decodes it and returns an object with the decoded buffer -// and the encoded type { base: , data: } - -// from @theobat : This is not what the multibase.spec.js test is waiting for, -// hence the return decodeObject.data -function decode (bufOrString) { - if (Buffer.isBuffer(bufOrString)) { - bufOrString = bufOrString.toString() - } - - const code = bufOrString.substring(0, 1) - bufOrString = bufOrString.substring(1, bufOrString.length) - - if (typeof bufOrString === 'string') { - bufOrString = new Buffer(bufOrString) - } - - const base = getBase(code) - - const decodeObject = { - base: base.name, - data: new Buffer(base.decode(bufOrString.toString())) - } - return decodeObject.data -} - -function isEncoded (bufOrString) { - if (Buffer.isBuffer(bufOrString)) { - bufOrString = bufOrString.toString() - } - - const code = bufOrString.substring(0, 1) - try { - const base = getBase(code) - return base.name - } catch (err) { - return false - } -} - -function validEncode (name, buf) { - const base = getBase(name) - base.decode(buf.toString()) -} - -function getBase (nameOrCode) { - let base - - if (constants.names[nameOrCode]) { - base = constants.names[nameOrCode] - } else if (constants.codes[nameOrCode]) { - base = constants.codes[nameOrCode] - } else { - throw errNotSupported - } - - if (!base.isImplemented()) { - throw new Error('Base ' + nameOrCode + ' is not implemented yet') - } - - return base -}