From 9e84881f369feb5dc8df465818ede71457b8996c Mon Sep 17 00:00:00 2001 From: Vincent Weevers Date: Sat, 14 Sep 2019 13:16:05 +0200 Subject: [PATCH] Use reachdown package (Level/community#82) --- leveldown.js | 21 +++++---------------- matchdown.js | 8 ++++++++ package.json | 3 ++- 3 files changed, 15 insertions(+), 17 deletions(-) create mode 100644 matchdown.js diff --git a/leveldown.js b/leveldown.js index a07988e..cffa5c2 100644 --- a/leveldown.js +++ b/leveldown.js @@ -1,6 +1,8 @@ var inherits = require('inherits') var abstract = require('abstract-leveldown') var wrap = require('level-option-wrap') +var reachdown = require('reachdown') +var matchdown = require('./matchdown') var rangeOptions = 'start end gt gte lt lte'.split(' ') var defaultClear = abstract.AbstractLevelDOWN.prototype._clear @@ -83,13 +85,13 @@ SubDown.prototype._open = function (opts, cb) { this.db.open(function (err) { if (err) return cb(err) - var subdb = down(self.db, 'subleveldown') + var subdb = reachdown(self.db, 'subleveldown') if (subdb && subdb.prefix) { self.prefix = subdb.prefix + self.prefix - self.leveldown = down(subdb.db) + self.leveldown = reachdown(subdb.db, matchdown, false) } else { - self.leveldown = down(self.db) + self.leveldown = reachdown(self.db, matchdown, false) } if (self._beforeOpen) self._beforeOpen(cb) @@ -179,16 +181,3 @@ SubDown.prototype._iterator = function (opts) { } module.exports = SubDown - -function down (db, type) { - if (typeof db.down === 'function') return db.down(type) - if (type && db.type === type) return db - if (isLooseAbstract(db.db)) return down(db.db, type) - if (isLooseAbstract(db._db)) return down(db._db, type) - return type ? null : db -} - -function isLooseAbstract (db) { - if (!db || typeof db !== 'object') { return false } - return typeof db._batch === 'function' && typeof db._iterator === 'function' -} diff --git a/matchdown.js b/matchdown.js new file mode 100644 index 0000000..089ce39 --- /dev/null +++ b/matchdown.js @@ -0,0 +1,8 @@ +module.exports = function matchdown (db, type) { + // Skip layers that we handle ourselves + if (type === 'levelup') return false + if (type === 'encoding-down') return false + if (type === 'deferred-leveldown') return false + + return true +} diff --git a/package.json b/package.json index 4f18f5d..dffbb46 100644 --- a/package.json +++ b/package.json @@ -20,7 +20,8 @@ "encoding-down": "^6.2.0", "inherits": "^2.0.3", "level-option-wrap": "^1.1.0", - "levelup": "^4.2.0" + "levelup": "^4.2.0", + "reachdown": "^1.0.0" }, "devDependencies": { "after": "^0.8.2",