@tinycloud/node-sdk 3.12.0-beta.1 → 3.12.0-beta.2

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.
@@ -1,10 +1,10 @@
1
1
  import { NamedKeyClient } from '@tinycloud/sdk-services/encryption/named-keys';
2
2
  export { DecryptManyResult, KeyCallOptions, KeyDescriptor, KeyEncryptOptions, NamedKeyClient, NamedKeyEnvelope, NamedKeyError, NamedKeyErrorCode, PublicKeyDescriptor, buildKeyAdminFacts, buildKeyDescribeFacts } from '@tinycloud/sdk-services/encryption/named-keys';
3
- import { T as TinyCloudNode } from './TinyCloudNode-C24728N_.cjs';
3
+ import { T as TinyCloudNode } from './TinyCloudNode-6_IumWu0.cjs';
4
4
  import '@tinycloud/sdk-core';
5
5
  import '@tinycloud/sdk-services/kv/replication';
6
6
  import '@tinycloud/sdk-services';
7
- import './NodeUserAuthorization-BtoDI9in.cjs';
7
+ import './NodeUserAuthorization-B05Wjjpb.cjs';
8
8
  import 'events';
9
9
  import './delegation-BQbAuKad.cjs';
10
10
 
@@ -1,10 +1,10 @@
1
1
  import { NamedKeyClient } from '@tinycloud/sdk-services/encryption/named-keys';
2
2
  export { DecryptManyResult, KeyCallOptions, KeyDescriptor, KeyEncryptOptions, NamedKeyClient, NamedKeyEnvelope, NamedKeyError, NamedKeyErrorCode, PublicKeyDescriptor, buildKeyAdminFacts, buildKeyDescribeFacts } from '@tinycloud/sdk-services/encryption/named-keys';
3
- import { T as TinyCloudNode } from './TinyCloudNode-DLrINr98.js';
3
+ import { T as TinyCloudNode } from './TinyCloudNode-CEFGtSWn.js';
4
4
  import '@tinycloud/sdk-core';
5
5
  import '@tinycloud/sdk-services/kv/replication';
6
6
  import '@tinycloud/sdk-services';
7
- import './NodeUserAuthorization-BtoDI9in.js';
7
+ import './NodeUserAuthorization-B05Wjjpb.js';
8
8
  import 'events';
9
9
  import './delegation-BQbAuKad.js';
10
10
 
package/dist/index.cjs CHANGED
@@ -19766,23 +19766,35 @@ var NodeUserAuthorization = class {
19766
19766
  * Create the space on the TinyCloud server (host delegation).
19767
19767
  * This registers the user as the owner of the space.
19768
19768
  */
19769
- async hostSpace(targetSpaceId, purpose, signal) {
19770
- if (!this._tinyCloudSession || !this._address || !this._chainId) {
19769
+ async hostSpace(targetSpaceId, purpose, signal, assertCurrent, siweParameters) {
19770
+ const context = typeof targetSpaceId === "object" ? targetSpaceId : void 0;
19771
+ const address = context?.address ?? this._address;
19772
+ const chainId = context?.chainId ?? this._chainId;
19773
+ if (!address || chainId === void 0 || !this._tinyCloudSession) {
19771
19774
  throw new Error("Must be signed in to host space");
19772
19775
  }
19776
+ const checkCurrent = () => {
19777
+ signal?.throwIfAborted();
19778
+ assertCurrent?.();
19779
+ };
19780
+ checkCurrent();
19773
19781
  await this.ensureTinyCloudHosts();
19774
- const host = this.primaryTinyCloudHost;
19775
- const spaceId = targetSpaceId ?? this._tinyCloudSession.spaceId;
19782
+ checkCurrent();
19783
+ const host = context?.host ?? this.primaryTinyCloudHost;
19784
+ const spaceId = context?.spaceId ?? (typeof targetSpaceId === "string" ? targetSpaceId : this._tinyCloudSession.spaceId);
19776
19785
  const peerId = await (0, import_sdk_core5.fetchPeerId)(host, spaceId, signal);
19777
- const siwe = this.wasm.generateHostSIWEMessage({
19778
- address: this._address,
19779
- chainId: this._chainId,
19780
- domain: this.domain,
19781
- issuedAt: (/* @__PURE__ */ new Date()).toISOString(),
19782
- spaceId,
19783
- peerId
19784
- });
19785
- if (signal?.aborted) throw signal.reason;
19786
+ checkCurrent();
19787
+ const siwe = this.wasm.generateHostSIWEMessage(
19788
+ context && siweParameters ? siweParameters(peerId) : {
19789
+ address,
19790
+ chainId,
19791
+ domain: this.domain,
19792
+ issuedAt: (/* @__PURE__ */ new Date()).toISOString(),
19793
+ spaceId,
19794
+ peerId
19795
+ }
19796
+ );
19797
+ checkCurrent();
19786
19798
  const signing = this.signMessage(siwe, purpose);
19787
19799
  let signature2;
19788
19800
  if (!signal) {
@@ -19801,8 +19813,9 @@ var NodeUserAuthorization = class {
19801
19813
  signal.removeEventListener("abort", onAbort);
19802
19814
  }
19803
19815
  }
19804
- if (signal?.aborted) throw signal.reason;
19816
+ checkCurrent();
19805
19817
  const headers = this.wasm.siweToDelegationHeaders({ siwe, signature: signature2 });
19818
+ checkCurrent();
19806
19819
  return (0, import_sdk_core5.submitHostDelegation)(host, headers, signal);
19807
19820
  }
19808
19821
  /**
@@ -19864,37 +19877,30 @@ var NodeUserAuthorization = class {
19864
19877
  *
19865
19878
  * @throws Error if the creation handler fails or times out, or hosting fails
19866
19879
  */
19867
- async ensurePrimarySpaceHosted(hosting) {
19868
- if (hosting !== "not-hosted") {
19869
- return;
19870
- }
19880
+ async ensurePrimarySpaceHosted(hosting, signal) {
19871
19881
  const session = this._tinyCloudSession;
19872
- if (!session) {
19873
- throw new Error("Must be signed in to host the primary space");
19874
- }
19882
+ if (hosting !== "not-hosted") return;
19883
+ if (!session) throw new Error("Must be signed in to host the primary space");
19875
19884
  const handler = this.spaceCreationHandler ?? (this.autoCreateSpace ? new import_sdk_core5.AutoApproveSpaceCreationHandler() : void 0);
19876
- if (!handler) {
19877
- return;
19878
- }
19879
- await this.ensureTinyCloudHosts();
19880
- const host = this.primaryTinyCloudHost;
19881
- const primarySpaceId = session.spaceId;
19882
- const creationContext = {
19883
- spaceId: primarySpaceId,
19884
- address: this._address,
19885
- chainId: this._chainId,
19886
- host
19887
- };
19885
+ if (!handler) return;
19888
19886
  const { createPrimarySpace } = await import("./space-hosting.cjs");
19889
19887
  await createPrimarySpace({
19890
19888
  handler,
19891
- context: creationContext,
19892
- guard: () => this.guardSpaceCreation(handler, creationContext),
19893
- host: () => this.hostSpace(primarySpaceId),
19889
+ session,
19890
+ currentSession: () => this._tinyCloudSession,
19891
+ signerAddress: () => this.signer.getAddress(),
19892
+ resolveHost: async () => {
19893
+ await this.ensureTinyCloudHosts();
19894
+ return this.primaryTinyCloudHost;
19895
+ },
19896
+ domain: this.domain,
19897
+ guard: (context) => this.guardSpaceCreation(handler, context),
19898
+ host: (context, assertCurrent, siweParameters) => this.hostSpace(context, void 0, signal, assertCurrent, siweParameters),
19894
19899
  onHosted: () => {
19895
19900
  this._spaceHosting = "hosted";
19896
19901
  },
19897
- activate: () => (0, import_sdk_core5.activateSessionWithHost)(host, session.delegationHeader)
19902
+ activate: (context) => (0, import_sdk_core5.activateSessionWithHost)(context.host, session.delegationHeader),
19903
+ signal
19898
19904
  });
19899
19905
  }
19900
19906
  /** {@link assertMayCreateSpace}, reporting a refusal to the creation handler before rethrowing it. */
@@ -22218,6 +22224,18 @@ var _TinyCloudNode = class _TinyCloudNode {
22218
22224
  return "unknown";
22219
22225
  }
22220
22226
  }
22227
+ /** Run the existing primary-space creation flow for the restored session. */
22228
+ async ensurePrimarySpaceHosted(hosting) {
22229
+ const graph = this._serviceGraph;
22230
+ const epoch = this.sessionEpoch;
22231
+ graph.assertActive();
22232
+ this.assertSessionEpoch(epoch, "Space hosting");
22233
+ const auth = this.auth;
22234
+ if (!auth || !this.session) return;
22235
+ await auth.ensurePrimarySpaceHosted(hosting, graph.signal);
22236
+ graph.assertActive();
22237
+ this.assertSessionEpoch(epoch, "Space hosting");
22238
+ }
22221
22239
  /**
22222
22240
  * The node's last answer for the primary space: recorded at sign-in from the
22223
22241
  * activation, or by {@link checkSpaceHosting}. `unknown` after a restore until