@arbidocs/sdk 0.4.95 → 0.4.96

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.js CHANGED
@@ -4079,6 +4079,12 @@ async function getRawWorkspaceKey(arbi, workspaceId, signingPrivateKeyBase64) {
4079
4079
  });
4080
4080
  return sealedBoxDecrypt(ws.wrapped_key, encryptionKeyPair.secretKey);
4081
4081
  }
4082
+ var StaleSessionError = class extends ArbiError {
4083
+ constructor(message) {
4084
+ super(message);
4085
+ this.name = "StaleSessionError";
4086
+ }
4087
+ };
4082
4088
  async function openWorkspaceEntry(arbi, ws, serverSessionKey, signingPrivateKeyBase64) {
4083
4089
  if (ws.wrapped_key) {
4084
4090
  const sealedKey = await selectWorkspace(
@@ -4088,12 +4094,13 @@ async function openWorkspaceEntry(arbi, ws, serverSessionKey, signingPrivateKeyB
4088
4094
  serverSessionKey,
4089
4095
  signingPrivateKeyBase64
4090
4096
  );
4091
- const { error: error2 } = await arbi.fetch.POST("/v1/workspace/{workspace_ext_id}/open", {
4097
+ const { error: error2, response } = await arbi.fetch.POST("/v1/workspace/{workspace_ext_id}/open", {
4092
4098
  params: { path: { workspace_ext_id: ws.external_id } },
4093
4099
  body: { workspace_key: sealedKey }
4094
4100
  });
4095
4101
  if (error2) {
4096
- throw new ArbiError(`Workspace open failed for ${ws.external_id}: ${JSON.stringify(error2)}`);
4102
+ const message = `Workspace open failed for ${ws.external_id}: ${JSON.stringify(error2)}`;
4103
+ throw response.status === 401 ? new StaleSessionError(message) : new ArbiError(message);
4097
4104
  }
4098
4105
  return sealedKey;
4099
4106
  }
@@ -4152,6 +4159,28 @@ async function resolveAuth(store) {
4152
4159
  const { arbi, loginResult } = await createAuthenticatedClient(config, creds, store);
4153
4160
  return { arbi, loginResult, config };
4154
4161
  }
4162
+ async function openOnCachedSession(store, config, creds, workspaceId) {
4163
+ const { arbi, loginResult } = await buildAuthFromCache(config, creds, store);
4164
+ arbi.session.setSelectedWorkspace(workspaceId);
4165
+ await selectWorkspaceById(
4166
+ arbi,
4167
+ workspaceId,
4168
+ loginResult.serverSessionKey,
4169
+ creds.signingPrivateKeyBase64
4170
+ );
4171
+ const nextOpened = Array.from(/* @__PURE__ */ new Set([...creds.openedWorkspaces ?? [], workspaceId]));
4172
+ store.saveCredentials({
4173
+ ...store.requireCredentials(),
4174
+ openedWorkspaces: nextOpened
4175
+ });
4176
+ return {
4177
+ arbi,
4178
+ loginResult,
4179
+ config,
4180
+ workspaceId,
4181
+ accessToken: creds.accessToken
4182
+ };
4183
+ }
4155
4184
  async function resolveWorkspace(store, workspaceOpt) {
4156
4185
  const config = store.requireConfig();
4157
4186
  const creds = store.requireCredentials();
@@ -4160,26 +4189,11 @@ async function resolveWorkspace(store, workspaceOpt) {
4160
4189
  throw new ArbiError("No workspace selected. Run: arbi workspace select <id>");
4161
4190
  }
4162
4191
  if (isSessionAlive(creds)) {
4163
- const { arbi: arbi2, loginResult: loginResult2 } = await buildAuthFromCache(config, creds, store);
4164
- arbi2.session.setSelectedWorkspace(workspaceId);
4165
- await selectWorkspaceById(
4166
- arbi2,
4167
- workspaceId,
4168
- loginResult2.serverSessionKey,
4169
- creds.signingPrivateKeyBase64
4170
- );
4171
- const nextOpened = Array.from(/* @__PURE__ */ new Set([...creds.openedWorkspaces ?? [], workspaceId]));
4172
- store.saveCredentials({
4173
- ...store.requireCredentials(),
4174
- openedWorkspaces: nextOpened
4175
- });
4176
- return {
4177
- arbi: arbi2,
4178
- loginResult: loginResult2,
4179
- config,
4180
- workspaceId,
4181
- accessToken: creds.accessToken
4182
- };
4192
+ try {
4193
+ return await openOnCachedSession(store, config, creds, workspaceId);
4194
+ } catch (err) {
4195
+ if (!(err instanceof StaleSessionError)) throw err;
4196
+ }
4183
4197
  }
4184
4198
  const { arbi, loginResult } = await createAuthenticatedClient(config, creds, store);
4185
4199
  await selectWorkspaceById(