-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
137 lines (107 loc) · 3.17 KB
/
index.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
'use strict'
var AbstractLevelDOWN = require('abstract-leveldown').AbstractLevelDOWN
var AbstractChainedBatch = require('abstract-leveldown').AbstractChainedBatch
var AbstractIterator = require('abstract-leveldown').AbstractIterator
var inherits = require('inherits')
var debug = require('debug')
module.exports = DebugDown.default = DebugDown
function DebugDown (db, scope) {
if (!(this instanceof DebugDown)) return new DebugDown(db, scope)
AbstractLevelDOWN.call(this, '')
this.db = db
this.debug = wrap(debug(scope || 'debugdown'))
}
inherits(DebugDown, AbstractLevelDOWN)
DebugDown.prototype._serializeKey = function (key) {
this.debug('serializeKey', key)
return this.db._serializeKey(key)
}
DebugDown.prototype._serializeValue = function (value) {
this.debug('serializeValue', value)
return this.db._serializeValue(value)
}
DebugDown.prototype._open = function (options, cb) {
this.debug('open', options)
this.db.open(options, cb)
}
DebugDown.prototype._close = function (cb) {
this.debug('close')
this.db.close(cb)
}
DebugDown.prototype._put = function (key, value, options, cb) {
this.debug('put', key, value, options)
this.db.put(key, value, options, cb)
}
DebugDown.prototype._get = function (key, options, cb) {
this.debug('get', key, options)
this.db.get(key, options, cb)
}
DebugDown.prototype._del = function (key, options, cb) {
this.debug('del', key, options)
this.db.del(key, options, cb)
}
DebugDown.prototype._chainedBatch = function () {
this.debug('chainedBatch')
return new Batch(this)
}
DebugDown.prototype._batch = function (operations, options, cb) {
this.debug('batch', operations, options)
this.db.batch(operations, options, cb)
}
DebugDown.prototype._iterator = function (options) {
this.debug('iterator', options)
return new Iterator(this, options)
}
function Iterator (db, options) {
AbstractIterator.call(this, db)
this.debug = db.debug
this.it = db.db.iterator(options)
}
inherits(Iterator, AbstractIterator)
Iterator.prototype._next = function (cb) {
this.debug('iterator#next')
this.it.next(cb)
}
Iterator.prototype._end = function (cb) {
this.debug('iterator#end')
this.it.end(cb)
}
function Batch (db) {
AbstractChainedBatch.call(this, db)
this.debug = db.debug
this.batch = db.db.batch()
}
inherits(Batch, AbstractChainedBatch)
Batch.prototype._put = function (key, value) {
this.debug('batch#put', key, value)
this.batch.put(key, value)
}
Batch.prototype._del = function (key) {
this.debug('batch#del', key)
this.batch.del(key)
}
Batch.prototype._clear = function () {
this.debug('batch#clear')
this.batch.clear()
}
Batch.prototype._write = function (options, cb) {
// This will be fixed in abstract-leveldown@6
if (typeof options === 'function') {
cb = options
options = {}
}
this.debug('batch#write', options)
this.batch.write(options, cb)
}
function wrap (debug) {
return function () {
var args = Array.prototype.slice.call(arguments)
for (var i = 1; i < args.length; i++) {
var arg = args[i]
if (arg != null && (Array.isArray(arg) || typeof arg !== 'object')) {
args[i] = JSON.stringify(arg)
}
}
debug.apply(null, args)
}
}