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

levelup

Package Overview
Dependencies
Maintainers
1
Versions
101
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

levelup - npm Package Compare versions

Comparing version 0.6.2 to 0.7.0-b00

externs_example.js

356

lib/levelup.js

@@ -7,23 +7,50 @@ /* Copyright (c) 2012-2013 LevelUP contributors

var leveldown = require('leveldown')
, EventEmitter = require('events').EventEmitter
, inherits = require('util').inherits
const leveldown = require('leveldown')
, EventEmitter = require('events').EventEmitter
, inherits = require('util').inherits
, extend = require('xtend')
, Externr = require('externr')
, errors = require('./errors')
, readStream = require('./read-stream')
, writeStream = require('./write-stream')
, extend = require('xtend')
, toEncoding = require('./util').toEncoding
, toSlice = require('./util').toSlice
, encodingOpts = require('./util').encodingOpts
, errors = require('./errors')
, ReadStream = require('./read-stream')
, writeStream = require('./write-stream')
, toEncoding = require('./util').toEncoding
, toSlice = require('./util').toSlice
, encodingOpts = require('./util').encodingOpts
, defaultOptions = {
createIfMissing : true
, errorIfExists : false
, encoding : 'utf8'
, keyEncoding : null
, valueEncoding : null
, compression : true
}
, externDefinitions = {
wrap: [
// sync methods
'constructor'
, 'isOpen'
, 'isClosed'
, 'createReadStream'
// async methods
, 'open'
, 'close'
, 'put'
, 'get'
, 'del'
, 'batch'
, 'approximateSize'
]
, extend: [
'inKey'
, 'inValue'
]
, extendReverse: [
'outKey'
, 'outValue'
]
}
, defaultOptions = {
createIfMissing : true
, errorIfExists : false
, encoding : 'utf8'
, keyEncoding : null
, valueEncoding : null
, compression : true
}
, createLevelUP = function (location, options, callback) {

@@ -52,2 +79,3 @@

}
, externs = Externr(externDefinitions)

@@ -82,9 +110,17 @@ , getCallback = function (options, callback) {

this._options = extend(defaultOptions, options)
Object.defineProperty(this, 'location', {
value: location
, configurable: false
, enumerable: true
, writable: false
})
externs.$register(options && options.use)
externs.constructor(
this
, [ extend(defaultOptions, options) ]
, function (options) {
this._options = options
Object.defineProperty(this, 'location', {
value: location
, configurable: false
, enumerable: true
, writable: false
})
}
)
}

@@ -102,23 +138,30 @@

if (isOpening())
return callback && this.once('open', callback.bind(null, null, this))
return callback && levelup.once(
'open'
, callback.bind(null, null, this)
)
status = 'opening'
var execute = function () {
var db = leveldown(this.location)
externs.open(
this
, [ callback ]
, function (callback) {
var db = leveldown(this.location)
db.open(this._options, function (err) {
if (err) {
err = new errors.OpenError(err)
return dispatchError(err, callback)
} else {
this._db = db
status = 'open'
if (callback)
callback(null, this)
this.emit('open')
this.emit('ready')
db.open(this._options, function (err) {
if (err) {
err = new errors.OpenError(err)
return dispatchError(err, callback)
} else {
levelup._db = db
status = 'open'
if (callback)
callback(null, levelup)
levelup.emit('open')
levelup.emit('ready')
}
})
}
}.bind(this))
}.bind(this)
)

@@ -131,12 +174,11 @@ var deferred = {}

var args = Array.prototype.slice.call(arguments)
this.once('ready', function () {
this._db[name].apply(this._db, args)
levelup.once('ready', function () {
levelup._db[name].apply(levelup._db, args)
})
}.bind(this)
}.bind(this))
}
})
this._db = deferred
execute()
this.emit('opening')
levelup.emit('opening')
}

@@ -147,17 +189,23 @@

status = 'closing'
this._db.close(function () {
status = 'closed'
this.emit('closed')
if (callback)
callback.apply(null, arguments)
}.bind(this))
this.emit('closing')
this._db = null
externs.close(
this
, [ callback ]
, function (callback) {
this._db.close(function () {
status = 'closed'
this.emit('closed')
if (callback)
callback.apply(null, arguments)
}.bind(this))
this.emit('closing')
this._db = null
}
)
} else if (status == 'closed' && callback) {
callback()
} else if (status == 'closing' && callback) {
this.once('closed', callback)
levelup.once('closed', callback)
} else if (isOpening()) {
this.once('open', function () {
this.close(callback)
levelup.once('open', function () {
levelup.close(callback)
})

@@ -167,8 +215,25 @@ }

LevelUP.prototype.isOpen = function () { return isOpen() }
LevelUP.prototype.isOpen = function () {
return externs.isOpen(
this
, []
, function () {
return isOpen()
}
)
}
LevelUP.prototype.isClosed = function () { return (/^clos/).test(status) }
LevelUP.prototype.isClosed = function () {
return externs.isOpen(
this
, []
, function () {
return (/^clos/).test(status)
}
)
}
LevelUP.prototype.get = function (key_, options, callback) {
var key
, keyEnc
, valueEnc

@@ -184,16 +249,23 @@ , err

options = getOptions(options)
key = toSlice[keyEncoding(options)](key_)
valueEnc = valueEncoding(options)
options = getOptions(options, this._options)
keyEnc = options.keyEncoding || options.encoding
valueEnc = options.valueEncoding || options.encoding
key = externs.inKey(toSlice[keyEnc](key_))
options.asBuffer = valueEnc != 'utf8' && valueEnc != 'json'
this._db.get(key, options, function (err, value) {
if (err) {
err = new errors.NotFoundError(
'Key not found in database [' + key_ + ']')
return dispatchError(err, callback)
}
if (callback)
callback(null, toEncoding[valueEnc](value))
})
externs.get(
this
, [ key, options, valueEnc, callback ]
, function (key, options, valueEnc, callback) {
this._db.get(key, options, function (err, value) {
if (err) {
err = new errors.NotFoundError(
'Key not found in database [' + key_ + ']')
return dispatchError(err, callback)
}
if (callback)
callback(null, externs.outValue(toEncoding[valueEnc](value)))
})
}
)
}

@@ -213,16 +285,22 @@

options = getOptions(options)
key = toSlice[keyEncoding(options)](key_)
value = toSlice[valueEncoding(options)](value_)
options = getOptions(options, this._options)
key = externs.inKey(toSlice[options.keyEncoding || options.encoding](key_))
value = externs.inValue(toSlice[options.valueEncoding || options.encoding](value_))
this._db.put(key, value, options, function (err) {
if (err) {
err = new errors.WriteError(err)
return dispatchError(err, callback)
} else {
this.emit('put', key_, value_)
if (callback)
callback()
}
}.bind(this))
externs.put(
this
, [ key, value, options, callback ]
, function (key, value, options, callback) {
this._db.put(key, value, options, function (err) {
if (err) {
err = new errors.WriteError(err)
return dispatchError(err, callback)
} else {
this.emit('put', key_, value_)
if (callback)
callback()
}
}.bind(this))
}
)
}

@@ -241,15 +319,21 @@

options = getOptions(options)
key = toSlice[keyEncoding(options)](key_)
options = getOptions(options, this._options)
key = externs.inKey(toSlice[options.keyEncoding || options.encoding](key_))
this._db.del(key, options, function (err) {
if (err) {
err = new errors.WriteError(err)
return dispatchError(err, callback)
} else {
this.emit('del', key_)
if (callback)
callback()
}
}.bind(this))
externs.get(
this
, [ key, options, callback ]
, function (key, options, callback) {
this._db.del(key, options, function (err) {
if (err) {
err = new errors.WriteError(err)
return dispatchError(err, callback)
} else {
this.emit('del', key_)
if (callback)
callback()
}
}.bind(this))
}
)
}

@@ -282,6 +366,9 @@

if (e.type !== undefined && e.key !== undefined) {
var o = { type: e.type, key: toSlice[keyEnc](e.key) }
var o = {
type: e.type
, key: externs.inKey(toSlice[keyEnc](e.key))
}
if (e.value !== undefined)
o.value = toSlice[valueEnc](e.value)
o.value = externs.inValue(toSlice[valueEnc](e.value))

@@ -296,12 +383,18 @@ return o

this._db.batch(arr, options, function (err) {
if (err) {
err = new errors.WriteError(err)
return dispatchError(err, callback)
} else {
this.emit('batch', arr_)
if (callback)
callback()
}
}.bind(this))
externs.batch(
this
, [ arr, options, callback ]
, function (arr, options, callback) {
this._db.batch(arr, options, function (err) {
if (err) {
err = new errors.WriteError(err)
return dispatchError(err, callback)
} else {
this.emit('batch', arr_)
if (callback)
callback()
}
}.bind(this))
}
)
}

@@ -317,9 +410,15 @@

this._db.approximateSize(start, end, function(err, size) {
if (err) {
err = new errors.OpenError(err)
return dispatchError(err, callback)
} else if (callback)
callback(null, size)
}.bind(this))
externs.approximateSize(
this
, [ start, end, callback ]
, function (start, end, callback) {
this._db.approximateSize(start, end, function(err, size) {
if (err) {
err = new errors.OpenError(err)
return dispatchError(err, callback)
} else if (callback)
callback(null, size)
}.bind(this))
}
)
}

@@ -329,10 +428,21 @@

LevelUP.prototype.createReadStream = function (options) {
options = extend(this._options, options)
return readStream.create(
options
, this
, function (options) {
return this._db.iterator(options)
}.bind(this)
options = extend(
extend({}, this._options)
, typeof options == 'object' ? options : {}
)
return externs.createReadStream(
this
, [ ReadStream, options ]
, function (ReadStream, options) {
return ReadStream.create(
options
, this
, externs
, function (options) {
return this._db.iterator(options)
}.bind(this)
)
}
)
}

@@ -339,0 +449,0 @@

@@ -18,17 +18,17 @@ /* Copyright (c) 2012-2013 LevelUP contributors

, makeKeyValueData = function (key, value) {
, makeKeyValueData = function (externs, key, value) {
return {
key: toEncoding[this._keyEncoding](key)
, value: toEncoding[this._valueEncoding](value)
key: externs.outKey(toEncoding[this._keyEncoding](key))
, value: externs.outValue(toEncoding[this._valueEncoding](value))
}
}
, makeKeyData = function (key) {
return toEncoding[this._keyEncoding](key)
, makeKeyData = function (externs, key) {
return externs.outKey(toEncoding[this._keyEncoding](key))
}
, makeValueData = function (key, value) {
return toEncoding[this._valueEncoding](value)
, makeValueData = function (externs, key, value) {
return externs.outValue(toEncoding[this._valueEncoding](value))
}
, makeNoData = function () { return null }
function ReadStream (options, db, iteratorFactory) {
function ReadStream (options, db, externs, iteratorFactory) {
Stream.call(this)

@@ -42,3 +42,4 @@

// purely to keep `db` around until we're done so it's not GCed if the user doesn't keep a ref
// purely to keep `db` around until we're done so it's not
// GCed if the user doesn't keep a ref
this._db = db

@@ -50,14 +51,21 @@

if (typeof this._options.start != 'undefined')
this._options.start = toSlice[this._keyEncoding](this._options.start)
this._options.start =
externs.inKey(toSlice[this._keyEncoding](this._options.start))
if (typeof this._options.end != 'undefined')
this._options.end = toSlice[this._keyEncoding](this._options.end)
this._options.end =
externs.inKey(toSlice[this._keyEncoding](this._options.end))
if (typeof this._options.limit != 'number')
this._options.limit = -1
this._options.keyAsBuffer = this._keyEncoding != 'utf8' && this._keyEncoding != 'json'
this._options.valueAsBuffer = this._valueEncoding != 'utf8' && this._valueEncoding != 'json'
this._options.keyAsBuffer =
this._keyEncoding != 'utf8' && this._keyEncoding != 'json'
this._options.valueAsBuffer =
this._valueEncoding != 'utf8' && this._valueEncoding != 'json'
this._makeData = this._options.keys && this._options.values
? makeKeyValueData.bind(this) : this._options.keys
? makeKeyData.bind(this) : this._options.values
? makeValueData.bind(this) : makeNoData
? makeKeyValueData.bind(this, externs)
: this._options.keys
? makeKeyData.bind(this, externs)
: this._options.values
? makeValueData.bind(this, externs)
: makeNoData

@@ -163,4 +171,4 @@

module.exports.create = function (options, db, iteratorFactory) {
return new ReadStream(options, db, iteratorFactory)
}
module.exports.create = function (options, db, externs, iteratorFactory) {
return new ReadStream(options, db, externs, iteratorFactory)
}

@@ -24,13 +24,17 @@ {

]
, "version" : "0.6.2"
, "version" : "0.7.0-b00"
, "main" : "lib/levelup.js"
, "dependencies" : {
"leveldown" : "~0.1.2"
, "errno" : "~0.0.3"
"errno" : "~0.0.3"
, "concat-stream" : "~0.1.1"
, "simple-bufferstream" : "~0.0.2"
, "xtend" : "~2.0.3"
, "externr" : "~0.0.1"
}
, "peerDependencies" : {
"leveldown" : "~0.2.0-b00"
}
, "devDependencies" : {
"buster" : "*"
"leveldown" : "~0.2.0-b00"
, "buster" : "*"
, "rimraf" : "*"

@@ -37,0 +41,0 @@ , "async" : "*"

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