-
-
Notifications
You must be signed in to change notification settings - Fork 191
/
polyfill.js
342 lines (296 loc) · 12.5 KB
/
polyfill.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
(function (idbModules) {
'use strict';
/**
* Polyfills missing features in the browser's native IndexedDB implementation.
* This is used for browsers that DON'T support WebSQL but DO support IndexedDB
*/
function polyfill() {
if (navigator.userAgent.match(/MSIE/) ||
navigator.userAgent.match(/Trident/) ||
navigator.userAgent.match(/Edge/)) {
// Internet Explorer's native IndexedDB does not support compound keys
compoundKeyPolyfill();
}
}
/**
* Polyfills support for compound keys
*/
function compoundKeyPolyfill() {
var cmp = IDBFactory.prototype.cmp;
var createObjectStore = IDBDatabase.prototype.createObjectStore;
var createIndex = IDBObjectStore.prototype.createIndex;
var add = IDBObjectStore.prototype.add;
var put = IDBObjectStore.prototype.put;
var indexGet = IDBIndex.prototype.get;
var indexGetKey = IDBIndex.prototype.getKey;
var indexCursor = IDBIndex.prototype.openCursor;
var indexKeyCursor = IDBIndex.prototype.openKeyCursor;
var storeGet = IDBObjectStore.prototype.get;
var storeDelete = IDBObjectStore.prototype.delete;
var storeCursor = IDBObjectStore.prototype.openCursor;
var storeKeyCursor = IDBObjectStore.prototype.openKeyCursor;
var bound = IDBKeyRange.bound;
var upperBound = IDBKeyRange.upperBound;
var lowerBound = IDBKeyRange.lowerBound;
var only = IDBKeyRange.only;
var requestResult = Object.getOwnPropertyDescriptor(IDBRequest.prototype, 'result');
var cursorPrimaryKey = Object.getOwnPropertyDescriptor(IDBCursor.prototype, 'primaryKey');
var cursorKey = Object.getOwnPropertyDescriptor(IDBCursor.prototype, 'key');
var cursorValue = Object.getOwnPropertyDescriptor(IDBCursorWithValue.prototype, 'value');
IDBFactory.prototype.cmp = function(key1, key2) {
var args = Array.prototype.slice.call(arguments);
if (key1 instanceof Array) {
args[0] = encodeCompoundKey(key1);
}
if (key2 instanceof Array) {
args[1] = encodeCompoundKey(key2);
}
return cmp.apply(this, args);
};
IDBDatabase.prototype.createObjectStore = function(name, opts) {
if (opts && opts.keyPath instanceof Array) {
opts.keyPath = encodeCompoundKeyPath(opts.keyPath);
}
return createObjectStore.apply(this, arguments);
};
IDBObjectStore.prototype.createIndex = function(name, keyPath, opts) {
var args = Array.prototype.slice.call(arguments);
if (keyPath instanceof Array) {
args[1] = encodeCompoundKeyPath(keyPath);
}
return createIndex.apply(this, args);
};
IDBObjectStore.prototype.add = function(value, key) {
return this.__insertData(add, arguments);
};
IDBObjectStore.prototype.put = function(value, key) {
return this.__insertData(put, arguments);
};
IDBObjectStore.prototype.__insertData = function(method, args) {
args = Array.prototype.slice.call(args);
var value = args[0];
var key = args[1];
// out-of-line key
if (key instanceof Array) {
args[1] = encodeCompoundKey(key);
}
if (typeof value === 'object') {
// inline key
if (isCompoundKey(this.keyPath)) {
setInlineCompoundKey(value, this.keyPath);
}
// inline indexes
for (var i = 0; i < this.indexNames.length; i++) {
var index = this.index(this.indexNames[i]);
if (isCompoundKey(index.keyPath)) {
try {
setInlineCompoundKey(value, index.keyPath);
}
catch (e) {
// The value doesn't have a valid key for this index.
}
}
}
}
return method.apply(this, args);
};
IDBIndex.prototype.get = function(key) {
var args = Array.prototype.slice.call(arguments);
if (key instanceof Array) {
args[0] = encodeCompoundKey(key);
}
return indexGet.apply(this, args);
};
IDBIndex.prototype.getKey = function(key) {
var args = Array.prototype.slice.call(arguments);
if (key instanceof Array) {
args[0] = encodeCompoundKey(key);
}
return indexGetKey.apply(this, args);
};
IDBIndex.prototype.openCursor = function(key) {
var args = Array.prototype.slice.call(arguments);
if (key instanceof Array) {
args[0] = encodeCompoundKey(key);
}
return indexCursor.apply(this, args);
};
IDBIndex.prototype.openKeyCursor = function(key) {
var args = Array.prototype.slice.call(arguments);
if (key instanceof Array) {
args[0] = encodeCompoundKey(key);
}
return indexKeyCursor.apply(this, args);
};
IDBObjectStore.prototype.get = function(key) {
var args = Array.prototype.slice.call(arguments);
if (key instanceof Array) {
args[0] = encodeCompoundKey(key);
}
return storeGet.apply(this, args);
};
IDBObjectStore.prototype.delete = function(key) {
var args = Array.prototype.slice.call(arguments);
if (key instanceof Array) {
args[0] = encodeCompoundKey(key);
}
return storeDelete.apply(this, args);
};
IDBObjectStore.prototype.openCursor = function(key) {
var args = Array.prototype.slice.call(arguments);
if (key instanceof Array) {
args[0] = encodeCompoundKey(key);
}
return storeCursor.apply(this, args);
};
IDBObjectStore.prototype.openKeyCursor = function(key) {
var args = Array.prototype.slice.call(arguments);
if (key instanceof Array) {
args[0] = encodeCompoundKey(key);
}
return storeKeyCursor.apply(this, args);
};
IDBKeyRange.bound = function(lower, upper, lowerOpen, upperOpen) {
var args = Array.prototype.slice.call(arguments);
if (lower instanceof Array) {
args[0] = encodeCompoundKey(lower);
}
if (upper instanceof Array) {
args[1] = encodeCompoundKey(upper);
}
return bound.apply(IDBKeyRange, args);
};
IDBKeyRange.upperBound = function(key, open) {
var args = Array.prototype.slice.call(arguments);
if (key instanceof Array) {
args[0] = encodeCompoundKey(key);
}
return upperBound.apply(IDBKeyRange, args);
};
IDBKeyRange.lowerBound = function(key, open) {
var args = Array.prototype.slice.call(arguments);
if (key instanceof Array) {
args[0] = encodeCompoundKey(key);
}
return lowerBound.apply(IDBKeyRange, args);
};
IDBKeyRange.only = function(key) {
var args = Array.prototype.slice.call(arguments);
if (key instanceof Array) {
args[0] = encodeCompoundKey(key);
}
return only.apply(IDBKeyRange, args);
};
Object.defineProperty(IDBRequest.prototype, 'result', {
enumerable: requestResult.enumerable,
configurable: requestResult.configurable,
get: function() {
var result = requestResult.get.call(this);
return removeInlineCompoundKey(result);
}
});
Object.defineProperty(IDBCursor.prototype, 'primaryKey', {
enumerable: cursorPrimaryKey.enumerable,
configurable: cursorPrimaryKey.configurable,
get: function() {
var result = cursorPrimaryKey.get.call(this);
return removeInlineCompoundKey(result);
}
});
Object.defineProperty(IDBCursor.prototype, 'key', {
enumerable: cursorKey.enumerable,
configurable: cursorKey.configurable,
get: function() {
var result = cursorKey.get.call(this);
return removeInlineCompoundKey(result);
}
});
Object.defineProperty(IDBCursorWithValue.prototype, 'value', {
enumerable: cursorValue.enumerable,
configurable: cursorValue.configurable,
get: function() {
var result = cursorValue.get.call(this);
return removeInlineCompoundKey(result);
}
});
try {
if (!IDBTransaction.VERSION_CHANGE) {
IDBTransaction.VERSION_CHANGE = 'versionchange';
}
}
catch (e) {}
}
var compoundKeysPropertyName = '__$$compoundKey';
var propertySeparatorRegExp = /\$\$/g;
var propertySeparator = '$$$$'; // "$$" after RegExp escaping
var keySeparator = '$_$';
function isCompoundKey(keyPath) {
return keyPath && (keyPath.indexOf(compoundKeysPropertyName + '.') === 0);
}
function encodeCompoundKeyPath(keyPath) {
// Encoded dotted properties
// ["name.first", "name.last"] ==> ["name$$first", "name$$last"]
for (var i = 0; i < keyPath.length; i++) {
keyPath[i] = keyPath[i].replace(/\./g, propertySeparator);
}
// Encode the array as a single property
// ["name$$first", "name$$last"] => "__$$compoundKey.name$$first$_$name$$last"
return compoundKeysPropertyName + '.' + keyPath.join(keySeparator);
}
function decodeCompoundKeyPath(keyPath) {
// Remove the "__$$compoundKey." prefix
keyPath = keyPath.substr(compoundKeysPropertyName.length + 1);
// Split the properties into an array
// "name$$first$_$name$$last" ==> ["name$$first", "name$$last"]
keyPath = keyPath.split(keySeparator);
// Decode dotted properties
// ["name$$first", "name$$last"] ==> ["name.first", "name.last"]
for (var i = 0; i < keyPath.length; i++) {
keyPath[i] = keyPath[i].replace(propertySeparatorRegExp, '.');
}
return keyPath;
}
function setInlineCompoundKey(value, encodedKeyPath) {
// Encode the key
var keyPath = decodeCompoundKeyPath(encodedKeyPath);
var key = idbModules.Key.getValue(value, keyPath);
var encodedKey = encodeCompoundKey(key);
// Store the encoded key inline
encodedKeyPath = encodedKeyPath.substr(compoundKeysPropertyName.length + 1);
value[compoundKeysPropertyName] = value[compoundKeysPropertyName] || {};
value[compoundKeysPropertyName][encodedKeyPath] = encodedKey;
}
function removeInlineCompoundKey(value) {
if (typeof value === "string" && isCompoundKey(value)) {
return decodeCompoundKey(value);
}
else if (value && typeof value[compoundKeysPropertyName] === "object") {
delete value[compoundKeysPropertyName];
}
return value;
}
function encodeCompoundKey(key) {
// Validate and encode the key
idbModules.Key.validate(key);
key = idbModules.Key.encode(key);
// Prepend the "__$$compoundKey." prefix
key = compoundKeysPropertyName + '.' + key;
validateKeyLength(key);
return key;
}
function decodeCompoundKey(key) {
validateKeyLength(key);
// Remove the "__$$compoundKey." prefix
key = key.substr(compoundKeysPropertyName.length + 1);
// Decode the key
key = idbModules.Key.decode(key);
return key;
}
function validateKeyLength(key) {
// BUG: Internet Explorer truncates string keys at 889 characters
if (key.length > 889) {
throw idbModules.util.createDOMException("DataError", "The encoded key is " + key.length + " characters long, but IE only allows 889 characters. Consider replacing numeric keys with strings to reduce the encoded length.");
}
}
idbModules.polyfill = polyfill;
})(idbModules);