forked from localForage/localForage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdummyStorageDriver.js
468 lines (404 loc) · 14.4 KB
/
dummyStorageDriver.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
(function() {
'use strict';
var dummyStorage = {};
// Config the localStorage backend, using options set in the config.
function _initStorage(options) {
var self = this;
var dbInfo = {};
if (options) {
for (var i in options) {
dbInfo[i] = options[i];
}
}
dummyStorage[dbInfo.name] = dbInfo.db = {};
self._dbInfo = dbInfo;
return Promise.resolve();
}
var SERIALIZED_MARKER = '__lfsc__:';
var SERIALIZED_MARKER_LENGTH = SERIALIZED_MARKER.length;
// OMG the serializations!
var TYPE_ARRAYBUFFER = 'arbf';
var TYPE_BLOB = 'blob';
var TYPE_INT8ARRAY = 'si08';
var TYPE_UINT8ARRAY = 'ui08';
var TYPE_UINT8CLAMPEDARRAY = 'uic8';
var TYPE_INT16ARRAY = 'si16';
var TYPE_INT32ARRAY = 'si32';
var TYPE_UINT16ARRAY = 'ur16';
var TYPE_UINT32ARRAY = 'ui32';
var TYPE_FLOAT32ARRAY = 'fl32';
var TYPE_FLOAT64ARRAY = 'fl64';
var TYPE_SERIALIZED_MARKER_LENGTH =
SERIALIZED_MARKER_LENGTH + TYPE_ARRAYBUFFER.length;
function clear(callback) {
var self = this;
var promise = new Promise(function(resolve, reject) {
self
.ready()
.then(function() {
var db = self._dbInfo.db;
for (var key in db) {
if (db.hasOwnProperty(key)) {
delete db[key];
// db[key] = undefined;
}
}
resolve();
})
.catch(reject);
});
executeCallback(promise, callback);
return promise;
}
function getItem(key, callback) {
var self = this;
// Cast the key to a string, as that's all we can set as a key.
if (typeof key !== 'string') {
window.console.warn(
key + ' used as a key, but it is not a string.'
);
key = String(key);
}
var promise = new Promise(function(resolve, reject) {
self
.ready()
.then(function() {
try {
var db = self._dbInfo.db;
var result = db[key];
if (result) {
result = _deserialize(result);
}
resolve(result);
} catch (e) {
reject(e);
}
})
.catch(reject);
});
executeCallback(promise, callback);
return promise;
}
function iterate(callback) {
var self = this;
var promise = new Promise(function(resolve, reject) {
self
.ready()
.then(function() {
try {
var db = self._dbInfo.db;
for (var key in db) {
var result = db[key];
if (result) {
result = _deserialize(result);
}
callback(result, key);
}
resolve();
} catch (e) {
reject(e);
}
})
.catch(reject);
});
executeCallback(promise, callback);
return promise;
}
function key(n, callback) {
var self = this;
var promise = new Promise(function(resolve, reject) {
self
.ready()
.then(function() {
var db = self._dbInfo.db;
var result = null;
var index = 0;
for (var key in db) {
if (db.hasOwnProperty(key) && db[key] !== undefined) {
if (n === index) {
result = key;
break;
}
index++;
}
}
resolve(result);
})
.catch(reject);
});
executeCallback(promise, callback);
return promise;
}
function keys(callback) {
var self = this;
var promise = new Promise(function(resolve, reject) {
self
.ready()
.then(function() {
var db = self._dbInfo.db;
var keys = [];
for (var key in db) {
if (db.hasOwnProperty(key)) {
keys.push(key);
}
}
resolve(keys);
})
.catch(reject);
});
executeCallback(promise, callback);
return promise;
}
function length(callback) {
var self = this;
var promise = new Promise(function(resolve, reject) {
self
.keys()
.then(function(keys) {
resolve(keys.length);
})
.catch(reject);
});
executeCallback(promise, callback);
return promise;
}
function removeItem(key, callback) {
var self = this;
// Cast the key to a string, as that's all we can set as a key.
if (typeof key !== 'string') {
window.console.warn(
key + ' used as a key, but it is not a string.'
);
key = String(key);
}
var promise = new Promise(function(resolve, reject) {
self
.ready()
.then(function() {
var db = self._dbInfo.db;
if (db.hasOwnProperty(key)) {
delete db[key];
// db[key] = undefined;
}
resolve();
})
.catch(reject);
});
executeCallback(promise, callback);
return promise;
}
function setItem(key, value, callback) {
var self = this;
// Cast the key to a string, as that's all we can set as a key.
if (typeof key !== 'string') {
window.console.warn(
key + ' used as a key, but it is not a string.'
);
key = String(key);
}
var promise = new Promise(function(resolve, reject) {
self
.ready()
.then(function() {
// Convert undefined values to null.
// https://github.com/mozilla/localForage/pull/42
if (value === undefined) {
value = null;
}
// Save the original value to pass to the callback.
var originalValue = value;
_serialize(value, function(value, error) {
if (error) {
reject(error);
} else {
try {
var db = self._dbInfo.db;
db[key] = value;
resolve(originalValue);
} catch (e) {
reject(e);
}
}
});
})
.catch(reject);
});
executeCallback(promise, callback);
return promise;
}
// _serialize just like in LocalStorage
function _serialize(value, callback) {
var valueString = '';
if (value) {
valueString = value.toString();
}
// Cannot use `value instanceof ArrayBuffer` or such here, as these
// checks fail when running the tests using casper.js...
//
// TODO: See why those tests fail and use a better solution.
if (
value &&
(value.toString() === '[object ArrayBuffer]' ||
(value.buffer &&
value.buffer.toString() === '[object ArrayBuffer]'))
) {
// Convert binary arrays to a string and prefix the string with
// a special marker.
var buffer;
var marker = SERIALIZED_MARKER;
if (value instanceof ArrayBuffer) {
buffer = value;
marker += TYPE_ARRAYBUFFER;
} else {
buffer = value.buffer;
if (valueString === '[object Int8Array]') {
marker += TYPE_INT8ARRAY;
} else if (valueString === '[object Uint8Array]') {
marker += TYPE_UINT8ARRAY;
} else if (valueString === '[object Uint8ClampedArray]') {
marker += TYPE_UINT8CLAMPEDARRAY;
} else if (valueString === '[object Int16Array]') {
marker += TYPE_INT16ARRAY;
} else if (valueString === '[object Uint16Array]') {
marker += TYPE_UINT16ARRAY;
} else if (valueString === '[object Int32Array]') {
marker += TYPE_INT32ARRAY;
} else if (valueString === '[object Uint32Array]') {
marker += TYPE_UINT32ARRAY;
} else if (valueString === '[object Float32Array]') {
marker += TYPE_FLOAT32ARRAY;
} else if (valueString === '[object Float64Array]') {
marker += TYPE_FLOAT64ARRAY;
} else {
callback(new Error('Failed to get type for BinaryArray'));
}
}
callback(marker + _bufferToString(buffer));
} else if (valueString === '[object Blob]') {
// Conver the blob to a binaryArray and then to a string.
var fileReader = new FileReader();
fileReader.onload = function() {
var str = _bufferToString(this.result);
callback(SERIALIZED_MARKER + TYPE_BLOB + str);
};
fileReader.readAsArrayBuffer(value);
} else {
try {
callback(JSON.stringify(value));
} catch (e) {
window.console.error(
"Couldn't convert value into a JSON " + 'string: ',
value
);
callback(e);
}
}
}
// _deserialize just like in LocalStorage
function _deserialize(value) {
// If we haven't marked this string as being specially serialized (i.e.
// something other than serialized JSON), we can just return it and be
// done with it.
if (
value.substring(0, SERIALIZED_MARKER_LENGTH) !== SERIALIZED_MARKER
) {
return JSON.parse(value);
}
// The following code deals with deserializing some kind of Blob or
// TypedArray. First we separate out the type of data we're dealing
// with from the data itself.
var serializedString = value.substring(TYPE_SERIALIZED_MARKER_LENGTH);
var type = value.substring(
SERIALIZED_MARKER_LENGTH,
TYPE_SERIALIZED_MARKER_LENGTH
);
// Fill the string into a ArrayBuffer.
// 2 bytes for each char.
var buffer = new ArrayBuffer(serializedString.length * 2);
var bufferView = new Uint16Array(buffer);
for (var i = serializedString.length - 1; i >= 0; i--) {
bufferView[i] = serializedString.charCodeAt(i);
}
// Return the right type based on the code/type set during
// serialization.
switch (type) {
case TYPE_ARRAYBUFFER:
return buffer;
case TYPE_BLOB:
return new Blob([buffer]);
case TYPE_INT8ARRAY:
return new Int8Array(buffer);
case TYPE_UINT8ARRAY:
return new Uint8Array(buffer);
case TYPE_UINT8CLAMPEDARRAY:
return new Uint8ClampedArray(buffer);
case TYPE_INT16ARRAY:
return new Int16Array(buffer);
case TYPE_UINT16ARRAY:
return new Uint16Array(buffer);
case TYPE_INT32ARRAY:
return new Int32Array(buffer);
case TYPE_UINT32ARRAY:
return new Uint32Array(buffer);
case TYPE_FLOAT32ARRAY:
return new Float32Array(buffer);
case TYPE_FLOAT64ARRAY:
return new Float64Array(buffer);
default:
throw new Error('Unkown type: ' + type);
}
}
// _bufferToString just like in LocalStorage
function _bufferToString(buffer) {
var str = '';
var uint16Array = new Uint16Array(buffer);
try {
str = String.fromCharCode.apply(null, uint16Array);
} catch (e) {
// This is a fallback implementation in case the first one does
// not work. This is required to get the phantomjs passing...
for (var i = 0; i < uint16Array.length; i++) {
str += String.fromCharCode(uint16Array[i]);
}
}
return str;
}
function executeCallback(promise, callback) {
if (callback) {
promise.then(
function(result) {
callback(null, result);
},
function(error) {
callback(error);
}
);
}
}
var dummyStorageDriver = {
_driver: 'dummyStorageDriver',
_initStorage: _initStorage,
// _supports: function() { return true; }
iterate: iterate,
getItem: getItem,
setItem: setItem,
removeItem: removeItem,
clear: clear,
length: length,
key: key,
keys: keys
};
if (typeof define === 'function' && define.amd) {
define('dummyStorageDriver', function() {
return dummyStorageDriver;
});
} else if (
typeof module !== 'undefined' &&
module.exports &&
typeof require !== 'undefined'
) {
module.exports = dummyStorageDriver;
} else {
this.dummyStorageDriver = dummyStorageDriver;
}
}.call(window));