This repository was archived by the owner on Dec 2, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathindex.js
More file actions
191 lines (153 loc) · 5.08 KB
/
index.js
File metadata and controls
191 lines (153 loc) · 5.08 KB
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
/* global indexedDB */
'use strict'
module.exports = Level
var AbstractLevelDOWN = require('abstract-leveldown').AbstractLevelDOWN
var util = require('util')
var Iterator = require('./iterator')
var mixedToBuffer = require('./util/mixed-to-buffer')
var isDataCloneError = require('./util/is-data-clone-error')
var setImmediate = require('./util/immediate')
var support = require('./util/support')
var DEFAULT_PREFIX = 'level-js-'
function Level (location, opts) {
if (!(this instanceof Level)) return new Level(location, opts)
AbstractLevelDOWN.call(this, location)
opts = opts || {}
this.prefix = opts.prefix || DEFAULT_PREFIX
this.version = parseInt(opts.version || 1, 10)
}
util.inherits(Level, AbstractLevelDOWN)
// Detect binary and array key support (IndexedDB Second Edition)
Level.binaryKeys = support.binaryKeys(indexedDB)
Level.arrayKeys = support.arrayKeys(indexedDB)
Level.prototype._open = function (options, callback) {
var req = indexedDB.open(this.prefix + this.location, this.version)
var self = this
req.onerror = function () {
callback(req.error || new Error('unknown error'))
}
req.onsuccess = function () {
self.db = req.result
callback()
}
req.onupgradeneeded = function (ev) {
var db = ev.target.result
if (!db.objectStoreNames.contains(self.location)) {
db.createObjectStore(self.location)
}
}
}
Level.prototype.store = function (mode) {
var transaction = this.db.transaction([this.location], mode)
return transaction.objectStore(this.location)
}
Level.prototype.await = function (request, callback) {
var 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) {
this.await(this.store('readonly').get(key), function (err, value) {
if (err) return callback(err)
if (value === undefined) {
// 'NotFound' error, consistent with LevelDOWN API
return callback(new Error('NotFound'))
}
if (options.asBuffer) {
value = mixedToBuffer(value)
}
callback(null, value)
})
}
Level.prototype._del = function (key, options, callback) {
this.await(this.store('readwrite').delete(key), callback)
}
Level.prototype._put = function (key, value, options, callback) {
var store = this.store('readwrite')
try {
// Will throw a DataCloneError if the environment
// does not support serializing the key or value.
var req = store.put(value, key)
} catch (err) {
if (!isDataCloneError(err)) {
throw err
}
return setImmediate(function () {
callback(err)
})
}
this.await(req, callback)
}
// Valid key types in IndexedDB Second Edition:
//
// - Number, except NaN. Includes Infinity and -Infinity
// - Date, except invalid (NaN)
// - String
// - ArrayBuffer or a view thereof (typed arrays). In level-js we also support
// Buffer (which is an Uint8Array) (and the primary binary type of Level).
// - Array, except cyclical and empty (e.g. Array(10)). Elements must be valid
// types themselves.
Level.prototype._serializeKey = function (key) {
if (Buffer.isBuffer(key)) {
return Level.binaryKeys ? key : key.toString()
} else if (Array.isArray(key)) {
return Level.arrayKeys ? key.map(this._serializeKey, this) : String(key)
} else if (typeof key === 'boolean' || (typeof key === 'number' && isNaN(key))) {
// These types are invalid per the IndexedDB spec and ideally we'd treat
// them that way, but they're valid per the current abstract test suite.
return String(key)
} else {
return key
}
}
Level.prototype._serializeValue = function (value) {
return value == null ? '' : value
}
Level.prototype._iterator = function (options) {
return new Iterator(this.db, this.location, options)
}
Level.prototype._batch = function (operations, options, callback) {
if (operations.length === 0) return setImmediate(callback)
var store = this.store('readwrite')
var transaction = store.transaction
var index = 0
transaction.onabort = function () {
callback(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 () {
var op = operations[index++]
var key = op.key
var req = op.type === 'del' ? store.delete(key) : store.put(op.value, key)
if (index < operations.length) {
req.onsuccess = loop
}
}
loop()
}
Level.prototype._close = function (callback) {
this.db.close()
setImmediate(callback)
}
Level.destroy = function (location, prefix, callback) {
if (typeof prefix === 'function') {
callback = prefix
prefix = DEFAULT_PREFIX
}
var request = indexedDB.deleteDatabase(prefix + location)
request.onsuccess = function () {
callback()
}
request.onerror = function (err) {
callback(err)
}
}