Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add manifest #93

Merged
merged 5 commits into from
Oct 13, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
58 changes: 52 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,55 @@ var AbstractIterator = require('abstract-leveldown').AbstractIterator
var inherits = require('inherits')
var Codec = require('level-codec')
var EncodingError = require('level-errors').EncodingError
var specialMethods = ['approximateSize', 'compactRange']

module.exports = DB.default = DB

function DB (db, opts) {
if (!(this instanceof DB)) return new DB(db, opts)
AbstractLevelDOWN.call(this, '')
if (db.supports && db.supports.encodings) throw new Error('Double encoding')

AbstractLevelDOWN.call(this, db.supports)
vweevers marked this conversation as resolved.
Show resolved Hide resolved
this.supports.encodings = true

// TODO (future major): remove this fallback
specialMethods.forEach(function (m) {
if (typeof db[m] === 'function' && !this.supports.additionalMethods[m]) {
this.supports.additionalMethods[m] = {
args: [{ type: 'key' }, { type: 'key' }, { type: 'options' }]
vweevers marked this conversation as resolved.
Show resolved Hide resolved
}
}
}, this)
vweevers marked this conversation as resolved.
Show resolved Hide resolved

opts = opts || {}
if (typeof opts.keyEncoding === 'undefined') opts.keyEncoding = 'utf8'
if (typeof opts.valueEncoding === 'undefined') opts.valueEncoding = 'utf8'

this.db = db
this.codec = new Codec(opts)

Object.keys(this.supports.additionalMethods).forEach(function (m) {
if (this[m] != null) return

var signature = this.supports.additionalMethods[m]
var args = (signature && signature.args) || []

this[m] = function () {
var i = Math.min(arguments.length, args.length)
var opts

while (i--) {
if (args[i].type === 'options' && !opts && isOptions(arguments[i])) {
opts = arguments[i]
arguments[i] = this.codec.encodeLtgt(opts)
} else if (args[i].type === 'key') {
arguments[i] = this.codec.encodeKey(arguments[i], opts)
}
}

return this.db[m].apply(this.db, arguments)
}
}, this)
vweevers marked this conversation as resolved.
Show resolved Hide resolved
}

inherits(DB, AbstractLevelDOWN)
Expand Down Expand Up @@ -84,11 +120,17 @@ DB.prototype._clear = function (opts, callback) {
this.db.clear(opts, callback)
}

DB.prototype.approximateSize = function (start, end, opts, cb) {
start = this.codec.encodeKey(start, opts)
end = this.codec.encodeKey(end, opts)
return this.db.approximateSize(start, end, opts, cb)
}
// DB.prototype.approximateSize = function (start, end, opts, cb) {
// start = this.codec.encodeKey(start, opts)
// end = this.codec.encodeKey(end, opts)
// return this.db.approximateSize(start, end, opts, cb)
// }
//
// DB.prototype.compactRange = function (start, end, opts, cb) {
// start = this.codec.encodeKey(start, opts)
// end = this.codec.encodeKey(end, opts)
// return this.db.compactRange(start, end, opts, cb)
// }

function Iterator (db, opts) {
AbstractIterator.call(this, db)
Expand Down Expand Up @@ -159,3 +201,7 @@ Batch.prototype._clear = function () {
Batch.prototype._write = function (opts, cb) {
this.batch.write(opts, cb)
}

function isOptions (o) {
return typeof o === 'object' && o !== null
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"prepublishOnly": "npm run dependency-check"
},
"dependencies": {
"abstract-leveldown": "^6.1.1",
"abstract-leveldown": "^6.2.1",
"inherits": "^2.0.3",
"level-codec": "^9.0.0",
"level-errors": "^2.0.0"
Expand Down
32 changes: 31 additions & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,7 @@ test('iterator catches decoding error from valueEncoding', function (t) {
})
})

test('approximateSize() encodes start and end', function (t) {
test('encodes start and end of approximateSize()', function (t) {
t.plan(2)

var down = {
Expand All @@ -592,6 +592,36 @@ test('approximateSize() encodes start and end', function (t) {
encdown(down).approximateSize(1, 2, noop)
})

test('encodes start and end of approximateSize() with custom encoding', function (t) {
t.plan(2)

var down = {
approximateSize: function (start, end) {
t.is(start, '"a"')
t.is(end, '"b"')
}
}

encdown(down).approximateSize('a', 'b', { keyEncoding: 'json' }, noop)
})

test('encodes options of additionalMethod', function (t) {
t.plan(1)

var down = {
supports: {
additionalMethods: {
foo: { args: [{ type: 'options' }] }
}
},
foo: function (options) {
t.is(options.gt, '"a"')
}
}

encdown(down).foo({ gt: 'a', keyEncoding: 'json' })
})

test('encodes seek target', function (t) {
t.plan(1)

Expand Down