indexeddbshim 16.0.0 → 16.1.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.
- package/README.md +1 -1
- package/badges/licenses-badge-dev.svg +1 -1
- package/dist/IDBCursor.d.ts +2 -1
- package/dist/IDBCursor.d.ts.map +1 -1
- package/dist/IDBFactory.d.ts.map +1 -1
- package/dist/Key.d.ts.map +1 -1
- package/dist/indexeddbshim-Key.js +1668 -1838
- package/dist/indexeddbshim-Key.js.map +1 -1
- package/dist/indexeddbshim-Key.min.js +2 -2
- package/dist/indexeddbshim-Key.min.js.map +1 -1
- package/dist/indexeddbshim-UnicodeIdentifiers-node.cjs +546 -550
- package/dist/indexeddbshim-UnicodeIdentifiers-node.cjs.map +1 -1
- package/dist/indexeddbshim-UnicodeIdentifiers.js +1812 -2495
- package/dist/indexeddbshim-UnicodeIdentifiers.js.map +1 -1
- package/dist/indexeddbshim-UnicodeIdentifiers.min.js +2 -4
- package/dist/indexeddbshim-UnicodeIdentifiers.min.js.map +1 -1
- package/dist/indexeddbshim-node.cjs +546 -550
- package/dist/indexeddbshim-node.cjs.map +1 -1
- package/dist/indexeddbshim-noninvasive.js +1810 -2493
- package/dist/indexeddbshim-noninvasive.js.map +1 -1
- package/dist/indexeddbshim-noninvasive.min.js +2 -4
- package/dist/indexeddbshim-noninvasive.min.js.map +1 -1
- package/dist/indexeddbshim.js +1811 -2486
- package/dist/indexeddbshim.js.map +1 -1
- package/dist/indexeddbshim.min.js +2 -4
- package/dist/indexeddbshim.min.js.map +1 -1
- package/dist/setGlobalVars.d.ts.map +1 -1
- package/package.json +20 -21
- package/src/IDBCursor.js +7 -3
- package/src/IDBFactory.js +0 -1
- package/src/Key.js +4 -0
- package/src/browser-UnicodeIdentifiers.js +2 -2
- package/src/browser-noninvasive.js +1 -1
- package/src/browser.js +1 -1
- package/src/setGlobalVars.js +0 -2
- package/src/unicode-regex.js +1 -1
|
@@ -1,1383 +1,1235 @@
|
|
|
1
|
-
/*! indexeddbshim - v16.
|
|
1
|
+
/*! indexeddbshim - v16.1.0 - 9/1/2025 */
|
|
2
2
|
|
|
3
3
|
(function (global, factory) {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
4
|
+
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
|
|
5
|
+
typeof define === 'function' && define.amd ? define(['exports'], factory) :
|
|
6
|
+
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.IDBKeyUtils = {}));
|
|
7
7
|
})(this, (function (exports) { 'use strict';
|
|
8
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
|
-
|
|
9
|
+
/* eslint-disable jsdoc/valid-types -- https://github.com/jsdoc-type-pratt-parser/jsdoc-type-pratt-parser/issues/147 */
|
|
10
|
+
/**
|
|
11
|
+
* @typedef {T[keyof T]} ValueOf<T>
|
|
12
|
+
* @template T
|
|
13
|
+
*/
|
|
14
|
+
/* eslint-enable jsdoc/valid-types -- https://github.com/jsdoc-type-pratt-parser/jsdoc-type-pratt-parser/issues/147 */
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* @typedef {{unlink: (path: string, cb: import('fs').NoParamCallback) => void}} FSApi
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* @typedef {{
|
|
22
|
+
* DEBUG: boolean,
|
|
23
|
+
* cacheDatabaseInstances: boolean,
|
|
24
|
+
* autoName: boolean,
|
|
25
|
+
* fullIDLSupport: boolean,
|
|
26
|
+
* checkOrigin: boolean,
|
|
27
|
+
* cursorPreloadPackSize: number,
|
|
28
|
+
* UnicodeIDStart: string,
|
|
29
|
+
* UnicodeIDContinue: string,
|
|
30
|
+
* registerSCA: (
|
|
31
|
+
* preset: import('typeson').Preset
|
|
32
|
+
* ) => import('typeson').Preset,
|
|
33
|
+
* avoidAutoShim: boolean,
|
|
34
|
+
* win: {
|
|
35
|
+
* openDatabase: (name: string, version: string, displayName: string, estimatedSize: number) => import('websql-configurable').default
|
|
36
|
+
* },
|
|
37
|
+
* DEFAULT_DB_SIZE: number,
|
|
38
|
+
* useSQLiteIndexes: boolean,
|
|
39
|
+
* fs: FSApi,
|
|
40
|
+
* addNonIDBGlobals: boolean,
|
|
41
|
+
* replaceNonIDBGlobals: boolean,
|
|
42
|
+
* escapeDatabaseName: (name: string) => string,
|
|
43
|
+
* unescapeDatabaseName: (name: string) => string,
|
|
44
|
+
* databaseCharacterEscapeList: string|false,
|
|
45
|
+
* databaseNameLengthLimit: number|false,
|
|
46
|
+
* escapeNFDForDatabaseNames: boolean,
|
|
47
|
+
* addSQLiteExtension: boolean,
|
|
48
|
+
* memoryDatabase: string,
|
|
49
|
+
* deleteDatabaseFiles: boolean,
|
|
50
|
+
* databaseBasePath: string,
|
|
51
|
+
* sysDatabaseBasePath: string,
|
|
52
|
+
* sqlBusyTimeout: number,
|
|
53
|
+
* sqlTrace: () => void,
|
|
54
|
+
* sqlProfile: () => void,
|
|
55
|
+
* createIndexes: boolean
|
|
56
|
+
* }} ConfigValues
|
|
57
|
+
*/
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* @typedef {ValueOf<ConfigValues>} ConfigValue
|
|
61
|
+
*/
|
|
62
|
+
|
|
63
|
+
/** @type {{[key: string]: ConfigValue}} */
|
|
64
|
+
const map = {};
|
|
65
|
+
const CFG = /** @type {ConfigValues} */{};
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* @typedef {keyof ConfigValues} KeyofConfigValues
|
|
69
|
+
*/
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* @typedef {KeyofConfigValues[]} Config
|
|
73
|
+
*/
|
|
74
|
+
|
|
75
|
+
/** @type {Config} */
|
|
76
|
+
[
|
|
77
|
+
// Boolean for verbose reporting
|
|
78
|
+
'DEBUG',
|
|
79
|
+
// Effectively defaults to false (ignored unless `true`)
|
|
80
|
+
|
|
81
|
+
// Boolean (effectively defaults to true) on whether to cache WebSQL
|
|
82
|
+
// `openDatabase` instances
|
|
83
|
+
'cacheDatabaseInstances',
|
|
84
|
+
// Boolean on whether to auto-name databases (based on an
|
|
85
|
+
// auto-increment) when the empty string is supplied; useful with
|
|
86
|
+
// `memoryDatabase`; defaults to `false` which means the empty string
|
|
87
|
+
// will be used as the (valid) database name
|
|
88
|
+
'autoName',
|
|
89
|
+
// Determines whether the slow-performing `Object.setPrototypeOf`
|
|
90
|
+
// calls required for full WebIDL compliance will be used. Probably
|
|
91
|
+
// only needed for testing or environments where full introspection
|
|
92
|
+
// on class relationships is required; see
|
|
93
|
+
// http://stackoverflow.com/questions/41927589/rationales-consequences-of-webidl-class-inheritance-requirements
|
|
94
|
+
'fullIDLSupport',
|
|
95
|
+
// Effectively defaults to false (ignored unless `true`)
|
|
96
|
+
|
|
97
|
+
// Boolean on whether to perform origin checks in `IDBFactory` methods
|
|
98
|
+
// Effectively defaults to `true` (must be set to `false` to cancel checks)
|
|
99
|
+
'checkOrigin',
|
|
100
|
+
// Used by `IDBCursor` continue methods for number of records to cache;
|
|
101
|
+
// Defaults to 100
|
|
102
|
+
'cursorPreloadPackSize',
|
|
103
|
+
// See optional API (`shimIndexedDB.__setUnicodeIdentifiers`);
|
|
104
|
+
// or just use the Unicode builds which invoke this method
|
|
105
|
+
// automatically using the large, fully spec-compliant, regular
|
|
106
|
+
// expression strings of `src/UnicodeIdentifiers.js`)
|
|
107
|
+
// In the non-Unicode builds, defaults to /[$A-Z_a-z]/
|
|
108
|
+
'UnicodeIDStart',
|
|
109
|
+
// In the non-Unicode builds, defaults to /[$0-9A-Z_a-z]/
|
|
110
|
+
'UnicodeIDContinue',
|
|
111
|
+
// Used by SCA.js for optional restructuring of typeson-registry
|
|
112
|
+
// Structured Cloning Algorithm; should only be needed for ensuring data
|
|
113
|
+
// created in 3.* versions of IndexedDBShim continue to work; see the
|
|
114
|
+
// library `typeson-registry-sca-reverter` to get a function to do this
|
|
115
|
+
'registerSCA',
|
|
116
|
+
// BROWSER-SPECIFIC CONFIG
|
|
117
|
+
'avoidAutoShim',
|
|
118
|
+
// Where WebSQL is detected but where `indexedDB` is
|
|
119
|
+
// missing or poor support is known (non-Chrome Android or
|
|
120
|
+
// non-Safari iOS9), the shim will be auto-applied without
|
|
121
|
+
// `shimIndexedDB.__useShim()`. Set this to `true` to avoid forcing
|
|
122
|
+
// the shim for such cases.
|
|
123
|
+
|
|
124
|
+
// -----------SQL CONFIG----------
|
|
125
|
+
// Object (`window` in the browser) on which there may be an
|
|
126
|
+
// `openDatabase` method (if any) for WebSQL. (The browser
|
|
127
|
+
// throws if attempting to call `openDatabase` without the window
|
|
128
|
+
// so this is why the config doesn't just allow the function.)
|
|
129
|
+
// Defaults to `window` or `self` in browser builds or
|
|
130
|
+
// a singleton object with the `openDatabase` method set to
|
|
131
|
+
// the "websql" package in Node.
|
|
132
|
+
'win',
|
|
133
|
+
// For internal `openDatabase` calls made by `IDBFactory` methods;
|
|
134
|
+
// per the WebSQL spec, "User agents are expected to use the display name
|
|
135
|
+
// and the estimated database size to optimize the user experience.
|
|
136
|
+
// For example, a user agent could use the estimated size to suggest an
|
|
137
|
+
// initial quota to the user. This allows a site that is aware that it
|
|
138
|
+
// will try to use hundreds of megabytes to declare this upfront, instead
|
|
139
|
+
// of the user agent prompting the user for permission to increase the
|
|
140
|
+
// quota every five megabytes."
|
|
141
|
+
// Defaults to (4 * 1024 * 1024) or (25 * 1024 * 1024) in Safari
|
|
142
|
+
'DEFAULT_DB_SIZE',
|
|
143
|
+
// Whether to create indexes on SQLite tables (and also whether to try
|
|
144
|
+
// dropping)
|
|
145
|
+
// Effectively defaults to `false` (ignored unless `true`)
|
|
146
|
+
'useSQLiteIndexes',
|
|
147
|
+
// NODE-IMPINGING SETTINGS (created for sake of limitations in Node
|
|
148
|
+
// or desktop file system implementation but applied by default in
|
|
149
|
+
// browser for parity)
|
|
150
|
+
|
|
151
|
+
// File system module with `unlink` to remove deleted database files
|
|
152
|
+
'fs',
|
|
153
|
+
// Used when setting global shims to determine whether to try to add
|
|
154
|
+
// other globals shimmed by the library (`ShimDOMException`,
|
|
155
|
+
// `ShimDOMStringList`, `ShimEvent`, `ShimCustomEvent`, `ShimEventTarget`)
|
|
156
|
+
// Effectively defaults to `false` (ignored unless `true`)
|
|
157
|
+
'addNonIDBGlobals',
|
|
158
|
+
// Used when setting global shims to determine whether to try to overwrite
|
|
159
|
+
// other globals shimmed by the library (`DOMException`, `DOMStringList`,
|
|
160
|
+
// `Event`, `CustomEvent`, `EventTarget`)
|
|
161
|
+
// Effectively defaults to `false` (ignored unless `true`)
|
|
162
|
+
'replaceNonIDBGlobals',
|
|
163
|
+
// Overcoming limitations with node-sqlite3/storing database name on
|
|
164
|
+
// file systems
|
|
165
|
+
// https://en.wikipedia.org/wiki/Filename#Reserved_characters_and_words
|
|
166
|
+
// Defaults to prefixing database with `D_`, escaping
|
|
167
|
+
// `databaseCharacterEscapeList`, escaping NUL, and
|
|
168
|
+
// escaping upper case letters, as well as enforcing
|
|
169
|
+
// `databaseNameLengthLimit`
|
|
170
|
+
'escapeDatabaseName',
|
|
171
|
+
// Not used internally; usable as a convenience method
|
|
172
|
+
'unescapeDatabaseName',
|
|
173
|
+
// Defaults to global regex representing the following
|
|
174
|
+
// (characters nevertheless commonly reserved in modern,
|
|
175
|
+
// Unicode-supporting systems): 0x00-0x1F 0x7F " * / : < > ? \ |
|
|
176
|
+
'databaseCharacterEscapeList',
|
|
177
|
+
// Defaults to 254 (shortest typical modern file length limit)
|
|
178
|
+
'databaseNameLengthLimit',
|
|
179
|
+
// Boolean defaulting to true on whether to escape NFD-escaping
|
|
180
|
+
// characters to avoid clashes on MacOS which performs NFD on files
|
|
181
|
+
'escapeNFDForDatabaseNames',
|
|
182
|
+
// Boolean on whether to add the `.sqlite` extension to file names;
|
|
183
|
+
// defaults to `true`
|
|
184
|
+
'addSQLiteExtension',
|
|
185
|
+
// Various types of in-memory databases that can auto-delete
|
|
186
|
+
['memoryDatabase',
|
|
187
|
+
/**
|
|
188
|
+
* @param {string} val
|
|
189
|
+
* @throws {TypeError}
|
|
190
|
+
* @returns {void}
|
|
191
|
+
*/
|
|
192
|
+
val => {
|
|
193
|
+
if (!/^(?::memory:|file::memory:(\?[^#]*)?(#.*)?)?$/u.test(/** @type {string} */val)) {
|
|
194
|
+
throw new TypeError('`memoryDatabase` must be the empty string, ":memory:", or a ' + '"file::memory:[?queryString][#hash] URL".');
|
|
65
195
|
}
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
o = true, n = r;
|
|
85
|
-
} finally {
|
|
86
|
-
try {
|
|
87
|
-
if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return;
|
|
88
|
-
} finally {
|
|
89
|
-
if (o) throw n;
|
|
90
|
-
}
|
|
196
|
+
}],
|
|
197
|
+
// NODE-SPECIFIC CONFIG
|
|
198
|
+
// Boolean on whether to delete the database file itself after
|
|
199
|
+
// `deleteDatabase`; defaults to `true` as the database will be empty
|
|
200
|
+
'deleteDatabaseFiles', 'databaseBasePath', 'sysDatabaseBasePath',
|
|
201
|
+
// NODE-SPECIFIC WEBSQL CONFIG
|
|
202
|
+
'sqlBusyTimeout',
|
|
203
|
+
// Defaults to 1000
|
|
204
|
+
'sqlTrace',
|
|
205
|
+
// Callback not used by default
|
|
206
|
+
'sqlProfile',
|
|
207
|
+
// Callback not used by default
|
|
208
|
+
|
|
209
|
+
'createIndexes'].forEach(prop => {
|
|
210
|
+
/** @type {(val: any) => void} */
|
|
211
|
+
let validator;
|
|
212
|
+
if (Array.isArray(prop)) {
|
|
213
|
+
[prop, validator] = prop;
|
|
91
214
|
}
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
function _slicedToArray(r, e) {
|
|
102
|
-
return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest();
|
|
103
|
-
}
|
|
104
|
-
function _toConsumableArray(r) {
|
|
105
|
-
return _arrayWithoutHoles(r) || _iterableToArray(r) || _unsupportedIterableToArray(r) || _nonIterableSpread();
|
|
106
|
-
}
|
|
107
|
-
function _typeof(o) {
|
|
108
|
-
"@babel/helpers - typeof";
|
|
109
|
-
|
|
110
|
-
return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) {
|
|
111
|
-
return typeof o;
|
|
112
|
-
} : function (o) {
|
|
113
|
-
return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o;
|
|
114
|
-
}, _typeof(o);
|
|
115
|
-
}
|
|
116
|
-
function _unsupportedIterableToArray(r, a) {
|
|
117
|
-
if (r) {
|
|
118
|
-
if ("string" == typeof r) return _arrayLikeToArray(r, a);
|
|
119
|
-
var t = {}.toString.call(r).slice(8, -1);
|
|
120
|
-
return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0;
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
/* eslint-disable jsdoc/valid-types -- https://github.com/jsdoc-type-pratt-parser/jsdoc-type-pratt-parser/issues/147 */
|
|
125
|
-
/**
|
|
126
|
-
* @typedef {T[keyof T]} ValueOf<T>
|
|
127
|
-
* @template T
|
|
128
|
-
*/
|
|
129
|
-
/* eslint-enable jsdoc/valid-types -- https://github.com/jsdoc-type-pratt-parser/jsdoc-type-pratt-parser/issues/147 */
|
|
130
|
-
|
|
131
|
-
/**
|
|
132
|
-
* @typedef {{unlink: (path: string, cb: import('fs').NoParamCallback) => void}} FSApi
|
|
133
|
-
*/
|
|
134
|
-
|
|
135
|
-
/**
|
|
136
|
-
* @typedef {{
|
|
137
|
-
* DEBUG: boolean,
|
|
138
|
-
* cacheDatabaseInstances: boolean,
|
|
139
|
-
* autoName: boolean,
|
|
140
|
-
* fullIDLSupport: boolean,
|
|
141
|
-
* checkOrigin: boolean,
|
|
142
|
-
* cursorPreloadPackSize: number,
|
|
143
|
-
* UnicodeIDStart: string,
|
|
144
|
-
* UnicodeIDContinue: string,
|
|
145
|
-
* registerSCA: (
|
|
146
|
-
* preset: import('typeson').Preset
|
|
147
|
-
* ) => import('typeson').Preset,
|
|
148
|
-
* avoidAutoShim: boolean,
|
|
149
|
-
* win: {
|
|
150
|
-
* openDatabase: (name: string, version: string, displayName: string, estimatedSize: number) => import('websql-configurable').default
|
|
151
|
-
* },
|
|
152
|
-
* DEFAULT_DB_SIZE: number,
|
|
153
|
-
* useSQLiteIndexes: boolean,
|
|
154
|
-
* fs: FSApi,
|
|
155
|
-
* addNonIDBGlobals: boolean,
|
|
156
|
-
* replaceNonIDBGlobals: boolean,
|
|
157
|
-
* escapeDatabaseName: (name: string) => string,
|
|
158
|
-
* unescapeDatabaseName: (name: string) => string,
|
|
159
|
-
* databaseCharacterEscapeList: string|false,
|
|
160
|
-
* databaseNameLengthLimit: number|false,
|
|
161
|
-
* escapeNFDForDatabaseNames: boolean,
|
|
162
|
-
* addSQLiteExtension: boolean,
|
|
163
|
-
* memoryDatabase: string,
|
|
164
|
-
* deleteDatabaseFiles: boolean,
|
|
165
|
-
* databaseBasePath: string,
|
|
166
|
-
* sysDatabaseBasePath: string,
|
|
167
|
-
* sqlBusyTimeout: number,
|
|
168
|
-
* sqlTrace: () => void,
|
|
169
|
-
* sqlProfile: () => void,
|
|
170
|
-
* createIndexes: boolean
|
|
171
|
-
* }} ConfigValues
|
|
172
|
-
*/
|
|
173
|
-
|
|
174
|
-
/**
|
|
175
|
-
* @typedef {ValueOf<ConfigValues>} ConfigValue
|
|
176
|
-
*/
|
|
177
|
-
|
|
178
|
-
/** @type {{[key: string]: ConfigValue}} */
|
|
179
|
-
var map = {};
|
|
180
|
-
var CFG = /** @type {ConfigValues} */{};
|
|
181
|
-
|
|
182
|
-
/**
|
|
183
|
-
* @typedef {keyof ConfigValues} KeyofConfigValues
|
|
184
|
-
*/
|
|
185
|
-
|
|
186
|
-
/**
|
|
187
|
-
* @typedef {KeyofConfigValues[]} Config
|
|
188
|
-
*/
|
|
189
|
-
|
|
190
|
-
/** @type {Config} */
|
|
191
|
-
[
|
|
192
|
-
// Boolean for verbose reporting
|
|
193
|
-
'DEBUG',
|
|
194
|
-
// Effectively defaults to false (ignored unless `true`)
|
|
195
|
-
|
|
196
|
-
// Boolean (effectively defaults to true) on whether to cache WebSQL
|
|
197
|
-
// `openDatabase` instances
|
|
198
|
-
'cacheDatabaseInstances',
|
|
199
|
-
// Boolean on whether to auto-name databases (based on an
|
|
200
|
-
// auto-increment) when the empty string is supplied; useful with
|
|
201
|
-
// `memoryDatabase`; defaults to `false` which means the empty string
|
|
202
|
-
// will be used as the (valid) database name
|
|
203
|
-
'autoName',
|
|
204
|
-
// Determines whether the slow-performing `Object.setPrototypeOf`
|
|
205
|
-
// calls required for full WebIDL compliance will be used. Probably
|
|
206
|
-
// only needed for testing or environments where full introspection
|
|
207
|
-
// on class relationships is required; see
|
|
208
|
-
// http://stackoverflow.com/questions/41927589/rationales-consequences-of-webidl-class-inheritance-requirements
|
|
209
|
-
'fullIDLSupport',
|
|
210
|
-
// Effectively defaults to false (ignored unless `true`)
|
|
211
|
-
|
|
212
|
-
// Boolean on whether to perform origin checks in `IDBFactory` methods
|
|
213
|
-
// Effectively defaults to `true` (must be set to `false` to cancel checks)
|
|
214
|
-
'checkOrigin',
|
|
215
|
-
// Used by `IDBCursor` continue methods for number of records to cache;
|
|
216
|
-
// Defaults to 100
|
|
217
|
-
'cursorPreloadPackSize',
|
|
218
|
-
// See optional API (`shimIndexedDB.__setUnicodeIdentifiers`);
|
|
219
|
-
// or just use the Unicode builds which invoke this method
|
|
220
|
-
// automatically using the large, fully spec-compliant, regular
|
|
221
|
-
// expression strings of `src/UnicodeIdentifiers.js`)
|
|
222
|
-
// In the non-Unicode builds, defaults to /[$A-Z_a-z]/
|
|
223
|
-
'UnicodeIDStart',
|
|
224
|
-
// In the non-Unicode builds, defaults to /[$0-9A-Z_a-z]/
|
|
225
|
-
'UnicodeIDContinue',
|
|
226
|
-
// Used by SCA.js for optional restructuring of typeson-registry
|
|
227
|
-
// Structured Cloning Algorithm; should only be needed for ensuring data
|
|
228
|
-
// created in 3.* versions of IndexedDBShim continue to work; see the
|
|
229
|
-
// library `typeson-registry-sca-reverter` to get a function to do this
|
|
230
|
-
'registerSCA',
|
|
231
|
-
// BROWSER-SPECIFIC CONFIG
|
|
232
|
-
'avoidAutoShim',
|
|
233
|
-
// Where WebSQL is detected but where `indexedDB` is
|
|
234
|
-
// missing or poor support is known (non-Chrome Android or
|
|
235
|
-
// non-Safari iOS9), the shim will be auto-applied without
|
|
236
|
-
// `shimIndexedDB.__useShim()`. Set this to `true` to avoid forcing
|
|
237
|
-
// the shim for such cases.
|
|
238
|
-
|
|
239
|
-
// -----------SQL CONFIG----------
|
|
240
|
-
// Object (`window` in the browser) on which there may be an
|
|
241
|
-
// `openDatabase` method (if any) for WebSQL. (The browser
|
|
242
|
-
// throws if attempting to call `openDatabase` without the window
|
|
243
|
-
// so this is why the config doesn't just allow the function.)
|
|
244
|
-
// Defaults to `window` or `self` in browser builds or
|
|
245
|
-
// a singleton object with the `openDatabase` method set to
|
|
246
|
-
// the "websql" package in Node.
|
|
247
|
-
'win',
|
|
248
|
-
// For internal `openDatabase` calls made by `IDBFactory` methods;
|
|
249
|
-
// per the WebSQL spec, "User agents are expected to use the display name
|
|
250
|
-
// and the estimated database size to optimize the user experience.
|
|
251
|
-
// For example, a user agent could use the estimated size to suggest an
|
|
252
|
-
// initial quota to the user. This allows a site that is aware that it
|
|
253
|
-
// will try to use hundreds of megabytes to declare this upfront, instead
|
|
254
|
-
// of the user agent prompting the user for permission to increase the
|
|
255
|
-
// quota every five megabytes."
|
|
256
|
-
// Defaults to (4 * 1024 * 1024) or (25 * 1024 * 1024) in Safari
|
|
257
|
-
'DEFAULT_DB_SIZE',
|
|
258
|
-
// Whether to create indexes on SQLite tables (and also whether to try
|
|
259
|
-
// dropping)
|
|
260
|
-
// Effectively defaults to `false` (ignored unless `true`)
|
|
261
|
-
'useSQLiteIndexes',
|
|
262
|
-
// NODE-IMPINGING SETTINGS (created for sake of limitations in Node
|
|
263
|
-
// or desktop file system implementation but applied by default in
|
|
264
|
-
// browser for parity)
|
|
265
|
-
|
|
266
|
-
// File system module with `unlink` to remove deleted database files
|
|
267
|
-
'fs',
|
|
268
|
-
// Used when setting global shims to determine whether to try to add
|
|
269
|
-
// other globals shimmed by the library (`ShimDOMException`,
|
|
270
|
-
// `ShimDOMStringList`, `ShimEvent`, `ShimCustomEvent`, `ShimEventTarget`)
|
|
271
|
-
// Effectively defaults to `false` (ignored unless `true`)
|
|
272
|
-
'addNonIDBGlobals',
|
|
273
|
-
// Used when setting global shims to determine whether to try to overwrite
|
|
274
|
-
// other globals shimmed by the library (`DOMException`, `DOMStringList`,
|
|
275
|
-
// `Event`, `CustomEvent`, `EventTarget`)
|
|
276
|
-
// Effectively defaults to `false` (ignored unless `true`)
|
|
277
|
-
'replaceNonIDBGlobals',
|
|
278
|
-
// Overcoming limitations with node-sqlite3/storing database name on
|
|
279
|
-
// file systems
|
|
280
|
-
// https://en.wikipedia.org/wiki/Filename#Reserved_characters_and_words
|
|
281
|
-
// Defaults to prefixing database with `D_`, escaping
|
|
282
|
-
// `databaseCharacterEscapeList`, escaping NUL, and
|
|
283
|
-
// escaping upper case letters, as well as enforcing
|
|
284
|
-
// `databaseNameLengthLimit`
|
|
285
|
-
'escapeDatabaseName',
|
|
286
|
-
// Not used internally; usable as a convenience method
|
|
287
|
-
'unescapeDatabaseName',
|
|
288
|
-
// Defaults to global regex representing the following
|
|
289
|
-
// (characters nevertheless commonly reserved in modern,
|
|
290
|
-
// Unicode-supporting systems): 0x00-0x1F 0x7F " * / : < > ? \ |
|
|
291
|
-
'databaseCharacterEscapeList',
|
|
292
|
-
// Defaults to 254 (shortest typical modern file length limit)
|
|
293
|
-
'databaseNameLengthLimit',
|
|
294
|
-
// Boolean defaulting to true on whether to escape NFD-escaping
|
|
295
|
-
// characters to avoid clashes on MacOS which performs NFD on files
|
|
296
|
-
'escapeNFDForDatabaseNames',
|
|
297
|
-
// Boolean on whether to add the `.sqlite` extension to file names;
|
|
298
|
-
// defaults to `true`
|
|
299
|
-
'addSQLiteExtension',
|
|
300
|
-
// Various types of in-memory databases that can auto-delete
|
|
301
|
-
['memoryDatabase',
|
|
302
|
-
/**
|
|
303
|
-
* @param {string} val
|
|
304
|
-
* @throws {TypeError}
|
|
305
|
-
* @returns {void}
|
|
306
|
-
*/
|
|
307
|
-
function (val) {
|
|
308
|
-
if (!/^(?::memory:|file::memory:(\?(?:[\0-"\$-\uD7FF\uE000-\uFFFF]|[\uD800-\uDBFF][\uDC00-\uDFFF]|[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?:[^\uD800-\uDBFF]|^)[\uDC00-\uDFFF])*)?(#(?:[\0-\t\x0B\f\x0E-\u2027\u202A-\uD7FF\uE000-\uFFFF]|[\uD800-\uDBFF][\uDC00-\uDFFF]|[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?:[^\uD800-\uDBFF]|^)[\uDC00-\uDFFF])*)?)?$/.test(/** @type {string} */val)) {
|
|
309
|
-
throw new TypeError('`memoryDatabase` must be the empty string, ":memory:", or a ' + '"file::memory:[?queryString][#hash] URL".');
|
|
310
|
-
}
|
|
311
|
-
}],
|
|
312
|
-
// NODE-SPECIFIC CONFIG
|
|
313
|
-
// Boolean on whether to delete the database file itself after
|
|
314
|
-
// `deleteDatabase`; defaults to `true` as the database will be empty
|
|
315
|
-
'deleteDatabaseFiles', 'databaseBasePath', 'sysDatabaseBasePath',
|
|
316
|
-
// NODE-SPECIFIC WEBSQL CONFIG
|
|
317
|
-
'sqlBusyTimeout',
|
|
318
|
-
// Defaults to 1000
|
|
319
|
-
'sqlTrace',
|
|
320
|
-
// Callback not used by default
|
|
321
|
-
'sqlProfile',
|
|
322
|
-
// Callback not used by default
|
|
323
|
-
|
|
324
|
-
'createIndexes'].forEach(function (prop) {
|
|
325
|
-
/** @type {(val: any) => void} */
|
|
326
|
-
var validator;
|
|
327
|
-
if (Array.isArray(prop)) {
|
|
328
|
-
var _prop = prop;
|
|
329
|
-
var _prop2 = _slicedToArray(_prop, 2);
|
|
330
|
-
prop = _prop2[0];
|
|
331
|
-
validator = _prop2[1];
|
|
332
|
-
}
|
|
333
|
-
Object.defineProperty(CFG, prop, {
|
|
334
|
-
get: function get() {
|
|
335
|
-
return map[prop];
|
|
336
|
-
},
|
|
337
|
-
set: function set(val) {
|
|
338
|
-
if (validator) {
|
|
339
|
-
validator(val);
|
|
215
|
+
Object.defineProperty(CFG, prop, {
|
|
216
|
+
get() {
|
|
217
|
+
return map[prop];
|
|
218
|
+
},
|
|
219
|
+
set(val) {
|
|
220
|
+
if (validator) {
|
|
221
|
+
validator(val);
|
|
222
|
+
}
|
|
223
|
+
map[prop] = val;
|
|
340
224
|
}
|
|
341
|
-
|
|
342
|
-
}
|
|
225
|
+
});
|
|
343
226
|
});
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
* @returns {DOMException}
|
|
351
|
-
*/
|
|
352
|
-
function createNativeDOMException(name, message) {
|
|
353
|
-
// @ts-expect-error It's ok
|
|
354
|
-
return new DOMException.prototype.constructor(message, name || 'DOMException');
|
|
355
|
-
}
|
|
356
|
-
|
|
357
|
-
// From web-platform-tests testharness.js name_code_map (though not in new spec)
|
|
358
|
-
|
|
359
|
-
/**
|
|
360
|
-
* @typedef {"IndexSizeError"|"HierarchyRequestError"|"WrongDocumentError"|
|
|
361
|
-
* "InvalidCharacterError"|"NoModificationAllowedError"|"NotFoundError"|
|
|
362
|
-
* "NotSupportedError"|"InUseAttributeError"|"InvalidStateError"|
|
|
363
|
-
* "SyntaxError"|"InvalidModificationError"|"NamespaceError"|
|
|
364
|
-
* "InvalidAccessError"|"TypeMismatchError"|"SecurityError"|
|
|
365
|
-
* "NetworkError"|"AbortError"|"URLMismatchError"|"QuotaExceededError"|
|
|
366
|
-
* "TimeoutError"|"InvalidNodeTypeError"|"DataCloneError"|"EncodingError"|
|
|
367
|
-
* "NotReadableError"|"UnknownError"|"ConstraintError"|"DataError"|
|
|
368
|
-
* "TransactionInactiveError"|"ReadOnlyError"|"VersionError"|
|
|
369
|
-
* "OperationError"|"NotAllowedError"} Code
|
|
370
|
-
*/
|
|
371
|
-
|
|
372
|
-
var codes = {
|
|
373
|
-
IndexSizeError: 1,
|
|
374
|
-
HierarchyRequestError: 3,
|
|
375
|
-
WrongDocumentError: 4,
|
|
376
|
-
InvalidCharacterError: 5,
|
|
377
|
-
NoModificationAllowedError: 7,
|
|
378
|
-
NotFoundError: 8,
|
|
379
|
-
NotSupportedError: 9,
|
|
380
|
-
InUseAttributeError: 10,
|
|
381
|
-
InvalidStateError: 11,
|
|
382
|
-
SyntaxError: 12,
|
|
383
|
-
InvalidModificationError: 13,
|
|
384
|
-
NamespaceError: 14,
|
|
385
|
-
InvalidAccessError: 15,
|
|
386
|
-
TypeMismatchError: 17,
|
|
387
|
-
SecurityError: 18,
|
|
388
|
-
NetworkError: 19,
|
|
389
|
-
AbortError: 20,
|
|
390
|
-
URLMismatchError: 21,
|
|
391
|
-
QuotaExceededError: 22,
|
|
392
|
-
TimeoutError: 23,
|
|
393
|
-
InvalidNodeTypeError: 24,
|
|
394
|
-
DataCloneError: 25,
|
|
395
|
-
EncodingError: 0,
|
|
396
|
-
NotReadableError: 0,
|
|
397
|
-
UnknownError: 0,
|
|
398
|
-
ConstraintError: 0,
|
|
399
|
-
DataError: 0,
|
|
400
|
-
TransactionInactiveError: 0,
|
|
401
|
-
ReadOnlyError: 0,
|
|
402
|
-
VersionError: 0,
|
|
403
|
-
OperationError: 0,
|
|
404
|
-
NotAllowedError: 0
|
|
405
|
-
};
|
|
406
|
-
|
|
407
|
-
/**
|
|
408
|
-
* @typedef {"INDEX_SIZE_ERR"|"DOMSTRING_SIZE_ERR"|"HIERARCHY_REQUEST_ERR"|
|
|
409
|
-
* "WRONG_DOCUMENT_ERR"|"INVALID_CHARACTER_ERR"|"NO_DATA_ALLOWED_ERR"|
|
|
410
|
-
* "NO_MODIFICATION_ALLOWED_ERR"|"NOT_FOUND_ERR"|"NOT_SUPPORTED_ERR"|
|
|
411
|
-
* "INUSE_ATTRIBUTE_ERR"|"INVALID_STATE_ERR"|"SYNTAX_ERR"|
|
|
412
|
-
* "INVALID_MODIFICATION_ERR"|"NAMESPACE_ERR"|"INVALID_ACCESS_ERR"|
|
|
413
|
-
* "VALIDATION_ERR"|"TYPE_MISMATCH_ERR"|"SECURITY_ERR"|"NETWORK_ERR"|
|
|
414
|
-
* "ABORT_ERR"|"URL_MISMATCH_ERR"|"QUOTA_EXCEEDED_ERR"|"TIMEOUT_ERR"|
|
|
415
|
-
* "INVALID_NODE_TYPE_ERR"|"DATA_CLONE_ERR"} LegacyCode
|
|
416
|
-
*/
|
|
417
|
-
|
|
418
|
-
var legacyCodes = {
|
|
419
|
-
INDEX_SIZE_ERR: 1,
|
|
420
|
-
DOMSTRING_SIZE_ERR: 2,
|
|
421
|
-
HIERARCHY_REQUEST_ERR: 3,
|
|
422
|
-
WRONG_DOCUMENT_ERR: 4,
|
|
423
|
-
INVALID_CHARACTER_ERR: 5,
|
|
424
|
-
NO_DATA_ALLOWED_ERR: 6,
|
|
425
|
-
NO_MODIFICATION_ALLOWED_ERR: 7,
|
|
426
|
-
NOT_FOUND_ERR: 8,
|
|
427
|
-
NOT_SUPPORTED_ERR: 9,
|
|
428
|
-
INUSE_ATTRIBUTE_ERR: 10,
|
|
429
|
-
INVALID_STATE_ERR: 11,
|
|
430
|
-
SYNTAX_ERR: 12,
|
|
431
|
-
INVALID_MODIFICATION_ERR: 13,
|
|
432
|
-
NAMESPACE_ERR: 14,
|
|
433
|
-
INVALID_ACCESS_ERR: 15,
|
|
434
|
-
VALIDATION_ERR: 16,
|
|
435
|
-
TYPE_MISMATCH_ERR: 17,
|
|
436
|
-
SECURITY_ERR: 18,
|
|
437
|
-
NETWORK_ERR: 19,
|
|
438
|
-
ABORT_ERR: 20,
|
|
439
|
-
URL_MISMATCH_ERR: 21,
|
|
440
|
-
QUOTA_EXCEEDED_ERR: 22,
|
|
441
|
-
TIMEOUT_ERR: 23,
|
|
442
|
-
INVALID_NODE_TYPE_ERR: 24,
|
|
443
|
-
DATA_CLONE_ERR: 25
|
|
444
|
-
};
|
|
445
|
-
|
|
446
|
-
/**
|
|
447
|
-
*
|
|
448
|
-
* @returns {typeof DOMException}
|
|
449
|
-
*/
|
|
450
|
-
function createNonNativeDOMExceptionClass() {
|
|
451
|
-
/**
|
|
452
|
-
* @param {string|undefined} message
|
|
453
|
-
* @param {Code|LegacyCode} name
|
|
454
|
-
* @returns {void}
|
|
227
|
+
|
|
228
|
+
/**
|
|
229
|
+
* Creates a native DOMException, for browsers that support it.
|
|
230
|
+
* @param {string} name
|
|
231
|
+
* @param {string} message
|
|
232
|
+
* @returns {DOMException}
|
|
455
233
|
*/
|
|
456
|
-
function
|
|
457
|
-
//
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
234
|
+
function createNativeDOMException(name, message) {
|
|
235
|
+
// @ts-expect-error It's ok
|
|
236
|
+
return new DOMException.prototype.constructor(message, name || 'DOMException');
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
// From web-platform-tests testharness.js name_code_map (though not in new spec)
|
|
240
|
+
|
|
241
|
+
/**
|
|
242
|
+
* @typedef {"IndexSizeError"|"HierarchyRequestError"|"WrongDocumentError"|
|
|
243
|
+
* "InvalidCharacterError"|"NoModificationAllowedError"|"NotFoundError"|
|
|
244
|
+
* "NotSupportedError"|"InUseAttributeError"|"InvalidStateError"|
|
|
245
|
+
* "SyntaxError"|"InvalidModificationError"|"NamespaceError"|
|
|
246
|
+
* "InvalidAccessError"|"TypeMismatchError"|"SecurityError"|
|
|
247
|
+
* "NetworkError"|"AbortError"|"URLMismatchError"|"QuotaExceededError"|
|
|
248
|
+
* "TimeoutError"|"InvalidNodeTypeError"|"DataCloneError"|"EncodingError"|
|
|
249
|
+
* "NotReadableError"|"UnknownError"|"ConstraintError"|"DataError"|
|
|
250
|
+
* "TransactionInactiveError"|"ReadOnlyError"|"VersionError"|
|
|
251
|
+
* "OperationError"|"NotAllowedError"} Code
|
|
252
|
+
*/
|
|
253
|
+
|
|
254
|
+
const codes = {
|
|
255
|
+
IndexSizeError: 1,
|
|
256
|
+
HierarchyRequestError: 3,
|
|
257
|
+
WrongDocumentError: 4,
|
|
258
|
+
InvalidCharacterError: 5,
|
|
259
|
+
NoModificationAllowedError: 7,
|
|
260
|
+
NotFoundError: 8,
|
|
261
|
+
NotSupportedError: 9,
|
|
262
|
+
InUseAttributeError: 10,
|
|
263
|
+
InvalidStateError: 11,
|
|
264
|
+
SyntaxError: 12,
|
|
265
|
+
InvalidModificationError: 13,
|
|
266
|
+
NamespaceError: 14,
|
|
267
|
+
InvalidAccessError: 15,
|
|
268
|
+
TypeMismatchError: 17,
|
|
269
|
+
SecurityError: 18,
|
|
270
|
+
NetworkError: 19,
|
|
271
|
+
AbortError: 20,
|
|
272
|
+
URLMismatchError: 21,
|
|
273
|
+
QuotaExceededError: 22,
|
|
274
|
+
TimeoutError: 23,
|
|
275
|
+
InvalidNodeTypeError: 24,
|
|
276
|
+
DataCloneError: 25,
|
|
277
|
+
EncodingError: 0,
|
|
278
|
+
NotReadableError: 0,
|
|
279
|
+
UnknownError: 0,
|
|
280
|
+
ConstraintError: 0,
|
|
281
|
+
DataError: 0,
|
|
282
|
+
TransactionInactiveError: 0,
|
|
283
|
+
ReadOnlyError: 0,
|
|
284
|
+
VersionError: 0,
|
|
285
|
+
OperationError: 0,
|
|
286
|
+
NotAllowedError: 0
|
|
287
|
+
};
|
|
288
|
+
|
|
289
|
+
/**
|
|
290
|
+
* @typedef {"INDEX_SIZE_ERR"|"DOMSTRING_SIZE_ERR"|"HIERARCHY_REQUEST_ERR"|
|
|
291
|
+
* "WRONG_DOCUMENT_ERR"|"INVALID_CHARACTER_ERR"|"NO_DATA_ALLOWED_ERR"|
|
|
292
|
+
* "NO_MODIFICATION_ALLOWED_ERR"|"NOT_FOUND_ERR"|"NOT_SUPPORTED_ERR"|
|
|
293
|
+
* "INUSE_ATTRIBUTE_ERR"|"INVALID_STATE_ERR"|"SYNTAX_ERR"|
|
|
294
|
+
* "INVALID_MODIFICATION_ERR"|"NAMESPACE_ERR"|"INVALID_ACCESS_ERR"|
|
|
295
|
+
* "VALIDATION_ERR"|"TYPE_MISMATCH_ERR"|"SECURITY_ERR"|"NETWORK_ERR"|
|
|
296
|
+
* "ABORT_ERR"|"URL_MISMATCH_ERR"|"QUOTA_EXCEEDED_ERR"|"TIMEOUT_ERR"|
|
|
297
|
+
* "INVALID_NODE_TYPE_ERR"|"DATA_CLONE_ERR"} LegacyCode
|
|
298
|
+
*/
|
|
299
|
+
|
|
300
|
+
const legacyCodes = {
|
|
301
|
+
INDEX_SIZE_ERR: 1,
|
|
302
|
+
DOMSTRING_SIZE_ERR: 2,
|
|
303
|
+
HIERARCHY_REQUEST_ERR: 3,
|
|
304
|
+
WRONG_DOCUMENT_ERR: 4,
|
|
305
|
+
INVALID_CHARACTER_ERR: 5,
|
|
306
|
+
NO_DATA_ALLOWED_ERR: 6,
|
|
307
|
+
NO_MODIFICATION_ALLOWED_ERR: 7,
|
|
308
|
+
NOT_FOUND_ERR: 8,
|
|
309
|
+
NOT_SUPPORTED_ERR: 9,
|
|
310
|
+
INUSE_ATTRIBUTE_ERR: 10,
|
|
311
|
+
INVALID_STATE_ERR: 11,
|
|
312
|
+
SYNTAX_ERR: 12,
|
|
313
|
+
INVALID_MODIFICATION_ERR: 13,
|
|
314
|
+
NAMESPACE_ERR: 14,
|
|
315
|
+
INVALID_ACCESS_ERR: 15,
|
|
316
|
+
VALIDATION_ERR: 16,
|
|
317
|
+
TYPE_MISMATCH_ERR: 17,
|
|
318
|
+
SECURITY_ERR: 18,
|
|
319
|
+
NETWORK_ERR: 19,
|
|
320
|
+
ABORT_ERR: 20,
|
|
321
|
+
URL_MISMATCH_ERR: 21,
|
|
322
|
+
QUOTA_EXCEEDED_ERR: 22,
|
|
323
|
+
TIMEOUT_ERR: 23,
|
|
324
|
+
INVALID_NODE_TYPE_ERR: 24,
|
|
325
|
+
DATA_CLONE_ERR: 25
|
|
326
|
+
};
|
|
327
|
+
|
|
328
|
+
/**
|
|
329
|
+
*
|
|
330
|
+
* @returns {typeof DOMException}
|
|
331
|
+
*/
|
|
332
|
+
function createNonNativeDOMExceptionClass() {
|
|
333
|
+
/**
|
|
334
|
+
* @param {string|undefined} message
|
|
335
|
+
* @param {Code|LegacyCode} name
|
|
336
|
+
* @returns {void}
|
|
337
|
+
*/
|
|
338
|
+
function DOMException(message, name) {
|
|
339
|
+
// const err = Error.prototype.constructor.call(this, message); // Any use to this? Won't set this.message
|
|
340
|
+
this[Symbol.toStringTag] = 'DOMException';
|
|
341
|
+
this._code = name in codes ? codes[(/** @type {Code} */name)] : legacyCodes[(/** @type {LegacyCode} */name)] || 0;
|
|
342
|
+
this._name = name || 'Error';
|
|
343
|
+
// We avoid `String()` in this next line as it converts Symbols
|
|
344
|
+
this._message = message === undefined ? '' : '' + message; // eslint-disable-line no-implicit-coercion -- Don't convert symbols
|
|
345
|
+
Object.defineProperty(this, 'code', {
|
|
471
346
|
configurable: true,
|
|
472
347
|
enumerable: true,
|
|
473
348
|
writable: true,
|
|
474
|
-
value: this.
|
|
475
|
-
});
|
|
476
|
-
}
|
|
477
|
-
if (message !== undefined) {
|
|
478
|
-
Object.defineProperty(this, 'message', {
|
|
479
|
-
configurable: true,
|
|
480
|
-
enumerable: false,
|
|
481
|
-
writable: true,
|
|
482
|
-
value: this._message
|
|
349
|
+
value: this._code
|
|
483
350
|
});
|
|
351
|
+
if (name !== undefined) {
|
|
352
|
+
Object.defineProperty(this, 'name', {
|
|
353
|
+
configurable: true,
|
|
354
|
+
enumerable: true,
|
|
355
|
+
writable: true,
|
|
356
|
+
value: this._name
|
|
357
|
+
});
|
|
358
|
+
}
|
|
359
|
+
if (message !== undefined) {
|
|
360
|
+
Object.defineProperty(this, 'message', {
|
|
361
|
+
configurable: true,
|
|
362
|
+
enumerable: false,
|
|
363
|
+
writable: true,
|
|
364
|
+
value: this._message
|
|
365
|
+
});
|
|
366
|
+
}
|
|
484
367
|
}
|
|
485
|
-
}
|
|
486
368
|
|
|
487
|
-
|
|
369
|
+
// Necessary for W3C tests which complains if `DOMException` has properties on its "own" prototype
|
|
488
370
|
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
371
|
+
// class DummyDOMException extends Error {}; // Sometimes causing problems in Node
|
|
372
|
+
/* eslint-disable func-name-matching -- See above */
|
|
373
|
+
/**
|
|
374
|
+
* @class
|
|
375
|
+
*/
|
|
376
|
+
const DummyDOMException = function DOMException() {/* */};
|
|
377
|
+
/* eslint-enable func-name-matching -- See above */
|
|
378
|
+
DummyDOMException.prototype = Object.create(Error.prototype); // Intended for subclassing
|
|
379
|
+
/** @type {const} */
|
|
380
|
+
['name', 'message'].forEach(prop => {
|
|
381
|
+
Object.defineProperty(DummyDOMException.prototype, prop, {
|
|
382
|
+
enumerable: true,
|
|
383
|
+
/**
|
|
384
|
+
* @this {DOMException}
|
|
385
|
+
* @returns {string}
|
|
386
|
+
*/
|
|
387
|
+
get() {
|
|
388
|
+
if (!(this instanceof DOMException ||
|
|
389
|
+
// @ts-expect-error Just checking
|
|
390
|
+
this instanceof DummyDOMException ||
|
|
391
|
+
// @ts-expect-error Just checking
|
|
392
|
+
this instanceof Error)) {
|
|
393
|
+
throw new TypeError('Illegal invocation');
|
|
394
|
+
}
|
|
395
|
+
return this[prop === 'name' ? '_name' : '_message'];
|
|
512
396
|
}
|
|
513
|
-
|
|
514
|
-
}
|
|
397
|
+
});
|
|
515
398
|
});
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
configurable: true,
|
|
520
|
-
enumerable: true,
|
|
521
|
-
get: function get() {
|
|
522
|
-
throw new TypeError('Illegal invocation');
|
|
523
|
-
}
|
|
524
|
-
});
|
|
525
|
-
// @ts-expect-error It's ok
|
|
526
|
-
DOMException.prototype = new DummyDOMException();
|
|
527
|
-
DOMException.prototype[Symbol.toStringTag] = 'DOMExceptionPrototype';
|
|
528
|
-
Object.defineProperty(DOMException, 'prototype', {
|
|
529
|
-
writable: false
|
|
530
|
-
});
|
|
531
|
-
var keys = Object.keys(codes);
|
|
532
|
-
|
|
533
|
-
/** @type {(keyof codes)[]} */
|
|
534
|
-
keys.forEach(function (codeName) {
|
|
535
|
-
Object.defineProperty(DOMException.prototype, codeName, {
|
|
399
|
+
// DOMException uses the same `toString` as `Error`
|
|
400
|
+
Object.defineProperty(DummyDOMException.prototype, 'code', {
|
|
401
|
+
configurable: true,
|
|
536
402
|
enumerable: true,
|
|
537
|
-
|
|
538
|
-
|
|
403
|
+
get() {
|
|
404
|
+
throw new TypeError('Illegal invocation');
|
|
405
|
+
}
|
|
539
406
|
});
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
407
|
+
// @ts-expect-error It's ok
|
|
408
|
+
DOMException.prototype = new DummyDOMException();
|
|
409
|
+
DOMException.prototype[Symbol.toStringTag] = 'DOMExceptionPrototype';
|
|
410
|
+
Object.defineProperty(DOMException, 'prototype', {
|
|
411
|
+
writable: false
|
|
544
412
|
});
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
413
|
+
const keys = Object.keys(codes);
|
|
414
|
+
|
|
415
|
+
/** @type {(keyof codes)[]} */
|
|
416
|
+
keys.forEach(codeName => {
|
|
417
|
+
Object.defineProperty(DOMException.prototype, codeName, {
|
|
418
|
+
enumerable: true,
|
|
419
|
+
configurable: false,
|
|
420
|
+
value: codes[codeName]
|
|
421
|
+
});
|
|
422
|
+
Object.defineProperty(DOMException, codeName, {
|
|
423
|
+
enumerable: true,
|
|
424
|
+
configurable: false,
|
|
425
|
+
value: codes[codeName]
|
|
426
|
+
});
|
|
552
427
|
});
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
428
|
+
/** @type {(keyof legacyCodes)[]} */
|
|
429
|
+
Object.keys(legacyCodes).forEach(codeName => {
|
|
430
|
+
Object.defineProperty(DOMException.prototype, codeName, {
|
|
431
|
+
enumerable: true,
|
|
432
|
+
configurable: false,
|
|
433
|
+
value: legacyCodes[codeName]
|
|
434
|
+
});
|
|
435
|
+
Object.defineProperty(DOMException, codeName, {
|
|
436
|
+
enumerable: true,
|
|
437
|
+
configurable: false,
|
|
438
|
+
value: legacyCodes[codeName]
|
|
439
|
+
});
|
|
440
|
+
});
|
|
441
|
+
Object.defineProperty(DOMException.prototype, 'constructor', {
|
|
442
|
+
writable: true,
|
|
443
|
+
configurable: true,
|
|
444
|
+
enumerable: false,
|
|
445
|
+
value: DOMException
|
|
557
446
|
});
|
|
558
|
-
});
|
|
559
|
-
Object.defineProperty(DOMException.prototype, 'constructor', {
|
|
560
|
-
writable: true,
|
|
561
|
-
configurable: true,
|
|
562
|
-
enumerable: false,
|
|
563
|
-
value: DOMException
|
|
564
|
-
});
|
|
565
447
|
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
}
|
|
569
|
-
var ShimNonNativeDOMException = createNonNativeDOMExceptionClass();
|
|
570
|
-
|
|
571
|
-
/**
|
|
572
|
-
* Creates a generic Error object.
|
|
573
|
-
* @param {string} name
|
|
574
|
-
* @param {string} message
|
|
575
|
-
* @returns {Error}
|
|
576
|
-
*/
|
|
577
|
-
function createNonNativeDOMException(name, message) {
|
|
578
|
-
return new ShimNonNativeDOMException(message, name);
|
|
579
|
-
}
|
|
580
|
-
|
|
581
|
-
/**
|
|
582
|
-
* @typedef {{
|
|
583
|
-
* message: string|DOMString
|
|
584
|
-
* }} ErrorLike
|
|
585
|
-
*/
|
|
586
|
-
|
|
587
|
-
/**
|
|
588
|
-
* Logs detailed error information to the console.
|
|
589
|
-
* @param {string} name
|
|
590
|
-
* @param {string} message
|
|
591
|
-
* @param {string|ErrorLike|boolean|null} [error]
|
|
592
|
-
* @returns {void}
|
|
593
|
-
*/
|
|
594
|
-
function logError(name, message, error) {
|
|
595
|
-
if (CFG.DEBUG) {
|
|
596
|
-
var msg = error && _typeof(error) === 'object' && error.message ? error.message : (/** @type {string} */error);
|
|
597
|
-
var method = typeof console.error === 'function' ? 'error' : 'log';
|
|
598
|
-
console[method](name + ': ' + message + '. ' + (msg || ''));
|
|
599
|
-
if (console.trace) {
|
|
600
|
-
console.trace();
|
|
601
|
-
}
|
|
448
|
+
// @ts-expect-error We don't need all its properties
|
|
449
|
+
return DOMException;
|
|
602
450
|
}
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
function isErrorOrDOMErrorOrDOMException(obj) {
|
|
614
|
-
return obj && _typeof(obj) === 'object' &&
|
|
615
|
-
// We don't use util.isObj here as mutual dependency causing problems in Babel with browser
|
|
616
|
-
typeof obj.name === 'string';
|
|
617
|
-
}
|
|
618
|
-
var test,
|
|
619
|
-
useNativeDOMException = false;
|
|
620
|
-
|
|
621
|
-
// Test whether we can use the browser's native DOMException class
|
|
622
|
-
try {
|
|
623
|
-
test = createNativeDOMException('test name', 'test message');
|
|
624
|
-
if (isErrorOrDOMErrorOrDOMException(test) && test.name === 'test name' && test.message === 'test message') {
|
|
625
|
-
// Native DOMException works as expected
|
|
626
|
-
useNativeDOMException = true;
|
|
451
|
+
const ShimNonNativeDOMException = createNonNativeDOMExceptionClass();
|
|
452
|
+
|
|
453
|
+
/**
|
|
454
|
+
* Creates a generic Error object.
|
|
455
|
+
* @param {string} name
|
|
456
|
+
* @param {string} message
|
|
457
|
+
* @returns {Error}
|
|
458
|
+
*/
|
|
459
|
+
function createNonNativeDOMException(name, message) {
|
|
460
|
+
return new ShimNonNativeDOMException(message, name);
|
|
627
461
|
}
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
function (name, message, error) {
|
|
651
|
-
logError(name, message, error);
|
|
652
|
-
return createNonNativeDOMException(name, message);
|
|
653
|
-
};
|
|
654
|
-
|
|
655
|
-
/**
|
|
656
|
-
* @typedef {number} Integer
|
|
657
|
-
*/
|
|
658
|
-
|
|
659
|
-
/**
|
|
660
|
-
* @param {string} arg
|
|
661
|
-
* @returns {string}
|
|
662
|
-
*/
|
|
663
|
-
function escapeUnmatchedSurrogates(arg) {
|
|
664
|
-
// http://stackoverflow.com/a/6701665/271577
|
|
665
|
-
return arg.replaceAll(/((?:[\uD800-\uDBFF](?![\uDC00-\uDFFF])))(?!(?:(?:[^\uD800-\uDBFF]|^)[\uDC00-\uDFFF]))|(^|(?:[\0-\uD7FF\uE000-\uFFFF]|[\uD800-\uDBFF][\uDC00-\uDFFF]|(?:[^\uD800-\uDBFF]|^)[\uDC00-\uDFFF]))((?:(?:[^\uD800-\uDBFF]|^)[\uDC00-\uDFFF]))/g, function (_, unmatchedHighSurrogate, precedingLow, unmatchedLowSurrogate) {
|
|
666
|
-
// Could add a corresponding surrogate for compatibility with `node-sqlite3`: http://bugs.python.org/issue12569 and http://stackoverflow.com/a/6701665/271577
|
|
667
|
-
// but Chrome having problems
|
|
668
|
-
if (unmatchedHighSurrogate) {
|
|
669
|
-
return '^2' + unmatchedHighSurrogate.codePointAt().toString(16).padStart(4, '0');
|
|
670
|
-
}
|
|
671
|
-
return (precedingLow || '') + '^3' + unmatchedLowSurrogate.codePointAt().toString(16).padStart(4, '0');
|
|
672
|
-
});
|
|
673
|
-
}
|
|
674
|
-
|
|
675
|
-
/**
|
|
676
|
-
* The escaping of unmatched surrogates was needed by Chrome but not Node.
|
|
677
|
-
* @param {string} arg
|
|
678
|
-
* @returns {string}
|
|
679
|
-
*/
|
|
680
|
-
function escapeSQLiteStatement(arg) {
|
|
681
|
-
return escapeUnmatchedSurrogates(arg.replaceAll('^', '^^').replaceAll('\0', '^0'));
|
|
682
|
-
}
|
|
683
|
-
|
|
684
|
-
/**
|
|
685
|
-
*
|
|
686
|
-
* @param {AnyValue} obj
|
|
687
|
-
* @returns {obj is object}
|
|
688
|
-
*/
|
|
689
|
-
function isObj(obj) {
|
|
690
|
-
return obj !== null && _typeof(obj) === 'object';
|
|
691
|
-
}
|
|
692
|
-
|
|
693
|
-
/**
|
|
694
|
-
*
|
|
695
|
-
* @param {object} obj
|
|
696
|
-
* @returns {boolean}
|
|
697
|
-
*/
|
|
698
|
-
function isDate(obj) {
|
|
699
|
-
return isObj(obj) && 'getDate' in obj && typeof obj.getDate === 'function';
|
|
700
|
-
}
|
|
701
|
-
|
|
702
|
-
/**
|
|
703
|
-
*
|
|
704
|
-
* @param {object} obj
|
|
705
|
-
* @returns {boolean}
|
|
706
|
-
*/
|
|
707
|
-
function isBlob(obj) {
|
|
708
|
-
return isObj(obj) && 'size' in obj && typeof obj.size === 'number' && 'slice' in obj && typeof obj.slice === 'function' && !('lastModified' in obj);
|
|
709
|
-
}
|
|
710
|
-
|
|
711
|
-
/**
|
|
712
|
-
*
|
|
713
|
-
* @param {object} obj
|
|
714
|
-
* @returns {boolean}
|
|
715
|
-
*/
|
|
716
|
-
function isFile(obj) {
|
|
717
|
-
return isObj(obj) && 'name' in obj && typeof obj.name === 'string' && 'slice' in obj && typeof obj.slice === 'function' && 'lastModified' in obj;
|
|
718
|
-
}
|
|
719
|
-
|
|
720
|
-
/**
|
|
721
|
-
*
|
|
722
|
-
* @param {AnyValue} obj
|
|
723
|
-
* @returns {boolean}
|
|
724
|
-
*/
|
|
725
|
-
function isBinary(obj) {
|
|
726
|
-
return isObj(obj) && 'byteLength' in obj && typeof obj.byteLength === 'number' && ('slice' in obj && typeof obj.slice === 'function' ||
|
|
727
|
-
// `TypedArray` (view on buffer) or `ArrayBuffer`
|
|
728
|
-
'getFloat64' in obj && typeof obj.getFloat64 === 'function' // `DataView` (view on buffer)
|
|
729
|
-
);
|
|
730
|
-
}
|
|
731
|
-
|
|
732
|
-
/**
|
|
733
|
-
* @param {AnyValue} v
|
|
734
|
-
* @returns {v is null|undefined}
|
|
735
|
-
*/
|
|
736
|
-
function isNullish(v) {
|
|
737
|
-
return v === null || v === undefined;
|
|
738
|
-
}
|
|
739
|
-
|
|
740
|
-
/**
|
|
741
|
-
* Compares two keys.
|
|
742
|
-
* @param {import('./Key.js').Key} first
|
|
743
|
-
* @param {import('./Key.js').Key} second
|
|
744
|
-
* @returns {0|1|-1}
|
|
745
|
-
*/
|
|
746
|
-
function cmp(first, second) {
|
|
747
|
-
var encodedKey1 = /** @type {string} */_encode(first);
|
|
748
|
-
var encodedKey2 = /** @type {string} */_encode(second);
|
|
749
|
-
var result = encodedKey1 > encodedKey2 ? 1 : encodedKey1 === encodedKey2 ? 0 : -1;
|
|
750
|
-
if (CFG.DEBUG) {
|
|
751
|
-
// verify that the keys encoded correctly
|
|
752
|
-
var decodedKey1 = _decode(encodedKey1);
|
|
753
|
-
var decodedKey2 = _decode(encodedKey2);
|
|
754
|
-
if (_typeof(first) === 'object') {
|
|
755
|
-
first = JSON.stringify(first);
|
|
756
|
-
decodedKey1 = JSON.stringify(decodedKey1);
|
|
757
|
-
}
|
|
758
|
-
if (_typeof(second) === 'object') {
|
|
759
|
-
second = JSON.stringify(second);
|
|
760
|
-
decodedKey2 = JSON.stringify(decodedKey2);
|
|
462
|
+
|
|
463
|
+
/**
|
|
464
|
+
* @typedef {{
|
|
465
|
+
* message: string|DOMString
|
|
466
|
+
* }} ErrorLike
|
|
467
|
+
*/
|
|
468
|
+
|
|
469
|
+
/**
|
|
470
|
+
* Logs detailed error information to the console.
|
|
471
|
+
* @param {string} name
|
|
472
|
+
* @param {string} message
|
|
473
|
+
* @param {string|ErrorLike|boolean|null} [error]
|
|
474
|
+
* @returns {void}
|
|
475
|
+
*/
|
|
476
|
+
function logError(name, message, error) {
|
|
477
|
+
if (CFG.DEBUG) {
|
|
478
|
+
const msg = error && typeof error === 'object' && error.message ? error.message : (/** @type {string} */error);
|
|
479
|
+
const method = typeof console.error === 'function' ? 'error' : 'log';
|
|
480
|
+
console[method](name + ': ' + message + '. ' + (msg || ''));
|
|
481
|
+
if (console.trace) {
|
|
482
|
+
console.trace();
|
|
483
|
+
}
|
|
761
484
|
}
|
|
485
|
+
}
|
|
762
486
|
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
487
|
+
/**
|
|
488
|
+
* @typedef {any} ArbitraryValue
|
|
489
|
+
*/
|
|
490
|
+
|
|
491
|
+
/**
|
|
492
|
+
* @param {ArbitraryValue} obj
|
|
493
|
+
* @returns {boolean}
|
|
494
|
+
*/
|
|
495
|
+
function isErrorOrDOMErrorOrDOMException(obj) {
|
|
496
|
+
return obj && typeof obj === 'object' &&
|
|
497
|
+
// We don't use util.isObj here as mutual dependency causing problems in Babel with browser
|
|
498
|
+
typeof obj.name === 'string';
|
|
499
|
+
}
|
|
500
|
+
let test,
|
|
501
|
+
useNativeDOMException = false;
|
|
502
|
+
|
|
503
|
+
// Test whether we can use the browser's native DOMException class
|
|
504
|
+
try {
|
|
505
|
+
test = createNativeDOMException('test name', 'test message');
|
|
506
|
+
if (isErrorOrDOMErrorOrDOMException(test) && test.name === 'test name' && test.message === 'test message') {
|
|
507
|
+
// Native DOMException works as expected
|
|
508
|
+
useNativeDOMException = true;
|
|
767
509
|
}
|
|
768
|
-
|
|
769
|
-
|
|
510
|
+
} catch (err) {}
|
|
511
|
+
const createDOMException = useNativeDOMException
|
|
512
|
+
// eslint-disable-next-line @stylistic/operator-linebreak -- Need JSDoc
|
|
513
|
+
?
|
|
514
|
+
/**
|
|
515
|
+
* @param {string} name
|
|
516
|
+
* @param {string} message
|
|
517
|
+
* @param {ErrorLike} [error]
|
|
518
|
+
* @returns {DOMException}
|
|
519
|
+
*/
|
|
520
|
+
function (name, message, error) {
|
|
521
|
+
logError(name, message, error);
|
|
522
|
+
return createNativeDOMException(name, message);
|
|
523
|
+
}
|
|
524
|
+
// eslint-disable-next-line @stylistic/operator-linebreak -- Need JSDoc
|
|
525
|
+
:
|
|
526
|
+
/**
|
|
527
|
+
* @param {string} name
|
|
528
|
+
* @param {string} message
|
|
529
|
+
* @param {ErrorLike} [error]
|
|
530
|
+
* @returns {Error}
|
|
531
|
+
*/
|
|
532
|
+
function (name, message, error) {
|
|
533
|
+
logError(name, message, error);
|
|
534
|
+
return createNonNativeDOMException(name, message);
|
|
535
|
+
};
|
|
536
|
+
|
|
537
|
+
/* eslint-disable new-cap -- ToString is how it is defined */
|
|
538
|
+
/* eslint-disable sonarjs/no-control-regex -- Needed */
|
|
539
|
+
|
|
540
|
+
/**
|
|
541
|
+
* @typedef {number} Integer
|
|
542
|
+
*/
|
|
543
|
+
|
|
544
|
+
/**
|
|
545
|
+
* @param {string} arg
|
|
546
|
+
* @returns {string}
|
|
547
|
+
*/
|
|
548
|
+
function escapeUnmatchedSurrogates(arg) {
|
|
549
|
+
// http://stackoverflow.com/a/6701665/271577
|
|
550
|
+
return arg.replaceAll(/([\uD800-\uDBFF])(?![\uDC00-\uDFFF])|(^|[^\uD800-\uDBFF])([\uDC00-\uDFFF])/gu, function (_, unmatchedHighSurrogate, precedingLow, unmatchedLowSurrogate) {
|
|
551
|
+
// Could add a corresponding surrogate for compatibility with `node-sqlite3`: http://bugs.python.org/issue12569 and http://stackoverflow.com/a/6701665/271577
|
|
552
|
+
// but Chrome having problems
|
|
553
|
+
if (unmatchedHighSurrogate) {
|
|
554
|
+
return '^2' + unmatchedHighSurrogate.codePointAt().toString(16).padStart(4, '0');
|
|
555
|
+
}
|
|
556
|
+
return (precedingLow || '') + '^3' + unmatchedLowSurrogate.codePointAt().toString(16).padStart(4, '0');
|
|
557
|
+
});
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
/**
|
|
561
|
+
* The escaping of unmatched surrogates was needed by Chrome but not Node.
|
|
562
|
+
* @param {string} arg
|
|
563
|
+
* @returns {string}
|
|
564
|
+
*/
|
|
565
|
+
function escapeSQLiteStatement(arg) {
|
|
566
|
+
return escapeUnmatchedSurrogates(arg.replaceAll('^', '^^').replaceAll('\0', '^0'));
|
|
567
|
+
}
|
|
568
|
+
|
|
569
|
+
/**
|
|
570
|
+
*
|
|
571
|
+
* @param {AnyValue} obj
|
|
572
|
+
* @returns {obj is object}
|
|
573
|
+
*/
|
|
574
|
+
function isObj(obj) {
|
|
575
|
+
return obj !== null && typeof obj === 'object';
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
/**
|
|
579
|
+
*
|
|
580
|
+
* @param {object} obj
|
|
581
|
+
* @returns {boolean}
|
|
582
|
+
*/
|
|
583
|
+
function isDate(obj) {
|
|
584
|
+
return isObj(obj) && 'getDate' in obj && typeof obj.getDate === 'function';
|
|
585
|
+
}
|
|
586
|
+
|
|
587
|
+
/**
|
|
588
|
+
*
|
|
589
|
+
* @param {object} obj
|
|
590
|
+
* @returns {boolean}
|
|
591
|
+
*/
|
|
592
|
+
function isBlob(obj) {
|
|
593
|
+
return isObj(obj) && 'size' in obj && typeof obj.size === 'number' && 'slice' in obj && typeof obj.slice === 'function' && !('lastModified' in obj);
|
|
594
|
+
}
|
|
595
|
+
|
|
596
|
+
/**
|
|
597
|
+
*
|
|
598
|
+
* @param {object} obj
|
|
599
|
+
* @returns {boolean}
|
|
600
|
+
*/
|
|
601
|
+
function isFile(obj) {
|
|
602
|
+
return isObj(obj) && 'name' in obj && typeof obj.name === 'string' && 'slice' in obj && typeof obj.slice === 'function' && 'lastModified' in obj;
|
|
603
|
+
}
|
|
604
|
+
|
|
605
|
+
/**
|
|
606
|
+
*
|
|
607
|
+
* @param {AnyValue} obj
|
|
608
|
+
* @returns {boolean}
|
|
609
|
+
*/
|
|
610
|
+
function isBinary(obj) {
|
|
611
|
+
return isObj(obj) && 'byteLength' in obj && typeof obj.byteLength === 'number' && ('slice' in obj && typeof obj.slice === 'function' ||
|
|
612
|
+
// `TypedArray` (view on buffer) or `ArrayBuffer`
|
|
613
|
+
'getFloat64' in obj && typeof obj.getFloat64 === 'function' // `DataView` (view on buffer)
|
|
614
|
+
);
|
|
615
|
+
}
|
|
616
|
+
|
|
617
|
+
/**
|
|
618
|
+
* @param {AnyValue} v
|
|
619
|
+
* @returns {v is null|undefined}
|
|
620
|
+
*/
|
|
621
|
+
function isNullish(v) {
|
|
622
|
+
return v === null || v === undefined;
|
|
623
|
+
}
|
|
624
|
+
|
|
625
|
+
/**
|
|
626
|
+
* Compares two keys.
|
|
627
|
+
* @param {import('./Key.js').Key} first
|
|
628
|
+
* @param {import('./Key.js').Key} second
|
|
629
|
+
* @returns {0|1|-1}
|
|
630
|
+
*/
|
|
631
|
+
function cmp(first, second) {
|
|
632
|
+
const encodedKey1 = /** @type {string} */encode(first);
|
|
633
|
+
const encodedKey2 = /** @type {string} */encode(second);
|
|
634
|
+
const result = encodedKey1 > encodedKey2 ? 1 : encodedKey1 === encodedKey2 ? 0 : -1;
|
|
635
|
+
if (CFG.DEBUG) {
|
|
636
|
+
// verify that the keys encoded correctly
|
|
637
|
+
let decodedKey1 = decode(encodedKey1);
|
|
638
|
+
let decodedKey2 = decode(encodedKey2);
|
|
639
|
+
if (typeof first === 'object') {
|
|
640
|
+
first = JSON.stringify(first);
|
|
641
|
+
decodedKey1 = JSON.stringify(decodedKey1);
|
|
642
|
+
}
|
|
643
|
+
if (typeof second === 'object') {
|
|
644
|
+
second = JSON.stringify(second);
|
|
645
|
+
decodedKey2 = JSON.stringify(decodedKey2);
|
|
646
|
+
}
|
|
647
|
+
|
|
648
|
+
// Encoding/decoding mismatches are usually due to a loss of
|
|
649
|
+
// floating-point precision
|
|
650
|
+
if (decodedKey1 !== first) {
|
|
651
|
+
console.warn(first + ' was incorrectly encoded as ' + decodedKey1);
|
|
652
|
+
}
|
|
653
|
+
if (decodedKey2 !== second) {
|
|
654
|
+
console.warn(second + ' was incorrectly encoded as ' + decodedKey2);
|
|
655
|
+
}
|
|
770
656
|
}
|
|
657
|
+
return result;
|
|
771
658
|
}
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
659
|
+
|
|
660
|
+
/**
|
|
661
|
+
* @typedef {NodeJS.TypedArray|DataView} ArrayBufferView
|
|
662
|
+
*/
|
|
663
|
+
|
|
664
|
+
/**
|
|
665
|
+
* @typedef {ArrayBufferView|ArrayBuffer} BufferSource
|
|
666
|
+
*/
|
|
667
|
+
|
|
668
|
+
/**
|
|
669
|
+
* @typedef {"number"|"date"|"string"|"binary"|"array"} KeyType
|
|
670
|
+
*/
|
|
671
|
+
|
|
672
|
+
/**
|
|
673
|
+
* @typedef {any} Value
|
|
674
|
+
*/
|
|
675
|
+
|
|
676
|
+
/**
|
|
677
|
+
* @typedef {any} Key
|
|
678
|
+
* @todo Specify possible value more precisely
|
|
679
|
+
*/
|
|
680
|
+
|
|
681
|
+
/**
|
|
682
|
+
* @typedef {KeyPath[]} KeyPathArray
|
|
683
|
+
*/
|
|
684
|
+
/**
|
|
685
|
+
* @typedef {string|KeyPathArray} KeyPath
|
|
686
|
+
*/
|
|
687
|
+
|
|
688
|
+
/**
|
|
689
|
+
* @typedef {object} KeyValueObject
|
|
690
|
+
* @property {KeyType|"NaN"|"null"|"undefined"|"boolean"|"object"|"symbol"|
|
|
691
|
+
* "function"|"bigint"} type If not `KeyType`, indicates invalid value
|
|
692
|
+
* @property {Value} [value]
|
|
693
|
+
* @property {boolean} [invalid]
|
|
694
|
+
* @property {string} [message]
|
|
695
|
+
* @todo Specify acceptable `value` more precisely
|
|
696
|
+
*/
|
|
697
|
+
|
|
698
|
+
/**
|
|
699
|
+
* @typedef {number|string|Date|ArrayBuffer} ValueTypePrimitive
|
|
700
|
+
*/
|
|
701
|
+
/**
|
|
702
|
+
* @typedef {ValueType[]} ValueTypeArray
|
|
703
|
+
*/
|
|
704
|
+
/**
|
|
705
|
+
* @typedef {ValueTypePrimitive|ValueTypeArray} ValueType
|
|
706
|
+
*/
|
|
707
|
+
|
|
708
|
+
/**
|
|
709
|
+
* Encodes the keys based on their types. This is required to maintain collations
|
|
710
|
+
* We leave space for future keys.
|
|
711
|
+
* @type {{[key: string]: Integer|string}}
|
|
712
|
+
*/
|
|
713
|
+
const keyTypeToEncodedChar = {
|
|
714
|
+
invalid: 100,
|
|
715
|
+
number: 200,
|
|
716
|
+
date: 300,
|
|
717
|
+
string: 400,
|
|
718
|
+
binary: 500,
|
|
719
|
+
array: 600
|
|
720
|
+
};
|
|
721
|
+
const keyTypes = /** @type {(KeyType|"invalid")[]} */Object.keys(keyTypeToEncodedChar);
|
|
722
|
+
keyTypes.forEach(k => {
|
|
723
|
+
keyTypeToEncodedChar[k] = String.fromCodePoint(/** @type {number} */keyTypeToEncodedChar[k]);
|
|
724
|
+
});
|
|
725
|
+
const encodedCharToKeyType = keyTypes.reduce((o, k) => {
|
|
726
|
+
o[keyTypeToEncodedChar[k]] = k;
|
|
727
|
+
return o;
|
|
728
|
+
}, /** @type {{[key: string]: KeyType|"invalid"}} */{});
|
|
729
|
+
|
|
730
|
+
/**
|
|
731
|
+
* The sign values for numbers, ordered from least to greatest.
|
|
732
|
+
* - "negativeInfinity": Sorts below all other values.
|
|
733
|
+
* - "bigNegative": Negative values less than or equal to negative one.
|
|
734
|
+
* - "smallNegative": Negative values between negative one and zero, noninclusive.
|
|
735
|
+
* - "smallPositive": Positive values between zero and one, including zero but not one.
|
|
736
|
+
* - "largePositive": Positive values greater than or equal to one.
|
|
737
|
+
* - "positiveInfinity": Sorts above all other values.
|
|
738
|
+
*/
|
|
739
|
+
const signValues = ['negativeInfinity', 'bigNegative', 'smallNegative', 'smallPositive', 'bigPositive', 'positiveInfinity'];
|
|
740
|
+
|
|
741
|
+
/**
|
|
742
|
+
* @typedef {any} AnyValue
|
|
743
|
+
*/
|
|
744
|
+
|
|
745
|
+
/**
|
|
746
|
+
* @type {{
|
|
747
|
+
* [key: string]: {
|
|
748
|
+
* encode: (param: any, inArray?: boolean) => string,
|
|
749
|
+
* decode: (param: string, inArray?: boolean) => any
|
|
750
|
+
* }
|
|
751
|
+
* }}
|
|
752
|
+
*/
|
|
753
|
+
const types = {
|
|
754
|
+
invalid: {
|
|
755
|
+
/**
|
|
756
|
+
* @returns {string}
|
|
757
|
+
*/
|
|
758
|
+
encode() {
|
|
759
|
+
return keyTypeToEncodedChar.invalid + '-';
|
|
760
|
+
},
|
|
761
|
+
/**
|
|
762
|
+
* @returns {undefined}
|
|
763
|
+
*/
|
|
764
|
+
decode() {
|
|
765
|
+
return undefined;
|
|
766
|
+
}
|
|
875
767
|
},
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
768
|
+
// Numbers are represented in a lexically sortable base-32 sign-exponent-mantissa
|
|
769
|
+
// notation.
|
|
770
|
+
//
|
|
771
|
+
// sign: takes a value between zero and five, inclusive. Represents infinite cases
|
|
772
|
+
// and the signs of both the exponent and the fractional part of the number.
|
|
773
|
+
// exponent: padded to two base-32 digits, represented by the 32's compliment in the
|
|
774
|
+
// "smallPositive" and "bigNegative" cases to ensure proper lexical sorting.
|
|
775
|
+
// mantissa: also called the fractional part. Normed 11-digit base-32 representation.
|
|
776
|
+
// Represented by the 32's compliment in the "smallNegative" and "bigNegative"
|
|
777
|
+
// cases to ensure proper lexical sorting.
|
|
778
|
+
number: {
|
|
779
|
+
// The encode step checks for six numeric cases and generates 14-digit encoded
|
|
780
|
+
// sign-exponent-mantissa strings.
|
|
781
|
+
/**
|
|
782
|
+
* @param {number} key
|
|
783
|
+
* @returns {string}
|
|
784
|
+
*/
|
|
785
|
+
encode(key) {
|
|
786
|
+
let key32 = key === Number.MIN_VALUE
|
|
787
|
+
// Mocha test `IDBFactory/cmp-spec.js` exposed problem for some
|
|
788
|
+
// Node (and Chrome) versions with `Number.MIN_VALUE` being treated
|
|
789
|
+
// as 0
|
|
790
|
+
// https://stackoverflow.com/questions/43305403/number-min-value-and-tostring
|
|
791
|
+
? '0.' + '0'.repeat(214) + '2' : Math.abs(key).toString(32);
|
|
792
|
+
// Get the index of the decimal.
|
|
793
|
+
const decimalIndex = key32.indexOf('.');
|
|
794
|
+
// Remove the decimal.
|
|
795
|
+
key32 = decimalIndex !== -1 ? key32.replace('.', '') : key32;
|
|
796
|
+
// Get the index of the first significant digit.
|
|
797
|
+
const significantDigitIndex = key32.search(/[^0]/u);
|
|
798
|
+
// Truncate leading zeros.
|
|
799
|
+
key32 = key32.slice(significantDigitIndex);
|
|
800
|
+
let sign, exponent, mantissa;
|
|
801
|
+
|
|
802
|
+
// Finite cases:
|
|
803
|
+
if (Number.isFinite(Number(key))) {
|
|
804
|
+
// Negative cases:
|
|
805
|
+
if (key < 0) {
|
|
806
|
+
// Negative exponent case:
|
|
807
|
+
if (key > -1) {
|
|
808
|
+
sign = signValues.indexOf('smallNegative');
|
|
809
|
+
exponent = padBase32Exponent(significantDigitIndex);
|
|
810
|
+
mantissa = flipBase32(padBase32Mantissa(key32));
|
|
811
|
+
// Non-negative exponent case:
|
|
812
|
+
} else {
|
|
813
|
+
sign = signValues.indexOf('bigNegative');
|
|
814
|
+
exponent = flipBase32(padBase32Exponent(decimalIndex !== -1 ? decimalIndex : key32.length));
|
|
815
|
+
mantissa = flipBase32(padBase32Mantissa(key32));
|
|
816
|
+
}
|
|
817
|
+
// Non-negative cases:
|
|
818
|
+
// Negative exponent case:
|
|
819
|
+
} else if (key < 1) {
|
|
820
|
+
sign = signValues.indexOf('smallPositive');
|
|
821
|
+
exponent = flipBase32(padBase32Exponent(significantDigitIndex));
|
|
822
|
+
mantissa = padBase32Mantissa(key32);
|
|
926
823
|
// Non-negative exponent case:
|
|
927
824
|
} else {
|
|
928
|
-
sign = signValues.indexOf('
|
|
929
|
-
exponent =
|
|
930
|
-
mantissa =
|
|
825
|
+
sign = signValues.indexOf('bigPositive');
|
|
826
|
+
exponent = padBase32Exponent(decimalIndex !== -1 ? decimalIndex : key32.length);
|
|
827
|
+
mantissa = padBase32Mantissa(key32);
|
|
931
828
|
}
|
|
932
|
-
//
|
|
933
|
-
// Negative exponent case:
|
|
934
|
-
} else if (key < 1) {
|
|
935
|
-
sign = signValues.indexOf('smallPositive');
|
|
936
|
-
exponent = flipBase32(padBase32Exponent(significantDigitIndex));
|
|
937
|
-
mantissa = padBase32Mantissa(key32);
|
|
938
|
-
// Non-negative exponent case:
|
|
829
|
+
// Infinite cases:
|
|
939
830
|
} else {
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
831
|
+
exponent = zeros(2);
|
|
832
|
+
mantissa = zeros(11);
|
|
833
|
+
sign = signValues.indexOf(key > 0 ? 'positiveInfinity' : 'negativeInfinity');
|
|
834
|
+
}
|
|
835
|
+
return keyTypeToEncodedChar.number + '-' + sign + exponent + mantissa;
|
|
836
|
+
},
|
|
837
|
+
// The decode step must interpret the sign, reflip values encoded as the 32's complements,
|
|
838
|
+
// apply signs to the exponent and mantissa, do the base-32 power operation, and return
|
|
839
|
+
// the original JavaScript number values.
|
|
840
|
+
/**
|
|
841
|
+
* @param {string} key
|
|
842
|
+
* @returns {number}
|
|
843
|
+
*/
|
|
844
|
+
decode(key) {
|
|
845
|
+
const sign = Number(key.slice(2, 3));
|
|
846
|
+
let exponent = key.slice(3, 5);
|
|
847
|
+
let mantissa = key.slice(5, 16);
|
|
848
|
+
switch (signValues[sign]) {
|
|
849
|
+
case 'negativeInfinity':
|
|
850
|
+
return Number.NEGATIVE_INFINITY;
|
|
851
|
+
case 'positiveInfinity':
|
|
852
|
+
return Number.POSITIVE_INFINITY;
|
|
853
|
+
case 'bigPositive':
|
|
854
|
+
return pow32(mantissa, exponent);
|
|
855
|
+
case 'smallPositive':
|
|
856
|
+
exponent = negate(flipBase32(exponent));
|
|
857
|
+
return pow32(mantissa, exponent);
|
|
858
|
+
case 'smallNegative':
|
|
859
|
+
exponent = negate(exponent);
|
|
860
|
+
mantissa = flipBase32(mantissa);
|
|
861
|
+
return -pow32(mantissa, exponent);
|
|
862
|
+
case 'bigNegative':
|
|
863
|
+
exponent = flipBase32(exponent);
|
|
864
|
+
mantissa = flipBase32(mantissa);
|
|
865
|
+
return -pow32(mantissa, exponent);
|
|
866
|
+
default:
|
|
867
|
+
throw new Error('Invalid number.');
|
|
943
868
|
}
|
|
944
|
-
// Infinite cases:
|
|
945
|
-
} else {
|
|
946
|
-
exponent = zeros(2);
|
|
947
|
-
mantissa = zeros(11);
|
|
948
|
-
sign = signValues.indexOf(key > 0 ? 'positiveInfinity' : 'negativeInfinity');
|
|
949
869
|
}
|
|
950
|
-
return keyTypeToEncodedChar.number + '-' + sign + exponent + mantissa;
|
|
951
870
|
},
|
|
952
|
-
//
|
|
953
|
-
//
|
|
954
|
-
// the
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
},
|
|
986
|
-
// Strings are encoded as JSON strings (with quotes and unicode characters escaped).
|
|
987
|
-
//
|
|
988
|
-
// If the strings are in an array, then some extra encoding is done to make sorting work correctly:
|
|
989
|
-
// Since we can't force all strings to be the same length, we need to ensure that characters line-up properly
|
|
990
|
-
// for sorting, while also accounting for the extra characters that are added when the array itself is encoded as JSON.
|
|
991
|
-
// To do this, each character of the string is prepended with a dash ("-"), and a space is added to the end of the string.
|
|
992
|
-
// This effectively doubles the size of every string, but it ensures that when two arrays of strings are compared,
|
|
993
|
-
// the indexes of each string's characters line up with each other.
|
|
994
|
-
string: {
|
|
995
|
-
/**
|
|
996
|
-
* @param {string} key
|
|
997
|
-
* @param {boolean} [inArray]
|
|
998
|
-
* @returns {string}
|
|
999
|
-
*/
|
|
1000
|
-
encode: function encode(key, inArray) {
|
|
1001
|
-
if (inArray) {
|
|
1002
|
-
// prepend each character with a dash, and append a space to the end
|
|
1003
|
-
key = key.replaceAll(/((?:[\0-\t\x0B\f\x0E-\u2027\u202A-\uD7FF\uE000-\uFFFF]|[\uD800-\uDBFF][\uDC00-\uDFFF]|[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?:[^\uD800-\uDBFF]|^)[\uDC00-\uDFFF]))/g, '-$1') + ' ';
|
|
871
|
+
// Strings are encoded as JSON strings (with quotes and unicode characters escaped).
|
|
872
|
+
//
|
|
873
|
+
// If the strings are in an array, then some extra encoding is done to make sorting work correctly:
|
|
874
|
+
// Since we can't force all strings to be the same length, we need to ensure that characters line-up properly
|
|
875
|
+
// for sorting, while also accounting for the extra characters that are added when the array itself is encoded as JSON.
|
|
876
|
+
// To do this, each character of the string is prepended with a dash ("-"), and a space is added to the end of the string.
|
|
877
|
+
// This effectively doubles the size of every string, but it ensures that when two arrays of strings are compared,
|
|
878
|
+
// the indexes of each string's characters line up with each other.
|
|
879
|
+
string: {
|
|
880
|
+
/**
|
|
881
|
+
* @param {string} key
|
|
882
|
+
* @param {boolean} [inArray]
|
|
883
|
+
* @returns {string}
|
|
884
|
+
*/
|
|
885
|
+
encode(key, inArray) {
|
|
886
|
+
if (inArray) {
|
|
887
|
+
// prepend each character with a dash, and append a space to the end
|
|
888
|
+
key = key.replaceAll(/(.)/gu, '-$1') + ' ';
|
|
889
|
+
}
|
|
890
|
+
return keyTypeToEncodedChar.string + '-' + key;
|
|
891
|
+
},
|
|
892
|
+
/**
|
|
893
|
+
* @param {string} key
|
|
894
|
+
* @param {boolean} [inArray]
|
|
895
|
+
* @returns {string}
|
|
896
|
+
*/
|
|
897
|
+
decode(key, inArray) {
|
|
898
|
+
key = key.slice(2);
|
|
899
|
+
if (inArray) {
|
|
900
|
+
// remove the space at the end, and the dash before each character
|
|
901
|
+
key = key.slice(0, -1).replaceAll(/-(.)/gu, '$1');
|
|
902
|
+
}
|
|
903
|
+
return key;
|
|
1004
904
|
}
|
|
1005
|
-
return keyTypeToEncodedChar.string + '-' + key;
|
|
1006
905
|
},
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
}
|
|
1020
|
-
},
|
|
1021
|
-
// Arrays are encoded as JSON strings.
|
|
1022
|
-
// An extra, value is added to each array during encoding to make
|
|
1023
|
-
// empty arrays sort correctly.
|
|
1024
|
-
array: {
|
|
1025
|
-
/**
|
|
1026
|
-
* @param {ValueTypeArray} key
|
|
1027
|
-
* @returns {string}
|
|
1028
|
-
*/
|
|
1029
|
-
encode: function encode(key) {
|
|
1030
|
-
var encoded = [];
|
|
1031
|
-
var _iterator = _createForOfIteratorHelper(key.entries()),
|
|
1032
|
-
_step;
|
|
1033
|
-
try {
|
|
1034
|
-
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
1035
|
-
var _step$value = _slicedToArray(_step.value, 2),
|
|
1036
|
-
i = _step$value[0],
|
|
1037
|
-
item = _step$value[1];
|
|
1038
|
-
var encodedItem = _encode(item, true); // encode the array item
|
|
906
|
+
// Arrays are encoded as JSON strings.
|
|
907
|
+
// An extra, value is added to each array during encoding to make
|
|
908
|
+
// empty arrays sort correctly.
|
|
909
|
+
array: {
|
|
910
|
+
/**
|
|
911
|
+
* @param {ValueTypeArray} key
|
|
912
|
+
* @returns {string}
|
|
913
|
+
*/
|
|
914
|
+
encode(key) {
|
|
915
|
+
const encoded = [];
|
|
916
|
+
for (const [i, item] of key.entries()) {
|
|
917
|
+
const encodedItem = encode(item, true); // encode the array item
|
|
1039
918
|
encoded[i] = encodedItem;
|
|
1040
919
|
}
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
}
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
var item = decoded[i];
|
|
1058
|
-
var decodedItem = _decode(item, true); // decode the item
|
|
1059
|
-
decoded[i] = decodedItem;
|
|
920
|
+
encoded.push(keyTypeToEncodedChar.invalid + '-'); // append an extra item, so empty arrays sort correctly
|
|
921
|
+
return keyTypeToEncodedChar.array + '-' + JSON.stringify(encoded);
|
|
922
|
+
},
|
|
923
|
+
/**
|
|
924
|
+
* @param {string} key
|
|
925
|
+
* @returns {ValueTypeArray}
|
|
926
|
+
*/
|
|
927
|
+
decode(key) {
|
|
928
|
+
const decoded = JSON.parse(key.slice(2));
|
|
929
|
+
decoded.pop(); // remove the extra item
|
|
930
|
+
for (let i = 0; i < decoded.length; i++) {
|
|
931
|
+
const item = decoded[i];
|
|
932
|
+
const decodedItem = decode(item, true); // decode the item
|
|
933
|
+
decoded[i] = decodedItem;
|
|
934
|
+
}
|
|
935
|
+
return decoded;
|
|
1060
936
|
}
|
|
1061
|
-
return decoded;
|
|
1062
|
-
}
|
|
1063
|
-
},
|
|
1064
|
-
// Dates are encoded as ISO 8601 strings, in UTC time zone.
|
|
1065
|
-
date: {
|
|
1066
|
-
/**
|
|
1067
|
-
* @param {Date} key
|
|
1068
|
-
* @returns {string}
|
|
1069
|
-
*/
|
|
1070
|
-
encode: function encode(key) {
|
|
1071
|
-
return keyTypeToEncodedChar.date + '-' + key.toJSON();
|
|
1072
937
|
},
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
return String(b).padStart(3, '0');
|
|
1090
|
-
}) // e.g., '255,005,254,000,001,033'
|
|
1091
|
-
: '');
|
|
938
|
+
// Dates are encoded as ISO 8601 strings, in UTC time zone.
|
|
939
|
+
date: {
|
|
940
|
+
/**
|
|
941
|
+
* @param {Date} key
|
|
942
|
+
* @returns {string}
|
|
943
|
+
*/
|
|
944
|
+
encode(key) {
|
|
945
|
+
return keyTypeToEncodedChar.date + '-' + key.toJSON();
|
|
946
|
+
},
|
|
947
|
+
/**
|
|
948
|
+
* @param {string} key
|
|
949
|
+
* @returns {Date}
|
|
950
|
+
*/
|
|
951
|
+
decode(key) {
|
|
952
|
+
return new Date(key.slice(2));
|
|
953
|
+
}
|
|
1092
954
|
},
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
}
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
955
|
+
binary: {
|
|
956
|
+
// `ArrayBuffer`/Views on buffers (`TypedArray` or `DataView`)
|
|
957
|
+
/**
|
|
958
|
+
* @param {BufferSource} key
|
|
959
|
+
* @returns {string}
|
|
960
|
+
*/
|
|
961
|
+
encode(key) {
|
|
962
|
+
return keyTypeToEncodedChar.binary + '-' + (key.byteLength ? [...getCopyBytesHeldByBufferSource(key)].map(b => String(b).padStart(3, '0')) // e.g., '255,005,254,000,001,033'
|
|
963
|
+
: '');
|
|
964
|
+
},
|
|
965
|
+
/**
|
|
966
|
+
* @param {string} key
|
|
967
|
+
* @returns {ArrayBuffer}
|
|
968
|
+
*/
|
|
969
|
+
decode(key) {
|
|
970
|
+
// Set the entries in buffer's [[ArrayBufferData]] to those in `value`
|
|
971
|
+
const k = key.slice(2);
|
|
972
|
+
const arr = k.length ? k.split(',').map(s => Number.parseInt(s)) : [];
|
|
973
|
+
const buffer = new ArrayBuffer(arr.length);
|
|
974
|
+
const uint8 = new Uint8Array(buffer);
|
|
975
|
+
uint8.set(arr);
|
|
976
|
+
return buffer;
|
|
977
|
+
}
|
|
1107
978
|
}
|
|
979
|
+
};
|
|
980
|
+
|
|
981
|
+
/**
|
|
982
|
+
* Return a padded base-32 exponent value.
|
|
983
|
+
* @param {number} n
|
|
984
|
+
* @returns {string}
|
|
985
|
+
*/
|
|
986
|
+
function padBase32Exponent(n) {
|
|
987
|
+
const exp = n.toString(32);
|
|
988
|
+
return exp.length === 1 ? '0' + exp : exp;
|
|
1108
989
|
}
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
return (s + zeros(11)).slice(0, 11);
|
|
1128
|
-
}
|
|
1129
|
-
|
|
1130
|
-
/**
|
|
1131
|
-
* Flips each digit of a base-32 encoded string.
|
|
1132
|
-
* @param {string} encoded
|
|
1133
|
-
* @returns {string}
|
|
1134
|
-
*/
|
|
1135
|
-
function flipBase32(encoded) {
|
|
1136
|
-
var flipped = '';
|
|
1137
|
-
var _iterator2 = _createForOfIteratorHelper(encoded),
|
|
1138
|
-
_step2;
|
|
1139
|
-
try {
|
|
1140
|
-
for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
|
|
1141
|
-
var ch = _step2.value;
|
|
990
|
+
|
|
991
|
+
/**
|
|
992
|
+
* Return a padded base-32 mantissa.
|
|
993
|
+
* @param {string} s
|
|
994
|
+
* @returns {string}
|
|
995
|
+
*/
|
|
996
|
+
function padBase32Mantissa(s) {
|
|
997
|
+
return (s + zeros(11)).slice(0, 11);
|
|
998
|
+
}
|
|
999
|
+
|
|
1000
|
+
/**
|
|
1001
|
+
* Flips each digit of a base-32 encoded string.
|
|
1002
|
+
* @param {string} encoded
|
|
1003
|
+
* @returns {string}
|
|
1004
|
+
*/
|
|
1005
|
+
function flipBase32(encoded) {
|
|
1006
|
+
let flipped = '';
|
|
1007
|
+
for (const ch of encoded) {
|
|
1142
1008
|
flipped += (31 - Number.parseInt(ch, 32)).toString(32);
|
|
1143
1009
|
}
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1010
|
+
return flipped;
|
|
1011
|
+
}
|
|
1012
|
+
|
|
1013
|
+
/**
|
|
1014
|
+
* Base-32 power function.
|
|
1015
|
+
* RESEARCH: This function does not precisely decode floats because it performs
|
|
1016
|
+
* floating point arithmetic to recover values. But can the original values be
|
|
1017
|
+
* recovered exactly?
|
|
1018
|
+
* Someone may have already figured out a good way to store JavaScript floats as
|
|
1019
|
+
* binary strings and convert back. Barring a better method, however, one route
|
|
1020
|
+
* may be to generate decimal strings that `parseFloat` decodes predictably.
|
|
1021
|
+
* @param {string} mantissa
|
|
1022
|
+
* @param {string} exponent
|
|
1023
|
+
* @returns {number}
|
|
1024
|
+
*/
|
|
1025
|
+
function pow32(mantissa, exponent) {
|
|
1026
|
+
const exp = Number.parseInt(exponent, 32);
|
|
1027
|
+
if (exp < 0) {
|
|
1028
|
+
return roundToPrecision(Number.parseInt(mantissa, 32) * 32 ** (exp - 10));
|
|
1029
|
+
}
|
|
1030
|
+
if (exp < 11) {
|
|
1031
|
+
const whole = mantissa.slice(0, exp);
|
|
1032
|
+
const wholeNum = Number.parseInt(whole, 32);
|
|
1033
|
+
const fraction = mantissa.slice(exp);
|
|
1034
|
+
const fractionNum = Number.parseInt(fraction, 32) * 32 ** (exp - 11);
|
|
1035
|
+
return roundToPrecision(wholeNum + fractionNum);
|
|
1036
|
+
}
|
|
1037
|
+
const expansion = mantissa + zeros(exp - 11);
|
|
1038
|
+
return Number.parseInt(expansion, 32);
|
|
1039
|
+
}
|
|
1040
|
+
|
|
1041
|
+
/**
|
|
1042
|
+
* @typedef {number} Float
|
|
1043
|
+
*/
|
|
1044
|
+
|
|
1045
|
+
/**
|
|
1046
|
+
* @param {Float} num
|
|
1047
|
+
* @param {Float} [precision]
|
|
1048
|
+
* @returns {Float}
|
|
1049
|
+
*/
|
|
1050
|
+
function roundToPrecision(num, precision = 16) {
|
|
1051
|
+
return Number.parseFloat(num.toPrecision(precision));
|
|
1148
1052
|
}
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
* Someone may have already figured out a good way to store JavaScript floats as
|
|
1158
|
-
* binary strings and convert back. Barring a better method, however, one route
|
|
1159
|
-
* may be to generate decimal strings that `parseFloat` decodes predictably.
|
|
1160
|
-
* @param {string} mantissa
|
|
1161
|
-
* @param {string} exponent
|
|
1162
|
-
* @returns {number}
|
|
1163
|
-
*/
|
|
1164
|
-
function pow32(mantissa, exponent) {
|
|
1165
|
-
var exp = Number.parseInt(exponent, 32);
|
|
1166
|
-
if (exp < 0) {
|
|
1167
|
-
return roundToPrecision(Number.parseInt(mantissa, 32) * Math.pow(32, exp - 10));
|
|
1053
|
+
|
|
1054
|
+
/**
|
|
1055
|
+
* Returns a string of n zeros.
|
|
1056
|
+
* @param {number} n
|
|
1057
|
+
* @returns {string}
|
|
1058
|
+
*/
|
|
1059
|
+
function zeros(n) {
|
|
1060
|
+
return '0'.repeat(n);
|
|
1168
1061
|
}
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
1062
|
+
|
|
1063
|
+
/**
|
|
1064
|
+
* Negates numeric strings.
|
|
1065
|
+
* @param {string} s
|
|
1066
|
+
* @returns {string}
|
|
1067
|
+
*/
|
|
1068
|
+
function negate(s) {
|
|
1069
|
+
return '-' + s;
|
|
1175
1070
|
}
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
/**
|
|
1195
|
-
* Returns a string of n zeros.
|
|
1196
|
-
* @param {number} n
|
|
1197
|
-
* @returns {string}
|
|
1198
|
-
*/
|
|
1199
|
-
function zeros(n) {
|
|
1200
|
-
return '0'.repeat(n);
|
|
1201
|
-
}
|
|
1202
|
-
|
|
1203
|
-
/**
|
|
1204
|
-
* Negates numeric strings.
|
|
1205
|
-
* @param {string} s
|
|
1206
|
-
* @returns {string}
|
|
1207
|
-
*/
|
|
1208
|
-
function negate(s) {
|
|
1209
|
-
return '-' + s;
|
|
1210
|
-
}
|
|
1211
|
-
|
|
1212
|
-
/**
|
|
1213
|
-
* @param {Key} key
|
|
1214
|
-
* @returns {KeyType|"invalid"}
|
|
1215
|
-
*/
|
|
1216
|
-
function getKeyType(key) {
|
|
1217
|
-
if (Array.isArray(key)) {
|
|
1218
|
-
return 'array';
|
|
1071
|
+
|
|
1072
|
+
/**
|
|
1073
|
+
* @param {Key} key
|
|
1074
|
+
* @returns {KeyType|"invalid"}
|
|
1075
|
+
*/
|
|
1076
|
+
function getKeyType(key) {
|
|
1077
|
+
if (Array.isArray(key)) {
|
|
1078
|
+
return 'array';
|
|
1079
|
+
}
|
|
1080
|
+
if (isDate(key)) {
|
|
1081
|
+
return 'date';
|
|
1082
|
+
}
|
|
1083
|
+
if (isBinary(key)) {
|
|
1084
|
+
return 'binary';
|
|
1085
|
+
}
|
|
1086
|
+
const keyType = typeof key;
|
|
1087
|
+
return ['string', 'number'].includes(keyType) ? (/** @type {"string"|"number"} */keyType) : 'invalid';
|
|
1219
1088
|
}
|
|
1220
|
-
|
|
1221
|
-
|
|
1089
|
+
|
|
1090
|
+
/**
|
|
1091
|
+
* Keys must be strings, numbers (besides `NaN`), Dates (if value is not
|
|
1092
|
+
* `NaN`), binary objects or Arrays.
|
|
1093
|
+
* @param {Value} input The key input
|
|
1094
|
+
* @param {Value[]|null|undefined} [seen] An array of already seen keys
|
|
1095
|
+
* @returns {KeyValueObject}
|
|
1096
|
+
*/
|
|
1097
|
+
function convertValueToKey(input, seen) {
|
|
1098
|
+
return convertValueToKeyValueDecoded(input, seen, false, true);
|
|
1222
1099
|
}
|
|
1223
|
-
|
|
1224
|
-
|
|
1100
|
+
|
|
1101
|
+
/**
|
|
1102
|
+
* Currently not in use.
|
|
1103
|
+
* @param {Value} input
|
|
1104
|
+
* @returns {KeyValueObject}
|
|
1105
|
+
*/
|
|
1106
|
+
function convertValueToMultiEntryKey(input) {
|
|
1107
|
+
return convertValueToKeyValueDecoded(input, null, true, true);
|
|
1225
1108
|
}
|
|
1226
|
-
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
|
|
1233
|
-
|
|
1234
|
-
|
|
1235
|
-
|
|
1236
|
-
|
|
1237
|
-
|
|
1238
|
-
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1244
|
-
|
|
1245
|
-
|
|
1246
|
-
|
|
1247
|
-
return convertValueToKeyValueDecoded(input, null, true, true);
|
|
1248
|
-
}
|
|
1249
|
-
|
|
1250
|
-
/**
|
|
1251
|
-
*
|
|
1252
|
-
* @param {BufferSource} O
|
|
1253
|
-
* @throws {TypeError}
|
|
1254
|
-
* @see https://heycam.github.io/webidl/#ref-for-dfn-get-buffer-source-copy-2
|
|
1255
|
-
* @returns {Uint8Array}
|
|
1256
|
-
*/
|
|
1257
|
-
function getCopyBytesHeldByBufferSource(O) {
|
|
1258
|
-
var offset = 0;
|
|
1259
|
-
var length = 0;
|
|
1260
|
-
if (ArrayBuffer.isView(O)) {
|
|
1261
|
-
// Has [[ViewedArrayBuffer]] internal slot
|
|
1262
|
-
var arrayBuffer = O.buffer;
|
|
1263
|
-
if (arrayBuffer === undefined) {
|
|
1264
|
-
throw new TypeError('Could not copy the bytes held by a buffer source as the buffer was undefined.');
|
|
1109
|
+
|
|
1110
|
+
/**
|
|
1111
|
+
*
|
|
1112
|
+
* @param {BufferSource} O
|
|
1113
|
+
* @throws {TypeError}
|
|
1114
|
+
* @see https://heycam.github.io/webidl/#ref-for-dfn-get-buffer-source-copy-2
|
|
1115
|
+
* @returns {Uint8Array}
|
|
1116
|
+
*/
|
|
1117
|
+
function getCopyBytesHeldByBufferSource(O) {
|
|
1118
|
+
let offset = 0;
|
|
1119
|
+
let length = 0;
|
|
1120
|
+
if (ArrayBuffer.isView(O)) {
|
|
1121
|
+
// Has [[ViewedArrayBuffer]] internal slot
|
|
1122
|
+
const arrayBuffer = O.buffer;
|
|
1123
|
+
if (arrayBuffer === undefined) {
|
|
1124
|
+
throw new TypeError('Could not copy the bytes held by a buffer source as the buffer was undefined.');
|
|
1125
|
+
}
|
|
1126
|
+
offset = O.byteOffset; // [[ByteOffset]] (will also throw as desired if detached)
|
|
1127
|
+
length = O.byteLength; // [[ByteLength]] (will also throw as desired if detached)
|
|
1128
|
+
} else {
|
|
1129
|
+
length = O.byteLength; // [[ArrayBufferByteLength]] on ArrayBuffer (will also throw as desired if detached)
|
|
1265
1130
|
}
|
|
1266
|
-
|
|
1267
|
-
|
|
1268
|
-
|
|
1269
|
-
|
|
1131
|
+
// const octets = new Uint8Array(input);
|
|
1132
|
+
// const octets = types.binary.decode(types.binary.encode(input));
|
|
1133
|
+
return new Uint8Array(
|
|
1134
|
+
// Should allow DataView
|
|
1135
|
+
/** @type {ArrayBuffer} */
|
|
1136
|
+
'buffer' in O && O.buffer || O, offset, length);
|
|
1270
1137
|
}
|
|
1271
|
-
|
|
1272
|
-
|
|
1273
|
-
|
|
1274
|
-
|
|
1275
|
-
|
|
1276
|
-
|
|
1277
|
-
|
|
1278
|
-
|
|
1279
|
-
|
|
1280
|
-
|
|
1281
|
-
|
|
1282
|
-
|
|
1283
|
-
|
|
1284
|
-
|
|
1285
|
-
|
|
1286
|
-
|
|
1287
|
-
|
|
1288
|
-
|
|
1289
|
-
|
|
1290
|
-
|
|
1291
|
-
|
|
1292
|
-
|
|
1293
|
-
|
|
1294
|
-
|
|
1295
|
-
type
|
|
1296
|
-
|
|
1297
|
-
message: 'An array key cannot be circular'
|
|
1138
|
+
|
|
1139
|
+
/**
|
|
1140
|
+
* Shortcut utility to avoid returning full keys from `convertValueToKey`
|
|
1141
|
+
* and subsequent need to process in calling code unless `fullKeys` is
|
|
1142
|
+
* set; may throw.
|
|
1143
|
+
* @param {Value} input
|
|
1144
|
+
* @param {Value[]|null} [seen]
|
|
1145
|
+
* @param {boolean} [multiEntry]
|
|
1146
|
+
* @param {boolean} [fullKeys]
|
|
1147
|
+
* @throws {TypeError} See `getCopyBytesHeldByBufferSource`
|
|
1148
|
+
* @todo Document other allowable `input`
|
|
1149
|
+
* @returns {KeyValueObject}
|
|
1150
|
+
*/
|
|
1151
|
+
function convertValueToKeyValueDecoded(input, seen, multiEntry, fullKeys) {
|
|
1152
|
+
seen = seen || [];
|
|
1153
|
+
if (seen.includes(input)) {
|
|
1154
|
+
return {
|
|
1155
|
+
type: 'array',
|
|
1156
|
+
invalid: true,
|
|
1157
|
+
message: 'An array key cannot be circular'
|
|
1158
|
+
};
|
|
1159
|
+
}
|
|
1160
|
+
const type = getKeyType(input);
|
|
1161
|
+
const ret = {
|
|
1162
|
+
type,
|
|
1163
|
+
value: input
|
|
1298
1164
|
};
|
|
1299
|
-
|
|
1300
|
-
|
|
1301
|
-
|
|
1302
|
-
|
|
1303
|
-
|
|
1304
|
-
|
|
1305
|
-
|
|
1306
|
-
|
|
1307
|
-
|
|
1308
|
-
|
|
1309
|
-
// List as 'NaN' type for convenience of consumers in reporting errors
|
|
1310
|
-
return {
|
|
1311
|
-
type: 'NaN',
|
|
1312
|
-
invalid: true
|
|
1313
|
-
};
|
|
1314
|
-
}
|
|
1165
|
+
switch (type) {
|
|
1166
|
+
case 'number':
|
|
1167
|
+
{
|
|
1168
|
+
if (Number.isNaN(input)) {
|
|
1169
|
+
// List as 'NaN' type for convenience of consumers in reporting errors
|
|
1170
|
+
return {
|
|
1171
|
+
type: 'NaN',
|
|
1172
|
+
invalid: true
|
|
1173
|
+
};
|
|
1174
|
+
}
|
|
1315
1175
|
|
|
1316
|
-
|
|
1317
|
-
|
|
1318
|
-
|
|
1176
|
+
// https://github.com/w3c/IndexedDB/issues/375
|
|
1177
|
+
// https://github.com/w3c/IndexedDB/pull/386
|
|
1178
|
+
if (Object.is(input, -0)) {
|
|
1179
|
+
return {
|
|
1180
|
+
type,
|
|
1181
|
+
value: 0
|
|
1182
|
+
};
|
|
1183
|
+
}
|
|
1184
|
+
return /** @type {{type: KeyType; value: Value}} */ret;
|
|
1185
|
+
}
|
|
1186
|
+
case 'string':
|
|
1187
|
+
{
|
|
1188
|
+
return /** @type {{type: KeyType; value: Value}} */ret;
|
|
1189
|
+
}
|
|
1190
|
+
case 'binary':
|
|
1191
|
+
{
|
|
1192
|
+
// May throw (if detached)
|
|
1193
|
+
// Get a copy of the bytes held by the buffer source
|
|
1194
|
+
// https://heycam.github.io/webidl/#ref-for-dfn-get-buffer-source-copy-2
|
|
1195
|
+
const octets = getCopyBytesHeldByBufferSource(/** @type {BufferSource} */input);
|
|
1319
1196
|
return {
|
|
1320
|
-
type:
|
|
1321
|
-
value:
|
|
1197
|
+
type: 'binary',
|
|
1198
|
+
value: octets
|
|
1322
1199
|
};
|
|
1323
1200
|
}
|
|
1324
|
-
|
|
1325
|
-
|
|
1326
|
-
|
|
1327
|
-
|
|
1328
|
-
|
|
1329
|
-
|
|
1330
|
-
|
|
1331
|
-
|
|
1332
|
-
|
|
1333
|
-
|
|
1334
|
-
// https://heycam.github.io/webidl/#ref-for-dfn-get-buffer-source-copy-2
|
|
1335
|
-
var octets = getCopyBytesHeldByBufferSource(/** @type {BufferSource} */input);
|
|
1336
|
-
return {
|
|
1337
|
-
type: 'binary',
|
|
1338
|
-
value: octets
|
|
1339
|
-
};
|
|
1340
|
-
}
|
|
1341
|
-
case 'array':
|
|
1342
|
-
{
|
|
1343
|
-
// May throw (from binary)
|
|
1344
|
-
var arr = /** @type {Array<any>} */input;
|
|
1345
|
-
var len = arr.length;
|
|
1346
|
-
seen.push(input);
|
|
1347
|
-
|
|
1348
|
-
/** @type {(KeyValueObject|Value)[]} */
|
|
1349
|
-
var keys = [];
|
|
1350
|
-
var _loop = function _loop() {
|
|
1201
|
+
case 'array':
|
|
1202
|
+
{
|
|
1203
|
+
// May throw (from binary)
|
|
1204
|
+
const arr = /** @type {Array<any>} */input;
|
|
1205
|
+
const len = arr.length;
|
|
1206
|
+
seen.push(input);
|
|
1207
|
+
|
|
1208
|
+
/** @type {(KeyValueObject|Value)[]} */
|
|
1209
|
+
const keys = [];
|
|
1210
|
+
for (let i = 0; i < len; i++) {
|
|
1351
1211
|
// We cannot iterate here with array extras as we must ensure sparse arrays are invalidated
|
|
1352
1212
|
if (!multiEntry && !Object.hasOwn(arr, i)) {
|
|
1353
1213
|
return {
|
|
1354
|
-
|
|
1355
|
-
|
|
1356
|
-
|
|
1357
|
-
message: 'Does not have own index property'
|
|
1358
|
-
}
|
|
1214
|
+
type,
|
|
1215
|
+
invalid: true,
|
|
1216
|
+
message: 'Does not have own index property'
|
|
1359
1217
|
};
|
|
1360
1218
|
}
|
|
1361
1219
|
try {
|
|
1362
|
-
|
|
1363
|
-
|
|
1220
|
+
const entry = arr[i];
|
|
1221
|
+
const key = convertValueToKeyValueDecoded(entry, seen, false, fullKeys); // Though steps do not list rethrowing, the next is returnifabrupt when not multiEntry
|
|
1364
1222
|
if (key.invalid) {
|
|
1365
1223
|
if (multiEntry) {
|
|
1366
|
-
|
|
1224
|
+
continue;
|
|
1367
1225
|
}
|
|
1368
1226
|
return {
|
|
1369
|
-
|
|
1370
|
-
|
|
1371
|
-
|
|
1372
|
-
message: 'Bad array entry value-to-key conversion'
|
|
1373
|
-
}
|
|
1227
|
+
type,
|
|
1228
|
+
invalid: true,
|
|
1229
|
+
message: 'Bad array entry value-to-key conversion'
|
|
1374
1230
|
};
|
|
1375
1231
|
}
|
|
1376
|
-
if (!multiEntry || !fullKeys && keys.every(
|
|
1377
|
-
return cmp(k, key.value) !== 0;
|
|
1378
|
-
}) || fullKeys && keys.every(function (k) {
|
|
1379
|
-
return cmp(k, key) !== 0;
|
|
1380
|
-
})) {
|
|
1232
|
+
if (!multiEntry || !fullKeys && keys.every(k => cmp(k, key.value) !== 0) || fullKeys && keys.every(k => cmp(k, key) !== 0)) {
|
|
1381
1233
|
keys.push(fullKeys ? key : key.value);
|
|
1382
1234
|
}
|
|
1383
1235
|
} catch (err) {
|
|
@@ -1385,309 +1237,290 @@
|
|
|
1385
1237
|
throw err;
|
|
1386
1238
|
}
|
|
1387
1239
|
}
|
|
1388
|
-
}
|
|
1389
|
-
|
|
1390
|
-
|
|
1391
|
-
|
|
1392
|
-
|
|
1393
|
-
if (_ret) return _ret.v;
|
|
1240
|
+
}
|
|
1241
|
+
return {
|
|
1242
|
+
type,
|
|
1243
|
+
value: keys
|
|
1244
|
+
};
|
|
1394
1245
|
}
|
|
1395
|
-
|
|
1396
|
-
|
|
1397
|
-
|
|
1398
|
-
|
|
1399
|
-
|
|
1400
|
-
|
|
1401
|
-
|
|
1402
|
-
|
|
1403
|
-
|
|
1404
|
-
|
|
1405
|
-
|
|
1406
|
-
|
|
1407
|
-
|
|
1408
|
-
type
|
|
1409
|
-
|
|
1246
|
+
case 'date':
|
|
1247
|
+
{
|
|
1248
|
+
const date = /** @type {Date} */input;
|
|
1249
|
+
if (!Number.isNaN(date.getTime())) {
|
|
1250
|
+
return fullKeys ? {
|
|
1251
|
+
type,
|
|
1252
|
+
value: date.getTime()
|
|
1253
|
+
} : {
|
|
1254
|
+
type,
|
|
1255
|
+
value: new Date(date)
|
|
1256
|
+
};
|
|
1257
|
+
}
|
|
1258
|
+
return {
|
|
1259
|
+
type,
|
|
1260
|
+
invalid: true,
|
|
1261
|
+
message: 'Not a valid date'
|
|
1410
1262
|
};
|
|
1263
|
+
// Falls through
|
|
1411
1264
|
}
|
|
1412
|
-
|
|
1413
|
-
|
|
1414
|
-
|
|
1415
|
-
|
|
1416
|
-
|
|
1417
|
-
|
|
1418
|
-
|
|
1419
|
-
|
|
1420
|
-
|
|
1421
|
-
|
|
1422
|
-
|
|
1423
|
-
|
|
1424
|
-
|
|
1425
|
-
|
|
1426
|
-
|
|
1427
|
-
|
|
1428
|
-
|
|
1429
|
-
|
|
1430
|
-
|
|
1265
|
+
case 'invalid':
|
|
1266
|
+
default:
|
|
1267
|
+
{
|
|
1268
|
+
// Other `typeof` types which are not valid keys:
|
|
1269
|
+
// 'undefined', 'boolean', 'object' (including `null`), 'symbol', 'function'
|
|
1270
|
+
const type = input === null ? 'null' : typeof input; // Convert `null` for convenience of consumers in reporting errors
|
|
1271
|
+
return {
|
|
1272
|
+
type,
|
|
1273
|
+
invalid: true,
|
|
1274
|
+
message: 'Not a valid key; type ' + type
|
|
1275
|
+
};
|
|
1276
|
+
}
|
|
1277
|
+
}
|
|
1278
|
+
}
|
|
1279
|
+
|
|
1280
|
+
/**
|
|
1281
|
+
*
|
|
1282
|
+
* @param {Key} key
|
|
1283
|
+
* @param {boolean} [fullKeys]
|
|
1284
|
+
* @returns {KeyValueObject}
|
|
1285
|
+
* @todo Document other allowable `key`?
|
|
1286
|
+
*/
|
|
1287
|
+
function convertValueToMultiEntryKeyDecoded(key, fullKeys) {
|
|
1288
|
+
return convertValueToKeyValueDecoded(key, null, true, fullKeys);
|
|
1289
|
+
}
|
|
1290
|
+
|
|
1291
|
+
/**
|
|
1292
|
+
* An internal utility.
|
|
1293
|
+
* @param {Value} input
|
|
1294
|
+
* @param {Value[]|null|undefined} [seen]
|
|
1295
|
+
* @throws {DOMException} `DataError`
|
|
1296
|
+
* @returns {KeyValueObject}
|
|
1297
|
+
*/
|
|
1298
|
+
function convertValueToKeyRethrowingAndIfInvalid(input, seen) {
|
|
1299
|
+
const key = convertValueToKey(input, seen);
|
|
1300
|
+
if (key.invalid) {
|
|
1301
|
+
throw createDOMException('DataError', key.message || 'Not a valid key; type: ' + key.type);
|
|
1302
|
+
}
|
|
1303
|
+
return key;
|
|
1431
1304
|
}
|
|
1432
|
-
|
|
1433
|
-
|
|
1434
|
-
|
|
1435
|
-
|
|
1436
|
-
|
|
1437
|
-
|
|
1438
|
-
|
|
1439
|
-
|
|
1440
|
-
|
|
1441
|
-
|
|
1442
|
-
|
|
1443
|
-
}
|
|
1444
|
-
|
|
1445
|
-
/**
|
|
1446
|
-
* An internal utility.
|
|
1447
|
-
* @param {Value} input
|
|
1448
|
-
* @param {Value[]|null|undefined} [seen]
|
|
1449
|
-
* @throws {DOMException} `DataError`
|
|
1450
|
-
* @returns {KeyValueObject}
|
|
1451
|
-
*/
|
|
1452
|
-
function convertValueToKeyRethrowingAndIfInvalid(input, seen) {
|
|
1453
|
-
var key = convertValueToKey(input, seen);
|
|
1454
|
-
if (key.invalid) {
|
|
1455
|
-
throw createDOMException('DataError', key.message || 'Not a valid key; type: ' + key.type);
|
|
1305
|
+
|
|
1306
|
+
/**
|
|
1307
|
+
*
|
|
1308
|
+
* @param {Value} value
|
|
1309
|
+
* @param {KeyPath} keyPath
|
|
1310
|
+
* @param {boolean} multiEntry
|
|
1311
|
+
* @returns {KeyValueObject|KeyPathEvaluateValue}
|
|
1312
|
+
* @todo Document other possible return?
|
|
1313
|
+
*/
|
|
1314
|
+
function extractKeyFromValueUsingKeyPath(value, keyPath, multiEntry) {
|
|
1315
|
+
return extractKeyValueDecodedFromValueUsingKeyPath(value, keyPath, multiEntry, true);
|
|
1456
1316
|
}
|
|
1457
|
-
|
|
1458
|
-
|
|
1459
|
-
|
|
1460
|
-
|
|
1461
|
-
|
|
1462
|
-
|
|
1463
|
-
|
|
1464
|
-
|
|
1465
|
-
|
|
1466
|
-
* @todo Document other possible return?
|
|
1467
|
-
*/
|
|
1468
|
-
function extractKeyFromValueUsingKeyPath(value, keyPath, multiEntry) {
|
|
1469
|
-
return extractKeyValueDecodedFromValueUsingKeyPath(value, keyPath, multiEntry, true);
|
|
1470
|
-
}
|
|
1471
|
-
/**
|
|
1472
|
-
* Not currently in use.
|
|
1473
|
-
* @param {Value} value
|
|
1474
|
-
* @param {KeyPath} keyPath
|
|
1475
|
-
* @param {boolean} multiEntry
|
|
1476
|
-
* @returns {KeyPathEvaluateValue}
|
|
1477
|
-
*/
|
|
1478
|
-
function evaluateKeyPathOnValue(value, keyPath, multiEntry) {
|
|
1479
|
-
return evaluateKeyPathOnValueToDecodedValue(value, keyPath);
|
|
1480
|
-
}
|
|
1481
|
-
|
|
1482
|
-
/**
|
|
1483
|
-
* May throw, return `{failure: true}` (e.g., non-object on keyPath resolution)
|
|
1484
|
-
* or `{invalid: true}` (e.g., `NaN`).
|
|
1485
|
-
* @param {Value} value
|
|
1486
|
-
* @param {KeyPath} keyPath
|
|
1487
|
-
* @param {boolean} [multiEntry]
|
|
1488
|
-
* @param {boolean} [fullKeys]
|
|
1489
|
-
* @returns {KeyValueObject|KeyPathEvaluateValue}
|
|
1490
|
-
* @todo Document other possible return?
|
|
1491
|
-
*/
|
|
1492
|
-
function extractKeyValueDecodedFromValueUsingKeyPath(value, keyPath, multiEntry, fullKeys) {
|
|
1493
|
-
var r = evaluateKeyPathOnValueToDecodedValue(value, keyPath);
|
|
1494
|
-
if (r.failure) {
|
|
1495
|
-
return r;
|
|
1317
|
+
/**
|
|
1318
|
+
* Not currently in use.
|
|
1319
|
+
* @param {Value} value
|
|
1320
|
+
* @param {KeyPath} keyPath
|
|
1321
|
+
* @param {boolean} multiEntry
|
|
1322
|
+
* @returns {KeyPathEvaluateValue}
|
|
1323
|
+
*/
|
|
1324
|
+
function evaluateKeyPathOnValue(value, keyPath, multiEntry) {
|
|
1325
|
+
return evaluateKeyPathOnValueToDecodedValue(value, keyPath);
|
|
1496
1326
|
}
|
|
1497
|
-
|
|
1498
|
-
|
|
1327
|
+
|
|
1328
|
+
/**
|
|
1329
|
+
* May throw, return `{failure: true}` (e.g., non-object on keyPath resolution)
|
|
1330
|
+
* or `{invalid: true}` (e.g., `NaN`).
|
|
1331
|
+
* @param {Value} value
|
|
1332
|
+
* @param {KeyPath} keyPath
|
|
1333
|
+
* @param {boolean} [multiEntry]
|
|
1334
|
+
* @param {boolean} [fullKeys]
|
|
1335
|
+
* @returns {KeyValueObject|KeyPathEvaluateValue}
|
|
1336
|
+
* @todo Document other possible return?
|
|
1337
|
+
*/
|
|
1338
|
+
function extractKeyValueDecodedFromValueUsingKeyPath(value, keyPath, multiEntry, fullKeys) {
|
|
1339
|
+
const r = evaluateKeyPathOnValueToDecodedValue(value, keyPath);
|
|
1340
|
+
if (r.failure) {
|
|
1341
|
+
return r;
|
|
1342
|
+
}
|
|
1343
|
+
if (!multiEntry) {
|
|
1344
|
+
return convertValueToKeyValueDecoded(r.value, null, false, fullKeys);
|
|
1345
|
+
}
|
|
1346
|
+
return convertValueToMultiEntryKeyDecoded(r.value, fullKeys);
|
|
1499
1347
|
}
|
|
1500
|
-
|
|
1501
|
-
|
|
1502
|
-
|
|
1503
|
-
|
|
1504
|
-
|
|
1505
|
-
|
|
1506
|
-
|
|
1507
|
-
|
|
1508
|
-
|
|
1509
|
-
|
|
1510
|
-
|
|
1511
|
-
|
|
1512
|
-
|
|
1513
|
-
|
|
1514
|
-
|
|
1515
|
-
|
|
1516
|
-
|
|
1517
|
-
|
|
1518
|
-
|
|
1519
|
-
|
|
1520
|
-
|
|
1521
|
-
|
|
1522
|
-
|
|
1523
|
-
|
|
1524
|
-
|
|
1525
|
-
|
|
1526
|
-
|
|
1527
|
-
|
|
1528
|
-
|
|
1529
|
-
|
|
1530
|
-
|
|
1531
|
-
|
|
1532
|
-
|
|
1533
|
-
|
|
1534
|
-
|
|
1535
|
-
|
|
1536
|
-
|
|
1537
|
-
|
|
1538
|
-
|
|
1348
|
+
|
|
1349
|
+
/**
|
|
1350
|
+
* Unused?
|
|
1351
|
+
* @typedef {object} KeyPathEvaluateFailure
|
|
1352
|
+
* @property {boolean} failure
|
|
1353
|
+
*/
|
|
1354
|
+
|
|
1355
|
+
/**
|
|
1356
|
+
* @typedef {KeyPathEvaluateValueValue[]} KeyPathEvaluateValueValueArray
|
|
1357
|
+
*/
|
|
1358
|
+
|
|
1359
|
+
/**
|
|
1360
|
+
* @typedef {undefined|number|string|Date|object|KeyPathEvaluateValueValueArray} KeyPathEvaluateValueValue
|
|
1361
|
+
*/
|
|
1362
|
+
|
|
1363
|
+
/**
|
|
1364
|
+
* @typedef {object} KeyPathEvaluateValue
|
|
1365
|
+
* @property {KeyPathEvaluateValueValue} [value]
|
|
1366
|
+
* @property {boolean} [failure]
|
|
1367
|
+
*/
|
|
1368
|
+
|
|
1369
|
+
/**
|
|
1370
|
+
* Returns the value of an inline key based on a key path (wrapped in an
|
|
1371
|
+
* object with key `value`) or `{failure: true}`
|
|
1372
|
+
* @param {Value} value
|
|
1373
|
+
* @param {KeyPath} keyPath
|
|
1374
|
+
* @param {boolean} [multiEntry]
|
|
1375
|
+
* @param {boolean} [fullKeys]
|
|
1376
|
+
* @returns {KeyPathEvaluateValue}
|
|
1377
|
+
*/
|
|
1378
|
+
function evaluateKeyPathOnValueToDecodedValue(value, keyPath, multiEntry, fullKeys) {
|
|
1379
|
+
if (Array.isArray(keyPath)) {
|
|
1380
|
+
/** @type {KeyPathEvaluateValueValueArray} */
|
|
1381
|
+
const result = [];
|
|
1382
|
+
return keyPath.some(item => {
|
|
1383
|
+
const key = evaluateKeyPathOnValueToDecodedValue(value, item);
|
|
1384
|
+
if (key.failure) {
|
|
1385
|
+
return true;
|
|
1386
|
+
}
|
|
1387
|
+
result.push(key.value);
|
|
1388
|
+
return false;
|
|
1389
|
+
}) ? {
|
|
1390
|
+
failure: true
|
|
1391
|
+
} : {
|
|
1392
|
+
value: result
|
|
1393
|
+
};
|
|
1394
|
+
}
|
|
1395
|
+
if (keyPath === '') {
|
|
1396
|
+
return {
|
|
1397
|
+
value
|
|
1398
|
+
};
|
|
1399
|
+
}
|
|
1400
|
+
const identifiers = keyPath.split('.');
|
|
1401
|
+
return identifiers.some(idntfr => {
|
|
1402
|
+
if (idntfr === 'length' && (typeof value === 'string' || Array.isArray(value))) {
|
|
1403
|
+
value = value.length;
|
|
1404
|
+
} else if (isBlob(value)) {
|
|
1405
|
+
switch (idntfr) {
|
|
1406
|
+
case 'size':
|
|
1407
|
+
case 'type':
|
|
1408
|
+
value = /** @type {Blob} */value[idntfr];
|
|
1409
|
+
break;
|
|
1410
|
+
}
|
|
1411
|
+
} else if (isFile(value)) {
|
|
1412
|
+
switch (idntfr) {
|
|
1413
|
+
case 'name':
|
|
1414
|
+
case 'lastModified':
|
|
1415
|
+
value = /** @type {File} */value[idntfr];
|
|
1416
|
+
break;
|
|
1417
|
+
case 'lastModifiedDate':
|
|
1418
|
+
value = new Date(/** @type {File} */value.lastModified);
|
|
1419
|
+
break;
|
|
1420
|
+
}
|
|
1421
|
+
} else if (!isObj(value) || !Object.hasOwn(value, idntfr)) {
|
|
1539
1422
|
return true;
|
|
1423
|
+
} else {
|
|
1424
|
+
value = /** @type {{[key: string]: KeyPathEvaluateValueValue}} */value[idntfr];
|
|
1425
|
+
return value === undefined;
|
|
1540
1426
|
}
|
|
1541
|
-
result.push(key.value);
|
|
1542
1427
|
return false;
|
|
1543
1428
|
}) ? {
|
|
1544
1429
|
failure: true
|
|
1545
1430
|
} : {
|
|
1546
|
-
value
|
|
1547
|
-
};
|
|
1548
|
-
}
|
|
1549
|
-
if (keyPath === '') {
|
|
1550
|
-
return {
|
|
1551
|
-
value: value
|
|
1431
|
+
value
|
|
1552
1432
|
};
|
|
1553
1433
|
}
|
|
1554
|
-
|
|
1555
|
-
|
|
1556
|
-
|
|
1557
|
-
|
|
1558
|
-
|
|
1559
|
-
|
|
1560
|
-
|
|
1561
|
-
|
|
1562
|
-
|
|
1563
|
-
|
|
1564
|
-
|
|
1565
|
-
|
|
1566
|
-
|
|
1567
|
-
|
|
1568
|
-
|
|
1569
|
-
value = /** @type {File} */value[idntfr];
|
|
1570
|
-
break;
|
|
1571
|
-
case 'lastModifiedDate':
|
|
1572
|
-
value = new Date(/** @type {File} */value.lastModified);
|
|
1573
|
-
break;
|
|
1434
|
+
|
|
1435
|
+
/**
|
|
1436
|
+
* Sets the inline key value.
|
|
1437
|
+
* @param {{[key: string]: AnyValue}} value
|
|
1438
|
+
* @param {Key} key
|
|
1439
|
+
* @param {string} keyPath
|
|
1440
|
+
* @returns {void}
|
|
1441
|
+
*/
|
|
1442
|
+
function injectKeyIntoValueUsingKeyPath(value, key, keyPath) {
|
|
1443
|
+
const identifiers = keyPath.split('.');
|
|
1444
|
+
const last = identifiers.pop();
|
|
1445
|
+
identifiers.forEach(identifier => {
|
|
1446
|
+
const hop = Object.hasOwn(value, identifier);
|
|
1447
|
+
if (!hop) {
|
|
1448
|
+
value[identifier] = {};
|
|
1574
1449
|
}
|
|
1575
|
-
|
|
1576
|
-
|
|
1577
|
-
}
|
|
1578
|
-
|
|
1579
|
-
|
|
1580
|
-
|
|
1581
|
-
|
|
1582
|
-
|
|
1583
|
-
|
|
1584
|
-
|
|
1585
|
-
|
|
1586
|
-
|
|
1587
|
-
|
|
1588
|
-
|
|
1589
|
-
|
|
1590
|
-
|
|
1591
|
-
* @param {{[key: string]: AnyValue}} value
|
|
1592
|
-
* @param {Key} key
|
|
1593
|
-
* @param {string} keyPath
|
|
1594
|
-
* @returns {void}
|
|
1595
|
-
*/
|
|
1596
|
-
function injectKeyIntoValueUsingKeyPath(value, key, keyPath) {
|
|
1597
|
-
var identifiers = keyPath.split('.');
|
|
1598
|
-
var last = identifiers.pop();
|
|
1599
|
-
identifiers.forEach(function (identifier) {
|
|
1600
|
-
var hop = Object.hasOwn(value, identifier);
|
|
1601
|
-
if (!hop) {
|
|
1602
|
-
value[identifier] = {};
|
|
1603
|
-
}
|
|
1604
|
-
value = value[identifier];
|
|
1605
|
-
});
|
|
1606
|
-
value[(/** @type {string} */last)] = key; // key is already a `keyValue` in our processing so no need to convert
|
|
1607
|
-
}
|
|
1608
|
-
|
|
1609
|
-
/**
|
|
1610
|
-
*
|
|
1611
|
-
* @param {Value} value
|
|
1612
|
-
* @param {string} keyPath
|
|
1613
|
-
* @see https://github.com/w3c/IndexedDB/pull/146
|
|
1614
|
-
* @returns {boolean}
|
|
1615
|
-
*/
|
|
1616
|
-
function checkKeyCouldBeInjectedIntoValue(value, keyPath) {
|
|
1617
|
-
var identifiers = keyPath.split('.');
|
|
1618
|
-
identifiers.pop();
|
|
1619
|
-
var _iterator3 = _createForOfIteratorHelper(identifiers),
|
|
1620
|
-
_step3;
|
|
1621
|
-
try {
|
|
1622
|
-
for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
|
|
1623
|
-
var identifier = _step3.value;
|
|
1450
|
+
value = value[identifier];
|
|
1451
|
+
});
|
|
1452
|
+
value[(/** @type {string} */last)] = key; // key is already a `keyValue` in our processing so no need to convert
|
|
1453
|
+
}
|
|
1454
|
+
|
|
1455
|
+
/**
|
|
1456
|
+
*
|
|
1457
|
+
* @param {Value} value
|
|
1458
|
+
* @param {string} keyPath
|
|
1459
|
+
* @see https://github.com/w3c/IndexedDB/pull/146
|
|
1460
|
+
* @returns {boolean}
|
|
1461
|
+
*/
|
|
1462
|
+
function checkKeyCouldBeInjectedIntoValue(value, keyPath) {
|
|
1463
|
+
const identifiers = keyPath.split('.');
|
|
1464
|
+
identifiers.pop();
|
|
1465
|
+
for (const identifier of identifiers) {
|
|
1624
1466
|
if (!isObj(value)) {
|
|
1625
1467
|
return false;
|
|
1626
1468
|
}
|
|
1627
|
-
|
|
1469
|
+
const hop = Object.hasOwn(value, identifier);
|
|
1628
1470
|
if (!hop) {
|
|
1629
1471
|
return true;
|
|
1630
1472
|
}
|
|
1631
1473
|
value = /** @type {{[key: string]: Value}} */value[identifier];
|
|
1632
1474
|
}
|
|
1633
|
-
|
|
1634
|
-
_iterator3.e(err);
|
|
1635
|
-
} finally {
|
|
1636
|
-
_iterator3.f();
|
|
1475
|
+
return isObj(value);
|
|
1637
1476
|
}
|
|
1638
|
-
|
|
1639
|
-
|
|
1640
|
-
|
|
1641
|
-
|
|
1642
|
-
|
|
1643
|
-
|
|
1644
|
-
|
|
1645
|
-
|
|
1646
|
-
|
|
1647
|
-
|
|
1648
|
-
|
|
1649
|
-
|
|
1650
|
-
|
|
1651
|
-
|
|
1652
|
-
|
|
1653
|
-
|
|
1654
|
-
|
|
1655
|
-
|
|
1656
|
-
|
|
1657
|
-
|
|
1658
|
-
|
|
1477
|
+
|
|
1478
|
+
/**
|
|
1479
|
+
*
|
|
1480
|
+
* @param {Key} key
|
|
1481
|
+
* @param {import('./IDBKeyRange.js').IDBKeyRangeFull} range
|
|
1482
|
+
* @param {boolean} [checkCached]
|
|
1483
|
+
* @returns {boolean}
|
|
1484
|
+
*/
|
|
1485
|
+
function isKeyInRange(key, range, checkCached) {
|
|
1486
|
+
let lowerMatch = range.lower === undefined;
|
|
1487
|
+
let upperMatch = range.upper === undefined;
|
|
1488
|
+
const encodedKey = encode(key, true);
|
|
1489
|
+
const lower = checkCached ? range.__lowerCached : encode(range.lower, true);
|
|
1490
|
+
const upper = checkCached ? range.__upperCached : encode(range.upper, true);
|
|
1491
|
+
if (!lowerMatch && (range.lowerOpen && encodedKey !== null && lower !== null && encodedKey > lower || !range.lowerOpen && (!encodedKey && !lower || encodedKey !== null && lower !== null && encodedKey >= lower))) {
|
|
1492
|
+
lowerMatch = true;
|
|
1493
|
+
}
|
|
1494
|
+
if (!upperMatch && (range.upperOpen && encodedKey !== null && upper !== null && encodedKey < upper || !range.upperOpen && (!encodedKey && !upper || encodedKey !== null && upper !== null && encodedKey <= upper))) {
|
|
1495
|
+
upperMatch = true;
|
|
1496
|
+
}
|
|
1497
|
+
return lowerMatch && upperMatch;
|
|
1659
1498
|
}
|
|
1660
|
-
|
|
1661
|
-
|
|
1662
|
-
|
|
1663
|
-
|
|
1664
|
-
|
|
1665
|
-
|
|
1666
|
-
|
|
1667
|
-
|
|
1668
|
-
|
|
1669
|
-
|
|
1670
|
-
|
|
1671
|
-
|
|
1672
|
-
return encodedKey
|
|
1499
|
+
|
|
1500
|
+
/**
|
|
1501
|
+
* Determines whether an index entry matches a multi-entry key value.
|
|
1502
|
+
* @param {string} encodedEntry The entry value (already encoded)
|
|
1503
|
+
* @param {string} encodedKey The full index key (already encoded)
|
|
1504
|
+
* @returns {boolean}
|
|
1505
|
+
*/
|
|
1506
|
+
function isMultiEntryMatch(encodedEntry, encodedKey) {
|
|
1507
|
+
const keyType = encodedCharToKeyType[encodedKey.slice(0, 1)];
|
|
1508
|
+
if (keyType === 'array') {
|
|
1509
|
+
return encodedKey.indexOf(encodedEntry) > 1;
|
|
1510
|
+
}
|
|
1511
|
+
return encodedKey === encodedEntry;
|
|
1673
1512
|
}
|
|
1674
|
-
|
|
1675
|
-
|
|
1676
|
-
|
|
1677
|
-
|
|
1678
|
-
|
|
1679
|
-
|
|
1680
|
-
|
|
1681
|
-
|
|
1682
|
-
|
|
1683
|
-
|
|
1684
|
-
|
|
1685
|
-
if (Array.isArray(keyEntry)) {
|
|
1686
|
-
var _iterator4 = _createForOfIteratorHelper(keyEntry),
|
|
1687
|
-
_step4;
|
|
1688
|
-
try {
|
|
1689
|
-
for (_iterator4.s(); !(_step4 = _iterator4.n()).done;) {
|
|
1690
|
-
var key = _step4.value;
|
|
1513
|
+
|
|
1514
|
+
/**
|
|
1515
|
+
*
|
|
1516
|
+
* @param {Key} keyEntry
|
|
1517
|
+
* @param {import('./IDBKeyRange.js').IDBKeyRangeFull|undefined} range
|
|
1518
|
+
* @returns {Key[]}
|
|
1519
|
+
*/
|
|
1520
|
+
function findMultiEntryMatches(keyEntry, range) {
|
|
1521
|
+
const matches = [];
|
|
1522
|
+
if (Array.isArray(keyEntry)) {
|
|
1523
|
+
for (let key of keyEntry) {
|
|
1691
1524
|
if (Array.isArray(key)) {
|
|
1692
1525
|
if (range && range.lower === range.upper) {
|
|
1693
1526
|
continue;
|
|
@@ -1696,7 +1529,7 @@
|
|
|
1696
1529
|
// eslint-disable-next-line sonarjs/updated-loop-counter -- Convenient
|
|
1697
1530
|
key = key[0];
|
|
1698
1531
|
} else {
|
|
1699
|
-
|
|
1532
|
+
const nested = findMultiEntryMatches(key, range);
|
|
1700
1533
|
if (nested.length > 0) {
|
|
1701
1534
|
matches.push(key);
|
|
1702
1535
|
}
|
|
@@ -1707,273 +1540,270 @@
|
|
|
1707
1540
|
matches.push(key);
|
|
1708
1541
|
}
|
|
1709
1542
|
}
|
|
1710
|
-
}
|
|
1711
|
-
|
|
1712
|
-
} finally {
|
|
1713
|
-
_iterator4.f();
|
|
1543
|
+
} else if (isNullish(range) || isKeyInRange(keyEntry, range, true)) {
|
|
1544
|
+
matches.push(keyEntry);
|
|
1714
1545
|
}
|
|
1715
|
-
|
|
1716
|
-
matches.push(keyEntry);
|
|
1546
|
+
return matches;
|
|
1717
1547
|
}
|
|
1718
|
-
|
|
1719
|
-
|
|
1720
|
-
|
|
1721
|
-
|
|
1722
|
-
|
|
1723
|
-
|
|
1724
|
-
|
|
1725
|
-
|
|
1726
|
-
|
|
1727
|
-
|
|
1728
|
-
|
|
1729
|
-
|
|
1730
|
-
|
|
1731
|
-
|
|
1732
|
-
|
|
1733
|
-
|
|
1734
|
-
|
|
1735
|
-
}
|
|
1736
|
-
case 'array':
|
|
1737
|
-
{
|
|
1738
|
-
var array = [];
|
|
1739
|
-
var len = value.length;
|
|
1740
|
-
var index = 0;
|
|
1741
|
-
while (index < len) {
|
|
1742
|
-
var entry = convertKeyToValue(value[index]);
|
|
1743
|
-
array[index] = entry;
|
|
1744
|
-
index++;
|
|
1548
|
+
|
|
1549
|
+
/**
|
|
1550
|
+
* Not currently in use but keeping for spec parity.
|
|
1551
|
+
* @param {Key} key
|
|
1552
|
+
* @throws {Error} Upon a "bad key"
|
|
1553
|
+
* @returns {ValueType}
|
|
1554
|
+
*/
|
|
1555
|
+
function convertKeyToValue(key) {
|
|
1556
|
+
const {
|
|
1557
|
+
type,
|
|
1558
|
+
value
|
|
1559
|
+
} = key;
|
|
1560
|
+
switch (type) {
|
|
1561
|
+
case 'number':
|
|
1562
|
+
case 'string':
|
|
1563
|
+
{
|
|
1564
|
+
return value;
|
|
1745
1565
|
}
|
|
1746
|
-
|
|
1747
|
-
|
|
1748
|
-
|
|
1749
|
-
|
|
1750
|
-
|
|
1751
|
-
|
|
1752
|
-
|
|
1753
|
-
|
|
1754
|
-
|
|
1755
|
-
|
|
1756
|
-
|
|
1757
|
-
|
|
1758
|
-
|
|
1759
|
-
|
|
1760
|
-
|
|
1761
|
-
|
|
1762
|
-
|
|
1763
|
-
|
|
1764
|
-
|
|
1765
|
-
|
|
1766
|
-
|
|
1767
|
-
|
|
1768
|
-
|
|
1769
|
-
|
|
1770
|
-
|
|
1771
|
-
|
|
1772
|
-
|
|
1773
|
-
|
|
1774
|
-
|
|
1775
|
-
if (key === undefined) {
|
|
1776
|
-
return null;
|
|
1777
|
-
}
|
|
1778
|
-
// array, date, number, string, binary (should already have detected "invalid")
|
|
1779
|
-
return types[getKeyType(key)].encode(key, inArray);
|
|
1780
|
-
}
|
|
1781
|
-
|
|
1782
|
-
/**
|
|
1783
|
-
*
|
|
1784
|
-
* @param {Key} key
|
|
1785
|
-
* @param {boolean} [inArray]
|
|
1786
|
-
* @throws {Error} Invalid number
|
|
1787
|
-
* @returns {undefined|ValueType}
|
|
1788
|
-
*/
|
|
1789
|
-
function _decode(key, inArray) {
|
|
1790
|
-
if (typeof key !== 'string') {
|
|
1791
|
-
return undefined;
|
|
1566
|
+
case 'array':
|
|
1567
|
+
{
|
|
1568
|
+
const array = [];
|
|
1569
|
+
const len = value.length;
|
|
1570
|
+
let index = 0;
|
|
1571
|
+
while (index < len) {
|
|
1572
|
+
const entry = convertKeyToValue(value[index]);
|
|
1573
|
+
array[index] = entry;
|
|
1574
|
+
index++;
|
|
1575
|
+
}
|
|
1576
|
+
return array;
|
|
1577
|
+
}
|
|
1578
|
+
case 'date':
|
|
1579
|
+
{
|
|
1580
|
+
return new Date(value);
|
|
1581
|
+
}
|
|
1582
|
+
case 'binary':
|
|
1583
|
+
{
|
|
1584
|
+
const len = value.length;
|
|
1585
|
+
const buffer = new ArrayBuffer(len);
|
|
1586
|
+
// Set the entries in buffer's [[ArrayBufferData]] to those in `value`
|
|
1587
|
+
const uint8 = new Uint8Array(buffer, value.byteOffset || 0, value.byteLength);
|
|
1588
|
+
uint8.set(value);
|
|
1589
|
+
return buffer;
|
|
1590
|
+
}
|
|
1591
|
+
case 'invalid':
|
|
1592
|
+
default:
|
|
1593
|
+
throw new Error('Bad key');
|
|
1594
|
+
}
|
|
1792
1595
|
}
|
|
1793
|
-
|
|
1794
|
-
|
|
1795
|
-
|
|
1796
|
-
|
|
1797
|
-
|
|
1798
|
-
|
|
1799
|
-
|
|
1800
|
-
|
|
1801
|
-
|
|
1802
|
-
|
|
1803
|
-
|
|
1804
|
-
}
|
|
1805
|
-
var MAX_ALLOWED_CURRENT_NUMBER = 9007199254740992; // 2 ^ 53 (Also equal to `Number.MAX_SAFE_INTEGER + 1`)
|
|
1806
|
-
|
|
1807
|
-
/**
|
|
1808
|
-
* @typedef {number} Integer
|
|
1809
|
-
*/
|
|
1810
|
-
|
|
1811
|
-
/**
|
|
1812
|
-
* @callback CurrentNumberCallback
|
|
1813
|
-
* @param {Integer} cn The current number
|
|
1814
|
-
* @returns {void}
|
|
1815
|
-
*/
|
|
1816
|
-
|
|
1817
|
-
/**
|
|
1818
|
-
* @callback SQLFailureCallback
|
|
1819
|
-
* @param {DOMException|Error} exception
|
|
1820
|
-
* @returns {void}
|
|
1821
|
-
*/
|
|
1822
|
-
|
|
1823
|
-
/**
|
|
1824
|
-
*
|
|
1825
|
-
* @param {SQLTransaction} tx
|
|
1826
|
-
* @param {import('./IDBObjectStore.js').IDBObjectStoreFull} store
|
|
1827
|
-
* @param {CurrentNumberCallback} func
|
|
1828
|
-
* @param {SQLFailureCallback} sqlFailCb
|
|
1829
|
-
* @returns {void}
|
|
1830
|
-
*/
|
|
1831
|
-
function getCurrentNumber(tx, store, func, sqlFailCb) {
|
|
1832
|
-
tx.executeSql('SELECT "currNum" FROM __sys__ WHERE "name" = ?', [escapeSQLiteStatement(store.__currentName)], function (tx, data) {
|
|
1833
|
-
if (data.rows.length !== 1) {
|
|
1834
|
-
func(1);
|
|
1835
|
-
} else {
|
|
1836
|
-
func(data.rows.item(0).currNum);
|
|
1596
|
+
|
|
1597
|
+
/**
|
|
1598
|
+
*
|
|
1599
|
+
* @param {Key} key
|
|
1600
|
+
* @param {boolean} [inArray]
|
|
1601
|
+
* @returns {string|null}
|
|
1602
|
+
*/
|
|
1603
|
+
function encode(key, inArray) {
|
|
1604
|
+
// Bad keys like `null`, `object`, `boolean`, 'function', 'symbol' should not be passed here due to prior validation
|
|
1605
|
+
if (key === undefined) {
|
|
1606
|
+
return null;
|
|
1837
1607
|
}
|
|
1838
|
-
|
|
1839
|
-
|
|
1840
|
-
return false;
|
|
1841
|
-
});
|
|
1842
|
-
}
|
|
1843
|
-
|
|
1844
|
-
/**
|
|
1845
|
-
*
|
|
1846
|
-
* @param {SQLTransaction} tx
|
|
1847
|
-
* @param {import('./IDBObjectStore.js').IDBObjectStoreFull} store
|
|
1848
|
-
* @param {Integer} num
|
|
1849
|
-
* @param {CurrentNumberCallback} successCb
|
|
1850
|
-
* @param {SQLFailureCallback} failCb
|
|
1851
|
-
* @returns {void}
|
|
1852
|
-
*/
|
|
1853
|
-
function assignCurrentNumber(tx, store, num, successCb, failCb) {
|
|
1854
|
-
var sql = 'UPDATE __sys__ SET "currNum" = ? WHERE "name" = ?';
|
|
1855
|
-
var sqlValues = [num, escapeSQLiteStatement(store.__currentName)];
|
|
1856
|
-
if (CFG.DEBUG) {
|
|
1857
|
-
console.log(sql, sqlValues);
|
|
1608
|
+
// array, date, number, string, binary (should already have detected "invalid")
|
|
1609
|
+
return types[getKeyType(key)].encode(key, inArray);
|
|
1858
1610
|
}
|
|
1859
|
-
|
|
1860
|
-
|
|
1861
|
-
|
|
1862
|
-
|
|
1863
|
-
|
|
1864
|
-
|
|
1865
|
-
|
|
1866
|
-
|
|
1867
|
-
|
|
1868
|
-
|
|
1869
|
-
|
|
1870
|
-
* valid (numeric and >= 1) and >= any primaryKey.
|
|
1871
|
-
* @param {SQLTransaction} tx
|
|
1872
|
-
* @param {import('./IDBObjectStore.js').IDBObjectStoreFull} store
|
|
1873
|
-
* @param {Integer} num
|
|
1874
|
-
* @param {CurrentNumberCallback} successCb
|
|
1875
|
-
* @param {SQLFailureCallback} failCb
|
|
1876
|
-
* @returns {void}
|
|
1877
|
-
*/
|
|
1878
|
-
function setCurrentNumber(tx, store, num, successCb, failCb) {
|
|
1879
|
-
num = num === MAX_ALLOWED_CURRENT_NUMBER ? num + 2 // Since incrementing by one will have no effect in JavaScript on this unsafe max, we represent the max as a number incremented by two. The getting of the current number is never returned to the user and is only used in safe comparisons, so it is safe for us to represent it in this manner
|
|
1880
|
-
: num + 1;
|
|
1881
|
-
return assignCurrentNumber(tx, store, num, successCb, failCb);
|
|
1882
|
-
}
|
|
1883
|
-
|
|
1884
|
-
/**
|
|
1885
|
-
* @callback KeyForStoreCallback
|
|
1886
|
-
* @param {"failure"|null} arg1
|
|
1887
|
-
* @param {Integer} [arg2]
|
|
1888
|
-
* @param {Integer} [arg3]
|
|
1889
|
-
* @returns {void}
|
|
1890
|
-
*/
|
|
1891
|
-
|
|
1892
|
-
/**
|
|
1893
|
-
*
|
|
1894
|
-
* @param {SQLTransaction} tx
|
|
1895
|
-
* @param {import('./IDBObjectStore.js').IDBObjectStoreFull} store
|
|
1896
|
-
* @param {KeyForStoreCallback} cb
|
|
1897
|
-
* @param {SQLFailureCallback} sqlFailCb
|
|
1898
|
-
* @returns {void}
|
|
1899
|
-
*/
|
|
1900
|
-
function generateKeyForStore(tx, store, cb, sqlFailCb) {
|
|
1901
|
-
getCurrentNumber(tx, store, function (key) {
|
|
1902
|
-
if (key > MAX_ALLOWED_CURRENT_NUMBER) {
|
|
1903
|
-
// 2 ^ 53 (See <https://github.com/w3c/IndexedDB/issues/147>)
|
|
1904
|
-
cb('failure');
|
|
1905
|
-
return;
|
|
1611
|
+
|
|
1612
|
+
/**
|
|
1613
|
+
*
|
|
1614
|
+
* @param {Key} key
|
|
1615
|
+
* @param {boolean} [inArray]
|
|
1616
|
+
* @throws {Error} Invalid number
|
|
1617
|
+
* @returns {undefined|ValueType}
|
|
1618
|
+
*/
|
|
1619
|
+
function decode(key, inArray) {
|
|
1620
|
+
if (typeof key !== 'string') {
|
|
1621
|
+
return undefined;
|
|
1906
1622
|
}
|
|
1907
|
-
|
|
1908
|
-
|
|
1909
|
-
|
|
1910
|
-
|
|
1911
|
-
|
|
1912
|
-
|
|
1913
|
-
|
|
1914
|
-
|
|
1915
|
-
|
|
1916
|
-
|
|
1917
|
-
|
|
1918
|
-
|
|
1919
|
-
|
|
1920
|
-
|
|
1921
|
-
|
|
1922
|
-
|
|
1923
|
-
|
|
1924
|
-
|
|
1925
|
-
|
|
1926
|
-
|
|
1927
|
-
|
|
1928
|
-
|
|
1929
|
-
|
|
1930
|
-
|
|
1931
|
-
|
|
1932
|
-
|
|
1933
|
-
|
|
1934
|
-
|
|
1935
|
-
|
|
1936
|
-
|
|
1937
|
-
|
|
1938
|
-
|
|
1939
|
-
|
|
1940
|
-
|
|
1941
|
-
|
|
1942
|
-
|
|
1943
|
-
|
|
1944
|
-
|
|
1945
|
-
|
|
1946
|
-
|
|
1947
|
-
|
|
1948
|
-
|
|
1623
|
+
return types[encodedCharToKeyType[key.slice(0, 1)]].decode(key, inArray);
|
|
1624
|
+
}
|
|
1625
|
+
|
|
1626
|
+
/**
|
|
1627
|
+
*
|
|
1628
|
+
* @param {Key} key
|
|
1629
|
+
* @param {boolean} [inArray]
|
|
1630
|
+
* @returns {undefined|ValueType}
|
|
1631
|
+
*/
|
|
1632
|
+
function roundTrip(key, inArray) {
|
|
1633
|
+
return decode(encode(key, inArray), inArray);
|
|
1634
|
+
}
|
|
1635
|
+
const MAX_ALLOWED_CURRENT_NUMBER = 9007199254740992; // 2 ^ 53 (Also equal to `Number.MAX_SAFE_INTEGER + 1`)
|
|
1636
|
+
|
|
1637
|
+
/**
|
|
1638
|
+
* @typedef {number} Integer
|
|
1639
|
+
*/
|
|
1640
|
+
|
|
1641
|
+
/**
|
|
1642
|
+
* @callback CurrentNumberCallback
|
|
1643
|
+
* @param {Integer} cn The current number
|
|
1644
|
+
* @returns {void}
|
|
1645
|
+
*/
|
|
1646
|
+
|
|
1647
|
+
/**
|
|
1648
|
+
* @callback SQLFailureCallback
|
|
1649
|
+
* @param {DOMException|Error} exception
|
|
1650
|
+
* @returns {void}
|
|
1651
|
+
*/
|
|
1652
|
+
|
|
1653
|
+
/**
|
|
1654
|
+
*
|
|
1655
|
+
* @param {SQLTransaction} tx
|
|
1656
|
+
* @param {import('./IDBObjectStore.js').IDBObjectStoreFull} store
|
|
1657
|
+
* @param {CurrentNumberCallback} func
|
|
1658
|
+
* @param {SQLFailureCallback} sqlFailCb
|
|
1659
|
+
* @returns {void}
|
|
1660
|
+
*/
|
|
1661
|
+
function getCurrentNumber(tx, store, func, sqlFailCb) {
|
|
1662
|
+
tx.executeSql('SELECT "currNum" FROM __sys__ WHERE "name" = ?', [escapeSQLiteStatement(store.__currentName)], function (tx, data) {
|
|
1663
|
+
if (data.rows.length !== 1) {
|
|
1664
|
+
func(1);
|
|
1949
1665
|
} else {
|
|
1950
|
-
|
|
1951
|
-
|
|
1666
|
+
func(data.rows.item(0).currNum);
|
|
1667
|
+
}
|
|
1668
|
+
}, function (tx, error) {
|
|
1669
|
+
sqlFailCb(createDOMException('DataError', 'Could not get the auto increment value for key', error));
|
|
1670
|
+
return false;
|
|
1671
|
+
});
|
|
1672
|
+
}
|
|
1673
|
+
|
|
1674
|
+
/**
|
|
1675
|
+
*
|
|
1676
|
+
* @param {SQLTransaction} tx
|
|
1677
|
+
* @param {import('./IDBObjectStore.js').IDBObjectStoreFull} store
|
|
1678
|
+
* @param {Integer} num
|
|
1679
|
+
* @param {CurrentNumberCallback} successCb
|
|
1680
|
+
* @param {SQLFailureCallback} failCb
|
|
1681
|
+
* @returns {void}
|
|
1682
|
+
*/
|
|
1683
|
+
function assignCurrentNumber(tx, store, num, successCb, failCb) {
|
|
1684
|
+
const sql = 'UPDATE __sys__ SET "currNum" = ? WHERE "name" = ?';
|
|
1685
|
+
const sqlValues = [num, escapeSQLiteStatement(store.__currentName)];
|
|
1686
|
+
if (CFG.DEBUG) {
|
|
1687
|
+
console.log(sql, sqlValues);
|
|
1688
|
+
}
|
|
1689
|
+
tx.executeSql(sql, sqlValues, function () {
|
|
1690
|
+
successCb(num);
|
|
1691
|
+
}, function (tx, err) {
|
|
1692
|
+
failCb(createDOMException('UnknownError', 'Could not set the auto increment value for key', err));
|
|
1693
|
+
return false;
|
|
1694
|
+
});
|
|
1695
|
+
}
|
|
1696
|
+
|
|
1697
|
+
/**
|
|
1698
|
+
* Bump up the auto-inc counter if the key path-resolved value is valid
|
|
1699
|
+
* (greater than old value and >=1) OR if a manually passed in key is
|
|
1700
|
+
* valid (numeric and >= 1) and >= any primaryKey.
|
|
1701
|
+
* @param {SQLTransaction} tx
|
|
1702
|
+
* @param {import('./IDBObjectStore.js').IDBObjectStoreFull} store
|
|
1703
|
+
* @param {Integer} num
|
|
1704
|
+
* @param {CurrentNumberCallback} successCb
|
|
1705
|
+
* @param {SQLFailureCallback} failCb
|
|
1706
|
+
* @returns {void}
|
|
1707
|
+
*/
|
|
1708
|
+
function setCurrentNumber(tx, store, num, successCb, failCb) {
|
|
1709
|
+
num = num === MAX_ALLOWED_CURRENT_NUMBER ? num + 2 // Since incrementing by one will have no effect in JavaScript on this unsafe max, we represent the max as a number incremented by two. The getting of the current number is never returned to the user and is only used in safe comparisons, so it is safe for us to represent it in this manner
|
|
1710
|
+
: num + 1;
|
|
1711
|
+
return assignCurrentNumber(tx, store, num, successCb, failCb);
|
|
1712
|
+
}
|
|
1713
|
+
|
|
1714
|
+
/**
|
|
1715
|
+
* @callback KeyForStoreCallback
|
|
1716
|
+
* @param {"failure"|null} arg1
|
|
1717
|
+
* @param {Integer} [arg2]
|
|
1718
|
+
* @param {Integer} [arg3]
|
|
1719
|
+
* @returns {void}
|
|
1720
|
+
*/
|
|
1721
|
+
|
|
1722
|
+
/**
|
|
1723
|
+
*
|
|
1724
|
+
* @param {SQLTransaction} tx
|
|
1725
|
+
* @param {import('./IDBObjectStore.js').IDBObjectStoreFull} store
|
|
1726
|
+
* @param {KeyForStoreCallback} cb
|
|
1727
|
+
* @param {SQLFailureCallback} sqlFailCb
|
|
1728
|
+
* @returns {void}
|
|
1729
|
+
*/
|
|
1730
|
+
function generateKeyForStore(tx, store, cb, sqlFailCb) {
|
|
1731
|
+
getCurrentNumber(tx, store, function (key) {
|
|
1732
|
+
if (key > MAX_ALLOWED_CURRENT_NUMBER) {
|
|
1733
|
+
// 2 ^ 53 (See <https://github.com/w3c/IndexedDB/issues/147>)
|
|
1734
|
+
cb('failure');
|
|
1735
|
+
return;
|
|
1952
1736
|
}
|
|
1737
|
+
// Increment current number by 1 (we cannot leverage SQLite's
|
|
1738
|
+
// autoincrement (and decrement when not needed), as decrementing
|
|
1739
|
+
// will be overwritten/ignored upon the next insert)
|
|
1740
|
+
setCurrentNumber(tx, store, key, function () {
|
|
1741
|
+
cb(null, key, key);
|
|
1742
|
+
}, sqlFailCb);
|
|
1953
1743
|
}, sqlFailCb);
|
|
1954
1744
|
}
|
|
1955
|
-
|
|
1956
|
-
|
|
1957
|
-
|
|
1958
|
-
|
|
1959
|
-
|
|
1960
|
-
|
|
1961
|
-
|
|
1962
|
-
|
|
1963
|
-
|
|
1964
|
-
|
|
1965
|
-
|
|
1966
|
-
|
|
1967
|
-
|
|
1968
|
-
|
|
1969
|
-
|
|
1970
|
-
|
|
1971
|
-
|
|
1972
|
-
|
|
1973
|
-
|
|
1974
|
-
|
|
1975
|
-
|
|
1976
|
-
|
|
1745
|
+
|
|
1746
|
+
// Fractional or numbers exceeding the max do not get changed in the result
|
|
1747
|
+
// per https://github.com/w3c/IndexedDB/issues/147
|
|
1748
|
+
// so we do not return a key
|
|
1749
|
+
/**
|
|
1750
|
+
*
|
|
1751
|
+
* @param {SQLTransaction} tx
|
|
1752
|
+
* @param {import('./IDBObjectStore.js').IDBObjectStoreFull} store
|
|
1753
|
+
* @param {Key} key
|
|
1754
|
+
* @param {(num?: Integer) => void} successCb
|
|
1755
|
+
* @param {SQLFailureCallback} sqlFailCb
|
|
1756
|
+
* @returns {void}
|
|
1757
|
+
*/
|
|
1758
|
+
function possiblyUpdateKeyGenerator(tx, store, key, successCb, sqlFailCb) {
|
|
1759
|
+
// Per https://github.com/w3c/IndexedDB/issues/147 , non-finite numbers
|
|
1760
|
+
// (or numbers larger than the max) are now to have the explicit effect of
|
|
1761
|
+
// setting the current number (up to the max), so we do not optimize them
|
|
1762
|
+
// out here
|
|
1763
|
+
if (typeof key !== 'number' || key < 1) {
|
|
1764
|
+
// Optimize with no need to get the current number
|
|
1765
|
+
// Auto-increment attempted with a bad key;
|
|
1766
|
+
// we are not to change the current number, but the steps don't call for failure
|
|
1767
|
+
// Numbers < 1 are optimized out as they will never be greater than the current number which must be at least 1
|
|
1768
|
+
successCb();
|
|
1769
|
+
} else {
|
|
1770
|
+
// If auto-increment and the keyPath item is a valid numeric key, get the old auto-increment to compare if the new is higher
|
|
1771
|
+
// to determine which to use and whether to update the current number
|
|
1772
|
+
getCurrentNumber(tx, store, function (cn) {
|
|
1773
|
+
const value = Math.floor(Math.min(key, MAX_ALLOWED_CURRENT_NUMBER));
|
|
1774
|
+
const useNewKeyForAutoInc = value >= cn;
|
|
1775
|
+
if (useNewKeyForAutoInc) {
|
|
1776
|
+
setCurrentNumber(tx, store, value, function () {
|
|
1777
|
+
successCb(cn); // Supply old current number in case needs to be reverted
|
|
1778
|
+
}, sqlFailCb);
|
|
1779
|
+
} else {
|
|
1780
|
+
// Not updated
|
|
1781
|
+
successCb();
|
|
1782
|
+
}
|
|
1783
|
+
}, sqlFailCb);
|
|
1784
|
+
}
|
|
1785
|
+
}
|
|
1786
|
+
|
|
1787
|
+
exports.assignCurrentNumber = assignCurrentNumber;
|
|
1788
|
+
exports.checkKeyCouldBeInjectedIntoValue = checkKeyCouldBeInjectedIntoValue;
|
|
1789
|
+
exports.convertKeyToValue = convertKeyToValue;
|
|
1790
|
+
exports.convertValueToKey = convertValueToKey;
|
|
1791
|
+
exports.convertValueToKeyRethrowingAndIfInvalid = convertValueToKeyRethrowingAndIfInvalid;
|
|
1792
|
+
exports.convertValueToKeyValueDecoded = convertValueToKeyValueDecoded;
|
|
1793
|
+
exports.convertValueToMultiEntryKey = convertValueToMultiEntryKey;
|
|
1794
|
+
exports.convertValueToMultiEntryKeyDecoded = convertValueToMultiEntryKeyDecoded;
|
|
1795
|
+
exports.decode = decode;
|
|
1796
|
+
exports.encode = encode;
|
|
1797
|
+
exports.evaluateKeyPathOnValue = evaluateKeyPathOnValue;
|
|
1798
|
+
exports.extractKeyFromValueUsingKeyPath = extractKeyFromValueUsingKeyPath;
|
|
1799
|
+
exports.extractKeyValueDecodedFromValueUsingKeyPath = extractKeyValueDecodedFromValueUsingKeyPath;
|
|
1800
|
+
exports.findMultiEntryMatches = findMultiEntryMatches;
|
|
1801
|
+
exports.generateKeyForStore = generateKeyForStore;
|
|
1802
|
+
exports.injectKeyIntoValueUsingKeyPath = injectKeyIntoValueUsingKeyPath;
|
|
1803
|
+
exports.isKeyInRange = isKeyInRange;
|
|
1804
|
+
exports.isMultiEntryMatch = isMultiEntryMatch;
|
|
1805
|
+
exports.possiblyUpdateKeyGenerator = possiblyUpdateKeyGenerator;
|
|
1806
|
+
exports.roundTrip = roundTrip;
|
|
1977
1807
|
|
|
1978
1808
|
}));
|
|
1979
1809
|
//# sourceMappingURL=indexeddbshim-Key.js.map
|