forked from indexeddbshim/IndexedDBShim
/
setGlobalVars.js
206 lines (199 loc) · 9.5 KB
/
setGlobalVars.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
/* globals self */
import shimIDBVersionChangeEvent from './IDBVersionChangeEvent';
import {IDBCursor as shimIDBCursor, IDBCursorWithValue as shimIDBCursorWithValue} from './IDBCursor';
import {IDBRequest as shimIDBRequest, IDBOpenDBRequest as shimIDBOpenDBRequest} from './IDBRequest';
import {ShimDOMException} from './DOMException';
import {shimIndexedDB} from './IDBFactory';
import shimIDBKeyRange from './IDBKeyRange';
import shimIDBObjectStore from './IDBObjectStore';
import shimIDBIndex from './IDBIndex';
import shimIDBTransaction from './IDBTransaction';
import shimIDBDatabase from './IDBDatabase';
import CFG from './CFG';
function setConfig (prop, val) {
if (prop && typeof prop === 'object') {
for (const p in prop) {
setConfig(p, prop[p]);
}
return;
}
if (!(prop in CFG)) {
throw new Error(prop + ' is not a valid configuration property');
}
CFG[prop] = val;
}
function setGlobalVars (idb, initialConfig) {
if (initialConfig) {
setConfig(initialConfig);
}
const IDB = idb || (typeof window !== 'undefined' ? window : (typeof self !== 'undefined' ? self : (typeof global !== 'undefined' ? global : {})));
function shim (name, value, propDesc) {
if (!propDesc || !Object.defineProperty) {
try {
// Try setting the property. This will fail if the property is read-only.
IDB[name] = value;
} catch (e) {
console.log(e);
}
}
if (IDB[name] !== value && Object.defineProperty) {
// Setting a read-only property failed, so try re-defining the property
try {
const desc = propDesc || {};
if (!('value' in desc)) {
desc.get = function () {
return value;
};
}
Object.defineProperty(IDB, name, desc);
} catch (e) {
// With `indexedDB`, PhantomJS fails here and below but
// not above, while Chrome is reverse (and Firefox doesn't
// get here since no WebSQL to use for shimming)
}
}
if (IDB[name] !== value) {
typeof console !== 'undefined' && console.warn && console.warn('Unable to shim ' + name);
}
}
shim('shimIndexedDB', shimIndexedDB, {
enumerable: false,
configurable: true
});
if (IDB.shimIndexedDB) {
IDB.shimIndexedDB.__useShim = function () {
function setNonIDBGlobals (prefix = '') {
shim(prefix + 'DOMException', IDB.indexedDB.modules.ShimDOMException);
shim(prefix + 'DOMStringList', IDB.indexedDB.modules.ShimDOMStringList, {
enumerable: false,
configurable: true,
writable: true,
value: IDB.indexedDB.modules.ShimDOMStringList
});
shim(prefix + 'Event', IDB.indexedDB.modules.ShimEvent, {
configurable: true,
writable: true,
value: IDB.indexedDB.modules.ShimEvent,
enumerable: false
});
shim(prefix + 'CustomEvent', IDB.indexedDB.modules.ShimCustomEvent, {
configurable: true,
writable: true,
value: IDB.indexedDB.modules.ShimCustomEvent,
enumerable: false
});
shim(prefix + 'EventTarget', IDB.indexedDB.modules.ShimEventTarget, {
configurable: true,
writable: true,
value: IDB.indexedDB.modules.ShimEventTarget,
enumerable: false
});
}
const shimIDBFactory = IDB.shimIndexedDB.modules.IDBFactory;
if (CFG.win.openDatabase !== undefined) {
shimIndexedDB.__openDatabase = CFG.win.openDatabase.bind(CFG.win); // We cache here in case the function is overwritten later as by the IndexedDB support promises tests
// Polyfill ALL of IndexedDB, using WebSQL
[
['indexedDB', shimIndexedDB],
['IDBFactory', shimIDBFactory],
['IDBDatabase', shimIDBDatabase],
['IDBObjectStore', shimIDBObjectStore],
['IDBIndex', shimIDBIndex],
['IDBTransaction', shimIDBTransaction],
['IDBCursor', shimIDBCursor],
['IDBCursorWithValue', shimIDBCursorWithValue],
['IDBKeyRange', shimIDBKeyRange],
['IDBRequest', shimIDBRequest],
['IDBOpenDBRequest', shimIDBOpenDBRequest],
['IDBVersionChangeEvent', shimIDBVersionChangeEvent]
].forEach(([prop, obj]) => {
shim(prop, obj, {
enumerable: false,
configurable: true
});
});
if (CFG.fullIDLSupport) {
// Slow per MDN so off by default! Though apparently needed for WebIDL: http://stackoverflow.com/questions/41927589/rationales-consequences-of-webidl-class-inheritance-requirements
Object.setPrototypeOf(IDB.IDBOpenDBRequest, IDB.IDBRequest);
Object.setPrototypeOf(IDB.IDBCursorWithValue, IDB.IDBCursor);
const ShimEvent = IDB.shimIndexedDB.modules.ShimEvent;
const ShimEventTarget = IDB.shimIndexedDB.modules.ShimEventTarget;
Object.setPrototypeOf(shimIDBDatabase, ShimEventTarget);
Object.setPrototypeOf(shimIDBRequest, ShimEventTarget);
Object.setPrototypeOf(shimIDBTransaction, ShimEventTarget);
Object.setPrototypeOf(shimIDBVersionChangeEvent, ShimEvent);
Object.setPrototypeOf(ShimDOMException, Error);
Object.setPrototypeOf(ShimDOMException.prototype, Error.prototype);
}
if (IDB.indexedDB && IDB.indexedDB.modules) {
if (CFG.addNonIDBGlobals) {
// As `DOMStringList` exists per IDL (and Chrome) in the global
// thread (but not in workers), we prefix the name to avoid
// shadowing or conflicts
setNonIDBGlobals('Shim');
}
if (CFG.replaceNonIDBGlobals) {
setNonIDBGlobals();
}
}
IDB.shimIndexedDB.__setConnectionQueueOrigin();
}
};
IDB.shimIndexedDB.__debug = function (val) {
CFG.DEBUG = val;
};
IDB.shimIndexedDB.__setConfig = setConfig;
IDB.shimIndexedDB.__getConfig = function (prop) {
if (!(prop in CFG)) {
throw new Error(prop + ' is not a valid configuration property');
}
return CFG[prop];
};
IDB.shimIndexedDB.__setUnicodeIdentifiers = function ({UnicodeIDStart, UnicodeIDContinue}) {
setConfig({UnicodeIDStart, UnicodeIDContinue});
};
}
// Workaround to prevent an error in Firefox
if (!('indexedDB' in IDB) && typeof window !== 'undefined') { // 2nd condition avoids problems in Node
IDB.indexedDB = IDB.indexedDB || IDB.webkitIndexedDB || IDB.mozIndexedDB || IDB.oIndexedDB || IDB.msIndexedDB;
}
// Detect browsers with known IndexedDb issues (e.g. Android pre-4.4)
let poorIndexedDbSupport = false;
if (typeof navigator !== 'undefined' && ( // Ignore Node or other environments
(
// Bad non-Chrome Android support
(/Android (?:2|3|4\.[0-3])/).test(navigator.userAgent) &&
!navigator.userAgent.includes('Chrome')
) ||
(
// Bad non-Safari iOS9 support (see <https://github.com/axemclion/IndexedDBShim/issues/252>)
(!navigator.userAgent.includes('Safari') || navigator.userAgent.includes('Chrome')) && // Exclude genuine Safari: http://stackoverflow.com/a/7768006/271577
// Detect iOS: http://stackoverflow.com/questions/9038625/detect-if-device-is-ios/9039885#9039885
// and detect version 9: http://stackoverflow.com/a/26363560/271577
(/(iPad|iPhone|iPod).* os 9_/i).test(navigator.userAgent) &&
!window.MSStream // But avoid IE11
)
)) {
poorIndexedDbSupport = true;
}
if (!CFG.DEFAULT_DB_SIZE) {
CFG.DEFAULT_DB_SIZE = (
( // Safari currently requires larger size: (We don't need a larger size for Node as node-websql doesn't use this info)
// https://github.com/axemclion/IndexedDBShim/issues/41
// https://github.com/axemclion/IndexedDBShim/issues/115
typeof navigator !== 'undefined' &&
navigator.userAgent.includes('Safari') &&
!navigator.userAgent.includes('Chrome')
) ? 25 : 4) * 1024 * 1024;
}
if ((!IDB.indexedDB || poorIndexedDbSupport) && CFG.win.openDatabase !== undefined) {
IDB.shimIndexedDB.__useShim();
} else {
IDB.IDBDatabase = IDB.IDBDatabase || IDB.webkitIDBDatabase;
IDB.IDBTransaction = IDB.IDBTransaction || IDB.webkitIDBTransaction || {};
IDB.IDBCursor = IDB.IDBCursor || IDB.webkitIDBCursor;
IDB.IDBKeyRange = IDB.IDBKeyRange || IDB.webkitIDBKeyRange;
}
return IDB;
}
export default setGlobalVars;