From 60ec6cd85f82066e86c5ac22f1088c34ad1d02f9 Mon Sep 17 00:00:00 2001 From: Hugo Dias Date: Fri, 3 Apr 2020 18:14:19 +0100 Subject: [PATCH] Add buffer and immediate for browsers (#362) --- abstract-iterator.js | 11 ++++++----- abstract-leveldown.js | 31 ++++++++++++++++--------------- next-tick.browser.js | 1 + next-tick.js | 1 + package.json | 5 +++++ 5 files changed, 29 insertions(+), 20 deletions(-) create mode 100644 next-tick.browser.js create mode 100644 next-tick.js diff --git a/abstract-iterator.js b/abstract-iterator.js index 39a462c8..f222fc1f 100644 --- a/abstract-iterator.js +++ b/abstract-iterator.js @@ -1,3 +1,4 @@ +var nextTick = require('./next-tick') function AbstractIterator (db) { if (typeof db !== 'object' || db === null) { throw new TypeError('First argument must be an abstract-leveldown compliant store') @@ -16,12 +17,12 @@ AbstractIterator.prototype.next = function (callback) { } if (self._ended) { - process.nextTick(callback, new Error('cannot call next() after end()')) + nextTick(callback, new Error('cannot call next() after end()')) return self } if (self._nexting) { - process.nextTick(callback, new Error('cannot call next() before previous next() has completed')) + nextTick(callback, new Error('cannot call next() before previous next() has completed')) return self } @@ -35,7 +36,7 @@ AbstractIterator.prototype.next = function (callback) { } AbstractIterator.prototype._next = function (callback) { - process.nextTick(callback) + nextTick(callback) } AbstractIterator.prototype.seek = function (target) { @@ -58,7 +59,7 @@ AbstractIterator.prototype.end = function (callback) { } if (this._ended) { - return process.nextTick(callback, new Error('end() already called on iterator')) + return nextTick(callback, new Error('end() already called on iterator')) } this._ended = true @@ -66,7 +67,7 @@ AbstractIterator.prototype.end = function (callback) { } AbstractIterator.prototype._end = function (callback) { - process.nextTick(callback) + nextTick(callback) } module.exports = AbstractIterator diff --git a/abstract-leveldown.js b/abstract-leveldown.js index 6295c286..3a00e730 100644 --- a/abstract-leveldown.js +++ b/abstract-leveldown.js @@ -3,6 +3,7 @@ var supports = require('level-supports') var Buffer = require('buffer').Buffer var AbstractIterator = require('./abstract-iterator') var AbstractChainedBatch = require('./abstract-chained-batch') +var nextTick = require('./next-tick') var hasOwnProperty = Object.prototype.hasOwnProperty var rangeOptions = 'start end gt gte lt lte'.split(' ') @@ -42,7 +43,7 @@ AbstractLevelDOWN.prototype.open = function (options, callback) { } AbstractLevelDOWN.prototype._open = function (options, callback) { - process.nextTick(callback) + nextTick(callback) } AbstractLevelDOWN.prototype.close = function (callback) { @@ -65,7 +66,7 @@ AbstractLevelDOWN.prototype.close = function (callback) { } AbstractLevelDOWN.prototype._close = function (callback) { - process.nextTick(callback) + nextTick(callback) } AbstractLevelDOWN.prototype.get = function (key, options, callback) { @@ -76,7 +77,7 @@ AbstractLevelDOWN.prototype.get = function (key, options, callback) { } var err = this._checkKey(key) - if (err) return process.nextTick(callback, err) + if (err) return nextTick(callback, err) key = this._serializeKey(key) @@ -88,7 +89,7 @@ AbstractLevelDOWN.prototype.get = function (key, options, callback) { } AbstractLevelDOWN.prototype._get = function (key, options, callback) { - process.nextTick(function () { callback(new Error('NotFound')) }) + nextTick(function () { callback(new Error('NotFound')) }) } AbstractLevelDOWN.prototype.put = function (key, value, options, callback) { @@ -99,7 +100,7 @@ AbstractLevelDOWN.prototype.put = function (key, value, options, callback) { } var err = this._checkKey(key) || this._checkValue(value) - if (err) return process.nextTick(callback, err) + if (err) return nextTick(callback, err) key = this._serializeKey(key) value = this._serializeValue(value) @@ -110,7 +111,7 @@ AbstractLevelDOWN.prototype.put = function (key, value, options, callback) { } AbstractLevelDOWN.prototype._put = function (key, value, options, callback) { - process.nextTick(callback) + nextTick(callback) } AbstractLevelDOWN.prototype.del = function (key, options, callback) { @@ -121,7 +122,7 @@ AbstractLevelDOWN.prototype.del = function (key, options, callback) { } var err = this._checkKey(key) - if (err) return process.nextTick(callback, err) + if (err) return nextTick(callback, err) key = this._serializeKey(key) @@ -131,7 +132,7 @@ AbstractLevelDOWN.prototype.del = function (key, options, callback) { } AbstractLevelDOWN.prototype._del = function (key, options, callback) { - process.nextTick(callback) + nextTick(callback) } AbstractLevelDOWN.prototype.batch = function (array, options, callback) { @@ -146,11 +147,11 @@ AbstractLevelDOWN.prototype.batch = function (array, options, callback) { } if (!Array.isArray(array)) { - return process.nextTick(callback, new Error('batch(array) requires an array argument')) + return nextTick(callback, new Error('batch(array) requires an array argument')) } if (array.length === 0) { - return process.nextTick(callback) + return nextTick(callback) } if (typeof options !== 'object' || options === null) options = {} @@ -159,23 +160,23 @@ AbstractLevelDOWN.prototype.batch = function (array, options, callback) { for (var i = 0; i < array.length; i++) { if (typeof array[i] !== 'object' || array[i] === null) { - return process.nextTick(callback, new Error('batch(array) element must be an object and not `null`')) + return nextTick(callback, new Error('batch(array) element must be an object and not `null`')) } var e = xtend(array[i]) if (e.type !== 'put' && e.type !== 'del') { - return process.nextTick(callback, new Error("`type` must be 'put' or 'del'")) + return nextTick(callback, new Error("`type` must be 'put' or 'del'")) } var err = this._checkKey(e.key) - if (err) return process.nextTick(callback, err) + if (err) return nextTick(callback, err) e.key = this._serializeKey(e.key) if (e.type === 'put') { var valueErr = this._checkValue(e.value) - if (valueErr) return process.nextTick(callback, valueErr) + if (valueErr) return nextTick(callback, valueErr) e.value = this._serializeValue(e.value) } @@ -187,7 +188,7 @@ AbstractLevelDOWN.prototype.batch = function (array, options, callback) { } AbstractLevelDOWN.prototype._batch = function (array, options, callback) { - process.nextTick(callback) + nextTick(callback) } AbstractLevelDOWN.prototype.clear = function (options, callback) { diff --git a/next-tick.browser.js b/next-tick.browser.js new file mode 100644 index 00000000..3e424be5 --- /dev/null +++ b/next-tick.browser.js @@ -0,0 +1 @@ +module.exports = require('immediate') diff --git a/next-tick.js b/next-tick.js new file mode 100644 index 00000000..32b40935 --- /dev/null +++ b/next-tick.js @@ -0,0 +1 @@ +module.exports = process.nextTick diff --git a/package.json b/package.json index 0557ae87..f8c65ff8 100644 --- a/package.json +++ b/package.json @@ -4,6 +4,9 @@ "description": "An abstract prototype matching the LevelDOWN API", "license": "MIT", "main": "index.js", + "browser": { + "./next-tick.js": "./next-tick.browser.js" + }, "scripts": { "test": "standard && hallmark && nyc node test/self.js", "test-browsers": "airtap --coverage --loopback airtap.local test/self.js", @@ -14,6 +17,8 @@ "prepublishOnly": "npm run dependency-check" }, "dependencies": { + "buffer": "^5.5.0", + "immediate": "^3.2.3", "level-concat-iterator": "~2.0.0", "level-supports": "~1.0.0", "xtend": "~4.0.0"