Skip to content

Commit

Permalink
Merge pull request #58 from interledgerjs/webpack
Browse files Browse the repository at this point in the history
feat: add browser support
  • Loading branch information
vhpoet authored Nov 1, 2016
2 parents ba96980 + ece6555 commit 3422a39
Show file tree
Hide file tree
Showing 12 changed files with 123 additions and 32 deletions.
4 changes: 4 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"presets": ["es2015"],
"plugins": ["babel-plugin-add-module-exports"]
}
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
coverage/
dist/index.js
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ docs/build/
/docs/README.intermediate.md
.tags*
/jsdoc-out/
/dist/
34 changes: 24 additions & 10 deletions karma.conf.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,32 @@
module.exports = function (karma) {
karma.set({

frameworks: [ 'browserify', 'mocha' ],
files: ['test/*Spec.js'],
frameworks: [ 'mocha' ],
files: ['test/index.js'],
preprocessors: {
'test/*Spec.js': [ 'browserify' ]
'test/index.js': [ 'webpack', 'sourcemap' ]
},

webpack: {
devtool: 'inline-source-map',
module: {
loaders: [
{ test: /\.js$/, exclude: /node_modules|dist/, loader: 'babel' },
{ test: /\.json$/, loader: 'json-loader' }
],
noParse: [
/sinon/
]
},
resolve: {
alias: { sinon: 'sinon/pkg/sinon' }
},
node: {
fs: 'empty'
}
},

browserify: {
debug: true,
transform: [
'brfs',
[ 'babelify', { presets: 'es2015', plugins: 'transform-runtime' } ]
]
webpackMiddleware: {
stats: 'errors-only'
},

browsers: [ 'PhantomJS' ]
Expand Down
15 changes: 12 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,25 @@
"version": "3.2.0",
"description": "Implementation of Interledger condition validation and fulfillment",
"main": "index.js",
"browser": "./dist/index.js",
"directories": {
"test": "test"
},
"dependencies": {
"asn1": "^0.2.3",
"core-js": "^2.4.1",
"oer-utils": "^1.2.0",
"tweetnacl": "^0.14.1"
},
"devDependencies": {
"babel-loader": "^6.2.7",
"babel-plugin-add-module-exports": "^0.2.1",
"babel-plugin-transform-runtime": "^6.7.5",
"babel-polyfill": "^6.16.0",
"babel-preset-es2015": "^6.9.0",
"babel-runtime": "^6.9.0",
"babelify": "^7.2.0",
"brfs": "^1.4.3",
"browserify": "^13.0.1",
"chai": "^3.2.0",
"cz-conventional-changelog": "^1.1.5",
"eslint": "^3.1.1",
Expand All @@ -28,20 +32,25 @@
"interledger-jsdoc-template": "^2.0.0",
"istanbul-harmony": "^0.3.16",
"jsdoc": "^3.4.0",
"json-loader": "^0.5.4",
"karma": "^1.1.0",
"karma-browserify": "^5.0.5",
"karma-mocha": "^1.1.0",
"karma-phantomjs-launcher": "^1.0.0",
"karma-sourcemap-loader": "^0.3.7",
"karma-webpack": "^1.8.0",
"md-toc-filter": "^0.9.0",
"mocha": "^3.0.0",
"phantomjs-prebuilt": "^2.1.5",
"readme-to-test": "justmoon/readme-to-test#feature/st-improvements",
"sinon": "^1.17.4",
"validate-commit-msg": "^2.1.0",
"watchify": "^3.7.0"
"watchify": "^3.7.0",
"webpack": "^2.1.0-beta.25"
},
"scripts": {
"lint": "eslint .",
"build:web": "webpack --config webpack.config.js",
"prepublish": "npm run build:web",
"test:readme": "readme-to-test",
"test:specs": "NODE_ENV=unit node node_modules/.bin/istanbul test -- _mocha",
"test:phantom": "karma start --single-run",
Expand Down
3 changes: 2 additions & 1 deletion src/lib/condition.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const MissingDataError = require('../errors/missing-data-error')
const base64url = require('../util/base64url')
const Reader = require('oer-utils/reader')
const Writer = require('oer-utils/writer')
const isInteger = require('core-js/library/fn/number/is-integer')

// Regex for validating conditions
//
Expand Down Expand Up @@ -214,7 +215,7 @@ class Condition {
* @param {Number} Maximum fulfillment payload length in bytes.
*/
setMaxFulfillmentLength (maxFulfillmentLength) {
if (!Number.isInteger(maxFulfillmentLength)) {
if (!isInteger(maxFulfillmentLength)) {
throw new TypeError('Fulfillment length must be an integer')
} else if (maxFulfillmentLength < 0) {
throw new TypeError('Fulfillment length must be positive or zero')
Expand Down
7 changes: 4 additions & 3 deletions src/types/threshold-sha256.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const Predictor = require('oer-utils/predictor')
const Writer = require('oer-utils/writer')
const MissingDataError = require('../errors/missing-data-error')
const ParseError = require('../errors/parse-error')
const isInteger = require('core-js/library/fn/number/is-integer')

const EMPTY_BUFFER = new Buffer(0)

Expand Down Expand Up @@ -71,7 +72,7 @@ class ThresholdSha256 extends BaseSha256 {

if (typeof weight === 'undefined') {
weight = 1
} else if (!Number.isInteger(weight) || weight < 1) {
} else if (!isInteger(weight) || weight < 1) {
throw new TypeError('Invalid weight, not an integer: ' + weight)
}

Expand Down Expand Up @@ -105,7 +106,7 @@ class ThresholdSha256 extends BaseSha256 {

if (typeof weight === 'undefined') {
weight = 1
} else if (!Number.isInteger(weight)) {
} else if (!isInteger(weight)) {
throw new Error('Invalid weight, not an integer: ' + weight)
}

Expand All @@ -127,7 +128,7 @@ class ThresholdSha256 extends BaseSha256 {
* @param {Number} threshold Integer threshold
*/
setThreshold (threshold) {
if (!Number.isInteger(threshold) || threshold < 1) {
if (!isInteger(threshold) || threshold < 1) {
throw new TypeError('Threshold must be a integer greater than zero, was: ' +
threshold)
}
Expand Down
12 changes: 6 additions & 6 deletions test/fulfillmentSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,23 @@ describe('Fulfillment', function () {
it('successfully parses the minimal fulfillment', function () {
const fulfillment = Fulfillment.fromUri('cf:0:')

assert.equal(fulfillment.constructor.name, 'PreimageSha256')
assert.equal(fulfillment.constructor, cc.PreimageSha256)
assert.equal(fulfillment.preimage.toString('hex'), '')
})

it('successfully parses a basic fulfillment', function () {
const fulfillment = Fulfillment.fromUri('cf:0:UNhY4JhezH9gQYqvDMWrWH9CwlcKiECVqejMrND2VFw')

const expectedPreimage = new Buffer('UNhY4JhezH9gQYqvDMWrWH9CwlcKiECVqejMrND2VFw=', 'base64')
assert.equal(fulfillment.constructor.name, 'PreimageSha256')
assert.equal(fulfillment.constructor, cc.PreimageSha256)
assert.equal(fulfillment.preimage.toString('hex'), expectedPreimage.toString('hex'))
})

it('successfully parses a fulfillment with base64url characters', function () {
const fulfillment = Fulfillment.fromUri('cf:0:-u_6')

const expectedPreimage = new Buffer('+u/6', 'base64')
assert.equal(fulfillment.constructor.name, 'PreimageSha256')
assert.equal(fulfillment.constructor, cc.PreimageSha256)
assert.equal(fulfillment.preimage.toString('hex'), expectedPreimage.toString('hex'))
})

Expand Down Expand Up @@ -99,7 +99,7 @@ describe('Fulfillment', function () {
it('successfully parses the minimal fulfillment', function () {
const fulfillment = Fulfillment.fromBinary(new Buffer('000000', 'hex'))

assert.equal(fulfillment.constructor.name, 'PreimageSha256')
assert.equal(fulfillment.constructor, cc.PreimageSha256)
assert.equal(fulfillment.preimage.toString('hex'), '')
})

Expand All @@ -108,15 +108,15 @@ describe('Fulfillment', function () {
const fulfillment = Fulfillment.fromBinary(fulfillmentBinary)

const expectedPreimage = new Buffer('UNhY4JhezH9gQYqvDMWrWH9CwlcKiECVqejMrND2VFw=', 'base64')
assert.equal(fulfillment.constructor.name, 'PreimageSha256')
assert.equal(fulfillment.constructor, cc.PreimageSha256)
assert.equal(fulfillment.preimage.toString('hex'), expectedPreimage.toString('hex'))
})

it('successfully parses a fulfillment with base64url characters', function () {
const fulfillment = Fulfillment.fromBinary(new Buffer('000003faeffa', 'hex'))

const expectedPreimage = new Buffer('+u/6', 'base64')
assert.equal(fulfillment.constructor.name, 'PreimageSha256')
assert.equal(fulfillment.constructor, cc.PreimageSha256)
assert.equal(fulfillment.preimage.toString('hex'), expectedPreimage.toString('hex'))
})

Expand Down
39 changes: 30 additions & 9 deletions test/helpers/salt.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,34 @@
const crypto = require('crypto')
const sinon = require('sinon')

exports.getSaltHelper = function (salt) {
const stub = sinon.stub(crypto, 'randomBytes')
stub.withArgs(salt.length).returns(salt)

return { verify: () => {
sinon.assert.calledOnce(stub)
sinon.assert.calledWith(stub, salt.length)
stub.restore()
}}
if (process.browser) {
// In the browser tests
exports.getSaltHelper = function (salt) {
const stub = sinon.stub(global.crypto, 'getRandomValues', (arr) => {
if (arr.length !== salt.byteLength) {
// throw new Error('Unexpected call to getRandomValues with length ' +
// arr.length + ', expected: ' + salt.byteLength)
arr[arr.length - 1] = 1
} else {
salt.copy(arr)
}
})

return { verify: () => {
sinon.assert.called(stub)
stub.restore()
}}
}
} else {
// In the Node.js tests
exports.getSaltHelper = function (salt) {
const stub = sinon.stub(crypto, 'randomBytes')
stub.withArgs(salt.length).returns(salt)

return { verify: () => {
sinon.assert.calledOnce(stub)
sinon.assert.calledWith(stub, salt.length)
stub.restore()
}}
}
}
11 changes: 11 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// require all modules ending in "Spec" from the
// current directory and all subdirectories
const testsContext = require.context('.', true, /Spec$/)
testsContext.keys().forEach(function (path) {
try {
testsContext(path)
} catch (err) {
console.error('[ERROR] WITH SPEC FILE: ', path)
console.error(err)
}
})
1 change: 1 addition & 0 deletions test/mocha.opts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test/*Spec.js
27 changes: 27 additions & 0 deletions webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const webpack = require('webpack')
const path = require('path')
const UglifyJsPlugin = webpack.optimize.UglifyJsPlugin

module.exports = {
devtool: 'inline-source-map',
entry: ['babel-polyfill', './index.js'],
output: {
path: path.join(__dirname, 'dist'),
filename: 'index.js',
library: 'FiveBellsCondition',
libraryTarget: 'umd',
umdNamedDefine: true
},
module: {
loaders: [
{ test: /\.js$/, exclude: /node_modules/, loader: 'babel' },
{ test: /\.json$/, loader: 'json-loader' }
]
},
node: {
fs: 'empty'
},
plugins: [
new UglifyJsPlugin()
]
}

0 comments on commit 3422a39

Please sign in to comment.