@abraca/dabra 0.6.0 → 0.8.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 +747 -66
- package/dist/abracadabra-provider.cjs.map +1 -1
- package/dist/abracadabra-provider.esm.js +725 -62
- package/dist/abracadabra-provider.esm.js.map +1 -1
- package/dist/index.d.ts +269 -31
- package/package.json +1 -2
- package/src/{HocuspocusProvider.ts → AbracadabraBaseProvider.ts} +33 -22
- package/src/AbracadabraClient.ts +96 -3
- package/src/AbracadabraProvider.ts +11 -11
- package/src/{HocuspocusProviderWebsocket.ts → AbracadabraWS.ts} +36 -22
- package/src/CloseEvents.ts +49 -0
- package/src/DocumentCache.ts +210 -0
- package/src/FileBlobStore.ts +300 -0
- package/src/MessageReceiver.ts +8 -8
- package/src/OutgoingMessages/AuthenticationMessage.ts +1 -1
- package/src/SearchIndex.ts +247 -0
- package/src/auth.ts +62 -0
- package/src/awarenessStatesToArray.ts +10 -0
- package/src/index.ts +9 -2
- package/src/types.ts +59 -1
|
@@ -26,13 +26,23 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
|
|
|
26
26
|
}) : target, mod));
|
|
27
27
|
|
|
28
28
|
//#endregion
|
|
29
|
-
let _abraca_dabra_common = require("@abraca/dabra-common");
|
|
30
29
|
let yjs = require("yjs");
|
|
31
30
|
yjs = __toESM(yjs);
|
|
32
31
|
let _lifeomic_attempt = require("@lifeomic/attempt");
|
|
33
32
|
let _noble_ed25519 = require("@noble/ed25519");
|
|
34
33
|
_noble_ed25519 = __toESM(_noble_ed25519);
|
|
35
34
|
|
|
35
|
+
//#region packages/provider/src/awarenessStatesToArray.ts
|
|
36
|
+
const awarenessStatesToArray = (states) => {
|
|
37
|
+
return Array.from(states.entries()).map(([key, value]) => {
|
|
38
|
+
return {
|
|
39
|
+
clientId: key,
|
|
40
|
+
...value
|
|
41
|
+
};
|
|
42
|
+
});
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
//#endregion
|
|
36
46
|
//#region node_modules/lib0/math.js
|
|
37
47
|
/**
|
|
38
48
|
* Common Math expressions.
|
|
@@ -1054,6 +1064,37 @@ var EventEmitter = class {
|
|
|
1054
1064
|
}
|
|
1055
1065
|
};
|
|
1056
1066
|
|
|
1067
|
+
//#endregion
|
|
1068
|
+
//#region packages/provider/src/types.ts
|
|
1069
|
+
/**
|
|
1070
|
+
* State of the WebSocket connection.
|
|
1071
|
+
* https://developer.mozilla.org/de/docs/Web/API/WebSocket/readyState
|
|
1072
|
+
*/
|
|
1073
|
+
let WsReadyStates = /* @__PURE__ */ function(WsReadyStates) {
|
|
1074
|
+
WsReadyStates[WsReadyStates["Connecting"] = 0] = "Connecting";
|
|
1075
|
+
WsReadyStates[WsReadyStates["Open"] = 1] = "Open";
|
|
1076
|
+
WsReadyStates[WsReadyStates["Closing"] = 2] = "Closing";
|
|
1077
|
+
WsReadyStates[WsReadyStates["Closed"] = 3] = "Closed";
|
|
1078
|
+
return WsReadyStates;
|
|
1079
|
+
}({});
|
|
1080
|
+
let MessageType = /* @__PURE__ */ function(MessageType) {
|
|
1081
|
+
MessageType[MessageType["Sync"] = 0] = "Sync";
|
|
1082
|
+
MessageType[MessageType["Awareness"] = 1] = "Awareness";
|
|
1083
|
+
MessageType[MessageType["Auth"] = 2] = "Auth";
|
|
1084
|
+
MessageType[MessageType["QueryAwareness"] = 3] = "QueryAwareness";
|
|
1085
|
+
MessageType[MessageType["Subdoc"] = 4] = "Subdoc";
|
|
1086
|
+
MessageType[MessageType["Stateless"] = 5] = "Stateless";
|
|
1087
|
+
MessageType[MessageType["CLOSE"] = 7] = "CLOSE";
|
|
1088
|
+
MessageType[MessageType["SyncStatus"] = 8] = "SyncStatus";
|
|
1089
|
+
return MessageType;
|
|
1090
|
+
}({});
|
|
1091
|
+
let WebSocketStatus = /* @__PURE__ */ function(WebSocketStatus) {
|
|
1092
|
+
WebSocketStatus["Connecting"] = "connecting";
|
|
1093
|
+
WebSocketStatus["Connected"] = "connected";
|
|
1094
|
+
WebSocketStatus["Disconnected"] = "disconnected";
|
|
1095
|
+
return WebSocketStatus;
|
|
1096
|
+
}({});
|
|
1097
|
+
|
|
1057
1098
|
//#endregion
|
|
1058
1099
|
//#region packages/provider/src/IncomingMessage.ts
|
|
1059
1100
|
var IncomingMessage = class {
|
|
@@ -1088,26 +1129,6 @@ var IncomingMessage = class {
|
|
|
1088
1129
|
}
|
|
1089
1130
|
};
|
|
1090
1131
|
|
|
1091
|
-
//#endregion
|
|
1092
|
-
//#region packages/provider/src/types.ts
|
|
1093
|
-
let MessageType = /* @__PURE__ */ function(MessageType) {
|
|
1094
|
-
MessageType[MessageType["Sync"] = 0] = "Sync";
|
|
1095
|
-
MessageType[MessageType["Awareness"] = 1] = "Awareness";
|
|
1096
|
-
MessageType[MessageType["Auth"] = 2] = "Auth";
|
|
1097
|
-
MessageType[MessageType["QueryAwareness"] = 3] = "QueryAwareness";
|
|
1098
|
-
MessageType[MessageType["Subdoc"] = 4] = "Subdoc";
|
|
1099
|
-
MessageType[MessageType["Stateless"] = 5] = "Stateless";
|
|
1100
|
-
MessageType[MessageType["CLOSE"] = 7] = "CLOSE";
|
|
1101
|
-
MessageType[MessageType["SyncStatus"] = 8] = "SyncStatus";
|
|
1102
|
-
return MessageType;
|
|
1103
|
-
}({});
|
|
1104
|
-
let WebSocketStatus = /* @__PURE__ */ function(WebSocketStatus) {
|
|
1105
|
-
WebSocketStatus["Connecting"] = "connecting";
|
|
1106
|
-
WebSocketStatus["Connected"] = "connected";
|
|
1107
|
-
WebSocketStatus["Disconnected"] = "disconnected";
|
|
1108
|
-
return WebSocketStatus;
|
|
1109
|
-
}({});
|
|
1110
|
-
|
|
1111
1132
|
//#endregion
|
|
1112
1133
|
//#region packages/provider/src/OutgoingMessage.ts
|
|
1113
1134
|
var OutgoingMessage = class {
|
|
@@ -1138,8 +1159,8 @@ var CloseMessage = class extends OutgoingMessage {
|
|
|
1138
1159
|
};
|
|
1139
1160
|
|
|
1140
1161
|
//#endregion
|
|
1141
|
-
//#region packages/provider/src/
|
|
1142
|
-
var
|
|
1162
|
+
//#region packages/provider/src/AbracadabraWS.ts
|
|
1163
|
+
var AbracadabraWS = class extends EventEmitter {
|
|
1143
1164
|
constructor(configuration) {
|
|
1144
1165
|
super();
|
|
1145
1166
|
this.messageQueue = [];
|
|
@@ -1372,7 +1393,7 @@ var HocuspocusProviderWebsocket = class extends EventEmitter {
|
|
|
1372
1393
|
}
|
|
1373
1394
|
}
|
|
1374
1395
|
send(message) {
|
|
1375
|
-
if (this.webSocket?.readyState ===
|
|
1396
|
+
if (this.webSocket?.readyState === WsReadyStates.Open) this.webSocket.send(message);
|
|
1376
1397
|
else this.messageQueue.push(message);
|
|
1377
1398
|
}
|
|
1378
1399
|
onClose({ event }) {
|
|
@@ -1400,6 +1421,46 @@ var HocuspocusProviderWebsocket = class extends EventEmitter {
|
|
|
1400
1421
|
this.cleanupWebSocket();
|
|
1401
1422
|
}
|
|
1402
1423
|
};
|
|
1424
|
+
/** @deprecated Use AbracadabraWS */
|
|
1425
|
+
const HocuspocusProviderWebsocket = AbracadabraWS;
|
|
1426
|
+
|
|
1427
|
+
//#endregion
|
|
1428
|
+
//#region packages/provider/src/auth.ts
|
|
1429
|
+
let AuthMessageType = /* @__PURE__ */ function(AuthMessageType) {
|
|
1430
|
+
AuthMessageType[AuthMessageType["Token"] = 0] = "Token";
|
|
1431
|
+
AuthMessageType[AuthMessageType["PermissionDenied"] = 1] = "PermissionDenied";
|
|
1432
|
+
AuthMessageType[AuthMessageType["Authenticated"] = 2] = "Authenticated";
|
|
1433
|
+
return AuthMessageType;
|
|
1434
|
+
}({});
|
|
1435
|
+
const writeAuthentication = (encoder, auth) => {
|
|
1436
|
+
writeVarUint(encoder, AuthMessageType.Token);
|
|
1437
|
+
writeVarString(encoder, auth);
|
|
1438
|
+
};
|
|
1439
|
+
const writePermissionDenied = (encoder, reason) => {
|
|
1440
|
+
writeVarUint(encoder, AuthMessageType.PermissionDenied);
|
|
1441
|
+
writeVarString(encoder, reason);
|
|
1442
|
+
};
|
|
1443
|
+
const writeAuthenticated = (encoder, scope) => {
|
|
1444
|
+
writeVarUint(encoder, AuthMessageType.Authenticated);
|
|
1445
|
+
writeVarString(encoder, scope);
|
|
1446
|
+
};
|
|
1447
|
+
const writeTokenSyncRequest = (encoder) => {
|
|
1448
|
+
writeVarUint(encoder, AuthMessageType.Token);
|
|
1449
|
+
};
|
|
1450
|
+
const readAuthMessage = (decoder, sendToken, permissionDeniedHandler, authenticatedHandler) => {
|
|
1451
|
+
switch (readVarUint(decoder)) {
|
|
1452
|
+
case AuthMessageType.Token:
|
|
1453
|
+
sendToken();
|
|
1454
|
+
break;
|
|
1455
|
+
case AuthMessageType.PermissionDenied:
|
|
1456
|
+
permissionDeniedHandler(readVarString(decoder));
|
|
1457
|
+
break;
|
|
1458
|
+
case AuthMessageType.Authenticated:
|
|
1459
|
+
authenticatedHandler(readVarString(decoder));
|
|
1460
|
+
break;
|
|
1461
|
+
default:
|
|
1462
|
+
}
|
|
1463
|
+
};
|
|
1403
1464
|
|
|
1404
1465
|
//#endregion
|
|
1405
1466
|
//#region node_modules/y-protocols/sync.js
|
|
@@ -1581,7 +1642,7 @@ var MessageReceiver = class {
|
|
|
1581
1642
|
}
|
|
1582
1643
|
applyAuthMessage(provider) {
|
|
1583
1644
|
const { message } = this;
|
|
1584
|
-
|
|
1645
|
+
readAuthMessage(message.decoder, provider.sendToken.bind(provider), provider.permissionDeniedHandler.bind(provider), provider.authenticatedHandler.bind(provider));
|
|
1585
1646
|
}
|
|
1586
1647
|
applyQueryAwarenessMessage(provider) {
|
|
1587
1648
|
if (!provider.awareness) return;
|
|
@@ -1618,7 +1679,7 @@ var AuthenticationMessage = class extends OutgoingMessage {
|
|
|
1618
1679
|
if (typeof args.token === "undefined") throw new Error("The authentication message requires `token` as an argument.");
|
|
1619
1680
|
writeVarString(this.encoder, args.documentName);
|
|
1620
1681
|
writeVarUint(this.encoder, this.type);
|
|
1621
|
-
|
|
1682
|
+
writeAuthentication(this.encoder, args.token);
|
|
1622
1683
|
return this.encoder;
|
|
1623
1684
|
}
|
|
1624
1685
|
};
|
|
@@ -1694,14 +1755,14 @@ var UpdateMessage = class extends OutgoingMessage {
|
|
|
1694
1755
|
};
|
|
1695
1756
|
|
|
1696
1757
|
//#endregion
|
|
1697
|
-
//#region packages/provider/src/
|
|
1758
|
+
//#region packages/provider/src/AbracadabraBaseProvider.ts
|
|
1698
1759
|
var AwarenessError = class extends Error {
|
|
1699
1760
|
constructor(..._args) {
|
|
1700
1761
|
super(..._args);
|
|
1701
1762
|
this.code = 1001;
|
|
1702
1763
|
}
|
|
1703
1764
|
};
|
|
1704
|
-
var
|
|
1765
|
+
var AbracadabraBaseProvider = class extends EventEmitter {
|
|
1705
1766
|
constructor(configuration) {
|
|
1706
1767
|
super();
|
|
1707
1768
|
this.configuration = {
|
|
@@ -1761,10 +1822,10 @@ var HocuspocusProvider = class extends EventEmitter {
|
|
|
1761
1822
|
this.on("authenticationFailed", this.configuration.onAuthenticationFailed);
|
|
1762
1823
|
this.on("rateLimited", this.configuration.onRateLimited);
|
|
1763
1824
|
this.awareness?.on("update", () => {
|
|
1764
|
-
this.emit("awarenessUpdate", { states:
|
|
1825
|
+
this.emit("awarenessUpdate", { states: awarenessStatesToArray(this.awareness.getStates()) });
|
|
1765
1826
|
});
|
|
1766
1827
|
this.awareness?.on("change", () => {
|
|
1767
|
-
this.emit("awarenessChange", { states:
|
|
1828
|
+
this.emit("awarenessChange", { states: awarenessStatesToArray(this.awareness.getStates()) });
|
|
1768
1829
|
});
|
|
1769
1830
|
this.document.on("update", this.boundDocumentUpdateHandler);
|
|
1770
1831
|
this.awareness?.on("update", this.boundAwarenessUpdateHandler);
|
|
@@ -1775,7 +1836,7 @@ var HocuspocusProvider = class extends EventEmitter {
|
|
|
1775
1836
|
setConfiguration(configuration = {}) {
|
|
1776
1837
|
if (!configuration.websocketProvider) {
|
|
1777
1838
|
this.manageSocket = true;
|
|
1778
|
-
this.configuration.websocketProvider = new
|
|
1839
|
+
this.configuration.websocketProvider = new AbracadabraWS(configuration);
|
|
1779
1840
|
}
|
|
1780
1841
|
this.configuration = {
|
|
1781
1842
|
...this.configuration,
|
|
@@ -1875,11 +1936,11 @@ var HocuspocusProvider = class extends EventEmitter {
|
|
|
1875
1936
|
}
|
|
1876
1937
|
async connect() {
|
|
1877
1938
|
if (this.manageSocket) return this.configuration.websocketProvider.connect();
|
|
1878
|
-
console.warn("
|
|
1939
|
+
console.warn("AbracadabraBaseProvider::connect() is deprecated and does not do anything. Please connect/disconnect on the websocketProvider, or attach/deattach providers.");
|
|
1879
1940
|
}
|
|
1880
1941
|
disconnect() {
|
|
1881
1942
|
if (this.manageSocket) return this.configuration.websocketProvider.disconnect();
|
|
1882
|
-
console.warn("
|
|
1943
|
+
console.warn("AbracadabraBaseProvider::disconnect() is deprecated and does not do anything. Please connect/disconnect on the websocketProvider, or attach/deattach providers.");
|
|
1883
1944
|
}
|
|
1884
1945
|
async onOpen(event) {
|
|
1885
1946
|
this.isAuthenticated = false;
|
|
@@ -1988,16 +2049,18 @@ var HocuspocusProvider = class extends EventEmitter {
|
|
|
1988
2049
|
this.awareness.setLocalStateField(key, value);
|
|
1989
2050
|
}
|
|
1990
2051
|
};
|
|
2052
|
+
/** @deprecated Use AbracadabraBaseProvider */
|
|
2053
|
+
const HocuspocusProvider = AbracadabraBaseProvider;
|
|
1991
2054
|
|
|
1992
2055
|
//#endregion
|
|
1993
2056
|
//#region packages/provider/src/OfflineStore.ts
|
|
1994
|
-
const DB_VERSION = 2;
|
|
1995
|
-
function idbAvailable() {
|
|
2057
|
+
const DB_VERSION$3 = 2;
|
|
2058
|
+
function idbAvailable$3() {
|
|
1996
2059
|
return typeof globalThis !== "undefined" && "indexedDB" in globalThis;
|
|
1997
2060
|
}
|
|
1998
|
-
function openDb$
|
|
2061
|
+
function openDb$4(storeKey) {
|
|
1999
2062
|
return new Promise((resolve, reject) => {
|
|
2000
|
-
const req = globalThis.indexedDB.open(`abracadabra:${storeKey}`, DB_VERSION);
|
|
2063
|
+
const req = globalThis.indexedDB.open(`abracadabra:${storeKey}`, DB_VERSION$3);
|
|
2001
2064
|
req.onupgradeneeded = (event) => {
|
|
2002
2065
|
const db = event.target.result;
|
|
2003
2066
|
if (!db.objectStoreNames.contains("updates")) db.createObjectStore("updates", { autoIncrement: true });
|
|
@@ -2009,7 +2072,7 @@ function openDb$1(storeKey) {
|
|
|
2009
2072
|
req.onerror = () => reject(req.error);
|
|
2010
2073
|
});
|
|
2011
2074
|
}
|
|
2012
|
-
function txPromise(store, request) {
|
|
2075
|
+
function txPromise$2(store, request) {
|
|
2013
2076
|
return new Promise((resolve, reject) => {
|
|
2014
2077
|
request.onsuccess = () => resolve(request.result);
|
|
2015
2078
|
request.onerror = () => reject(request.error);
|
|
@@ -2028,8 +2091,8 @@ var OfflineStore = class {
|
|
|
2028
2091
|
this.storeKey = serverOrigin ? `${serverOrigin}/${docId}` : docId;
|
|
2029
2092
|
}
|
|
2030
2093
|
getDb() {
|
|
2031
|
-
if (!idbAvailable()) return Promise.resolve(null);
|
|
2032
|
-
if (!this.dbPromise) this.dbPromise = openDb$
|
|
2094
|
+
if (!idbAvailable$3()) return Promise.resolve(null);
|
|
2095
|
+
if (!this.dbPromise) this.dbPromise = openDb$4(this.storeKey).catch(() => null).then((db) => {
|
|
2033
2096
|
this.db = db;
|
|
2034
2097
|
return db;
|
|
2035
2098
|
});
|
|
@@ -2039,7 +2102,7 @@ var OfflineStore = class {
|
|
|
2039
2102
|
const db = await this.getDb();
|
|
2040
2103
|
if (!db) return;
|
|
2041
2104
|
const store = db.transaction("updates", "readwrite").objectStore("updates");
|
|
2042
|
-
await txPromise(store, store.add(update));
|
|
2105
|
+
await txPromise$2(store, store.add(update));
|
|
2043
2106
|
}
|
|
2044
2107
|
async getPendingUpdates() {
|
|
2045
2108
|
const db = await this.getDb();
|
|
@@ -2054,7 +2117,7 @@ var OfflineStore = class {
|
|
|
2054
2117
|
const db = await this.getDb();
|
|
2055
2118
|
if (!db) return;
|
|
2056
2119
|
const tx = db.transaction("updates", "readwrite");
|
|
2057
|
-
await txPromise(tx.objectStore("updates"), tx.objectStore("updates").clear());
|
|
2120
|
+
await txPromise$2(tx.objectStore("updates"), tx.objectStore("updates").clear());
|
|
2058
2121
|
}
|
|
2059
2122
|
/**
|
|
2060
2123
|
* Persist a full Y.js state snapshot (Y.encodeStateAsUpdate output).
|
|
@@ -2064,7 +2127,7 @@ var OfflineStore = class {
|
|
|
2064
2127
|
const db = await this.getDb();
|
|
2065
2128
|
if (!db) return;
|
|
2066
2129
|
const tx = db.transaction("doc_state", "readwrite");
|
|
2067
|
-
await txPromise(tx.objectStore("doc_state"), tx.objectStore("doc_state").put(snapshot, "snapshot"));
|
|
2130
|
+
await txPromise$2(tx.objectStore("doc_state"), tx.objectStore("doc_state").put(snapshot, "snapshot"));
|
|
2068
2131
|
}
|
|
2069
2132
|
/**
|
|
2070
2133
|
* Retrieve the stored full document snapshot, or null if none exists.
|
|
@@ -2073,37 +2136,37 @@ var OfflineStore = class {
|
|
|
2073
2136
|
const db = await this.getDb();
|
|
2074
2137
|
if (!db) return null;
|
|
2075
2138
|
const tx = db.transaction("doc_state", "readonly");
|
|
2076
|
-
return await txPromise(tx.objectStore("doc_state"), tx.objectStore("doc_state").get("snapshot")) ?? null;
|
|
2139
|
+
return await txPromise$2(tx.objectStore("doc_state"), tx.objectStore("doc_state").get("snapshot")) ?? null;
|
|
2077
2140
|
}
|
|
2078
2141
|
async getStateVector() {
|
|
2079
2142
|
const db = await this.getDb();
|
|
2080
2143
|
if (!db) return null;
|
|
2081
2144
|
const tx = db.transaction("meta", "readonly");
|
|
2082
|
-
return await txPromise(tx.objectStore("meta"), tx.objectStore("meta").get("sv")) ?? null;
|
|
2145
|
+
return await txPromise$2(tx.objectStore("meta"), tx.objectStore("meta").get("sv")) ?? null;
|
|
2083
2146
|
}
|
|
2084
2147
|
async saveStateVector(sv) {
|
|
2085
2148
|
const db = await this.getDb();
|
|
2086
2149
|
if (!db) return;
|
|
2087
2150
|
const tx = db.transaction("meta", "readwrite");
|
|
2088
|
-
await txPromise(tx.objectStore("meta"), tx.objectStore("meta").put(sv, "sv"));
|
|
2151
|
+
await txPromise$2(tx.objectStore("meta"), tx.objectStore("meta").put(sv, "sv"));
|
|
2089
2152
|
}
|
|
2090
2153
|
async getPermissionSnapshot() {
|
|
2091
2154
|
const db = await this.getDb();
|
|
2092
2155
|
if (!db) return null;
|
|
2093
2156
|
const tx = db.transaction("meta", "readonly");
|
|
2094
|
-
return await txPromise(tx.objectStore("meta"), tx.objectStore("meta").get("role")) ?? null;
|
|
2157
|
+
return await txPromise$2(tx.objectStore("meta"), tx.objectStore("meta").get("role")) ?? null;
|
|
2095
2158
|
}
|
|
2096
2159
|
async savePermissionSnapshot(role) {
|
|
2097
2160
|
const db = await this.getDb();
|
|
2098
2161
|
if (!db) return;
|
|
2099
2162
|
const tx = db.transaction("meta", "readwrite");
|
|
2100
|
-
await txPromise(tx.objectStore("meta"), tx.objectStore("meta").put(role, "role"));
|
|
2163
|
+
await txPromise$2(tx.objectStore("meta"), tx.objectStore("meta").put(role, "role"));
|
|
2101
2164
|
}
|
|
2102
2165
|
async queueSubdoc(entry) {
|
|
2103
2166
|
const db = await this.getDb();
|
|
2104
2167
|
if (!db) return;
|
|
2105
2168
|
const tx = db.transaction("subdoc_queue", "readwrite");
|
|
2106
|
-
await txPromise(tx.objectStore("subdoc_queue"), tx.objectStore("subdoc_queue").put(entry));
|
|
2169
|
+
await txPromise$2(tx.objectStore("subdoc_queue"), tx.objectStore("subdoc_queue").put(entry));
|
|
2107
2170
|
}
|
|
2108
2171
|
async getPendingSubdocs() {
|
|
2109
2172
|
const db = await this.getDb();
|
|
@@ -2118,7 +2181,7 @@ var OfflineStore = class {
|
|
|
2118
2181
|
const db = await this.getDb();
|
|
2119
2182
|
if (!db) return;
|
|
2120
2183
|
const tx = db.transaction("subdoc_queue", "readwrite");
|
|
2121
|
-
await txPromise(tx.objectStore("subdoc_queue"), tx.objectStore("subdoc_queue").delete(childId));
|
|
2184
|
+
await txPromise$2(tx.objectStore("subdoc_queue"), tx.objectStore("subdoc_queue").delete(childId));
|
|
2122
2185
|
}
|
|
2123
2186
|
destroy() {
|
|
2124
2187
|
this.db?.close();
|
|
@@ -2161,7 +2224,7 @@ function isValidDocId(id) {
|
|
|
2161
2224
|
return /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(id);
|
|
2162
2225
|
}
|
|
2163
2226
|
/**
|
|
2164
|
-
* AbracadabraProvider extends
|
|
2227
|
+
* AbracadabraProvider extends AbracadabraBaseProvider with:
|
|
2165
2228
|
*
|
|
2166
2229
|
* 1. Subdocument lifecycle – intercepts Y.Doc subdoc events and syncs them
|
|
2167
2230
|
* with the server via MSG_SUBDOC (4) frames. Child documents get their
|
|
@@ -2181,7 +2244,7 @@ function isValidDocId(id) {
|
|
|
2181
2244
|
* can gate write operations without a network round-trip. Role is
|
|
2182
2245
|
* refreshed from the server on every reconnect.
|
|
2183
2246
|
*/
|
|
2184
|
-
var AbracadabraProvider = class AbracadabraProvider extends
|
|
2247
|
+
var AbracadabraProvider = class AbracadabraProvider extends AbracadabraBaseProvider {
|
|
2185
2248
|
constructor(configuration) {
|
|
2186
2249
|
const resolved = { ...configuration };
|
|
2187
2250
|
const client = configuration.client ?? null;
|
|
@@ -2462,6 +2525,7 @@ var AbracadabraClient = class {
|
|
|
2462
2525
|
this.persistAuth = config.persistAuth ?? typeof localStorage !== "undefined";
|
|
2463
2526
|
this.storageKey = config.storageKey ?? "abracadabra:auth";
|
|
2464
2527
|
this._fetch = config.fetch ?? globalThis.fetch.bind(globalThis);
|
|
2528
|
+
this.cache = config.cache ?? null;
|
|
2465
2529
|
this._token = config.token ?? this.loadPersistedToken() ?? null;
|
|
2466
2530
|
}
|
|
2467
2531
|
get token() {
|
|
@@ -2498,7 +2562,8 @@ var AbracadabraClient = class {
|
|
|
2498
2562
|
identityPublicKey: opts.publicKey,
|
|
2499
2563
|
deviceName: opts.deviceName,
|
|
2500
2564
|
displayName: opts.displayName,
|
|
2501
|
-
email: opts.email
|
|
2565
|
+
email: opts.email,
|
|
2566
|
+
inviteCode: opts.inviteCode
|
|
2502
2567
|
},
|
|
2503
2568
|
auth: false
|
|
2504
2569
|
});
|
|
@@ -2559,7 +2624,13 @@ var AbracadabraClient = class {
|
|
|
2559
2624
|
}
|
|
2560
2625
|
/** Get the current user's profile. */
|
|
2561
2626
|
async getMe() {
|
|
2562
|
-
|
|
2627
|
+
if (this.cache) {
|
|
2628
|
+
const cached = await this.cache.getCurrentProfile();
|
|
2629
|
+
if (cached) return cached;
|
|
2630
|
+
}
|
|
2631
|
+
const profile = await this.request("GET", "/users/me");
|
|
2632
|
+
if (this.cache) await this.cache.setCurrentProfile(profile).catch(() => null);
|
|
2633
|
+
return profile;
|
|
2563
2634
|
}
|
|
2564
2635
|
/** Update the current user's display name. */
|
|
2565
2636
|
async updateMe(opts) {
|
|
@@ -2571,15 +2642,28 @@ var AbracadabraClient = class {
|
|
|
2571
2642
|
}
|
|
2572
2643
|
/** Get document metadata. */
|
|
2573
2644
|
async getDoc(docId) {
|
|
2574
|
-
|
|
2645
|
+
if (this.cache) {
|
|
2646
|
+
const cached = await this.cache.getDoc(docId);
|
|
2647
|
+
if (cached) return cached;
|
|
2648
|
+
}
|
|
2649
|
+
const meta = await this.request("GET", `/docs/${encodeURIComponent(docId)}`);
|
|
2650
|
+
if (this.cache) await this.cache.setDoc(meta).catch(() => null);
|
|
2651
|
+
return meta;
|
|
2575
2652
|
}
|
|
2576
2653
|
/** Delete a document (requires Owner role). Cascades to children and uploads. */
|
|
2577
2654
|
async deleteDoc(docId) {
|
|
2578
2655
|
await this.request("DELETE", `/docs/${encodeURIComponent(docId)}`);
|
|
2656
|
+
if (this.cache) await this.cache.invalidateDoc(docId).catch(() => null);
|
|
2579
2657
|
}
|
|
2580
2658
|
/** List immediate child documents. */
|
|
2581
2659
|
async listChildren(docId) {
|
|
2582
|
-
|
|
2660
|
+
if (this.cache) {
|
|
2661
|
+
const cached = await this.cache.getChildren(docId);
|
|
2662
|
+
if (cached) return cached;
|
|
2663
|
+
}
|
|
2664
|
+
const res = await this.request("GET", `/docs/${encodeURIComponent(docId)}/children`);
|
|
2665
|
+
if (this.cache) await this.cache.setChildren(docId, res.children).catch(() => null);
|
|
2666
|
+
return res.children;
|
|
2583
2667
|
}
|
|
2584
2668
|
/** Create a child document under a parent (requires write permission). */
|
|
2585
2669
|
async createChild(docId, opts) {
|
|
@@ -2587,7 +2671,13 @@ var AbracadabraClient = class {
|
|
|
2587
2671
|
}
|
|
2588
2672
|
/** List all permissions for a document (requires read access). */
|
|
2589
2673
|
async listPermissions(docId) {
|
|
2590
|
-
|
|
2674
|
+
if (this.cache) {
|
|
2675
|
+
const cached = await this.cache.getPermissions(docId);
|
|
2676
|
+
if (cached) return cached;
|
|
2677
|
+
}
|
|
2678
|
+
const res = await this.request("GET", `/docs/${encodeURIComponent(docId)}/permissions`);
|
|
2679
|
+
if (this.cache) await this.cache.setPermissions(docId, res.permissions).catch(() => null);
|
|
2680
|
+
return res.permissions;
|
|
2591
2681
|
}
|
|
2592
2682
|
/** Grant or change a user's role on a document (requires Owner). */
|
|
2593
2683
|
async setPermission(docId, opts) {
|
|
@@ -2609,11 +2699,19 @@ var AbracadabraClient = class {
|
|
|
2609
2699
|
body: formData
|
|
2610
2700
|
});
|
|
2611
2701
|
if (!res.ok) throw await this.toError(res);
|
|
2612
|
-
|
|
2702
|
+
const meta = await res.json();
|
|
2703
|
+
if (this.cache) await this.cache.invalidateUploads(docId).catch(() => null);
|
|
2704
|
+
return meta;
|
|
2613
2705
|
}
|
|
2614
2706
|
/** List all uploads for a document. */
|
|
2615
2707
|
async listUploads(docId) {
|
|
2616
|
-
|
|
2708
|
+
if (this.cache) {
|
|
2709
|
+
const cached = await this.cache.getUploads(docId);
|
|
2710
|
+
if (cached) return cached;
|
|
2711
|
+
}
|
|
2712
|
+
const res = await this.request("GET", `/docs/${encodeURIComponent(docId)}/uploads`);
|
|
2713
|
+
if (this.cache) await this.cache.setUploads(docId, res.uploads).catch(() => null);
|
|
2714
|
+
return res.uploads;
|
|
2617
2715
|
}
|
|
2618
2716
|
/** Download an upload as a Blob. */
|
|
2619
2717
|
async getUpload(docId, uploadId) {
|
|
@@ -2629,11 +2727,35 @@ var AbracadabraClient = class {
|
|
|
2629
2727
|
/** Delete an upload (requires uploader or document Owner). */
|
|
2630
2728
|
async deleteUpload(docId, uploadId) {
|
|
2631
2729
|
await this.request("DELETE", `/docs/${encodeURIComponent(docId)}/uploads/${encodeURIComponent(uploadId)}`);
|
|
2730
|
+
if (this.cache) await this.cache.invalidateUploads(docId).catch(() => null);
|
|
2731
|
+
}
|
|
2732
|
+
/** Create an invite code (requires permission per server config). */
|
|
2733
|
+
async createInvite(opts) {
|
|
2734
|
+
return this.request("POST", "/invites", { body: opts ?? {} });
|
|
2735
|
+
}
|
|
2736
|
+
/** List invite codes visible to the current user. */
|
|
2737
|
+
async listInvites() {
|
|
2738
|
+
return (await this.request("GET", "/invites")).invites;
|
|
2739
|
+
}
|
|
2740
|
+
/** Revoke an invite by its code. */
|
|
2741
|
+
async revokeInvite(code) {
|
|
2742
|
+
await this.request("DELETE", `/invites/${encodeURIComponent(code)}`);
|
|
2743
|
+
}
|
|
2744
|
+
/** Redeem an invite code for the currently authenticated user. */
|
|
2745
|
+
async redeemInvite(code) {
|
|
2746
|
+
await this.request("POST", "/invites/redeem", { body: { code } });
|
|
2632
2747
|
}
|
|
2633
2748
|
/** Health check — no auth required. */
|
|
2634
2749
|
async health() {
|
|
2635
2750
|
return this.request("GET", "/health", { auth: false });
|
|
2636
2751
|
}
|
|
2752
|
+
/**
|
|
2753
|
+
* Fetch server metadata including the optional `index_doc_id` entry point.
|
|
2754
|
+
* No auth required.
|
|
2755
|
+
*/
|
|
2756
|
+
async serverInfo() {
|
|
2757
|
+
return this.request("GET", "/info", { auth: false });
|
|
2758
|
+
}
|
|
2637
2759
|
async request(method, path, opts) {
|
|
2638
2760
|
const auth = opts?.auth ?? true;
|
|
2639
2761
|
const headers = {};
|
|
@@ -2681,6 +2803,49 @@ var AbracadabraClient = class {
|
|
|
2681
2803
|
}
|
|
2682
2804
|
};
|
|
2683
2805
|
|
|
2806
|
+
//#endregion
|
|
2807
|
+
//#region packages/provider/src/CloseEvents.ts
|
|
2808
|
+
/**
|
|
2809
|
+
* The server is terminating the connection because a data frame was received
|
|
2810
|
+
* that is too large.
|
|
2811
|
+
* See: https://developer.mozilla.org/en-US/docs/Web/API/CloseEvent/code
|
|
2812
|
+
*/
|
|
2813
|
+
const MessageTooBig = {
|
|
2814
|
+
code: 1009,
|
|
2815
|
+
reason: "Message Too Big"
|
|
2816
|
+
};
|
|
2817
|
+
/**
|
|
2818
|
+
* The server successfully processed the request, asks that the requester reset
|
|
2819
|
+
* its document view, and is not returning any content.
|
|
2820
|
+
*/
|
|
2821
|
+
const ResetConnection = {
|
|
2822
|
+
code: 4205,
|
|
2823
|
+
reason: "Reset Connection"
|
|
2824
|
+
};
|
|
2825
|
+
/**
|
|
2826
|
+
* Similar to Forbidden, but specifically for use when authentication is required and has
|
|
2827
|
+
* failed or has not yet been provided.
|
|
2828
|
+
*/
|
|
2829
|
+
const Unauthorized = {
|
|
2830
|
+
code: 4401,
|
|
2831
|
+
reason: "Unauthorized"
|
|
2832
|
+
};
|
|
2833
|
+
/**
|
|
2834
|
+
* The request contained valid data and was understood by the server, but the server
|
|
2835
|
+
* is refusing action.
|
|
2836
|
+
*/
|
|
2837
|
+
const Forbidden = {
|
|
2838
|
+
code: 4403,
|
|
2839
|
+
reason: "Forbidden"
|
|
2840
|
+
};
|
|
2841
|
+
/**
|
|
2842
|
+
* The server timed out waiting for the request.
|
|
2843
|
+
*/
|
|
2844
|
+
const ConnectionTimeout = {
|
|
2845
|
+
code: 4408,
|
|
2846
|
+
reason: "Connection Timeout"
|
|
2847
|
+
};
|
|
2848
|
+
|
|
2684
2849
|
//#endregion
|
|
2685
2850
|
//#region node_modules/@noble/hashes/esm/utils.js
|
|
2686
2851
|
/** Checks if something is Uint8Array. Be careful: nodejs Buffer will return true. */
|
|
@@ -3358,7 +3523,7 @@ const DB_NAME = "abracadabra:identity";
|
|
|
3358
3523
|
const STORE_NAME = "identity";
|
|
3359
3524
|
const RECORD_KEY = "current";
|
|
3360
3525
|
const HKDF_INFO = new TextEncoder().encode("abracadabra-identity-v1");
|
|
3361
|
-
function openDb() {
|
|
3526
|
+
function openDb$3() {
|
|
3362
3527
|
return new Promise((resolve, reject) => {
|
|
3363
3528
|
const req = indexedDB.open(DB_NAME, 1);
|
|
3364
3529
|
req.onupgradeneeded = () => {
|
|
@@ -3441,7 +3606,7 @@ var CryptoIdentityKeystore = class {
|
|
|
3441
3606
|
name: "AES-GCM",
|
|
3442
3607
|
iv
|
|
3443
3608
|
}, aesKey, privateKey);
|
|
3444
|
-
const db = await openDb();
|
|
3609
|
+
const db = await openDb$3();
|
|
3445
3610
|
await dbPut(db, {
|
|
3446
3611
|
username,
|
|
3447
3612
|
publicKey: toBase64url(publicKey),
|
|
@@ -3464,7 +3629,7 @@ var CryptoIdentityKeystore = class {
|
|
|
3464
3629
|
* @returns base64url-encoded Ed25519 signature (64 bytes).
|
|
3465
3630
|
*/
|
|
3466
3631
|
async sign(challengeB64) {
|
|
3467
|
-
const db = await openDb();
|
|
3632
|
+
const db = await openDb$3();
|
|
3468
3633
|
const stored = await dbGet(db);
|
|
3469
3634
|
db.close();
|
|
3470
3635
|
if (!stored) throw new Error("No identity stored. Call register() first.");
|
|
@@ -3493,7 +3658,7 @@ var CryptoIdentityKeystore = class {
|
|
|
3493
3658
|
}
|
|
3494
3659
|
/** Returns the stored base64url public key, or null if no identity exists. */
|
|
3495
3660
|
async getPublicKey() {
|
|
3496
|
-
const db = await openDb();
|
|
3661
|
+
const db = await openDb$3();
|
|
3497
3662
|
const stored = await dbGet(db);
|
|
3498
3663
|
db.close();
|
|
3499
3664
|
return stored?.publicKey ?? null;
|
|
@@ -3506,35 +3671,551 @@ var CryptoIdentityKeystore = class {
|
|
|
3506
3671
|
* a real display name via PATCH /users/me.
|
|
3507
3672
|
*/
|
|
3508
3673
|
async getUsername() {
|
|
3509
|
-
const db = await openDb();
|
|
3674
|
+
const db = await openDb$3();
|
|
3510
3675
|
const stored = await dbGet(db);
|
|
3511
3676
|
db.close();
|
|
3512
3677
|
return stored?.username ?? null;
|
|
3513
3678
|
}
|
|
3514
3679
|
/** Returns true if an identity is stored in IndexedDB. */
|
|
3515
3680
|
async hasIdentity() {
|
|
3516
|
-
const db = await openDb();
|
|
3681
|
+
const db = await openDb$3();
|
|
3517
3682
|
const stored = await dbGet(db);
|
|
3518
3683
|
db.close();
|
|
3519
3684
|
return stored !== void 0;
|
|
3520
3685
|
}
|
|
3521
3686
|
/** Remove the stored identity from IndexedDB. */
|
|
3522
3687
|
async clear() {
|
|
3523
|
-
const db = await openDb();
|
|
3688
|
+
const db = await openDb$3();
|
|
3524
3689
|
await dbDelete(db);
|
|
3525
3690
|
db.close();
|
|
3526
3691
|
}
|
|
3527
3692
|
};
|
|
3528
3693
|
|
|
3529
3694
|
//#endregion
|
|
3695
|
+
//#region packages/provider/src/DocumentCache.ts
|
|
3696
|
+
const DB_VERSION$2 = 1;
|
|
3697
|
+
const DEFAULT_TTL_MS = 300 * 1e3;
|
|
3698
|
+
function idbAvailable$2() {
|
|
3699
|
+
return typeof globalThis !== "undefined" && "indexedDB" in globalThis;
|
|
3700
|
+
}
|
|
3701
|
+
function openDb$2(origin) {
|
|
3702
|
+
return new Promise((resolve, reject) => {
|
|
3703
|
+
const req = globalThis.indexedDB.open(`abracadabra:meta-cache:${origin}`, DB_VERSION$2);
|
|
3704
|
+
req.onupgradeneeded = (event) => {
|
|
3705
|
+
const db = event.target.result;
|
|
3706
|
+
for (const name of [
|
|
3707
|
+
"doc_meta",
|
|
3708
|
+
"children",
|
|
3709
|
+
"user_profile",
|
|
3710
|
+
"permissions",
|
|
3711
|
+
"uploads"
|
|
3712
|
+
]) if (!db.objectStoreNames.contains(name)) db.createObjectStore(name);
|
|
3713
|
+
};
|
|
3714
|
+
req.onsuccess = () => resolve(req.result);
|
|
3715
|
+
req.onerror = () => reject(req.error);
|
|
3716
|
+
});
|
|
3717
|
+
}
|
|
3718
|
+
function txPromise$1(store, request) {
|
|
3719
|
+
return new Promise((resolve, reject) => {
|
|
3720
|
+
request.onsuccess = () => resolve(request.result);
|
|
3721
|
+
request.onerror = () => reject(request.error);
|
|
3722
|
+
});
|
|
3723
|
+
}
|
|
3724
|
+
var DocumentCache = class {
|
|
3725
|
+
constructor(serverOrigin, opts) {
|
|
3726
|
+
this.dbPromise = null;
|
|
3727
|
+
this.db = null;
|
|
3728
|
+
this.origin = serverOrigin;
|
|
3729
|
+
this.ttlMs = opts?.ttlMs ?? DEFAULT_TTL_MS;
|
|
3730
|
+
}
|
|
3731
|
+
getDb() {
|
|
3732
|
+
if (!idbAvailable$2()) return Promise.resolve(null);
|
|
3733
|
+
if (!this.dbPromise) this.dbPromise = openDb$2(this.origin).catch(() => null).then((db) => {
|
|
3734
|
+
this.db = db;
|
|
3735
|
+
return db;
|
|
3736
|
+
});
|
|
3737
|
+
return this.dbPromise;
|
|
3738
|
+
}
|
|
3739
|
+
isExpired(cachedAt) {
|
|
3740
|
+
return Date.now() - cachedAt > this.ttlMs;
|
|
3741
|
+
}
|
|
3742
|
+
async getWithTtl(storeName, key) {
|
|
3743
|
+
const db = await this.getDb();
|
|
3744
|
+
if (!db) return null;
|
|
3745
|
+
const tx = db.transaction(storeName, "readonly");
|
|
3746
|
+
const result = await txPromise$1(tx.objectStore(storeName), tx.objectStore(storeName).get(key));
|
|
3747
|
+
if (!result) return null;
|
|
3748
|
+
if (this.isExpired(result.cachedAt)) {
|
|
3749
|
+
this.deleteKey(storeName, key).catch(() => null);
|
|
3750
|
+
return null;
|
|
3751
|
+
}
|
|
3752
|
+
return result.value;
|
|
3753
|
+
}
|
|
3754
|
+
async setWithTtl(storeName, key, value) {
|
|
3755
|
+
const db = await this.getDb();
|
|
3756
|
+
if (!db) return;
|
|
3757
|
+
const entry = {
|
|
3758
|
+
value,
|
|
3759
|
+
cachedAt: Date.now()
|
|
3760
|
+
};
|
|
3761
|
+
const tx = db.transaction(storeName, "readwrite");
|
|
3762
|
+
await txPromise$1(tx.objectStore(storeName), tx.objectStore(storeName).put(entry, key));
|
|
3763
|
+
}
|
|
3764
|
+
async deleteKey(storeName, key) {
|
|
3765
|
+
const db = await this.getDb();
|
|
3766
|
+
if (!db) return;
|
|
3767
|
+
const tx = db.transaction(storeName, "readwrite");
|
|
3768
|
+
await txPromise$1(tx.objectStore(storeName), tx.objectStore(storeName).delete(key));
|
|
3769
|
+
}
|
|
3770
|
+
async getDoc(docId) {
|
|
3771
|
+
return this.getWithTtl("doc_meta", docId);
|
|
3772
|
+
}
|
|
3773
|
+
async setDoc(meta) {
|
|
3774
|
+
return this.setWithTtl("doc_meta", meta.id, meta);
|
|
3775
|
+
}
|
|
3776
|
+
async invalidateDoc(docId) {
|
|
3777
|
+
await this.deleteKey("doc_meta", docId).catch(() => null);
|
|
3778
|
+
}
|
|
3779
|
+
async getChildren(parentId) {
|
|
3780
|
+
return this.getWithTtl("children", parentId);
|
|
3781
|
+
}
|
|
3782
|
+
async setChildren(parentId, items) {
|
|
3783
|
+
return this.setWithTtl("children", parentId, items);
|
|
3784
|
+
}
|
|
3785
|
+
async invalidateChildren(parentId) {
|
|
3786
|
+
await this.deleteKey("children", parentId).catch(() => null);
|
|
3787
|
+
}
|
|
3788
|
+
async getProfile(userId) {
|
|
3789
|
+
return this.getWithTtl("user_profile", userId);
|
|
3790
|
+
}
|
|
3791
|
+
async setProfile(profile) {
|
|
3792
|
+
return this.setWithTtl("user_profile", profile.id, profile);
|
|
3793
|
+
}
|
|
3794
|
+
/** Get the cached profile for the currently authenticated user. */
|
|
3795
|
+
async getCurrentProfile() {
|
|
3796
|
+
return this.getWithTtl("user_profile", "__current__");
|
|
3797
|
+
}
|
|
3798
|
+
/** Cache a profile both by its ID and as the current user. */
|
|
3799
|
+
async setCurrentProfile(profile) {
|
|
3800
|
+
await Promise.all([this.setWithTtl("user_profile", profile.id, profile), this.setWithTtl("user_profile", "__current__", profile)]);
|
|
3801
|
+
}
|
|
3802
|
+
async getPermissions(docId) {
|
|
3803
|
+
return this.getWithTtl("permissions", docId);
|
|
3804
|
+
}
|
|
3805
|
+
async setPermissions(docId, items) {
|
|
3806
|
+
return this.setWithTtl("permissions", docId, items);
|
|
3807
|
+
}
|
|
3808
|
+
async getUploads(docId) {
|
|
3809
|
+
return this.getWithTtl("uploads", docId);
|
|
3810
|
+
}
|
|
3811
|
+
async setUploads(docId, items) {
|
|
3812
|
+
return this.setWithTtl("uploads", docId, items);
|
|
3813
|
+
}
|
|
3814
|
+
async invalidateUploads(docId) {
|
|
3815
|
+
await this.deleteKey("uploads", docId).catch(() => null);
|
|
3816
|
+
}
|
|
3817
|
+
destroy() {
|
|
3818
|
+
this.db?.close();
|
|
3819
|
+
this.db = null;
|
|
3820
|
+
}
|
|
3821
|
+
};
|
|
3822
|
+
|
|
3823
|
+
//#endregion
|
|
3824
|
+
//#region packages/provider/src/SearchIndex.ts
|
|
3825
|
+
const DB_VERSION$1 = 1;
|
|
3826
|
+
function idbAvailable$1() {
|
|
3827
|
+
return typeof globalThis !== "undefined" && "indexedDB" in globalThis;
|
|
3828
|
+
}
|
|
3829
|
+
function openDb$1(origin) {
|
|
3830
|
+
return new Promise((resolve, reject) => {
|
|
3831
|
+
const req = globalThis.indexedDB.open(`abracadabra:search:${origin}`, DB_VERSION$1);
|
|
3832
|
+
req.onupgradeneeded = (event) => {
|
|
3833
|
+
const db = event.target.result;
|
|
3834
|
+
if (!db.objectStoreNames.contains("postings")) db.createObjectStore("postings");
|
|
3835
|
+
if (!db.objectStoreNames.contains("doc_trigrams")) db.createObjectStore("doc_trigrams");
|
|
3836
|
+
};
|
|
3837
|
+
req.onsuccess = () => resolve(req.result);
|
|
3838
|
+
req.onerror = () => reject(req.error);
|
|
3839
|
+
});
|
|
3840
|
+
}
|
|
3841
|
+
/** Extract the set of trigrams for a piece of text. */
|
|
3842
|
+
function extractTrigrams(text) {
|
|
3843
|
+
const trigrams = /* @__PURE__ */ new Set();
|
|
3844
|
+
const padded = ` ${text.toLowerCase()} `;
|
|
3845
|
+
for (let i = 0; i <= padded.length - 3; i++) trigrams.add(padded.slice(i, i + 3));
|
|
3846
|
+
return trigrams;
|
|
3847
|
+
}
|
|
3848
|
+
/** Merge trigrams from multiple texts into a single set. */
|
|
3849
|
+
function extractAllTrigrams(texts) {
|
|
3850
|
+
const result = /* @__PURE__ */ new Set();
|
|
3851
|
+
for (const t of texts) for (const trigram of extractTrigrams(t)) result.add(trigram);
|
|
3852
|
+
return result;
|
|
3853
|
+
}
|
|
3854
|
+
var SearchIndex = class {
|
|
3855
|
+
constructor(serverOrigin) {
|
|
3856
|
+
this.dbPromise = null;
|
|
3857
|
+
this.db = null;
|
|
3858
|
+
this.origin = serverOrigin;
|
|
3859
|
+
}
|
|
3860
|
+
getDb() {
|
|
3861
|
+
if (!idbAvailable$1()) return Promise.resolve(null);
|
|
3862
|
+
if (!this.dbPromise) this.dbPromise = openDb$1(this.origin).catch(() => null).then((db) => {
|
|
3863
|
+
this.db = db;
|
|
3864
|
+
return db;
|
|
3865
|
+
});
|
|
3866
|
+
return this.dbPromise;
|
|
3867
|
+
}
|
|
3868
|
+
/**
|
|
3869
|
+
* Replace the index for docId with the given texts.
|
|
3870
|
+
* Old trigram associations are removed before new ones are added.
|
|
3871
|
+
*/
|
|
3872
|
+
async index(docId, texts) {
|
|
3873
|
+
const db = await this.getDb();
|
|
3874
|
+
if (!db) return;
|
|
3875
|
+
const newTrigrams = extractAllTrigrams(texts);
|
|
3876
|
+
return new Promise((resolve, reject) => {
|
|
3877
|
+
const tx = db.transaction(["postings", "doc_trigrams"], "readwrite");
|
|
3878
|
+
tx.oncomplete = () => resolve();
|
|
3879
|
+
tx.onerror = () => reject(tx.error);
|
|
3880
|
+
const postings = tx.objectStore("postings");
|
|
3881
|
+
const docTrigramsStore = tx.objectStore("doc_trigrams");
|
|
3882
|
+
const oldReq = docTrigramsStore.get(docId);
|
|
3883
|
+
oldReq.onsuccess = () => {
|
|
3884
|
+
const oldTrigrams = oldReq.result ?? [];
|
|
3885
|
+
let pending = oldTrigrams.length + newTrigrams.size + 1;
|
|
3886
|
+
function done() {
|
|
3887
|
+
pending--;
|
|
3888
|
+
}
|
|
3889
|
+
for (const trigram of oldTrigrams) {
|
|
3890
|
+
const req = postings.get(trigram);
|
|
3891
|
+
req.onsuccess = () => {
|
|
3892
|
+
const updated = (req.result ?? []).filter((id) => id !== docId);
|
|
3893
|
+
if (updated.length === 0) postings.delete(trigram);
|
|
3894
|
+
else postings.put(updated, trigram);
|
|
3895
|
+
done();
|
|
3896
|
+
};
|
|
3897
|
+
req.onerror = done;
|
|
3898
|
+
}
|
|
3899
|
+
for (const trigram of newTrigrams) {
|
|
3900
|
+
const req = postings.get(trigram);
|
|
3901
|
+
req.onsuccess = () => {
|
|
3902
|
+
const list = req.result ?? [];
|
|
3903
|
+
if (!list.includes(docId)) list.push(docId);
|
|
3904
|
+
postings.put(list, trigram);
|
|
3905
|
+
done();
|
|
3906
|
+
};
|
|
3907
|
+
req.onerror = done;
|
|
3908
|
+
}
|
|
3909
|
+
const writeReq = docTrigramsStore.put([...newTrigrams], docId);
|
|
3910
|
+
writeReq.onsuccess = done;
|
|
3911
|
+
writeReq.onerror = done;
|
|
3912
|
+
};
|
|
3913
|
+
oldReq.onerror = () => reject(oldReq.error);
|
|
3914
|
+
});
|
|
3915
|
+
}
|
|
3916
|
+
/** Remove all indexed content for a document. */
|
|
3917
|
+
async remove(docId) {
|
|
3918
|
+
const db = await this.getDb();
|
|
3919
|
+
if (!db) return;
|
|
3920
|
+
return new Promise((resolve, reject) => {
|
|
3921
|
+
const tx = db.transaction(["postings", "doc_trigrams"], "readwrite");
|
|
3922
|
+
tx.oncomplete = () => resolve();
|
|
3923
|
+
tx.onerror = () => reject(tx.error);
|
|
3924
|
+
const postings = tx.objectStore("postings");
|
|
3925
|
+
const docTrigramsStore = tx.objectStore("doc_trigrams");
|
|
3926
|
+
const oldReq = docTrigramsStore.get(docId);
|
|
3927
|
+
oldReq.onsuccess = () => {
|
|
3928
|
+
const oldTrigrams = oldReq.result ?? [];
|
|
3929
|
+
for (const trigram of oldTrigrams) {
|
|
3930
|
+
const req = postings.get(trigram);
|
|
3931
|
+
req.onsuccess = () => {
|
|
3932
|
+
const updated = (req.result ?? []).filter((id) => id !== docId);
|
|
3933
|
+
if (updated.length === 0) postings.delete(trigram);
|
|
3934
|
+
else postings.put(updated, trigram);
|
|
3935
|
+
};
|
|
3936
|
+
}
|
|
3937
|
+
docTrigramsStore.delete(docId);
|
|
3938
|
+
};
|
|
3939
|
+
oldReq.onerror = () => reject(oldReq.error);
|
|
3940
|
+
});
|
|
3941
|
+
}
|
|
3942
|
+
/**
|
|
3943
|
+
* Search for documents matching the query.
|
|
3944
|
+
* Returns results sorted by score (matching trigram count) descending.
|
|
3945
|
+
*/
|
|
3946
|
+
async search(query, limit = 20) {
|
|
3947
|
+
const db = await this.getDb();
|
|
3948
|
+
if (!db) return [];
|
|
3949
|
+
const queryTrigrams = [...extractTrigrams(query)];
|
|
3950
|
+
if (queryTrigrams.length === 0) return [];
|
|
3951
|
+
return new Promise((resolve, reject) => {
|
|
3952
|
+
const tx = db.transaction("postings", "readonly");
|
|
3953
|
+
const postings = tx.objectStore("postings");
|
|
3954
|
+
const scores = /* @__PURE__ */ new Map();
|
|
3955
|
+
let remaining = queryTrigrams.length;
|
|
3956
|
+
for (const trigram of queryTrigrams) {
|
|
3957
|
+
const req = postings.get(trigram);
|
|
3958
|
+
req.onsuccess = () => {
|
|
3959
|
+
const docIds = req.result ?? [];
|
|
3960
|
+
for (const docId of docIds) scores.set(docId, (scores.get(docId) ?? 0) + 1);
|
|
3961
|
+
remaining--;
|
|
3962
|
+
if (remaining === 0) resolve([...scores.entries()].map(([docId, score]) => ({
|
|
3963
|
+
docId,
|
|
3964
|
+
score
|
|
3965
|
+
})).sort((a, b) => b.score - a.score).slice(0, limit));
|
|
3966
|
+
};
|
|
3967
|
+
req.onerror = () => {
|
|
3968
|
+
remaining--;
|
|
3969
|
+
if (remaining === 0) resolve([...scores.entries()].map(([docId, score]) => ({
|
|
3970
|
+
docId,
|
|
3971
|
+
score
|
|
3972
|
+
})).sort((a, b) => b.score - a.score).slice(0, limit));
|
|
3973
|
+
};
|
|
3974
|
+
}
|
|
3975
|
+
tx.onerror = () => reject(tx.error);
|
|
3976
|
+
});
|
|
3977
|
+
}
|
|
3978
|
+
destroy() {
|
|
3979
|
+
this.db?.close();
|
|
3980
|
+
this.db = null;
|
|
3981
|
+
}
|
|
3982
|
+
};
|
|
3983
|
+
|
|
3984
|
+
//#endregion
|
|
3985
|
+
//#region packages/provider/src/FileBlobStore.ts
|
|
3986
|
+
const DB_VERSION = 1;
|
|
3987
|
+
function idbAvailable() {
|
|
3988
|
+
return typeof globalThis !== "undefined" && "indexedDB" in globalThis;
|
|
3989
|
+
}
|
|
3990
|
+
function openDb(origin) {
|
|
3991
|
+
return new Promise((resolve, reject) => {
|
|
3992
|
+
const req = globalThis.indexedDB.open(`abracadabra:files:${origin}`, DB_VERSION);
|
|
3993
|
+
req.onupgradeneeded = (event) => {
|
|
3994
|
+
const db = event.target.result;
|
|
3995
|
+
if (!db.objectStoreNames.contains("blobs")) db.createObjectStore("blobs");
|
|
3996
|
+
if (!db.objectStoreNames.contains("upload_queue")) db.createObjectStore("upload_queue", { keyPath: "id" });
|
|
3997
|
+
};
|
|
3998
|
+
req.onsuccess = () => resolve(req.result);
|
|
3999
|
+
req.onerror = () => reject(req.error);
|
|
4000
|
+
});
|
|
4001
|
+
}
|
|
4002
|
+
function txPromise(store, request) {
|
|
4003
|
+
return new Promise((resolve, reject) => {
|
|
4004
|
+
request.onsuccess = () => resolve(request.result);
|
|
4005
|
+
request.onerror = () => reject(request.error);
|
|
4006
|
+
});
|
|
4007
|
+
}
|
|
4008
|
+
var FileBlobStore = class extends EventEmitter {
|
|
4009
|
+
constructor(serverOrigin, client) {
|
|
4010
|
+
super();
|
|
4011
|
+
this.dbPromise = null;
|
|
4012
|
+
this.db = null;
|
|
4013
|
+
this.objectUrls = /* @__PURE__ */ new Map();
|
|
4014
|
+
this._flushing = false;
|
|
4015
|
+
this.origin = serverOrigin;
|
|
4016
|
+
this.client = client;
|
|
4017
|
+
this._onlineHandler = () => {
|
|
4018
|
+
this.flushQueue().catch(() => null);
|
|
4019
|
+
};
|
|
4020
|
+
if (typeof window !== "undefined") window.addEventListener("online", this._onlineHandler);
|
|
4021
|
+
}
|
|
4022
|
+
getDb() {
|
|
4023
|
+
if (!idbAvailable()) return Promise.resolve(null);
|
|
4024
|
+
if (!this.dbPromise) this.dbPromise = openDb(this.origin).catch(() => null).then((db) => {
|
|
4025
|
+
this.db = db;
|
|
4026
|
+
return db;
|
|
4027
|
+
});
|
|
4028
|
+
return this.dbPromise;
|
|
4029
|
+
}
|
|
4030
|
+
blobKey(docId, uploadId) {
|
|
4031
|
+
return `${docId}/${uploadId}`;
|
|
4032
|
+
}
|
|
4033
|
+
/**
|
|
4034
|
+
* Return a local object URL for a file.
|
|
4035
|
+
* On first call the blob is downloaded from the server and cached in IDB.
|
|
4036
|
+
* Returns null when offline and the blob is not yet cached, or when
|
|
4037
|
+
* URL.createObjectURL is unavailable (e.g. Node.js / SSR).
|
|
4038
|
+
*/
|
|
4039
|
+
async getBlobUrl(docId, uploadId) {
|
|
4040
|
+
if (typeof window === "undefined") return null;
|
|
4041
|
+
const key = this.blobKey(docId, uploadId);
|
|
4042
|
+
const existing = this.objectUrls.get(key);
|
|
4043
|
+
if (existing) return existing;
|
|
4044
|
+
const db = await this.getDb();
|
|
4045
|
+
if (db) {
|
|
4046
|
+
const tx = db.transaction("blobs", "readonly");
|
|
4047
|
+
const entry = await txPromise(tx.objectStore("blobs"), tx.objectStore("blobs").get(key));
|
|
4048
|
+
if (entry) {
|
|
4049
|
+
const url = URL.createObjectURL(entry.blob);
|
|
4050
|
+
this.objectUrls.set(key, url);
|
|
4051
|
+
return url;
|
|
4052
|
+
}
|
|
4053
|
+
}
|
|
4054
|
+
let blob;
|
|
4055
|
+
try {
|
|
4056
|
+
blob = await this.client.getUpload(docId, uploadId);
|
|
4057
|
+
} catch {
|
|
4058
|
+
return null;
|
|
4059
|
+
}
|
|
4060
|
+
if (db) {
|
|
4061
|
+
const entry = {
|
|
4062
|
+
blob,
|
|
4063
|
+
mime_type: blob.type,
|
|
4064
|
+
filename: uploadId,
|
|
4065
|
+
cachedAt: Date.now()
|
|
4066
|
+
};
|
|
4067
|
+
db.transaction("blobs", "readwrite").objectStore("blobs").put(entry, key);
|
|
4068
|
+
}
|
|
4069
|
+
const url = URL.createObjectURL(blob);
|
|
4070
|
+
this.objectUrls.set(key, url);
|
|
4071
|
+
return url;
|
|
4072
|
+
}
|
|
4073
|
+
/** Revoke the object URL and remove the blob from cache. */
|
|
4074
|
+
async evictBlob(docId, uploadId) {
|
|
4075
|
+
const key = this.blobKey(docId, uploadId);
|
|
4076
|
+
const url = this.objectUrls.get(key);
|
|
4077
|
+
if (url) {
|
|
4078
|
+
URL.revokeObjectURL(url);
|
|
4079
|
+
this.objectUrls.delete(key);
|
|
4080
|
+
}
|
|
4081
|
+
const db = await this.getDb();
|
|
4082
|
+
if (!db) return;
|
|
4083
|
+
const tx = db.transaction("blobs", "readwrite");
|
|
4084
|
+
await txPromise(tx.objectStore("blobs"), tx.objectStore("blobs").delete(key));
|
|
4085
|
+
}
|
|
4086
|
+
/**
|
|
4087
|
+
* Queue a file for upload. Works offline — the entry is persisted to IDB
|
|
4088
|
+
* and flushed the next time the queue is flushed.
|
|
4089
|
+
* Returns the generated queue entry id.
|
|
4090
|
+
*/
|
|
4091
|
+
async queueUpload(docId, file, filename) {
|
|
4092
|
+
const id = crypto.randomUUID();
|
|
4093
|
+
const entry = {
|
|
4094
|
+
id,
|
|
4095
|
+
docId,
|
|
4096
|
+
file,
|
|
4097
|
+
filename: file instanceof File ? file.name : filename ?? "file",
|
|
4098
|
+
status: "pending",
|
|
4099
|
+
createdAt: Date.now()
|
|
4100
|
+
};
|
|
4101
|
+
const db = await this.getDb();
|
|
4102
|
+
if (db) {
|
|
4103
|
+
const tx = db.transaction("upload_queue", "readwrite");
|
|
4104
|
+
await txPromise(tx.objectStore("upload_queue"), tx.objectStore("upload_queue").put(entry));
|
|
4105
|
+
}
|
|
4106
|
+
this.emit("upload:queued", entry);
|
|
4107
|
+
return id;
|
|
4108
|
+
}
|
|
4109
|
+
/** Return all upload queue entries. */
|
|
4110
|
+
async getQueue() {
|
|
4111
|
+
const db = await this.getDb();
|
|
4112
|
+
if (!db) return [];
|
|
4113
|
+
return new Promise((resolve, reject) => {
|
|
4114
|
+
const req = db.transaction("upload_queue", "readonly").objectStore("upload_queue").getAll();
|
|
4115
|
+
req.onsuccess = () => resolve(req.result);
|
|
4116
|
+
req.onerror = () => reject(req.error);
|
|
4117
|
+
});
|
|
4118
|
+
}
|
|
4119
|
+
/**
|
|
4120
|
+
* Upload all pending queue entries via AbracadabraClient.
|
|
4121
|
+
* Safe to call repeatedly — a concurrent call is a no-op.
|
|
4122
|
+
* Entries that fail are marked with status "error" and left in the queue.
|
|
4123
|
+
*/
|
|
4124
|
+
async flushQueue() {
|
|
4125
|
+
if (this._flushing) return;
|
|
4126
|
+
this._flushing = true;
|
|
4127
|
+
try {
|
|
4128
|
+
const pending = (await this.getQueue()).filter((e) => e.status === "pending");
|
|
4129
|
+
for (const entry of pending) {
|
|
4130
|
+
await this._updateQueueEntry(entry.id, { status: "uploading" });
|
|
4131
|
+
this.emit("upload:started", {
|
|
4132
|
+
...entry,
|
|
4133
|
+
status: "uploading"
|
|
4134
|
+
});
|
|
4135
|
+
try {
|
|
4136
|
+
await this.client.upload(entry.docId, entry.file, entry.filename);
|
|
4137
|
+
await this._updateQueueEntry(entry.id, { status: "done" });
|
|
4138
|
+
this.emit("upload:done", {
|
|
4139
|
+
...entry,
|
|
4140
|
+
status: "done"
|
|
4141
|
+
});
|
|
4142
|
+
} catch (err) {
|
|
4143
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
4144
|
+
await this._updateQueueEntry(entry.id, {
|
|
4145
|
+
status: "error",
|
|
4146
|
+
error: message
|
|
4147
|
+
});
|
|
4148
|
+
this.emit("upload:error", {
|
|
4149
|
+
...entry,
|
|
4150
|
+
status: "error",
|
|
4151
|
+
error: message
|
|
4152
|
+
});
|
|
4153
|
+
}
|
|
4154
|
+
}
|
|
4155
|
+
} finally {
|
|
4156
|
+
this._flushing = false;
|
|
4157
|
+
}
|
|
4158
|
+
}
|
|
4159
|
+
async _updateQueueEntry(id, patch) {
|
|
4160
|
+
const db = await this.getDb();
|
|
4161
|
+
if (!db) return;
|
|
4162
|
+
return new Promise((resolve, reject) => {
|
|
4163
|
+
const tx = db.transaction("upload_queue", "readwrite");
|
|
4164
|
+
const store = tx.objectStore("upload_queue");
|
|
4165
|
+
const req = store.get(id);
|
|
4166
|
+
req.onsuccess = () => {
|
|
4167
|
+
if (!req.result) {
|
|
4168
|
+
resolve();
|
|
4169
|
+
return;
|
|
4170
|
+
}
|
|
4171
|
+
const updated = {
|
|
4172
|
+
...req.result,
|
|
4173
|
+
...patch
|
|
4174
|
+
};
|
|
4175
|
+
store.put(updated);
|
|
4176
|
+
tx.oncomplete = () => resolve();
|
|
4177
|
+
tx.onerror = () => reject(tx.error);
|
|
4178
|
+
};
|
|
4179
|
+
req.onerror = () => reject(req.error);
|
|
4180
|
+
});
|
|
4181
|
+
}
|
|
4182
|
+
destroy() {
|
|
4183
|
+
if (typeof window !== "undefined") window.removeEventListener("online", this._onlineHandler);
|
|
4184
|
+
for (const url of this.objectUrls.values()) URL.revokeObjectURL(url);
|
|
4185
|
+
this.objectUrls.clear();
|
|
4186
|
+
this.db?.close();
|
|
4187
|
+
this.db = null;
|
|
4188
|
+
this.removeAllListeners();
|
|
4189
|
+
}
|
|
4190
|
+
};
|
|
4191
|
+
|
|
4192
|
+
//#endregion
|
|
4193
|
+
exports.AbracadabraBaseProvider = AbracadabraBaseProvider;
|
|
3530
4194
|
exports.AbracadabraClient = AbracadabraClient;
|
|
3531
4195
|
exports.AbracadabraProvider = AbracadabraProvider;
|
|
4196
|
+
exports.AbracadabraWS = AbracadabraWS;
|
|
4197
|
+
exports.AuthMessageType = AuthMessageType;
|
|
3532
4198
|
exports.AwarenessError = AwarenessError;
|
|
4199
|
+
exports.ConnectionTimeout = ConnectionTimeout;
|
|
3533
4200
|
exports.CryptoIdentityKeystore = CryptoIdentityKeystore;
|
|
4201
|
+
exports.DocumentCache = DocumentCache;
|
|
4202
|
+
exports.FileBlobStore = FileBlobStore;
|
|
4203
|
+
exports.Forbidden = Forbidden;
|
|
3534
4204
|
exports.HocuspocusProvider = HocuspocusProvider;
|
|
3535
4205
|
exports.HocuspocusProviderWebsocket = HocuspocusProviderWebsocket;
|
|
4206
|
+
exports.MessageTooBig = MessageTooBig;
|
|
3536
4207
|
exports.MessageType = MessageType;
|
|
3537
4208
|
exports.OfflineStore = OfflineStore;
|
|
4209
|
+
exports.ResetConnection = ResetConnection;
|
|
4210
|
+
exports.SearchIndex = SearchIndex;
|
|
3538
4211
|
exports.SubdocMessage = SubdocMessage;
|
|
4212
|
+
exports.Unauthorized = Unauthorized;
|
|
3539
4213
|
exports.WebSocketStatus = WebSocketStatus;
|
|
4214
|
+
exports.WsReadyStates = WsReadyStates;
|
|
4215
|
+
exports.awarenessStatesToArray = awarenessStatesToArray;
|
|
4216
|
+
exports.readAuthMessage = readAuthMessage;
|
|
4217
|
+
exports.writeAuthenticated = writeAuthenticated;
|
|
4218
|
+
exports.writeAuthentication = writeAuthentication;
|
|
4219
|
+
exports.writePermissionDenied = writePermissionDenied;
|
|
4220
|
+
exports.writeTokenSyncRequest = writeTokenSyncRequest;
|
|
3540
4221
|
//# sourceMappingURL=abracadabra-provider.cjs.map
|