@arbidocs/sdk 0.4.82 → 0.4.83

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/index.cjs CHANGED
@@ -5760,11 +5760,11 @@ async function deleteTag(arbi, tagId) {
5760
5760
  "Failed to delete tag"
5761
5761
  );
5762
5762
  }
5763
- async function updateTag(arbi, tagId, body) {
5763
+ async function updateTag(arbi, tagId, change) {
5764
5764
  return requireData(
5765
5765
  await arbi.fetch.PATCH("/v1/tag/{tag_ext_id}", {
5766
5766
  params: { path: { tag_ext_id: tagId } },
5767
- body
5767
+ body: { kind_to_children: false, ...change }
5768
5768
  }),
5769
5769
  "Failed to update tag"
5770
5770
  );
@@ -7637,6 +7637,8 @@ var Arbi = class {
7637
7637
  userProfile = null;
7638
7638
  profileListeners = /* @__PURE__ */ new Set();
7639
7639
  currentWorkspaceId = null;
7640
+ /** Wrapped workspace keys seen in the last list/create, so selecting needs no re-fetch. */
7641
+ wrappedKeys = /* @__PURE__ */ new Map();
7640
7642
  dmCryptoContext = null;
7641
7643
  sessionExpiredListeners = /* @__PURE__ */ new Set();
7642
7644
  options;
@@ -7716,47 +7718,79 @@ var Arbi = class {
7716
7718
  this.userProfile = this.loginResult.profile ?? null;
7717
7719
  return true;
7718
7720
  }
7721
+ /** Remember the wrapped keys of workspaces the server just returned. */
7722
+ rememberWorkspaces(workspaces) {
7723
+ for (const ws of workspaces) {
7724
+ if (ws.wrapped_key) this.wrappedKeys.set(ws.external_id, ws.wrapped_key);
7725
+ }
7726
+ return workspaces;
7727
+ }
7728
+ /**
7729
+ * A workspace's wrapped key: from the cache when a list or create already returned it, else from
7730
+ * a fresh workspace list (a workspace created or shared since the last list).
7731
+ */
7732
+ async wrappedKeyFor(workspaceId, refresh2 = false) {
7733
+ const cached = refresh2 ? void 0 : this.wrappedKeys.get(workspaceId);
7734
+ if (cached) return { wrappedKey: cached, fromCache: true };
7735
+ this.wrappedKeys.delete(workspaceId);
7736
+ this.rememberWorkspaces(await listWorkspaces(this.requireClient()));
7737
+ const wrappedKey = this.wrappedKeys.get(workspaceId);
7738
+ if (!wrappedKey) {
7739
+ throw new ArbiError(`Workspace ${workspaceId} not found or has no encryption key`);
7740
+ }
7741
+ return { wrappedKey, fromCache: false };
7742
+ }
7743
+ /**
7744
+ * Run `withKey` with the workspace's wrapped key. A cached key that no longer works (access was
7745
+ * re-granted with a new key) is dropped and the call retried once with a fresh list.
7746
+ */
7747
+ async withWrappedKey(workspaceId, withKey) {
7748
+ const { wrappedKey, fromCache } = await this.wrappedKeyFor(workspaceId);
7749
+ try {
7750
+ return await withKey(wrappedKey);
7751
+ } catch (error) {
7752
+ if (!fromCache) throw error;
7753
+ const fresh = await this.wrappedKeyFor(workspaceId, true);
7754
+ return withKey(fresh.wrappedKey);
7755
+ }
7756
+ }
7719
7757
  /** A workspace's key, sealed to the caller's own session (what `/open` and `POST /workspace/users` take). */
7720
7758
  async sealWorkspaceKey(workspaceId) {
7721
7759
  const client = this.requireClient();
7722
7760
  const lr2 = this.requireLogin();
7723
- const all = await listWorkspaces(client);
7724
- const ws = all.find((w2) => w2.external_id === workspaceId);
7725
- if (!ws || !ws.wrapped_key) {
7726
- throw new ArbiError(`Workspace ${workspaceId} not found or has no encryption key`);
7727
- }
7728
- return generateEncryptedWorkspaceKey(
7729
- client,
7730
- ws.wrapped_key,
7731
- lr2.serverSessionKey,
7732
- client.crypto.bytesToBase64(lr2.signingPrivateKey)
7761
+ return this.withWrappedKey(
7762
+ workspaceId,
7763
+ (wrappedKey) => generateEncryptedWorkspaceKey(
7764
+ client,
7765
+ wrappedKey,
7766
+ lr2.serverSessionKey,
7767
+ client.crypto.bytesToBase64(lr2.signingPrivateKey)
7768
+ )
7733
7769
  );
7734
7770
  }
7735
7771
  /**
7736
- * Select a workspace by ID. Fetches the workspace list, finds the matching
7737
- * workspace, decrypts the wrapped key, and sets up auth headers.
7772
+ * Select a workspace by ID. Uses the wrapped key from the last workspace list or create (the
7773
+ * list is fetched only when the workspace is not known yet), decrypts it, and opens the
7774
+ * workspace on the server.
7738
7775
  */
7739
7776
  async selectWorkspace(workspaceId) {
7740
7777
  const client = this.requireClient();
7741
7778
  const lr2 = this.requireLogin();
7742
- const allWorkspaces = await listWorkspaces(client);
7743
- const ws = allWorkspaces.find((w2) => w2.external_id === workspaceId);
7744
- if (!ws || !ws.wrapped_key) {
7745
- throw new ArbiError(`Workspace ${workspaceId} not found or has no encryption key`);
7746
- }
7747
7779
  const signingPrivateKeyBase64 = client.crypto.bytesToBase64(lr2.signingPrivateKey);
7748
- const sealedKey = await selectWorkspace(
7749
- client,
7750
- ws.external_id,
7751
- ws.wrapped_key,
7752
- lr2.serverSessionKey,
7753
- signingPrivateKeyBase64
7754
- );
7755
- await client.fetch.POST("/v1/workspace/{workspace_ext_id}/open", {
7756
- params: { path: { workspace_ext_id: ws.external_id } },
7757
- body: { workspace_key: sealedKey }
7780
+ await this.withWrappedKey(workspaceId, async (wrappedKey) => {
7781
+ const sealedKey = await selectWorkspace(
7782
+ client,
7783
+ workspaceId,
7784
+ wrappedKey,
7785
+ lr2.serverSessionKey,
7786
+ signingPrivateKeyBase64
7787
+ );
7788
+ await client.fetch.POST("/v1/workspace/{workspace_ext_id}/open", {
7789
+ params: { path: { workspace_ext_id: workspaceId } },
7790
+ body: { workspace_key: sealedKey }
7791
+ });
7758
7792
  });
7759
- this.currentWorkspaceId = ws.external_id;
7793
+ this.currentWorkspaceId = workspaceId;
7760
7794
  }
7761
7795
  /**
7762
7796
  * Subscribe to "the session cannot be recovered". Fires when a request failed with 401 or
@@ -7776,6 +7810,7 @@ var Arbi = class {
7776
7810
  this.loginResult = null;
7777
7811
  this.userProfile = null;
7778
7812
  this.currentWorkspaceId = null;
7813
+ this.wrappedKeys.clear();
7779
7814
  this.dmCryptoContext = null;
7780
7815
  this.client?.session.clear();
7781
7816
  for (const listener of [...this.sessionExpiredListeners]) listener();
@@ -7821,6 +7856,7 @@ var Arbi = class {
7821
7856
  this.loginResult = null;
7822
7857
  this.userProfile = null;
7823
7858
  this.currentWorkspaceId = null;
7859
+ this.wrappedKeys.clear();
7824
7860
  this.dmCryptoContext = null;
7825
7861
  }
7826
7862
  // ── Accessors ─────────────────────────────────────────────────────────
@@ -7962,6 +7998,7 @@ var Arbi = class {
7962
7998
  }
7963
7999
  );
7964
8000
  this.loginResult = { ...lr2, signingPrivateKey: result.newSigningPrivateKey };
8001
+ this.wrappedKeys.clear();
7965
8002
  return result;
7966
8003
  },
7967
8004
  /** Close the account (soft delete) and sign out. Refused while a subscription is active. */
@@ -8016,7 +8053,7 @@ var Arbi = class {
8016
8053
  }
8017
8054
  // ── Workspaces ────────────────────────────────────────────────────────
8018
8055
  workspaces = {
8019
- list: () => listWorkspaces(this.requireClient()),
8056
+ list: async () => this.rememberWorkspaces(await listWorkspaces(this.requireClient())),
8020
8057
  create: async (name, projectExtId, description, isPublic) => {
8021
8058
  const client = this.requireClient();
8022
8059
  const lr2 = this.requireLogin();
@@ -8025,7 +8062,7 @@ var Arbi = class {
8025
8062
  await sodium.ready;
8026
8063
  const workspaceKey = sodium.randombytes_buf(32);
8027
8064
  const encryptedKey = await client.crypto.sealKeyForSession(workspaceKey, lr2.serverSessionKey);
8028
- return createWorkspace(
8065
+ const created = await createWorkspace(
8029
8066
  client,
8030
8067
  name,
8031
8068
  encryptedKey,
@@ -8033,8 +8070,13 @@ var Arbi = class {
8033
8070
  description,
8034
8071
  isPublic
8035
8072
  );
8073
+ this.rememberWorkspaces([created]);
8074
+ return created;
8075
+ },
8076
+ delete: async (workspaceIds) => {
8077
+ await deleteWorkspaces(this.requireClient(), workspaceIds);
8078
+ for (const id of workspaceIds) this.wrappedKeys.delete(id);
8036
8079
  },
8037
- delete: (workspaceIds) => deleteWorkspaces(this.requireClient(), workspaceIds),
8038
8080
  update: (body) => updateWorkspace(this.requireClient(), body),
8039
8081
  listUsers: () => listWorkspaceUsers(this.requireClient()),
8040
8082
  addUsers: (emails, role) => addWorkspaceUsers(this.requireClient(), emails, role),
@@ -8305,7 +8347,7 @@ var Arbi = class {
8305
8347
  workspaceId: options.workspaceId ?? this.requireWorkspace()
8306
8348
  }),
8307
8349
  delete: (tagId) => deleteTag(this.requireClient(), tagId),
8308
- update: (tagId, body) => updateTag(this.requireClient(), tagId, body),
8350
+ update: (tagId, change) => updateTag(this.requireClient(), tagId, change),
8309
8351
  /** Replace a tag's own field definitions and/or merge-patch its values (`null` clears one). */
8310
8352
  updateFields: (tagId, change) => updateTagFields(this.requireClient(), tagId, change),
8311
8353
  /** Tags as rows (a parent's children, or the given tags) with field values, docs and facts. */