@thatopen/services 0.30.0 → 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);
@@ -7437,6 +7442,10 @@ const DOCUMENT_TYPES = {
7437
7442
  bim: { label: "BIM model", icon: "tabler:building" },
7438
7443
  drawings: { label: "Drawings", icon: "tabler:layout-board" },
7439
7444
  reality: { label: "Reality capture", icon: "tabler:3d-cube-sphere" },
7445
+ // 🔴 The project's coordination record (Antonio, 22-10: «los topics sean su propio documento… no pertenecen a
7446
+ // ningún modelo, pueden hablar de varios a la vez»): its topics, as a BCF, with a history of their own. Named as a thing,
7447
+ // as a plan set is («Coordination set», Antonio 22-10).
7448
+ coordination: { label: "Coordination set", icon: "tabler:messages" },
7440
7449
  generic: { label: "Generic", icon: "tabler:file" }
7441
7450
  };
7442
7451
  const TYPE_OF_FORMAT = {
@@ -7444,6 +7453,7 @@ const TYPE_OF_FORMAT = {
7444
7453
  rfa: "bim",
7445
7454
  ifc: "bim",
7446
7455
  frag: "bim",
7456
+ tostrm: "bim",
7447
7457
  "3dm": "bim",
7448
7458
  nwd: "bim",
7449
7459
  dgn: "bim",
@@ -7456,7 +7466,9 @@ const TYPE_OF_FORMAT = {
7456
7466
  potree: "reality",
7457
7467
  splat: "reality",
7458
7468
  spz: "reality",
7459
- ksplat: "reality"
7469
+ ksplat: "reality",
7470
+ bcf: "coordination",
7471
+ bcfzip: "coordination"
7460
7472
  };
7461
7473
  function documentTypeOf(descriptor) {
7462
7474
  if (descriptor.type && descriptor.type in DOCUMENT_TYPES) {
@@ -7486,6 +7498,13 @@ const SELF_VIEWING_FORMATS = [
7486
7498
  function idOf$1(item) {
7487
7499
  return String(item._id ?? item.id ?? "");
7488
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
+ };
7489
7508
  class ProjectDocuments {
7490
7509
  constructor(client, projectId) {
7491
7510
  this.client = client;
@@ -7515,7 +7534,7 @@ class ProjectDocuments {
7515
7534
  const where = item;
7516
7535
  const folderId = String(where.folderId ?? where.parentFolderId ?? "");
7517
7536
  if (!folderId) return null;
7518
- const descriptor = await this.#readDescriptor(idOf$1(item));
7537
+ const descriptor = await this.#readDescriptor(idOf$1(item), stampOf(item));
7519
7538
  return descriptor ? { folderId, descriptor } : null;
7520
7539
  })
7521
7540
  );
@@ -7595,10 +7614,28 @@ class ProjectDocuments {
7595
7614
  async descriptor(folderId) {
7596
7615
  const file = await this.#descriptorFile(folderId);
7597
7616
  if (!file) return null;
7598
- return this.#readDescriptor(file.id);
7617
+ return this.#readDescriptor(file.id, file.stamp);
7599
7618
  }
7600
- 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) {
7601
7637
  const response = await this.client.downloadFile(fileId);
7638
+ if (!response.ok) throw new Error(`Reading a document's descriptor answered ${response.status}`);
7602
7639
  const text2 = await response.text();
7603
7640
  if (!text2.trim()) return null;
7604
7641
  let parsed;
@@ -7666,6 +7703,7 @@ class ProjectDocuments {
7666
7703
  });
7667
7704
  const existing = await this.#descriptorFile(folderId);
7668
7705
  if (existing) {
7706
+ descriptorReads.delete(existing.id);
7669
7707
  await this.client.updateFile(existing.id, {
7670
7708
  file: blob,
7671
7709
  versionTag: await this.client.nextVersionTag(existing.id)
@@ -7730,11 +7768,14 @@ class ProjectDocuments {
7730
7768
  return void 0;
7731
7769
  }
7732
7770
  async #descriptorFile(folderId) {
7733
- 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
+ });
7734
7775
  const found = items2.find(
7735
7776
  (item) => String(item.name ?? "") === DOCUMENT_FILE
7736
7777
  );
7737
- return found ? { id: idOf$1(found) } : void 0;
7778
+ return found ? { id: idOf$1(found), stamp: stampOf(found) } : void 0;
7738
7779
  }
7739
7780
  }
7740
7781
  const DRAFT_TAG = "draft:";
@@ -8739,6 +8780,8 @@ const ITEM_TYPE_COMPONENT = "TOOL";
8739
8780
  const ITEM_TYPE_APP = "APP";
8740
8781
  const RETRY_BASE_DELAY_MS = 500;
8741
8782
  const RETRY_MAX_DELAY_MS = 3e4;
8783
+ const READ_SHARED_MS = 2e3;
8784
+ const sharedReads = /* @__PURE__ */ new Map();
8742
8785
  const RETRY_JITTER_RATIO = 0.25;
8743
8786
  const sleep = (ms) => new Promise((resolve2) => setTimeout(resolve2, ms));
8744
8787
  function parseRetryAfter(response) {
@@ -8904,6 +8947,30 @@ class EngineServicesClient {
8904
8947
  return this.accessToken;
8905
8948
  }
8906
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) {
8907
8974
  const { body, query, contentType, retries, responseType, retryAttempt } = requestData || {};
8908
8975
  const url2 = this.#buildUrl(path2);
8909
8976
  const cleanQuery = this.#cleanData(query);
@@ -9821,29 +9888,6 @@ return main;`);
9821
9888
  { ...expiresIn != null && { query: { expiresIn } } }
9822
9889
  );
9823
9890
  }
9824
- /**
9825
- * A short-lived presigned URL for a file's own content (one version of it),
9826
- * fetched straight from storage with HTTP `Range` requests: how a streamed
9827
- * model's bundle is read by pieces. The same as
9828
- * {@link getHiddenFileSignedUrl}, for a regular file. Storage serves ONE
9829
- * range per request: ask several pieces in parallel, not in one request.
9830
- *
9831
- * @param fileId - The file's item id.
9832
- * @param options - `versionTag` (default: the latest), `expiresIn` in
9833
- * seconds (60–3600, default 900).
9834
- * @returns `{ url, expiresAt }`.
9835
- */
9836
- async getFileSignedUrl(fileId, options) {
9837
- const query = {
9838
- ...options?.versionTag && { versionTag: options.versionTag },
9839
- ...options?.expiresIn != null && { expiresIn: options.expiresIn }
9840
- };
9841
- return await this.#requestApi(
9842
- "GET",
9843
- `${ITEM_PATH}/${encodeURIComponent(fileId)}/signed-url`,
9844
- { ...Object.keys(query).length > 0 && { query } }
9845
- );
9846
- }
9847
9891
  /**
9848
9892
  * Batch variant of {@link getHiddenFileSignedUrl}. Mints presigned URLs for
9849
9893
  * many hidden files in one request.
@@ -10106,12 +10150,16 @@ return main;`);
10106
10150
  }
10107
10151
  async #downloadItem(itemId, params) {
10108
10152
  const { versionTag, withDraft } = params || {};
10109
- return await this.#requestFile(`${ITEM_PATH}/${itemId}/download`, {
10153
+ const response = await this.#requestFile(`${ITEM_PATH}/${itemId}/download`, {
10110
10154
  query: {
10111
10155
  ...versionTag && { versionTag },
10112
10156
  ...withDraft && { withDraft }
10113
10157
  }
10114
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;
10115
10163
  }
10116
10164
  async #createItem(fileData, itemType, extraProps) {
10117
10165
  const { name, versionTag, parentFolderId, projectId, file, metadata: metadata2 } = fileData;