indexeddbshim 17.3.3 → 17.3.4

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "indexeddbshim",
3
- "version": "17.3.3",
3
+ "version": "17.3.4",
4
4
  "type": "module",
5
5
  "author": "Parashuram <code@nparashuram.com>",
6
6
  "contributors": [
@@ -172,7 +172,7 @@
172
172
  "eventtargeter": "0.12.0",
173
173
  "sync-promise-expanded": "^2.0.0",
174
174
  "typeson": "10.2.0",
175
- "typeson-registry": "14.2.2",
175
+ "typeson-registry": "14.3.0",
176
176
  "websql-configurable": "^3.1.0"
177
177
  },
178
178
  "devDependencies": {
@@ -214,7 +214,7 @@
214
214
  "npm-run-all": "^4.1.5",
215
215
  "open-cli": "^9.0.0",
216
216
  "rimraf": "^6.1.3",
217
- "rollup": "^4.62.5",
217
+ "rollup": "^4.63.0",
218
218
  "rollup-plugin-filesize": "^10.0.0",
219
219
  "rollup-plugin-node-polyfills": "^0.2.1",
220
220
  "sinon": "^22.1.0",
package/src/IDBFactory.js CHANGED
@@ -557,6 +557,7 @@ IDBFactory.prototype.open = function (name /* , version */) {
557
557
 
558
558
  const req = IDBOpenDBRequest.__createInstance();
559
559
  let calledDbCreateError = false;
560
+ let isRevertingSysdb = false;
560
561
 
561
562
  if (CFG.autoName && name === '') {
562
563
  // eslint-disable-next-line unicorn/no-top-level-assignment-in-function -- Necessary?
@@ -585,7 +586,7 @@ IDBFactory.prototype.open = function (name /* , version */) {
585
586
  * @returns {boolean}
586
587
  */
587
588
  function dbCreateError (tx, err) {
588
- if (calledDbCreateError) {
589
+ if (calledDbCreateError || isRevertingSysdb) {
589
590
  return false;
590
591
  }
591
592
  // Defensive cleanup: `pendingVersionChanges.set(name, ...)` (see
@@ -647,46 +648,63 @@ IDBFactory.prototype.open = function (name /* , version */) {
647
648
  */
648
649
  let sysdbFinishedCb = function (systx, err, cb) {
649
650
  if (err) {
650
- try {
651
- systx.executeSql('ROLLBACK', [], cb, cb);
652
- } catch (err) {
653
- // Browser may fail with expired transaction above so
654
- // no choice but to manually revert
655
- sysdb.transaction(function (systx) {
656
- /**
657
- *
658
- * @param {string} msg
659
- * @throws {Error}
660
- * @returns {never}
661
- */
662
- function reportError (msg) {
663
- throw new Error('Unable to roll back upgrade transaction!' + (msg || ''));
651
+ /**
652
+ * @param {any} [errorToShow]
653
+ * @returns {void}
654
+ */
655
+ const manualRevert = function (errorToShow) {
656
+ /**
657
+ * @param {string} [msg]
658
+ * @throws {Error}
659
+ * @returns {never}
660
+ */
661
+ function reportError (msg) {
662
+ throw new Error('Unable to roll back upgrade transaction!' + (msg || ''));
663
+ }
664
+
665
+ sysdb.transaction(
666
+ function (systx) {
667
+ // Attempt to revert
668
+ if (oldVersion === 0) {
669
+ systx.executeSql(
670
+ 'DELETE FROM dbVersions WHERE "name" = ?',
671
+ [sqlSafeName]
672
+ );
673
+ } else {
674
+ systx.executeSql(
675
+ 'UPDATE dbVersions SET "version" = ? WHERE "name" = ?',
676
+ [oldVersion, sqlSafeName]
677
+ );
678
+ }
679
+ },
680
+ function (sqlErr) {
681
+ isRevertingSysdb = false;
682
+ cb(sqlErr); // eslint-disable-line promise/no-callback-in-promise -- Convenient
683
+ },
684
+ function () {
685
+ isRevertingSysdb = false;
686
+ cb(errorToShow || reportError); // eslint-disable-line promise/no-callback-in-promise -- Convenient
664
687
  }
688
+ );
689
+ };
665
690
 
666
- // Attempt to revert
667
- if (oldVersion === 0) {
668
- systx.executeSql(
669
- 'DELETE FROM dbVersions WHERE "name" = ?',
670
- [sqlSafeName],
671
- function () {
672
- // @ts-expect-error Force to work
673
- cb(reportError); // eslint-disable-line promise/no-callback-in-promise -- Convenient
674
- },
675
- // @ts-expect-error Force to work
676
- reportError
677
- );
678
- } else {
679
- systx.executeSql(
680
- 'UPDATE dbVersions SET "version" = ? WHERE "name" = ?',
681
- [
682
- oldVersion, sqlSafeName
683
- ],
684
- cb,
685
- // @ts-expect-error Force to work
686
- reportError
687
- );
691
+ try {
692
+ systx.executeSql(
693
+ 'ROLLBACK',
694
+ [],
695
+ function () {
696
+ cb(); // eslint-disable-line promise/no-callback-in-promise -- Convenient
697
+ },
698
+ function (tx, sqlErr) {
699
+ // Browser/Node may fail with expired transaction, so manually revert
700
+ manualRevert(sqlErr);
701
+ return false;
688
702
  }
689
- });
703
+ );
704
+ } catch (e) {
705
+ // Browser may fail with expired transaction above so
706
+ // no choice but to manually revert
707
+ manualRevert(e);
690
708
  }
691
709
  return;
692
710
  }
@@ -790,6 +808,7 @@ IDBFactory.prototype.open = function (name /* , version */) {
790
808
 
791
809
  // eslint-disable-next-line camelcase -- Clear API
792
810
  req.transaction.on__preabort = function () {
811
+ isRevertingSysdb = true;
793
812
  pendingVersionChanges.delete(name);
794
813
  connection.__upgradeTransaction = null;
795
814
  // We ensure any cache is deleted before any request error events fire and try to reopen
@@ -808,6 +827,7 @@ IDBFactory.prototype.open = function (name /* , version */) {
808
827
  req.__done = false;
809
828
 
810
829
  connection.close();
830
+ isRevertingSysdb = true;
811
831
  setTimeout(() => {
812
832
  const err = createDOMException('AbortError', 'The upgrade transaction was aborted.');
813
833
  sysdbFinishedCb(systx, err, function (reportError) {
@@ -876,7 +896,36 @@ IDBFactory.prototype.open = function (name /* , version */) {
876
896
  }
877
897
  sysdbFinishedCb = function (systx, err, cb) {
878
898
  if (err) {
879
- rollback(err, cb);
899
+ rollback(err,
900
+ /**
901
+ * @param {Error} [reportError]
902
+ * @returns {void}
903
+ */
904
+ function (reportError) {
905
+ sysdb.transaction(
906
+ function (systx) {
907
+ if (oldVersion === 0) {
908
+ systx.executeSql(
909
+ 'DELETE FROM dbVersions WHERE "name" = ?',
910
+ [sqlSafeName]
911
+ );
912
+ } else {
913
+ systx.executeSql(
914
+ 'UPDATE dbVersions SET "version" = ? WHERE "name" = ?',
915
+ [oldVersion, sqlSafeName]
916
+ );
917
+ }
918
+ },
919
+ function (sqlErr) {
920
+ isRevertingSysdb = false;
921
+ cb(sqlErr); // eslint-disable-line promise/no-callback-in-promise -- Convenient
922
+ },
923
+ function () {
924
+ isRevertingSysdb = false;
925
+ cb(reportError); // eslint-disable-line promise/no-callback-in-promise -- Convenient
926
+ }
927
+ );
928
+ });
880
929
  } else {
881
930
  commit(cb);
882
931
  }
@@ -8,6 +8,7 @@ import IDBObjectStore from './IDBObjectStore.js';
8
8
  import CFG from './CFG.js';
9
9
 
10
10
  let uniqueID = 0;
11
+ const activeTransactions = new Set();
11
12
  const listeners = ['onabort', 'oncomplete', 'onerror'];
12
13
  const readonlyProperties = ['objectStoreNames', 'mode', 'durability', 'db', 'error'];
13
14
 
@@ -137,6 +138,7 @@ IDBTransaction.__createInstance = function (db, storeNames, mode, durability = '
137
138
  me.__mode = mode;
138
139
  me.__durability = durability;
139
140
  me.__db = db;
141
+ activeTransactions.add(me);
140
142
  me.__error = null;
141
143
  // @ts-expect-error Part of `ShimEventTarget`
142
144
  me.__setOptions({
@@ -641,6 +643,7 @@ IDBTransaction.prototype.__executeRequests = function () {
641
643
  me.__errored = true;
642
644
  throw e;
643
645
  } finally {
646
+ activeTransactions.delete(me);
644
647
  me.__storeHandles = {};
645
648
  }
646
649
  }
@@ -855,6 +858,7 @@ IDBTransaction.prototype.__abortTransaction = function (err) {
855
858
  });
856
859
  }
857
860
  me.__active = false; // Setting here and in requestsFinished for https://github.com/w3c/IndexedDB/issues/87
861
+ activeTransactions.delete(me);
858
862
 
859
863
  if (err !== null) {
860
864
  me.__error = err;
@@ -1064,7 +1068,7 @@ IDBTransaction.__assertNotFinishedObjectStoreMethod = function (tx) {
1064
1068
  * @returns {void}
1065
1069
  */
1066
1070
  IDBTransaction.__assertActive = function (tx) {
1067
- if (!tx || !tx.__active || tx.__committed) {
1071
+ if (!tx || !tx.__active || !tx.__handlerActive || tx.__committed) {
1068
1072
  throw createDOMException('TransactionInactiveError', 'A request was placed against a transaction which is currently not active, or which is finished');
1069
1073
  }
1070
1074
  };
@@ -1095,4 +1099,6 @@ Object.defineProperty(IDBTransaction, 'prototype', {
1095
1099
  });
1096
1100
  /* eslint-enable unicorn/no-top-level-side-effects -- Would be good */
1097
1101
 
1102
+ IDBTransaction.activeTransactions = activeTransactions;
1103
+
1098
1104
  export default IDBTransaction;
package/src/Key.js CHANGED
@@ -1,3 +1,22 @@
1
+ /**
2
+ * @param {unknown[]} arr
3
+ * @param {number} index
4
+ * @param {unknown} val
5
+ * @returns {void}
6
+ */
7
+ const setArrayValue = (arr, index, val) => {
8
+ if (Reflect.has(Array.prototype, index)) {
9
+ Object.defineProperty(arr, index, {value: val, enumerable: true, writable: true, configurable: true});
10
+ } else {
11
+ arr[index] = val;
12
+ }
13
+ };
14
+ /**
15
+ * @param {unknown[]} arr
16
+ * @param {unknown} val
17
+ * @returns {void}
18
+ */
19
+ const safePush = (arr, val) => setArrayValue(arr, arr.length, val);
1
20
  import {createDOMException} from './DOMException.js';
2
21
  import * as util from './util.js';
3
22
  import cmp from './cmp.js';
@@ -281,12 +300,13 @@ const types = {
281
300
  * @returns {string}
282
301
  */
283
302
  encode (key) {
303
+ /** @type {(string|null)[]} */
284
304
  const encoded = [];
285
305
  for (const [i, item] of key.entries()) {
286
306
  const encodedItem = encode(item, true); // encode the array item
287
- encoded[i] = encodedItem;
307
+ setArrayValue(encoded, i, encodedItem);
288
308
  }
289
- encoded.push(keyTypeToEncodedChar.invalid + '-'); // append an extra item, so empty arrays sort correctly
309
+ safePush(encoded, keyTypeToEncodedChar.invalid + '-'); // append an extra item, so empty arrays sort correctly
290
310
  let encodedKey = JSON.stringify(encoded);
291
311
  if (CFG.escapeNULForSQLiteStatements === false) {
292
312
  encodedKey = encodedKey.replaceAll(String.raw`\u0000`, '\0');
@@ -307,7 +327,7 @@ const types = {
307
327
  for (let i = 0; i < decoded.length; i++) {
308
328
  const item = decoded[i];
309
329
  const decodedItem = decode(item, true); // decode the item
310
- decoded[i] = decodedItem;
330
+ setArrayValue(decoded, i, decodedItem);
311
331
  }
312
332
  return decoded;
313
333
  }
@@ -577,7 +597,7 @@ function convertValueToKeyValueDecoded (input, seen, multiEntry, fullKeys) {
577
597
  } case 'array': { // May throw (from binary)
578
598
  const arr = /** @type {Array<any>} */ (input);
579
599
  const len = arr.length;
580
- seen.push(input);
600
+ safePush(seen, input);
581
601
 
582
602
  /** @type {(KeyValueObject|Value)[]} */
583
603
  const keys = [];
@@ -598,7 +618,7 @@ function convertValueToKeyValueDecoded (input, seen, multiEntry, fullKeys) {
598
618
  (!fullKeys && keys.every((k) => cmp(k, key.value) !== 0)) ||
599
619
  (fullKeys && keys.every((k) => cmp(k, key) !== 0))
600
620
  ) {
601
- keys.push(fullKeys ? key : key.value);
621
+ safePush(keys, fullKeys ? key : key.value);
602
622
  }
603
623
  } catch (err) {
604
624
  if (!multiEntry) {
@@ -732,7 +752,7 @@ function evaluateKeyPathOnValueToDecodedValue (value, keyPath, multiEntry, fullK
732
752
  if (key.failure) {
733
753
  return true;
734
754
  }
735
- result.push(key.value);
755
+ safePush(result, key.value);
736
756
  return false;
737
757
  })
738
758
  ? {failure: true}
@@ -793,11 +813,11 @@ function injectKeyIntoValueUsingKeyPath (value, key, keyPath) {
793
813
  identifiers.forEach((identifier) => {
794
814
  const hop = Object.hasOwn(value, identifier);
795
815
  if (!hop) {
796
- value[identifier] = {};
816
+ Object.defineProperty(value, identifier, {value: {}, enumerable: true, writable: true, configurable: true});
797
817
  }
798
818
  value = value[identifier];
799
819
  });
800
- value[/** @type {string} */ (last)] = key; // key is already a `keyValue` in our processing so no need to convert
820
+ Object.defineProperty(value, /** @type {string} */ (last), {value: key, enumerable: true, writable: true, configurable: true}); // key is already a `keyValue` in our processing so no need to convert
801
821
  }
802
822
 
803
823
  /**
@@ -883,6 +903,7 @@ function isMultiEntryMatch (encodedEntry, encodedKey) {
883
903
  * @returns {Key[]}
884
904
  */
885
905
  function findMultiEntryMatches (keyEntry, range) {
906
+ /** @type {unknown[]} */
886
907
  const matches = [];
887
908
 
888
909
  if (Array.isArray(keyEntry)) {
@@ -896,18 +917,18 @@ function findMultiEntryMatches (keyEntry, range) {
896
917
  } else {
897
918
  const nested = findMultiEntryMatches(key, range);
898
919
  if (nested.length > 0) {
899
- matches.push(key);
920
+ safePush(matches, key);
900
921
  }
901
922
  continue;
902
923
  }
903
924
  }
904
925
 
905
926
  if (util.isNullish(range) || isKeyInRange(key, range, true)) {
906
- matches.push(key);
927
+ safePush(matches, key);
907
928
  }
908
929
  }
909
930
  } else if (util.isNullish(range) || isKeyInRange(keyEntry, range, true)) {
910
- matches.push(keyEntry);
931
+ safePush(matches, keyEntry);
911
932
  }
912
933
  return matches;
913
934
  }
@@ -924,12 +945,13 @@ function convertKeyToValue (key) {
924
945
  case 'number': case 'string': {
925
946
  return value;
926
947
  } case 'array': {
948
+ /** @type {ValueType[]} */
927
949
  const array = [];
928
950
  const len = value.length;
929
951
  let index = 0;
930
952
  while (index < len) {
931
953
  const entry = convertKeyToValue(value[index]);
932
- array[index] = entry;
954
+ setArrayValue(array, index, entry);
933
955
  index++;
934
956
  }
935
957
  return array;
package/src/Sca.js CHANGED
@@ -1,12 +1,62 @@
1
1
  import {
2
- Typeson, hasConstructorOf, structuredCloningThrowing
2
+ Typeson, hasConstructorOf, structuredCloningForStorage
3
3
  } from 'typeson-registry';
4
4
 
5
5
  import {createDOMException, ShimDOMException} from './DOMException.js';
6
6
 
7
7
  // See: https://stackoverflow.com/questions/42170826/categories-for-rejection-by-the-structured-cloning-algorithm
8
8
 
9
- let typeson = new Typeson().register(structuredCloningThrowing);
9
+ // Although typeson-registry already has a FileList type in its structured cloning presets,
10
+ // we need to override it so it works with our tests
11
+
12
+ const specSet = structuredCloningForStorage
13
+ .flatMap((preset) => (Array.isArray(preset) ? preset : [preset]))
14
+ .find((preset) => preset && !Array.isArray(preset) && 'filelist' in preset);
15
+
16
+ const origFileList = specSet && !Array.isArray(specSet) && 'filelist' in specSet
17
+ ? specSet.filelist
18
+ : undefined;
19
+
20
+ const origTest = origFileList && typeof origFileList === 'object' && 'test' in origFileList && typeof origFileList.test === 'function'
21
+ ? origFileList.test
22
+ : undefined;
23
+
24
+ const origRevive = origFileList && typeof origFileList === 'object' && 'revive' in origFileList && typeof origFileList.revive === 'function'
25
+ ? origFileList.revive
26
+ : undefined;
27
+
28
+ const customFileList = origFileList
29
+ ? {
30
+ ...origFileList,
31
+ /**
32
+ * @param {unknown} x
33
+ * @param {import('typeson').StateObject} state
34
+ * @returns {boolean}
35
+ */
36
+ test (x, state) {
37
+ if (typeof FileList !== 'undefined') {
38
+ return x instanceof FileList;
39
+ }
40
+ return typeof origTest === 'function' ? origTest(x, state) : false;
41
+ },
42
+ /**
43
+ * @param {unknown} x
44
+ * @param {import('typeson').StateObject} state
45
+ * @returns {unknown}
46
+ */
47
+ revive (x, state) {
48
+ if (typeof FileList !== 'undefined') {
49
+ return Reflect.construct(FileList, [x]);
50
+ }
51
+ return typeof origRevive === 'function' ? origRevive(x, state) : undefined;
52
+ }
53
+ }
54
+ : undefined;
55
+
56
+ let typeson = new Typeson().register([
57
+ structuredCloningForStorage,
58
+ customFileList ? {filelist: customFileList} : {}
59
+ ]);
10
60
 
11
61
  /**
12
62
  * @param {(preset: import('typeson-registry').Preset) =>
@@ -15,7 +65,7 @@ let typeson = new Typeson().register(structuredCloningThrowing);
15
65
  */
16
66
  function register (func) {
17
67
  // eslint-disable-next-line unicorn/no-top-level-assignment-in-function -- Should be one-time cache
18
- typeson = new Typeson().register(func(structuredCloningThrowing));
68
+ typeson = new Typeson().register(func(structuredCloningForStorage));
19
69
  }
20
70
 
21
71
  /**