@thatopen/services 0.31.2 → 0.32.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.
package/dist/cli.js CHANGED
@@ -6991,6 +6991,7 @@ class FlowDoc {
6991
6991
  );
6992
6992
  }
6993
6993
  const response = await this.client.downloadFile(file.id);
6994
+ if (!response.ok) throw new Error(`Reading the model answered ${response.status}`);
6994
6995
  return await response.blob();
6995
6996
  }
6996
6997
  /**
@@ -7284,7 +7285,10 @@ class FlowDoc {
7284
7285
  this.#filesAt = Date.now();
7285
7286
  this.#filesListed = (async () => {
7286
7287
  const folderId = await this.#folder(false);
7287
- const items2 = await this.client.listFiles({ folderId });
7288
+ const items2 = (await this.client.listFiles({ projectId: this.project.projectId })).filter((item) => {
7289
+ const where = item;
7290
+ return String(where.folderId ?? where.parentFolderId ?? "") === folderId;
7291
+ });
7288
7292
  return items2.map((item) => ({
7289
7293
  id: idOf$2(item),
7290
7294
  name: String(item.name ?? "")
@@ -7317,6 +7321,7 @@ class FlowDoc {
7317
7321
  const file = await this.#file(name);
7318
7322
  if (!file) return void 0;
7319
7323
  const response = await this.client.downloadFile(file.id);
7324
+ if (!response.ok) throw new Error(`Reading ${name} answered ${response.status}`);
7320
7325
  const text2 = await response.text();
7321
7326
  if (!text2.trim()) return void 0;
7322
7327
  return JSON.parse(text2);
@@ -7493,6 +7498,13 @@ const SELF_VIEWING_FORMATS = [
7493
7498
  function idOf$1(item) {
7494
7499
  return String(item._id ?? item.id ?? "");
7495
7500
  }
7501
+ const descriptorReads = /* @__PURE__ */ new Map();
7502
+ const stampOf = (item) => {
7503
+ const it = item;
7504
+ const newest = [...it.versions ?? []].sort((a, b) => String(b.createdAt ?? "").localeCompare(String(a.createdAt ?? "")))[0];
7505
+ const stamp = newest?.tag ?? it.versionTag;
7506
+ return stamp === void 0 || stamp === null ? void 0 : String(stamp);
7507
+ };
7496
7508
  class ProjectDocuments {
7497
7509
  constructor(client, projectId) {
7498
7510
  this.client = client;
@@ -7522,7 +7534,7 @@ class ProjectDocuments {
7522
7534
  const where = item;
7523
7535
  const folderId = String(where.folderId ?? where.parentFolderId ?? "");
7524
7536
  if (!folderId) return null;
7525
- const descriptor = await this.#readDescriptor(idOf$1(item));
7537
+ const descriptor = await this.#readDescriptor(idOf$1(item), stampOf(item));
7526
7538
  return descriptor ? { folderId, descriptor } : null;
7527
7539
  })
7528
7540
  );
@@ -7602,10 +7614,28 @@ class ProjectDocuments {
7602
7614
  async descriptor(folderId) {
7603
7615
  const file = await this.#descriptorFile(folderId);
7604
7616
  if (!file) return null;
7605
- return this.#readDescriptor(file.id);
7617
+ return this.#readDescriptor(file.id, file.stamp);
7606
7618
  }
7607
- async #readDescriptor(fileId) {
7619
+ /**
7620
+ * 🔴 EACH DESCRIPTOR READ ONCE A VERSION (Antonio 23-10: the calls «siempre lo mínimo posible»): kept by its file and
7621
+ * the moment its listing says it last changed, for every list and panel of the page (the Coordinator's start read
7622
+ * the same 16 four times). Written here, it is forgotten.
7623
+ */
7624
+ #readDescriptor(fileId, stamp) {
7625
+ const kept = stamp ? descriptorReads.get(fileId) : void 0;
7626
+ if (kept && kept.stamp === stamp) return kept.value;
7627
+ const value2 = this.#downloadDescriptor(fileId);
7628
+ if (stamp) {
7629
+ descriptorReads.set(fileId, { stamp, value: value2 });
7630
+ value2.catch(() => {
7631
+ if (descriptorReads.get(fileId)?.value === value2) descriptorReads.delete(fileId);
7632
+ });
7633
+ }
7634
+ return value2;
7635
+ }
7636
+ async #downloadDescriptor(fileId) {
7608
7637
  const response = await this.client.downloadFile(fileId);
7638
+ if (!response.ok) throw new Error(`Reading a document's descriptor answered ${response.status}`);
7609
7639
  const text2 = await response.text();
7610
7640
  if (!text2.trim()) return null;
7611
7641
  let parsed;
@@ -7673,6 +7703,7 @@ class ProjectDocuments {
7673
7703
  });
7674
7704
  const existing = await this.#descriptorFile(folderId);
7675
7705
  if (existing) {
7706
+ descriptorReads.delete(existing.id);
7676
7707
  await this.client.updateFile(existing.id, {
7677
7708
  file: blob,
7678
7709
  versionTag: await this.client.nextVersionTag(existing.id)
@@ -7737,11 +7768,14 @@ class ProjectDocuments {
7737
7768
  return void 0;
7738
7769
  }
7739
7770
  async #descriptorFile(folderId) {
7740
- const items2 = await this.client.listFiles({ folderId });
7771
+ const items2 = (await this.client.listFiles({ projectId: this.projectId })).filter((item) => {
7772
+ const where = item;
7773
+ return String(where.folderId ?? where.parentFolderId ?? "") === folderId;
7774
+ });
7741
7775
  const found = items2.find(
7742
7776
  (item) => String(item.name ?? "") === DOCUMENT_FILE
7743
7777
  );
7744
- return found ? { id: idOf$1(found) } : void 0;
7778
+ return found ? { id: idOf$1(found), stamp: stampOf(found) } : void 0;
7745
7779
  }
7746
7780
  }
7747
7781
  const DRAFT_TAG = "draft:";
@@ -8746,6 +8780,8 @@ const ITEM_TYPE_COMPONENT = "TOOL";
8746
8780
  const ITEM_TYPE_APP = "APP";
8747
8781
  const RETRY_BASE_DELAY_MS = 500;
8748
8782
  const RETRY_MAX_DELAY_MS = 3e4;
8783
+ const READ_SHARED_MS = 2e3;
8784
+ const sharedReads = /* @__PURE__ */ new Map();
8749
8785
  const RETRY_JITTER_RATIO = 0.25;
8750
8786
  const sleep = (ms) => new Promise((resolve2) => setTimeout(resolve2, ms));
8751
8787
  function parseRetryAfter(response) {
@@ -8911,6 +8947,30 @@ class EngineServicesClient {
8911
8947
  return this.accessToken;
8912
8948
  }
8913
8949
  async #requestApi(method, path2, requestData) {
8950
+ if (method === "GET" && requestData?.responseType !== "blob" && !requestData?.retryAttempt) {
8951
+ const key = `${this.#buildUrl(path2)}?${new URLSearchParams(this.#cleanData(requestData?.query)).toString()}`;
8952
+ const now = Date.now();
8953
+ const kept = sharedReads.get(key);
8954
+ if (kept && (kept.pending || now - kept.at < READ_SHARED_MS)) {
8955
+ return kept.answer.then((data) => data === void 0 ? data : structuredClone(data));
8956
+ }
8957
+ const entry = { at: now, pending: true, answer: this.#requestApiOnce(method, path2, requestData) };
8958
+ sharedReads.set(key, entry);
8959
+ entry.answer.then(
8960
+ () => {
8961
+ entry.pending = false;
8962
+ entry.at = Date.now();
8963
+ },
8964
+ () => {
8965
+ if (sharedReads.get(key) === entry) sharedReads.delete(key);
8966
+ }
8967
+ );
8968
+ return entry.answer.then((data) => data === void 0 ? data : structuredClone(data));
8969
+ }
8970
+ if (method !== "GET") sharedReads.clear();
8971
+ return this.#requestApiOnce(method, path2, requestData);
8972
+ }
8973
+ async #requestApiOnce(method, path2, requestData) {
8914
8974
  const { body, query, contentType, retries, responseType, retryAttempt } = requestData || {};
8915
8975
  const url2 = this.#buildUrl(path2);
8916
8976
  const cleanQuery = this.#cleanData(query);
@@ -10090,12 +10150,16 @@ return main;`);
10090
10150
  }
10091
10151
  async #downloadItem(itemId, params) {
10092
10152
  const { versionTag, withDraft } = params || {};
10093
- return await this.#requestFile(`${ITEM_PATH}/${itemId}/download`, {
10153
+ const response = await this.#requestFile(`${ITEM_PATH}/${itemId}/download`, {
10094
10154
  query: {
10095
10155
  ...versionTag && { versionTag },
10096
10156
  ...withDraft && { withDraft }
10097
10157
  }
10098
10158
  });
10159
+ if (response.status === 429 || response.status >= 500) {
10160
+ throw new Error(`Downloading ${itemId} answered ${response.status}${response.status === 429 ? " (rate limited)" : ""}`);
10161
+ }
10162
+ return response;
10099
10163
  }
10100
10164
  async #createItem(fileData, itemType, extraProps) {
10101
10165
  const { name, versionTag, parentFolderId, projectId, file, metadata: metadata2 } = fileData;