@tinycloud/operations 0.5.0-beta.0 → 0.5.0-beta.1

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
@@ -5516,7 +5516,10 @@ async function importRequestBoundDelegation(context, input) {
5516
5516
  delegation,
5517
5517
  { host }
5518
5518
  );
5519
- } catch {
5519
+ } catch (error) {
5520
+ if (typeof error === "object" && error !== null && "reason" in error && error.reason === "expired") {
5521
+ return expiredDelegation();
5522
+ }
5520
5523
  return operationFailure(
5521
5524
  "DELEGATION_REJECTED",
5522
5525
  "The delegation was rejected by the TinyCloud runtime."
@@ -5534,55 +5537,67 @@ async function importRequestBoundDelegation(context, input) {
5534
5537
  );
5535
5538
  }
5536
5539
  try {
5537
- return await updateProfileStore(
5538
- context.summary.profile,
5539
- "additional-delegations",
5540
- async (records) => {
5541
- const state = await importState(context, input.requestId);
5542
- if (!state.ok) return { records, result: state.result };
5543
- if (state.sessionDid !== sessionDid || normalizeHost(state.host) !== normalizeHost(host)) {
5544
- return {
5545
- records,
5546
- result: audienceMismatch(state.sessionDid, sessionDid)
5540
+ return await withProfileLock(context.summary.profile, async () => {
5541
+ const previousRecords = await readAdditionalDelegations(context.summary.profile);
5542
+ const result = await updateProfileStore(
5543
+ context.summary.profile,
5544
+ "additional-delegations",
5545
+ async (records) => {
5546
+ const state = await importState(context, input.requestId);
5547
+ if (!state.ok) return { records, result: state.result };
5548
+ if (state.sessionDid !== sessionDid || normalizeHost(state.host) !== normalizeHost(host)) {
5549
+ return {
5550
+ records,
5551
+ result: audienceMismatch(state.sessionDid, sessionDid)
5552
+ };
5553
+ }
5554
+ if (state.request.profile !== context.summary.profile || !samePrincipal(state.request.sessionDid, sessionDid) || normalizeHost(state.request.host) !== normalizeHost(host) || JSON.stringify(canonicalizeOperationCapabilities(state.request.requested, resolveSpace2)) !== JSON.stringify(canonicalizeOperationCapabilities(request.requested, resolveSpace2))) {
5555
+ return {
5556
+ records,
5557
+ result: operationFailure(
5558
+ "DELEGATION_ARTIFACT_INVALID",
5559
+ "The delegation does not reference a current stored request."
5560
+ )
5561
+ };
5562
+ }
5563
+ const record = {
5564
+ delegation: activated.delegation,
5565
+ permissions: activated.effectivePermissions,
5566
+ authorityRequest: {
5567
+ requestId: request.requestId,
5568
+ requested: request.requested
5569
+ }
5547
5570
  };
5548
- }
5549
- if (state.request.profile !== context.summary.profile || !samePrincipal(state.request.sessionDid, sessionDid) || normalizeHost(state.request.host) !== normalizeHost(host) || JSON.stringify(canonicalizeOperationCapabilities(state.request.requested, resolveSpace2)) !== JSON.stringify(canonicalizeOperationCapabilities(request.requested, resolveSpace2))) {
5571
+ const position = records.findIndex((entry) => delegationCid(entry) === activated.cid);
5572
+ const alreadyPresent = position !== -1;
5573
+ const others = records.filter((entry) => delegationCid(entry) !== activated.cid);
5550
5574
  return {
5551
- records,
5552
- result: operationFailure(
5553
- "DELEGATION_ARTIFACT_INVALID",
5554
- "The delegation does not reference a current stored request."
5555
- )
5575
+ records: alreadyPresent ? [...others.slice(0, position), record, ...others.slice(position)] : [...records, record],
5576
+ result: {
5577
+ status: "ok",
5578
+ output: {
5579
+ cid: activated.cid,
5580
+ effectivePermissions,
5581
+ expiry: activated.expiry.toISOString(),
5582
+ audience: activated.audience,
5583
+ host: activated.host,
5584
+ activated: true,
5585
+ alreadyPresent
5586
+ }
5587
+ }
5556
5588
  };
5557
5589
  }
5558
- const record = {
5559
- delegation: activated.delegation,
5560
- permissions: activated.effectivePermissions,
5561
- authorityRequest: {
5562
- requestId: request.requestId,
5563
- requested: request.requested
5564
- }
5565
- };
5566
- const position = records.findIndex((entry) => delegationCid(entry) === activated.cid);
5567
- const alreadyPresent = position !== -1;
5568
- const others = records.filter((entry) => delegationCid(entry) !== activated.cid);
5569
- return {
5570
- records: alreadyPresent ? [...others.slice(0, position), record, ...others.slice(position)] : [...records, record],
5571
- result: {
5572
- status: "ok",
5573
- output: {
5574
- cid: activated.cid,
5575
- effectivePermissions,
5576
- expiry: activated.expiry.toISOString(),
5577
- audience: activated.audience,
5578
- host: activated.host,
5579
- activated: true,
5580
- alreadyPresent
5581
- }
5582
- }
5583
- };
5590
+ );
5591
+ if (activated.expiry.getTime() <= Date.now()) {
5592
+ await updateProfileStore(
5593
+ context.summary.profile,
5594
+ "additional-delegations",
5595
+ () => ({ records: previousRecords, result: void 0 })
5596
+ );
5597
+ return expiredDelegation();
5584
5598
  }
5585
- );
5599
+ return result;
5600
+ });
5586
5601
  } catch {
5587
5602
  return internalImportFailure();
5588
5603
  }