@abraca/dabra 2.76.0 → 2.78.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.
@@ -3493,6 +3493,7 @@ var AbracadabraProvider = class AbracadabraProvider extends AbracadabraBaseProvi
3493
3493
  async flushPendingUpdates() {
3494
3494
  const store = this.offlineStore;
3495
3495
  if (!store) return;
3496
+ await this.persistSnapshot();
3496
3497
  if (this.canWrite) {
3497
3498
  const updates = await store.getPendingUpdates();
3498
3499
  if (updates.length > 0) {
@@ -3508,6 +3509,22 @@ var AbracadabraProvider = class AbracadabraProvider extends AbracadabraBaseProvi
3508
3509
  childDocumentName: childId
3509
3510
  });
3510
3511
  }
3512
+ }
3513
+ /**
3514
+ * Persist the current merged document state to the offline store.
3515
+ *
3516
+ * This is the ONLY path that persists server-delivered content
3517
+ * (`documentUpdateHandler` skips `origin === this`), so it MUST run for
3518
+ * read-only roles too — otherwise observers/viewers get a permanently
3519
+ * empty offline cache and every cold start waits for a full re-sync.
3520
+ *
3521
+ * Public so short-lived consumers that load a document, read it and
3522
+ * immediately unload it (BackgroundSyncManager) can force the write to
3523
+ * land before teardown instead of racing `destroy()`.
3524
+ */
3525
+ async persistSnapshot() {
3526
+ const store = this.offlineStore;
3527
+ if (!store) return;
3511
3528
  const snapshot = yjs.encodeStateAsUpdate(this.document);
3512
3529
  await store.saveDocSnapshot(snapshot).catch(() => null);
3513
3530
  }
@@ -10592,6 +10609,8 @@ function decodeBase64UrlToString(b64url) {
10592
10609
  var AbracadabraClient = class {
10593
10610
  constructor(config) {
10594
10611
  this.rootListInflight = /* @__PURE__ */ new Map();
10612
+ this.streamTokens = /* @__PURE__ */ new Map();
10613
+ this.streamTokenInflight = /* @__PURE__ */ new Map();
10595
10614
  this.baseUrl = config.url.replace(/\/+$/, "");
10596
10615
  this.persistAuth = config.persistAuth ?? typeof localStorage !== "undefined";
10597
10616
  this.storageKey = config.storageKey ?? "abracadabra:auth";
@@ -10605,6 +10624,8 @@ var AbracadabraClient = class {
10605
10624
  }
10606
10625
  set token(value) {
10607
10626
  this._token = value;
10627
+ this.streamTokens.clear();
10628
+ this.streamTokenInflight.clear();
10608
10629
  if (this.persistAuth) if (value) this.persistToken(value);
10609
10630
  else this.clearPersistedToken();
10610
10631
  }
@@ -10984,6 +11005,28 @@ var AbracadabraClient = class {
10984
11005
  return (await this.request("GET", `/docs/search?${params.toString()}`)).results;
10985
11006
  }
10986
11007
  /**
11008
+ * Documents that link TO `docId`, via `GET /docs/:id/backlinks`.
11009
+ *
11010
+ * Served from the server's `doc_links` index, which is rebuilt from each
11011
+ * document's CRDT state whenever a snapshot is written. Prefer this over
11012
+ * inverting links from a local cache: a client can only invert what it has
11013
+ * downloaded, so on a large space a purely local backlink list is silently
11014
+ * incomplete rather than empty-and-obviously-wrong.
11015
+ *
11016
+ * The caller must be able to read the target document, and each hit is
11017
+ * cascade-filtered, so this never reveals a document the caller couldn't
11018
+ * otherwise see. `limit` is clamped to `[1, 200]` server-side (default 50).
11019
+ *
11020
+ * Throws on servers that predate the endpoint — callers that need to work
11021
+ * against older deployments should catch and fall back to a local index.
11022
+ */
11023
+ async getBacklinks(docId, opts) {
11024
+ const params = new URLSearchParams();
11025
+ if (opts?.limit != null) params.set("limit", String(opts.limit));
11026
+ const qs = params.toString();
11027
+ return (await this.request("GET", `/docs/${encodeURIComponent(docId)}/backlinks${qs ? `?${qs}` : ""}`)).results;
11028
+ }
11029
+ /**
10987
11030
  * Query for documents matching a structural predicate. v1 of the
10988
11031
  * indexed-query layer described in
10989
11032
  * `ARCHITECTURE/20-kickoff-phase3-queries.md`.
@@ -11114,6 +11157,55 @@ var AbracadabraClient = class {
11114
11157
  return res.blob();
11115
11158
  }
11116
11159
  /**
11160
+ * Mint a short-lived, read-only token bound to this one upload.
11161
+ * Prefer `getStreamUrl`, which caches and reuses the grant.
11162
+ */
11163
+ async createStreamToken(docId, uploadId) {
11164
+ return this.request("POST", `/docs/${encodeURIComponent(docId)}/uploads/${encodeURIComponent(uploadId)}/stream-token`);
11165
+ }
11166
+ /**
11167
+ * Absolute URL that streams an upload straight from the server, safe to
11168
+ * hand to `<video src>` / `<audio src>`.
11169
+ *
11170
+ * Unlike `FileBlobStore.getBlobUrl` (which downloads the whole blob into
11171
+ * IndexedDB and hands back an object URL), this keeps the bytes on the
11172
+ * server so the browser can seek natively over HTTP Range — the difference
11173
+ * between "wait for 400 MB" and "scrub instantly". It needs the network,
11174
+ * so callers should fall back to `getBlobUrl` when offline.
11175
+ *
11176
+ * Grants are cached per upload and re-minted shortly before they expire.
11177
+ * Throws on servers older than the `stream-token` endpoint (404).
11178
+ */
11179
+ async getStreamUrl(docId, uploadId) {
11180
+ const key = `${docId}/${uploadId}`;
11181
+ const cached = this.streamTokens.get(key);
11182
+ if (cached && cached.expiresAt - 6e4 > Date.now()) return cached.url;
11183
+ const inflight = this.streamTokenInflight.get(key);
11184
+ if (inflight) return inflight;
11185
+ const pending = this.createStreamToken(docId, uploadId).then((info) => {
11186
+ const url = `${this.baseUrl}${info.url}`;
11187
+ this.streamTokens.set(key, {
11188
+ url,
11189
+ expiresAt: Date.now() + info.expires_in * 1e3
11190
+ });
11191
+ return url;
11192
+ }).finally(() => {
11193
+ this.streamTokenInflight.delete(key);
11194
+ });
11195
+ this.streamTokenInflight.set(key, pending);
11196
+ return pending;
11197
+ }
11198
+ /** Drop any cached stream grant for an upload (or all of them). */
11199
+ clearStreamUrl(docId, uploadId) {
11200
+ if (docId && uploadId) {
11201
+ this.streamTokens.delete(`${docId}/${uploadId}`);
11202
+ this.streamTokenInflight.delete(`${docId}/${uploadId}`);
11203
+ return;
11204
+ }
11205
+ this.streamTokens.clear();
11206
+ this.streamTokenInflight.clear();
11207
+ }
11208
+ /**
11117
11209
  * Build the request path (with query string) for an upload's thumbnail.
11118
11210
  * Also the canonical cache key suffix — the query string is deterministic
11119
11211
  * in insertion order, so identical options map to the same URL and the
@@ -15687,6 +15779,26 @@ var FileBlobStore = class FileBlobStore extends EventEmitter {
15687
15779
  return this._objectUrlForKey(this.blobKey(docId, uploadId), uploadId, this.client ? () => this.client.getUpload(docId, uploadId) : null);
15688
15780
  }
15689
15781
  /**
15782
+ * Return a **server** URL that streams an upload with HTTP Range support —
15783
+ * for `<video src>` / `<audio src>`, where `getBlobUrl` would first pull
15784
+ * the entire file into IndexedDB and seeking would stall until it landed.
15785
+ *
15786
+ * Requires a client and the network (the URL points at the server, and
15787
+ * minting its token is a request). Returns `null` when either is missing,
15788
+ * when the server predates the `stream-token` endpoint, or when we're
15789
+ * already known to be offline — callers fall back to `getBlobUrl` for the
15790
+ * cached copy.
15791
+ */
15792
+ async getStreamUrl(docId, uploadId) {
15793
+ if (!this.client) return null;
15794
+ if (typeof navigator !== "undefined" && navigator.onLine === false) return null;
15795
+ try {
15796
+ return await this.client.getStreamUrl(docId, uploadId);
15797
+ } catch {
15798
+ return null;
15799
+ }
15800
+ }
15801
+ /**
15690
15802
  * Return a local object URL for a **thumbnail** of an image upload.
15691
15803
  * On first call the server renders + caches it and this stores the bytes
15692
15804
  * in IDB (same offline-serving model as `getBlobUrl`). Returns null when
@@ -17090,6 +17202,7 @@ var BackgroundSyncManager = class extends EventEmitter {
17090
17202
  try {
17091
17203
  await childProvider.ready;
17092
17204
  await this._waitForSynced(childProvider);
17205
+ if (typeof childProvider.persistSnapshot === "function") await childProvider.persistSnapshot();
17093
17206
  {
17094
17207
  const treeEntry = treeMap.get(docId);
17095
17208
  this.emit("docSynced", {