@tinycloud/cli 1.1.0-beta.6 → 1.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.js CHANGED
@@ -5659,20 +5659,55 @@ import {
5659
5659
  profileStoreMetadataPath,
5660
5660
  readAdditionalDelegations,
5661
5661
  readAuthRequests,
5662
- upsertProfileRecord,
5662
+ updateProfileStore,
5663
5663
  withProfileLock as withProfileLock2,
5664
5664
  writeJsonAtomic
5665
5665
  } from "@tinycloud/operations/state";
5666
5666
  async function loadAdditionalDelegations(profile) {
5667
5667
  return readAdditionalDelegations(profile);
5668
5668
  }
5669
- async function replayAdditionalDelegations(node, profile) {
5669
+ async function replayAdditionalDelegations(node, profile, options) {
5670
+ const {
5671
+ operationSpaceResolver,
5672
+ prepareStoredDelegationReplay,
5673
+ replayStoredDelegation,
5674
+ storedDelegationKind
5675
+ } = await import("@tinycloud/operations/delegation-binding");
5676
+ const activator = node;
5677
+ const migrated = await prepareStoredDelegationReplay(profile, activator, {
5678
+ host: options.host,
5679
+ migrate: options.migrate
5680
+ });
5681
+ const resolveSpace = operationSpaceResolver(node, options.ownerSpace);
5670
5682
  const entries = await loadAdditionalDelegations(profile);
5671
- for (const entry of entries) {
5683
+ for (const stored of entries) {
5684
+ const kind = storedDelegationKind(stored);
5685
+ if (kind === "compact" || kind === "signed-login") {
5686
+ const installed = await replayStoredDelegation(activator, stored, {
5687
+ host: options.host,
5688
+ migrated,
5689
+ resolveSpace
5690
+ });
5691
+ if (installed === void 0 && process.env.TC_DEBUG_REPLAY === "1") {
5692
+ process.stderr.write("[replay] skipping a stored delegation refused by validation or its request binding\n");
5693
+ }
5694
+ continue;
5695
+ }
5696
+ if (kind === "refused") {
5697
+ if (process.env.TC_DEBUG_REPLAY === "1") {
5698
+ process.stderr.write("[replay] skipping a malformed stored record or one bound to a request it cannot be held to\n");
5699
+ }
5700
+ continue;
5701
+ }
5702
+ const entry = stored;
5672
5703
  const expiry = entry.delegation.expiry instanceof Date ? entry.delegation.expiry : new Date(entry.delegation.expiry);
5673
5704
  if (expiry.getTime() <= Date.now()) continue;
5674
5705
  try {
5675
- await node.useRuntimeDelegation({ ...entry.delegation, expiry });
5706
+ await node.useRuntimeDelegation({
5707
+ ...entry.delegation,
5708
+ delegationHeader: { Authorization: entry.delegation.delegationHeader.Authorization },
5709
+ expiry
5710
+ });
5676
5711
  } catch (err) {
5677
5712
  if (process.env.TC_DEBUG_REPLAY === "1") {
5678
5713
  process.stderr.write(`[replay] skipping ${entry.delegation.cid}: ${err.message}
@@ -5749,6 +5784,7 @@ async function createSDKInstance(ctx, options) {
5749
5784
  host: ctx.host,
5750
5785
  privateKey: effectivePrivateKey
5751
5786
  });
5787
+ let restoredOwnSession2 = false;
5752
5788
  if (session && session.delegationHeader && session.delegationCid && session.spaceId) {
5753
5789
  await node2.restoreSession({
5754
5790
  delegationHeader: session.delegationHeader,
@@ -5761,16 +5797,22 @@ async function createSDKInstance(ctx, options) {
5761
5797
  siwe: session.siwe,
5762
5798
  signature: session.signature
5763
5799
  });
5800
+ restoredOwnSession2 = true;
5764
5801
  } else {
5765
5802
  await node2.signIn();
5766
5803
  }
5767
- await replayAdditionalDelegations(node2, ctx.profile);
5804
+ await replayAdditionalDelegations(node2, ctx.profile, {
5805
+ host: ctx.host,
5806
+ ownerSpace: profile.spaceId,
5807
+ migrate: restoredOwnSession2 && options?.privateKey === void 0
5808
+ });
5768
5809
  return node2;
5769
5810
  }
5770
5811
  const node = new TinyCloudNode({
5771
5812
  host: ctx.host,
5772
5813
  privateKey: options?.privateKey
5773
5814
  });
5815
+ let restoredOwnSession = false;
5774
5816
  if (options?.privateKey) {
5775
5817
  await node.signIn();
5776
5818
  } else if (session && session.delegationHeader && session.delegationCid && session.spaceId) {
@@ -5785,8 +5827,13 @@ async function createSDKInstance(ctx, options) {
5785
5827
  siwe: session.siwe,
5786
5828
  signature: session.signature
5787
5829
  });
5830
+ restoredOwnSession = true;
5788
5831
  }
5789
- await replayAdditionalDelegations(node, ctx.profile);
5832
+ await replayAdditionalDelegations(node, ctx.profile, {
5833
+ host: ctx.host,
5834
+ ownerSpace: profile?.spaceId,
5835
+ migrate: restoredOwnSession
5836
+ });
5790
5837
  return node;
5791
5838
  }
5792
5839
  async function bootstrapDelegatedSession(ctx, delegation) {
@@ -5880,7 +5927,7 @@ async function ensureAuthenticated(ctx, options) {
5880
5927
  }
5881
5928
  const profile = await ProfileManager.getProfile(ctx.profile).catch(() => null);
5882
5929
  if (profile?.authMethod === "local" && profile.privateKey) {
5883
- return createSDKInstance(ctx, { privateKey: profile.privateKey });
5930
+ return createSDKInstance(ctx);
5884
5931
  }
5885
5932
  const session = await ProfileManager.getSession(ctx.profile);
5886
5933
  if (!session) {