From 8358fa59c69405099412990f35ebd038e448dd46 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Tue, 27 Jun 2017 00:31:14 -0700 Subject: [PATCH] util: drop all homemade polyfills. --- lib/blockchain/chain.js | 2 +- lib/http/client.js | 6 +-- lib/mining/cpuminer.js | 8 +++- lib/net/pool.js | 2 +- lib/node/config.js | 10 +++-- lib/utils/encoding.js | 10 +---- lib/utils/ip.js | 12 +----- lib/utils/util.js | 90 ++++----------------------------------- lib/workers/master.js | 12 ++++-- lib/workers/workerpool.js | 11 +++-- package.json | 3 +- 11 files changed, 44 insertions(+), 122 deletions(-) diff --git a/lib/blockchain/chain.js b/lib/blockchain/chain.js index a09e5c0f..03022cf4 100644 --- a/lib/blockchain/chain.js +++ b/lib/blockchain/chain.js @@ -1943,7 +1943,7 @@ Chain.prototype.getProofTime = function getProofTime(to, from) { work = work.div(this.tip.getProof()); if (work.bitLength() > 53) - return sign * util.MAX_SAFE_INTEGER; + return sign * Number.MAX_SAFE_INTEGER; return sign * work.toNumber(); }; diff --git a/lib/http/client.js b/lib/http/client.js index 767d3371..d80247ff 100644 --- a/lib/http/client.js +++ b/lib/http/client.js @@ -646,7 +646,7 @@ HTTPClient.prototype.getWalletCoin = function getWalletCoin(id, account, hash, i */ HTTPClient.prototype.send = function send(id, options) { - options = util.merge({}, options); + options = Object.assign({}, options); options.outputs = options.outputs || []; if (options.rate) @@ -695,7 +695,7 @@ HTTPClient.prototype.setPassphrase = function setPassphrase(id, old, new_) { */ HTTPClient.prototype.createTX = function createTX(id, options) { - options = util.merge({}, options); + options = Object.assign({}, options); if (options.rate) options.rate = Amount.btc(options.rate); @@ -725,7 +725,7 @@ HTTPClient.prototype.sign = function sign(id, tx, options) { if (!options) options = {}; - body = util.merge({}, options); + body = Object.assign({}, options); body.tx = toHex(tx); return this._post('/wallet/' + id + '/sign', body); diff --git a/lib/mining/cpuminer.js b/lib/mining/cpuminer.js index 8e2b6e5e..573b7e00 100644 --- a/lib/mining/cpuminer.js +++ b/lib/mining/cpuminer.js @@ -99,7 +99,7 @@ CPUMiner.prototype._close = async function close() { CPUMiner.prototype.start = function start() { assert(!this.running, 'Miner is already running.'); - this._start().catch(util.nop); + this._start().catch(nop); }; /** @@ -571,6 +571,12 @@ CPUJob.prototype.pushTX = function pushTX(tx, view) { return this.attempt.pushTX(tx, view); }; +/* + * Helpers + */ + +function nop() {} + /* * Expose */ diff --git a/lib/net/pool.js b/lib/net/pool.js index 75a3fe8f..c730e826 100644 --- a/lib/net/pool.js +++ b/lib/net/pool.js @@ -1186,7 +1186,7 @@ Pool.prototype.createInbound = function createInbound(socket) { */ Pool.prototype.uid = function uid() { - var MAX = util.MAX_SAFE_INTEGER; + var MAX = Number.MAX_SAFE_INTEGER; if (this.id >= MAX - this.peers.size() - 1) this.id = 0; diff --git a/lib/node/config.js b/lib/node/config.js index dc165fce..6172bf32 100644 --- a/lib/node/config.js +++ b/lib/node/config.js @@ -9,6 +9,8 @@ var assert = require('assert'); var fs = require('../utils/fs'); var util = require('../utils/util'); +var os = require('os'); +var HOME = os.homedir(); /** * Config Parser @@ -25,7 +27,7 @@ function Config(module) { this.module = module; this.network = 'main'; - this.prefix = util.HOME + '/.' + module; + this.prefix = HOME + '/.' + module; this.options = Object.create(null); this.data = Object.create(null); @@ -531,7 +533,7 @@ Config.prototype.path = function path(key, fallback) { switch (value[0]) { case '~': // home dir - value = util.HOME + value.substring(1); + value = HOME + value.substring(1); break; case '@': // prefix value = this.prefix + value.substring(1); @@ -591,11 +593,11 @@ Config.prototype.getPrefix = function getPrefix() { if (prefix) { if (prefix[0] === '~') - prefix = util.HOME + prefix.substring(1); + prefix = HOME + prefix.substring(1); return prefix; } - prefix = util.HOME + '/.' + this.module; + prefix = HOME + '/.' + this.module; network = this.str('network'); if (network) { diff --git a/lib/utils/encoding.js b/lib/utils/encoding.js index 4eef2162..40cd71dc 100644 --- a/lib/utils/encoding.js +++ b/lib/utils/encoding.js @@ -14,14 +14,6 @@ var BN = require('bn.js'); var encoding = exports; -/** - * Max safe integer (53 bits). - * @const {Number} - * @default - */ - -encoding.MAX_SAFE_INTEGER = 0x1fffffffffffff; - /** * An empty buffer. * @const {Buffer} @@ -360,7 +352,7 @@ encoding._write64 = function _write64(dst, num, off, be) { num -= 1; } - enforce(num <= encoding.MAX_SAFE_INTEGER, off, 'Number exceeds 2^53-1'); + enforce(num <= Number.MAX_SAFE_INTEGER, off, 'Number exceeds 2^53-1'); lo = num % 0x100000000; hi = (num - lo) / 0x100000000; diff --git a/lib/utils/ip.js b/lib/utils/ip.js index da2bcbd4..979396de 100644 --- a/lib/utils/ip.js +++ b/lib/utils/ip.js @@ -990,7 +990,7 @@ IP.isEqual = function isEqual(a, b) { */ IP.getInterfaces = function _getInterfaces(name, family) { - var interfaces = getInterfaces(); + var interfaces = os.networkInterfaces(); var keys = Object.keys(interfaces); var result = []; var i, j, key, items, details, type, raw; @@ -1081,16 +1081,6 @@ function Address(host, port, type, hostname, raw) { this.raw = raw || ZERO_IP; } -/* - * Helpers - */ - -function getInterfaces() { - if (os.unsupported) - return {}; - return os.networkInterfaces(); -} - /* * Expose */ diff --git a/lib/utils/util.js b/lib/utils/util.js index 127b1132..1313c75b 100644 --- a/lib/utils/util.js +++ b/lib/utils/util.js @@ -9,7 +9,6 @@ var assert = require('assert'); var nodeUtil = require('util'); -var os = require('os'); /** * @exports utils/util @@ -17,25 +16,6 @@ var os = require('os'); var util = exports; -/** - * The home directory. - * @const {String} - */ - -util.HOME = '/'; - -if (os.homedir) - util.HOME = os.homedir(); - -/** - * Global NOP function. - * @type function - * @static - * @method - */ - -util.nop = function() {}; - /** * Clone a buffer. * @param {Buffer} data @@ -135,39 +115,6 @@ util.revHex = function revHex(data) { return out; }; -/** - * Shallow merge between multiple objects. - * @param {Object} target - * @param {...Object} args - * @returns {Object} target - */ - -util.merge = function merge(target) { - var i, j, obj, keys, key; - - for (i = 1; i < arguments.length; i++) { - obj = arguments[i]; - keys = Object.keys(obj); - for (j = 0; j < keys.length; j++) { - key = keys[j]; - target[key] = obj[key]; - } - } - - return target; -}; - -if (Object.assign) - util.merge = Object.assign; - -/** - * Max safe integer (53 bits). - * @const {Number} - * @default - */ - -util.MAX_SAFE_INTEGER = 0x1fffffffffffff; - /** * Test whether a number is below MAX_SAFE_INTEGER. * @param {Number} value @@ -175,7 +122,7 @@ util.MAX_SAFE_INTEGER = 0x1fffffffffffff; */ util.isSafeInteger = function isSafeInteger(value) { - return value >= -0x1fffffffffffff && value <= 0x1fffffffffffff; + return Number.isSafeInteger(value); }; /** @@ -363,6 +310,8 @@ util.inspectify = function inspectify(obj, color) { /** * Format a string. * @function + * @param {...String} args + * @returns {String} */ util.fmt = nodeUtil.format; @@ -380,7 +329,7 @@ util.format = function format(args, color) { return typeof args[0] === 'object' ? util.inspectify(args[0], color) - : nodeUtil.format.apply(nodeUtil, args); + : util.fmt.apply(util, args); }; /** @@ -567,33 +516,12 @@ util.mb = function mb(size) { */ util.inherits = function inherits(child, parent) { - var f; - child.super_ = parent; - - if (Object.setPrototypeOf) { - Object.setPrototypeOf(child.prototype, parent.prototype); - Object.defineProperty(child.prototype, 'constructor', { - value: child, - enumerable: false - }); - return; - } - - if (Object.create) { - child.prototype = Object.create(parent.prototype, { - constructor: { - value: child, - enumerable: false - } - }); - return; - } - - f = function() {}; - f.prototype = parent.prototype; - child.prototype = new f(); - child.prototype.constructor = child; + Object.setPrototypeOf(child.prototype, parent.prototype); + Object.defineProperty(child.prototype, 'constructor', { + value: child, + enumerable: false + }); }; /** diff --git a/lib/workers/master.js b/lib/workers/master.js index 4365f5c1..7042092b 100644 --- a/lib/workers/master.js +++ b/lib/workers/master.js @@ -110,9 +110,9 @@ Master.prototype._initChildProcess = function _initChildProcess() { }); // Nowhere to send these errors: - process.stdin.on('error', util.nop); - process.stdout.on('error', util.nop); - process.stderr.on('error', util.nop); + process.stdin.on('error', nop); + process.stdout.on('error', nop); + process.stderr.on('error', nop); process.on('uncaughtException', function(err) { self.send(new packets.ErrorPacket(err)); @@ -265,6 +265,12 @@ Master.prototype.handlePacket = function handlePacket(packet) { } }; +/* + * Helpers + */ + +function nop() {} + /* * Expose */ diff --git a/lib/workers/workerpool.js b/lib/workers/workerpool.js index 4ceee1c5..94a99916 100644 --- a/lib/workers/workerpool.js +++ b/lib/workers/workerpool.js @@ -48,7 +48,7 @@ function WorkerPool(options) { this.set(options); - this.on('error', util.nop); + this.on('error', nop); } util.inherits(WorkerPool, EventEmitter); @@ -547,7 +547,7 @@ Worker.prototype._initChildProcess = function _initChildProcess() { var self = this; var file = process.argv[0]; var argv = [__dirname + '/worker.js']; - var env = util.merge({}, process.env, this.env); + var env = Object.assign({}, process.env, this.env); var options = { stdio: 'pipe', env: env }; this.child = cp.spawn(file, argv, options); @@ -887,12 +887,11 @@ PendingJob.prototype.reject = function reject(err) { */ function getCores() { - if (os.unsupported) - return 2; - - return Math.max(1, os.cpus().length); + return Math.max(2, os.cpus().length); } +function nop() {} + /* * Default Pool */ diff --git a/package.json b/package.json index f102bf68..b641ef63 100644 --- a/package.json +++ b/package.json @@ -67,14 +67,13 @@ "webpack": "webpack" }, "browser": { - "bcoin-native": "./browser/empty.js", "child_process": "./browser/empty.js", "crypto": "./browser/empty.js", "dgram": "./browser/empty.js", "dns": "./browser/empty.js", "fs": "./browser/empty.js", "net": "./browser/empty.js", - "os": "./browser/empty.js", + "bcoin-native": "./browser/empty.js", "secp256k1": "./browser/empty.js", "socket.io": "./browser/empty.js", "./lib/bcoin": "./lib/bcoin-browser.js",