util: rely on polyfill for nextTick and typed arrays.

This commit is contained in:
Christopher Jeffrey 2017-06-26 23:31:32 -07:00
parent 8b2a0ce446
commit d9342cd81c
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
11 changed files with 26 additions and 293 deletions

View File

@ -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.
*/

View File

@ -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++)

View File

@ -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);
};
/**

View File

@ -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

View File

@ -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);
}
/**

View File

@ -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');

View File

@ -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 68.
nextTick = installReadyState();
} else if (hasSetTimeout()) {
// For older browsers.
nextTick = installSetTimeout();
} else {
throw new Error('nextTick not supported.');
}
/*
* Expose
*/
module.exports = nextTick;

View File

@ -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);
};

View File

@ -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);
});
});

View File

@ -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) {

View File

@ -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"