@abraca/dabra 2.76.0 → 2.77.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.
@@ -10562,6 +10562,8 @@ function decodeBase64UrlToString(b64url) {
10562
10562
  var AbracadabraClient = class {
10563
10563
  constructor(config) {
10564
10564
  this.rootListInflight = /* @__PURE__ */ new Map();
10565
+ this.streamTokens = /* @__PURE__ */ new Map();
10566
+ this.streamTokenInflight = /* @__PURE__ */ new Map();
10565
10567
  this.baseUrl = config.url.replace(/\/+$/, "");
10566
10568
  this.persistAuth = config.persistAuth ?? typeof localStorage !== "undefined";
10567
10569
  this.storageKey = config.storageKey ?? "abracadabra:auth";
@@ -10575,6 +10577,8 @@ var AbracadabraClient = class {
10575
10577
  }
10576
10578
  set token(value) {
10577
10579
  this._token = value;
10580
+ this.streamTokens.clear();
10581
+ this.streamTokenInflight.clear();
10578
10582
  if (this.persistAuth) if (value) this.persistToken(value);
10579
10583
  else this.clearPersistedToken();
10580
10584
  }
@@ -11084,6 +11088,55 @@ var AbracadabraClient = class {
11084
11088
  return res.blob();
11085
11089
  }
11086
11090
  /**
11091
+ * Mint a short-lived, read-only token bound to this one upload.
11092
+ * Prefer `getStreamUrl`, which caches and reuses the grant.
11093
+ */
11094
+ async createStreamToken(docId, uploadId) {
11095
+ return this.request("POST", `/docs/${encodeURIComponent(docId)}/uploads/${encodeURIComponent(uploadId)}/stream-token`);
11096
+ }
11097
+ /**
11098
+ * Absolute URL that streams an upload straight from the server, safe to
11099
+ * hand to `<video src>` / `<audio src>`.
11100
+ *
11101
+ * Unlike `FileBlobStore.getBlobUrl` (which downloads the whole blob into
11102
+ * IndexedDB and hands back an object URL), this keeps the bytes on the
11103
+ * server so the browser can seek natively over HTTP Range — the difference
11104
+ * between "wait for 400 MB" and "scrub instantly". It needs the network,
11105
+ * so callers should fall back to `getBlobUrl` when offline.
11106
+ *
11107
+ * Grants are cached per upload and re-minted shortly before they expire.
11108
+ * Throws on servers older than the `stream-token` endpoint (404).
11109
+ */
11110
+ async getStreamUrl(docId, uploadId) {
11111
+ const key = `${docId}/${uploadId}`;
11112
+ const cached = this.streamTokens.get(key);
11113
+ if (cached && cached.expiresAt - 6e4 > Date.now()) return cached.url;
11114
+ const inflight = this.streamTokenInflight.get(key);
11115
+ if (inflight) return inflight;
11116
+ const pending = this.createStreamToken(docId, uploadId).then((info) => {
11117
+ const url = `${this.baseUrl}${info.url}`;
11118
+ this.streamTokens.set(key, {
11119
+ url,
11120
+ expiresAt: Date.now() + info.expires_in * 1e3
11121
+ });
11122
+ return url;
11123
+ }).finally(() => {
11124
+ this.streamTokenInflight.delete(key);
11125
+ });
11126
+ this.streamTokenInflight.set(key, pending);
11127
+ return pending;
11128
+ }
11129
+ /** Drop any cached stream grant for an upload (or all of them). */
11130
+ clearStreamUrl(docId, uploadId) {
11131
+ if (docId && uploadId) {
11132
+ this.streamTokens.delete(`${docId}/${uploadId}`);
11133
+ this.streamTokenInflight.delete(`${docId}/${uploadId}`);
11134
+ return;
11135
+ }
11136
+ this.streamTokens.clear();
11137
+ this.streamTokenInflight.clear();
11138
+ }
11139
+ /**
11087
11140
  * Build the request path (with query string) for an upload's thumbnail.
11088
11141
  * Also the canonical cache key suffix — the query string is deterministic
11089
11142
  * in insertion order, so identical options map to the same URL and the
@@ -15657,6 +15710,26 @@ var FileBlobStore = class FileBlobStore extends EventEmitter {
15657
15710
  return this._objectUrlForKey(this.blobKey(docId, uploadId), uploadId, this.client ? () => this.client.getUpload(docId, uploadId) : null);
15658
15711
  }
15659
15712
  /**
15713
+ * Return a **server** URL that streams an upload with HTTP Range support —
15714
+ * for `<video src>` / `<audio src>`, where `getBlobUrl` would first pull
15715
+ * the entire file into IndexedDB and seeking would stall until it landed.
15716
+ *
15717
+ * Requires a client and the network (the URL points at the server, and
15718
+ * minting its token is a request). Returns `null` when either is missing,
15719
+ * when the server predates the `stream-token` endpoint, or when we're
15720
+ * already known to be offline — callers fall back to `getBlobUrl` for the
15721
+ * cached copy.
15722
+ */
15723
+ async getStreamUrl(docId, uploadId) {
15724
+ if (!this.client) return null;
15725
+ if (typeof navigator !== "undefined" && navigator.onLine === false) return null;
15726
+ try {
15727
+ return await this.client.getStreamUrl(docId, uploadId);
15728
+ } catch {
15729
+ return null;
15730
+ }
15731
+ }
15732
+ /**
15660
15733
  * Return a local object URL for a **thumbnail** of an image upload.
15661
15734
  * On first call the server renders + caches it and this stores the bytes
15662
15735
  * in IDB (same offline-serving model as `getBlobUrl`). Returns null when