From 1035f3c1d8dadc12b1b1d3ebfb6ab8fdcbc4c49e Mon Sep 17 00:00:00 2001 From: Vincent Weevers Date: Fri, 9 Apr 2021 21:11:19 +0200 Subject: [PATCH] Modernize syntax and bump standard (Level/community#98) --- .github/dependabot.yml | 2 - index.js | 89 +++++++++++++++++++++++------------------- iterator.js | 43 ++++++++++---------- package.json | 2 +- test/custom-test.js | 66 +++++++++++++++---------------- test/index.js | 10 ++--- test/support-test.js | 6 +-- test/upgrade-test.js | 10 ++--- util/clear.js | 12 +++--- util/deserialize.js | 12 +++--- util/key-range.js | 12 +++--- util/serialize.js | 6 +-- util/support.js | 2 +- 13 files changed, 139 insertions(+), 133 deletions(-) diff --git a/.github/dependabot.yml b/.github/dependabot.yml index 5d7f065..a571853 100644 --- a/.github/dependabot.yml +++ b/.github/dependabot.yml @@ -5,7 +5,5 @@ updates: schedule: interval: monthly ignore: - - dependency-name: buffer - dependency-name: dependency-check - dependency-name: uuid - - dependency-name: standard diff --git a/index.js b/index.js index 397cb5f..1d244bd 100644 --- a/index.js +++ b/index.js @@ -4,16 +4,16 @@ module.exports = Level -var AbstractLevelDOWN = require('abstract-leveldown').AbstractLevelDOWN -var inherits = require('inherits') -var Iterator = require('./iterator') -var serialize = require('./util/serialize') -var deserialize = require('./util/deserialize') -var support = require('./util/support') -var clear = require('./util/clear') -var createKeyRange = require('./util/key-range') +const AbstractLevelDOWN = require('abstract-leveldown').AbstractLevelDOWN +const inherits = require('inherits') +const Iterator = require('./iterator') +const serialize = require('./util/serialize') +const deserialize = require('./util/deserialize') +const support = require('./util/support') +const clear = require('./util/clear') +const createKeyRange = require('./util/key-range') -var DEFAULT_PREFIX = 'level-js-' +const DEFAULT_PREFIX = 'level-js-' function Level (location, opts) { if (!(this instanceof Level)) return new Level(location, opts) @@ -41,34 +41,33 @@ inherits(Level, AbstractLevelDOWN) Level.prototype.type = 'level-js' Level.prototype._open = function (options, callback) { - var req = indexedDB.open(this.prefix + this.location, this.version) - var self = this + const req = indexedDB.open(this.prefix + this.location, this.version) req.onerror = function () { callback(req.error || new Error('unknown error')) } - req.onsuccess = function () { - self.db = req.result + req.onsuccess = () => { + this.db = req.result callback() } - req.onupgradeneeded = function (ev) { - var db = ev.target.result + req.onupgradeneeded = (ev) => { + const db = ev.target.result - if (!db.objectStoreNames.contains(self.location)) { - db.createObjectStore(self.location) + if (!db.objectStoreNames.contains(this.location)) { + db.createObjectStore(this.location) } } } Level.prototype.store = function (mode) { - var transaction = this.db.transaction([this.location], mode) + const transaction = this.db.transaction([this.location], mode) return transaction.objectStore(this.location) } Level.prototype.await = function (request, callback) { - var transaction = request.transaction + const transaction = request.transaction // Take advantage of the fact that a non-canceled request error aborts // the transaction. I.e. no need to listen for "request.onerror". @@ -82,10 +81,11 @@ Level.prototype.await = function (request, callback) { } Level.prototype._get = function (key, options, callback) { - var store = this.store('readonly') + const store = this.store('readonly') + let req try { - var req = store.get(key) + req = store.get(key) } catch (err) { return this._nextTick(callback, err) } @@ -103,10 +103,11 @@ Level.prototype._get = function (key, options, callback) { } Level.prototype._del = function (key, options, callback) { - var store = this.store('readwrite') + const store = this.store('readwrite') + let req try { - var req = store.delete(key) + req = store.delete(key) } catch (err) { return this._nextTick(callback, err) } @@ -115,12 +116,13 @@ Level.prototype._del = function (key, options, callback) { } Level.prototype._put = function (key, value, options, callback) { - var store = this.store('readwrite') + const store = this.store('readwrite') + let req try { // Will throw a DataError or DataCloneError if the environment // does not support serializing the key or value respectively. - var req = store.put(value, key) + req = store.put(value, key) } catch (err) { return this._nextTick(callback, err) } @@ -143,10 +145,10 @@ Level.prototype._iterator = function (options) { Level.prototype._batch = function (operations, options, callback) { if (operations.length === 0) return this._nextTick(callback) - var store = this.store('readwrite') - var transaction = store.transaction - var index = 0 - var error + const store = this.store('readwrite') + const transaction = store.transaction + let index = 0 + let error transaction.onabort = function () { callback(error || transaction.error || new Error('aborted by user')) @@ -158,11 +160,13 @@ Level.prototype._batch = function (operations, options, callback) { // Wait for a request to complete before making the next, saving CPU. function loop () { - var op = operations[index++] - var key = op.key + const op = operations[index++] + const key = op.key + + let req try { - var req = op.type === 'del' ? store.delete(key) : store.put(op.value, key) + req = op.type === 'del' ? store.delete(key) : store.put(op.value, key) } catch (err) { error = err transaction.abort() @@ -178,8 +182,11 @@ Level.prototype._batch = function (operations, options, callback) { } Level.prototype._clear = function (options, callback) { + let keyRange + let req + try { - var keyRange = createKeyRange(options) + keyRange = createKeyRange(options) } catch (e) { // The lower key is greater than the upper key. // IndexedDB throws an error, but we'll just do nothing. @@ -193,8 +200,8 @@ Level.prototype._clear = function (options, callback) { } try { - var store = this.store('readwrite') - var req = keyRange ? store.delete(keyRange) : store.clear() + const store = this.store('readwrite') + req = keyRange ? store.delete(keyRange) : store.clear() } catch (err) { return this._nextTick(callback, err) } @@ -213,9 +220,9 @@ Level.prototype.upgrade = function (callback) { return this._nextTick(callback, new Error('cannot upgrade() before open()')) } - var it = this.iterator() - var batchOptions = {} - var self = this + const it = this.iterator() + const batchOptions = {} + const self = this it._deserializeKey = it._deserializeValue = identity next() @@ -230,8 +237,8 @@ Level.prototype.upgrade = function (callback) { return finish(err) } - var newKey = self._serializeKey(deserialize(key, true)) - var newValue = self._serializeValue(deserialize(value, true)) + const newKey = self._serializeKey(deserialize(key, true)) + const newValue = self._serializeValue(deserialize(value, true)) // To bypass serialization on the old key, use _batch() instead of batch(). // NOTE: if we disable snapshotting (#86) this could lead to a loop of @@ -259,7 +266,7 @@ Level.destroy = function (location, prefix, callback) { callback = prefix prefix = DEFAULT_PREFIX } - var request = indexedDB.deleteDatabase(prefix + location) + const request = indexedDB.deleteDatabase(prefix + location) request.onsuccess = function () { callback() } diff --git a/iterator.js b/iterator.js index ec5bddb..0a10f0a 100644 --- a/iterator.js +++ b/iterator.js @@ -1,10 +1,10 @@ 'use strict' -var inherits = require('inherits') -var AbstractIterator = require('abstract-leveldown').AbstractIterator -var createKeyRange = require('./util/key-range') -var deserialize = require('./util/deserialize') -var noop = function () {} +const inherits = require('inherits') +const AbstractIterator = require('abstract-leveldown').AbstractIterator +const createKeyRange = require('./util/key-range') +const deserialize = require('./util/deserialize') +const noop = function () {} module.exports = Iterator @@ -30,8 +30,10 @@ function Iterator (db, location, options) { return } + let keyRange + try { - var keyRange = createKeyRange(options) + keyRange = createKeyRange(options) } catch (e) { // The lower key is greater than the upper key. // IndexedDB throws an error, but we'll just return 0 results. @@ -45,25 +47,24 @@ function Iterator (db, location, options) { inherits(Iterator, AbstractIterator) Iterator.prototype.createIterator = function (location, keyRange, reverse) { - var self = this - var transaction = this.db.db.transaction([location], 'readonly') - var store = transaction.objectStore(location) - var req = store.openCursor(keyRange, reverse ? 'prev' : 'next') - - req.onsuccess = function (ev) { - var cursor = ev.target.result - if (cursor) self.onItem(cursor) + const transaction = this.db.db.transaction([location], 'readonly') + const store = transaction.objectStore(location) + const req = store.openCursor(keyRange, reverse ? 'prev' : 'next') + + req.onsuccess = (ev) => { + const cursor = ev.target.result + if (cursor) this.onItem(cursor) } this._transaction = transaction // If an error occurs (on the request), the transaction will abort. - transaction.onabort = function () { - self.onAbort(self._transaction.error || new Error('aborted by user')) + transaction.onabort = () => { + this.onAbort(this._transaction.error || new Error('aborted by user')) } - transaction.oncomplete = function () { - self.onComplete() + transaction.oncomplete = () => { + this.onComplete() } } @@ -98,12 +99,12 @@ Iterator.prototype.maybeNext = function () { Iterator.prototype._next = function (callback) { if (this._aborted) { // The error should be picked up by either next() or end(). - var err = this._error + const err = this._error this._error = null this._nextTick(callback, err) } else if (this._cache.length > 0) { - var key = this._cache.shift() - var value = this._cache.shift() + let key = this._cache.shift() + let value = this._cache.shift() if (this._keys && key !== undefined) { key = this._deserializeKey(key, this._keyAsBuffer) diff --git a/package.json b/package.json index 6b88cd2..5daf701 100644 --- a/package.json +++ b/package.json @@ -38,7 +38,7 @@ "level-community": "^3.0.0", "level-concat-iterator": "^2.0.0", "nyc": "^15.0.0", - "standard": "^15.0.1", + "standard": "^16.0.3", "tape": "^5.0.0", "uuid": "^3.3.2" }, diff --git a/test/custom-test.js b/test/custom-test.js index 5d63140..64d29a3 100644 --- a/test/custom-test.js +++ b/test/custom-test.js @@ -1,12 +1,12 @@ 'use strict' -var concat = require('level-concat-iterator') +const concat = require('level-concat-iterator') module.exports = function (leveljs, test, testCommon) { test('setUp', testCommon.setUp) test('default prefix', function (t) { - var db = testCommon.factory() + const db = testCommon.factory() t.ok(db.location, 'instance has location property') t.is(db.prefix, 'level-js-', 'instance has prefix property') @@ -14,9 +14,9 @@ module.exports = function (leveljs, test, testCommon) { db.open(function (err) { t.notOk(err, 'no open error') - var idb = db.db - var databaseName = idb.name - var storeNames = idb.objectStoreNames + const idb = db.db + const databaseName = idb.name + const storeNames = idb.objectStoreNames t.is(databaseName, 'level-js-' + db.location, 'database name is prefixed') t.is(storeNames.length, 1, 'created 1 object store') @@ -27,7 +27,7 @@ module.exports = function (leveljs, test, testCommon) { }) test('custom prefix', function (t) { - var db = testCommon.factory({ prefix: 'custom-' }) + const db = testCommon.factory({ prefix: 'custom-' }) t.ok(db.location, 'instance has location property') t.is(db.prefix, 'custom-', 'instance has prefix property') @@ -35,9 +35,9 @@ module.exports = function (leveljs, test, testCommon) { db.open(function (err) { t.notOk(err, 'no open error') - var idb = db.db - var databaseName = idb.name - var storeNames = idb.objectStoreNames + const idb = db.db + const databaseName = idb.name + const storeNames = idb.objectStoreNames t.is(databaseName, 'custom-' + db.location, 'database name is prefixed') t.is(storeNames.length, 1, 'created 1 object store') @@ -48,7 +48,7 @@ module.exports = function (leveljs, test, testCommon) { }) test('empty prefix', function (t) { - var db = testCommon.factory({ prefix: '' }) + const db = testCommon.factory({ prefix: '' }) t.ok(db.location, 'instance has location property') t.is(db.prefix, '', 'instance has prefix property') @@ -56,9 +56,9 @@ module.exports = function (leveljs, test, testCommon) { db.open(function (err) { t.notOk(err, 'no open error') - var idb = db.db - var databaseName = idb.name - var storeNames = idb.objectStoreNames + const idb = db.db + const databaseName = idb.name + const storeNames = idb.objectStoreNames t.is(databaseName, db.location, 'database name is prefixed') t.is(storeNames.length, 1, 'created 1 object store') @@ -69,7 +69,7 @@ module.exports = function (leveljs, test, testCommon) { }) test('put Buffer value, get Buffer value', function (t) { - var level = testCommon.factory() + const level = testCommon.factory() level.open(function (err) { t.notOk(err, 'no error') level.put('key', Buffer.from('00ff', 'hex'), function (err) { @@ -85,7 +85,7 @@ module.exports = function (leveljs, test, testCommon) { }) test('put Buffer value, get string value', function (t) { - var level = testCommon.factory() + const level = testCommon.factory() level.open(function (err) { t.notOk(err, 'no error') level.put('key', Buffer.from('abc'), function (err) { @@ -100,7 +100,7 @@ module.exports = function (leveljs, test, testCommon) { }) test('put utf8 string, get utf8 string', function (t) { - var level = testCommon.factory() + const level = testCommon.factory() level.open(function (err) { t.notOk(err, 'no error') level.put('💩', '💩', function (err) { @@ -118,7 +118,7 @@ module.exports = function (leveljs, test, testCommon) { // prevented by process.browser checks (Level/abstract-leveldown#121). // This test is adapted from memdown. test('buffer keys', function (t) { - var db = testCommon.factory() + const db = testCommon.factory() if (!db.supports.bufferKeys) { t.fail('environment does not support buffer keys') @@ -128,8 +128,8 @@ module.exports = function (leveljs, test, testCommon) { db.open(function (err) { t.ifError(err, 'no open error') - var one = Buffer.from('80', 'hex') - var two = Buffer.from('c0', 'hex') + const one = Buffer.from('80', 'hex') + const two = Buffer.from('c0', 'hex') t.ok(two.toString() === one.toString(), 'would be equal when not buffer-aware') t.ok(Buffer.compare(two, one) > 0, 'but greater when buffer-aware') @@ -162,7 +162,7 @@ module.exports = function (leveljs, test, testCommon) { // This should be covered by abstract-leveldown tests, but that's // prevented by process.browser checks (Level/abstract-leveldown#121). test('iterator yields buffer keys', function (t) { - var db = testCommon.factory() + const db = testCommon.factory() if (!db.supports.bufferKeys) { t.fail('environment does not support buffer keys') @@ -178,7 +178,7 @@ module.exports = function (leveljs, test, testCommon) { ], function (err) { t.ifError(err, 'no batch error') - var it = db.iterator({ valueAsBuffer: false }) + const it = db.iterator({ valueAsBuffer: false }) concat(it, function (err, entries) { t.ifError(err, 'no iterator error') @@ -197,7 +197,7 @@ module.exports = function (leveljs, test, testCommon) { }) test('buffer range option', function (t) { - var db = testCommon.factory() + const db = testCommon.factory() if (!db.supports.bufferKeys) { t.fail('environment does not support buffer keys') @@ -207,8 +207,8 @@ module.exports = function (leveljs, test, testCommon) { db.open(function (err) { t.ifError(err, 'no open error') - var one = Buffer.from('80', 'hex') - var two = Buffer.from('c0', 'hex') + const one = Buffer.from('80', 'hex') + const two = Buffer.from('c0', 'hex') db.batch([ { type: 'put', key: one, value: one }, @@ -233,7 +233,7 @@ module.exports = function (leveljs, test, testCommon) { test('iterator stringifies buffer input', function (t) { t.plan(6) - var db = testCommon.factory() + const db = testCommon.factory() db.open(function (err) { t.ifError(err, 'no open error') @@ -258,8 +258,8 @@ module.exports = function (leveljs, test, testCommon) { // then create it again, then try and destroy it again. these avoid doing that test('test .destroy', function (t) { - var db = testCommon.factory() - var location = db.location + const db = testCommon.factory() + const location = db.location db.open(function (err) { t.notOk(err, 'no error') db.put('key', 'value', function (err) { @@ -271,7 +271,7 @@ module.exports = function (leveljs, test, testCommon) { t.notOk(err, 'no error') leveljs.destroy(location, function (err) { t.notOk(err, 'no error') - var db2 = leveljs(location) + const db2 = leveljs(location) db2.open(function (err) { t.notOk(err, 'no error') db2.get('key', { asBuffer: false }, function (err, value) { @@ -287,9 +287,9 @@ module.exports = function (leveljs, test, testCommon) { }) test('test .destroy and custom prefix', function (t) { - var prefix = 'custom-' - var db = testCommon.factory({ prefix: prefix }) - var location = db.location + const prefix = 'custom-' + const db = testCommon.factory({ prefix: prefix }) + const location = db.location db.open(function (err) { t.notOk(err, 'no error') @@ -302,7 +302,7 @@ module.exports = function (leveljs, test, testCommon) { t.notOk(err, 'no error') leveljs.destroy(location, prefix, function (err) { t.notOk(err, 'no error') - var db2 = leveljs(location, { prefix: prefix }) + const db2 = leveljs(location, { prefix: prefix }) db2.open(function (err) { t.notOk(err, 'no error') db2.get('key', { asBuffer: false }, function (err, value) { @@ -319,7 +319,7 @@ module.exports = function (leveljs, test, testCommon) { // TODO: move to abstract-leveldown test suite (and add to iterator tests too) test('clear() with lower key greater than upper key', function (t) { - var db = testCommon.factory() + const db = testCommon.factory() db.open(function (err) { t.ifError(err, 'no open error') diff --git a/test/index.js b/test/index.js index 7feb50e..230e25d 100644 --- a/test/index.js +++ b/test/index.js @@ -1,14 +1,14 @@ 'use strict' -var test = require('tape') -var uuid = require('uuid/v4') -var suite = require('abstract-leveldown/test') -var leveljs = require('..') +const test = require('tape') +const uuid = require('uuid/v4') +const suite = require('abstract-leveldown/test') +const leveljs = require('..') // Test feature detection require('./support-test')(leveljs, test) -var testCommon = suite.common({ +const testCommon = suite.common({ test: test, factory: function (opts) { return leveljs(uuid(), opts) diff --git a/test/support-test.js b/test/support-test.js index df514bb..2b37caf 100644 --- a/test/support-test.js +++ b/test/support-test.js @@ -1,9 +1,9 @@ 'use strict' -var support = require('../util/support') +const support = require('../util/support') -var pos = function () { } -var neg = function () { throw new Error() } +const pos = function () { } +const neg = function () { throw new Error() } module.exports = function (leveljs, test) { test('mock bufferKeys support', function (t) { diff --git a/test/upgrade-test.js b/test/upgrade-test.js index 8a15887..5049b47 100644 --- a/test/upgrade-test.js +++ b/test/upgrade-test.js @@ -1,19 +1,19 @@ 'use strict' -var concat = require('level-concat-iterator') +const concat = require('level-concat-iterator') module.exports = function (leveljs, test, testCommon) { test('upgrade', function (t) { - var db = testCommon.factory() + const db = testCommon.factory() - var input = [ + const input = [ { key: -1, value: 'a' }, { key: '0', value: ab('b') }, { key: '1', value: 1 }, { key: ab('2'), value: new Uint8Array(ab('2')) } ] - var output = [ + const output = [ { key: ab('-1'), value: new Uint8Array(ab('a')) }, { key: ab('0'), value: new Uint8Array(ab('b')) }, { key: ab('1'), value: new Uint8Array(ab('1')) }, @@ -46,7 +46,7 @@ module.exports = function (leveljs, test, testCommon) { }) function concatRaw (callback) { - var it = db.iterator() + const it = db.iterator() it._deserializeKey = it._deserializeValue = identity concat(it, callback) } diff --git a/util/clear.js b/util/clear.js index 3837d86..5efcf6f 100644 --- a/util/clear.js +++ b/util/clear.js @@ -3,9 +3,9 @@ module.exports = function clear (db, location, keyRange, options, callback) { if (options.limit === 0) return db._nextTick(callback) - var transaction = db.db.transaction([location], 'readwrite') - var store = transaction.objectStore(location) - var count = 0 + const transaction = db.db.transaction([location], 'readwrite') + const store = transaction.objectStore(location) + let count = 0 transaction.oncomplete = function () { callback() @@ -16,11 +16,11 @@ module.exports = function clear (db, location, keyRange, options, callback) { } // A key cursor is faster (skips reading values) but not supported by IE - var method = store.openKeyCursor ? 'openKeyCursor' : 'openCursor' - var direction = options.reverse ? 'prev' : 'next' + const method = store.openKeyCursor ? 'openKeyCursor' : 'openCursor' + const direction = options.reverse ? 'prev' : 'next' store[method](keyRange, direction).onsuccess = function (ev) { - var cursor = ev.target.result + const cursor = ev.target.result if (cursor) { // Wait for a request to complete before continuing, saving CPU. diff --git a/util/deserialize.js b/util/deserialize.js index f54c8f6..a79868a 100644 --- a/util/deserialize.js +++ b/util/deserialize.js @@ -1,9 +1,9 @@ 'use strict' -var Buffer = require('buffer').Buffer -var ta2str = (function () { +const Buffer = require('buffer').Buffer +const ta2str = (function () { if (global.TextDecoder) { - var decoder = new TextDecoder('utf-8') + const decoder = new TextDecoder('utf-8') return decoder.decode.bind(decoder) } else { return function ta2str (ta) { @@ -12,9 +12,9 @@ var ta2str = (function () { } })() -var ab2str = (function () { +const ab2str = (function () { if (global.TextDecoder) { - var decoder = new TextDecoder('utf-8') + const decoder = new TextDecoder('utf-8') return decoder.decode.bind(decoder) } else { return function ab2str (ab) { @@ -24,7 +24,7 @@ var ab2str = (function () { })() function ta2buf (ta) { - var buf = Buffer.from(ta.buffer) + const buf = Buffer.from(ta.buffer) if (ta.byteLength === ta.buffer.byteLength) { return buf diff --git a/util/key-range.js b/util/key-range.js index 74fac14..7526958 100644 --- a/util/key-range.js +++ b/util/key-range.js @@ -2,14 +2,14 @@ 'use strict' -var ltgt = require('ltgt') -var NONE = {} +const ltgt = require('ltgt') +const NONE = Symbol('none') module.exports = function createKeyRange (options) { - var lower = ltgt.lowerBound(options, NONE) - var upper = ltgt.upperBound(options, NONE) - var lowerOpen = ltgt.lowerBoundExclusive(options, NONE) - var upperOpen = ltgt.upperBoundExclusive(options, NONE) + const lower = ltgt.lowerBound(options, NONE) + const upper = ltgt.upperBound(options, NONE) + const lowerOpen = ltgt.lowerBoundExclusive(options, NONE) + const upperOpen = ltgt.upperBoundExclusive(options, NONE) if (lower !== NONE && upper !== NONE) { return IDBKeyRange.bound(lower, upper, lowerOpen, upperOpen) diff --git a/util/serialize.js b/util/serialize.js index 8fa0f0a..e2f7d1d 100644 --- a/util/serialize.js +++ b/util/serialize.js @@ -1,11 +1,11 @@ 'use strict' -var Buffer = require('buffer').Buffer +const Buffer = require('buffer').Buffer // Returns either a Uint8Array or Buffer (doesn't matter to // IndexedDB, because Buffer is a subclass of Uint8Array) -var str2bin = (function () { +const str2bin = (function () { if (global.TextEncoder) { - var encoder = new TextEncoder('utf-8') + const encoder = new TextEncoder('utf-8') return encoder.encode.bind(encoder) } else { return Buffer.from diff --git a/util/support.js b/util/support.js index 7b90211..d864e1b 100644 --- a/util/support.js +++ b/util/support.js @@ -1,6 +1,6 @@ 'use strict' -var Buffer = require('buffer').Buffer +const Buffer = require('buffer').Buffer exports.test = function (key) { return function test (impl) {