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

Awesome API Documentation #9

Merged
merged 4 commits into from
Dec 14, 2016
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,4 @@ build
node_modules

test
docs
23 changes: 17 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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
9 changes: 9 additions & 0 deletions example.js
Original file line number Diff line number Diff line change
@@ -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
16 changes: 8 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand All @@ -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 <daviddias@ipfs.io>",
"license": "MIT",
Expand All @@ -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 <daviddias.p@gmail.com>",
Expand Down
File renamed without changes.
3 changes: 2 additions & 1 deletion src/constants.js
Original file line number Diff line number Diff line change
@@ -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'],
Expand Down
135 changes: 134 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -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: <name>, data: <buffer> }
*
* 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
}
94 changes: 0 additions & 94 deletions src/multibase.js

This file was deleted.