forked from pubkey/rxdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocalstorage.js
131 lines (116 loc) · 3.77 KB
/
localstorage.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
'use strict';
// ArrayBuffer/Uint8Array are old formats that date back to before we
// had a proper browserified buffer type. they may be removed later
var arrayBuffPrefix = 'ArrayBuffer:';
var arrayBuffRegex = new RegExp('^' + arrayBuffPrefix);
var uintPrefix = 'Uint8Array:';
var uintRegex = new RegExp('^' + uintPrefix);
// this is the new encoding format used going forward
var bufferPrefix = 'Buff:';
var bufferRegex = new RegExp('^' + bufferPrefix);
var utils = require('./utils');
var LocalStorageCore = require('./localstorage-core');
var TaskQueue = require('./taskqueue');
var d64 = require('d64');
function LocalStorage(dbname) {
this._store = new LocalStorageCore(dbname);
this._queue = new TaskQueue();
}
LocalStorage.prototype.sequentialize = function (callback, fun) {
this._queue.add(fun, callback);
};
LocalStorage.prototype.init = function (callback) {
var self = this;
self.sequentialize(callback, function (callback) {
self._store.getKeys(function (err, keys) {
if (err) {
return callback(err);
}
self._keys = keys;
return callback();
});
});
};
LocalStorage.prototype.keys = function (callback) {
var self = this;
// RxDB-MonkeyPatch
self.sequentialize(callback, function (callback) {
self._store.getKeys(function (err, keys) {
callback(null, keys.slice());
});
});
};
//setItem: Saves and item at the key provided.
LocalStorage.prototype.setItem = function (key, value, callback) {
var self = this;
self.sequentialize(callback, function (callback) {
if (Buffer.isBuffer(value)) {
value = bufferPrefix + d64.encode(value);
}
var idx = utils.sortedIndexOf(self._keys, key);
if (self._keys[idx] !== key) {
self._keys.splice(idx, 0, key);
}
self._store.put(key, value, callback);
});
};
//getItem: Returns the item identified by it's key.
LocalStorage.prototype.getItem = function (key, callback) {
var self = this;
self.sequentialize(callback, function (callback) {
self._store.get(key, function (err, retval) {
if (err) {
return callback(err);
}
if (typeof retval === 'undefined' || retval === null) {
// 'NotFound' error, consistent with LevelDOWN API
return callback(new Error('NotFound'));
}
if (typeof retval !== 'undefined') {
if (bufferRegex.test(retval)) {
retval = d64.decode(retval.substring(bufferPrefix.length));
} else if (arrayBuffRegex.test(retval)) {
// this type is kept for backwards
// compatibility with older databases, but may be removed
// after a major version bump
retval = retval.substring(arrayBuffPrefix.length);
retval = new ArrayBuffer(atob(retval).split('').map(function (c) {
return c.charCodeAt(0);
}));
} else if (uintRegex.test(retval)) {
// ditto
retval = retval.substring(uintPrefix.length);
retval = new Uint8Array(atob(retval).split('').map(function (c) {
return c.charCodeAt(0);
}));
}
}
callback(null, retval);
});
});
};
//removeItem: Removes the item identified by it's key.
LocalStorage.prototype.removeItem = function (key, callback) {
var self = this;
self.sequentialize(callback, function (callback) {
var idx = utils.sortedIndexOf(self._keys, key);
if (self._keys[idx] === key) {
self._keys.splice(idx, 1);
self._store.remove(key, function (err) {
if (err) {
return callback(err);
}
callback();
});
} else {
callback();
}
});
};
LocalStorage.prototype.length = function (callback) {
var self = this;
self.sequentialize(callback, function (callback) {
callback(null, self._keys.length);
});
};
exports.LocalStorage = LocalStorage;