@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.
@@ -3463,6 +3463,7 @@ var AbracadabraProvider = class AbracadabraProvider extends AbracadabraBaseProvi
3463
3463
  async flushPendingUpdates() {
3464
3464
  const store = this.offlineStore;
3465
3465
  if (!store) return;
3466
+ await this.persistSnapshot();
3466
3467
  if (this.canWrite) {
3467
3468
  const updates = await store.getPendingUpdates();
3468
3469
  if (updates.length > 0) {
@@ -3478,6 +3479,22 @@ var AbracadabraProvider = class AbracadabraProvider extends AbracadabraBaseProvi
3478
3479
  childDocumentName: childId
3479
3480
  });
3480
3481
  }
3482
+ }
3483
+ /**
3484
+ * Persist the current merged document state to the offline store.
3485
+ *
3486
+ * This is the ONLY path that persists server-delivered content
3487
+ * (`documentUpdateHandler` skips `origin === this`), so it MUST run for
3488
+ * read-only roles too — otherwise observers/viewers get a permanently
3489
+ * empty offline cache and every cold start waits for a full re-sync.
3490
+ *
3491
+ * Public so short-lived consumers that load a document, read it and
3492
+ * immediately unload it (BackgroundSyncManager) can force the write to
3493
+ * land before teardown instead of racing `destroy()`.
3494
+ */
3495
+ async persistSnapshot() {
3496
+ const store = this.offlineStore;
3497
+ if (!store) return;
3481
3498
  const snapshot = Y.encodeStateAsUpdate(this.document);
3482
3499
  await store.saveDocSnapshot(snapshot).catch(() => null);
3483
3500
  }
@@ -10562,6 +10579,8 @@ function decodeBase64UrlToString(b64url) {
10562
10579
  var AbracadabraClient = class {
10563
10580
  constructor(config) {
10564
10581
  this.rootListInflight = /* @__PURE__ */ new Map();
10582
+ this.streamTokens = /* @__PURE__ */ new Map();
10583
+ this.streamTokenInflight = /* @__PURE__ */ new Map();
10565
10584
  this.baseUrl = config.url.replace(/\/+$/, "");
10566
10585
  this.persistAuth = config.persistAuth ?? typeof localStorage !== "undefined";
10567
10586
  this.storageKey = config.storageKey ?? "abracadabra:auth";
@@ -10575,6 +10594,8 @@ var AbracadabraClient = class {
10575
10594
  }
10576
10595
  set token(value) {
10577
10596
  this._token = value;
10597
+ this.streamTokens.clear();
10598
+ this.streamTokenInflight.clear();
10578
10599
  if (this.persistAuth) if (value) this.persistToken(value);
10579
10600
  else this.clearPersistedToken();
10580
10601
  }
@@ -10954,6 +10975,28 @@ var AbracadabraClient = class {
10954
10975
  return (await this.request("GET", `/docs/search?${params.toString()}`)).results;
10955
10976
  }
10956
10977
  /**
10978
+ * Documents that link TO `docId`, via `GET /docs/:id/backlinks`.
10979
+ *
10980
+ * Served from the server's `doc_links` index, which is rebuilt from each
10981
+ * document's CRDT state whenever a snapshot is written. Prefer this over
10982
+ * inverting links from a local cache: a client can only invert what it has
10983
+ * downloaded, so on a large space a purely local backlink list is silently
10984
+ * incomplete rather than empty-and-obviously-wrong.
10985
+ *
10986
+ * The caller must be able to read the target document, and each hit is
10987
+ * cascade-filtered, so this never reveals a document the caller couldn't
10988
+ * otherwise see. `limit` is clamped to `[1, 200]` server-side (default 50).
10989
+ *
10990
+ * Throws on servers that predate the endpoint — callers that need to work
10991
+ * against older deployments should catch and fall back to a local index.
10992
+ */
10993
+ async getBacklinks(docId, opts) {
10994
+ const params = new URLSearchParams();
10995
+ if (opts?.limit != null) params.set("limit", String(opts.limit));
10996
+ const qs = params.toString();
10997
+ return (await this.request("GET", `/docs/${encodeURIComponent(docId)}/backlinks${qs ? `?${qs}` : ""}`)).results;
10998
+ }
10999
+ /**
10957
11000
  * Query for documents matching a structural predicate. v1 of the
10958
11001
  * indexed-query layer described in
10959
11002
  * `ARCHITECTURE/20-kickoff-phase3-queries.md`.
@@ -11084,6 +11127,55 @@ var AbracadabraClient = class {
11084
11127
  return res.blob();
11085
11128
  }
11086
11129
  /**
11130
+ * Mint a short-lived, read-only token bound to this one upload.
11131
+ * Prefer `getStreamUrl`, which caches and reuses the grant.
11132
+ */
11133
+ async createStreamToken(docId, uploadId) {
11134
+ return this.request("POST", `/docs/${encodeURIComponent(docId)}/uploads/${encodeURIComponent(uploadId)}/stream-token`);
11135
+ }
11136
+ /**
11137
+ * Absolute URL that streams an upload straight from the server, safe to
11138
+ * hand to `<video src>` / `<audio src>`.
11139
+ *
11140
+ * Unlike `FileBlobStore.getBlobUrl` (which downloads the whole blob into
11141
+ * IndexedDB and hands back an object URL), this keeps the bytes on the
11142
+ * server so the browser can seek natively over HTTP Range — the difference
11143
+ * between "wait for 400 MB" and "scrub instantly". It needs the network,
11144
+ * so callers should fall back to `getBlobUrl` when offline.
11145
+ *
11146
+ * Grants are cached per upload and re-minted shortly before they expire.
11147
+ * Throws on servers older than the `stream-token` endpoint (404).
11148
+ */
11149
+ async getStreamUrl(docId, uploadId) {
11150
+ const key = `${docId}/${uploadId}`;
11151
+ const cached = this.streamTokens.get(key);
11152
+ if (cached && cached.expiresAt - 6e4 > Date.now()) return cached.url;
11153
+ const inflight = this.streamTokenInflight.get(key);
11154
+ if (inflight) return inflight;
11155
+ const pending = this.createStreamToken(docId, uploadId).then((info) => {
11156
+ const url = `${this.baseUrl}${info.url}`;
11157
+ this.streamTokens.set(key, {
11158
+ url,
11159
+ expiresAt: Date.now() + info.expires_in * 1e3
11160
+ });
11161
+ return url;
11162
+ }).finally(() => {
11163
+ this.streamTokenInflight.delete(key);
11164
+ });
11165
+ this.streamTokenInflight.set(key, pending);
11166
+ return pending;
11167
+ }
11168
+ /** Drop any cached stream grant for an upload (or all of them). */
11169
+ clearStreamUrl(docId, uploadId) {
11170
+ if (docId && uploadId) {
11171
+ this.streamTokens.delete(`${docId}/${uploadId}`);
11172
+ this.streamTokenInflight.delete(`${docId}/${uploadId}`);
11173
+ return;
11174
+ }
11175
+ this.streamTokens.clear();
11176
+ this.streamTokenInflight.clear();
11177
+ }
11178
+ /**
11087
11179
  * Build the request path (with query string) for an upload's thumbnail.
11088
11180
  * Also the canonical cache key suffix — the query string is deterministic
11089
11181
  * in insertion order, so identical options map to the same URL and the
@@ -15657,6 +15749,26 @@ var FileBlobStore = class FileBlobStore extends EventEmitter {
15657
15749
  return this._objectUrlForKey(this.blobKey(docId, uploadId), uploadId, this.client ? () => this.client.getUpload(docId, uploadId) : null);
15658
15750
  }
15659
15751
  /**
15752
+ * Return a **server** URL that streams an upload with HTTP Range support —
15753
+ * for `<video src>` / `<audio src>`, where `getBlobUrl` would first pull
15754
+ * the entire file into IndexedDB and seeking would stall until it landed.
15755
+ *
15756
+ * Requires a client and the network (the URL points at the server, and
15757
+ * minting its token is a request). Returns `null` when either is missing,
15758
+ * when the server predates the `stream-token` endpoint, or when we're
15759
+ * already known to be offline — callers fall back to `getBlobUrl` for the
15760
+ * cached copy.
15761
+ */
15762
+ async getStreamUrl(docId, uploadId) {
15763
+ if (!this.client) return null;
15764
+ if (typeof navigator !== "undefined" && navigator.onLine === false) return null;
15765
+ try {
15766
+ return await this.client.getStreamUrl(docId, uploadId);
15767
+ } catch {
15768
+ return null;
15769
+ }
15770
+ }
15771
+ /**
15660
15772
  * Return a local object URL for a **thumbnail** of an image upload.
15661
15773
  * On first call the server renders + caches it and this stores the bytes
15662
15774
  * in IDB (same offline-serving model as `getBlobUrl`). Returns null when
@@ -17037,6 +17149,7 @@ var BackgroundSyncManager = class extends EventEmitter {
17037
17149
  try {
17038
17150
  await childProvider.ready;
17039
17151
  await this._waitForSynced(childProvider);
17152
+ if (typeof childProvider.persistSnapshot === "function") await childProvider.persistSnapshot();
17040
17153
  {
17041
17154
  const treeEntry = treeMap.get(docId);
17042
17155
  this.emit("docSynced", {