@abraca/dabra 2.72.0 → 2.74.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.
@@ -11103,6 +11103,40 @@ var AbracadabraClient = class {
11103
11103
  if (!res.ok) throw await this.toError(res);
11104
11104
  return res.blob();
11105
11105
  }
11106
+ /**
11107
+ * Build the request path (with query string) for an upload's thumbnail.
11108
+ * Also the canonical cache key suffix — the query string is deterministic
11109
+ * in insertion order, so identical options map to the same URL and the
11110
+ * same `FileBlobStore` cache entry.
11111
+ */
11112
+ thumbnailPath(docId, uploadId, opts = {}) {
11113
+ const p = new URLSearchParams();
11114
+ if (opts.w != null) p.set("w", String(opts.w));
11115
+ if (opts.h != null) p.set("h", String(opts.h));
11116
+ if (opts.fit) p.set("fit", opts.fit);
11117
+ if (opts.format) p.set("format", opts.format);
11118
+ if (opts.q != null) p.set("q", String(opts.q));
11119
+ const qs = p.toString();
11120
+ return `/docs/${encodeURIComponent(docId)}/uploads/${encodeURIComponent(uploadId)}/thumbnail${qs ? `?${qs}` : ""}`;
11121
+ }
11122
+ /**
11123
+ * Download a generated thumbnail of an image upload as a Blob. The server
11124
+ * renders + caches it on first request. Because a browser `<img>` can't
11125
+ * send `Authorization`, callers render the returned Blob via an object URL
11126
+ * (see `FileBlobStore.getThumbUrl`, which also caches it locally).
11127
+ * Throws (with `.status = 415`) when the source isn't a thumbnailable
11128
+ * raster image, or `422` for an out-of-allowlist size/format.
11129
+ */
11130
+ async getThumbnail(docId, uploadId, opts = {}) {
11131
+ const headers = {};
11132
+ if (this._token) headers["Authorization"] = `Bearer ${this._token}`;
11133
+ const res = await this._fetch(`${this.baseUrl}${this.thumbnailPath(docId, uploadId, opts)}`, {
11134
+ method: "GET",
11135
+ headers
11136
+ });
11137
+ if (!res.ok) throw await this.toError(res);
11138
+ return res.blob();
11139
+ }
11106
11140
  /** Delete an upload (requires uploader or document Owner). */
11107
11141
  async deleteUpload(docId, uploadId) {
11108
11142
  await this.request("DELETE", `/docs/${encodeURIComponent(docId)}/uploads/${encodeURIComponent(uploadId)}`);
@@ -15619,14 +15653,48 @@ var FileBlobStore = class FileBlobStore extends EventEmitter {
15619
15653
  return `${docId}/${uploadId}`;
15620
15654
  }
15621
15655
  /**
15656
+ * Cache key for a thumbnail. Lives in the SAME IDB `blobs` store as
15657
+ * originals (no schema bump — the frozen cross-SDK IDB contract stays
15658
+ * intact) under a distinct `thumb:` namespace. The spec suffix is
15659
+ * deterministic so identical options collapse to one entry.
15660
+ */
15661
+ thumbKey(docId, uploadId, opts) {
15662
+ return `thumb:${docId}/${uploadId}/${[
15663
+ opts.w != null ? `w${opts.w}` : "",
15664
+ opts.h != null ? `h${opts.h}` : "",
15665
+ opts.fit ? `f${opts.fit}` : "",
15666
+ opts.format ? `m${opts.format}` : "",
15667
+ opts.q != null ? `q${opts.q}` : ""
15668
+ ].filter(Boolean).join("-")}`;
15669
+ }
15670
+ /**
15622
15671
  * Return a local object URL for a file.
15623
15672
  * On first call the blob is downloaded from the server and cached in IDB.
15624
15673
  * Returns null when offline and the blob is not yet cached, or when
15625
15674
  * URL.createObjectURL is unavailable (e.g. Node.js / SSR).
15626
15675
  */
15627
15676
  async getBlobUrl(docId, uploadId) {
15677
+ return this._objectUrlForKey(this.blobKey(docId, uploadId), uploadId, this.client ? () => this.client.getUpload(docId, uploadId) : null);
15678
+ }
15679
+ /**
15680
+ * Return a local object URL for a **thumbnail** of an image upload.
15681
+ * On first call the server renders + caches it and this stores the bytes
15682
+ * in IDB (same offline-serving model as `getBlobUrl`). Returns null when
15683
+ * offline + uncached, on non-image sources (server 415 → treated like a
15684
+ * miss), or outside the browser. Options must match the server's
15685
+ * `allowed_sizes` / `allowed_formats` or the fetch 422s (→ null).
15686
+ */
15687
+ async getThumbUrl(docId, uploadId, opts = {}) {
15688
+ return this._objectUrlForKey(this.thumbKey(docId, uploadId, opts), uploadId, this.client ? () => this.client.getThumbnail(docId, uploadId, opts) : null);
15689
+ }
15690
+ /**
15691
+ * Shared read-through: in-memory object URL → IDB `blobs` cache (with a
15692
+ * one-shot reopen on `InvalidStateError`) → `download()` (when a client is
15693
+ * available), caching the result. `download === null` means "no network
15694
+ * source" (offline / no client) → cache-only. 404 is negative-cached.
15695
+ */
15696
+ async _objectUrlForKey(key, filename, download) {
15628
15697
  if (typeof window === "undefined") return null;
15629
- const key = this.blobKey(docId, uploadId);
15630
15698
  const existing = this.objectUrls.get(key);
15631
15699
  if (existing) return existing;
15632
15700
  let db = await this.getDb();
@@ -15652,12 +15720,12 @@ var FileBlobStore = class FileBlobStore extends EventEmitter {
15652
15720
  return url;
15653
15721
  }
15654
15722
  }
15655
- if (!this.client) return null;
15723
+ if (!download) return null;
15656
15724
  const nfTime = this._notFound.get(key);
15657
15725
  if (nfTime && Date.now() - nfTime < FileBlobStore.NOT_FOUND_TTL) return null;
15658
15726
  let blob;
15659
15727
  try {
15660
- blob = await this.client.getUpload(docId, uploadId);
15728
+ blob = await download();
15661
15729
  } catch (err) {
15662
15730
  if (err?.status === 404) this._notFound.set(key, Date.now());
15663
15731
  return null;
@@ -15666,7 +15734,7 @@ var FileBlobStore = class FileBlobStore extends EventEmitter {
15666
15734
  const entry = {
15667
15735
  blob,
15668
15736
  mime_type: blob.type,
15669
- filename: uploadId,
15737
+ filename,
15670
15738
  cachedAt: Date.now()
15671
15739
  };
15672
15740
  db.transaction("blobs", "readwrite").objectStore("blobs").put(entry, key);
@@ -15769,6 +15837,18 @@ var FileBlobStore = class FileBlobStore extends EventEmitter {
15769
15837
  }
15770
15838
  }
15771
15839
  /**
15840
+ * Thumbnail counterpart of {@link invalidateUrl}: revoke the in-memory
15841
+ * object URL for a specific thumbnail spec without touching its IDB cache.
15842
+ */
15843
+ invalidateThumbUrl(docId, uploadId, opts = {}) {
15844
+ const key = this.thumbKey(docId, uploadId, opts);
15845
+ const url = this.objectUrls.get(key);
15846
+ if (url) {
15847
+ URL.revokeObjectURL(url);
15848
+ this.objectUrls.delete(key);
15849
+ }
15850
+ }
15851
+ /**
15772
15852
  * Clear the 404 negative-cache entry for (docId, uploadId) so the next
15773
15853
  * getBlobUrl() re-fetches from the server instead of short-circuiting.
15774
15854
  * Use on explicit user retry or after reconnect, when the file may have