Skip to content

Commit

Permalink
workers/build: require only one worker file.
Browse files Browse the repository at this point in the history
  • Loading branch information
chjj committed Jul 17, 2017
1 parent f4f5abe commit 6154ca8
Show file tree
Hide file tree
Showing 12 changed files with 42 additions and 80 deletions.
4 changes: 0 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
all:
@npm run webpack
@cp -f lib/workers/worker-browser.js browser/bcoin-worker.js

browser:
@npm run webpack-browser
@cp -f lib/workers/worker-browser.js browser/bcoin-worker.js

compat:
@npm run webpack-compat
@cp -f lib/workers/worker-browser.js browser/bcoin-worker.js

node:
@npm run webpack-node
@cp -f lib/workers/worker.js ./bcoin-worker.js

clean:
@npm run clean
Expand Down
3 changes: 1 addition & 2 deletions browser/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,7 @@ node = new bcoin.fullnode({
coinCache: 30000000,
logConsole: true,
workers: true,
workerURL: '/bcoin-worker.js',
masterURL: '/bcoin-master.js',
workerFile: '/bcoin-worker.js',
logger: logger
});

Expand Down
5 changes: 0 additions & 5 deletions browser/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ const index = fs.readFileSync(`${__dirname}/index.html`);
const indexjs = fs.readFileSync(`${__dirname}/index.js`);
const debug = fs.readFileSync(`${__dirname}/debug.html`);
const bcoin = fs.readFileSync(`${__dirname}/bcoin.js`);
const master = fs.readFileSync(`${__dirname}/bcoin-master.js`);
const worker = fs.readFileSync(`${__dirname}/bcoin-worker.js`);

let proxy = new WSProxy({
Expand Down Expand Up @@ -49,10 +48,6 @@ server.get('/bcoin.js', (req, res) => {
res.send(200, bcoin, 'js');
});

server.get('/bcoin-master.js', (req, res) => {
res.send(200, master, 'js');
});

server.get('/bcoin-worker.js', (req, res) => {
res.send(200, worker, 'js');
});
Expand Down
4 changes: 1 addition & 3 deletions lib/node/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,7 @@ Node.prototype.initOptions = function initOptions() {
enabled: config.bool('workers'),
size: config.num('workers-size'),
timeout: config.num('workers-timeout'),
workerFile: config.str('worker-file'),
workerURL: config.str('worker-url'),
masterURL: config.str('master-url')
file: config.str('worker-file')
});
};

Expand Down
8 changes: 1 addition & 7 deletions lib/workers/master.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ const Framer = require('./framer');
const packets = require('./packets');
const HAS_WORKERS = typeof global.postMessage === 'function';
const HAS_CP = !!(process.stdin && process.stdout && process.stderr);
let server;

/**
* Represents the master process.
Expand Down Expand Up @@ -249,9 +248,4 @@ Master.prototype.handlePacket = function handlePacket(packet) {
* Expose
*/

server = new Master();

if (HAS_WORKERS)
global.master = server;

module.exports = server;
module.exports = Master;
9 changes: 5 additions & 4 deletions lib/workers/worker-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@

'use strict';

let Master = require('./master');

self.onmessage = function onmessage(event) {
var env;
let env, server;

self.onmessage = function() {};

env = JSON.parse(event.data);

self.importScripts(env.BCOIN_MASTER_URL);
self.master.listen(env);
server = new Master();
server.listen(env);
};
5 changes: 3 additions & 2 deletions lib/workers/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

'use strict';

let master = require('./master');
let Master = require('./master');
let server = new Master();

master.listen(process.env);
server.listen(process.env);
54 changes: 14 additions & 40 deletions lib/workers/workerpool.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,7 @@ function WorkerPool(options) {
this.nonce = 0;
this.enabled = true;

this.workerFile = WorkerPool.WORKER_FILE;
this.workerURL = WorkerPool.WORKER_URL;
this.masterURL = WorkerPool.MASTER_URL;
this.file = WorkerPool.WORKER_FILE;

bindExit();
pools.add(this);
Expand All @@ -78,20 +76,6 @@ WorkerPool.CORES = getCores();

WorkerPool.WORKER_FILE = process.env.BCOIN_WORKER_FILE || 'worker.js';

/**
* Default worker URL.
* @const {String}
*/

WorkerPool.WORKER_URL = process.env.BCOIN_WORKER_URL || '/bcoin-worker.js';

/**
* Default master URL.
* @const {String}
*/

WorkerPool.MASTER_URL = process.env.BCOIN_MASTER_URL || '/bcoin-master.js';

/**
* Set worker pool options.
* @param {Object} options
Expand All @@ -118,19 +102,9 @@ WorkerPool.prototype.set = function set(options) {
this.timeout = options.timeout;
}

if (options.workerFile != null) {
assert(typeof options.workerFile === 'string');
this.workerFile = options.workerFile;
}

if (options.workerURL != null) {
assert(typeof options.workerURL === 'string');
this.workerURL = options.workerURL;
}

if (options.masterURL != null) {
assert(typeof options.masterURL === 'string');
this.masterURL = options.masterURL;
if (options.file != null) {
assert(typeof options.file === 'string');
this.file = options.file;
}
};

Expand Down Expand Up @@ -176,7 +150,9 @@ WorkerPool.prototype.close = async function close() {
*/

WorkerPool.prototype.spawn = function spawn(id) {
let child = new Worker(id, this);
let child = new Worker(this.file);

child.id = id;

child.on('error', (err) => {
this.emit('error', err, child);
Expand Down Expand Up @@ -416,26 +392,24 @@ WorkerPool.prototype.scrypt = async function scrypt(passwd, salt, N, r, p, len)
* Represents a worker.
* @alias module:workers.Worker
* @constructor
* @param {Number} id
* @param {Object} options
* @param {String} file
*/

function Worker(id, options) {
function Worker(file) {
if (!(this instanceof Worker))
return new Worker(id, options);
return new Worker(file);

EventEmitter.call(this);

this.id = id;
this.options = options;
this.file = file;
this.id = -1;

this.framer = new Framer();
this.parser = new Parser();
this.pending = new Map();
this.child = null;

this.env = {
BCOIN_MASTER_URL: this.options.masterURL,
BCOIN_WORKER_NETWORK: Network.type,
BCOIN_WORKER_ISTTY: process.stdout
? (process.stdout.isTTY ? '1' : '0')
Expand Down Expand Up @@ -484,7 +458,7 @@ Worker.prototype._init = function _init() {
*/

Worker.prototype._initWebWorkers = function _initWebWorkers() {
this.child = new global.Worker(this.options.workerURL);
this.child = new global.Worker(this.file);

this.child.onerror = (event) => {
this.emit('error', new Error('Worker error.'));
Expand Down Expand Up @@ -515,7 +489,7 @@ Worker.prototype._initWebWorkers = function _initWebWorkers() {

Worker.prototype._initChildProcess = function _initChildProcess() {
let bin = process.argv[0];
let file = path.resolve(__dirname, this.options.workerFile);
let file = path.resolve(__dirname, this.file);
let env = Object.assign({}, process.env, this.env);
let options = { stdio: 'pipe', env: env };

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
"bcoin": "./bin/bcoin"
},
"scripts": {
"clean": "rm -f {browser/,}{bcoin.js,bcoin-master.js,bcoin-worker.js}",
"clean": "rm -f {browser/,}{bcoin.js,bcoin-worker.js}",
"docs": "jsdoc -c jsdoc.json",
"lint": "eslint lib/ test/ migrate/ examples/ bench/ scripts/*.js bin/cli bin/node bin/spvnode || exit 0",
"lint-file": "eslint",
Expand Down
14 changes: 8 additions & 6 deletions webpack.browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module.exports = {
target: 'web',
entry: {
'bcoin': './lib/bcoin-browser',
'bcoin-master': './lib/workers/master'
'bcoin-worker': './lib/workers/worker-browser'
},
output: {
path: path.join(__dirname, 'browser'),
Expand All @@ -24,11 +24,13 @@ module.exports = {
new webpack.DefinePlugin({
'process.env.BCOIN_NETWORK':
str(env.BCOIN_NETWORK || 'main'),
'process.env.BCOIN_WORKER_URL':
str(env.BCOIN_WORKER_URL || '/bcoin-worker.js'),
'process.env.BCOIN_MASTER_URL':
str(env.BCOIN_MASTER_URL || '/bcoin-master.js')
'process.env.BCOIN_WORKER_FILE':
str(env.BCOIN_WORKER_FILE || '/bcoin-worker.js')
}),
new UglifyJsPlugin()
new UglifyJsPlugin({
compress: {
warnings: true
}
})
]
};
8 changes: 3 additions & 5 deletions webpack.compat.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module.exports = {
target: 'web',
entry: {
'bcoin': './lib/bcoin-browser',
'bcoin-master': './lib/workers/master'
'bcoin-worker': './lib/workers/worker-browser'
},
output: {
path: path.join(__dirname, 'browser'),
Expand All @@ -30,10 +30,8 @@ module.exports = {
new webpack.DefinePlugin({
'process.env.BCOIN_NETWORK':
str(env.BCOIN_NETWORK || 'main'),
'process.env.BCOIN_WORKER_URL':
str(env.BCOIN_WORKER_URL || '/bcoin-worker.js'),
'process.env.BCOIN_MASTER_URL':
str(env.BCOIN_MASTER_URL || '/bcoin-master.js')
'process.env.BCOIN_WORKER_FILE':
str(env.BCOIN_WORKER_FILE || '/bcoin-worker.js')
}),
new webpack.optimize.UglifyJsPlugin({
compress: {
Expand Down
6 changes: 5 additions & 1 deletion webpack.node.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ module.exports = {
str(env.BCOIN_WORKER_FILE || 'bcoin-worker.js')
}),
new webpack.IgnorePlugin(/^utf-8-validate|bufferutil$/),
new UglifyJsPlugin()
new UglifyJsPlugin({
compress: {
warnings: true
}
})
]
};

0 comments on commit 6154ca8

Please sign in to comment.