diff --git a/.travis.yml b/.travis.yml index dd78643..f6d1d55 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,8 @@ language: node_js node_js: - "0.10" +before_script: + - "sudo apt-get install -y chromium-browser" notifications: email: on_success: never diff --git a/karma.conf.js b/karma.conf.js new file mode 100644 index 0000000..821066a --- /dev/null +++ b/karma.conf.js @@ -0,0 +1,73 @@ +"use strict"; + +process.env.CHROME_BIN = "chromium-browser"; + +module.exports = function(config) { + config.set({ + + // base path that will be used to resolve all patterns (eg. files, exclude) + basePath: "", + + + // frameworks to use + // available frameworks: https://npmjs.org/browse/keyword/karma-adapter + frameworks: ["mocha", "browserify"], + + + // list of files / patterns to load in the browser + files: [ + "test.js" + ], + + + // preprocess matching files before serving them to the browser + // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor + preprocessors: { + "test.js": ["browserify"], + }, + + + // list of files to exclude + exclude: [ + ], + + + // test results reporter to use + // possible values: "dots", "progress" + // available reporters: https://npmjs.org/browse/keyword/karma-reporter + reporters: ["mocha"], + + + // web server port + port: 9876, + + + // enable / disable colors in the output (reporters and logs) + colors: true, + + + // level of logging + // possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG + logLevel: config.LOG_INFO, + + + // enable / disable watching file and executing tests whenever any file changes + autoWatch: true, + + + // start these browsers + // available browser launchers: https://npmjs.org/browse/keyword/karma-launcher + browsers: ["chromium_no_sandbox"], + customLaunchers: { + chromium_no_sandbox: { + base: "Chrome", + flags: ["--no-sandbox"], + }, + }, + + + // Continuous Integration mode + // if true, Karma captures browsers, runs the tests and exits + singleRun: false, + }); +}; diff --git a/lib/crypto.browser.js b/lib/crypto.browser.js index 3c553ff..91f3c69 100644 --- a/lib/crypto.browser.js +++ b/lib/crypto.browser.js @@ -2,3 +2,11 @@ * Browser version of the crypto for Bitmessage JS implementation. * @module bitmessage/lib/crypto.browser */ + +"use strict"; + +exports.sha512 = function(buf) { + return window.crypto.subtle.digest({name: "SHA-512"}, buf).then(function(arr) { + return new Buffer(new Uint8Array(arr)); + }); +}; diff --git a/package.json b/package.json index 0a88059..89cedd6 100644 --- a/package.json +++ b/package.json @@ -2,12 +2,13 @@ "name": "bitmessage", "version": "0.0.0", "description": "JavaScript Bitmessage library", - "main": "./lib/index", + "main": "./lib/index.js", "browser": { - "./lib/crypto": "./lib/crypto.browser" + "./lib/crypto.js": "./lib/crypto.browser.js" }, "scripts": { - "test": "mocha && jshint ." + "test": "mocha && xvfb-run -a karma start --single-run && jshint .", + "karma": "xvfb-run -a karma start" }, "repository": { "type": "git", @@ -28,6 +29,12 @@ "devDependencies": { "chai": "*", "jshint": "*", + "karma": "^0.12.28", + "karma-browserify": "^1.0.1", + "karma-chrome-launcher": "^0.1.7", + "karma-cli": "~0.0.4", + "karma-mocha": "^0.1.10", + "karma-mocha-reporter": "^0.3.1", "mocha": "*" }, "dependencies": { diff --git a/test.js b/test.js index 34d9898..2683bf3 100644 --- a/test.js +++ b/test.js @@ -3,10 +3,9 @@ var expect = require("chai").expect; var bmcrypto = require("./lib/crypto"); describe("Bitmessage crypto", function() { - it("should calculate sha512 hash for both node and browserify", function(done) { - bmcrypto.sha512(new Buffer("test")).then(function(res) { + it("should calculate sha512 hash", function() { + return bmcrypto.sha512(new Buffer("test")).then(function(res) { expect(res.toString("hex")).to.equal("ee26b0dd4af7e749aa1a8ee3c10ae9923f618980772e473f8819a5d4940e0db27ac185f8a0e1d5f84f88bc887fd67b143732c304cc5fa9ad8e6f57f50028a8ff"); - done(); }); }); });