@thatopen/services 0.31.2 → 0.33.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
@@ -6810,6 +6810,7 @@ const HISTORY_FILE = "revitflow_history.json";
6810
6810
  const LEGACY_MODEL_FILE = "revitflow_model.frag";
6811
6811
  const INTERNAL_PREFIX = "revitflow_";
6812
6812
  const LOCKS_ANCHOR = "bimterop-locks-anchor";
6813
+ const jsonReads = /* @__PURE__ */ new Map();
6813
6814
  function childrenOf(folders, parentId) {
6814
6815
  return folders.filter((f) => String(f.parentId ?? "") === parentId);
6815
6816
  }
@@ -6991,6 +6992,7 @@ class FlowDoc {
6991
6992
  );
6992
6993
  }
6993
6994
  const response = await this.client.downloadFile(file.id);
6995
+ if (!response.ok) throw new Error(`Reading the model answered ${response.status}`);
6994
6996
  return await response.blob();
6995
6997
  }
6996
6998
  /**
@@ -7284,10 +7286,14 @@ class FlowDoc {
7284
7286
  this.#filesAt = Date.now();
7285
7287
  this.#filesListed = (async () => {
7286
7288
  const folderId = await this.#folder(false);
7287
- const items2 = await this.client.listFiles({ folderId });
7289
+ const items2 = (await this.client.listFiles({ projectId: this.project.projectId })).filter((item) => {
7290
+ const where = item;
7291
+ return String(where.folderId ?? where.parentFolderId ?? "") === folderId;
7292
+ });
7288
7293
  return items2.map((item) => ({
7289
7294
  id: idOf$2(item),
7290
- name: String(item.name ?? "")
7295
+ name: String(item.name ?? ""),
7296
+ stamp: stampOf(item)
7291
7297
  }));
7292
7298
  })();
7293
7299
  this.#filesListed.catch(() => {
@@ -7316,7 +7322,20 @@ class FlowDoc {
7316
7322
  async #readJson(name) {
7317
7323
  const file = await this.#file(name);
7318
7324
  if (!file) return void 0;
7325
+ const kept = file.stamp ? jsonReads.get(file.id) : void 0;
7326
+ if (kept && kept.stamp === file.stamp) return structuredClone(await kept.value);
7327
+ const value2 = this.#downloadJson(file, name);
7328
+ if (file.stamp) {
7329
+ jsonReads.set(file.id, { stamp: file.stamp, value: value2 });
7330
+ value2.catch(() => {
7331
+ if (jsonReads.get(file.id)?.value === value2) jsonReads.delete(file.id);
7332
+ });
7333
+ }
7334
+ return structuredClone(await value2);
7335
+ }
7336
+ async #downloadJson(file, name) {
7319
7337
  const response = await this.client.downloadFile(file.id);
7338
+ if (!response.ok) throw new Error(`Reading ${name} answered ${response.status}`);
7320
7339
  const text2 = await response.text();
7321
7340
  if (!text2.trim()) return void 0;
7322
7341
  return JSON.parse(text2);
@@ -7330,6 +7349,7 @@ class FlowDoc {
7330
7349
  });
7331
7350
  const existing = await this.#file(name);
7332
7351
  if (existing) {
7352
+ jsonReads.delete(existing.id);
7333
7353
  await this.client.updateFile(existing.id, {
7334
7354
  file: blob,
7335
7355
  versionTag: await this.client.nextVersionTag(existing.id)
@@ -7493,6 +7513,14 @@ const SELF_VIEWING_FORMATS = [
7493
7513
  function idOf$1(item) {
7494
7514
  return String(item._id ?? item.id ?? "");
7495
7515
  }
7516
+ const descriptorReads = /* @__PURE__ */ new Map();
7517
+ let descriptorsRouteMissing = false;
7518
+ const stampOf = (item) => {
7519
+ const it = item;
7520
+ const newest = [...it.versions ?? []].sort((a, b) => String(b.createdAt ?? "").localeCompare(String(a.createdAt ?? "")))[0];
7521
+ const stamp = newest?.tag ?? it.versionTag;
7522
+ return stamp === void 0 || stamp === null ? void 0 : String(stamp);
7523
+ };
7496
7524
  class ProjectDocuments {
7497
7525
  constructor(client, projectId) {
7498
7526
  this.client = client;
@@ -7513,6 +7541,8 @@ class ProjectDocuments {
7513
7541
  return all.filter((d) => types2.includes(documentTypeOf(d.descriptor)));
7514
7542
  }
7515
7543
  async #listAll() {
7544
+ const all = await this.#listAllAtOnce();
7545
+ if (all) return all;
7516
7546
  const files = await this.client.listFiles({ projectId: this.projectId });
7517
7547
  const descriptors = files.filter(
7518
7548
  (item) => String(item.name ?? "") === DOCUMENT_FILE
@@ -7522,7 +7552,7 @@ class ProjectDocuments {
7522
7552
  const where = item;
7523
7553
  const folderId = String(where.folderId ?? where.parentFolderId ?? "");
7524
7554
  if (!folderId) return null;
7525
- const descriptor = await this.#readDescriptor(idOf$1(item));
7555
+ const descriptor = await this.#readDescriptor(idOf$1(item), stampOf(item));
7526
7556
  return descriptor ? { folderId, descriptor } : null;
7527
7557
  })
7528
7558
  );
@@ -7602,10 +7632,59 @@ class ProjectDocuments {
7602
7632
  async descriptor(folderId) {
7603
7633
  const file = await this.#descriptorFile(folderId);
7604
7634
  if (!file) return null;
7605
- return this.#readDescriptor(file.id);
7635
+ return this.#readDescriptor(file.id, file.stamp);
7636
+ }
7637
+ /**
7638
+ * Every descriptor from the platform in one request, or null when the
7639
+ * platform has no such route (one from before it: 404, or 400 taking
7640
+ * `documents` for an item id) and they are read one by one. Asked once per
7641
+ * page: a platform without it is not asked again. A 429 or a 5xx is an
7642
+ * error, never "no documents" (23-10).
7643
+ */
7644
+ async #listAllAtOnce() {
7645
+ if (descriptorsRouteMissing) return null;
7646
+ let answer;
7647
+ try {
7648
+ answer = await this.client.listDocumentDescriptors(this.projectId);
7649
+ } catch (error2) {
7650
+ const status = error2.status;
7651
+ if (status === 404 || status === 400 || status === 405) {
7652
+ descriptorsRouteMissing = true;
7653
+ return null;
7654
+ }
7655
+ throw error2;
7656
+ }
7657
+ const found = [];
7658
+ for (const one of answer?.documents ?? []) {
7659
+ const descriptor = normalize$1(one.descriptor);
7660
+ if (!descriptor || !one.folderId) continue;
7661
+ if (one.fileId && one.versionTag) {
7662
+ descriptorReads.set(one.fileId, { stamp: one.versionTag, value: Promise.resolve(descriptor) });
7663
+ }
7664
+ found.push({ folderId: String(one.folderId), descriptor });
7665
+ }
7666
+ return found;
7667
+ }
7668
+ /**
7669
+ * 🔴 EACH DESCRIPTOR READ ONCE A VERSION (Antonio 23-10: the calls «siempre lo mínimo posible»): kept by its file and
7670
+ * the moment its listing says it last changed, for every list and panel of the page (the Coordinator's start read
7671
+ * the same 16 four times). Written here, it is forgotten.
7672
+ */
7673
+ #readDescriptor(fileId, stamp) {
7674
+ const kept = stamp ? descriptorReads.get(fileId) : void 0;
7675
+ if (kept && kept.stamp === stamp) return kept.value;
7676
+ const value2 = this.#downloadDescriptor(fileId);
7677
+ if (stamp) {
7678
+ descriptorReads.set(fileId, { stamp, value: value2 });
7679
+ value2.catch(() => {
7680
+ if (descriptorReads.get(fileId)?.value === value2) descriptorReads.delete(fileId);
7681
+ });
7682
+ }
7683
+ return value2;
7606
7684
  }
7607
- async #readDescriptor(fileId) {
7685
+ async #downloadDescriptor(fileId) {
7608
7686
  const response = await this.client.downloadFile(fileId);
7687
+ if (!response.ok) throw new Error(`Reading a document's descriptor answered ${response.status}`);
7609
7688
  const text2 = await response.text();
7610
7689
  if (!text2.trim()) return null;
7611
7690
  let parsed;
@@ -7673,6 +7752,7 @@ class ProjectDocuments {
7673
7752
  });
7674
7753
  const existing = await this.#descriptorFile(folderId);
7675
7754
  if (existing) {
7755
+ descriptorReads.delete(existing.id);
7676
7756
  await this.client.updateFile(existing.id, {
7677
7757
  file: blob,
7678
7758
  versionTag: await this.client.nextVersionTag(existing.id)
@@ -7737,11 +7817,14 @@ class ProjectDocuments {
7737
7817
  return void 0;
7738
7818
  }
7739
7819
  async #descriptorFile(folderId) {
7740
- const items2 = await this.client.listFiles({ folderId });
7820
+ const items2 = (await this.client.listFiles({ projectId: this.projectId })).filter((item) => {
7821
+ const where = item;
7822
+ return String(where.folderId ?? where.parentFolderId ?? "") === folderId;
7823
+ });
7741
7824
  const found = items2.find(
7742
7825
  (item) => String(item.name ?? "") === DOCUMENT_FILE
7743
7826
  );
7744
- return found ? { id: idOf$1(found) } : void 0;
7827
+ return found ? { id: idOf$1(found), stamp: stampOf(found) } : void 0;
7745
7828
  }
7746
7829
  }
7747
7830
  const DRAFT_TAG = "draft:";
@@ -8746,6 +8829,11 @@ const ITEM_TYPE_COMPONENT = "TOOL";
8746
8829
  const ITEM_TYPE_APP = "APP";
8747
8830
  const RETRY_BASE_DELAY_MS = 500;
8748
8831
  const RETRY_MAX_DELAY_MS = 3e4;
8832
+ const READ_SHARED_MS = 2e3;
8833
+ const LISTING_SHARED_MS = 15e3;
8834
+ const isAsking = (path2) => /^\/?project\/permissions\/check/.test(path2);
8835
+ const isListing = (path2) => /^\/?(item\/folder\/?$|item\/?$|item\/folder\/[^/]+\/items\/?$|project\/[^/]+\/data\/?$)/.test(path2);
8836
+ const sharedReads = /* @__PURE__ */ new Map();
8749
8837
  const RETRY_JITTER_RATIO = 0.25;
8750
8838
  const sleep = (ms) => new Promise((resolve2) => setTimeout(resolve2, ms));
8751
8839
  function parseRetryAfter(response) {
@@ -8911,6 +8999,30 @@ class EngineServicesClient {
8911
8999
  return this.accessToken;
8912
9000
  }
8913
9001
  async #requestApi(method, path2, requestData) {
9002
+ if (method === "GET" && requestData?.responseType !== "blob" && !requestData?.retryAttempt) {
9003
+ const key = `${this.#buildUrl(path2)}?${new URLSearchParams(this.#cleanData(requestData?.query)).toString()}`;
9004
+ const now = Date.now();
9005
+ const kept = sharedReads.get(key);
9006
+ if (kept && (kept.pending || now - kept.at < (isListing(path2) ? LISTING_SHARED_MS : READ_SHARED_MS))) {
9007
+ return kept.answer.then((data) => data === void 0 ? data : structuredClone(data));
9008
+ }
9009
+ const entry = { at: now, pending: true, answer: this.#requestApiOnce(method, path2, requestData) };
9010
+ sharedReads.set(key, entry);
9011
+ entry.answer.then(
9012
+ () => {
9013
+ entry.pending = false;
9014
+ entry.at = Date.now();
9015
+ },
9016
+ () => {
9017
+ if (sharedReads.get(key) === entry) sharedReads.delete(key);
9018
+ }
9019
+ );
9020
+ return entry.answer.then((data) => data === void 0 ? data : structuredClone(data));
9021
+ }
9022
+ if (method !== "GET" && !isAsking(path2)) sharedReads.clear();
9023
+ return this.#requestApiOnce(method, path2, requestData);
9024
+ }
9025
+ async #requestApiOnce(method, path2, requestData) {
8914
9026
  const { body, query, contentType, retries, responseType, retryAttempt } = requestData || {};
8915
9027
  const url2 = this.#buildUrl(path2);
8916
9028
  const cleanQuery = this.#cleanData(query);
@@ -9010,6 +9122,16 @@ class EngineServicesClient {
9010
9122
  }
9011
9123
  });
9012
9124
  }
9125
+ /**
9126
+ * Every document descriptor of a project in one request, read by the
9127
+ * platform (`GET /item/documents`). Throws a {@link RequestError} like any
9128
+ * request; a platform from before the route answers 404 (or 400, taking
9129
+ * `documents` for an item id), which `documents.list()` reads as "ask one
9130
+ * by one".
9131
+ */
9132
+ async listDocumentDescriptors(projectId) {
9133
+ return await this.#requestApi("GET", `${ITEM_PATH}/documents`, { query: { projectId } });
9134
+ }
9013
9135
  /**
9014
9136
  * Gets a single file by ID, optionally including version history.
9015
9137
  * @param fileId - The file's unique identifier.
@@ -10090,12 +10212,16 @@ return main;`);
10090
10212
  }
10091
10213
  async #downloadItem(itemId, params) {
10092
10214
  const { versionTag, withDraft } = params || {};
10093
- return await this.#requestFile(`${ITEM_PATH}/${itemId}/download`, {
10215
+ const response = await this.#requestFile(`${ITEM_PATH}/${itemId}/download`, {
10094
10216
  query: {
10095
10217
  ...versionTag && { versionTag },
10096
10218
  ...withDraft && { withDraft }
10097
10219
  }
10098
10220
  });
10221
+ if (response.status === 429 || response.status >= 500) {
10222
+ throw new Error(`Downloading ${itemId} answered ${response.status}${response.status === 429 ? " (rate limited)" : ""}`);
10223
+ }
10224
+ return response;
10099
10225
  }
10100
10226
  async #createItem(fileData, itemType, extraProps) {
10101
10227
  const { name, versionTag, parentFolderId, projectId, file, metadata: metadata2 } = fileData;