Use webworkify

This commit is contained in:
Kagami Hiiragi 2015-01-10 16:00:08 +03:00
parent 3b662af5ed
commit 53f38be9ed
8 changed files with 27 additions and 38 deletions

1
.gitignore vendored
View File

@ -1,5 +1,4 @@
/node_modules/
/npm-debug.log
/docs/
/worker.browserify.js
/build/

View File

@ -2,4 +2,3 @@
/test*
/karma*
/docs/
/worker.browserify.js

View File

@ -3,5 +3,4 @@
/karma*
/docs/
/jsdoc.json
/worker.browserify.js
/build/

View File

@ -4,8 +4,6 @@ var files = ["test.js"];
if (allTests) {
// Kludgy way to pass a variable to `test.js`.
files.unshift("karma-all-tests.js");
// Worker code.
files.push({pattern: "worker.browserify.js", included: false});
};
module.exports = function(config) {
@ -75,6 +73,6 @@ module.exports = function(config) {
singleRun: true,
browserNoActivityTimeout: allTests ? 120000 : 10000,
browserNoActivityTimeout: allTests ? 300000 : 60000,
});
};

View File

@ -4,12 +4,20 @@
"use strict";
// `hash.js` is already required by
// `bitmessage -> eccrypto -> elliptic -> hash.js` so it won't add
// additional bytes to the bundle.
var hash = require("hash.js");
// Use only one submodule from `sha.js` here and in subworker because
// it's faster. It will add additional bytes to the bundle but not that
// much (~10-30KB).
var Sha512 = require("sha.js/sha512");
var BN = require("bn.js");
var work = require("webworkify");
var assert = require("./util").assert;
exports.sha512 = function(buf) {
return new Buffer(hash.sha512().update(buf).digest());
return new Sha512().update(buf).digest();
};
exports.sha256 = function(buf) {
@ -63,7 +71,6 @@ exports.doPOW = function(opts) {
// Check all input params prematurely to not let promise executor or
// worker to fail because of it.
assert(poolSize > 0, "Pool size is too low");
assert(opts.workerUrl, "Bad worker URL");
assert(typeof opts.target === "number", "Bad target");
assert(Buffer.isBuffer(opts.initialHash), "Bad initial hash");
@ -99,7 +106,7 @@ exports.doPOW = function(opts) {
var workers = [];
var worker;
for (var i = 0; i < poolSize; i++) {
worker = new Worker(opts.workerUrl);
worker = work(require("./worker.browser.js"));
workers.push(worker);
// NOTE(Kagami): There is no race condition here. `onmessage` can
// only be called _after_ this for-loop finishes. See

View File

@ -4,23 +4,10 @@
"use strict";
// NOTE(Kagami): In order to use it you need to create separate
// browserify bundle for this file, place it somewhere in your HTTP
// server assets path (under the same origin with your application code)
// and then pass appropriate `workerUrl` value to the function that
// spawns workers.
// You may also try to pass object URL instead. See
// <http://mdn.io/worker.worker>, <https://stackoverflow.com/q/10343913>
// for details.
// XXX(Kagami): This is rather unpleasent that we use different SHA-2
// implementations for main library code and for worker code (we use
// `sha.js` here because it's faster). Though worker code lays in
// separate file so it shouldn't result in any download overhead.
var createHash = require("sha.js");
var Sha512 = require("sha.js/sha512");
function sha512(buf) {
return createHash("sha512").update(buf).digest();
return new Sha512().update(buf).digest();
}
function pow(opts) {
@ -59,7 +46,8 @@ function pow(opts) {
}
}
onmessage = function(e) { // jshint ignore:line
var nonce = pow(e.data);
postMessage(nonce); // jshint ignore:line
module.exports = function(self) {
self.onmessage = function(e) {
self.postMessage(pow(e.data));
};
};

View File

@ -8,12 +8,11 @@
},
"scripts": {
"install": "node-gyp rebuild || exit 0",
"test": "ALL_TESTS=1 mocha && ALL_TESTS=1 npm run -s kc && ALL_TESTS=1 npm run -s kf && jshint .",
"test": "ALL_TESTS=1 mocha && ALL_TESTS=1 xvfb-run -a karma start && jshint .",
"m": "mocha",
"k": "npm run -s w && xvfb-run -a karma start",
"kc": "npm run -s w && xvfb-run -a karma start --browsers Chromium",
"kf": "npm run -s w && xvfb-run -a karma start --browsers Firefox",
"w": "browserify lib/worker.browser.js > worker.browserify.js",
"k": "xvfb-run -a karma start",
"kc": "xvfb-run -a karma start --browsers Chromium",
"kf": "xvfb-run -a karma start --browsers Firefox",
"j": "jshint .",
"d": "jsdoc -c jsdoc.json",
"mv-docs": "rm -rf docs && jsdoc -c jsdoc.json && D=`mktemp -d` && mv docs \"$D\" && git checkout gh-pages && rm -rf docs && mv \"$D/docs\" . && rm -rf \"$D\""
@ -35,7 +34,6 @@
},
"homepage": "https://github.com/bitchan/bitmessage",
"devDependencies": {
"browserify": "^8.1.0",
"chai": "*",
"jsdoc": "^3.3.0-alpha13",
"jshint": "*",
@ -58,6 +56,7 @@
"hash.js": "^1.0.2",
"nan": "^1.4.1",
"object-assign": "^2.0.0",
"sha.js": "^2.3.0"
"sha.js": "^2.3.0",
"webworkify": "^1.0.1"
}
}

View File

@ -281,8 +281,8 @@ describe("POW", function() {
if (allTests) {
it("should do a POW", function() {
this.timeout(120000);
return POW.do({workerUrl: "/base/worker.browserify.js", target: 10693764680411, initialHash: Buffer("8ff2d685db89a0af2e3dbfd3f700ae96ef4d9a1eac72fd778bbb368c7510cddda349e03207e1c4965bd95c6f7265e8f1a481a08afab3874eaafb9ade09a10880", "hex")})
this.timeout(300000);
return POW.do({target: 10693764680411, initialHash: Buffer("8ff2d685db89a0af2e3dbfd3f700ae96ef4d9a1eac72fd778bbb368c7510cddda349e03207e1c4965bd95c6f7265e8f1a481a08afab3874eaafb9ade09a10880", "hex")})
.then(function(nonce) {
// Multiple valid nonces.
expect([2373146, 2543600]).to.include(nonce);
@ -309,7 +309,7 @@ describe("High-level classes", function() {
});
it("should allow to generate new Bitmessage address", function() {
this.timeout(10000);
this.timeout(60000);
var addr = Address.fromRandom();
expect(addr.version).to.equal(4);
expect(addr.stream).to.equal(1);
@ -328,7 +328,7 @@ describe("High-level classes", function() {
// very slow. This need to be fixed.
if (allTests && typeof window === "undefined") {
it("should allow to generate shorter address", function() {
this.timeout(120000);
this.timeout(300000);
var addr = Address.fromRandom({ripelen: 18});
var ripe = addr.getRipe({short: true});
expect(ripe.length).to.be.at.most(18);