@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.
@@ -10592,6 +10592,8 @@ function decodeBase64UrlToString(b64url) {
10592
10592
  var AbracadabraClient = class {
10593
10593
  constructor(config) {
10594
10594
  this.rootListInflight = /* @__PURE__ */ new Map();
10595
+ this.streamTokens = /* @__PURE__ */ new Map();
10596
+ this.streamTokenInflight = /* @__PURE__ */ new Map();
10595
10597
  this.baseUrl = config.url.replace(/\/+$/, "");
10596
10598
  this.persistAuth = config.persistAuth ?? typeof localStorage !== "undefined";
10597
10599
  this.storageKey = config.storageKey ?? "abracadabra:auth";
@@ -10605,6 +10607,8 @@ var AbracadabraClient = class {
10605
10607
  }
10606
10608
  set token(value) {
10607
10609
  this._token = value;
10610
+ this.streamTokens.clear();
10611
+ this.streamTokenInflight.clear();
10608
10612
  if (this.persistAuth) if (value) this.persistToken(value);
10609
10613
  else this.clearPersistedToken();
10610
10614
  }
@@ -11114,6 +11118,55 @@ var AbracadabraClient = class {
11114
11118
  return res.blob();
11115
11119
  }
11116
11120
  /**
11121
+ * Mint a short-lived, read-only token bound to this one upload.
11122
+ * Prefer `getStreamUrl`, which caches and reuses the grant.
11123
+ */
11124
+ async createStreamToken(docId, uploadId) {
11125
+ return this.request("POST", `/docs/${encodeURIComponent(docId)}/uploads/${encodeURIComponent(uploadId)}/stream-token`);
11126
+ }
11127
+ /**
11128
+ * Absolute URL that streams an upload straight from the server, safe to
11129
+ * hand to `<video src>` / `<audio src>`.
11130
+ *
11131
+ * Unlike `FileBlobStore.getBlobUrl` (which downloads the whole blob into
11132
+ * IndexedDB and hands back an object URL), this keeps the bytes on the
11133
+ * server so the browser can seek natively over HTTP Range — the difference
11134
+ * between "wait for 400 MB" and "scrub instantly". It needs the network,
11135
+ * so callers should fall back to `getBlobUrl` when offline.
11136
+ *
11137
+ * Grants are cached per upload and re-minted shortly before they expire.
11138
+ * Throws on servers older than the `stream-token` endpoint (404).
11139
+ */
11140
+ async getStreamUrl(docId, uploadId) {
11141
+ const key = `${docId}/${uploadId}`;
11142
+ const cached = this.streamTokens.get(key);
11143
+ if (cached && cached.expiresAt - 6e4 > Date.now()) return cached.url;
11144
+ const inflight = this.streamTokenInflight.get(key);
11145
+ if (inflight) return inflight;
11146
+ const pending = this.createStreamToken(docId, uploadId).then((info) => {
11147
+ const url = `${this.baseUrl}${info.url}`;
11148
+ this.streamTokens.set(key, {
11149
+ url,
11150
+ expiresAt: Date.now() + info.expires_in * 1e3
11151
+ });
11152
+ return url;
11153
+ }).finally(() => {
11154
+ this.streamTokenInflight.delete(key);
11155
+ });
11156
+ this.streamTokenInflight.set(key, pending);
11157
+ return pending;
11158
+ }
11159
+ /** Drop any cached stream grant for an upload (or all of them). */
11160
+ clearStreamUrl(docId, uploadId) {
11161
+ if (docId && uploadId) {
11162
+ this.streamTokens.delete(`${docId}/${uploadId}`);
11163
+ this.streamTokenInflight.delete(`${docId}/${uploadId}`);
11164
+ return;
11165
+ }
11166
+ this.streamTokens.clear();
11167
+ this.streamTokenInflight.clear();
11168
+ }
11169
+ /**
11117
11170
  * Build the request path (with query string) for an upload's thumbnail.
11118
11171
  * Also the canonical cache key suffix — the query string is deterministic
11119
11172
  * in insertion order, so identical options map to the same URL and the
@@ -15687,6 +15740,26 @@ var FileBlobStore = class FileBlobStore extends EventEmitter {
15687
15740
  return this._objectUrlForKey(this.blobKey(docId, uploadId), uploadId, this.client ? () => this.client.getUpload(docId, uploadId) : null);
15688
15741
  }
15689
15742
  /**
15743
+ * Return a **server** URL that streams an upload with HTTP Range support —
15744
+ * for `<video src>` / `<audio src>`, where `getBlobUrl` would first pull
15745
+ * the entire file into IndexedDB and seeking would stall until it landed.
15746
+ *
15747
+ * Requires a client and the network (the URL points at the server, and
15748
+ * minting its token is a request). Returns `null` when either is missing,
15749
+ * when the server predates the `stream-token` endpoint, or when we're
15750
+ * already known to be offline — callers fall back to `getBlobUrl` for the
15751
+ * cached copy.
15752
+ */
15753
+ async getStreamUrl(docId, uploadId) {
15754
+ if (!this.client) return null;
15755
+ if (typeof navigator !== "undefined" && navigator.onLine === false) return null;
15756
+ try {
15757
+ return await this.client.getStreamUrl(docId, uploadId);
15758
+ } catch {
15759
+ return null;
15760
+ }
15761
+ }
15762
+ /**
15690
15763
  * Return a local object URL for a **thumbnail** of an image upload.
15691
15764
  * On first call the server renders + caches it and this stores the bytes
15692
15765
  * in IDB (same offline-serving model as `getBlobUrl`). Returns null when