workers: misc fixes.

This commit is contained in:
Christopher Jeffrey 2017-07-05 14:47:32 -07:00
parent 4ebe557807
commit 886008a182
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
2 changed files with 18 additions and 15 deletions

View File

@ -83,13 +83,14 @@ Master.prototype._initWebWorkers = function _initWebWorkers() {
global.onmessage = (event) => { global.onmessage = (event) => {
let data; let data;
if (typeof event.data !== 'string') { if (typeof event.data === 'string') {
data = Buffer.from(event.data, 'hex');
assert(data.length === event.data.length / 2);
} else {
assert(event.data && typeof event.data === 'object'); assert(event.data && typeof event.data === 'object');
assert(event.data.data); assert(event.data.data && typeof event.data.data.length === 'number');
data = event.data.data; data = event.data.data;
data.__proto__ = Buffer.prototype; data.__proto__ = Buffer.prototype;
} else {
data = Buffer.from(event.data, 'hex');
} }
this.emit('data', data); this.emit('data', data);
}; };
@ -112,7 +113,7 @@ Master.prototype._initChildProcess = function _initChildProcess() {
process.on('uncaughtException', (err) => { process.on('uncaughtException', (err) => {
this.send(new packets.ErrorPacket(err)); this.send(new packets.ErrorPacket(err));
setTimeout(() => process.exit(1), 500); setTimeout(() => process.exit(1), 1000);
}); });
}; };

View File

@ -176,7 +176,7 @@ WorkerPool.prototype.close = async function close() {
*/ */
WorkerPool.prototype.spawn = function spawn(id) { WorkerPool.prototype.spawn = function spawn(id) {
let child = new Worker(id); let child = new Worker(id, this.masterURL);
child.on('error', (err) => { child.on('error', (err) => {
this.emit('error', err, child); this.emit('error', err, child);
@ -416,24 +416,25 @@ WorkerPool.prototype.scrypt = async function scrypt(passwd, salt, N, r, p, len)
* Represents a worker. * Represents a worker.
* @alias module:workers.Worker * @alias module:workers.Worker
* @constructor * @constructor
* @param {Number?} id * @param {Number} id
* @param {String} masterURL
*/ */
function Worker(id) { function Worker(id, masterURL) {
if (!(this instanceof Worker)) if (!(this instanceof Worker))
return new Worker(id); return new Worker(id, masterURL);
EventEmitter.call(this); EventEmitter.call(this);
this.framer = new Framer(); this.framer = new Framer();
this.parser = new Parser(); this.parser = new Parser();
this.id = id != null ? id : -1; this.id = id;
this.child = null; this.child = null;
this.pending = new Map(); this.pending = new Map();
this.env = { this.env = {
BCOIN_MASTER_URL: this.masterURL, BCOIN_MASTER_URL: masterURL,
BCOIN_WORKER_NETWORK: Network.type, BCOIN_WORKER_NETWORK: Network.type,
BCOIN_WORKER_ISTTY: process.stdout BCOIN_WORKER_ISTTY: process.stdout
? (process.stdout.isTTY ? '1' : '0') ? (process.stdout.isTTY ? '1' : '0')
@ -491,13 +492,14 @@ Worker.prototype._initWebWorkers = function _initWebWorkers() {
this.child.onmessage = (event) => { this.child.onmessage = (event) => {
let data; let data;
if (typeof event.data !== 'string') { if (typeof event.data === 'string') {
data = Buffer.from(event.data, 'hex');
assert(data.length === event.data.length / 2);
} else {
assert(event.data && typeof event.data === 'object'); assert(event.data && typeof event.data === 'object');
assert(event.data.data); assert(event.data.data && typeof event.data.data.length === 'number');
data = event.data.data; data = event.data.data;
data.__proto__ = Buffer.prototype; data.__proto__ = Buffer.prototype;
} else {
data = Buffer.from(event.data, 'hex');
} }
this.emit('data', data); this.emit('data', data);
}; };