From 3be5655539f6c5a3f80c5b6d864d418a758314ff Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Thu, 19 May 2016 16:09:21 -0700 Subject: [PATCH] get bcoin working on windows. --- bin/node | 2 +- lib/bcoin/env.js | 65 +++++++++++++++++++++++++++++++++++++++++----- lib/bcoin/pool.js | 4 +-- lib/bcoin/utils.js | 15 +++++++++++ 4 files changed, 76 insertions(+), 10 deletions(-) diff --git a/bin/node b/bin/node index 5799bc59..4241e49b 100755 --- a/bin/node +++ b/bin/node @@ -19,7 +19,7 @@ var node = new bcoin.fullnode({ }); node.on('error', function(err) { - bcoin.debug(err.stack + ''); + bcoin.error(err); }); node.open(function(err) { diff --git a/lib/bcoin/env.js b/lib/bcoin/env.js index d5818e17..36d0d79f 100644 --- a/lib/bcoin/env.js +++ b/lib/bcoin/env.js @@ -209,7 +209,7 @@ Environment.prototype.set = function set(options) { options.prefix = options.prefix || process.env.BCOIN_PREFIX - || process.env.HOME + '/.bcoin'; + || utils.HOME + '/.bcoin'; options.network = options.network || process.env.BCOIN_NETWORK @@ -286,7 +286,10 @@ Environment.prototype.ensurePrefix = function ensurePrefix() { try { fs.statSync(this.prefix); } catch (e) { - fs.mkdirSync(this.prefix, 488 /* 0750 */); + if (e.code === 'ENOENT') + fs.mkdirSync(this.prefix, 488 /* 0750 */); + else + throw e; } }; @@ -318,15 +321,63 @@ Environment.prototype.debug = function debug() { } if (this.debugFile) { - if (!this._debug) { - this.ensurePrefix(); - this._debug = fs.createWriteStream(this.debugFile, { flags: 'a' }); - } msg = utils.format(args, false); - this._debug.write(process.pid + ': ' + msg + '\n'); + this.write(msg); } }; +/** + * Output an error. + * @param {Error} err + */ + +Environment.prototype.error = function error(err) { + var msg; + + if (!err) + return; + + if (typeof err === 'string') + err = new Error(err); + + if (this.isBrowser) { + if (this.debugLogs) + console.error(err); + return; + } + + if (this.debugLogs) { + msg = (err.message + '').replace(/^ *Error: */, ''); + + if (process.stdout && process.stdout.isTTY) + msg = '\x1b[1;31m[Error]\x1b[m ' + msg; + else + msg = '[Error] ' + msg; + + process.stderr.write(msg + '\n'); + } + + if (this.debugFile) + this.write(err.stack + ''); +}; + +/** + * Write a message to the debug log. + * @param {String} msg + */ + +Environment.prototype.write = function write(msg) { + if (this.isBrowser) + return; + + if (!this._debug) { + this.ensurePrefix(); + this._debug = fs.createWriteStream(this.debugFile, { flags: 'a' }); + } + + this._debug.write(process.pid + ': ' + msg + '\n'); +}; + /** * Get the adjusted time. * @returns {Number} Adjusted time. diff --git a/lib/bcoin/pool.js b/lib/bcoin/pool.js index caaa8bce..bbbf0097 100644 --- a/lib/bcoin/pool.js +++ b/lib/bcoin/pool.js @@ -1430,7 +1430,7 @@ Pool.prototype.searchWallet = function(wallet, callback) { self.chain.reset(height, function(err) { if (err) { - bcoin.debug('Failed to reset height: %s', err.message); + bcoin.error(err); return callback(err); } @@ -1452,7 +1452,7 @@ Pool.prototype.searchWallet = function(wallet, callback) { self.chain.resetTime(ts, function(err) { if (err) { - bcoin.debug('Failed to reset time: %s', err.message); + bcoin.error(err); return callback(err); } diff --git a/lib/bcoin/utils.js b/lib/bcoin/utils.js index ca997aad..39912bc8 100644 --- a/lib/bcoin/utils.js +++ b/lib/bcoin/utils.js @@ -37,6 +37,21 @@ if (!utils.isBrowser) { aes = require('./aes'); } +/** + * The home directory. + * @const {String} + */ + +try { + utils.HOME = require('o' + 's').homedir(); +} catch (e) { + utils.HOME = process.env.HOME + || process.env.USERPROFILE + || process.env.HOMEPATH; +} + +assert(typeof utils.HOME === 'string'); + /** * Global NOP function. * @type function