@tinycloud/sdk-core 3.1.0-beta.1 → 3.1.0-beta.11

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.
@@ -3324,6 +3324,23 @@ var import_sha256 = require("@noble/hashes/sha256");
3324
3324
  var import_blake3 = require("@noble/hashes/blake3");
3325
3325
  var import_ed255192 = require("@noble/curves/ed25519");
3326
3326
  var import_zod = require("zod");
3327
+
3328
+ // src/http-error.ts
3329
+ var import_sdk_services = require("@tinycloud/sdk-services");
3330
+ async function httpResponseError(response, context) {
3331
+ let body;
3332
+ try {
3333
+ body = await response.text();
3334
+ } catch {
3335
+ body = response.statusText;
3336
+ }
3337
+ body = body.trim().slice(0, 512);
3338
+ return Object.assign(new Error(`${context}: HTTP ${response.status}${body ? ` - ${body}` : ""}`), {
3339
+ status: response.status
3340
+ });
3341
+ }
3342
+
3343
+ // src/policy/unified.ts
3327
3344
  var POLICY_V1_SCHEMA = "xyz.tinycloud.policy/policy/v1";
3328
3345
  var POLICY_CAPABILITY_V1_SCHEMA = "xyz.tinycloud.policy/capability/v1";
3329
3346
  var POLICY_ENFORCEMENT_DELEGATION_V2_SCHEMA = "xyz.tinycloud.policy/enforcement-delegation/v2";
@@ -3351,7 +3368,7 @@ async function requestPolicyChallengeV3(input) {
3351
3368
  headers: { accept: "application/json", "content-type": "application/json" },
3352
3369
  body: JSON.stringify({ policyCid: input.policyCid, recipientDid: input.recipientDid, requestedCapabilities: input.requestedCapabilities.map(normalizeUnifiedPolicyCapability) })
3353
3370
  });
3354
- if (!response.ok) throw new Error(`policy challenge rejected (${response.status})`);
3371
+ if (!response.ok) throw await httpResponseError(response, "policy challenge rejected");
3355
3372
  const value = object(await response.json(), "policy challenge");
3356
3373
  if (typeof value.challengeId !== "string" || typeof value.nonce !== "string" || value.policyCid !== input.policyCid || value.recipientDid !== input.recipientDid)
3357
3374
  throw new Error("policy challenge binding is invalid");
@@ -3376,7 +3393,7 @@ async function mintPolicySessionV3(input) {
3376
3393
  headers: { accept: "application/json", "content-type": "application/json" },
3377
3394
  body: JSON.stringify({ policyCid: input.policyCid, challengeId: challenge.challengeId, nonce: challenge.nonce, claim: input.claim, presentation: input.presentation })
3378
3395
  });
3379
- if (!response.ok) throw new Error(`policy delegation rejected (${response.status})`);
3396
+ if (!response.ok) throw await httpResponseError(response, "policy delegation rejected");
3380
3397
  const value = object(await response.json(), "policy delegation");
3381
3398
  if (value.admitted !== true || typeof value.sessionCid !== "string" || typeof value.authorization !== "string")
3382
3399
  throw new Error("policy delegation response is invalid");
@@ -3436,7 +3453,7 @@ async function getPolicyRootStatusV3(input) {
3436
3453
  redirect: "error",
3437
3454
  headers: { accept: "application/json" }
3438
3455
  });
3439
- if (!response.ok) throw new Error(`policy root status rejected (${response.status})`);
3456
+ if (!response.ok) throw await httpResponseError(response, "policy root status rejected");
3440
3457
  const value = object(await response.json(), "policy root status");
3441
3458
  const checkpoint = object(value.checkpoint, "policy root checkpoint");
3442
3459
  if (value.rootCid !== input.rootCid || value.state !== "active" && value.state !== "revoked" || value.state !== checkpoint.state || !verifyPolicyRootStatusCheckpointV3({ rootCid: input.rootCid, checkpoint, expectedNodeAudience: input.expectedNodeAudience, now: input.now }))
@@ -3496,9 +3513,12 @@ async function renewPolicyRootStatusV3(input) {
3496
3513
  headers: { accept: "application/json", "content-type": "application/json" },
3497
3514
  body: JSON.stringify({ rootCid: input.rootCid, renewal: { ...unsigned, signature: { suite: "Ed25519", signerDid: input.ownerDid, value: encodeBase64Url(signature) } } })
3498
3515
  });
3499
- if (!response.ok) throw new Error(`policy root renewal rejected (${response.status})`);
3516
+ if (!response.ok) throw await httpResponseError(response, "policy root renewal rejected");
3500
3517
  return object(await response.json(), "policy root renewal");
3501
3518
  }
3519
+ function rfc3339Seconds(value) {
3520
+ return new Date(Math.floor(value.getTime() / 1e3) * 1e3).toISOString().replace(/\.000Z$/, "Z");
3521
+ }
3502
3522
  async function revokePolicyRootV3(input) {
3503
3523
  if (input.reason.length === 0) throw new Error("policy root revocation reason is required");
3504
3524
  const unsigned = {
@@ -3507,7 +3527,7 @@ async function revokePolicyRootV3(input) {
3507
3527
  targetRole: input.targetRole,
3508
3528
  ownerDid: input.ownerDid,
3509
3529
  nodeAudience: input.nodeAudience,
3510
- revokedAt: (input.now ?? /* @__PURE__ */ new Date()).toISOString(),
3530
+ revokedAt: rfc3339Seconds(input.now ?? /* @__PURE__ */ new Date()),
3511
3531
  reason: input.reason,
3512
3532
  issuerDid: input.issuerDid
3513
3533
  };
@@ -3520,7 +3540,7 @@ async function revokePolicyRootV3(input) {
3520
3540
  headers: { accept: "application/json", "content-type": "application/json" },
3521
3541
  body: JSON.stringify({ revocation: { ...unsigned, signature: { suite: "Ed25519", signerDid: input.issuerDid, value: encodeBase64Url(signature) } } })
3522
3542
  });
3523
- if (!response.ok) throw new Error(`policy root revocation rejected (${response.status})`);
3543
+ if (!response.ok) throw await httpResponseError(response, "policy root revocation rejected");
3524
3544
  return object(await response.json(), "policy root revocation");
3525
3545
  }
3526
3546
  var POLICY_SESSION_FACT_KEYS = [
@@ -3789,7 +3809,7 @@ async function registerPolicyV3(input) {
3789
3809
  }
3790
3810
  );
3791
3811
  if (!bindingResponse.ok) {
3792
- throw new Error(`policy enforcer binding rejected (${bindingResponse.status})`);
3812
+ throw await httpResponseError(bindingResponse, "policy enforcer binding rejected");
3793
3813
  }
3794
3814
  const binding = exactObject(
3795
3815
  await bindingResponse.json(),
@@ -3855,7 +3875,7 @@ async function registerPolicyV3(input) {
3855
3875
  }
3856
3876
  );
3857
3877
  if (!response.ok) {
3858
- throw new Error(`policy registration rejected (${response.status})`);
3878
+ throw await httpResponseError(response, "policy registration rejected");
3859
3879
  }
3860
3880
  const receipt = exactObject(
3861
3881
  await response.json(),
@@ -4329,7 +4349,7 @@ function date(value, label) {
4329
4349
  if (!Number.isFinite(Date.parse(text))) throw new Error(`${label} is invalid`);
4330
4350
  return text;
4331
4351
  }
4332
- function rfc3339Seconds(value) {
4352
+ function rfc3339Seconds2(value) {
4333
4353
  return new Date(Math.floor(value.getTime() / 1e3) * 1e3).toISOString().replace(/\.000Z$/, "Z");
4334
4354
  }
4335
4355
  function validateUnifiedPolicyV2(value, expectedPolicyCid) {
@@ -4494,8 +4514,8 @@ async function buildPolicyCredentialPresentationV4(input) {
4494
4514
  requirementDigest,
4495
4515
  descriptorDigest: input.credential.descriptorDigest,
4496
4516
  requestedCapabilities: Object.freeze(requestedCapabilities),
4497
- issuedAt: rfc3339Seconds(now),
4498
- expiresAt: rfc3339Seconds(new Date(expiresAtMs))
4517
+ issuedAt: rfc3339Seconds2(now),
4518
+ expiresAt: rfc3339Seconds2(new Date(expiresAtMs))
4499
4519
  }), input.sign);
4500
4520
  }
4501
4521
  async function buildPolicyCredentialPresentationV3(input) {
@@ -4549,8 +4569,8 @@ async function buildPolicyCredentialPresentationV3(input) {
4549
4569
  requirementDigest,
4550
4570
  descriptorDigest: input.credential.descriptorDigest,
4551
4571
  requestedCapabilities: Object.freeze(requestedCapabilities),
4552
- issuedAt: rfc3339Seconds(now),
4553
- expiresAt: rfc3339Seconds(new Date(expiresAtMs))
4572
+ issuedAt: rfc3339Seconds2(now),
4573
+ expiresAt: rfc3339Seconds2(new Date(expiresAtMs))
4554
4574
  });
4555
4575
  const digest2 = (0, import_sha2562.sha256)(
4556
4576
  new TextEncoder().encode(
@@ -4612,7 +4632,7 @@ async function admitPolicyCredentialV3(input) {
4612
4632
  requestedSessionExpiry(input.requestedExpiresAt, policy),
4613
4633
  input.signal
4614
4634
  );
4615
- if (!response.ok) throw new Error(`policy delegation rejected (${response.status})`);
4635
+ if (!response.ok) throw await httpResponseError(response, "policy delegation rejected");
4616
4636
  const value = record(await response.json(), "policy delegation");
4617
4637
  if (value.admitted !== true || typeof value.sessionCid !== "string" || typeof value.authorization !== "string") {
4618
4638
  throw new Error("policy delegation response is invalid");
@@ -4665,7 +4685,7 @@ async function admitPolicyCredentialV4(input) {
4665
4685
  requestedSessionExpiry(input.requestedExpiresAt, policy),
4666
4686
  input.signal
4667
4687
  );
4668
- if (!response.ok) throw new Error(`policy delegation rejected (${response.status})`);
4688
+ if (!response.ok) throw await httpResponseError(response, "policy delegation rejected");
4669
4689
  const value = record(await response.json(), "policy delegation");
4670
4690
  if (value.admitted !== true || typeof value.sessionCid !== "string" || typeof value.authorization !== "string") throw new Error("policy delegation response is invalid");
4671
4691
  const session = parsePolicySessionUcan(value.authorization, [
@@ -4684,7 +4704,7 @@ async function admitPolicyCredentialV4(input) {
4684
4704
  signal: input.signal,
4685
4705
  headers: { Authorization: session.authorization }
4686
4706
  });
4687
- if (!importResponse.ok) throw new Error(`delegation import rejected (${importResponse.status})`);
4707
+ if (!importResponse.ok) throw await httpResponseError(importResponse, "delegation import rejected");
4688
4708
  input.signal?.throwIfAborted();
4689
4709
  return Object.freeze({ challenge, presentation, session, delegationImported: true });
4690
4710
  }