@arbidocs/sdk 0.4.81 → 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/browser.cjs CHANGED
@@ -5211,11 +5211,11 @@ async function deleteTag(arbi, tagId) {
5211
5211
  "Failed to delete tag"
5212
5212
  );
5213
5213
  }
5214
- async function updateTag(arbi, tagId, body) {
5214
+ async function updateTag(arbi, tagId, change) {
5215
5215
  return requireData(
5216
5216
  await arbi.fetch.PATCH("/v1/tag/{tag_ext_id}", {
5217
5217
  params: { path: { tag_ext_id: tagId } },
5218
- body
5218
+ body: { kind_to_children: false, ...change }
5219
5219
  }),
5220
5220
  "Failed to update tag"
5221
5221
  );
@@ -7073,6 +7073,8 @@ var Arbi = class {
7073
7073
  userProfile = null;
7074
7074
  profileListeners = /* @__PURE__ */ new Set();
7075
7075
  currentWorkspaceId = null;
7076
+ /** Wrapped workspace keys seen in the last list/create, so selecting needs no re-fetch. */
7077
+ wrappedKeys = /* @__PURE__ */ new Map();
7076
7078
  dmCryptoContext = null;
7077
7079
  sessionExpiredListeners = /* @__PURE__ */ new Set();
7078
7080
  options;
@@ -7152,47 +7154,79 @@ var Arbi = class {
7152
7154
  this.userProfile = this.loginResult.profile ?? null;
7153
7155
  return true;
7154
7156
  }
7157
+ /** Remember the wrapped keys of workspaces the server just returned. */
7158
+ rememberWorkspaces(workspaces) {
7159
+ for (const ws of workspaces) {
7160
+ if (ws.wrapped_key) this.wrappedKeys.set(ws.external_id, ws.wrapped_key);
7161
+ }
7162
+ return workspaces;
7163
+ }
7164
+ /**
7165
+ * A workspace's wrapped key: from the cache when a list or create already returned it, else from
7166
+ * a fresh workspace list (a workspace created or shared since the last list).
7167
+ */
7168
+ async wrappedKeyFor(workspaceId, refresh2 = false) {
7169
+ const cached = refresh2 ? void 0 : this.wrappedKeys.get(workspaceId);
7170
+ if (cached) return { wrappedKey: cached, fromCache: true };
7171
+ this.wrappedKeys.delete(workspaceId);
7172
+ this.rememberWorkspaces(await listWorkspaces(this.requireClient()));
7173
+ const wrappedKey = this.wrappedKeys.get(workspaceId);
7174
+ if (!wrappedKey) {
7175
+ throw new ArbiError(`Workspace ${workspaceId} not found or has no encryption key`);
7176
+ }
7177
+ return { wrappedKey, fromCache: false };
7178
+ }
7179
+ /**
7180
+ * Run `withKey` with the workspace's wrapped key. A cached key that no longer works (access was
7181
+ * re-granted with a new key) is dropped and the call retried once with a fresh list.
7182
+ */
7183
+ async withWrappedKey(workspaceId, withKey) {
7184
+ const { wrappedKey, fromCache } = await this.wrappedKeyFor(workspaceId);
7185
+ try {
7186
+ return await withKey(wrappedKey);
7187
+ } catch (error) {
7188
+ if (!fromCache) throw error;
7189
+ const fresh = await this.wrappedKeyFor(workspaceId, true);
7190
+ return withKey(fresh.wrappedKey);
7191
+ }
7192
+ }
7155
7193
  /** A workspace's key, sealed to the caller's own session (what `/open` and `POST /workspace/users` take). */
7156
7194
  async sealWorkspaceKey(workspaceId) {
7157
7195
  const client = this.requireClient();
7158
7196
  const lr2 = this.requireLogin();
7159
- const all = await listWorkspaces(client);
7160
- const ws = all.find((w2) => w2.external_id === workspaceId);
7161
- if (!ws || !ws.wrapped_key) {
7162
- throw new ArbiError(`Workspace ${workspaceId} not found or has no encryption key`);
7163
- }
7164
- return generateEncryptedWorkspaceKey(
7165
- client,
7166
- ws.wrapped_key,
7167
- lr2.serverSessionKey,
7168
- client.crypto.bytesToBase64(lr2.signingPrivateKey)
7197
+ return this.withWrappedKey(
7198
+ workspaceId,
7199
+ (wrappedKey) => generateEncryptedWorkspaceKey(
7200
+ client,
7201
+ wrappedKey,
7202
+ lr2.serverSessionKey,
7203
+ client.crypto.bytesToBase64(lr2.signingPrivateKey)
7204
+ )
7169
7205
  );
7170
7206
  }
7171
7207
  /**
7172
- * Select a workspace by ID. Fetches the workspace list, finds the matching
7173
- * workspace, decrypts the wrapped key, and sets up auth headers.
7208
+ * Select a workspace by ID. Uses the wrapped key from the last workspace list or create (the
7209
+ * list is fetched only when the workspace is not known yet), decrypts it, and opens the
7210
+ * workspace on the server.
7174
7211
  */
7175
7212
  async selectWorkspace(workspaceId) {
7176
7213
  const client = this.requireClient();
7177
7214
  const lr2 = this.requireLogin();
7178
- const allWorkspaces = await listWorkspaces(client);
7179
- const ws = allWorkspaces.find((w2) => w2.external_id === workspaceId);
7180
- if (!ws || !ws.wrapped_key) {
7181
- throw new ArbiError(`Workspace ${workspaceId} not found or has no encryption key`);
7182
- }
7183
7215
  const signingPrivateKeyBase64 = client.crypto.bytesToBase64(lr2.signingPrivateKey);
7184
- const sealedKey = await selectWorkspace(
7185
- client,
7186
- ws.external_id,
7187
- ws.wrapped_key,
7188
- lr2.serverSessionKey,
7189
- signingPrivateKeyBase64
7190
- );
7191
- await client.fetch.POST("/v1/workspace/{workspace_ext_id}/open", {
7192
- params: { path: { workspace_ext_id: ws.external_id } },
7193
- body: { workspace_key: sealedKey }
7216
+ await this.withWrappedKey(workspaceId, async (wrappedKey) => {
7217
+ const sealedKey = await selectWorkspace(
7218
+ client,
7219
+ workspaceId,
7220
+ wrappedKey,
7221
+ lr2.serverSessionKey,
7222
+ signingPrivateKeyBase64
7223
+ );
7224
+ await client.fetch.POST("/v1/workspace/{workspace_ext_id}/open", {
7225
+ params: { path: { workspace_ext_id: workspaceId } },
7226
+ body: { workspace_key: sealedKey }
7227
+ });
7194
7228
  });
7195
- this.currentWorkspaceId = ws.external_id;
7229
+ this.currentWorkspaceId = workspaceId;
7196
7230
  }
7197
7231
  /**
7198
7232
  * Subscribe to "the session cannot be recovered". Fires when a request failed with 401 or
@@ -7212,6 +7246,7 @@ var Arbi = class {
7212
7246
  this.loginResult = null;
7213
7247
  this.userProfile = null;
7214
7248
  this.currentWorkspaceId = null;
7249
+ this.wrappedKeys.clear();
7215
7250
  this.dmCryptoContext = null;
7216
7251
  this.client?.session.clear();
7217
7252
  for (const listener of [...this.sessionExpiredListeners]) listener();
@@ -7257,6 +7292,7 @@ var Arbi = class {
7257
7292
  this.loginResult = null;
7258
7293
  this.userProfile = null;
7259
7294
  this.currentWorkspaceId = null;
7295
+ this.wrappedKeys.clear();
7260
7296
  this.dmCryptoContext = null;
7261
7297
  }
7262
7298
  // ── Accessors ─────────────────────────────────────────────────────────
@@ -7398,6 +7434,7 @@ var Arbi = class {
7398
7434
  }
7399
7435
  );
7400
7436
  this.loginResult = { ...lr2, signingPrivateKey: result.newSigningPrivateKey };
7437
+ this.wrappedKeys.clear();
7401
7438
  return result;
7402
7439
  },
7403
7440
  /** Close the account (soft delete) and sign out. Refused while a subscription is active. */
@@ -7452,7 +7489,7 @@ var Arbi = class {
7452
7489
  }
7453
7490
  // ── Workspaces ────────────────────────────────────────────────────────
7454
7491
  workspaces = {
7455
- list: () => listWorkspaces(this.requireClient()),
7492
+ list: async () => this.rememberWorkspaces(await listWorkspaces(this.requireClient())),
7456
7493
  create: async (name, projectExtId, description, isPublic) => {
7457
7494
  const client = this.requireClient();
7458
7495
  const lr2 = this.requireLogin();
@@ -7461,7 +7498,7 @@ var Arbi = class {
7461
7498
  await sodium.ready;
7462
7499
  const workspaceKey = sodium.randombytes_buf(32);
7463
7500
  const encryptedKey = await client.crypto.sealKeyForSession(workspaceKey, lr2.serverSessionKey);
7464
- return createWorkspace(
7501
+ const created = await createWorkspace(
7465
7502
  client,
7466
7503
  name,
7467
7504
  encryptedKey,
@@ -7469,8 +7506,13 @@ var Arbi = class {
7469
7506
  description,
7470
7507
  isPublic
7471
7508
  );
7509
+ this.rememberWorkspaces([created]);
7510
+ return created;
7511
+ },
7512
+ delete: async (workspaceIds) => {
7513
+ await deleteWorkspaces(this.requireClient(), workspaceIds);
7514
+ for (const id of workspaceIds) this.wrappedKeys.delete(id);
7472
7515
  },
7473
- delete: (workspaceIds) => deleteWorkspaces(this.requireClient(), workspaceIds),
7474
7516
  update: (body) => updateWorkspace(this.requireClient(), body),
7475
7517
  listUsers: () => listWorkspaceUsers(this.requireClient()),
7476
7518
  addUsers: (emails, role) => addWorkspaceUsers(this.requireClient(), emails, role),
@@ -7741,7 +7783,7 @@ var Arbi = class {
7741
7783
  workspaceId: options.workspaceId ?? this.requireWorkspace()
7742
7784
  }),
7743
7785
  delete: (tagId) => deleteTag(this.requireClient(), tagId),
7744
- update: (tagId, body) => updateTag(this.requireClient(), tagId, body),
7786
+ update: (tagId, change) => updateTag(this.requireClient(), tagId, change),
7745
7787
  /** Replace a tag's own field definitions and/or merge-patch its values (`null` clears one). */
7746
7788
  updateFields: (tagId, change) => updateTagFields(this.requireClient(), tagId, change),
7747
7789
  /** Tags as rows (a parent's children, or the given tags) with field values, docs and facts. */