@kya-os/checkpoint-nextjs 1.7.1 → 1.10.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 (156) hide show
  1. package/CHANGELOG.md +107 -0
  2. package/EDGE_RUNTIME_WASM_SETUP.md +11 -11
  3. package/README.md +144 -251
  4. package/bin/setup-edge-wasm.js +9 -1
  5. package/dist/adapt.d.mts +6 -34
  6. package/dist/adapt.d.mts.map +1 -0
  7. package/dist/adapt.d.ts +6 -34
  8. package/dist/adapt.d.ts.map +1 -0
  9. package/dist/adapt.js +79 -49
  10. package/dist/adapt.mjs +78 -46
  11. package/dist/adapt.mjs.map +1 -0
  12. package/dist/api-client.d.mts +150 -150
  13. package/dist/api-client.d.mts.map +1 -0
  14. package/dist/api-client.d.ts +150 -150
  15. package/dist/api-client.d.ts.map +1 -0
  16. package/dist/api-client.js +218 -204
  17. package/dist/api-client.mjs +217 -201
  18. package/dist/api-client.mjs.map +1 -0
  19. package/dist/api-middleware-CDSh3TSE.js +379 -0
  20. package/dist/api-middleware-CMDRoQik.mjs +357 -0
  21. package/dist/api-middleware-CMDRoQik.mjs.map +1 -0
  22. package/dist/api-middleware.d.mts +117 -129
  23. package/dist/api-middleware.d.mts.map +1 -0
  24. package/dist/api-middleware.d.ts +117 -129
  25. package/dist/api-middleware.d.ts.map +1 -0
  26. package/dist/api-middleware.js +8 -540
  27. package/dist/api-middleware.mjs +3 -534
  28. package/dist/composed-policy-BCaNiVHv.d.mts +98 -0
  29. package/dist/composed-policy-BCaNiVHv.d.mts.map +1 -0
  30. package/dist/composed-policy-zzJYNFTb.d.ts +98 -0
  31. package/dist/composed-policy-zzJYNFTb.d.ts.map +1 -0
  32. package/dist/composed-policy.d.mts +2 -115
  33. package/dist/composed-policy.d.ts +2 -115
  34. package/dist/composed-policy.js +122 -88
  35. package/dist/composed-policy.mjs +121 -85
  36. package/dist/composed-policy.mjs.map +1 -0
  37. package/dist/config-CbAg2mze.d.ts +255 -0
  38. package/dist/config-CbAg2mze.d.ts.map +1 -0
  39. package/dist/config-Doe0Tn53.d.mts +255 -0
  40. package/dist/config-Doe0Tn53.d.mts.map +1 -0
  41. package/dist/create-middleware.d.mts +7 -18
  42. package/dist/create-middleware.d.mts.map +1 -0
  43. package/dist/create-middleware.d.ts +7 -18
  44. package/dist/create-middleware.d.ts.map +1 -0
  45. package/dist/create-middleware.js +36 -31
  46. package/dist/create-middleware.mjs +35 -28
  47. package/dist/create-middleware.mjs.map +1 -0
  48. package/dist/edge/index.d.mts +64 -86
  49. package/dist/edge/index.d.mts.map +1 -0
  50. package/dist/edge/index.d.ts +64 -86
  51. package/dist/edge/index.d.ts.map +1 -0
  52. package/dist/edge/index.js +349 -275
  53. package/dist/edge/index.mjs +348 -272
  54. package/dist/edge/index.mjs.map +1 -0
  55. package/dist/edge-runtime-loader.d.mts +37 -63
  56. package/dist/edge-runtime-loader.d.mts.map +1 -0
  57. package/dist/edge-runtime-loader.d.ts +37 -63
  58. package/dist/edge-runtime-loader.d.ts.map +1 -0
  59. package/dist/edge-runtime-loader.js +268 -225
  60. package/dist/edge-runtime-loader.mjs +267 -222
  61. package/dist/edge-runtime-loader.mjs.map +1 -0
  62. package/dist/edge-wasm-middleware.d.mts +27 -46
  63. package/dist/edge-wasm-middleware.d.mts.map +1 -0
  64. package/dist/edge-wasm-middleware.d.ts +27 -46
  65. package/dist/edge-wasm-middleware.d.ts.map +1 -0
  66. package/dist/edge-wasm-middleware.js +26 -15
  67. package/dist/edge-wasm-middleware.mjs +25 -12
  68. package/dist/edge-wasm-middleware.mjs.map +1 -0
  69. package/dist/index.d.mts +10 -17
  70. package/dist/index.d.ts +10 -17
  71. package/dist/index.js +55 -1224
  72. package/dist/index.mjs +16 -1193
  73. package/dist/index.mjs.map +1 -0
  74. package/dist/middleware-bundler.d.mts +7 -0
  75. package/dist/middleware-bundler.d.mts.map +1 -0
  76. package/dist/middleware-bundler.d.ts +7 -0
  77. package/dist/middleware-bundler.d.ts.map +1 -0
  78. package/dist/middleware-bundler.js +16 -0
  79. package/dist/middleware-bundler.mjs +16 -0
  80. package/dist/middleware-bundler.mjs.map +1 -0
  81. package/dist/middleware-core-BYQP9FND.mjs +165 -0
  82. package/dist/middleware-core-BYQP9FND.mjs.map +1 -0
  83. package/dist/middleware-core-D6TijE6P.js +175 -0
  84. package/dist/middleware-edge.d.mts +8 -50
  85. package/dist/middleware-edge.d.mts.map +1 -0
  86. package/dist/middleware-edge.d.ts +8 -50
  87. package/dist/middleware-edge.d.ts.map +1 -0
  88. package/dist/middleware-edge.js +41 -307
  89. package/dist/middleware-edge.mjs +39 -299
  90. package/dist/middleware-edge.mjs.map +1 -0
  91. package/dist/middleware-node-CE4GevYs.d.ts +20 -0
  92. package/dist/middleware-node-CE4GevYs.d.ts.map +1 -0
  93. package/dist/middleware-node-DtyOuPta.d.mts +20 -0
  94. package/dist/middleware-node-DtyOuPta.d.mts.map +1 -0
  95. package/dist/middleware-node-context-BqPbf-fI.mjs +34 -0
  96. package/dist/middleware-node-context-BqPbf-fI.mjs.map +1 -0
  97. package/dist/middleware-node-context-rgnc7UNR.js +38 -0
  98. package/dist/middleware-node.d.mts +3 -63
  99. package/dist/middleware-node.d.ts +3 -63
  100. package/dist/middleware-node.js +13 -308
  101. package/dist/middleware-node.mjs +12 -301
  102. package/dist/middleware-node.mjs.map +1 -0
  103. package/dist/middleware.d.mts +8 -28
  104. package/dist/middleware.d.mts.map +1 -0
  105. package/dist/middleware.d.ts +8 -28
  106. package/dist/middleware.d.ts.map +1 -0
  107. package/dist/middleware.js +43 -9
  108. package/dist/middleware.mjs +42 -6
  109. package/dist/middleware.mjs.map +1 -0
  110. package/dist/nodejs-wasm-loader.d.mts +8 -7
  111. package/dist/nodejs-wasm-loader.d.mts.map +1 -0
  112. package/dist/nodejs-wasm-loader.d.ts +8 -7
  113. package/dist/nodejs-wasm-loader.d.ts.map +1 -0
  114. package/dist/nodejs-wasm-loader.js +53 -19
  115. package/dist/nodejs-wasm-loader.mjs +52 -16
  116. package/dist/nodejs-wasm-loader.mjs.map +1 -0
  117. package/dist/policy.d.mts +86 -110
  118. package/dist/policy.d.mts.map +1 -0
  119. package/dist/policy.d.ts +86 -110
  120. package/dist/policy.d.ts.map +1 -0
  121. package/dist/policy.js +203 -160
  122. package/dist/policy.mjs +183 -147
  123. package/dist/policy.mjs.map +1 -0
  124. package/dist/session-tracker.d.mts +42 -47
  125. package/dist/session-tracker.d.mts.map +1 -0
  126. package/dist/session-tracker.d.ts +42 -47
  127. package/dist/session-tracker.d.ts.map +1 -0
  128. package/dist/session-tracker.js +141 -163
  129. package/dist/session-tracker.mjs +140 -160
  130. package/dist/session-tracker.mjs.map +1 -0
  131. package/dist/translate.d.mts +18 -30
  132. package/dist/translate.d.mts.map +1 -0
  133. package/dist/translate.d.ts +18 -30
  134. package/dist/translate.d.ts.map +1 -0
  135. package/dist/translate.js +70 -34
  136. package/dist/translate.mjs +70 -32
  137. package/dist/translate.mjs.map +1 -0
  138. package/dist/types-C0SkqLMM.d.mts +102 -0
  139. package/dist/types-C0SkqLMM.d.mts.map +1 -0
  140. package/dist/types-C7DNSCTO.d.ts +102 -0
  141. package/dist/types-C7DNSCTO.d.ts.map +1 -0
  142. package/dist/utils-Cj4XNhIY.mjs +35 -0
  143. package/dist/utils-Cj4XNhIY.mjs.map +1 -0
  144. package/dist/utils-DAisnYnI.js +45 -0
  145. package/dist/wasm-setup.d.mts +6 -5
  146. package/dist/wasm-setup.d.mts.map +1 -0
  147. package/dist/wasm-setup.d.ts +6 -5
  148. package/dist/wasm-setup.d.ts.map +1 -0
  149. package/dist/wasm-setup.js +82 -113
  150. package/dist/wasm-setup.mjs +85 -108
  151. package/dist/wasm-setup.mjs.map +1 -0
  152. package/package.json +22 -15
  153. package/dist/config-DAwIA4DB.d.mts +0 -214
  154. package/dist/config-DyU4l5er.d.ts +0 -214
  155. package/dist/types-D9RQvPNy.d.mts +0 -105
  156. package/dist/types-D9RQvPNy.d.ts +0 -105
@@ -0,0 +1,98 @@
1
+ import { PolicyFetcher } from "@kya-os/checkpoint-shared";
2
+ import { ComposedPolicyCompile } from "@kya-os/checkpoint-wasm-runtime/composed-policy";
3
+ import { Decision, VerifyResult } from "@kya-os/checkpoint-wasm-runtime/engine";
4
+ //#region src/composed-policy.d.ts
5
+ /** Dashboard the composed-policy fetch defaults to when no base URL is set. */
6
+ declare const DEFAULT_DASHBOARD_URL = "https://kya.vouched.id";
7
+ /**
8
+ * Telemetry sink. The shadow-divergence signal is the go/no-go instrument for
9
+ * flipping a project to ENFORCE. Defaults to a no-op so the SDK never logs
10
+ * unprompted — `withCheckpoint({ debug: true })` wires the console logger.
11
+ */
12
+ interface ComposedPolicyLogger {
13
+ shadowDivergence(info: ShadowDivergenceInfo): void;
14
+ evaluationError(projectId: string, error: unknown): void;
15
+ }
16
+ interface ShadowDivergenceInfo {
17
+ projectId: string;
18
+ path?: string;
19
+ engineDecision: Decision['kind'];
20
+ structuredDecision: Decision['kind'];
21
+ detectionClass: string;
22
+ verificationMethod?: string;
23
+ confidence: number;
24
+ agentName?: string;
25
+ }
26
+ /** Result of one composed-policy evaluation: the decision to enforce + whether it replaced the baseline. */
27
+ interface ComposedPolicyApplyResult {
28
+ decision: Decision;
29
+ acted: boolean;
30
+ /**
31
+ * SSOT-served revision pin (sha256 of the evaluated bundle, byte-identical
32
+ * to `policy_revisions.composed_blob_hash`) — set ONLY when `acted`, echoed
33
+ * from `PolicyConfig.policySourceHash` (#3246 PR5). Absent on structured
34
+ * decisions and when the dashboard predates the field.
35
+ */
36
+ composedBlobHash?: string;
37
+ }
38
+ interface ComposedPolicyContext {
39
+ /**
40
+ * Evaluate the project's composed policy for one request. Reads
41
+ * `result.decision` as the structured baseline for the divergence comparison;
42
+ * the caller applies `acted ? decision` afterwards. Never throws.
43
+ */
44
+ apply(result: VerifyResult, path: string | undefined): Promise<ComposedPolicyApplyResult>;
45
+ /**
46
+ * Resolve the org's trusted Layer-2 delegation root DIDs from the project's
47
+ * policy payload (P2 / DR-1). The middleware forwards these into the engine's
48
+ * `ContextSpec.trustedDelegationRoots` on delegation requests. Optional so an
49
+ * injected `composedPolicyEnforcer` (advanced/testing) need not implement it —
50
+ * absence ⇒ no roots ⇒ the engine's open default. Never throws (a fetch fault
51
+ * resolves to `[]`).
52
+ */
53
+ trustedDelegationRoots?(): Promise<string[]>;
54
+ }
55
+ interface MakeComposedPolicyContextOptions {
56
+ projectId: string;
57
+ /** Fetches the project's `PolicyConfig` (carries the composed-policy fields). */
58
+ fetcher: PolicyFetcher;
59
+ /** Target compiler — Node `createPolicyEvaluator`-backed or edge `evaluatePolicy`. */
60
+ compile: ComposedPolicyCompile;
61
+ logger?: ComposedPolicyLogger;
62
+ cacheMax?: number;
63
+ }
64
+ declare function makeComposedPolicyContext(opts: MakeComposedPolicyContextOptions): ComposedPolicyContext;
65
+ /**
66
+ * Resolves the org's trusted delegation roots (empty when none). Built per
67
+ * middleware factory from whatever source can supply them — the composed
68
+ * context's accessor, or (when no context exists, e.g. Edge without
69
+ * `cedarWasmModule`) a `projectId`-only policy fetch.
70
+ */
71
+ type TrustedDelegationRootsResolver = () => Promise<string[]>;
72
+ /**
73
+ * Resolve the org trusted-delegation roots to inject into the engine for THIS
74
+ * request (P2 / DR-1). Returns `undefined` — leaving `VerifyRequestOpts`
75
+ * byte-identical to pre-P2 — unless (a) a resolver is configured and (b) the
76
+ * request actually carries a delegation-proof header (so the human /
77
+ * no-delegation hot path never pays the early policy fetch). Shared by the Node
78
+ * and Edge middleware entries.
79
+ */
80
+ declare function resolveTrustedDelegationRootsForRequest(resolver: TrustedDelegationRootsResolver | null, headers: Headers): Promise<string[] | undefined>;
81
+ /**
82
+ * The per-request seam shared by both middleware entries. Evaluates the composed
83
+ * policy and swaps `result.decision` IN PLACE when the engine acts — so the
84
+ * swap happens BEFORE the detection reporter fires and before
85
+ * `renderDecisionAsResponse`, exactly like the Express runtime (the reporter
86
+ * records the enforced decision). Fail-OPEN: any fault keeps the structured
87
+ * decision; a policy fault never breaks the response.
88
+ */
89
+ declare function applyComposedPolicy(context: ComposedPolicyContext | null, result: VerifyResult, path: string | undefined): Promise<void>;
90
+ /**
91
+ * Debug logger mirroring the worker's `console.warn`/`console.error` shadow +
92
+ * fail-open telemetry. Wired by `withCheckpoint` only when `config.debug` is
93
+ * set, so the default SDK posture stays silent.
94
+ */
95
+ declare const consoleComposedPolicyLogger: ComposedPolicyLogger;
96
+ //#endregion
97
+ export { MakeComposedPolicyContextOptions as a, applyComposedPolicy as c, resolveTrustedDelegationRootsForRequest as d, DEFAULT_DASHBOARD_URL as i, consoleComposedPolicyLogger as l, ComposedPolicyContext as n, ShadowDivergenceInfo as o, ComposedPolicyLogger as r, TrustedDelegationRootsResolver as s, ComposedPolicyApplyResult as t, makeComposedPolicyContext as u };
98
+ //# sourceMappingURL=composed-policy-zzJYNFTb.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"composed-policy-zzJYNFTb.d.ts","names":[],"sources":["../src/composed-policy.ts"],"mappings":";;;;;cA6Ba;;;;;;UAOI;EACf,iBAAiB,MAAM;EACvB,gBAAgB,mBAAmB;;UAGpB;EACf;EACA;EACA,gBAAgB;EAChB,oBAAoB;EACpB;EACA;EACA;EACA;;;UASe;EACf,UAAU;EACV;;;;;;;EAOA;;UAGe;;;;;;EAMf,MAAM,QAAQ,cAAc,2BAA2B,QAAQ;;;;;;;;;EAS/D,2BAA2B;;UAGZ;EACf;;EAEA,SAAS;;EAET,SAAS;EACT,SAAS;EACT;;iBAGc,0BACd,MAAM,mCACL;;;;;;;KA8ES,uCAAuC;;;;;;;;;iBAU7B,wCACpB,UAAU,uCACV,SAAS,UACR;;;;;;;;;iBAemB,oBACpB,SAAS,8BACT,QAAQ,cACR,2BACC;;;;;;cAsBU,6BAA6B"}
@@ -1,115 +1,2 @@
1
- import { ComposedPolicyCompile } from '@kya-os/checkpoint-wasm-runtime/composed-policy';
2
- import { Decision, VerifyResult } from '@kya-os/checkpoint-wasm-runtime/engine';
3
- import { PolicyFetcher } from '@kya-os/checkpoint-shared';
4
-
5
- /**
6
- * Composed-policy (kya-os-engine) wiring for the Next.js middleware.
7
- *
8
- * A THIN layer on top of the shared core
9
- * (`@kya-os/checkpoint-wasm-runtime/composed-policy`): the core owns all the
10
- * logic (empty-Cedar deny-all guard, compile-once LRU cache, act-gate,
11
- * divergence, and the `verifyResultToAuthorizeInput` parity projection), so this
12
- * module only fetches the project's `PolicyConfig`, drives the core once per
13
- * request, maps the tagged outcome to a decision-swap signal, and logs
14
- * (debug-gated). Runtime-neutral — the caller injects the compiler:
15
- * - Node: `(language, source) => createPolicyEvaluator(source)` (`./policy`)
16
- * - Edge: `(language, source) => evaluatePolicy(language, source)` (`./policy-edge`)
17
- *
18
- * This is the SDK sibling of `@kya-os/checkpoint-express`'s composed-policy
19
- * wiring; both consume the same core so the DNS Gateway, Express, and Next.js
20
- * produce the same decision for the same composed Cedar bundle.
21
- */
22
-
23
- /** Dashboard the composed-policy fetch defaults to when no base URL is set. */
24
- declare const DEFAULT_DASHBOARD_URL = "https://kya.vouched.id";
25
- /**
26
- * Telemetry sink. The shadow-divergence signal is the go/no-go instrument for
27
- * flipping a project to ENFORCE. Defaults to a no-op so the SDK never logs
28
- * unprompted — `withCheckpoint({ debug: true })` wires the console logger.
29
- */
30
- interface ComposedPolicyLogger {
31
- shadowDivergence(info: ShadowDivergenceInfo): void;
32
- evaluationError(projectId: string, error: unknown): void;
33
- }
34
- interface ShadowDivergenceInfo {
35
- projectId: string;
36
- path?: string;
37
- engineDecision: Decision['kind'];
38
- structuredDecision: Decision['kind'];
39
- detectionClass: string;
40
- verificationMethod?: string;
41
- confidence: number;
42
- agentName?: string;
43
- }
44
- /** Result of one composed-policy evaluation: the decision to enforce + whether it replaced the baseline. */
45
- interface ComposedPolicyApplyResult {
46
- decision: Decision;
47
- acted: boolean;
48
- /**
49
- * SSOT-served revision pin (sha256 of the evaluated bundle, byte-identical
50
- * to `policy_revisions.composed_blob_hash`) — set ONLY when `acted`, echoed
51
- * from `PolicyConfig.policySourceHash` (#3246 PR5). Absent on structured
52
- * decisions and when the dashboard predates the field.
53
- */
54
- composedBlobHash?: string;
55
- }
56
- interface ComposedPolicyContext {
57
- /**
58
- * Evaluate the project's composed policy for one request. Reads
59
- * `result.decision` as the structured baseline for the divergence comparison;
60
- * the caller applies `acted ? decision` afterwards. Never throws.
61
- */
62
- apply(result: VerifyResult, path: string | undefined): Promise<ComposedPolicyApplyResult>;
63
- /**
64
- * Resolve the org's trusted Layer-2 delegation root DIDs from the project's
65
- * policy payload (P2 / DR-1). The middleware forwards these into the engine's
66
- * `ContextSpec.trustedDelegationRoots` on delegation requests. Optional so an
67
- * injected `composedPolicyEnforcer` (advanced/testing) need not implement it —
68
- * absence ⇒ no roots ⇒ the engine's open default. Never throws (a fetch fault
69
- * resolves to `[]`).
70
- */
71
- trustedDelegationRoots?(): Promise<string[]>;
72
- }
73
- interface MakeComposedPolicyContextOptions {
74
- projectId: string;
75
- /** Fetches the project's `PolicyConfig` (carries the composed-policy fields). */
76
- fetcher: PolicyFetcher;
77
- /** Target compiler — Node `createPolicyEvaluator`-backed or edge `evaluatePolicy`. */
78
- compile: ComposedPolicyCompile;
79
- logger?: ComposedPolicyLogger;
80
- cacheMax?: number;
81
- }
82
- declare function makeComposedPolicyContext(opts: MakeComposedPolicyContextOptions): ComposedPolicyContext;
83
- /**
84
- * Resolves the org's trusted delegation roots (empty when none). Built per
85
- * middleware factory from whatever source can supply them — the composed
86
- * context's accessor, or (when no context exists, e.g. Edge without
87
- * `cedarWasmModule`) a `projectId`-only policy fetch.
88
- */
89
- type TrustedDelegationRootsResolver = () => Promise<string[]>;
90
- /**
91
- * Resolve the org trusted-delegation roots to inject into the engine for THIS
92
- * request (P2 / DR-1). Returns `undefined` — leaving `VerifyRequestOpts`
93
- * byte-identical to pre-P2 — unless (a) a resolver is configured and (b) the
94
- * request actually carries a delegation-proof header (so the human /
95
- * no-delegation hot path never pays the early policy fetch). Shared by the Node
96
- * and Edge middleware entries.
97
- */
98
- declare function resolveTrustedDelegationRootsForRequest(resolver: TrustedDelegationRootsResolver | null, headers: Headers): Promise<string[] | undefined>;
99
- /**
100
- * The per-request seam shared by both middleware entries. Evaluates the composed
101
- * policy and swaps `result.decision` IN PLACE when the engine acts — so the
102
- * swap happens BEFORE the detection reporter fires and before
103
- * `renderDecisionAsResponse`, exactly like the Express runtime (the reporter
104
- * records the enforced decision). Fail-OPEN: any fault keeps the structured
105
- * decision; a policy fault never breaks the response.
106
- */
107
- declare function applyComposedPolicy(context: ComposedPolicyContext | null, result: VerifyResult, path: string | undefined): Promise<void>;
108
- /**
109
- * Debug logger mirroring the worker's `console.warn`/`console.error` shadow +
110
- * fail-open telemetry. Wired by `withCheckpoint` only when `config.debug` is
111
- * set, so the default SDK posture stays silent.
112
- */
113
- declare const consoleComposedPolicyLogger: ComposedPolicyLogger;
114
-
115
- export { type ComposedPolicyApplyResult, type ComposedPolicyContext, type ComposedPolicyLogger, DEFAULT_DASHBOARD_URL, type MakeComposedPolicyContextOptions, type ShadowDivergenceInfo, type TrustedDelegationRootsResolver, applyComposedPolicy, consoleComposedPolicyLogger, makeComposedPolicyContext, resolveTrustedDelegationRootsForRequest };
1
+ import { a as MakeComposedPolicyContextOptions, c as applyComposedPolicy, d as resolveTrustedDelegationRootsForRequest, i as DEFAULT_DASHBOARD_URL, l as consoleComposedPolicyLogger, n as ComposedPolicyContext, o as ShadowDivergenceInfo, r as ComposedPolicyLogger, s as TrustedDelegationRootsResolver, t as ComposedPolicyApplyResult, u as makeComposedPolicyContext } from "./composed-policy-BCaNiVHv.mjs";
2
+ export { ComposedPolicyApplyResult, ComposedPolicyContext, ComposedPolicyLogger, DEFAULT_DASHBOARD_URL, MakeComposedPolicyContextOptions, ShadowDivergenceInfo, TrustedDelegationRootsResolver, applyComposedPolicy, consoleComposedPolicyLogger, makeComposedPolicyContext, resolveTrustedDelegationRootsForRequest };
@@ -1,115 +1,2 @@
1
- import { ComposedPolicyCompile } from '@kya-os/checkpoint-wasm-runtime/composed-policy';
2
- import { Decision, VerifyResult } from '@kya-os/checkpoint-wasm-runtime/engine';
3
- import { PolicyFetcher } from '@kya-os/checkpoint-shared';
4
-
5
- /**
6
- * Composed-policy (kya-os-engine) wiring for the Next.js middleware.
7
- *
8
- * A THIN layer on top of the shared core
9
- * (`@kya-os/checkpoint-wasm-runtime/composed-policy`): the core owns all the
10
- * logic (empty-Cedar deny-all guard, compile-once LRU cache, act-gate,
11
- * divergence, and the `verifyResultToAuthorizeInput` parity projection), so this
12
- * module only fetches the project's `PolicyConfig`, drives the core once per
13
- * request, maps the tagged outcome to a decision-swap signal, and logs
14
- * (debug-gated). Runtime-neutral — the caller injects the compiler:
15
- * - Node: `(language, source) => createPolicyEvaluator(source)` (`./policy`)
16
- * - Edge: `(language, source) => evaluatePolicy(language, source)` (`./policy-edge`)
17
- *
18
- * This is the SDK sibling of `@kya-os/checkpoint-express`'s composed-policy
19
- * wiring; both consume the same core so the DNS Gateway, Express, and Next.js
20
- * produce the same decision for the same composed Cedar bundle.
21
- */
22
-
23
- /** Dashboard the composed-policy fetch defaults to when no base URL is set. */
24
- declare const DEFAULT_DASHBOARD_URL = "https://kya.vouched.id";
25
- /**
26
- * Telemetry sink. The shadow-divergence signal is the go/no-go instrument for
27
- * flipping a project to ENFORCE. Defaults to a no-op so the SDK never logs
28
- * unprompted — `withCheckpoint({ debug: true })` wires the console logger.
29
- */
30
- interface ComposedPolicyLogger {
31
- shadowDivergence(info: ShadowDivergenceInfo): void;
32
- evaluationError(projectId: string, error: unknown): void;
33
- }
34
- interface ShadowDivergenceInfo {
35
- projectId: string;
36
- path?: string;
37
- engineDecision: Decision['kind'];
38
- structuredDecision: Decision['kind'];
39
- detectionClass: string;
40
- verificationMethod?: string;
41
- confidence: number;
42
- agentName?: string;
43
- }
44
- /** Result of one composed-policy evaluation: the decision to enforce + whether it replaced the baseline. */
45
- interface ComposedPolicyApplyResult {
46
- decision: Decision;
47
- acted: boolean;
48
- /**
49
- * SSOT-served revision pin (sha256 of the evaluated bundle, byte-identical
50
- * to `policy_revisions.composed_blob_hash`) — set ONLY when `acted`, echoed
51
- * from `PolicyConfig.policySourceHash` (#3246 PR5). Absent on structured
52
- * decisions and when the dashboard predates the field.
53
- */
54
- composedBlobHash?: string;
55
- }
56
- interface ComposedPolicyContext {
57
- /**
58
- * Evaluate the project's composed policy for one request. Reads
59
- * `result.decision` as the structured baseline for the divergence comparison;
60
- * the caller applies `acted ? decision` afterwards. Never throws.
61
- */
62
- apply(result: VerifyResult, path: string | undefined): Promise<ComposedPolicyApplyResult>;
63
- /**
64
- * Resolve the org's trusted Layer-2 delegation root DIDs from the project's
65
- * policy payload (P2 / DR-1). The middleware forwards these into the engine's
66
- * `ContextSpec.trustedDelegationRoots` on delegation requests. Optional so an
67
- * injected `composedPolicyEnforcer` (advanced/testing) need not implement it —
68
- * absence ⇒ no roots ⇒ the engine's open default. Never throws (a fetch fault
69
- * resolves to `[]`).
70
- */
71
- trustedDelegationRoots?(): Promise<string[]>;
72
- }
73
- interface MakeComposedPolicyContextOptions {
74
- projectId: string;
75
- /** Fetches the project's `PolicyConfig` (carries the composed-policy fields). */
76
- fetcher: PolicyFetcher;
77
- /** Target compiler — Node `createPolicyEvaluator`-backed or edge `evaluatePolicy`. */
78
- compile: ComposedPolicyCompile;
79
- logger?: ComposedPolicyLogger;
80
- cacheMax?: number;
81
- }
82
- declare function makeComposedPolicyContext(opts: MakeComposedPolicyContextOptions): ComposedPolicyContext;
83
- /**
84
- * Resolves the org's trusted delegation roots (empty when none). Built per
85
- * middleware factory from whatever source can supply them — the composed
86
- * context's accessor, or (when no context exists, e.g. Edge without
87
- * `cedarWasmModule`) a `projectId`-only policy fetch.
88
- */
89
- type TrustedDelegationRootsResolver = () => Promise<string[]>;
90
- /**
91
- * Resolve the org trusted-delegation roots to inject into the engine for THIS
92
- * request (P2 / DR-1). Returns `undefined` — leaving `VerifyRequestOpts`
93
- * byte-identical to pre-P2 — unless (a) a resolver is configured and (b) the
94
- * request actually carries a delegation-proof header (so the human /
95
- * no-delegation hot path never pays the early policy fetch). Shared by the Node
96
- * and Edge middleware entries.
97
- */
98
- declare function resolveTrustedDelegationRootsForRequest(resolver: TrustedDelegationRootsResolver | null, headers: Headers): Promise<string[] | undefined>;
99
- /**
100
- * The per-request seam shared by both middleware entries. Evaluates the composed
101
- * policy and swaps `result.decision` IN PLACE when the engine acts — so the
102
- * swap happens BEFORE the detection reporter fires and before
103
- * `renderDecisionAsResponse`, exactly like the Express runtime (the reporter
104
- * records the enforced decision). Fail-OPEN: any fault keeps the structured
105
- * decision; a policy fault never breaks the response.
106
- */
107
- declare function applyComposedPolicy(context: ComposedPolicyContext | null, result: VerifyResult, path: string | undefined): Promise<void>;
108
- /**
109
- * Debug logger mirroring the worker's `console.warn`/`console.error` shadow +
110
- * fail-open telemetry. Wired by `withCheckpoint` only when `config.debug` is
111
- * set, so the default SDK posture stays silent.
112
- */
113
- declare const consoleComposedPolicyLogger: ComposedPolicyLogger;
114
-
115
- export { type ComposedPolicyApplyResult, type ComposedPolicyContext, type ComposedPolicyLogger, DEFAULT_DASHBOARD_URL, type MakeComposedPolicyContextOptions, type ShadowDivergenceInfo, type TrustedDelegationRootsResolver, applyComposedPolicy, consoleComposedPolicyLogger, makeComposedPolicyContext, resolveTrustedDelegationRootsForRequest };
1
+ import { a as MakeComposedPolicyContextOptions, c as applyComposedPolicy, d as resolveTrustedDelegationRootsForRequest, i as DEFAULT_DASHBOARD_URL, l as consoleComposedPolicyLogger, n as ComposedPolicyContext, o as ShadowDivergenceInfo, r as ComposedPolicyLogger, s as TrustedDelegationRootsResolver, t as ComposedPolicyApplyResult, u as makeComposedPolicyContext } from "./composed-policy-zzJYNFTb.js";
2
+ export { ComposedPolicyApplyResult, ComposedPolicyContext, ComposedPolicyLogger, DEFAULT_DASHBOARD_URL, MakeComposedPolicyContextOptions, ShadowDivergenceInfo, TrustedDelegationRootsResolver, applyComposedPolicy, consoleComposedPolicyLogger, makeComposedPolicyContext, resolveTrustedDelegationRootsForRequest };
@@ -1,102 +1,136 @@
1
- 'use strict';
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2
+ let _kya_os_checkpoint_wasm_runtime_composed_policy = require("@kya-os/checkpoint-wasm-runtime/composed-policy");
3
+ let _kya_os_checkpoint_shared = require("@kya-os/checkpoint-shared");
2
4
 
3
- var composedPolicy = require('@kya-os/checkpoint-wasm-runtime/composed-policy');
4
- var checkpointShared = require('@kya-os/checkpoint-shared');
5
-
6
- // src/composed-policy.ts
7
- var DEFAULT_DASHBOARD_URL = "https://kya.vouched.id";
8
- var NOOP_LOGGER = {
9
- shadowDivergence: () => {
10
- },
11
- evaluationError: () => {
12
- }
5
+ //#region src/composed-policy.ts
6
+ /**
7
+ * Composed-policy (kya-os-engine) wiring for the Next.js middleware.
8
+ *
9
+ * A THIN layer on top of the shared core
10
+ * (`@kya-os/checkpoint-wasm-runtime/composed-policy`): the core owns all the
11
+ * logic (empty-Cedar deny-all guard, compile-once LRU cache, act-gate,
12
+ * divergence, and the `verifyResultToAuthorizeInput` parity projection), so this
13
+ * module only fetches the project's `PolicyConfig`, drives the core once per
14
+ * request, maps the tagged outcome to a decision-swap signal, and logs
15
+ * (debug-gated). Runtime-neutral — the caller injects the compiler:
16
+ * - Node: `(language, source) => createPolicyEvaluator(source)` (`./policy`)
17
+ * - Edge: `(language, source) => evaluatePolicy(language, source)` (`./policy-edge`)
18
+ *
19
+ * This is the SDK sibling of `@kya-os/checkpoint-express`'s composed-policy
20
+ * wiring; both consume the same core so the DNS Gateway, Express, and Next.js
21
+ * produce the same decision for the same composed Cedar bundle.
22
+ */
23
+ /** Dashboard the composed-policy fetch defaults to when no base URL is set. */
24
+ const DEFAULT_DASHBOARD_URL = "https://kya.vouched.id";
25
+ const NOOP_LOGGER = {
26
+ shadowDivergence: () => {},
27
+ evaluationError: () => {}
13
28
  };
14
29
  function makeComposedPolicyContext(opts) {
15
- const { projectId, fetcher } = opts;
16
- const cache = composedPolicy.makeComposedPolicyCache({ compile: opts.compile, cacheMax: opts.cacheMax });
17
- const logger = opts.logger ?? NOOP_LOGGER;
18
- return {
19
- async apply(result, path) {
20
- const structured = { decision: result.decision, acted: false };
21
- const policy = await fetcher.getPolicy(projectId);
22
- const outcome = await composedPolicy.evaluateComposedPolicy({
23
- cache,
24
- projectId,
25
- flags: {
26
- policyLanguage: policy.policyLanguage,
27
- policySourceText: policy.policySourceText,
28
- engineEnforcementEnabled: policy.engineEnforcementEnabled,
29
- enabled: policy.enabled
30
- },
31
- authorizeInput: composedPolicy.verifyResultToAuthorizeInput(result, { tenantId: projectId, path }),
32
- baselineDecisionKind: result.decision.kind
33
- });
34
- if ((outcome.status === "acting" || outcome.status === "shadow") && outcome.diverged) {
35
- logger.shadowDivergence({
36
- projectId,
37
- path,
38
- engineDecision: outcome.engineDecision.kind,
39
- structuredDecision: result.decision.kind,
40
- detectionClass: result.detectionDetail.detectionClass.type,
41
- verificationMethod: result.detectionDetail.verificationMethod,
42
- confidence: result.detectionDetail.confidence,
43
- agentName: result.detectionDetail.detectedAgent?.name
44
- });
45
- }
46
- if (outcome.status === "error") {
47
- logger.evaluationError(projectId, outcome.error);
48
- return structured;
49
- }
50
- if (outcome.status === "acting") {
51
- return {
52
- decision: outcome.engineDecision,
53
- acted: true,
54
- // Echo the SSOT revision pin with the decision it produced (#3246
55
- // PR5). Conditional spread: absent upstream stays absent here.
56
- ...policy.policySourceHash ? { composedBlobHash: policy.policySourceHash } : {}
57
- };
58
- }
59
- return structured;
60
- },
61
- async trustedDelegationRoots() {
62
- const policy = await fetcher.getPolicy(projectId);
63
- return policy.trustedDelegationRoots ?? [];
64
- }
65
- };
30
+ const { projectId, fetcher } = opts;
31
+ const cache = (0, _kya_os_checkpoint_wasm_runtime_composed_policy.makeComposedPolicyCache)({
32
+ compile: opts.compile,
33
+ cacheMax: opts.cacheMax
34
+ });
35
+ const logger = opts.logger ?? NOOP_LOGGER;
36
+ return {
37
+ async apply(result, path) {
38
+ const structured = {
39
+ decision: result.decision,
40
+ acted: false
41
+ };
42
+ if ((0, _kya_os_checkpoint_wasm_runtime_composed_policy.isCredentialVerificationBlock)(result.decision)) return structured;
43
+ const policy = await fetcher.getPolicy(projectId);
44
+ const outcome = await (0, _kya_os_checkpoint_wasm_runtime_composed_policy.evaluateComposedPolicy)({
45
+ cache,
46
+ projectId,
47
+ flags: {
48
+ policyLanguage: policy.policyLanguage,
49
+ policySourceText: policy.policySourceText,
50
+ engineEnforcementEnabled: policy.engineEnforcementEnabled,
51
+ enabled: policy.enabled
52
+ },
53
+ authorizeInput: (0, _kya_os_checkpoint_wasm_runtime_composed_policy.verifyResultToAuthorizeInput)(result, {
54
+ tenantId: projectId,
55
+ path
56
+ }),
57
+ baselineDecisionKind: result.decision.kind
58
+ });
59
+ if ((outcome.status === "acting" || outcome.status === "shadow") && outcome.diverged) logger.shadowDivergence({
60
+ projectId,
61
+ path,
62
+ engineDecision: outcome.engineDecision.kind,
63
+ structuredDecision: result.decision.kind,
64
+ detectionClass: result.detectionDetail.detectionClass.type,
65
+ verificationMethod: result.detectionDetail.verificationMethod,
66
+ confidence: result.detectionDetail.confidence,
67
+ agentName: result.detectionDetail.detectedAgent?.name
68
+ });
69
+ if (outcome.status === "error") {
70
+ logger.evaluationError(projectId, outcome.error);
71
+ return structured;
72
+ }
73
+ if (outcome.status === "acting") return {
74
+ decision: outcome.engineDecision,
75
+ acted: true,
76
+ ...policy.policySourceHash ? { composedBlobHash: policy.policySourceHash } : {}
77
+ };
78
+ return structured;
79
+ },
80
+ async trustedDelegationRoots() {
81
+ return (await fetcher.getPolicy(projectId)).trustedDelegationRoots ?? [];
82
+ }
83
+ };
66
84
  }
85
+ /**
86
+ * Resolve the org trusted-delegation roots to inject into the engine for THIS
87
+ * request (P2 / DR-1). Returns `undefined` — leaving `VerifyRequestOpts`
88
+ * byte-identical to pre-P2 — unless (a) a resolver is configured and (b) the
89
+ * request actually carries a delegation-proof header (so the human /
90
+ * no-delegation hot path never pays the early policy fetch). Shared by the Node
91
+ * and Edge middleware entries.
92
+ */
67
93
  async function resolveTrustedDelegationRootsForRequest(resolver, headers) {
68
- if (!resolver) return void 0;
69
- if (!checkpointShared.requestCarriesDelegationProof(headers)) return void 0;
70
- const roots = await resolver();
71
- return roots.length > 0 ? roots : void 0;
94
+ if (!resolver) return void 0;
95
+ if (!(0, _kya_os_checkpoint_shared.requestCarriesDelegationProof)(headers)) return void 0;
96
+ const roots = await resolver();
97
+ return roots.length > 0 ? roots : void 0;
72
98
  }
99
+ /**
100
+ * The per-request seam shared by both middleware entries. Evaluates the composed
101
+ * policy and swaps `result.decision` IN PLACE when the engine acts — so the
102
+ * swap happens BEFORE the detection reporter fires and before
103
+ * `renderDecisionAsResponse`, exactly like the Express runtime (the reporter
104
+ * records the enforced decision). Fail-OPEN: any fault keeps the structured
105
+ * decision; a policy fault never breaks the response.
106
+ */
73
107
  async function applyComposedPolicy(context, result, path) {
74
- if (!context) return;
75
- try {
76
- const outcome = await context.apply(result, path);
77
- if (outcome.acted) {
78
- result.decision = outcome.decision;
79
- if (outcome.composedBlobHash) {
80
- result.composedBlobHash = outcome.composedBlobHash;
81
- }
82
- }
83
- } catch {
84
- }
108
+ if (!context) return;
109
+ try {
110
+ const outcome = await context.apply(result, path);
111
+ if (outcome.acted) {
112
+ result.decision = outcome.decision;
113
+ if (outcome.composedBlobHash) result.composedBlobHash = outcome.composedBlobHash;
114
+ }
115
+ } catch {}
85
116
  }
86
- var consoleComposedPolicyLogger = {
87
- shadowDivergence(info) {
88
- console.warn("[checkpoint/composed-policy] shadow-divergence", info);
89
- },
90
- evaluationError(projectId, error) {
91
- console.error(
92
- `[checkpoint/composed-policy] evaluation failed for ${projectId}; using structured decision:`,
93
- error
94
- );
95
- }
117
+ /**
118
+ * Debug logger mirroring the worker's `console.warn`/`console.error` shadow +
119
+ * fail-open telemetry. Wired by `withCheckpoint` only when `config.debug` is
120
+ * set, so the default SDK posture stays silent.
121
+ */
122
+ const consoleComposedPolicyLogger = {
123
+ shadowDivergence(info) {
124
+ console.warn("[checkpoint/composed-policy] shadow-divergence", info);
125
+ },
126
+ evaluationError(projectId, error) {
127
+ console.error(`[checkpoint/composed-policy] evaluation failed for ${projectId}; using structured decision:`, error);
128
+ }
96
129
  };
97
130
 
131
+ //#endregion
98
132
  exports.DEFAULT_DASHBOARD_URL = DEFAULT_DASHBOARD_URL;
99
133
  exports.applyComposedPolicy = applyComposedPolicy;
100
134
  exports.consoleComposedPolicyLogger = consoleComposedPolicyLogger;
101
135
  exports.makeComposedPolicyContext = makeComposedPolicyContext;
102
- exports.resolveTrustedDelegationRootsForRequest = resolveTrustedDelegationRootsForRequest;
136
+ exports.resolveTrustedDelegationRootsForRequest = resolveTrustedDelegationRootsForRequest;