From d9342cd81c945b7e2a94d88bc48bda9a1337e98e Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Mon, 26 Jun 2017 23:31:32 -0700 Subject: [PATCH] util: rely on polyfill for nextTick and typed arrays. --- lib/blockchain/chaindb.js | 4 +- lib/crypto/aes.js | 5 +- lib/db/memdb.js | 37 +++--- lib/mempool/fees.js | 2 - lib/utils/co.js | 7 +- lib/utils/index.js | 1 - lib/utils/nexttick-browser.js | 242 ---------------------------------- lib/utils/nexttick.js | 14 -- lib/workers/master.js | 3 +- lib/workers/workerpool.js | 3 +- package.json | 1 - 11 files changed, 26 insertions(+), 293 deletions(-) delete mode 100644 lib/utils/nexttick-browser.js delete mode 100644 lib/utils/nexttick.js diff --git a/lib/blockchain/chaindb.js b/lib/blockchain/chaindb.js index 5ce3143d..ffc64938 100644 --- a/lib/blockchain/chaindb.js +++ b/lib/blockchain/chaindb.js @@ -254,9 +254,7 @@ ChainDB.prototype.hasCache = function hasCache(block) { }; /** - * Get an entry directly from the LRU cache. This is - * useful for optimization if we don't want to wait on a - * nextTick during a `get()` call. + * Get an entry directly from the LRU cache. * @param {Hash|Number} block - Hash or height. */ diff --git a/lib/crypto/aes.js b/lib/crypto/aes.js index e48ce9b7..9cabecad 100644 --- a/lib/crypto/aes.js +++ b/lib/crypto/aes.js @@ -13,7 +13,6 @@ 'use strict'; var assert = require('assert'); -var U32Array = typeof Uint32Array === 'function' ? Uint32Array : Array; /** * @exports crypto/aes @@ -99,7 +98,7 @@ AESKey.prototype.getEncryptKey = function getEncryptKey() { if (this.encryptKey) return this.encryptKey; - key = new U32Array(60); + key = new Uint32Array(60); kp = 0; key[kp + 0] = readU32(this.userKey, 0); @@ -209,7 +208,7 @@ AESKey.prototype.getDecryptKey = function getDecryptKey() { // First, start with an encryption schedule. enc = this.getEncryptKey(); - key = new U32Array(60); + key = new Uint32Array(60); kp = 0; for (i = 0; i < enc.length; i++) diff --git a/lib/db/memdb.js b/lib/db/memdb.js index 64181ee7..eab6c411 100644 --- a/lib/db/memdb.js +++ b/lib/db/memdb.js @@ -7,7 +7,6 @@ 'use strict'; var assert = require('assert'); -var nextTick = require('../utils/nexttick'); var RBT = require('../utils/rbt'); var DUMMY = Buffer.alloc(0); @@ -130,7 +129,7 @@ MemDB.prototype.open = function open(options, callback) { this.options = options; - nextTick(callback); + setImmediate(callback); }; /** @@ -139,7 +138,7 @@ MemDB.prototype.open = function open(options, callback) { */ MemDB.prototype.close = function close(callback) { - nextTick(callback); + setImmediate(callback); }; /** @@ -166,7 +165,7 @@ MemDB.prototype.get = function get(key, options, callback) { err = new Error('MEMDB_NOTFOUND: Key not found.'); err.notFound = true; err.type = 'NotFoundError'; - nextTick(function() { + setImmediate(function() { callback(err); }); return; @@ -175,7 +174,7 @@ MemDB.prototype.get = function get(key, options, callback) { if (options.asBuffer === false) value = value.toString('utf8'); - nextTick(function() { + setImmediate(function() { callback(null, value); }); }; @@ -196,7 +195,7 @@ MemDB.prototype.put = function put(key, value, options, callback) { this.insert(key, value); - nextTick(callback); + setImmediate(callback); }; /** @@ -214,7 +213,7 @@ MemDB.prototype.del = function del(key, options, callback) { this.remove(key); - nextTick(callback); + setImmediate(callback); }; /** @@ -282,7 +281,7 @@ MemDB.prototype.approximateSize = function approximateSize(start, end, callback) size += item.value.length; } - nextTick(function() { + setImmediate(function() { callback(null, size); }); }; @@ -294,7 +293,7 @@ MemDB.prototype.approximateSize = function approximateSize(start, end, callback) */ MemDB.destroy = function destroy(location, callback) { - nextTick(callback); + setImmediate(callback); }; /** @@ -304,7 +303,7 @@ MemDB.destroy = function destroy(location, callback) { */ MemDB.repair = function repair(location, callback) { - nextTick(callback); + setImmediate(callback); }; /** @@ -355,7 +354,7 @@ Batch.prototype.write = function write(callback) { var i, op; if (this.written) { - nextTick(function() { + setImmediate(function() { callback(new Error('Already written.')); }); return; @@ -371,7 +370,7 @@ Batch.prototype.write = function write(callback) { this.db.remove(op.key); break; default: - nextTick(function() { + setImmediate(function() { callback(new Error('Bad operation: ' + op.type)); }); return; @@ -381,7 +380,7 @@ Batch.prototype.write = function write(callback) { this.ops = []; this.written = true; - nextTick(callback); + setImmediate(callback); return this; }; @@ -474,7 +473,7 @@ Iterator.prototype.next = function(callback) { var key, value, result; if (!this.iter) { - nextTick(function() { + setImmediate(function() { callback(new Error('Cannot call next after end.')); }); return; @@ -510,14 +509,14 @@ Iterator.prototype.next = function(callback) { if (!result) { this.iter = null; - nextTick(callback); + setImmediate(callback); return; } if (options.limit !== -1) { if (this.total >= options.limit) { this.iter = null; - nextTick(callback); + setImmediate(callback); return; } this.total += 1; @@ -538,7 +537,7 @@ Iterator.prototype.next = function(callback) { if (!options.valueAsBuffer) value = value.toString('utf8'); - nextTick(function() { + setImmediate(function() { callback(null, key, value); }); }; @@ -569,7 +568,7 @@ Iterator.prototype.seek = function seek(key) { Iterator.prototype.end = function end(callback) { if (this.ended) { - nextTick(function() { + setImmediate(function() { callback(new Error('Already ended.')); }); return; @@ -578,7 +577,7 @@ Iterator.prototype.end = function end(callback) { this.ended = true; this.iter = null; - nextTick(callback); + setImmediate(callback); }; /** diff --git a/lib/mempool/fees.js b/lib/mempool/fees.js index 7d708d6c..06881e6c 100644 --- a/lib/mempool/fees.js +++ b/lib/mempool/fees.js @@ -17,8 +17,6 @@ var StaticWriter = require('../utils/staticwriter'); var encoding = require('../utils/encoding'); var Map = require('../utils/map'); var Logger = require('../node/logger'); -var Float64Array = global.Float64Array || Array; -var Int32Array = global.Int32Array || Array; /* * Constants diff --git a/lib/utils/co.js b/lib/utils/co.js index 24d985f2..e63f6026 100644 --- a/lib/utils/co.js +++ b/lib/utils/co.js @@ -12,7 +12,6 @@ */ var assert = require('assert'); -var nextTick = require('./nexttick'); /** * Execute an instantiated generator. @@ -122,11 +121,11 @@ function cob(func) { args[i] = arguments[i]; func.apply(this, args).then(function(value) { - nextTick(function() { + setImmediate(function() { callback(null, value); }); }, function(err) { - nextTick(function() { + setImmediate(function() { callback(err); }); }); @@ -150,7 +149,7 @@ function wait() { */ function tick(resolve, reject) { - nextTick(resolve); + setImmediate(resolve); } /** diff --git a/lib/utils/index.js b/lib/utils/index.js index a9dc8bc8..35defb8d 100644 --- a/lib/utils/index.js +++ b/lib/utils/index.js @@ -22,7 +22,6 @@ exports.MappedLock = exports.Lock.Mapped; exports.LRU = require('./lru'); exports.List = require('./list'); exports.murmur3 = require('./murmur3'); -exports.nextTick = require('./nexttick'); exports.nfkd = require('./nfkd'); exports.PEM = require('./pem'); exports.protobuf = require('./protobuf'); diff --git a/lib/utils/nexttick-browser.js b/lib/utils/nexttick-browser.js deleted file mode 100644 index b6856fd0..00000000 --- a/lib/utils/nexttick-browser.js +++ /dev/null @@ -1,242 +0,0 @@ -/*! - * nexttick.js - nexttick for bcoin - * Copyright (c) 2017, Christopher Jeffrey (MIT License). - * https://github.com/bcoin-org/bcoin - * - * Parts of this software are based on setimmediate. - * - * Copyright (c) 2012 Barnesandnoble.com, llc, Donavon West, and Domenic Denicola - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the 'Software'), to - * deal in the Software without restriction, including without limitation the - * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or - * sell copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER - * DEALINGS IN THE SOFTWARE. - */ - -'use strict'; - -var document = global.document; -var nextHandle = 1; -var taskMap = {}; -var running = false; -var nextTick; - -/* - * Task Runner - */ - -function addTask(handler) { - if (typeof handler !== 'function') - throw new Error('callback must be a function.'); - - taskMap[nextHandle] = handler; - - return nextHandle++; -} - -function runTask(handle) { - var task; - - if (running) { - setTimeout(function() { - runTask(handle); - }, 1); - return; - } - - task = taskMap[handle]; - - if (task) { - running = true; - try { - task(); - } finally { - delete taskMap[handle]; - running = false; - } - } -} - -/* - * Set Immediate Implementation - */ - -function hasSetImmediate() { - return typeof global.setImmediate === 'function'; -} - -function installSetImmediate() { - return function nextTick(handler) { - if (typeof handler !== 'function') - throw new Error('callback must be a function.'); - - setImmediate(handler); - }; -} - -/* - * Next Tick Implementation - */ - -function hasNextTick() { - // Don't get fooled by browserify. - return ({}).toString.call(global.process) === '[object process]'; -} - -function installNextTick() { - return process.nextTick; -} - -/* - * Post Message Implementation - */ - -function hasPostMessage() { - var isAsync = false; - var onMessage; - - // Be sure to exclude web workers. - if (global.postMessage && !global.importScripts) { - isAsync = true; - onMessage = global.onmessage; - global.onmessage = function() { - isAsync = false; - }; - global.postMessage('', '*'); - global.onmessage = onMessage; - } - - return isAsync; -} - -function installPostMessage() { - var prefix = 'nextTick' + Math.random(); - var onMessage; - - onMessage = function(event) { - if (event.source === global - && typeof event.data === 'string' - && event.data.indexOf(prefix) === 0) { - runTask(+event.data.slice(prefix.length)); - } - }; - - if (global.addEventListener) - global.addEventListener('message', onMessage, false); - else - global.attachEvent('onmessage', onMessage); - - return function nextTick(handler) { - var handle = addTask(handler); - global.postMessage(prefix + handle, '*'); - }; -} - -/* - * Message Channel Implementation - */ - -function hasMessageChannel() { - return typeof global.MessageChannel === 'function'; -} - -function installMessageChannel() { - var channel = new MessageChannel(); - - channel.port1.onmessage = function(event) { - runTask(event.data); - }; - - return function nextTick(handler) { - var handle = addTask(handler); - channel.port2.postMessage(handle); - }; -} - -/* - * Ready State Change Implementation - */ - -function hasReadyState() { - return document && ('onreadystatechange' in document.createElement('script')); -} - -function installReadyState() { - var html = document.documentElement; - - return function nextTick(handler) { - var handle = addTask(handler); - var script = document.createElement('script'); - - script.onreadystatechange = function() { - runTask(handle); - script.onreadystatechange = null; - html.removeChild(script); - script = null; - }; - - html.appendChild(script); - }; -} - -/* - * Set Timeout Implementation - */ - -function hasSetTimeout() { - return typeof global.setTimeout === 'function'; -} - -function installSetTimeout() { - return function nextTick(handler) { - var handle = addTask(handler); - setTimeout(function() { - runTask(handle); - }, 1); - }; -} - -/* - * Install - */ - -if (hasSetImmediate()) { - // `setImmediate` is already available. - nextTick = installSetImmediate(); -} else if (hasNextTick()) { - // For Node.js before 0.9. - nextTick = installNextTick(); -} else if (hasPostMessage()) { - // For non-IE10 modern browsers. - nextTick = installPostMessage(); -} else if (hasMessageChannel()) { - // For web workers, where supported. - nextTick = installMessageChannel(); -} else if (hasReadyState()) { - // For IE 6–8. - nextTick = installReadyState(); -} else if (hasSetTimeout()) { - // For older browsers. - nextTick = installSetTimeout(); -} else { - throw new Error('nextTick not supported.'); -} - -/* - * Expose - */ - -module.exports = nextTick; diff --git a/lib/utils/nexttick.js b/lib/utils/nexttick.js deleted file mode 100644 index 365f5574..00000000 --- a/lib/utils/nexttick.js +++ /dev/null @@ -1,14 +0,0 @@ -/*! - * nexttick.js - setimmediate for bcoin - * Copyright (c) 2014-2017, Christopher Jeffrey (MIT License). - * https://github.com/bcoin-org/bcoin - */ - -'use strict'; - -module.exports = function nextTick(handler) { - if (typeof handler !== 'function') - throw new Error('callback must be a function.'); - - setImmediate(handler); -}; diff --git a/lib/workers/master.js b/lib/workers/master.js index ee9afb92..4365f5c1 100644 --- a/lib/workers/master.js +++ b/lib/workers/master.js @@ -10,7 +10,6 @@ var assert = require('assert'); var EventEmitter = require('events').EventEmitter; var util = require('../utils/util'); -var nextTick = require('../utils/nexttick'); var Network = require('../protocol/network'); var jobs = require('./jobs'); var Parser = require('./parser-client'); @@ -117,7 +116,7 @@ Master.prototype._initChildProcess = function _initChildProcess() { process.on('uncaughtException', function(err) { self.send(new packets.ErrorPacket(err)); - nextTick(function() { + setImmediate(function() { process.exit(1); }); }); diff --git a/lib/workers/workerpool.js b/lib/workers/workerpool.js index a9e71588..4ceee1c5 100644 --- a/lib/workers/workerpool.js +++ b/lib/workers/workerpool.js @@ -12,7 +12,6 @@ var EventEmitter = require('events').EventEmitter; var os = require('os'); var cp = require('child_process'); var util = require('../utils/util'); -var nextTick = require('../utils/nexttick'); var co = require('../utils/co'); var Network = require('../protocol/network'); var jobs = require('./jobs'); @@ -288,7 +287,7 @@ WorkerPool.prototype.execute = function execute(packet, timeout) { if (!this.enabled || !WorkerPool.support) { return new Promise(function(resolve, reject) { - nextTick(function() { + setImmediate(function() { try { result = jobs._execute(packet); } catch (e) { diff --git a/package.json b/package.json index 0c7d2922..f102bf68 100644 --- a/package.json +++ b/package.json @@ -98,7 +98,6 @@ "./lib/utils/fs": "./browser/empty.js", "./lib/utils/native": "./browser/empty.js", "./lib/utils/nfkd": "./lib/utils/nfkd-browser.js", - "./lib/utils/nexttick": "./lib/utils/nexttick-browser.js", "./lib/wallet/http": "./browser/empty.js", "./lib/wallet/layout": "./lib/wallet/layout-browser.js", "./lib/wallet/server": "./browser/empty.js"