-
-
Notifications
You must be signed in to change notification settings - Fork 266
/
util.js
93 lines (80 loc) · 2.46 KB
/
util.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/* Copyright (c) 2012-2013 LevelUP contributors
* See list at <https://github.com/rvagg/node-levelup#contributing>
* MIT +no-false-attribs License
* <https://github.com/rvagg/node-levelup/blob/master/LICENSE>
*/
var extend = require('xtend')
, LevelUPError = require('./errors').LevelUPError
, encodings = require('./encodings')
, defaultOptions = {
createIfMissing : true
, errorIfExists : false
, keyEncoding : 'utf8'
, valueEncoding : 'utf8'
, compression : true
}
, leveldown
, encodingOpts = (function () {
var eo = {}
for(var e in encodings)
eo[e] = {valueEncoding: encodings[e]}
return eo
}())
function copy (srcdb, dstdb, callback) {
srcdb.readStream()
.pipe(dstdb.writeStream())
.on('close', callback ? callback : function () {})
.on('error', callback ? callback : function (err) { throw err })
}
function getOptions (levelup, options) {
var s = typeof options == 'string' // just an encoding
if (!s && options && options.encoding && !options.valueEncoding)
options.valueEncoding = options.encoding
return extend(
(levelup && levelup.options) || {}
, s ? encodingOpts[options] || encodingOpts[defaultOptions.valueEncoding]
: options
)
}
function getLevelDOWN () {
if (leveldown)
return leveldown
var requiredVersion = require('../package.json').devDependencies.leveldown
, missingLevelDOWNError = 'Could not locate LevelDOWN, try `npm install leveldown`'
, leveldownVersion
try {
leveldownVersion = require('leveldown/package').version
} catch (e) {
throw new LevelUPError(missingLevelDOWNError)
}
if (!require('semver').satisfies(leveldownVersion, requiredVersion)) {
throw new LevelUPError(
'Installed version of LevelDOWN ('
+ leveldownVersion
+ ') does not match required version ('
+ requiredVersion
+ ')'
)
}
try {
return leveldown = require('leveldown')
} catch (e) {
throw new LevelUPError(missingLevelDOWNError)
}
}
function dispatchError (levelup, error, callback) {
return typeof callback == 'function'
? callback(error)
: levelup.emit('error', error)
}
function isDefined (v) {
return typeof v !== 'undefined'
}
module.exports = {
defaultOptions : defaultOptions
, copy : copy
, getOptions : getOptions
, getLevelDOWN : getLevelDOWN
, dispatchError : dispatchError
, isDefined : isDefined
}