Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

subleveldown

Package Overview
Dependencies
Maintainers
1
Versions
23
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

subleveldown - npm Package Compare versions

Comparing version 2.0.0 to 2.1.0

12

example.js

@@ -5,12 +5,12 @@ var sub = require('./')

var db = levelup('test', {db:memdown})
var db = levelup('test', {db: memdown})
var test = sub(db, 'test', {valueEncoding:'utf-8'})
var test2 = sub(test, 'tester', {valueEncoding:'utf-8'})
var test = sub(db, 'test', {valueEncoding: 'utf-8', separator: '@'})
var test2 = sub(test, 'tester', {valueEncoding: 'utf-8', separator: '@'})
test.put('hi', 'der')
test.put('hello', ['world'], function() {
test2.put('nested', 'value', function() {
test.put('hello', ['world'], function () {
test2.put('nested', 'value', function () {
db.createReadStream().on('data', console.log)
})
})
})
var subdown = require('./leveldown')
var levelup = require('levelup')
module.exports = function(db, prefix, opts) {
module.exports = function (db, prefix, opts) {
if (typeof prefix === 'object' && !opts) return module.exports(db, null, prefix)
if (!opts) opts = {}
opts.db = function() {
return subdown(db, prefix, opts.separator)
opts.db = function () {
return subdown(db, prefix, opts)
}
return levelup(opts)
}
}

@@ -7,4 +7,4 @@ var util = require('util')

var concat = function(prefix, key, force) {
if (typeof key === 'string' && (force || key.length)) return prefix+key
var concat = function (prefix, key, force) {
if (typeof key === 'string' && (force || key.length)) return prefix + key
if (Buffer.isBuffer(key) && (force || key.length)) return Buffer.concat([new Buffer(prefix), key])

@@ -14,3 +14,3 @@ return key

var SubIterator = function(ite, prefix) {
var SubIterator = function (ite, prefix) {
this.iterator = ite

@@ -20,5 +20,5 @@ this.prefix = prefix

SubIterator.prototype.next = function(cb) {
SubIterator.prototype.next = function (cb) {
var self = this
this.iterator.next(cb && function(err, key, value) {
this.iterator.next(cb && function (err, key, value) {
if (err) return cb(err)

@@ -30,17 +30,22 @@ if (key) key = key.slice(self.prefix.length)

SubIterator.prototype.end = function(cb) {
SubIterator.prototype.end = function (cb) {
this.iterator.end(cb)
}
var SubDown = function(db, prefix, separator) {
if (!(this instanceof SubDown)) return new SubDown(db, prefix, separator)
var SubDown = function (db, prefix, opts) {
if (!(this instanceof SubDown)) return new SubDown(db, prefix, opts)
if (typeof opts === 'string') opts = {separator: opts}
if (!opts) opts = {}
var separator = opts.separator
if (!prefix) prefix = ''
if (!separator) separator = '!'
if (prefix[0] === separator) prefix = prefix.slice(1)
if (prefix[prefix.length-1] === separator) prefix = prefix.slice(0, -1)
if (prefix[prefix.length - 1] === separator) prefix = prefix.slice(0, -1)
this.db = db
this.leveldown = null
this.prefix = separator+prefix+separator
this.prefix = separator + prefix + separator
this._beforeOpen = opts.open

@@ -50,6 +55,6 @@ var self = this

this._wrap = {
gt: function(x) {
gt: function (x) {
return concat(self.prefix, x || '', true)
},
lt: function(x) {
lt: function (x) {
if (Buffer.isBuffer(x) && !x.length) x = END

@@ -67,3 +72,5 @@ return concat(self.prefix, x || '\xff')

SubDown.prototype._open = function(opts, cb) {
SubDown.prototype._open = function (opts, cb) {
var self = this
if (this.db.isOpen()) {

@@ -76,25 +83,30 @@ if (this.db.db.type === 'subdown' && this.db.db.prefix) {

}
return cb()
return done()
}
this.db.on('open', this.open.bind(this, opts, cb))
this.db.on('open', this.open.bind(this, opts, done))
function done (err) {
if (err || !self._beforeOpen) return cb(err)
self._beforeOpen(cb)
}
}
SubDown.prototype.close = function() {
SubDown.prototype.close = function () {
this.leveldown.close.apply(this.leveldown, arguments)
}
SubDown.prototype.setDb = function() {
SubDown.prototype.setDb = function () {
this.leveldown.setDb.apply(this.leveldown, arguments)
}
SubDown.prototype.put = function(key, value, opts, cb) {
SubDown.prototype.put = function (key, value, opts, cb) {
this.leveldown.put(concat(this.prefix, key), value, opts, cb)
}
SubDown.prototype.get = function(key, opts, cb) {
SubDown.prototype.get = function (key, opts, cb) {
this.leveldown.get(concat(this.prefix, key), opts, cb)
}
SubDown.prototype.del = function(key, opts, cb) {
SubDown.prototype.del = function (key, opts, cb) {
this.leveldown.del(concat(this.prefix, key), opts, cb)

@@ -104,3 +116,3 @@ }

SubDown.prototype.batch =
SubDown.prototype._batch = function(operations, opts, cb) {
SubDown.prototype._batch = function (operations, opts, cb) {
if (arguments.length === 0) return new abstract.AbstractChainedBatch(this)

@@ -112,3 +124,3 @@ if (!Array.isArray(operations)) return this.leveldown.batch.apply(null, arguments)

var o = operations[i]
subops[i] = {type:o.type, key:concat(this.prefix, o.key), value:o.value}
subops[i] = {type: o.type, key: concat(this.prefix, o.key), value: o.value}
}

@@ -119,19 +131,19 @@

SubDown.prototype.approximateSize = function(start, end, cb) {
SubDown.prototype.approximateSize = function (start, end, cb) {
this.leveldown.approximateSize.apply(this.leveldown, arguments)
}
SubDown.prototype.getProperty = function() {
SubDown.prototype.getProperty = function () {
return this.leveldown.getProperty.apply(this.leveldown, arguments)
}
SubDown.prototype.destroy = function() {
SubDown.prototype.destroy = function () {
return this.leveldown.destroy.apply(this.leveldown, arguments)
}
SubDown.prototype.repair = function() {
SubDown.prototype.repair = function () {
return this.leveldown.repair.apply(this.leveldown, arguments)
}
var extend = function(xopts, opts) {
var extend = function (xopts, opts) {
xopts.keys = opts.keys

@@ -152,7 +164,7 @@ xopts.values = opts.values

var fixRange = function(opts) {
return (!opts.reverse || (!opts.end && !opts.start)) ? opts : {start:opts.end, end:opts.start}
var fixRange = function (opts) {
return (!opts.reverse || (!opts.end && !opts.start)) ? opts : {start: opts.end, end: opts.start}
}
SubDown.prototype.iterator = function(opts) {
SubDown.prototype.iterator = function (opts) {
if (!opts) opts = {}

@@ -163,2 +175,2 @@ var xopts = extend(wrap(fixRange(opts), this._wrap), opts)

module.exports = SubDown
module.exports = SubDown
{
"name": "subleveldown",
"version": "2.0.0",
"version": "2.1.0",
"description": "sublevels implemented using leveldowns",
"main": "index.js",
"dependencies": {
"abstract-leveldown": "^2.1.0",
"abstract-leveldown": "^2.4.1",
"level-option-wrap": "^1.1.0",
"levelup": "^0.19.0"
"levelup": "^1.2.1"
},
"devDependencies": {
"memdown": "^1.0.0",
"tape": "^3.0.3"
"memdown": "^1.1.0",
"standard": "^5.3.1",
"tape": "^4.2.2"
},

@@ -29,4 +30,4 @@ "repository": {

"scripts": {
"test": "node test"
"test": "standard && node test"
}
}
var dbidx = 0
, location = function () {
return '_leveldown_test_db_' + dbidx++
}
, lastLocation = function () {
return '_leveldown_test_db_' + dbidx
}
module.exports = {
location: location,
cleanup: cleanup,
lastLocation: lastLocation,
setUp: setUp,
tearDown: tearDown,
collectEntries: collectEntries,
makeExistingDbTest: makeExistingDbTest
}
, cleanup = function (callback) {
callback()
}
function location () {
return '_leveldown_test_db_' + dbidx++
}
, setUp = function (t) {
cleanup(function (err) {
t.notOk(err, 'cleanup returned an error')
t.end()
})
}
function lastLocation () {
return '_leveldown_test_db_' + dbidx
}
, tearDown = function (t) {
setUp(t) // same cleanup!
}
function cleanup (callback) {
callback()
}
, collectEntries = function (iterator, callback) {
var data = []
, next = function () {
iterator.next(function (err, key, value) {
if (err) return callback(err)
if (!arguments.length) {
return iterator.end(function (err) {
callback(err, data)
})
}
data.push({ key: key, value: value })
process.nextTick(next)
})
}
next()
}
function setUp (t) {
cleanup(function (err) {
t.notOk(err, 'cleanup returned an error')
t.end()
})
}
, makeExistingDbTest = function (name, test, leveldown, testFn) {
test(name, function (t) {
cleanup(function () {
var loc = location()
, db = leveldown(loc)
, done = function (close) {
if (close === false)
return cleanup(t.end.bind(t))
db.close(function (err) {
t.notOk(err, 'no error from close()')
cleanup(t.end.bind(t))
})
}
db.open(function (err) {
t.notOk(err, 'no error from open()')
db.batch(
[
{ type: 'put', key: 'one', value: '1' }
, { type: 'put', key: 'two', value: '2' }
, { type: 'put', key: 'three', value: '3' }
]
, function (err) {
t.notOk(err, 'no error from batch()')
testFn(db, t, done, loc)
}
)
})
function tearDown (t) {
setUp(t) // same cleanup!
}
function collectEntries (iterator, callback) {
var data = []
next()
function next () {
iterator.next(function (err, key, value) {
if (err) return callback(err)
if (!arguments.length) {
return iterator.end(function (err) {
callback(err, data)
})
}
data.push({ key: key, value: value })
process.nextTick(next)
})
}
}
function makeExistingDbTest (name, test, leveldown, testFn) {
test(name, function (t) {
cleanup(function () {
var loc = location()
var db = leveldown(loc)
db.open(function (err) {
t.notOk(err, 'no error from open()')
db.batch([
{type: 'put', key: 'one', value: '1'},
{type: 'put', key: 'two', value: '2'},
{type: 'put', key: 'three', value: '3'}
], function (err) {
t.notOk(err, 'no error from batch()')
testFn(db, t, done, loc)
})
})
}
module.exports = {
location : location
, cleanup : cleanup
, lastLocation : lastLocation
, setUp : setUp
, tearDown : tearDown
, collectEntries : collectEntries
, makeExistingDbTest : makeExistingDbTest
}
function done (close) {
if (close === false) return cleanup(t.end.bind(t))
db.close(function (err) {
t.notOk(err, 'no error from close()')
cleanup(t.end.bind(t))
})
}
})
})
}

@@ -8,8 +8,2 @@ var test = require('tape')

var down = function(loc) {
return subdown(levelup(loc, {db:memdown}), 'test')
}
/*** compatibility with basic LevelDOWN API ***/
require('abstract-leveldown/abstract/open-test').args(down, test, testCommon)

@@ -25,2 +19,6 @@ require('abstract-leveldown/abstract/open-test').open(down, test, testCommon)

require('abstract-leveldown/abstract/iterator-test').all(down, test, testCommon)
require('abstract-leveldown/abstract/ranges-test').all(down, test, testCommon)
require('abstract-leveldown/abstract/ranges-test').all(down, test, testCommon)
function down (loc) {
return subdown(levelup(loc, {db: memdown}), 'test')
}
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc