From 8739d2dbb3aaffe38e9bd9b27f4bdc2204a009ff Mon Sep 17 00:00:00 2001 From: Friedel Ziegelmayer Date: Wed, 7 Dec 2016 12:53:37 +0100 Subject: [PATCH 1/4] docs(api): first pass --- .gitignore | 2 +- .npmignore | 1 + .travis.yml | 23 +++++-- package.json | 5 +- src/{Base.js => base.js} | 0 src/constants.js | 3 +- src/index.js | 140 ++++++++++++++++++++++++++++++++++++++- src/multibase.js | 94 -------------------------- 8 files changed, 162 insertions(+), 106 deletions(-) rename src/{Base.js => base.js} (100%) delete mode 100644 src/multibase.js 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/package.json b/package.json index 04b61c6..f28a0fc 100644 --- a/package.json +++ b/package.json @@ -2,8 +2,7 @@ "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", @@ -33,7 +32,7 @@ "formats" ], "devDependencies": { - "aegir": "^6.0.1", + "aegir": "^9.1.2", "chai": "^3.5.0", "pre-commit": "^1.1.3" }, 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..3f110ed 100644 --- a/src/index.js +++ b/src/index.js @@ -1,3 +1,141 @@ +/** + * 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()) +} + +/** + * @param {string|number} nameOrCode + * @returns {Base} + * @private + */ +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 -} From 9b6bffddcced5b0d4d09f53912708f0de5b383ff Mon Sep 17 00:00:00 2001 From: Friedel Ziegelmayer Date: Mon, 12 Dec 2016 11:57:06 +0100 Subject: [PATCH 2/4] add aegir tasks --- package.json | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/package.json b/package.json index f28a0fc..6febb8d 100644 --- a/package.json +++ b/package.json @@ -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" }, @@ -32,9 +33,9 @@ "formats" ], "devDependencies": { - "aegir": "^9.1.2", + "aegir": "^9.2.2", "chai": "^3.5.0", - "pre-commit": "^1.1.3" + "pre-commit": "^1.2.1" }, "author": "David Dias ", "license": "MIT", @@ -43,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 ", From 249e919e9c7637c6411a029edd133cdb07c0ec82 Mon Sep 17 00:00:00 2001 From: Friedel Ziegelmayer Date: Mon, 12 Dec 2016 15:38:18 +0100 Subject: [PATCH 3/4] add example.js --- example.js | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 example.js 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 From 881a389a0b36530e999ec2a94de3ece216d86c9d Mon Sep 17 00:00:00 2001 From: Friedel Ziegelmayer Date: Wed, 14 Dec 2016 14:35:43 +0100 Subject: [PATCH 4/4] cleanup --- package.json | 4 ++-- src/index.js | 5 ----- 2 files changed, 2 insertions(+), 7 deletions(-) diff --git a/package.json b/package.json index 6febb8d..5b8e1fd 100644 --- a/package.json +++ b/package.json @@ -33,9 +33,9 @@ "formats" ], "devDependencies": { - "aegir": "^9.2.2", + "aegir": "^9.3.0", "chai": "^3.5.0", - "pre-commit": "^1.2.1" + "pre-commit": "^1.2.2" }, "author": "David Dias ", "license": "MIT", diff --git a/src/index.js b/src/index.js index 3f110ed..d43f330 100644 --- a/src/index.js +++ b/src/index.js @@ -117,11 +117,6 @@ function validEncode (name, buf) { base.decode(buf.toString()) } -/** - * @param {string|number} nameOrCode - * @returns {Base} - * @private - */ function getBase (nameOrCode) { let base