@tinycloud/node-sdk 3.1.0-beta.5 → 3.1.0-beta.7

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
@@ -17371,6 +17371,10 @@ var MemorySessionStorage = class {
17371
17371
  // src/authorization/NodeUserAuthorization.ts
17372
17372
  var DECRYPT_ACTION = import_sdk_core2.ENCRYPTION.DECRYPT;
17373
17373
  var NETWORK_CREATE_ACTION = import_sdk_core2.ENCRYPTION.NETWORK_CREATE;
17374
+ function describeHostFailure(result) {
17375
+ const text = result.error?.trim();
17376
+ return text ? `${result.status} - ${text}` : `${result.status}`;
17377
+ }
17374
17378
  function canonicalizeOpenKeyManifestJson(value) {
17375
17379
  const json = JSON.stringify(value);
17376
17380
  if (json === void 0) {
@@ -17899,10 +17903,12 @@ var NodeUserAuthorization = class {
17899
17903
  return;
17900
17904
  }
17901
17905
  try {
17902
- const created = (await this.hostSpace()).success;
17903
- if (!created) {
17904
- const err = new Error(`Failed to create space: ${primarySpaceId}`);
17905
- handler.onSpaceCreationFailed?.(creationContext, err);
17906
+ const created = await this.hostSpace();
17907
+ if (!created.success) {
17908
+ const err = Object.assign(
17909
+ new Error(`Failed to create space ${primarySpaceId}: ${describeHostFailure(created)}`),
17910
+ { cause: created }
17911
+ );
17906
17912
  throw err;
17907
17913
  }
17908
17914
  } catch (error) {
@@ -17918,8 +17924,9 @@ var NodeUserAuthorization = class {
17918
17924
  this._tinyCloudSession.delegationHeader
17919
17925
  );
17920
17926
  if (!retryResult.success) {
17921
- const err = new Error(
17922
- `Failed to activate session after creating space: ${retryResult.error}`
17927
+ const err = Object.assign(
17928
+ new Error(`Failed to activate session after creating space: ${describeHostFailure(retryResult)}`),
17929
+ { cause: retryResult }
17923
17930
  );
17924
17931
  handler.onSpaceCreationFailed?.(creationContext, err);
17925
17932
  throw err;
@@ -17936,10 +17943,12 @@ var NodeUserAuthorization = class {
17936
17943
  return;
17937
17944
  }
17938
17945
  try {
17939
- const created = (await this.hostSpace()).success;
17940
- if (!created) {
17941
- const err = new Error(`Failed to create space: ${primarySpaceId}`);
17942
- handler.onSpaceCreationFailed?.(creationContext, err);
17946
+ const created = await this.hostSpace();
17947
+ if (!created.success) {
17948
+ const err = Object.assign(
17949
+ new Error(`Failed to create space ${primarySpaceId}: ${describeHostFailure(created)}`),
17950
+ { cause: created }
17951
+ );
17943
17952
  throw err;
17944
17953
  }
17945
17954
  } catch (error) {
@@ -17955,8 +17964,9 @@ var NodeUserAuthorization = class {
17955
17964
  this._tinyCloudSession.delegationHeader
17956
17965
  );
17957
17966
  if (!retryResult.success) {
17958
- const err = new Error(
17959
- `Failed to activate session after creating space: ${retryResult.error}`
17967
+ const err = Object.assign(
17968
+ new Error(`Failed to activate session after creating space: ${describeHostFailure(retryResult)}`),
17969
+ { cause: retryResult }
17960
17970
  );
17961
17971
  handler.onSpaceCreationFailed?.(creationContext, err);
17962
17972
  throw err;
@@ -17964,7 +17974,10 @@ var NodeUserAuthorization = class {
17964
17974
  handler.onSpaceCreated?.(creationContext);
17965
17975
  return;
17966
17976
  }
17967
- throw new Error(`Failed to activate session: ${result.error}`);
17977
+ throw Object.assign(
17978
+ new Error(`Failed to activate session: ${describeHostFailure(result)}`),
17979
+ { cause: result }
17980
+ );
17968
17981
  }
17969
17982
  recordActivationSkippedSpaces(result, primarySpaceId) {
17970
17983
  if (result.success) {
@@ -19435,7 +19448,7 @@ function persistedExpiry(value) {
19435
19448
  function sameInstant(left, right) {
19436
19449
  return left.getTime() === right.getTime();
19437
19450
  }
19438
- function describeHostFailure(result) {
19451
+ function describeHostFailure2(result) {
19439
19452
  const text = result.error?.trim();
19440
19453
  return text ? `${result.status} - ${text}` : `${result.status}`;
19441
19454
  }
@@ -20242,7 +20255,10 @@ var _TinyCloudNode = class _TinyCloudNode {
20242
20255
  marker
20243
20256
  );
20244
20257
  if (!written.ok) {
20245
- throw new Error(`Failed to write bootstrap completion marker: ${written.error.message}`);
20258
+ throw Object.assign(
20259
+ new Error(`Failed to write bootstrap completion marker: ${written.error.message}`),
20260
+ { cause: written.error }
20261
+ );
20246
20262
  }
20247
20263
  }
20248
20264
  isAcceptedBootstrapCompletionMarker(value) {
@@ -20288,17 +20304,23 @@ var _TinyCloudNode = class _TinyCloudNode {
20288
20304
  rawAbilities
20289
20305
  });
20290
20306
  } catch (err) {
20291
- throw new Error(
20292
- `Account bootstrap aborted: signature rejected for space "${step.space}". Cause: ${err instanceof Error ? err.message : String(err)}`
20307
+ throw Object.assign(
20308
+ new Error(
20309
+ `Account bootstrap aborted: signature rejected for space "${step.space}". Cause: ${err instanceof Error ? err.message : String(err)}`
20310
+ ),
20311
+ { cause: err }
20293
20312
  );
20294
20313
  }
20295
20314
  sessions.set(step.space, session);
20296
20315
  }
20297
20316
  for (const step of steps) {
20298
20317
  if (step.kind !== "host") continue;
20299
- const hosted = await auth.hostOwnedSpace(step.spaceId, "bootstrap-host");
20300
- if (!hosted) {
20301
- throw new Error(`Failed to host bootstrap space: ${step.spaceId}`);
20318
+ const hosted = await auth.hostOwnedSpaceResult(step.spaceId, "bootstrap-host");
20319
+ if (!hosted.success) {
20320
+ throw Object.assign(
20321
+ new Error(`Failed to host bootstrap space ${step.spaceId}: ${describeHostFailure2(hosted)}`),
20322
+ { cause: hosted }
20323
+ );
20302
20324
  }
20303
20325
  }
20304
20326
  await new Promise((resolve) => setTimeout(resolve, 100));
@@ -20310,8 +20332,11 @@ var _TinyCloudNode = class _TinyCloudNode {
20310
20332
  }
20311
20333
  const activated = await (0, import_sdk_core7.activateSessionWithHost)(host, session.delegationHeader);
20312
20334
  if (!activated.success || activated.skipped?.includes(step.spaceId)) {
20313
- throw new Error(
20314
- `Failed to activate bootstrap session for ${step.spaceId}: ${activated.error ?? "space was skipped"}`
20335
+ throw Object.assign(
20336
+ new Error(
20337
+ `Failed to activate bootstrap session for ${step.spaceId}: ${activated.success ? "space was skipped" : describeHostFailure2(activated)}`
20338
+ ),
20339
+ { cause: activated }
20315
20340
  );
20316
20341
  }
20317
20342
  this.registerBootstrapRuntimeGrant(
@@ -20324,7 +20349,7 @@ var _TinyCloudNode = class _TinyCloudNode {
20324
20349
  if (step.kind === "account-index-schema") {
20325
20350
  const ensured = await this.account.index.ensure();
20326
20351
  if (!ensured.ok) {
20327
- throw new Error(`Failed to create account index schema: ${ensured.error.message}`);
20352
+ throw Object.assign(new Error(`Failed to create account index schema: ${ensured.error.message}`), { cause: ensured.error });
20328
20353
  }
20329
20354
  }
20330
20355
  if (step.kind === "seed-spaces") {
@@ -20339,8 +20364,9 @@ var _TinyCloudNode = class _TinyCloudNode {
20339
20364
  }))
20340
20365
  );
20341
20366
  if (!registered.ok) {
20342
- throw new Error(
20343
- `Failed to seed account spaces: ${registered.error.message}`
20367
+ throw Object.assign(
20368
+ new Error(`Failed to seed account spaces: ${registered.error.message}`),
20369
+ { cause: registered.error }
20344
20370
  );
20345
20371
  }
20346
20372
  const batchError = registered.data.recoveredFromBatchError;
@@ -20361,7 +20387,7 @@ var _TinyCloudNode = class _TinyCloudNode {
20361
20387
  { assumeUnregistered: true }
20362
20388
  );
20363
20389
  if (!registered.ok) {
20364
- throw new Error(`Failed to seed bootstrap applications: ${registered.error.message}`);
20390
+ throw Object.assign(new Error(`Failed to seed bootstrap applications: ${registered.error.message}`), { cause: registered.error });
20365
20391
  }
20366
20392
  }
20367
20393
  if (step.kind === "encryption-network-create") {
@@ -20379,8 +20405,9 @@ var _TinyCloudNode = class _TinyCloudNode {
20379
20405
  ]
20380
20406
  });
20381
20407
  if (!migrated.ok) {
20382
- throw new Error(
20383
- `Failed to create secret_records schema: ${migrated.error.message}`
20408
+ throw Object.assign(
20409
+ new Error(`Failed to create secret_records schema: ${migrated.error.message}`),
20410
+ { cause: migrated.error }
20384
20411
  );
20385
20412
  }
20386
20413
  }
@@ -20725,14 +20752,14 @@ var _TinyCloudNode = class _TinyCloudNode {
20725
20752
  }
20726
20753
  if (!activation.success && activation.status !== 404) {
20727
20754
  throw Object.assign(
20728
- new Error(`Failed to check owned space ${spaceId}: ${describeHostFailure(activation)}`),
20755
+ new Error(`Failed to check owned space ${spaceId}: ${describeHostFailure2(activation)}`),
20729
20756
  { cause: activation }
20730
20757
  );
20731
20758
  }
20732
20759
  const created = await this.auth.hostOwnedSpaceResult(spaceId);
20733
20760
  if (!created.success) {
20734
20761
  throw Object.assign(
20735
- new Error(`Failed to create owned space ${spaceId}: ${describeHostFailure(created)}`),
20762
+ new Error(`Failed to create owned space ${spaceId}: ${describeHostFailure2(created)}`),
20736
20763
  { cause: created }
20737
20764
  );
20738
20765
  }
@@ -20741,7 +20768,7 @@ var _TinyCloudNode = class _TinyCloudNode {
20741
20768
  if (!retry.success || retry.skipped?.includes(spaceId)) {
20742
20769
  throw Object.assign(
20743
20770
  new Error(
20744
- `Failed to activate session after creating owned space ${spaceId}: ${retry.success ? "space was skipped" : describeHostFailure(retry)}`
20771
+ `Failed to activate session after creating owned space ${spaceId}: ${retry.success ? "space was skipped" : describeHostFailure2(retry)}`
20745
20772
  ),
20746
20773
  { cause: retry }
20747
20774
  );
@@ -20778,19 +20805,23 @@ var _TinyCloudNode = class _TinyCloudNode {
20778
20805
  if (!host) {
20779
20806
  throw new Error("Owned space hosting requires a TinyCloud host");
20780
20807
  }
20781
- const hosted = await this.auth.hostOwnedSpace(
20782
- spaceId
20783
- );
20784
- if (!hosted) {
20785
- throw new Error(`Failed to host owned space: ${spaceId}`);
20808
+ const hosted = await this.auth.hostOwnedSpaceResult(spaceId);
20809
+ if (!hosted.success) {
20810
+ throw Object.assign(
20811
+ new Error(`Failed to host owned space ${spaceId}: ${describeHostFailure2(hosted)}`),
20812
+ { cause: hosted }
20813
+ );
20786
20814
  }
20787
20815
  const activation = await (0, import_sdk_core7.activateSessionWithHost)(
20788
20816
  host,
20789
20817
  session.delegationHeader
20790
20818
  );
20791
20819
  if (!activation.success || activation.skipped?.includes(spaceId)) {
20792
- throw new Error(
20793
- `Failed to activate session for owned space ${spaceId}: ${activation.error ?? "space was skipped"}`
20820
+ throw Object.assign(
20821
+ new Error(
20822
+ `Failed to activate session for owned space ${spaceId}: ${activation.success ? "space was skipped" : describeHostFailure2(activation)}`
20823
+ ),
20824
+ { cause: activation }
20794
20825
  );
20795
20826
  }
20796
20827
  try {
@@ -21806,7 +21837,7 @@ var _TinyCloudNode = class _TinyCloudNode {
21806
21837
  if (cached) return cached;
21807
21838
  const response = await fetch(`${this.config.host}/info`);
21808
21839
  if (!response.ok) {
21809
- throw new Error(`Failed to fetch node info: HTTP ${response.status}`);
21840
+ throw await (0, import_sdk_core7.httpResponseError)(response, "Failed to fetch node info");
21810
21841
  }
21811
21842
  const info = await response.json();
21812
21843
  if (typeof info.nodeId !== "string" || info.nodeId.length === 0) {
@@ -21856,9 +21887,7 @@ var _TinyCloudNode = class _TinyCloudNode {
21856
21887
  );
21857
21888
  if (response.status === 404) return null;
21858
21889
  if (!response.ok) {
21859
- throw new Error(
21860
- `Failed to fetch encryption network ${networkId}: HTTP ${response.status} ${await response.text()}`
21861
- );
21890
+ throw await (0, import_sdk_core7.httpResponseError)(response, `Failed to fetch encryption network ${networkId}`);
21862
21891
  }
21863
21892
  const body = await response.json();
21864
21893
  return "descriptor" in body && body.descriptor ? body.descriptor : body;
@@ -22359,7 +22388,7 @@ var _TinyCloudNode = class _TinyCloudNode {
22359
22388
  headers: { accept: "application/json", "content-type": "application/json", authorization },
22360
22389
  body: (0, import_sdk_core7.canonicalizeEncryptionJson)(request)
22361
22390
  });
22362
- if (!response.ok) throw new Error(`V3 share delivery authorization failed: ${response.status}`);
22391
+ if (!response.ok) throw await (0, import_sdk_core7.httpResponseError)(response, "V3 share delivery authorization failed");
22363
22392
  const verified = (0, import_sdk_core7.validateShareDeliveryAuthorizationV3Bytes)(new Uint8Array(await response.arrayBuffer()), {
22364
22393
  request,
22365
22394
  senderKeyDid: this.credentialHolderDid,
@@ -22581,9 +22610,7 @@ var _TinyCloudNode = class _TinyCloudNode {
22581
22610
  );
22582
22611
  }
22583
22612
  if (!response.ok) {
22584
- throw new Error(
22585
- `Failed to create encryption network ${networkId}: HTTP ${response.status} ${await response.text()}`
22586
- );
22613
+ throw await (0, import_sdk_core7.httpResponseError)(response, `Failed to create encryption network ${networkId}`);
22587
22614
  }
22588
22615
  const created = await response.json();
22589
22616
  return created.descriptor;
@@ -22891,8 +22918,9 @@ var _TinyCloudNode = class _TinyCloudNode {
22891
22918
  delegation.delegationHeader
22892
22919
  );
22893
22920
  if (!activateResult.success) {
22894
- throw new Error(
22895
- `Failed to activate runtime permission delegation: ${activateResult.error}`
22921
+ throw Object.assign(
22922
+ new Error(`Failed to activate runtime permission delegation: ${describeHostFailure2(activateResult)}`),
22923
+ { cause: activateResult }
22896
22924
  );
22897
22925
  }
22898
22926
  this.runtimePermissionGrants = this.runtimePermissionGrants.filter(
@@ -22988,8 +23016,9 @@ var _TinyCloudNode = class _TinyCloudNode {
22988
23016
  delegatedSession.delegationHeader
22989
23017
  );
22990
23018
  if (!activateResult.success) {
22991
- throw new Error(
22992
- `Failed to activate runtime permission delegation: ${activateResult.error}`
23019
+ throw Object.assign(
23020
+ new Error(`Failed to activate runtime permission delegation: ${describeHostFailure2(activateResult)}`),
23021
+ { cause: activateResult }
22993
23022
  );
22994
23023
  }
22995
23024
  const delegation = this.runtimeDelegationFromSession(
@@ -23174,7 +23203,10 @@ var _TinyCloudNode = class _TinyCloudNode {
23174
23203
  delegationSession.delegationHeader
23175
23204
  );
23176
23205
  if (!activateResult.success) {
23177
- throw new Error(`Failed to activate public space delegation: ${activateResult.error}`);
23206
+ throw Object.assign(
23207
+ new Error(`Failed to activate public space delegation: ${describeHostFailure2(activateResult)}`),
23208
+ { cause: activateResult }
23209
+ );
23178
23210
  }
23179
23211
  if (this._capabilityRegistry && this.session) {
23180
23212
  const sessionKey = {
@@ -23527,8 +23559,9 @@ var _TinyCloudNode = class _TinyCloudNode {
23527
23559
  delegationHeader
23528
23560
  );
23529
23561
  if (!activateResult.success) {
23530
- throw new Error(
23531
- `Failed to activate delegation with host: ${activateResult.error}`
23562
+ throw Object.assign(
23563
+ new Error(`Failed to activate delegation with host: ${describeHostFailure2(activateResult)}`),
23564
+ { cause: activateResult }
23532
23565
  );
23533
23566
  }
23534
23567
  return {
@@ -23564,8 +23597,9 @@ var _TinyCloudNode = class _TinyCloudNode {
23564
23597
  delegationHeader
23565
23598
  );
23566
23599
  if (!activateResult.success) {
23567
- throw new Error(
23568
- `Failed to activate delegation with host: ${activateResult.error}`
23600
+ throw Object.assign(
23601
+ new Error(`Failed to activate delegation with host: ${describeHostFailure2(activateResult)}`),
23602
+ { cause: activateResult }
23569
23603
  );
23570
23604
  }
23571
23605
  return {
@@ -24172,7 +24206,10 @@ var _TinyCloudNode = class _TinyCloudNode {
24172
24206
  delegationSession.delegationHeader
24173
24207
  );
24174
24208
  if (!activateResult.success) {
24175
- throw new Error(`Failed to activate delegation: ${activateResult.error}`);
24209
+ throw Object.assign(
24210
+ new Error(`Failed to activate delegation: ${describeHostFailure2(activateResult)}`),
24211
+ { cause: activateResult }
24212
+ );
24176
24213
  }
24177
24214
  const result = {
24178
24215
  cid: delegationSession.delegationCid,
@@ -24322,7 +24359,10 @@ var _TinyCloudNode = class _TinyCloudNode {
24322
24359
  invokerSession.delegationHeader
24323
24360
  );
24324
24361
  if (!activateResult.success) {
24325
- throw new Error(`Failed to activate delegated session: ${activateResult.error}`);
24362
+ throw Object.assign(
24363
+ new Error(`Failed to activate delegated session: ${describeHostFailure2(activateResult)}`),
24364
+ { cause: activateResult }
24365
+ );
24326
24366
  }
24327
24367
  const session = {
24328
24368
  address: mySession.address,
@@ -24439,7 +24479,10 @@ var _TinyCloudNode = class _TinyCloudNode {
24439
24479
  subDelegationSession.delegationHeader
24440
24480
  );
24441
24481
  if (!activateResult.success) {
24442
- throw new Error(`Failed to activate sub-delegation: ${activateResult.error}`);
24482
+ throw Object.assign(
24483
+ new Error(`Failed to activate sub-delegation: ${describeHostFailure2(activateResult)}`),
24484
+ { cause: activateResult }
24485
+ );
24443
24486
  }
24444
24487
  return {
24445
24488
  cid: subDelegationSession.delegationCid,