-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
index.js
304 lines (240 loc) · 7.2 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/* global indexedDB */
'use strict'
module.exports = Level
const AbstractLevelDOWN = require('abstract-leveldown').AbstractLevelDOWN
const inherits = require('inherits')
const parallel = require('run-parallel-limit')
const Iterator = require('./iterator')
const serialize = require('./util/serialize')
const deserialize = require('./util/deserialize')
const support = require('./util/support')
const clear = require('./util/clear')
const createKeyRange = require('./util/key-range')
const DEFAULT_PREFIX = 'level-js-'
function Level (location, opts) {
if (!(this instanceof Level)) return new Level(location, opts)
AbstractLevelDOWN.call(this, {
bufferKeys: support.bufferKeys(indexedDB),
snapshots: true,
permanence: true,
clear: true,
getMany: true
})
opts = opts || {}
if (typeof location !== 'string') {
throw new Error('constructor requires a location string argument')
}
this.location = location
this.prefix = opts.prefix == null ? DEFAULT_PREFIX : opts.prefix
this.version = parseInt(opts.version || 1, 10)
}
inherits(Level, AbstractLevelDOWN)
Level.prototype.type = 'level-js'
Level.prototype._open = function (options, callback) {
const req = indexedDB.open(this.prefix + this.location, this.version)
req.onerror = function () {
callback(req.error || new Error('unknown error'))
}
req.onsuccess = () => {
this.db = req.result
callback()
}
req.onupgradeneeded = (ev) => {
const db = ev.target.result
if (!db.objectStoreNames.contains(this.location)) {
db.createObjectStore(this.location)
}
}
}
Level.prototype.store = function (mode) {
const transaction = this.db.transaction([this.location], mode)
return transaction.objectStore(this.location)
}
Level.prototype.await = function (request, callback) {
const transaction = request.transaction
// Take advantage of the fact that a non-canceled request error aborts
// the transaction. I.e. no need to listen for "request.onerror".
transaction.onabort = function () {
callback(transaction.error || new Error('aborted by user'))
}
transaction.oncomplete = function () {
callback(null, request.result)
}
}
Level.prototype._get = function (key, options, callback) {
const store = this.store('readonly')
let req
try {
req = store.get(key)
} catch (err) {
return this._nextTick(callback, err)
}
this.await(req, function (err, value) {
if (err) return callback(err)
if (value === undefined) {
// 'NotFound' error, consistent with LevelDOWN API
return callback(new Error('NotFound'))
}
callback(null, deserialize(value, options.asBuffer))
})
}
Level.prototype._getMany = function (keys, options, callback) {
const asBuffer = options.asBuffer
const store = this.store('readonly')
const tasks = keys.map((key) => (next) => {
let request
try {
request = store.get(key)
} catch (err) {
return next(err)
}
request.onsuccess = () => {
const value = request.result
next(null, value === undefined ? value : deserialize(value, asBuffer))
}
request.onerror = (ev) => {
ev.stopPropagation()
next(request.error)
}
})
parallel(tasks, 16, callback)
}
Level.prototype._del = function (key, options, callback) {
const store = this.store('readwrite')
let req
try {
req = store.delete(key)
} catch (err) {
return this._nextTick(callback, err)
}
this.await(req, callback)
}
Level.prototype._put = function (key, value, options, callback) {
const store = this.store('readwrite')
let req
try {
// Will throw a DataError or DataCloneError if the environment
// does not support serializing the key or value respectively.
req = store.put(value, key)
} catch (err) {
return this._nextTick(callback, err)
}
this.await(req, callback)
}
Level.prototype._serializeKey = function (key) {
return serialize(key, this.supports.bufferKeys)
}
Level.prototype._serializeValue = function (value) {
return serialize(value, true)
}
Level.prototype._iterator = function (options) {
return new Iterator(this, this.location, options)
}
Level.prototype._batch = function (operations, options, callback) {
if (operations.length === 0) return this._nextTick(callback)
const store = this.store('readwrite')
const transaction = store.transaction
let index = 0
let error
transaction.onabort = function () {
callback(error || transaction.error || new Error('aborted by user'))
}
transaction.oncomplete = function () {
callback()
}
// Wait for a request to complete before making the next, saving CPU.
function loop () {
const op = operations[index++]
const key = op.key
let req
try {
req = op.type === 'del' ? store.delete(key) : store.put(op.value, key)
} catch (err) {
error = err
transaction.abort()
return
}
if (index < operations.length) {
req.onsuccess = loop
}
}
loop()
}
Level.prototype._clear = function (options, callback) {
let keyRange
let req
try {
keyRange = createKeyRange(options)
} catch (e) {
// The lower key is greater than the upper key.
// IndexedDB throws an error, but we'll just do nothing.
return this._nextTick(callback)
}
if (options.limit >= 0) {
// IDBObjectStore#delete(range) doesn't have such an option.
// Fall back to cursor-based implementation.
return clear(this, this.location, keyRange, options, callback)
}
try {
const store = this.store('readwrite')
req = keyRange ? store.delete(keyRange) : store.clear()
} catch (err) {
return this._nextTick(callback, err)
}
this.await(req, callback)
}
Level.prototype._close = function (callback) {
this.db.close()
this._nextTick(callback)
}
// NOTE: remove in a next major release
Level.prototype.upgrade = function (callback) {
if (this.status !== 'open') {
return this._nextTick(callback, new Error('cannot upgrade() before open()'))
}
const it = this.iterator()
const batchOptions = {}
const self = this
it._deserializeKey = it._deserializeValue = identity
next()
function next (err) {
if (err) return finish(err)
it.next(each)
}
function each (err, key, value) {
if (err || key === undefined) {
return finish(err)
}
const newKey = self._serializeKey(deserialize(key, true))
const newValue = self._serializeValue(deserialize(value, true))
// To bypass serialization on the old key, use _batch() instead of batch().
// NOTE: if we disable snapshotting (#86) this could lead to a loop of
// inserting and then iterating those same entries, because the new keys
// possibly sort after the old keys.
self._batch([
{ type: 'del', key: key },
{ type: 'put', key: newKey, value: newValue }
], batchOptions, next)
}
function finish (err) {
it.end(function (err2) {
callback(err || err2)
})
}
function identity (data) {
return data
}
}
Level.destroy = function (location, prefix, callback) {
if (typeof prefix === 'function') {
callback = prefix
prefix = DEFAULT_PREFIX
}
const request = indexedDB.deleteDatabase(prefix + location)
request.onsuccess = function () {
callback()
}
request.onerror = function (err) {
callback(err)
}
}