@abraca/dabra 2.81.0 → 2.83.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/dist/abracadabra-provider.cjs +199 -0
- package/dist/abracadabra-provider.cjs.map +1 -1
- package/dist/abracadabra-provider.esm.js +197 -1
- package/dist/abracadabra-provider.esm.js.map +1 -1
- package/dist/index.d.ts +144 -1
- package/package.json +2 -2
- package/src/AbracadabraClient.ts +34 -0
- package/src/ServerReset.ts +222 -0
- package/src/index.ts +1 -0
- package/src/types.ts +10 -0
|
@@ -3572,6 +3572,174 @@ var AbracadabraProvider = class AbracadabraProvider extends AbracadabraBaseProvi
|
|
|
3572
3572
|
}
|
|
3573
3573
|
};
|
|
3574
3574
|
|
|
3575
|
+
//#endregion
|
|
3576
|
+
//#region packages/provider/src/ServerReset.ts
|
|
3577
|
+
/**
|
|
3578
|
+
* Detect that a server's database has been REPLACED, and drop this browser's
|
|
3579
|
+
* caches for it before anything re-pushes them.
|
|
3580
|
+
*
|
|
3581
|
+
* ## The bug
|
|
3582
|
+
*
|
|
3583
|
+
* Deleting a backend's database does not give you a clean server. Every browser
|
|
3584
|
+
* that was connected still holds that space's CRDT state in IndexedDB, and on
|
|
3585
|
+
* reconnect it re-pushes the old tree into the fresh one. A seeder then adds its
|
|
3586
|
+
* own on top and you get every page twice — `/about` AND `/about-1`. Repeat the
|
|
3587
|
+
* cycle and it MULTIPLIES: `-2`, `-3`, `-4`.
|
|
3588
|
+
*
|
|
3589
|
+
* This is inherent to a CRDT and cannot be fixed downstream. A deleted database
|
|
3590
|
+
* is not a tombstone; it is a peer that lost its memory, and the client is the
|
|
3591
|
+
* one holding the newer state. No idempotency guard can see it — by the time one
|
|
3592
|
+
* runs, the re-pushed tree looks exactly like real content.
|
|
3593
|
+
*
|
|
3594
|
+
* It is not a development-only annoyance. The expensive case is a production
|
|
3595
|
+
* restore from backup, where the peers re-pushing stale trees are visitors who
|
|
3596
|
+
* will never read a release note.
|
|
3597
|
+
*
|
|
3598
|
+
* ## Why the server can be believed
|
|
3599
|
+
*
|
|
3600
|
+
* The server mints a `server_id` and persists it beside its database. It
|
|
3601
|
+
* survives restarts and changes when, and only when, that database is replaced.
|
|
3602
|
+
* So it states outright what a CRDT cannot work out for itself: *the peer you
|
|
3603
|
+
* are syncing with is not the one you have been talking to.*
|
|
3604
|
+
*
|
|
3605
|
+
* ## What gets dropped
|
|
3606
|
+
*
|
|
3607
|
+
* Everything this SDK scopes to a server, which is more than the document
|
|
3608
|
+
* caches:
|
|
3609
|
+
*
|
|
3610
|
+
* | store | written by |
|
|
3611
|
+
* |---|---|
|
|
3612
|
+
* | `abracadabra:<host>/<docId>` | {@link OfflineStore} |
|
|
3613
|
+
* | `abracadabra:meta-cache:<host>` | `DocumentCache` |
|
|
3614
|
+
* | `abracadabra:files:<host>` | `FileBlobStore` |
|
|
3615
|
+
* | `abracadabra:bgsync:<host>` | `BackgroundSyncPersistence` |
|
|
3616
|
+
* | `abracadabra:search:<host>` | `SearchIndex` |
|
|
3617
|
+
*
|
|
3618
|
+
* One ANCHORED rule covers all five shapes, so nothing here has to stay in step
|
|
3619
|
+
* with how each store builds its key. Anchored rather than a substring test on
|
|
3620
|
+
* purpose — see {@link isServerScopedStore}, where the naive version would have
|
|
3621
|
+
* cleared a different server's caches.
|
|
3622
|
+
*
|
|
3623
|
+
* `abracadabra:identity` is deliberately NOT dropped. It is this browser's
|
|
3624
|
+
* keypair across every server it talks to; clearing it would sign the user out
|
|
3625
|
+
* of all of them because one of them was reinstalled.
|
|
3626
|
+
*/
|
|
3627
|
+
/** Where the last-seen id for a host is remembered. */
|
|
3628
|
+
const STORAGE_PREFIX = "abracadabra:server-id:";
|
|
3629
|
+
/**
|
|
3630
|
+
* Stores that belong to no particular server and must survive a reset.
|
|
3631
|
+
*
|
|
3632
|
+
* `abracadabra:identity` is this browser's keypair for EVERY server it talks to,
|
|
3633
|
+
* so dropping it because one server was reinstalled would sign the user out of
|
|
3634
|
+
* all of them. Listed explicitly rather than relying on the anchored match, so
|
|
3635
|
+
* that a future store named after a host cannot take it by accident.
|
|
3636
|
+
*/
|
|
3637
|
+
const GLOBAL_STORES = new Set(["abracadabra:identity", "abracadabra:softkey"]);
|
|
3638
|
+
function storageAvailable() {
|
|
3639
|
+
try {
|
|
3640
|
+
return typeof globalThis !== "undefined" && "localStorage" in globalThis && "indexedDB" in globalThis;
|
|
3641
|
+
} catch {
|
|
3642
|
+
return false;
|
|
3643
|
+
}
|
|
3644
|
+
}
|
|
3645
|
+
/**
|
|
3646
|
+
* Does this database belong to the given server?
|
|
3647
|
+
*
|
|
3648
|
+
* The match is ANCHORED, and it has to be. A plain `name.includes(host)` looks
|
|
3649
|
+
* right and is wrong in a way that destroys data: `other.example.com:3011`
|
|
3650
|
+
* contains `example.com:3011`, so clearing one server's caches would delete a
|
|
3651
|
+
* DIFFERENT server's — the exact "too broad" failure this guard exists to avoid.
|
|
3652
|
+
* A unit test covers that pair.
|
|
3653
|
+
*
|
|
3654
|
+
* Three anchored shapes are needed, because the stores are NOT consistently
|
|
3655
|
+
* scoped and pretending otherwise silently misses some:
|
|
3656
|
+
*
|
|
3657
|
+
* - `<host>/…` — {@link OfflineStore}, keyed `<host>/<docId>`
|
|
3658
|
+
* - `…:<host>` — a namespaced store handed a bare host
|
|
3659
|
+
* - `…://<host>` — a namespaced store handed a full URL. Real, not hypothetical:
|
|
3660
|
+
* `@abraca/nuxt` passes `serverUrl` to `FileBlobStore`, so the database is
|
|
3661
|
+
* named `abracadabra:files:http://example.com:3011`. Measured in a live
|
|
3662
|
+
* browser; a rule written only for the first two shapes left it behind, which
|
|
3663
|
+
* is a whole file cache surviving a wipe.
|
|
3664
|
+
*/
|
|
3665
|
+
function isServerScopedStore(name, host) {
|
|
3666
|
+
if (!name.startsWith("abracadabra:")) return false;
|
|
3667
|
+
if (GLOBAL_STORES.has(name)) return false;
|
|
3668
|
+
const rest = name.slice(12);
|
|
3669
|
+
return rest === host || rest.startsWith(`${host}/`) || rest.endsWith(`:${host}`) || rest.endsWith(`://${host}`);
|
|
3670
|
+
}
|
|
3671
|
+
/**
|
|
3672
|
+
* Compare the server's identity against the one this browser last saw, and drop
|
|
3673
|
+
* every cache belonging to it if the database has been replaced.
|
|
3674
|
+
*
|
|
3675
|
+
* **Call this before opening any provider.** Once a provider holds an IndexedDB
|
|
3676
|
+
* handle, `deleteDatabase` blocks instead of deleting and the stale tree is
|
|
3677
|
+
* already queued for re-push — the deletion completes later, having achieved
|
|
3678
|
+
* nothing. Any store in that position comes back in `blocked`.
|
|
3679
|
+
*
|
|
3680
|
+
* Conservative by design:
|
|
3681
|
+
*
|
|
3682
|
+
* - the FIRST time a host is seen, the id is recorded and nothing is deleted —
|
|
3683
|
+
* a new visitor must not have caches cleared for having no history;
|
|
3684
|
+
* - a browser with no `indexedDB`/`localStorage` is a no-op;
|
|
3685
|
+
* - it never throws. Failing to tidy a cache must not stop an app from starting.
|
|
3686
|
+
*
|
|
3687
|
+
* @param host The server's `host` (`example.com:3011`) — the same value
|
|
3688
|
+
* providers scope their stores by. Not the full origin: the
|
|
3689
|
+
* scheme is not part of a server's cache identity here.
|
|
3690
|
+
* @param serverId `server_id` from `GET /info`.
|
|
3691
|
+
*/
|
|
3692
|
+
async function dropCachesIfServerReplaced(host, serverId) {
|
|
3693
|
+
if (!storageAvailable() || !host || !serverId) return null;
|
|
3694
|
+
const key = STORAGE_PREFIX + host;
|
|
3695
|
+
let previousServerId = null;
|
|
3696
|
+
try {
|
|
3697
|
+
previousServerId = globalThis.localStorage.getItem(key);
|
|
3698
|
+
} catch {
|
|
3699
|
+
return null;
|
|
3700
|
+
}
|
|
3701
|
+
if (previousServerId === null) {
|
|
3702
|
+
try {
|
|
3703
|
+
globalThis.localStorage.setItem(key, serverId);
|
|
3704
|
+
} catch {}
|
|
3705
|
+
return null;
|
|
3706
|
+
}
|
|
3707
|
+
if (previousServerId === serverId) return null;
|
|
3708
|
+
const cleared = [];
|
|
3709
|
+
const blocked = [];
|
|
3710
|
+
try {
|
|
3711
|
+
const doomed = (await globalThis.indexedDB.databases?.() ?? []).map((d) => d.name).filter((n) => !!n && isServerScopedStore(n, host));
|
|
3712
|
+
await Promise.all(doomed.map((name) => new Promise((resolve) => {
|
|
3713
|
+
const request = globalThis.indexedDB.deleteDatabase(name);
|
|
3714
|
+
request.onsuccess = () => {
|
|
3715
|
+
cleared.push(name);
|
|
3716
|
+
resolve();
|
|
3717
|
+
};
|
|
3718
|
+
request.onerror = () => resolve();
|
|
3719
|
+
request.onblocked = () => {
|
|
3720
|
+
blocked.push(name);
|
|
3721
|
+
resolve();
|
|
3722
|
+
};
|
|
3723
|
+
})));
|
|
3724
|
+
} catch {}
|
|
3725
|
+
try {
|
|
3726
|
+
globalThis.localStorage.setItem(key, serverId);
|
|
3727
|
+
} catch {}
|
|
3728
|
+
return {
|
|
3729
|
+
previousServerId,
|
|
3730
|
+
serverId,
|
|
3731
|
+
cleared,
|
|
3732
|
+
blocked
|
|
3733
|
+
};
|
|
3734
|
+
}
|
|
3735
|
+
/** Forget what we know about a host, so the next check treats it as new. */
|
|
3736
|
+
function forgetServerIdentity(host) {
|
|
3737
|
+
if (!storageAvailable() || !host) return;
|
|
3738
|
+
try {
|
|
3739
|
+
globalThis.localStorage.removeItem(STORAGE_PREFIX + host);
|
|
3740
|
+
} catch {}
|
|
3741
|
+
}
|
|
3742
|
+
|
|
3575
3743
|
//#endregion
|
|
3576
3744
|
//#region node_modules/@noble/hashes/utils.js
|
|
3577
3745
|
/**
|
|
@@ -11759,6 +11927,34 @@ var AbracadabraClient = class {
|
|
|
11759
11927
|
return this.request("GET", "/info", { auth: false });
|
|
11760
11928
|
}
|
|
11761
11929
|
/**
|
|
11930
|
+
* Drop this browser's caches for this server if its database has been
|
|
11931
|
+
* REPLACED since the last visit, and report what happened.
|
|
11932
|
+
*
|
|
11933
|
+
* **Call it before opening any provider**, and await it. A deleted database
|
|
11934
|
+
* is not a tombstone — it is a peer that lost its memory — so a tab that
|
|
11935
|
+
* reconnects with stale IndexedDB re-pushes the old tree into the fresh one
|
|
11936
|
+
* and a seeder adds its own on top: every page twice, then three times. Once
|
|
11937
|
+
* a provider holds a store open, `deleteDatabase` blocks instead of deleting
|
|
11938
|
+
* and the damage is already queued; those stores come back in `blocked`,
|
|
11939
|
+
* which is a signal that this ran too late rather than something to ignore.
|
|
11940
|
+
*
|
|
11941
|
+
* Returns `null` when there is nothing to do — no `server_id`, no browser
|
|
11942
|
+
* storage, a first visit, or the same database as last time. It never
|
|
11943
|
+
* throws: an app must not fail to start because a cache could not be tidied.
|
|
11944
|
+
*
|
|
11945
|
+
* @see dropCachesIfServerReplaced for what is dropped and what is kept.
|
|
11946
|
+
*/
|
|
11947
|
+
async ensureFreshServer() {
|
|
11948
|
+
try {
|
|
11949
|
+
const info = await this.serverInfo();
|
|
11950
|
+
if (!info?.server_id) return null;
|
|
11951
|
+
const host = new URL(this.wsUrl).host;
|
|
11952
|
+
return await dropCachesIfServerReplaced(host, info.server_id);
|
|
11953
|
+
} catch {
|
|
11954
|
+
return null;
|
|
11955
|
+
}
|
|
11956
|
+
}
|
|
11957
|
+
/**
|
|
11762
11958
|
* Fetch ICE server configuration for WebRTC peer connections.
|
|
11763
11959
|
* Falls back to default Google STUN server if the endpoint is unavailable.
|
|
11764
11960
|
* No auth required.
|
|
@@ -22181,13 +22377,16 @@ exports.deriveDmDocId = deriveDmDocId;
|
|
|
22181
22377
|
exports.deriveIdentityDocId = deriveIdentityDocId;
|
|
22182
22378
|
exports.deriveSeedWrappingKey = deriveSeedWrappingKey;
|
|
22183
22379
|
exports.deterministicKeypair = deterministicKeypair;
|
|
22380
|
+
exports.dropCachesIfServerReplaced = dropCachesIfServerReplaced;
|
|
22184
22381
|
exports.encryptChatContent = encryptChatContent;
|
|
22185
22382
|
exports.encryptField = encryptField;
|
|
22186
22383
|
exports.filenameToLabel = filenameToLabel;
|
|
22187
22384
|
exports.foldRecords = foldRecords;
|
|
22385
|
+
exports.forgetServerIdentity = forgetServerIdentity;
|
|
22188
22386
|
exports.generateMnemonic = generateMnemonic;
|
|
22189
22387
|
exports.isEncryptedContent = isEncryptedContent;
|
|
22190
22388
|
exports.isPlaceholderLabel = isPlaceholderLabel;
|
|
22389
|
+
exports.isServerScopedStore = isServerScopedStore;
|
|
22191
22390
|
exports.loadOrCreateKeyFile = loadOrCreateKeyFile;
|
|
22192
22391
|
exports.loadServerRegistry = loadServerRegistry;
|
|
22193
22392
|
exports.loginOrRegisterWithKey = loginOrRegisterWithKey;
|