@geolonia/geonicdb-sdk 0.27.1 → 0.28.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 +17 -0
- package/geonicdb.cjs +423 -137
- package/geonicdb.cjs.map +3 -3
- package/geonicdb.iife.js +423 -137
- package/geonicdb.iife.js.map +3 -3
- package/geonicdb.mjs +423 -137
- package/geonicdb.mjs.map +3 -3
- package/package.json +1 -1
package/geonicdb.cjs
CHANGED
|
@@ -93,12 +93,93 @@ var SDK_POLL_INTERVAL_MS_DEFAULT = 5e3;
|
|
|
93
93
|
var DPOP_IDB_NAME = "geonicdb-sdk";
|
|
94
94
|
var DPOP_IDB_STORE = "dpop-sessions";
|
|
95
95
|
var DPOP_IDB_VERSION = 1;
|
|
96
|
+
var SDK_IDB_OPERATION_TIMEOUT_MS = 3e3;
|
|
96
97
|
var SDK_CACHE_IDB_NAME = "geonicdb-sdk-cache";
|
|
97
98
|
var SDK_CACHE_IDB_STORE = "responses";
|
|
98
99
|
var SDK_CACHE_IDB_VERSION = 1;
|
|
99
100
|
var WEBPUSH_VAPID_KEY_PATH = "/.well-known/webpush-vapid-key";
|
|
100
101
|
var WEBPUSH_SUBSCRIPTION_ID_STORAGE_PREFIX = "geonicdb:webpush:subscriptionId";
|
|
101
102
|
|
|
103
|
+
// src/sdk/idb-timeout.ts
|
|
104
|
+
var IdbTimeoutError = class extends Error {
|
|
105
|
+
constructor(message) {
|
|
106
|
+
super(message);
|
|
107
|
+
this.name = "IdbTimeoutError";
|
|
108
|
+
}
|
|
109
|
+
};
|
|
110
|
+
function withIdbTimeout(promise, options) {
|
|
111
|
+
return new Promise((resolve, reject) => {
|
|
112
|
+
let settled = false;
|
|
113
|
+
const timer = setTimeout(() => {
|
|
114
|
+
if (settled) return;
|
|
115
|
+
settled = true;
|
|
116
|
+
try {
|
|
117
|
+
options.onTimeout?.();
|
|
118
|
+
} catch {
|
|
119
|
+
}
|
|
120
|
+
reject(new IdbTimeoutError(`IndexedDB ${options.operation} timed out`));
|
|
121
|
+
}, options.timeoutMs);
|
|
122
|
+
promise.then(
|
|
123
|
+
(value) => {
|
|
124
|
+
if (settled) return;
|
|
125
|
+
settled = true;
|
|
126
|
+
clearTimeout(timer);
|
|
127
|
+
resolve(value);
|
|
128
|
+
},
|
|
129
|
+
(err) => {
|
|
130
|
+
if (settled) return;
|
|
131
|
+
settled = true;
|
|
132
|
+
clearTimeout(timer);
|
|
133
|
+
reject(err instanceof Error ? err : new Error(String(err)));
|
|
134
|
+
}
|
|
135
|
+
);
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
function openIndexedDbWithTimeout(options) {
|
|
139
|
+
return new Promise((resolve, reject) => {
|
|
140
|
+
let settled = false;
|
|
141
|
+
const timer = setTimeout(() => {
|
|
142
|
+
if (settled) return;
|
|
143
|
+
settled = true;
|
|
144
|
+
reject(new IdbTimeoutError("IndexedDB open timed out"));
|
|
145
|
+
}, options.timeoutMs);
|
|
146
|
+
let req;
|
|
147
|
+
try {
|
|
148
|
+
req = globalThis.indexedDB.open(options.name, options.version);
|
|
149
|
+
} catch (err) {
|
|
150
|
+
if (settled) return;
|
|
151
|
+
settled = true;
|
|
152
|
+
clearTimeout(timer);
|
|
153
|
+
reject(err instanceof Error ? err : new Error(String(err)));
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
156
|
+
req.onupgradeneeded = () => {
|
|
157
|
+
options.onUpgradeNeeded?.(req);
|
|
158
|
+
};
|
|
159
|
+
req.onsuccess = () => {
|
|
160
|
+
if (settled) {
|
|
161
|
+
req.result?.close?.();
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
settled = true;
|
|
165
|
+
clearTimeout(timer);
|
|
166
|
+
resolve(req.result);
|
|
167
|
+
};
|
|
168
|
+
req.onerror = () => {
|
|
169
|
+
if (settled) return;
|
|
170
|
+
settled = true;
|
|
171
|
+
clearTimeout(timer);
|
|
172
|
+
reject(req.error ?? new Error("IndexedDB open failed"));
|
|
173
|
+
};
|
|
174
|
+
req.onblocked = () => {
|
|
175
|
+
if (settled) return;
|
|
176
|
+
settled = true;
|
|
177
|
+
clearTimeout(timer);
|
|
178
|
+
reject(req.error ?? new Error("IndexedDB open blocked"));
|
|
179
|
+
};
|
|
180
|
+
});
|
|
181
|
+
}
|
|
182
|
+
|
|
102
183
|
// src/sdk/dpop.ts
|
|
103
184
|
var dpopSupported = typeof crypto !== "undefined" && !!crypto.subtle && !!crypto.subtle.generateKey;
|
|
104
185
|
var dpopPersistenceSupported = typeof globalThis.indexedDB !== "undefined" && typeof structuredClone !== "undefined";
|
|
@@ -159,40 +240,52 @@ async function createDPoPProof(keyPair, htm, htu, accessToken, nonce) {
|
|
|
159
240
|
return signingInput + "." + b64url(new Uint8Array(sigBuf));
|
|
160
241
|
}
|
|
161
242
|
function openSessionDB() {
|
|
162
|
-
return
|
|
163
|
-
|
|
164
|
-
|
|
243
|
+
return openIndexedDbWithTimeout({
|
|
244
|
+
name: DPOP_IDB_NAME,
|
|
245
|
+
version: DPOP_IDB_VERSION,
|
|
246
|
+
timeoutMs: SDK_IDB_OPERATION_TIMEOUT_MS,
|
|
247
|
+
onUpgradeNeeded: (req) => {
|
|
165
248
|
const db = req.result;
|
|
166
249
|
if (!db.objectStoreNames.contains(DPOP_IDB_STORE)) {
|
|
167
250
|
db.createObjectStore(DPOP_IDB_STORE);
|
|
168
251
|
}
|
|
169
|
-
}
|
|
170
|
-
req.onsuccess = () => {
|
|
171
|
-
resolve(req.result);
|
|
172
|
-
};
|
|
173
|
-
req.onerror = () => {
|
|
174
|
-
reject(req.error ?? new Error("IndexedDB open failed"));
|
|
175
|
-
};
|
|
252
|
+
}
|
|
176
253
|
});
|
|
177
254
|
}
|
|
178
|
-
async function saveDPoPSession(session) {
|
|
255
|
+
async function saveDPoPSession(session, checkValid) {
|
|
179
256
|
if (!dpopPersistenceSupported) return false;
|
|
180
257
|
let db = null;
|
|
181
258
|
try {
|
|
182
259
|
db = await openSessionDB();
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
260
|
+
if (checkValid && !checkValid()) {
|
|
261
|
+
return false;
|
|
262
|
+
}
|
|
263
|
+
let tx = null;
|
|
264
|
+
await withIdbTimeout(
|
|
265
|
+
new Promise((resolve, reject) => {
|
|
266
|
+
tx = db.transaction(DPOP_IDB_STORE, "readwrite");
|
|
267
|
+
tx.objectStore(DPOP_IDB_STORE).put(session, session.tenant);
|
|
268
|
+
tx.oncomplete = () => {
|
|
269
|
+
resolve();
|
|
270
|
+
};
|
|
271
|
+
tx.onerror = () => {
|
|
272
|
+
reject(tx.error ?? new Error("IndexedDB put failed"));
|
|
273
|
+
};
|
|
274
|
+
tx.onabort = () => {
|
|
275
|
+
reject(tx.error ?? new Error("IndexedDB transaction aborted"));
|
|
276
|
+
};
|
|
277
|
+
}),
|
|
278
|
+
{
|
|
279
|
+
operation: "put",
|
|
280
|
+
timeoutMs: SDK_IDB_OPERATION_TIMEOUT_MS,
|
|
281
|
+
onTimeout: () => {
|
|
282
|
+
try {
|
|
283
|
+
tx?.abort?.();
|
|
284
|
+
} catch {
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
);
|
|
196
289
|
return true;
|
|
197
290
|
} catch {
|
|
198
291
|
return false;
|
|
@@ -205,16 +298,29 @@ async function loadDPoPSession(tenant) {
|
|
|
205
298
|
let db = null;
|
|
206
299
|
try {
|
|
207
300
|
db = await openSessionDB();
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
301
|
+
let tx = null;
|
|
302
|
+
const data = await withIdbTimeout(
|
|
303
|
+
new Promise((resolve, reject) => {
|
|
304
|
+
tx = db.transaction(DPOP_IDB_STORE, "readonly");
|
|
305
|
+
const req = tx.objectStore(DPOP_IDB_STORE).get(tenant);
|
|
306
|
+
req.onsuccess = () => {
|
|
307
|
+
resolve(req.result);
|
|
308
|
+
};
|
|
309
|
+
req.onerror = () => {
|
|
310
|
+
reject(req.error ?? new Error("IndexedDB get failed"));
|
|
311
|
+
};
|
|
312
|
+
}),
|
|
313
|
+
{
|
|
314
|
+
operation: "get",
|
|
315
|
+
timeoutMs: SDK_IDB_OPERATION_TIMEOUT_MS,
|
|
316
|
+
onTimeout: () => {
|
|
317
|
+
try {
|
|
318
|
+
tx?.abort?.();
|
|
319
|
+
} catch {
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
);
|
|
218
324
|
return validatePersistedSession(data);
|
|
219
325
|
} catch {
|
|
220
326
|
return null;
|
|
@@ -227,19 +333,81 @@ async function clearDPoPSession(tenant) {
|
|
|
227
333
|
let db = null;
|
|
228
334
|
try {
|
|
229
335
|
db = await openSessionDB();
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
336
|
+
let tx = null;
|
|
337
|
+
await withIdbTimeout(
|
|
338
|
+
new Promise((resolve, reject) => {
|
|
339
|
+
tx = db.transaction(DPOP_IDB_STORE, "readwrite");
|
|
340
|
+
tx.objectStore(DPOP_IDB_STORE).delete(tenant);
|
|
341
|
+
tx.oncomplete = () => {
|
|
342
|
+
resolve();
|
|
343
|
+
};
|
|
344
|
+
tx.onerror = () => {
|
|
345
|
+
reject(tx.error ?? new Error("IndexedDB delete failed"));
|
|
346
|
+
};
|
|
347
|
+
tx.onabort = () => {
|
|
348
|
+
reject(tx.error ?? new Error("IndexedDB transaction aborted"));
|
|
349
|
+
};
|
|
350
|
+
}),
|
|
351
|
+
{
|
|
352
|
+
operation: "delete",
|
|
353
|
+
timeoutMs: SDK_IDB_OPERATION_TIMEOUT_MS,
|
|
354
|
+
onTimeout: () => {
|
|
355
|
+
try {
|
|
356
|
+
tx?.abort?.();
|
|
357
|
+
} catch {
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
);
|
|
362
|
+
} catch {
|
|
363
|
+
} finally {
|
|
364
|
+
db?.close();
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
async function clearDPoPSessionUpToRevision(tenant, maxPersistRevision) {
|
|
368
|
+
if (!dpopPersistenceSupported) return;
|
|
369
|
+
let db = null;
|
|
370
|
+
try {
|
|
371
|
+
db = await openSessionDB();
|
|
372
|
+
let tx = null;
|
|
373
|
+
await withIdbTimeout(
|
|
374
|
+
new Promise((resolve, reject) => {
|
|
375
|
+
tx = db.transaction(DPOP_IDB_STORE, "readwrite");
|
|
376
|
+
const store = tx.objectStore(DPOP_IDB_STORE);
|
|
377
|
+
const req = store.get(tenant);
|
|
378
|
+
req.onsuccess = () => {
|
|
379
|
+
const current = validatePersistedSession(req.result);
|
|
380
|
+
const revision = current?.persistRevision;
|
|
381
|
+
const isLegacy = typeof revision !== "number";
|
|
382
|
+
const isOlderOrEqual = typeof revision === "number" && revision <= maxPersistRevision;
|
|
383
|
+
if (!current || isLegacy || isOlderOrEqual) {
|
|
384
|
+
store.delete(tenant);
|
|
385
|
+
}
|
|
386
|
+
};
|
|
387
|
+
req.onerror = () => {
|
|
388
|
+
reject(req.error ?? new Error("IndexedDB get failed"));
|
|
389
|
+
};
|
|
390
|
+
tx.oncomplete = () => {
|
|
391
|
+
resolve();
|
|
392
|
+
};
|
|
393
|
+
tx.onerror = () => {
|
|
394
|
+
reject(tx.error ?? new Error("IndexedDB delete failed"));
|
|
395
|
+
};
|
|
396
|
+
tx.onabort = () => {
|
|
397
|
+
reject(tx.error ?? new Error("IndexedDB transaction aborted"));
|
|
398
|
+
};
|
|
399
|
+
}),
|
|
400
|
+
{
|
|
401
|
+
operation: "conditional delete",
|
|
402
|
+
timeoutMs: SDK_IDB_OPERATION_TIMEOUT_MS,
|
|
403
|
+
onTimeout: () => {
|
|
404
|
+
try {
|
|
405
|
+
tx?.abort?.();
|
|
406
|
+
} catch {
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
);
|
|
243
411
|
} catch {
|
|
244
412
|
} finally {
|
|
245
413
|
db?.close();
|
|
@@ -249,7 +417,7 @@ function validatePersistedSession(data) {
|
|
|
249
417
|
if (!data || typeof data !== "object") return null;
|
|
250
418
|
const s = data;
|
|
251
419
|
const kp = s.keyPair;
|
|
252
|
-
if (typeof s.tenant !== "string" || typeof s.accessToken !== "string" || typeof s.refreshToken !== "string" || typeof s.expiresAt !== "number" || !kp || !kp.privateKey || !kp.publicJwk) {
|
|
420
|
+
if (typeof s.tenant !== "string" || typeof s.accessToken !== "string" || typeof s.refreshToken !== "string" || typeof s.expiresAt !== "number" || s.persistRevision !== void 0 && typeof s.persistRevision !== "number" || !kp || !kp.privateKey || !kp.publicJwk) {
|
|
253
421
|
return null;
|
|
254
422
|
}
|
|
255
423
|
return data;
|
|
@@ -464,11 +632,15 @@ var SdkCache = class {
|
|
|
464
632
|
* best-effort) and does not stall the chain. Returns the tail so callers that
|
|
465
633
|
* need ordering (`clearPersisted`) can await it.
|
|
466
634
|
*/
|
|
467
|
-
_enqueuePersist(op) {
|
|
635
|
+
_enqueuePersist(op, options) {
|
|
468
636
|
const persistence = this._persistence;
|
|
469
637
|
if (!persistence) return this._persistQueue;
|
|
470
638
|
const scope = this._scope;
|
|
471
|
-
|
|
639
|
+
const next = this._persistQueue.catch(() => void 0).then(() => op(persistence, scope));
|
|
640
|
+
this._persistQueue = next.catch(() => void 0);
|
|
641
|
+
if (options?.propagateError) {
|
|
642
|
+
return next;
|
|
643
|
+
}
|
|
472
644
|
return this._persistQueue;
|
|
473
645
|
}
|
|
474
646
|
/**
|
|
@@ -490,10 +662,21 @@ var SdkCache = class {
|
|
|
490
662
|
this._store.delete(oldest);
|
|
491
663
|
}
|
|
492
664
|
}
|
|
493
|
-
/**
|
|
665
|
+
/**
|
|
666
|
+
* Drop every persisted entry for the current scope (auth change / flush).
|
|
667
|
+
* Returns false when the persistence backend failed to clear.
|
|
668
|
+
*/
|
|
494
669
|
async clearPersisted() {
|
|
495
|
-
if (!this._persistence) return;
|
|
496
|
-
|
|
670
|
+
if (!this._persistence) return true;
|
|
671
|
+
try {
|
|
672
|
+
await this._enqueuePersist(
|
|
673
|
+
(persistence, scope) => persistence.clearScope(scope),
|
|
674
|
+
{ propagateError: true }
|
|
675
|
+
);
|
|
676
|
+
return true;
|
|
677
|
+
} catch {
|
|
678
|
+
return false;
|
|
679
|
+
}
|
|
497
680
|
}
|
|
498
681
|
/** Build a stable cache key. Method is upper-cased so casing differences do not produce duplicate entries. */
|
|
499
682
|
static keyFor(method, path) {
|
|
@@ -695,6 +878,8 @@ var _AuthManager = class _AuthManager {
|
|
|
695
878
|
__publicField(this, "_dpopKeyPair", null);
|
|
696
879
|
__publicField(this, "_dpopReady", null);
|
|
697
880
|
__publicField(this, "_dpopNonce", null);
|
|
881
|
+
/** Monotonic sequence used to guard delayed persisted-session cleanup. */
|
|
882
|
+
__publicField(this, "_persistRevision", 0);
|
|
698
883
|
/**
|
|
699
884
|
* When true, every DPoP token mutation (login bind, refresh) is mirrored to
|
|
700
885
|
* IndexedDB so the session survives a page reload (#1230). Toggled by
|
|
@@ -794,8 +979,19 @@ var _AuthManager = class _AuthManager {
|
|
|
794
979
|
*/
|
|
795
980
|
async _enablePersistentCache(clearFirst) {
|
|
796
981
|
if (!this._cache) return;
|
|
982
|
+
const generation = this._credentialGeneration;
|
|
797
983
|
this._cache.setScope(this._cacheScope());
|
|
798
|
-
if (clearFirst)
|
|
984
|
+
if (clearFirst) {
|
|
985
|
+
const cleared = await this._cache.clearPersisted();
|
|
986
|
+
if (generation !== this._credentialGeneration || !this._rememberSession) {
|
|
987
|
+
return;
|
|
988
|
+
}
|
|
989
|
+
if (!cleared) {
|
|
990
|
+
this._cache.setPersistEnabled(false);
|
|
991
|
+
this._log("Persistent cache clear failed; keeping persistence disabled for this session");
|
|
992
|
+
return;
|
|
993
|
+
}
|
|
994
|
+
}
|
|
799
995
|
this._cache.setPersistEnabled(true);
|
|
800
996
|
}
|
|
801
997
|
/**
|
|
@@ -898,6 +1094,7 @@ var _AuthManager = class _AuthManager {
|
|
|
898
1094
|
this._assertCredentialGeneration(generation, "login");
|
|
899
1095
|
if (this._rememberSession) {
|
|
900
1096
|
await this._persistDpopSession();
|
|
1097
|
+
this._assertCredentialGeneration(generation, "login");
|
|
901
1098
|
await this._enablePersistentCache(true);
|
|
902
1099
|
} else {
|
|
903
1100
|
this._disablePersistentCache();
|
|
@@ -1081,6 +1278,7 @@ var _AuthManager = class _AuthManager {
|
|
|
1081
1278
|
this._dpopNonce = null;
|
|
1082
1279
|
this._rememberSession = true;
|
|
1083
1280
|
this._email = persisted.email ?? null;
|
|
1281
|
+
this._persistRevision = Math.max(this._persistRevision, persisted.persistRevision ?? 0);
|
|
1084
1282
|
this._tokenPromise = null;
|
|
1085
1283
|
this._invalidateAuthScopedState();
|
|
1086
1284
|
await this._enablePersistentCache(false);
|
|
@@ -1106,15 +1304,35 @@ var _AuthManager = class _AuthManager {
|
|
|
1106
1304
|
if (!this._rememberSession || !this._tenant || !this._dpopKeyPair || !this._token || !this._refreshToken) {
|
|
1107
1305
|
return;
|
|
1108
1306
|
}
|
|
1109
|
-
const
|
|
1307
|
+
const generation = this._credentialGeneration;
|
|
1308
|
+
if (this._persistRevision === 0) {
|
|
1309
|
+
const existing = await loadDPoPSession(this._tenant);
|
|
1310
|
+
if (generation !== this._credentialGeneration || !this._rememberSession) {
|
|
1311
|
+
return;
|
|
1312
|
+
}
|
|
1313
|
+
this._persistRevision = Math.max(0, existing?.persistRevision ?? 0);
|
|
1314
|
+
}
|
|
1315
|
+
const expectedPersistedSession = {
|
|
1110
1316
|
tenant: this._tenant,
|
|
1111
1317
|
accessToken: this._token,
|
|
1112
1318
|
refreshToken: this._refreshToken,
|
|
1113
1319
|
expiresAt: this._tokenExpiry,
|
|
1114
|
-
email: this._email ?? void 0
|
|
1320
|
+
email: this._email ?? void 0
|
|
1321
|
+
};
|
|
1322
|
+
const persistRevision = ++this._persistRevision;
|
|
1323
|
+
const ok = await saveDPoPSession({
|
|
1324
|
+
tenant: expectedPersistedSession.tenant,
|
|
1325
|
+
accessToken: expectedPersistedSession.accessToken,
|
|
1326
|
+
refreshToken: expectedPersistedSession.refreshToken,
|
|
1327
|
+
expiresAt: expectedPersistedSession.expiresAt,
|
|
1328
|
+
email: expectedPersistedSession.email,
|
|
1329
|
+
persistRevision,
|
|
1115
1330
|
keyPair: this._dpopKeyPair
|
|
1116
|
-
});
|
|
1117
|
-
if (!ok)
|
|
1331
|
+
}, () => generation === this._credentialGeneration && this._rememberSession);
|
|
1332
|
+
if (!ok) {
|
|
1333
|
+
void clearDPoPSessionUpToRevision(expectedPersistedSession.tenant, persistRevision);
|
|
1334
|
+
this._log("DPoP session persistence failed (IndexedDB unavailable?)");
|
|
1335
|
+
}
|
|
1118
1336
|
}
|
|
1119
1337
|
/**
|
|
1120
1338
|
* Drop everything that may have been associated with the previous auth
|
|
@@ -1765,126 +1983,194 @@ var cachePersistenceSupported = typeof globalThis.indexedDB !== "undefined" && t
|
|
|
1765
1983
|
var SCOPE_SEP = "\0";
|
|
1766
1984
|
var SCOPE_RANGE_END = "\uFFFF";
|
|
1767
1985
|
function openCacheDB() {
|
|
1768
|
-
return
|
|
1769
|
-
|
|
1770
|
-
|
|
1771
|
-
|
|
1772
|
-
)
|
|
1773
|
-
req.onupgradeneeded = () => {
|
|
1986
|
+
return openIndexedDbWithTimeout({
|
|
1987
|
+
name: SDK_CACHE_IDB_NAME,
|
|
1988
|
+
version: SDK_CACHE_IDB_VERSION,
|
|
1989
|
+
timeoutMs: SDK_IDB_OPERATION_TIMEOUT_MS,
|
|
1990
|
+
onUpgradeNeeded: (req) => {
|
|
1774
1991
|
const db = req.result;
|
|
1775
1992
|
if (!db.objectStoreNames.contains(SDK_CACHE_IDB_STORE)) {
|
|
1776
1993
|
db.createObjectStore(SDK_CACHE_IDB_STORE, { keyPath: "id" });
|
|
1777
1994
|
}
|
|
1778
|
-
}
|
|
1779
|
-
req.onsuccess = () => {
|
|
1780
|
-
resolve(req.result);
|
|
1781
|
-
};
|
|
1782
|
-
req.onerror = () => {
|
|
1783
|
-
reject(req.error ?? new Error("IndexedDB open failed"));
|
|
1784
|
-
};
|
|
1785
|
-
req.onblocked = () => {
|
|
1786
|
-
reject(req.error ?? new Error("IndexedDB open blocked"));
|
|
1787
|
-
};
|
|
1995
|
+
}
|
|
1788
1996
|
});
|
|
1789
1997
|
}
|
|
1790
1998
|
function recordId(scope, key) {
|
|
1791
1999
|
return scope + SCOPE_SEP + key;
|
|
1792
2000
|
}
|
|
1793
2001
|
var IdbCachePersistence = class {
|
|
2002
|
+
constructor() {
|
|
2003
|
+
/**
|
|
2004
|
+
* Circuit breaker for repeated IDB timeout failures (#3898). Once a timeout
|
|
2005
|
+
* is observed, subsequent operations fail fast instead of spending another
|
|
2006
|
+
* timeout window per queued op.
|
|
2007
|
+
*/
|
|
2008
|
+
__publicField(this, "_timeoutCircuitOpen", false);
|
|
2009
|
+
}
|
|
2010
|
+
_tripTimeoutCircuit(error) {
|
|
2011
|
+
if (error instanceof IdbTimeoutError) {
|
|
2012
|
+
this._timeoutCircuitOpen = true;
|
|
2013
|
+
}
|
|
2014
|
+
}
|
|
2015
|
+
_assertCircuitClosed() {
|
|
2016
|
+
if (!this._timeoutCircuitOpen) return;
|
|
2017
|
+
throw new Error("IndexedDB persistence circuit is open");
|
|
2018
|
+
}
|
|
1794
2019
|
async loadScope(scope, limit) {
|
|
2020
|
+
if (this._timeoutCircuitOpen) return [];
|
|
1795
2021
|
let db = null;
|
|
1796
2022
|
try {
|
|
1797
2023
|
db = await openCacheDB();
|
|
1798
|
-
|
|
1799
|
-
|
|
1800
|
-
|
|
1801
|
-
|
|
1802
|
-
|
|
1803
|
-
|
|
1804
|
-
|
|
1805
|
-
|
|
1806
|
-
|
|
1807
|
-
|
|
1808
|
-
|
|
1809
|
-
|
|
1810
|
-
|
|
1811
|
-
|
|
2024
|
+
let tx = null;
|
|
2025
|
+
const records = await withIdbTimeout(
|
|
2026
|
+
new Promise((resolve, reject) => {
|
|
2027
|
+
tx = db.transaction(SDK_CACHE_IDB_STORE, "readonly");
|
|
2028
|
+
const range = globalThis.IDBKeyRange.bound(
|
|
2029
|
+
scope + SCOPE_SEP,
|
|
2030
|
+
scope + SCOPE_SEP + SCOPE_RANGE_END
|
|
2031
|
+
);
|
|
2032
|
+
const req = tx.objectStore(SDK_CACHE_IDB_STORE).getAll(range);
|
|
2033
|
+
req.onsuccess = () => {
|
|
2034
|
+
resolve(req.result ?? []);
|
|
2035
|
+
};
|
|
2036
|
+
req.onerror = () => {
|
|
2037
|
+
reject(req.error ?? new Error("IndexedDB getAll failed"));
|
|
2038
|
+
};
|
|
2039
|
+
}),
|
|
2040
|
+
{
|
|
2041
|
+
operation: "getAll",
|
|
2042
|
+
timeoutMs: SDK_IDB_OPERATION_TIMEOUT_MS,
|
|
2043
|
+
onTimeout: () => {
|
|
2044
|
+
try {
|
|
2045
|
+
tx?.abort?.();
|
|
2046
|
+
} catch {
|
|
2047
|
+
}
|
|
2048
|
+
}
|
|
2049
|
+
}
|
|
2050
|
+
);
|
|
1812
2051
|
return records.filter((r) => r && r.scope === scope && typeof r.key === "string" && r.entry).sort((a, b) => (a.entry.cachedAt ?? 0) - (b.entry.cachedAt ?? 0)).slice(-limit).map((r) => ({ key: r.key, entry: r.entry }));
|
|
1813
|
-
} catch {
|
|
2052
|
+
} catch (err) {
|
|
2053
|
+
this._tripTimeoutCircuit(err);
|
|
1814
2054
|
return [];
|
|
1815
2055
|
} finally {
|
|
1816
2056
|
db?.close();
|
|
1817
2057
|
}
|
|
1818
2058
|
}
|
|
1819
2059
|
async put(scope, key, entry) {
|
|
2060
|
+
if (this._timeoutCircuitOpen) return;
|
|
1820
2061
|
let db = null;
|
|
1821
2062
|
try {
|
|
1822
2063
|
db = await openCacheDB();
|
|
1823
|
-
|
|
1824
|
-
|
|
1825
|
-
|
|
1826
|
-
|
|
1827
|
-
|
|
1828
|
-
|
|
1829
|
-
|
|
1830
|
-
|
|
1831
|
-
|
|
1832
|
-
|
|
1833
|
-
|
|
1834
|
-
|
|
1835
|
-
|
|
1836
|
-
|
|
1837
|
-
|
|
2064
|
+
let tx = null;
|
|
2065
|
+
await withIdbTimeout(
|
|
2066
|
+
new Promise((resolve, reject) => {
|
|
2067
|
+
tx = db.transaction(SDK_CACHE_IDB_STORE, "readwrite");
|
|
2068
|
+
const record = { id: recordId(scope, key), scope, key, entry };
|
|
2069
|
+
tx.objectStore(SDK_CACHE_IDB_STORE).put(record);
|
|
2070
|
+
tx.oncomplete = () => {
|
|
2071
|
+
resolve();
|
|
2072
|
+
};
|
|
2073
|
+
tx.onerror = () => {
|
|
2074
|
+
reject(tx.error ?? new Error("IndexedDB put failed"));
|
|
2075
|
+
};
|
|
2076
|
+
tx.onabort = () => {
|
|
2077
|
+
reject(tx.error ?? new Error("IndexedDB transaction aborted"));
|
|
2078
|
+
};
|
|
2079
|
+
}),
|
|
2080
|
+
{
|
|
2081
|
+
operation: "put",
|
|
2082
|
+
timeoutMs: SDK_IDB_OPERATION_TIMEOUT_MS,
|
|
2083
|
+
onTimeout: () => {
|
|
2084
|
+
try {
|
|
2085
|
+
tx?.abort?.();
|
|
2086
|
+
} catch {
|
|
2087
|
+
}
|
|
2088
|
+
}
|
|
2089
|
+
}
|
|
2090
|
+
);
|
|
2091
|
+
} catch (err) {
|
|
2092
|
+
this._tripTimeoutCircuit(err);
|
|
1838
2093
|
} finally {
|
|
1839
2094
|
db?.close();
|
|
1840
2095
|
}
|
|
1841
2096
|
}
|
|
1842
2097
|
async remove(scope, keys) {
|
|
1843
2098
|
if (keys.length === 0) return;
|
|
2099
|
+
if (this._timeoutCircuitOpen) return;
|
|
1844
2100
|
let db = null;
|
|
1845
2101
|
try {
|
|
1846
2102
|
db = await openCacheDB();
|
|
1847
|
-
|
|
1848
|
-
|
|
1849
|
-
|
|
1850
|
-
|
|
1851
|
-
|
|
1852
|
-
|
|
1853
|
-
|
|
1854
|
-
|
|
1855
|
-
|
|
1856
|
-
|
|
1857
|
-
|
|
1858
|
-
|
|
1859
|
-
|
|
1860
|
-
|
|
1861
|
-
|
|
2103
|
+
let tx = null;
|
|
2104
|
+
await withIdbTimeout(
|
|
2105
|
+
new Promise((resolve, reject) => {
|
|
2106
|
+
tx = db.transaction(SDK_CACHE_IDB_STORE, "readwrite");
|
|
2107
|
+
const store = tx.objectStore(SDK_CACHE_IDB_STORE);
|
|
2108
|
+
for (const key of keys) store.delete(recordId(scope, key));
|
|
2109
|
+
tx.oncomplete = () => {
|
|
2110
|
+
resolve();
|
|
2111
|
+
};
|
|
2112
|
+
tx.onerror = () => {
|
|
2113
|
+
reject(tx.error ?? new Error("IndexedDB delete failed"));
|
|
2114
|
+
};
|
|
2115
|
+
tx.onabort = () => {
|
|
2116
|
+
reject(tx.error ?? new Error("IndexedDB transaction aborted"));
|
|
2117
|
+
};
|
|
2118
|
+
}),
|
|
2119
|
+
{
|
|
2120
|
+
operation: "delete",
|
|
2121
|
+
timeoutMs: SDK_IDB_OPERATION_TIMEOUT_MS,
|
|
2122
|
+
onTimeout: () => {
|
|
2123
|
+
try {
|
|
2124
|
+
tx?.abort?.();
|
|
2125
|
+
} catch {
|
|
2126
|
+
}
|
|
2127
|
+
}
|
|
2128
|
+
}
|
|
2129
|
+
);
|
|
2130
|
+
} catch (err) {
|
|
2131
|
+
this._tripTimeoutCircuit(err);
|
|
1862
2132
|
} finally {
|
|
1863
2133
|
db?.close();
|
|
1864
2134
|
}
|
|
1865
2135
|
}
|
|
1866
2136
|
async clearScope(scope) {
|
|
2137
|
+
this._assertCircuitClosed();
|
|
1867
2138
|
let db = null;
|
|
1868
2139
|
try {
|
|
1869
2140
|
db = await openCacheDB();
|
|
1870
|
-
|
|
1871
|
-
|
|
1872
|
-
|
|
1873
|
-
|
|
1874
|
-
|
|
1875
|
-
|
|
1876
|
-
|
|
1877
|
-
|
|
1878
|
-
|
|
1879
|
-
|
|
1880
|
-
|
|
1881
|
-
|
|
1882
|
-
|
|
1883
|
-
|
|
1884
|
-
|
|
1885
|
-
|
|
1886
|
-
|
|
1887
|
-
|
|
2141
|
+
let tx = null;
|
|
2142
|
+
await withIdbTimeout(
|
|
2143
|
+
new Promise((resolve, reject) => {
|
|
2144
|
+
tx = db.transaction(SDK_CACHE_IDB_STORE, "readwrite");
|
|
2145
|
+
const range = globalThis.IDBKeyRange.bound(
|
|
2146
|
+
scope + SCOPE_SEP,
|
|
2147
|
+
scope + SCOPE_SEP + SCOPE_RANGE_END
|
|
2148
|
+
);
|
|
2149
|
+
tx.objectStore(SDK_CACHE_IDB_STORE).delete(range);
|
|
2150
|
+
tx.oncomplete = () => {
|
|
2151
|
+
resolve();
|
|
2152
|
+
};
|
|
2153
|
+
tx.onerror = () => {
|
|
2154
|
+
reject(tx.error ?? new Error("IndexedDB clear failed"));
|
|
2155
|
+
};
|
|
2156
|
+
tx.onabort = () => {
|
|
2157
|
+
reject(tx.error ?? new Error("IndexedDB transaction aborted"));
|
|
2158
|
+
};
|
|
2159
|
+
}),
|
|
2160
|
+
{
|
|
2161
|
+
operation: "clear",
|
|
2162
|
+
timeoutMs: SDK_IDB_OPERATION_TIMEOUT_MS,
|
|
2163
|
+
onTimeout: () => {
|
|
2164
|
+
try {
|
|
2165
|
+
tx?.abort?.();
|
|
2166
|
+
} catch {
|
|
2167
|
+
}
|
|
2168
|
+
}
|
|
2169
|
+
}
|
|
2170
|
+
);
|
|
2171
|
+
} catch (err) {
|
|
2172
|
+
this._tripTimeoutCircuit(err);
|
|
2173
|
+
throw err instanceof Error ? err : new Error(String(err));
|
|
1888
2174
|
} finally {
|
|
1889
2175
|
db?.close();
|
|
1890
2176
|
}
|