@kya-os/checkpoint-shared 1.6.0 → 1.8.0

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.
Files changed (222) hide show
  1. package/CHANGELOG.md +122 -9
  2. package/README.md +24 -7
  3. package/dist/beacon-nonce/index.d.mts +1 -0
  4. package/dist/beacon-nonce/index.d.mts.map +1 -1
  5. package/dist/beacon-nonce/index.d.ts +1 -0
  6. package/dist/beacon-nonce/index.d.ts.map +1 -1
  7. package/dist/beacon-nonce/index.js +9 -3
  8. package/dist/beacon-nonce/index.mjs +9 -4
  9. package/dist/beacon-nonce/index.mjs.map +1 -1
  10. package/dist/beacon-types/index.d.mts +23 -1
  11. package/dist/beacon-types/index.d.mts.map +1 -1
  12. package/dist/beacon-types/index.d.ts +23 -1
  13. package/dist/beacon-types/index.d.ts.map +1 -1
  14. package/dist/beacon-types/index.js +49 -4
  15. package/dist/beacon-types/index.mjs +47 -4
  16. package/dist/beacon-types/index.mjs.map +1 -1
  17. package/dist/{browser-posture-BuChqmH5.d.mts → browser-posture-5fPrsxx7.d.mts} +2 -2
  18. package/dist/{browser-posture-BuChqmH5.d.mts.map → browser-posture-5fPrsxx7.d.mts.map} +1 -1
  19. package/dist/{browser-posture-DIaVLuW3.d.ts → browser-posture-CCR1kh5e.d.ts} +2 -2
  20. package/dist/{browser-posture-DIaVLuW3.d.ts.map → browser-posture-CCR1kh5e.d.ts.map} +1 -1
  21. package/dist/browser-posture-cookie.js +1 -1
  22. package/dist/browser-posture-cookie.mjs +1 -1
  23. package/dist/browser-posture-cookie.mjs.map +1 -1
  24. package/dist/browser-posture-proof.d.mts +16 -2
  25. package/dist/browser-posture-proof.d.mts.map +1 -1
  26. package/dist/browser-posture-proof.d.ts +16 -2
  27. package/dist/browser-posture-proof.d.ts.map +1 -1
  28. package/dist/browser-posture-proof.js +114 -17
  29. package/dist/browser-posture-proof.mjs +112 -17
  30. package/dist/browser-posture-proof.mjs.map +1 -1
  31. package/dist/browser-posture.d.mts +1 -1
  32. package/dist/browser-posture.d.ts +1 -1
  33. package/dist/browser-posture.js +6 -5
  34. package/dist/browser-posture.mjs +4 -3
  35. package/dist/browser-posture.mjs.map +1 -1
  36. package/dist/browser.d.mts +25 -4
  37. package/dist/browser.d.mts.map +1 -1
  38. package/dist/browser.d.ts +25 -4
  39. package/dist/browser.d.ts.map +1 -1
  40. package/dist/browser.js +1 -1
  41. package/dist/browser.mjs +1 -1
  42. package/dist/browser.mjs.map +1 -1
  43. package/dist/canonical-signer/index.d.mts.map +1 -1
  44. package/dist/canonical-signer/index.d.ts.map +1 -1
  45. package/dist/canonical-signer/index.js +116 -79
  46. package/dist/canonical-signer/index.mjs +117 -52
  47. package/dist/canonical-signer/index.mjs.map +1 -1
  48. package/dist/clearance.d.mts +1 -1
  49. package/dist/clearance.d.ts +1 -1
  50. package/dist/clearance.js +2 -2
  51. package/dist/clearance.mjs +2 -2
  52. package/dist/client-ip-B7w-uHfS.d.mts +55 -0
  53. package/dist/client-ip-B7w-uHfS.d.mts.map +1 -0
  54. package/dist/client-ip-B7w-uHfS.d.ts +55 -0
  55. package/dist/client-ip-B7w-uHfS.d.ts.map +1 -0
  56. package/dist/{client-ip-CW6pRyLf.js → client-ip-B8wFzJ5f.js} +117 -7
  57. package/dist/{client-ip-B2Xeo-GK.mjs → client-ip-DIJyr6px.mjs} +113 -9
  58. package/dist/client-ip-DIJyr6px.mjs.map +1 -0
  59. package/dist/client-ip.d.mts +2 -0
  60. package/dist/client-ip.d.ts +2 -0
  61. package/dist/client-ip.js +8 -0
  62. package/dist/client-ip.mjs +3 -0
  63. package/dist/detection-reason-mapping-B9RSkOs8.js +277 -0
  64. package/dist/detection-reason-mapping-BGymWIPl.mjs +249 -0
  65. package/dist/detection-reason-mapping-BGymWIPl.mjs.map +1 -0
  66. package/dist/detection-reason-mapping-CQexNvR7.d.mts +52 -0
  67. package/dist/detection-reason-mapping-CQexNvR7.d.mts.map +1 -0
  68. package/dist/detection-reason-mapping-CQexNvR7.d.ts +52 -0
  69. package/dist/detection-reason-mapping-CQexNvR7.d.ts.map +1 -0
  70. package/dist/engine-enforcing-AYEcgNsb.js +39 -0
  71. package/dist/engine-enforcing-B5eNfCpG.mjs +29 -0
  72. package/dist/engine-enforcing-B5eNfCpG.mjs.map +1 -0
  73. package/dist/engine-enforcing-DiOclGqf.d.mts +11 -0
  74. package/dist/engine-enforcing-DiOclGqf.d.mts.map +1 -0
  75. package/dist/engine-enforcing-DiOclGqf.d.ts +11 -0
  76. package/dist/engine-enforcing-DiOclGqf.d.ts.map +1 -0
  77. package/dist/engine-info.d.mts +2 -0
  78. package/dist/engine-info.d.ts +2 -0
  79. package/dist/engine-info.js +4 -0
  80. package/dist/engine-info.mjs +3 -0
  81. package/dist/errors/index.d.mts +5 -0
  82. package/dist/errors/index.d.mts.map +1 -1
  83. package/dist/errors/index.d.ts +5 -0
  84. package/dist/errors/index.d.ts.map +1 -1
  85. package/dist/errors/index.js +23 -4
  86. package/dist/errors/index.mjs +23 -4
  87. package/dist/errors/index.mjs.map +1 -1
  88. package/dist/{external-B7WI9FyQ.d.ts → external-m3Kmafwj.d.mts} +2 -2
  89. package/dist/{external-B7WI9FyQ.d.mts.map → external-m3Kmafwj.d.mts.map} +1 -1
  90. package/dist/{external-B7WI9FyQ.d.mts → external-m3Kmafwj.d.ts} +2 -2
  91. package/dist/{external-B7WI9FyQ.d.ts.map → external-m3Kmafwj.d.ts.map} +1 -1
  92. package/dist/fetcher-CivUCbhL.d.ts +44 -0
  93. package/dist/fetcher-CivUCbhL.d.ts.map +1 -0
  94. package/dist/fetcher-cXQsbm6s.d.mts +44 -0
  95. package/dist/fetcher-cXQsbm6s.d.mts.map +1 -0
  96. package/dist/gateway-client-ip.d.mts +1 -1
  97. package/dist/gateway-client-ip.d.ts +1 -1
  98. package/dist/gateway-client-ip.js +2 -2
  99. package/dist/gateway-client-ip.mjs +2 -2
  100. package/dist/host-adapter/index.d.mts +2 -0
  101. package/dist/host-adapter/index.d.ts +2 -0
  102. package/dist/host-adapter/index.js +24 -0
  103. package/dist/host-adapter/index.mjs +3 -0
  104. package/dist/host-adapter-0F8EpbD6.js +753 -0
  105. package/dist/host-adapter-DHyISsIf.mjs +593 -0
  106. package/dist/host-adapter-DHyISsIf.mjs.map +1 -0
  107. package/dist/index-B3cHxr3O.d.mts +156 -0
  108. package/dist/index-B3cHxr3O.d.mts.map +1 -0
  109. package/dist/index-CXbOY7gB.d.ts +156 -0
  110. package/dist/index-CXbOY7gB.d.ts.map +1 -0
  111. package/dist/index.d.mts +6997 -12170
  112. package/dist/index.d.mts.map +1 -1
  113. package/dist/index.d.ts +6997 -12170
  114. package/dist/index.d.ts.map +1 -1
  115. package/dist/index.js +2510 -8893
  116. package/dist/index.mjs +2366 -8534
  117. package/dist/index.mjs.map +1 -1
  118. package/dist/kya-http-error-codes-7ewV5RuK.d.mts +29 -0
  119. package/dist/kya-http-error-codes-7ewV5RuK.d.mts.map +1 -0
  120. package/dist/kya-http-error-codes-7ewV5RuK.d.ts +29 -0
  121. package/dist/kya-http-error-codes-7ewV5RuK.d.ts.map +1 -0
  122. package/dist/kya-http-error-codes-C2JiWPsi.mjs +73 -0
  123. package/dist/kya-http-error-codes-C2JiWPsi.mjs.map +1 -0
  124. package/dist/kya-http-error-codes-UYb1zpGM.js +83 -0
  125. package/dist/kya-http.d.mts +3 -0
  126. package/dist/kya-http.d.ts +3 -0
  127. package/dist/kya-http.js +19 -0
  128. package/dist/kya-http.mjs +5 -0
  129. package/dist/mcpi/dashboard-config/index.d.mts.map +1 -1
  130. package/dist/mcpi/dashboard-config/index.d.ts.map +1 -1
  131. package/dist/mcpi/status-list/index.d.mts +72 -0
  132. package/dist/mcpi/status-list/index.d.mts.map +1 -0
  133. package/dist/mcpi/status-list/index.d.ts +72 -0
  134. package/dist/mcpi/status-list/index.d.ts.map +1 -0
  135. package/dist/mcpi/status-list/index.js +703 -0
  136. package/dist/mcpi/status-list/index.mjs +684 -0
  137. package/dist/mcpi/status-list/index.mjs.map +1 -0
  138. package/dist/patterns-B2r7M9wR.js +243 -0
  139. package/dist/patterns-B7dQoeE3.mjs +173 -0
  140. package/dist/patterns-B7dQoeE3.mjs.map +1 -0
  141. package/dist/patterns-Bp40cwh2.d.mts +110 -0
  142. package/dist/patterns-Bp40cwh2.d.mts.map +1 -0
  143. package/dist/patterns-Bp40cwh2.d.ts +110 -0
  144. package/dist/patterns-Bp40cwh2.d.ts.map +1 -0
  145. package/dist/patterns.d.mts +2 -0
  146. package/dist/patterns.d.ts +2 -0
  147. package/dist/patterns.js +15 -0
  148. package/dist/patterns.mjs +3 -0
  149. package/dist/policy/enforcement.d.mts +3 -0
  150. package/dist/policy/enforcement.d.ts +3 -0
  151. package/dist/policy/enforcement.js +21 -0
  152. package/dist/policy/enforcement.mjs +4 -0
  153. package/dist/policy/fetcher.d.mts +2 -0
  154. package/dist/policy/fetcher.d.ts +2 -0
  155. package/dist/policy/fetcher.js +240 -0
  156. package/dist/policy/fetcher.mjs +238 -0
  157. package/dist/policy/fetcher.mjs.map +1 -0
  158. package/dist/policy/schemas.d.mts +3 -0
  159. package/dist/policy/schemas.d.ts +3 -0
  160. package/dist/policy/schemas.js +27 -0
  161. package/dist/policy/schemas.mjs +4 -0
  162. package/dist/{posture-origins-Pn-ii-Pp.mjs → posture-origins-Ci6k4Zh1.js} +24 -173
  163. package/dist/{posture-origins-LFkX80oF.js → posture-origins-DGyS4Vuk.mjs} +2 -267
  164. package/dist/posture-origins-DGyS4Vuk.mjs.map +1 -0
  165. package/dist/{project-verification-key-BccT12tQ.js → project-verification-key-B-L3D2nC.js} +1 -1
  166. package/dist/{project-verification-key-66N4edKn.mjs → project-verification-key-CWXdu3-n.mjs} +2 -2
  167. package/dist/{project-verification-key-66N4edKn.mjs.map → project-verification-key-CWXdu3-n.mjs.map} +1 -1
  168. package/dist/provider-registry.d.mts +2 -0
  169. package/dist/provider-registry.d.ts +2 -0
  170. package/dist/provider-registry.js +51 -0
  171. package/dist/provider-registry.mjs +3 -0
  172. package/dist/reason-codes.d.mts +2 -0
  173. package/dist/reason-codes.d.ts +2 -0
  174. package/dist/reason-codes.js +8 -0
  175. package/dist/reason-codes.mjs +3 -0
  176. package/dist/registered-sid-26hNxp6t.mjs +151 -0
  177. package/dist/registered-sid-26hNxp6t.mjs.map +1 -0
  178. package/dist/registered-sid-D6KCaT1E.d.mts +33 -0
  179. package/dist/registered-sid-D6KCaT1E.d.mts.map +1 -0
  180. package/dist/registered-sid-D6KCaT1E.d.ts +33 -0
  181. package/dist/registered-sid-D6KCaT1E.d.ts.map +1 -0
  182. package/dist/registered-sid-DVGTSH0L.js +185 -0
  183. package/dist/ruleset-hash-Cmv8MSO5.js +34 -0
  184. package/dist/ruleset-hash-IRN5eI9H.d.mts +5 -0
  185. package/dist/ruleset-hash-IRN5eI9H.d.mts.map +1 -0
  186. package/dist/ruleset-hash-IRN5eI9H.d.ts +5 -0
  187. package/dist/ruleset-hash-IRN5eI9H.d.ts.map +1 -0
  188. package/dist/ruleset-hash-QQIaLuPi.mjs +30 -0
  189. package/dist/ruleset-hash-QQIaLuPi.mjs.map +1 -0
  190. package/dist/schemas-02CI7jgm.d.mts +535 -0
  191. package/dist/schemas-02CI7jgm.d.mts.map +1 -0
  192. package/dist/{schemas-YhagYDx_.js → schemas-BlScS1it.js} +0 -20
  193. package/dist/schemas-C_3C72SR.d.ts +535 -0
  194. package/dist/schemas-C_3C72SR.d.ts.map +1 -0
  195. package/dist/schemas-Cm4C4Z5G.js +703 -0
  196. package/dist/schemas-DPIT8usF.mjs +525 -0
  197. package/dist/schemas-DPIT8usF.mjs.map +1 -0
  198. package/dist/{schemas-7qodsG-Z.mjs → schemas-IHAhH3gV.mjs} +2 -16
  199. package/dist/schemas-IHAhH3gV.mjs.map +1 -0
  200. package/dist/step-up-BK0oBi11.mjs +1563 -0
  201. package/dist/step-up-BK0oBi11.mjs.map +1 -0
  202. package/dist/step-up-Bp8RAQwv.js +1759 -0
  203. package/dist/step-up-Bz12gYKT.d.mts +130 -0
  204. package/dist/step-up-Bz12gYKT.d.mts.map +1 -0
  205. package/dist/step-up-Bz12gYKT.d.ts +130 -0
  206. package/dist/step-up-Bz12gYKT.d.ts.map +1 -0
  207. package/dist/test-fixtures/index.d.mts +4 -4
  208. package/dist/test-fixtures/index.d.mts.map +1 -1
  209. package/dist/test-fixtures/index.d.ts +4 -4
  210. package/dist/test-fixtures/index.d.ts.map +1 -1
  211. package/dist/test-fixtures/index.js +4 -4
  212. package/dist/test-fixtures/index.mjs +4 -4
  213. package/dist/test-fixtures/index.mjs.map +1 -1
  214. package/dist/verification-BAw9N8Pt.js +181 -0
  215. package/dist/verification-Cvvv86TT.mjs +123 -0
  216. package/dist/verification-Cvvv86TT.mjs.map +1 -0
  217. package/fixtures/client-ip-resolution.n01.json +101 -0
  218. package/fixtures/reviewer-grant-revocation.v1.json +53 -81
  219. package/package.json +59 -2
  220. package/dist/client-ip-B2Xeo-GK.mjs.map +0 -1
  221. package/dist/posture-origins-Pn-ii-Pp.mjs.map +0 -1
  222. package/dist/schemas-7qodsG-Z.mjs.map +0 -1
@@ -1,35 +1,37 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2
- //#region \0rolldown/runtime.js
3
- var __create = Object.create;
4
- var __defProp = Object.defineProperty;
5
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
6
- var __getOwnPropNames = Object.getOwnPropertyNames;
7
- var __getProtoOf = Object.getPrototypeOf;
8
- var __hasOwnProp = Object.prototype.hasOwnProperty;
9
- var __copyProps = (to, from, except, desc) => {
10
- if (from && typeof from === "object" || typeof from === "function") {
11
- for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
12
- key = keys[i];
13
- if (!__hasOwnProp.call(to, key) && key !== except) {
14
- __defProp(to, key, {
15
- get: ((k) => from[k]).bind(null, key),
16
- enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
17
- });
18
- }
2
+ let _noble_ed25519 = require("@noble/ed25519");
3
+
4
+ //#region ../../node_modules/.pnpm/canonicalize@3.0.0/node_modules/canonicalize/lib/canonicalize.js
5
+ function canonicalize(object, seen = /* @__PURE__ */ new Set()) {
6
+ if (typeof object === "number" && isNaN(object)) throw new Error("NaN is not allowed");
7
+ if (typeof object === "number" && !isFinite(object)) throw new Error("Infinity is not allowed");
8
+ if (object === null || typeof object !== "object") return JSON.stringify(object);
9
+ if (typeof object.toJSON === "function") {
10
+ if (seen.has(object)) throw new Error("Circular reference detected");
11
+ seen.add(object);
12
+ const result = canonicalize(object.toJSON(), seen);
13
+ seen.delete(object);
14
+ return result;
15
+ }
16
+ if (seen.has(object)) throw new Error("Circular reference detected");
17
+ seen.add(object);
18
+ let result;
19
+ if (Array.isArray(object)) result = `[${object.map((cv) => {
20
+ return canonicalize(cv === void 0 || typeof cv === "symbol" ? null : cv, seen);
21
+ }).join(",")}]`;
22
+ else {
23
+ const parts = [];
24
+ for (const key of Object.keys(object).sort()) {
25
+ if (object[key] === void 0 || typeof object[key] === "symbol") continue;
26
+ parts.push(`${canonicalize(key)}:${canonicalize(object[key], seen)}`);
19
27
  }
28
+ result = `{${parts.join(",")}}`;
20
29
  }
21
- return to;
22
- };
23
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule || !__hasOwnProp.call(mod, "default") ? __defProp(target, "default", {
24
- value: mod,
25
- enumerable: true
26
- }) : target, mod));
30
+ seen.delete(object);
31
+ return result;
32
+ }
27
33
 
28
34
  //#endregion
29
- let canonicalize = require("canonicalize");
30
- canonicalize = __toESM(canonicalize);
31
- let _noble_ed25519 = require("@noble/ed25519");
32
-
33
35
  //#region src/canonical-signer/payload.ts
34
36
  /**
35
37
  * Canonical KYA-OS signing payload (RFC 8785 / JCS).
@@ -70,7 +72,7 @@ let _noble_ed25519 = require("@noble/ed25519");
70
72
  * will catch the byte drift before it reaches production.
71
73
  */
72
74
  function canonicalSigningPayload(payload) {
73
- const json = (0, canonicalize.default)(payload);
75
+ const json = canonicalize(payload);
74
76
  if (json === void 0) throw new Error("canonicalSigningPayload: payload contains non-serializable values");
75
77
  return new TextEncoder().encode(json);
76
78
  }
@@ -235,6 +237,53 @@ async function parseSigningKey(seed) {
235
237
  };
236
238
  }
237
239
 
240
+ //#endregion
241
+ //#region src/utils/base64url.ts
242
+ /**
243
+ * Base64url (RFC 4648 § 5) built on `atob` and `btoa` rather than Node's
244
+ * buffer API, so it runs on every runtime the SDKs support, Cloudflare Workers
245
+ * without `nodejs_compat` included.
246
+ */
247
+ const BASE64URL_UNPADDED = /^[A-Za-z0-9_-]*$/;
248
+ const PADDING = 61;
249
+ /**
250
+ * `text` without its trailing `=` padding. A backward scan, not
251
+ * `replace(/=+$/, '')`: that regex retries from every `=` in a long run that
252
+ * does not reach the end, which is quadratic on hostile input.
253
+ */
254
+ function withoutPadding(text) {
255
+ let end = text.length;
256
+ while (end > 0 && text.charCodeAt(end - 1) === PADDING) end -= 1;
257
+ return text.slice(0, end);
258
+ }
259
+ /**
260
+ * Decode base64url, with or without `=` padding.
261
+ *
262
+ * Strict where Node's buffer decoder is lenient: a character outside the
263
+ * alphabet, or a length no encoder produces (one character past a multiple of
264
+ * four), throws a `TypeError` instead of being skipped or truncated. This
265
+ * matches the engine's decoder.
266
+ */
267
+ function base64UrlToBytes(text) {
268
+ const unpadded = withoutPadding(text);
269
+ if (!BASE64URL_UNPADDED.test(unpadded) || unpadded.length % 4 === 1) throw new TypeError("not valid base64url (RFC 4648 § 5)");
270
+ const base64 = unpadded.replace(/-/g, "+").replace(/_/g, "/");
271
+ const binary = atob(base64 + "=".repeat((4 - base64.length % 4) % 4));
272
+ const bytes = new Uint8Array(binary.length);
273
+ for (let index = 0; index < binary.length; index += 1) bytes[index] = binary.charCodeAt(index);
274
+ return bytes;
275
+ }
276
+ /** Decode base64url to a string as UTF-8, replacing invalid sequences with U+FFFD. */
277
+ function base64UrlToString(text) {
278
+ return new TextDecoder().decode(base64UrlToBytes(text));
279
+ }
280
+ /** Encode bytes as base64url without padding. */
281
+ function bytesToBase64Url(bytes) {
282
+ let binary = "";
283
+ for (const byte of bytes) binary += String.fromCharCode(byte);
284
+ return withoutPadding(btoa(binary).replace(/\+/g, "-").replace(/\//g, "_"));
285
+ }
286
+
238
287
  //#endregion
239
288
  //#region src/canonical-signer/envelope.ts
240
289
  /**
@@ -254,22 +303,6 @@ async function parseSigningKey(seed) {
254
303
  * Sites-4 parity drivers, the Adobe trial.
255
304
  */
256
305
  /**
257
- * URL-safe base64 encoding without padding (RFC 4648 § 5).
258
- *
259
- * **Node-only.** Uses Node's `Buffer` API because every current consumer
260
- * of this module runs on Node (Vercel functions, Sites-1/2 Playwright
261
- * suites, future SDKs that bundle for Node + bundler-polyfilled runtimes
262
- * like Vercel Edge with `nodejs_compat`). If a future consumer needs a
263
- * pure-Web-API runtime (Cloudflare Workers without `nodejs_compat`,
264
- * Deno's stricter modes), swap to `Uint8Array.toBase64()` (Stage 4 as
265
- * of mid-2026) or a small inline encoder — but verify against the
266
- * envelope.test.ts round-trip first; the `base64url` character
267
- * substitution path is the part that has historically tripped polyfills.
268
- */
269
- function base64UrlEncode(bytes) {
270
- return Buffer.from(bytes).toString("base64").replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
271
- }
272
- /**
273
306
  * Sign an `McpIPayload` and produce a compact-JWS envelope ready to drop
274
307
  * into `_meta.proof.jws` of the request body per KYA-OS spec § 7.4.
275
308
  *
@@ -290,15 +323,40 @@ async function signMcpiEnvelope(payload, key) {
290
323
  kid: key.kid
291
324
  };
292
325
  const headerJson = JSON.stringify(header);
293
- const headerB64 = base64UrlEncode(new TextEncoder().encode(headerJson));
294
- const payloadB64 = base64UrlEncode(canonicalSigningPayload(payload));
326
+ const headerB64 = bytesToBase64Url(new TextEncoder().encode(headerJson));
327
+ const payloadB64 = bytesToBase64Url(canonicalSigningPayload(payload));
295
328
  const signingInput = new TextEncoder().encode(`${headerB64}.${payloadB64}`);
296
- return `${headerB64}.${payloadB64}.${base64UrlEncode(await (0, _noble_ed25519.signAsync)(signingInput, key.signingKey))}`;
329
+ const signature = await (0, _noble_ed25519.signAsync)(signingInput, key.signingKey);
330
+ return `${headerB64}.${payloadB64}.${bytesToBase64Url(signature)}`;
297
331
  }
298
332
 
299
333
  //#endregion
300
334
  //#region src/canonical-signer/parse.ts
301
335
  /**
336
+ * Compact-JWS parser for KYA-OS detached proofs.
337
+ *
338
+ * Producer-side counterpart to Sign-1's `signMcpiEnvelope`: takes the
339
+ * wire string the engine's H-1 parser would consume and decomposes it
340
+ * into the same logical pieces (header, payload, signature, signing
341
+ * input). Lives in `checkpoint-shared/canonical-signer/` so producer
342
+ * AND consumer share one wire-format implementation — the JCS / base64
343
+ * subtleties are handled in one place, not three.
344
+ *
345
+ * Consumed by:
346
+ * - `@kya-os/bouncer-middleware` (Envelope-1 / #2537)
347
+ * - any future TS host that needs to introspect a JWS without going
348
+ * through the WASM bridge
349
+ *
350
+ * The Rust engine's `jws::parse_jws_compact` is the structural mirror
351
+ * (`rust/crates/kya-os-engine/src/jws.rs`); this module produces the
352
+ * same logical decomposition. Cross-runtime parity here is implicit:
353
+ * if both parsers consume the same JWS string and disagree on what's
354
+ * inside, the H-1 fixtures would catch it.
355
+ *
356
+ * Sign-1 (#2500). Anti-pattern: don't write a parallel parser
357
+ * elsewhere in the org — import from here.
358
+ */
359
+ /**
302
360
  * Discriminated error type so consumers can map parse failures to
303
361
  * appropriate response codes (400 for malformed shape, 422 for
304
362
  * unsupported alg) without string-matching error messages.
@@ -364,7 +422,7 @@ function parseJwsCompact(jws) {
364
422
  const [headerB64, payloadB64, signatureB64] = parts;
365
423
  let headerJson;
366
424
  try {
367
- headerJson = JSON.parse(base64UrlDecodeToString(headerB64));
425
+ headerJson = JSON.parse(base64UrlToString(headerB64));
368
426
  } catch (cause) {
369
427
  throw new JwsParseError("InvalidJson", `JWS header is not valid JSON: ${String(cause)}`);
370
428
  }
@@ -382,7 +440,7 @@ function parseJwsCompact(jws) {
382
440
  };
383
441
  let payloadJson;
384
442
  try {
385
- payloadJson = JSON.parse(base64UrlDecodeToString(payloadB64));
443
+ payloadJson = JSON.parse(base64UrlToString(payloadB64));
386
444
  } catch (cause) {
387
445
  throw new JwsParseError("InvalidJson", `JWS payload is not valid JSON: ${String(cause)}`);
388
446
  }
@@ -393,7 +451,12 @@ function parseJwsCompact(jws) {
393
451
  throw new JwsParseError("InvalidPayloadShape", `JWS payload does not match McpIPayload shape: ${cause.message}`);
394
452
  }
395
453
  if (!BASE64URL_ALPHABET.test(signatureB64)) throw new JwsParseError("InvalidBase64", `JWS signature segment contains characters outside the base64url alphabet (RFC 4648 § 5)`);
396
- const signatureBytes = base64UrlDecodeToBytes(signatureB64);
454
+ let signatureBytes;
455
+ try {
456
+ signatureBytes = base64UrlToBytes(signatureB64);
457
+ } catch {
458
+ throw new JwsParseError("InvalidBase64", `JWS signature segment is not a valid base64url length (RFC 4648 § 5)`);
459
+ }
397
460
  if (signatureBytes.length !== 64) throw new JwsParseError("InvalidSignatureLength", `Ed25519 signatures are 64 bytes (RFC 8032 § 5.1.6); got ${signatureBytes.length}`);
398
461
  const signingInput = new TextEncoder().encode(`${headerB64}.${payloadB64}`);
399
462
  return {
@@ -404,37 +467,11 @@ function parseJwsCompact(jws) {
404
467
  };
405
468
  }
406
469
  /**
407
- * Strict base64url alphabet check (RFC 4648 § 5). The set is
408
- * `A-Za-z0-9-_` plus optional `=` padding. We use this to validate
409
- * input BEFORE handing to Node's `Buffer.from(s, 'base64')`, which
410
- * silently drops invalid characters rather than throwing — bypassing
411
- * the discriminated `InvalidBase64` error code consumers rely on for
412
- * HTTP-status mapping.
413
- *
414
- * The empty-string case is rejected by the regex (`+` requires ≥1
415
- * char), which is the desired behavior — an empty segment is a
416
- * different bug class from "non-base64 characters in a non-empty
417
- * segment" but both warrant the same `InvalidBase64` response.
470
+ * Strict base64url alphabet check (RFC 4648 § 5): `A-Za-z0-9-_` plus optional
471
+ * `=` padding. The empty string fails it on purpose: an empty signature
472
+ * segment is reported as `InvalidBase64` too.
418
473
  */
419
474
  const BASE64URL_ALPHABET = /^[A-Za-z0-9_-]+=*$/;
420
- /**
421
- * Decode URL-safe base64 (with optional padding) to bytes. RFC 4648 § 5.
422
- * Tolerates the no-padding form Sign-1 produces AND the padded form
423
- * some external producers emit.
424
- *
425
- * **Caller must validate the input alphabet first** if they want
426
- * `InvalidBase64` reported as a distinct error class — Node's underlying
427
- * `Buffer.from(s, 'base64')` silently truncates on invalid characters
428
- * rather than throwing.
429
- */
430
- function base64UrlDecodeToBytes(s) {
431
- const padded = s.replace(/-/g, "+").replace(/_/g, "/");
432
- const padLen = padded.length % 4 === 0 ? 0 : 4 - padded.length % 4;
433
- return new Uint8Array(Buffer.from(padded + "=".repeat(padLen), "base64"));
434
- }
435
- function base64UrlDecodeToString(s) {
436
- return Buffer.from(base64UrlDecodeToBytes(s)).toString("utf-8");
437
- }
438
475
 
439
476
  //#endregion
440
477
  exports.JwsParseError = JwsParseError;
@@ -1,6 +1,36 @@
1
- import canonicalize from "canonicalize";
2
1
  import { getPublicKeyAsync, signAsync } from "@noble/ed25519";
3
2
 
3
+ //#region ../../node_modules/.pnpm/canonicalize@3.0.0/node_modules/canonicalize/lib/canonicalize.js
4
+ function canonicalize(object, seen = /* @__PURE__ */ new Set()) {
5
+ if (typeof object === "number" && isNaN(object)) throw new Error("NaN is not allowed");
6
+ if (typeof object === "number" && !isFinite(object)) throw new Error("Infinity is not allowed");
7
+ if (object === null || typeof object !== "object") return JSON.stringify(object);
8
+ if (typeof object.toJSON === "function") {
9
+ if (seen.has(object)) throw new Error("Circular reference detected");
10
+ seen.add(object);
11
+ const result = canonicalize(object.toJSON(), seen);
12
+ seen.delete(object);
13
+ return result;
14
+ }
15
+ if (seen.has(object)) throw new Error("Circular reference detected");
16
+ seen.add(object);
17
+ let result;
18
+ if (Array.isArray(object)) result = `[${object.map((cv) => {
19
+ return canonicalize(cv === void 0 || typeof cv === "symbol" ? null : cv, seen);
20
+ }).join(",")}]`;
21
+ else {
22
+ const parts = [];
23
+ for (const key of Object.keys(object).sort()) {
24
+ if (object[key] === void 0 || typeof object[key] === "symbol") continue;
25
+ parts.push(`${canonicalize(key)}:${canonicalize(object[key], seen)}`);
26
+ }
27
+ result = `{${parts.join(",")}}`;
28
+ }
29
+ seen.delete(object);
30
+ return result;
31
+ }
32
+
33
+ //#endregion
4
34
  //#region src/canonical-signer/payload.ts
5
35
  /**
6
36
  * Canonical KYA-OS signing payload (RFC 8785 / JCS).
@@ -206,6 +236,53 @@ async function parseSigningKey(seed) {
206
236
  };
207
237
  }
208
238
 
239
+ //#endregion
240
+ //#region src/utils/base64url.ts
241
+ /**
242
+ * Base64url (RFC 4648 § 5) built on `atob` and `btoa` rather than Node's
243
+ * buffer API, so it runs on every runtime the SDKs support, Cloudflare Workers
244
+ * without `nodejs_compat` included.
245
+ */
246
+ const BASE64URL_UNPADDED = /^[A-Za-z0-9_-]*$/;
247
+ const PADDING = 61;
248
+ /**
249
+ * `text` without its trailing `=` padding. A backward scan, not
250
+ * `replace(/=+$/, '')`: that regex retries from every `=` in a long run that
251
+ * does not reach the end, which is quadratic on hostile input.
252
+ */
253
+ function withoutPadding(text) {
254
+ let end = text.length;
255
+ while (end > 0 && text.charCodeAt(end - 1) === PADDING) end -= 1;
256
+ return text.slice(0, end);
257
+ }
258
+ /**
259
+ * Decode base64url, with or without `=` padding.
260
+ *
261
+ * Strict where Node's buffer decoder is lenient: a character outside the
262
+ * alphabet, or a length no encoder produces (one character past a multiple of
263
+ * four), throws a `TypeError` instead of being skipped or truncated. This
264
+ * matches the engine's decoder.
265
+ */
266
+ function base64UrlToBytes(text) {
267
+ const unpadded = withoutPadding(text);
268
+ if (!BASE64URL_UNPADDED.test(unpadded) || unpadded.length % 4 === 1) throw new TypeError("not valid base64url (RFC 4648 § 5)");
269
+ const base64 = unpadded.replace(/-/g, "+").replace(/_/g, "/");
270
+ const binary = atob(base64 + "=".repeat((4 - base64.length % 4) % 4));
271
+ const bytes = new Uint8Array(binary.length);
272
+ for (let index = 0; index < binary.length; index += 1) bytes[index] = binary.charCodeAt(index);
273
+ return bytes;
274
+ }
275
+ /** Decode base64url to a string as UTF-8, replacing invalid sequences with U+FFFD. */
276
+ function base64UrlToString(text) {
277
+ return new TextDecoder().decode(base64UrlToBytes(text));
278
+ }
279
+ /** Encode bytes as base64url without padding. */
280
+ function bytesToBase64Url(bytes) {
281
+ let binary = "";
282
+ for (const byte of bytes) binary += String.fromCharCode(byte);
283
+ return withoutPadding(btoa(binary).replace(/\+/g, "-").replace(/\//g, "_"));
284
+ }
285
+
209
286
  //#endregion
210
287
  //#region src/canonical-signer/envelope.ts
211
288
  /**
@@ -225,22 +302,6 @@ async function parseSigningKey(seed) {
225
302
  * Sites-4 parity drivers, the Adobe trial.
226
303
  */
227
304
  /**
228
- * URL-safe base64 encoding without padding (RFC 4648 § 5).
229
- *
230
- * **Node-only.** Uses Node's `Buffer` API because every current consumer
231
- * of this module runs on Node (Vercel functions, Sites-1/2 Playwright
232
- * suites, future SDKs that bundle for Node + bundler-polyfilled runtimes
233
- * like Vercel Edge with `nodejs_compat`). If a future consumer needs a
234
- * pure-Web-API runtime (Cloudflare Workers without `nodejs_compat`,
235
- * Deno's stricter modes), swap to `Uint8Array.toBase64()` (Stage 4 as
236
- * of mid-2026) or a small inline encoder — but verify against the
237
- * envelope.test.ts round-trip first; the `base64url` character
238
- * substitution path is the part that has historically tripped polyfills.
239
- */
240
- function base64UrlEncode(bytes) {
241
- return Buffer.from(bytes).toString("base64").replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
242
- }
243
- /**
244
305
  * Sign an `McpIPayload` and produce a compact-JWS envelope ready to drop
245
306
  * into `_meta.proof.jws` of the request body per KYA-OS spec § 7.4.
246
307
  *
@@ -261,15 +322,40 @@ async function signMcpiEnvelope(payload, key) {
261
322
  kid: key.kid
262
323
  };
263
324
  const headerJson = JSON.stringify(header);
264
- const headerB64 = base64UrlEncode(new TextEncoder().encode(headerJson));
265
- const payloadB64 = base64UrlEncode(canonicalSigningPayload(payload));
325
+ const headerB64 = bytesToBase64Url(new TextEncoder().encode(headerJson));
326
+ const payloadB64 = bytesToBase64Url(canonicalSigningPayload(payload));
266
327
  const signingInput = new TextEncoder().encode(`${headerB64}.${payloadB64}`);
267
- return `${headerB64}.${payloadB64}.${base64UrlEncode(await signAsync(signingInput, key.signingKey))}`;
328
+ const signature = await signAsync(signingInput, key.signingKey);
329
+ return `${headerB64}.${payloadB64}.${bytesToBase64Url(signature)}`;
268
330
  }
269
331
 
270
332
  //#endregion
271
333
  //#region src/canonical-signer/parse.ts
272
334
  /**
335
+ * Compact-JWS parser for KYA-OS detached proofs.
336
+ *
337
+ * Producer-side counterpart to Sign-1's `signMcpiEnvelope`: takes the
338
+ * wire string the engine's H-1 parser would consume and decomposes it
339
+ * into the same logical pieces (header, payload, signature, signing
340
+ * input). Lives in `checkpoint-shared/canonical-signer/` so producer
341
+ * AND consumer share one wire-format implementation — the JCS / base64
342
+ * subtleties are handled in one place, not three.
343
+ *
344
+ * Consumed by:
345
+ * - `@kya-os/bouncer-middleware` (Envelope-1 / #2537)
346
+ * - any future TS host that needs to introspect a JWS without going
347
+ * through the WASM bridge
348
+ *
349
+ * The Rust engine's `jws::parse_jws_compact` is the structural mirror
350
+ * (`rust/crates/kya-os-engine/src/jws.rs`); this module produces the
351
+ * same logical decomposition. Cross-runtime parity here is implicit:
352
+ * if both parsers consume the same JWS string and disagree on what's
353
+ * inside, the H-1 fixtures would catch it.
354
+ *
355
+ * Sign-1 (#2500). Anti-pattern: don't write a parallel parser
356
+ * elsewhere in the org — import from here.
357
+ */
358
+ /**
273
359
  * Discriminated error type so consumers can map parse failures to
274
360
  * appropriate response codes (400 for malformed shape, 422 for
275
361
  * unsupported alg) without string-matching error messages.
@@ -335,7 +421,7 @@ function parseJwsCompact(jws) {
335
421
  const [headerB64, payloadB64, signatureB64] = parts;
336
422
  let headerJson;
337
423
  try {
338
- headerJson = JSON.parse(base64UrlDecodeToString(headerB64));
424
+ headerJson = JSON.parse(base64UrlToString(headerB64));
339
425
  } catch (cause) {
340
426
  throw new JwsParseError("InvalidJson", `JWS header is not valid JSON: ${String(cause)}`);
341
427
  }
@@ -353,7 +439,7 @@ function parseJwsCompact(jws) {
353
439
  };
354
440
  let payloadJson;
355
441
  try {
356
- payloadJson = JSON.parse(base64UrlDecodeToString(payloadB64));
442
+ payloadJson = JSON.parse(base64UrlToString(payloadB64));
357
443
  } catch (cause) {
358
444
  throw new JwsParseError("InvalidJson", `JWS payload is not valid JSON: ${String(cause)}`);
359
445
  }
@@ -364,7 +450,12 @@ function parseJwsCompact(jws) {
364
450
  throw new JwsParseError("InvalidPayloadShape", `JWS payload does not match McpIPayload shape: ${cause.message}`);
365
451
  }
366
452
  if (!BASE64URL_ALPHABET.test(signatureB64)) throw new JwsParseError("InvalidBase64", `JWS signature segment contains characters outside the base64url alphabet (RFC 4648 § 5)`);
367
- const signatureBytes = base64UrlDecodeToBytes(signatureB64);
453
+ let signatureBytes;
454
+ try {
455
+ signatureBytes = base64UrlToBytes(signatureB64);
456
+ } catch {
457
+ throw new JwsParseError("InvalidBase64", `JWS signature segment is not a valid base64url length (RFC 4648 § 5)`);
458
+ }
368
459
  if (signatureBytes.length !== 64) throw new JwsParseError("InvalidSignatureLength", `Ed25519 signatures are 64 bytes (RFC 8032 § 5.1.6); got ${signatureBytes.length}`);
369
460
  const signingInput = new TextEncoder().encode(`${headerB64}.${payloadB64}`);
370
461
  return {
@@ -375,37 +466,11 @@ function parseJwsCompact(jws) {
375
466
  };
376
467
  }
377
468
  /**
378
- * Strict base64url alphabet check (RFC 4648 § 5). The set is
379
- * `A-Za-z0-9-_` plus optional `=` padding. We use this to validate
380
- * input BEFORE handing to Node's `Buffer.from(s, 'base64')`, which
381
- * silently drops invalid characters rather than throwing — bypassing
382
- * the discriminated `InvalidBase64` error code consumers rely on for
383
- * HTTP-status mapping.
384
- *
385
- * The empty-string case is rejected by the regex (`+` requires ≥1
386
- * char), which is the desired behavior — an empty segment is a
387
- * different bug class from "non-base64 characters in a non-empty
388
- * segment" but both warrant the same `InvalidBase64` response.
469
+ * Strict base64url alphabet check (RFC 4648 § 5): `A-Za-z0-9-_` plus optional
470
+ * `=` padding. The empty string fails it on purpose: an empty signature
471
+ * segment is reported as `InvalidBase64` too.
389
472
  */
390
473
  const BASE64URL_ALPHABET = /^[A-Za-z0-9_-]+=*$/;
391
- /**
392
- * Decode URL-safe base64 (with optional padding) to bytes. RFC 4648 § 5.
393
- * Tolerates the no-padding form Sign-1 produces AND the padded form
394
- * some external producers emit.
395
- *
396
- * **Caller must validate the input alphabet first** if they want
397
- * `InvalidBase64` reported as a distinct error class — Node's underlying
398
- * `Buffer.from(s, 'base64')` silently truncates on invalid characters
399
- * rather than throwing.
400
- */
401
- function base64UrlDecodeToBytes(s) {
402
- const padded = s.replace(/-/g, "+").replace(/_/g, "/");
403
- const padLen = padded.length % 4 === 0 ? 0 : 4 - padded.length % 4;
404
- return new Uint8Array(Buffer.from(padded + "=".repeat(padLen), "base64"));
405
- }
406
- function base64UrlDecodeToString(s) {
407
- return Buffer.from(base64UrlDecodeToBytes(s)).toString("utf-8");
408
- }
409
474
 
410
475
  //#endregion
411
476
  export { JwsParseError, canonicalSigningPayload, parseJwsCompact, parseSigningKey, signMcpiEnvelope, validateMcpIPayload };