indexeddbshim 12.0.0 → 13.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (101) hide show
  1. package/README.md +1 -1
  2. package/badges/licenses-badge-dev.svg +1 -1
  3. package/dist/CFG.d.ts +80 -0
  4. package/dist/CFG.d.ts.map +1 -0
  5. package/dist/DOMException.d.ts +71 -0
  6. package/dist/DOMException.d.ts.map +1 -0
  7. package/dist/DOMStringList.d.ts +20 -0
  8. package/dist/DOMStringList.d.ts.map +1 -0
  9. package/dist/Event.d.ts +21 -0
  10. package/dist/Event.d.ts.map +1 -0
  11. package/dist/IDBCursor.d.ts +288 -0
  12. package/dist/IDBCursor.d.ts.map +1 -0
  13. package/dist/IDBDatabase.d.ts +143 -0
  14. package/dist/IDBDatabase.d.ts.map +1 -0
  15. package/dist/IDBFactory.d.ts +96 -0
  16. package/dist/IDBFactory.d.ts.map +1 -0
  17. package/dist/IDBIndex.d.ts +243 -0
  18. package/dist/IDBIndex.d.ts.map +1 -0
  19. package/dist/IDBKeyRange.d.ts +79 -0
  20. package/dist/IDBKeyRange.d.ts.map +1 -0
  21. package/dist/IDBObjectStore.d.ts +273 -0
  22. package/dist/IDBObjectStore.d.ts.map +1 -0
  23. package/dist/IDBRequest.d.ts +88 -0
  24. package/dist/IDBRequest.d.ts.map +1 -0
  25. package/dist/IDBTransaction.d.ts +270 -0
  26. package/dist/IDBTransaction.d.ts.map +1 -0
  27. package/dist/IDBVersionChangeEvent.d.ts +22 -0
  28. package/dist/IDBVersionChangeEvent.d.ts.map +1 -0
  29. package/dist/Key.d.ts +214 -0
  30. package/dist/Key.d.ts.map +1 -0
  31. package/dist/Sca.d.ts +31 -0
  32. package/dist/Sca.d.ts.map +1 -0
  33. package/dist/UnicodeIdentifiers.d.ts +3 -0
  34. package/dist/UnicodeIdentifiers.d.ts.map +1 -0
  35. package/dist/browser-UnicodeIdentifiers.d.ts +2 -0
  36. package/dist/browser-UnicodeIdentifiers.d.ts.map +1 -0
  37. package/dist/browser-noninvasive.d.ts +3 -0
  38. package/dist/browser-noninvasive.d.ts.map +1 -0
  39. package/dist/browser.d.ts +2 -0
  40. package/dist/browser.d.ts.map +1 -0
  41. package/dist/cmp.d.ts +9 -0
  42. package/dist/cmp.d.ts.map +1 -0
  43. package/dist/indexeddbshim-Key.js +553 -252
  44. package/dist/indexeddbshim-Key.js.map +1 -1
  45. package/dist/indexeddbshim-Key.min.js +2 -2
  46. package/dist/indexeddbshim-Key.min.js.map +1 -1
  47. package/dist/indexeddbshim-UnicodeIdentifiers-node.cjs +17560 -1302
  48. package/dist/indexeddbshim-UnicodeIdentifiers-node.cjs.map +1 -1
  49. package/dist/indexeddbshim-UnicodeIdentifiers.js +4367 -1604
  50. package/dist/indexeddbshim-UnicodeIdentifiers.js.map +1 -1
  51. package/dist/indexeddbshim-UnicodeIdentifiers.min.js +2 -2
  52. package/dist/indexeddbshim-UnicodeIdentifiers.min.js.map +1 -1
  53. package/dist/indexeddbshim-node.cjs +17556 -1302
  54. package/dist/indexeddbshim-node.cjs.map +1 -1
  55. package/dist/indexeddbshim-noninvasive.js +4362 -1604
  56. package/dist/indexeddbshim-noninvasive.js.map +1 -1
  57. package/dist/indexeddbshim-noninvasive.min.js +2 -2
  58. package/dist/indexeddbshim-noninvasive.min.js.map +1 -1
  59. package/dist/indexeddbshim.js +4362 -1604
  60. package/dist/indexeddbshim.js.map +1 -1
  61. package/dist/indexeddbshim.min.js +2 -2
  62. package/dist/indexeddbshim.min.js.map +1 -1
  63. package/dist/node-UnicodeIdentifiers.d.ts +8 -0
  64. package/dist/node-UnicodeIdentifiers.d.ts.map +1 -0
  65. package/dist/node.d.ts +8 -0
  66. package/dist/node.d.ts.map +1 -0
  67. package/dist/nodeWebSQL.d.ts +3 -0
  68. package/dist/nodeWebSQL.d.ts.map +1 -0
  69. package/dist/setGlobalVars.d.ts +79 -0
  70. package/dist/setGlobalVars.d.ts.map +1 -0
  71. package/dist/unicode-regex.d.ts +3 -0
  72. package/dist/unicode-regex.d.ts.map +1 -0
  73. package/dist/util.d.ts +174 -0
  74. package/dist/util.d.ts.map +1 -0
  75. package/index.html +1 -1
  76. package/package.json +51 -37
  77. package/src/CFG.js +89 -11
  78. package/src/DOMException.js +117 -31
  79. package/src/DOMStringList.js +93 -3
  80. package/src/Event.js +19 -4
  81. package/src/IDBCursor.js +350 -76
  82. package/src/IDBDatabase.js +88 -10
  83. package/src/IDBFactory.js +331 -61
  84. package/src/IDBIndex.js +393 -110
  85. package/src/IDBKeyRange.js +90 -17
  86. package/src/IDBObjectStore.js +347 -86
  87. package/src/IDBRequest.js +59 -1
  88. package/src/IDBTransaction.js +254 -46
  89. package/src/IDBVersionChangeEvent.js +28 -3
  90. package/src/Key.d.ts +1 -0
  91. package/src/Key.js +272 -162
  92. package/src/Sca.js +29 -4
  93. package/src/browser-UnicodeIdentifiers.js +5 -1
  94. package/src/cmp.js +5 -5
  95. package/src/node-UnicodeIdentifiers.js +9 -0
  96. package/src/node.js +5 -0
  97. package/src/nodeWebSQL.js +11 -5
  98. package/src/setGlobalVars.js +159 -22
  99. package/src/unicode-regex.js +2 -0
  100. package/src/util.js +229 -40
  101. package/typings/shimIndexedDB.d.ts +9 -0
package/src/IDBFactory.js CHANGED
@@ -1,6 +1,5 @@
1
- /* globals location */
2
1
  import path from 'path';
3
- import SyncPromise from 'sync-promise';
2
+ import SyncPromise from 'sync-promise-expanded';
4
3
 
5
4
  import {createEvent} from './Event.js';
6
5
  import IDBVersionChangeEvent from './IDBVersionChangeEvent.js';
@@ -13,11 +12,29 @@ import IDBTransaction from './IDBTransaction.js';
13
12
  import IDBDatabase from './IDBDatabase.js';
14
13
  import CFG from './CFG.js';
15
14
 
15
+ /**
16
+ * @typedef {number} Integer
17
+ */
18
+
19
+ /**
20
+ * @callback DatabaseDeleted
21
+ * @returns {void}
22
+ */
23
+
24
+ /** @type {import('./CFG.js').FSApi} */
16
25
  let fs;
26
+
27
+ /**
28
+ * @param {import('./CFG.js').FSApi} _fs
29
+ * @returns {void}
30
+ */
17
31
  const setFS = (_fs) => {
18
32
  fs = _fs;
19
33
  };
20
34
 
35
+ /**
36
+ * @returns {string}
37
+ */
21
38
  const getOrigin = () => {
22
39
  return (typeof location !== 'object' || !location) ? 'null' : location.origin;
23
40
  };
@@ -26,14 +43,34 @@ const hasNullOrigin = () => CFG.checkOrigin !== false && (getOrigin() === 'null'
26
43
  // Todo: This really should be process and tab-independent so the
27
44
  // origin could vary; in the browser, this might be through a
28
45
  // `SharedWorker`
46
+
47
+ /**
48
+ * @type {{
49
+ * [key: string]: {
50
+ * [key: string]: {
51
+ * req: import('./IDBRequest.js').IDBOpenDBRequestFull,
52
+ * cb: (req: import('./IDBRequest.js').IDBRequestFull) => void,
53
+ * }[]
54
+ * }
55
+ * }}
56
+ */
29
57
  const connectionQueue = {};
30
58
 
59
+ /**
60
+ * @param {string} name
61
+ * @param {string} origin
62
+ * @returns {void}
63
+ */
31
64
  function processNextInConnectionQueue (name, origin = getOrigin()) {
32
65
  const queueItems = connectionQueue[origin][name];
33
66
  if (!queueItems[0]) { // Nothing left to process
34
67
  return;
35
68
  }
36
69
  const {req, cb} = queueItems[0]; // Keep in queue to prevent continuation
70
+
71
+ /**
72
+ * @returns {void}
73
+ */
37
74
  function removeFromQueue () {
38
75
  queueItems.shift();
39
76
  processNextInConnectionQueue(name, origin);
@@ -43,8 +80,16 @@ function processNextInConnectionQueue (name, origin = getOrigin()) {
43
80
  cb(req);
44
81
  }
45
82
 
46
- // eslint-disable-next-line default-param-last
83
+ /* eslint-disable default-param-last */
84
+ /**
85
+ * @param {import('./IDBRequest.js').IDBOpenDBRequestFull} req
86
+ * @param {string} name
87
+ * @param {string} origin
88
+ * @param {(req: import('./IDBRequest.js').IDBOpenDBRequestFull) => void} cb
89
+ * @returns {void}
90
+ */
47
91
  function addRequestToConnectionQueue (req, name, origin = getOrigin(), cb) {
92
+ /* eslint-enable default-param-last */
48
93
  if (!connectionQueue[origin][name]) {
49
94
  connectionQueue[origin][name] = [];
50
95
  }
@@ -55,10 +100,22 @@ function addRequestToConnectionQueue (req, name, origin = getOrigin(), cb) {
55
100
  }
56
101
  }
57
102
 
103
+ /**
104
+ * @param {import('./IDBDatabase.js').IDBDatabaseFull[]} openConnections
105
+ * @param {import('./IDBRequest.js').IDBRequestFull} req
106
+ * @param {Integer} oldVersion
107
+ * @param {Integer|null} newVersion
108
+ * @returns {SyncPromise}
109
+ */
58
110
  function triggerAnyVersionChangeAndBlockedEvents (openConnections, req, oldVersion, newVersion) {
59
111
  // Todo: For Node (and in browser using service workers if available?) the
60
112
  // connections ought to involve those in any process; should also
61
113
  // auto-close if unloading
114
+
115
+ /**
116
+ * @param {IDBDatabase} connection
117
+ * @returns {boolean|undefined}
118
+ */
62
119
  const connectionIsClosed = (connection) => connection.__closePending;
63
120
  const connectionsClosed = () => openConnections.every((conn) => {
64
121
  return connectionIsClosed(conn);
@@ -72,15 +129,17 @@ function triggerAnyVersionChangeAndBlockedEvents (openConnections, req, oldVersi
72
129
  // Prior onversionchange must have caused this connection to be closed
73
130
  return undefined;
74
131
  }
75
- const e = new IDBVersionChangeEvent('versionchange', {oldVersion, newVersion});
132
+ const e = /** @type {Event & IDBVersionChangeEvent} */ (
133
+ new IDBVersionChangeEvent('versionchange', {oldVersion, newVersion})
134
+ );
76
135
  return new SyncPromise(function (resolve) {
77
136
  setTimeout(() => {
78
137
  entry.dispatchEvent(e); // No need to catch errors
79
- resolve();
138
+ resolve(undefined);
80
139
  });
81
140
  });
82
141
  });
83
- }, SyncPromise.resolve()).then(function () {
142
+ }, SyncPromise.resolve(undefined)).then(function () {
84
143
  if (connectionsClosed()) {
85
144
  return undefined;
86
145
  }
@@ -88,11 +147,13 @@ function triggerAnyVersionChangeAndBlockedEvents (openConnections, req, oldVersi
88
147
  const unblocking = {
89
148
  check () {
90
149
  if (connectionsClosed()) {
91
- resolve();
150
+ resolve(undefined);
92
151
  }
93
152
  }
94
153
  };
95
- const e = new IDBVersionChangeEvent('blocked', {oldVersion, newVersion});
154
+ const e = /** @type {Event & IDBVersionChangeEvent} */ (
155
+ new IDBVersionChangeEvent('blocked', {oldVersion, newVersion})
156
+ );
96
157
  setTimeout(() => {
97
158
  req.dispatchEvent(e); // No need to catch errors
98
159
  if (!connectionsClosed()) {
@@ -102,17 +163,40 @@ function triggerAnyVersionChangeAndBlockedEvents (openConnections, req, oldVersi
102
163
  }
103
164
  });
104
165
  } else {
105
- resolve();
166
+ resolve(undefined);
106
167
  }
107
168
  });
108
169
  });
109
170
  });
110
171
  }
111
172
 
173
+ /**
174
+ * @typedef {import('websql-configurable/lib/websql/WebSQLDatabase.js').default & {
175
+ * _db: {
176
+ * _db: {
177
+ * close: (errBack: (err: Error) => void) => void
178
+ * }
179
+ * }
180
+ * }} DatabaseFull
181
+ */
182
+
183
+ /**
184
+ * @type {{
185
+ * [key: string]: {
186
+ * [key: string]: DatabaseFull
187
+ * }
188
+ * }}
189
+ */
112
190
  const websqlDBCache = {};
191
+
192
+ /** @type {import('websql-configurable/lib/websql/WebSQLDatabase.js').default} */
113
193
  let sysdb;
114
194
  let nameCounter = 0;
115
195
 
196
+ /**
197
+ * @param {string} name
198
+ * @returns {Integer}
199
+ */
116
200
  function getLatestCachedWebSQLVersion (name) {
117
201
  return Object.keys(websqlDBCache[name]).map(Number).reduce(
118
202
  (prev, curr) => {
@@ -121,12 +205,24 @@ function getLatestCachedWebSQLVersion (name) {
121
205
  );
122
206
  }
123
207
 
208
+ /**
209
+ * @param {string} name
210
+ * @returns {DatabaseFull}
211
+ */
124
212
  function getLatestCachedWebSQLDB (name) {
125
213
  return websqlDBCache[name] && websqlDBCache[name][
126
214
  getLatestCachedWebSQLVersion(name)
127
215
  ];
128
216
  }
129
217
 
218
+ /**
219
+ * @param {OpenDatabase} __openDatabase
220
+ * @param {string} name
221
+ * @param {string} escapedDatabaseName
222
+ * @param {DatabaseDeleted} databaseDeleted
223
+ * @param {(tx: SQLTransaction|Error|SQLError, err?: SQLError) => boolean} dbError
224
+ * @returns {void}
225
+ */
130
226
  function cleanupDatabaseResources (__openDatabase, name, escapedDatabaseName, databaseDeleted, dbError) {
131
227
  const useMemoryDatabase = typeof CFG.memoryDatabase === 'string';
132
228
  if (useMemoryDatabase) {
@@ -141,13 +237,19 @@ function cleanupDatabaseResources (__openDatabase, name, escapedDatabaseName, da
141
237
  console.error('The `openDatabase` implementation does not have the expected `._db._db.close` method for closing the database');
142
238
  return;
143
239
  }
144
- sqliteDB.close(function (err) {
145
- if (err) {
146
- console.warn('Error closing (destroying) memory database');
147
- return;
240
+ sqliteDB.close(
241
+ /**
242
+ * @param {Error} err
243
+ * @returns {void}
244
+ */
245
+ (err) => {
246
+ if (err) {
247
+ console.warn('Error closing (destroying) memory database');
248
+ return;
249
+ }
250
+ databaseDeleted();
148
251
  }
149
- databaseDeleted();
150
- });
252
+ );
151
253
  return;
152
254
  }
153
255
  if (fs && CFG.deleteDatabaseFiles !== false) {
@@ -166,7 +268,7 @@ function cleanupDatabaseResources (__openDatabase, name, escapedDatabaseName, da
166
268
 
167
269
  const sqliteDB = __openDatabase(
168
270
  path.join(CFG.databaseBasePath || '', escapedDatabaseName),
169
- 1,
271
+ '1',
170
272
  name,
171
273
  CFG.DEFAULT_DB_SIZE
172
274
  );
@@ -189,33 +291,41 @@ function cleanupDatabaseResources (__openDatabase, name, escapedDatabaseName, da
189
291
  deleteTables(i + 1);
190
292
  }, function () {
191
293
  deleteTables(i + 1);
294
+ return false;
192
295
  });
193
296
  }
194
297
  }(0));
195
- }, function (e) {
298
+ }, function () {
196
299
  // __sys__ table does not exist, but that does not mean delete did not happen
197
300
  databaseDeleted();
301
+ return false;
198
302
  });
199
303
  });
200
304
  }
201
305
 
202
306
  /**
203
- * @callback CreateSysDBSuccessCallback
204
- * @returns {void}
205
- */
307
+ * @callback CreateSysDBSuccessCallback
308
+ * @returns {void}
309
+ */
206
310
 
207
311
  /**
208
312
  * Creates the sysDB to keep track of version numbers for databases.
209
- * @param {openDatabase} __openDatabase
313
+ * @param {OpenDatabase} __openDatabase
210
314
  * @param {CreateSysDBSuccessCallback} success
211
- * @param {DOMException} failure
315
+ * @param {(tx: SQLTransaction|SQLError|Error, err?: SQLError) => void} failure
212
316
  * @returns {void}
213
317
  */
214
318
  function createSysDB (__openDatabase, success, failure) {
319
+ /**
320
+ *
321
+ * @param {boolean|SQLTransaction|SQLError} tx
322
+ * @param {SQLError} [err]
323
+ * @returns {void}
324
+ */
215
325
  function sysDbCreateError (tx, err) {
216
- err = webSQLErrback(err || tx);
217
- CFG.DEBUG && console.log('Error in sysdb transaction - when creating dbVersions', err);
218
- failure(err);
326
+ const er = webSQLErrback(/** @type {SQLError} */ (err) || tx);
327
+ if (CFG.DEBUG) { console.log('Error in sysdb transaction - when creating dbVersions', err); }
328
+ failure(er);
219
329
  }
220
330
 
221
331
  if (sysdb) {
@@ -230,7 +340,7 @@ function createSysDB (__openDatabase, success, failure) {
230
340
  : (CFG.databaseBasePath || '')),
231
341
  '__sysdb__' + (CFG.addSQLiteExtension !== false ? '.sqlite' : '')
232
342
  ),
233
- 1,
343
+ '1',
234
344
  'System Database',
235
345
  CFG.DEFAULT_DB_SIZE
236
346
  );
@@ -240,8 +350,13 @@ function createSysDB (__openDatabase, success, failure) {
240
350
  success();
241
351
  return;
242
352
  }
243
- systx.executeSql('CREATE INDEX IF NOT EXISTS dbvname ON dbVersions(name)', [], success, sysDbCreateError);
244
- }, sysDbCreateError);
353
+ systx.executeSql(
354
+ 'CREATE INDEX IF NOT EXISTS dbvname ON dbVersions(name)',
355
+ [],
356
+ success,
357
+ /** @type {SQLStatementErrorCallback} */ (sysDbCreateError)
358
+ );
359
+ }, /** @type {SQLStatementErrorCallback} */ (sysDbCreateError));
245
360
  }, sysDbCreateError);
246
361
  }
247
362
  }
@@ -254,26 +369,48 @@ function createSysDB (__openDatabase, success, failure) {
254
369
  function IDBFactory () {
255
370
  throw new TypeError('Illegal constructor');
256
371
  }
372
+
373
+ /**
374
+ * @typedef {(
375
+ * name: string, version: string, displayName: string, estimatedSize: number
376
+ * ) => import('websql-configurable/lib/websql/WebSQLDatabase.js').default} OpenDatabase
377
+ */
378
+
379
+ /**
380
+ * @typedef {globalThis.IDBFactory & {
381
+ * __openDatabase: OpenDatabase,
382
+ * __connections: {
383
+ * [key: string]: import('./IDBDatabase.js').IDBDatabaseFull[]
384
+ * }
385
+ * }} IDBFactoryFull
386
+ */
387
+
257
388
  const IDBFactoryAlias = IDBFactory;
389
+ /**
390
+ * @returns {IDBFactoryFull}
391
+ */
258
392
  IDBFactory.__createInstance = function () {
393
+ /**
394
+ * @class
395
+ */
259
396
  function IDBFactory () {
260
397
  this[Symbol.toStringTag] = 'IDBFactory';
261
398
  this.__connections = {};
262
399
  }
263
400
  IDBFactory.prototype = IDBFactoryAlias.prototype;
401
+
402
+ // @ts-expect-error It's ok
264
403
  return new IDBFactory();
265
404
  };
266
405
 
267
- /* eslint-disable jsdoc/check-param-names */
268
406
  /**
269
407
  * The IndexedDB Method to create a new database and return the DB.
270
408
  * @param {string} name
271
- * @param {number} version
409
+ * @this {IDBFactoryFull}
272
410
  * @throws {TypeError} Illegal invocation or no arguments (for database name)
273
411
  * @returns {IDBOpenDBRequest}
274
412
  */
275
413
  IDBFactory.prototype.open = function (name /* , version */) {
276
- /* eslint-enable jsdoc/check-param-names */
277
414
  const me = this;
278
415
  if (!(me instanceof IDBFactory)) {
279
416
  throw new TypeError('Illegal invocation');
@@ -306,6 +443,7 @@ IDBFactory.prototype.open = function (name /* , version */) {
306
443
  const useMemoryDatabase = typeof CFG.memoryDatabase === 'string';
307
444
  const useDatabaseCache = CFG.cacheDatabaseInstances !== false || useMemoryDatabase;
308
445
 
446
+ /** @type {string} */
309
447
  let escapedDatabaseName;
310
448
  // eslint-disable-next-line no-useless-catch
311
449
  try {
@@ -315,22 +453,39 @@ IDBFactory.prototype.open = function (name /* , version */) {
315
453
  throw err; // new TypeError('You have supplied a database name which does not match the currently supported configuration, possibly due to a length limit enforced for Node compatibility.');
316
454
  }
317
455
 
456
+ /**
457
+ *
458
+ * @param {SQLTransaction|Error|SQLError} tx
459
+ * @param {SQLError} [err]
460
+ * @returns {boolean}
461
+ */
318
462
  function dbCreateError (tx, err) {
319
463
  if (calledDbCreateError) {
320
- return;
464
+ return false;
321
465
  }
322
- err = err ? webSQLErrback(err) : tx;
466
+ const er = err ? webSQLErrback(err) : /** @type {Error} */ (tx);
323
467
  calledDbCreateError = true;
324
468
  // Re: why bubbling here (and how cancelable is only really relevant for `window.onerror`) see: https://github.com/w3c/IndexedDB/issues/86
325
- const evt = createEvent('error', err, {bubbles: true, cancelable: true});
469
+ const evt = createEvent('error', er, {bubbles: true, cancelable: true});
326
470
  req.__done = true;
327
- req.__error = err;
471
+ req.__error = er;
328
472
  req.__result = undefined; // Must be undefined if an error per `result` getter
329
473
  req.dispatchEvent(evt);
474
+ return false;
330
475
  }
331
476
 
477
+ /**
478
+ *
479
+ * @param {SQLTransaction} tx
480
+ * @param {DatabaseFull} db
481
+ * @param {Integer} oldVersion
482
+ * @returns {void}
483
+ */
332
484
  function setupDatabase (tx, db, oldVersion) {
333
485
  tx.executeSql('SELECT "name", "keyPath", "autoInc", "indexList" FROM __sys__', [], function (tx, data) {
486
+ /**
487
+ * @returns {void}
488
+ */
334
489
  function finishRequest () {
335
490
  req.__result = connection;
336
491
  req.__done = true;
@@ -345,6 +500,13 @@ IDBFactory.prototype.open = function (name /* , version */) {
345
500
  const openConnections = me.__connections[name].slice(0, -1);
346
501
  triggerAnyVersionChangeAndBlockedEvents(openConnections, req, oldVersion, version).then(function () {
347
502
  // DB Upgrade in progress
503
+ /**
504
+ *
505
+ * @param {SQLTransaction} systx
506
+ * @param {boolean|SQLError|DOMException|Error} err
507
+ * @param {(tx?: SQLTransaction|SQLError, err?: SQLError|SQLResultSet) => boolean} cb
508
+ * @returns {void}
509
+ */
348
510
  let sysdbFinishedCb = function (systx, err, cb) {
349
511
  if (err) {
350
512
  try {
@@ -353,17 +515,38 @@ IDBFactory.prototype.open = function (name /* , version */) {
353
515
  // Browser may fail with expired transaction above so
354
516
  // no choice but to manually revert
355
517
  sysdb.transaction(function (systx) {
518
+ /**
519
+ *
520
+ * @param {string} msg
521
+ * @throws {Error}
522
+ * @returns {never}
523
+ */
356
524
  function reportError (msg) {
357
525
  throw new Error('Unable to roll back upgrade transaction!' + (msg || ''));
358
526
  }
359
527
 
360
528
  // Attempt to revert
361
529
  if (oldVersion === 0) {
362
- systx.executeSql('DELETE FROM dbVersions WHERE "name" = ?', [sqlSafeName], function () {
363
- cb(reportError); // eslint-disable-line promise/no-callback-in-promise
364
- }, reportError);
530
+ systx.executeSql(
531
+ 'DELETE FROM dbVersions WHERE "name" = ?',
532
+ [sqlSafeName],
533
+ function () {
534
+ // @ts-expect-error Force to work
535
+ cb(reportError); // eslint-disable-line promise/no-callback-in-promise
536
+ },
537
+ // @ts-expect-error Force to work
538
+ reportError
539
+ );
365
540
  } else {
366
- systx.executeSql('UPDATE dbVersions SET "version" = ? WHERE "name" = ?', [oldVersion, sqlSafeName], cb, reportError);
541
+ systx.executeSql(
542
+ 'UPDATE dbVersions SET "version" = ? WHERE "name" = ?',
543
+ [
544
+ oldVersion, sqlSafeName
545
+ ],
546
+ cb,
547
+ // @ts-expect-error Force to work
548
+ reportError
549
+ );
367
550
  }
368
551
  });
369
552
  }
@@ -374,32 +557,44 @@ IDBFactory.prototype.open = function (name /* , version */) {
374
557
  };
375
558
 
376
559
  sysdb.transaction(function (systx) {
560
+ /**
561
+ * @returns {void}
562
+ */
377
563
  function versionSet () {
378
- const e = new IDBVersionChangeEvent('upgradeneeded', {oldVersion, newVersion: version});
564
+ const e = /** @type {import('eventtargeter').EventWithProps & Event & IDBVersionChangeEvent} */ (
565
+ new IDBVersionChangeEvent('upgradeneeded', {oldVersion, newVersion: version})
566
+ );
379
567
  req.__result = connection;
380
568
  connection.__upgradeTransaction = req.__transaction = req.__result.__versionTransaction = IDBTransaction.__createInstance(req.__result, req.__result.objectStoreNames, 'versionchange');
381
569
  req.__done = true;
382
570
 
383
- req.transaction.__addNonRequestToTransactionQueue(function onupgradeneeded (tx, args, finished, error) {
571
+ req.transaction.__addNonRequestToTransactionQueue(function onupgradeneeded (tx, args, finished /* , error */) {
384
572
  req.dispatchEvent(e);
385
573
 
386
574
  if (e.__legacyOutputDidListenersThrowError) {
387
- logError('Error', 'An error occurred in an upgradeneeded handler attached to request chain', e.__legacyOutputDidListenersThrowError); // We do nothing else with this error as per spec
575
+ logError('Error', 'An error occurred in an upgradeneeded handler attached to request chain', /** @type {Error} */ (e.__legacyOutputDidListenersThrowError)); // We do nothing else with this error as per spec
388
576
  req.transaction.__abortTransaction(createDOMException('AbortError', 'A request was aborted.'));
389
577
  return;
390
578
  }
391
579
  finished();
392
580
  });
581
+
582
+ // eslint-disable-next-line camelcase -- Clear API
393
583
  req.transaction.on__beforecomplete = function (ev) {
394
584
  connection.__upgradeTransaction = null;
395
- req.__result.__versionTransaction = null;
585
+ /** @type {import('./IDBDatabase.js').IDBDatabaseFull} */ (
586
+ req.__result
587
+ ).__versionTransaction = null;
396
588
  sysdbFinishedCb(systx, false, function () {
397
589
  req.transaction.__transFinishedCb(false, function () {
398
590
  ev.complete();
399
591
  req.__transaction = null;
400
592
  });
593
+ return false;
401
594
  });
402
595
  };
596
+
597
+ // eslint-disable-next-line camelcase -- Clear API
403
598
  req.transaction.on__preabort = function () {
404
599
  connection.__upgradeTransaction = null;
405
600
  // We ensure any cache is deleted before any request error events fire and try to reopen
@@ -409,6 +604,8 @@ IDBFactory.prototype.open = function (name /* , version */) {
409
604
  }
410
605
  }
411
606
  };
607
+
608
+ // eslint-disable-next-line camelcase -- Clear API
412
609
  req.transaction.on__abort = function () {
413
610
  req.__transaction = null;
414
611
  // `readyState` and `result` will be reset anyways by `dbCreateError` but we follow spec.
@@ -420,15 +617,27 @@ IDBFactory.prototype.open = function (name /* , version */) {
420
617
  const err = createDOMException('AbortError', 'The upgrade transaction was aborted.');
421
618
  sysdbFinishedCb(systx, err, function (reportError) {
422
619
  if (oldVersion === 0) {
423
- cleanupDatabaseResources(me.__openDatabase, name, escapedDatabaseName, dbCreateError.bind(null, err), reportError || dbCreateError);
424
- return;
620
+ cleanupDatabaseResources(
621
+ me.__openDatabase,
622
+ name,
623
+ escapedDatabaseName,
624
+ dbCreateError.bind(null, err),
625
+ // @ts-expect-error It's ok
626
+ reportError || dbCreateError
627
+ );
628
+ return false;
425
629
  }
426
630
  dbCreateError(err);
631
+ return false;
427
632
  });
428
633
  });
429
634
  };
635
+
636
+ // eslint-disable-next-line camelcase -- Clear API
430
637
  req.transaction.on__complete = function () {
431
- if (req.__result.__closePending) {
638
+ if (/** @type {import('./IDBDatabase.js').IDBDatabaseFull} */ (
639
+ req.__result
640
+ ).__closePending) {
432
641
  req.__transaction = null;
433
642
  const err = createDOMException('AbortError', 'The connection has been closed.');
434
643
  dbCreateError(err);
@@ -460,7 +669,7 @@ IDBFactory.prototype.open = function (name /* , version */) {
460
669
  } else {
461
670
  systx.executeSql('UPDATE dbVersions SET "version" = ? WHERE "name" = ?', [version, sqlSafeName], versionSet, dbCreateError);
462
671
  }
463
- }, dbCreateError, null, function (currentTask, err, done, rollback, commit) {
672
+ }, dbCreateError, undefined, function (currentTask, err, done, rollback, commit) {
464
673
  if (currentTask.readOnly || err) {
465
674
  return true;
466
675
  }
@@ -487,14 +696,20 @@ IDBFactory.prototype.open = function (name /* , version */) {
487
696
  }, dbCreateError);
488
697
  }
489
698
 
699
+ /**
700
+ *
701
+ * @param {Integer} oldVersion
702
+ * @returns {void}
703
+ */
490
704
  function openDB (oldVersion) {
705
+ /** @type {DatabaseFull} */
491
706
  let db;
492
707
  if ((useMemoryDatabase || useDatabaseCache) && name in websqlDBCache && websqlDBCache[name][version]) {
493
708
  db = websqlDBCache[name][version];
494
709
  } else {
495
710
  db = me.__openDatabase(
496
711
  useMemoryDatabase ? CFG.memoryDatabase : path.join(CFG.databaseBasePath || '', escapedDatabaseName),
497
- 1,
712
+ '1',
498
713
  name,
499
714
  CFG.DEFAULT_DB_SIZE
500
715
  );
@@ -523,6 +738,9 @@ IDBFactory.prototype.open = function (name /* , version */) {
523
738
 
524
739
  db.transaction(function (tx) {
525
740
  tx.executeSql('CREATE TABLE IF NOT EXISTS __sys__ (name BLOB, keyPath BLOB, autoInc BOOLEAN, indexList BLOB, currNum INTEGER)', [], function () {
741
+ /**
742
+ * @returns {void}
743
+ */
526
744
  function setup () {
527
745
  setupDatabase(tx, db, oldVersion);
528
746
  }
@@ -531,11 +749,11 @@ IDBFactory.prototype.open = function (name /* , version */) {
531
749
  return;
532
750
  }
533
751
  tx.executeSql('CREATE INDEX IF NOT EXISTS sysname ON __sys__(name)', [], setup, dbCreateError);
534
- }, dbCreateError);
752
+ }, /** @type {SQLStatementErrorCallback} */ (dbCreateError));
535
753
  }, dbCreateError);
536
754
  }
537
755
 
538
- addRequestToConnectionQueue(req, name, /* origin */ undefined, function (req) {
756
+ addRequestToConnectionQueue(req, name, /* origin */ undefined, function () {
539
757
  let latestCachedVersion;
540
758
  if (useDatabaseCache) {
541
759
  if (!(name in websqlDBCache)) {
@@ -567,6 +785,7 @@ IDBFactory.prototype.open = function (name /* , version */) {
567
785
  /**
568
786
  * Deletes a database.
569
787
  * @param {string} name
788
+ * @this {IDBFactoryFull}
570
789
  * @returns {IDBOpenDBRequest}
571
790
  */
572
791
  IDBFactory.prototype.deleteDatabase = function (name) {
@@ -584,6 +803,7 @@ IDBFactory.prototype.deleteDatabase = function (name) {
584
803
  name = String(name); // cast to a string
585
804
  const sqlSafeName = util.escapeSQLiteStatement(name);
586
805
 
806
+ /** @type {string} */
587
807
  let escapedDatabaseName;
588
808
  // eslint-disable-next-line no-useless-catch
589
809
  try {
@@ -600,6 +820,12 @@ IDBFactory.prototype.deleteDatabase = function (name) {
600
820
  let calledDBError = false;
601
821
  let version = 0;
602
822
 
823
+ /**
824
+ *
825
+ * @param {boolean} err
826
+ * @param {(erred?: boolean) => void} cb
827
+ * @returns {void}
828
+ */
603
829
  let sysdbFinishedCbDelete = function (err, cb) {
604
830
  cb(err);
605
831
  };
@@ -607,20 +833,27 @@ IDBFactory.prototype.deleteDatabase = function (name) {
607
833
  // Although the spec has no specific conditions where an error
608
834
  // may occur in `deleteDatabase`, it does provide for
609
835
  // `UnknownError` as we may require upon a SQL deletion error
836
+ /**
837
+ *
838
+ * @param {SQLTransaction|SQLError|Error} tx
839
+ * @param {SQLError|boolean} [err]
840
+ * @returns {boolean}
841
+ */
610
842
  function dbError (tx, err) {
611
843
  if (calledDBError || err === true) {
612
- return;
844
+ return false;
613
845
  }
614
- err = webSQLErrback(err || tx);
846
+ const er = webSQLErrback(/** @type {SQLError} */ (err || tx));
615
847
  sysdbFinishedCbDelete(true, function () {
616
848
  req.__done = true;
617
- req.__error = err;
849
+ req.__error = er;
618
850
  req.__result = undefined; // Must be undefined if an error per `result` getter
619
851
  // Re: why bubbling here (and how cancelable is only really relevant for `window.onerror`) see: https://github.com/w3c/IndexedDB/issues/86
620
- const e = createEvent('error', err, {bubbles: true, cancelable: true});
852
+ const e = createEvent('error', er, {bubbles: true, cancelable: true});
621
853
  req.dispatchEvent(e);
622
854
  calledDBError = true;
623
855
  });
856
+ return false;
624
857
  }
625
858
 
626
859
  addRequestToConnectionQueue(req, name, /* origin */ undefined, function (req) {
@@ -628,13 +861,19 @@ IDBFactory.prototype.deleteDatabase = function (name) {
628
861
  // function callback (cb) { cb(); }
629
862
  // callback(function () {
630
863
 
864
+ /**
865
+ * @returns {void}
866
+ */
631
867
  function completeDatabaseDelete () {
632
868
  req.__result = undefined;
633
869
  req.__done = true;
634
- const e = new IDBVersionChangeEvent('success', {oldVersion: version, newVersion: null});
870
+ const e = /** @type {Event & IDBVersionChangeEvent} */ (
871
+ new IDBVersionChangeEvent('success', {oldVersion: version, newVersion: null})
872
+ );
635
873
  req.dispatchEvent(e);
636
874
  }
637
875
 
876
+ /** @type {DatabaseDeleted} */
638
877
  function databaseDeleted () {
639
878
  sysdbFinishedCbDelete(false, function () {
640
879
  if (useDatabaseCache && name in websqlDBCache) {
@@ -668,7 +907,7 @@ IDBFactory.prototype.deleteDatabase = function (name) {
668
907
  // above code with `DELETE FROM dbVersions`)
669
908
  cleanupDatabaseResources(me.__openDatabase, name, escapedDatabaseName, databaseDeleted, dbError);
670
909
  }, dbError);
671
- }, dbError, null, function (currentTask, err, done, rollback, commit) {
910
+ }, dbError, undefined, function (currentTask, err, done, rollback, commit) {
672
911
  if (currentTask.readOnly || err) {
673
912
  return true;
674
913
  }
@@ -682,6 +921,7 @@ IDBFactory.prototype.deleteDatabase = function (name) {
682
921
  return false;
683
922
  });
684
923
  return undefined;
924
+ // @ts-expect-error It's ok
685
925
  }, dbError);
686
926
  return undefined;
687
927
  }, dbError);
@@ -692,6 +932,13 @@ IDBFactory.prototype.deleteDatabase = function (name) {
692
932
  return req;
693
933
  };
694
934
 
935
+ /**
936
+ *
937
+ * @param {import('./Key.js').Key} key1
938
+ * @param {import('./Key.js').Key} key2
939
+ * @throws {TypeError}
940
+ * @returns {0|1|-1}
941
+ */
695
942
  IDBFactory.prototype.cmp = function (key1, key2) {
696
943
  if (!(this instanceof IDBFactory)) {
697
944
  throw new TypeError('Illegal invocation');
@@ -709,7 +956,11 @@ IDBFactory.prototype.cmp = function (key1, key2) {
709
956
  /**
710
957
  * May return outdated information if a database has since been deleted.
711
958
  * @see https://github.com/w3c/IndexedDB/pull/240/files
712
- * @returns {Promise<string[]>}
959
+ * @this {IDBFactoryFull}
960
+ * @returns {Promise<{
961
+ * name: string,
962
+ * version: Integer
963
+ * }[]>}
713
964
  */
714
965
  IDBFactory.prototype.databases = function () {
715
966
  const me = this;
@@ -721,13 +972,20 @@ IDBFactory.prototype.databases = function () {
721
972
  if (hasNullOrigin()) {
722
973
  throw createDOMException('SecurityError', 'Cannot get IndexedDB database names from an opaque origin.');
723
974
  }
975
+ /**
976
+ *
977
+ * @param {true|SQLTransaction|SQLError|DOMException|Error} tx
978
+ * @param {SQLError|DOMException|Error} [err]
979
+ * @returns {boolean}
980
+ */
724
981
  function dbGetDatabaseNamesError (tx, err) {
725
982
  if (calledDbCreateError) {
726
- return;
983
+ return false;
727
984
  }
728
- err = err ? webSQLErrback(err) : tx;
985
+ const er = err ? webSQLErrback(/** @type {SQLError} */ (err)) : tx;
729
986
  calledDbCreateError = true;
730
- reject(err);
987
+ reject(er);
988
+ return false;
731
989
  }
732
990
  createSysDB(me.__openDatabase, function () {
733
991
  sysdb.readTransaction(function (sysReadTx) {
@@ -756,15 +1014,22 @@ IDBFactory.prototype.databases = function () {
756
1014
  * @param {Integer} connIdx
757
1015
  * @param {string} msg
758
1016
  * @throws {TypeError}
1017
+ * @this {IDBFactoryFull}
759
1018
  * @returns {void}
760
1019
  */
761
1020
  IDBFactory.prototype.__forceClose = function (dbName, connIdx, msg) {
762
1021
  const me = this;
1022
+ /**
1023
+ *
1024
+ * @param {import('./IDBDatabase.js').IDBDatabaseFull} conn
1025
+ * @returns {void}
1026
+ */
763
1027
  function forceClose (conn) {
764
1028
  conn.__forceClose(msg);
765
1029
  }
766
1030
  if (util.isNullish(dbName)) {
767
- Object.values(me.__connections).forEach((conn) => {
1031
+ (Object.values(me.__connections)).forEach((conn) => {
1032
+ // @ts-expect-error It's ok
768
1033
  forceClose(conn);
769
1034
  });
770
1035
  } else if (!me.__connections[dbName]) {
@@ -783,6 +1048,11 @@ IDBFactory.prototype.__forceClose = function (dbName, connIdx, msg) {
783
1048
  }
784
1049
  };
785
1050
 
1051
+ /**
1052
+ *
1053
+ * @param {string} [origin]
1054
+ * @returns {void}
1055
+ */
786
1056
  IDBFactory.prototype.__setConnectionQueueOrigin = function (origin = getOrigin()) {
787
1057
  connectionQueue[origin] = {};
788
1058
  };