@kya-os/checkpoint-nextjs 1.9.0 → 1.11.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 +322 -0
  2. package/README.md +205 -31
  3. package/dist/adapt-BdQ9R7Iw.mjs +97 -0
  4. package/dist/adapt-BdQ9R7Iw.mjs.map +1 -0
  5. package/dist/adapt-CCSKGjVH.js +107 -0
  6. package/dist/api-client-JHOrRUA_.d.mts +222 -0
  7. package/dist/api-client-JHOrRUA_.d.mts.map +1 -0
  8. package/dist/api-client-JHOrRUA_.d.ts +222 -0
  9. package/dist/api-client-JHOrRUA_.d.ts.map +1 -0
  10. package/dist/api-client.d.mts +2 -213
  11. package/dist/api-client.d.ts +2 -213
  12. package/dist/api-client.js +221 -206
  13. package/dist/api-client.mjs +222 -202
  14. package/dist/api-client.mjs.map +1 -0
  15. package/dist/api-middleware-BWjokNis.d.mts +131 -0
  16. package/dist/api-middleware-BWjokNis.d.mts.map +1 -0
  17. package/dist/api-middleware-Bf3xmM9W.js +391 -0
  18. package/dist/api-middleware-C-ngFX_8.mjs +387 -0
  19. package/dist/api-middleware-C-ngFX_8.mjs.map +1 -0
  20. package/dist/api-middleware-DyLDqOYF.d.ts +131 -0
  21. package/dist/api-middleware-DyLDqOYF.d.ts.map +1 -0
  22. package/dist/api-middleware.d.mts +2 -166
  23. package/dist/api-middleware.d.ts +2 -166
  24. package/dist/api-middleware.js +3 -526
  25. package/dist/api-middleware.mjs +2 -521
  26. package/dist/cedar-wasm.d.mts +20 -0
  27. package/dist/cedar-wasm.d.mts.map +1 -0
  28. package/dist/cedar-wasm.d.ts +19 -0
  29. package/dist/cedar-wasm.d.ts.map +1 -0
  30. package/dist/cedar-wasm.js +24 -0
  31. package/dist/cedar-wasm.mjs +23 -0
  32. package/dist/cedar-wasm.mjs.map +1 -0
  33. package/dist/config-CPP1RV0f.d.mts +291 -0
  34. package/dist/config-CPP1RV0f.d.mts.map +1 -0
  35. package/dist/config-DVgOFxJs.d.ts +291 -0
  36. package/dist/config-DVgOFxJs.d.ts.map +1 -0
  37. package/dist/engine-wasm.d.mts +20 -0
  38. package/dist/engine-wasm.d.mts.map +1 -0
  39. package/dist/engine-wasm.d.ts +19 -0
  40. package/dist/engine-wasm.d.ts.map +1 -0
  41. package/dist/engine-wasm.js +24 -0
  42. package/dist/engine-wasm.mjs +23 -0
  43. package/dist/engine-wasm.mjs.map +1 -0
  44. package/dist/index-edge.d.mts +8 -0
  45. package/dist/index-edge.d.ts +8 -0
  46. package/dist/index-edge.js +14 -0
  47. package/dist/index-edge.mjs +7 -0
  48. package/dist/index.d.mts +8 -17
  49. package/dist/index.d.ts +8 -17
  50. package/dist/index.js +36 -1275
  51. package/dist/index.mjs +29 -1234
  52. package/dist/index.mjs.map +1 -0
  53. package/dist/middleware-bundler.d.mts +6 -12
  54. package/dist/middleware-bundler.d.mts.map +1 -0
  55. package/dist/middleware-bundler.d.ts +6 -12
  56. package/dist/middleware-bundler.d.ts.map +1 -0
  57. package/dist/middleware-bundler.js +12 -350
  58. package/dist/middleware-bundler.mjs +11 -347
  59. package/dist/middleware-bundler.mjs.map +1 -0
  60. package/dist/middleware-core-B0jf5_Hm.mjs +264 -0
  61. package/dist/middleware-core-B0jf5_Hm.mjs.map +1 -0
  62. package/dist/middleware-core-Bol44X9C.d.ts +17 -0
  63. package/dist/middleware-core-Bol44X9C.d.ts.map +1 -0
  64. package/dist/middleware-core-CMc-2QkU.js +274 -0
  65. package/dist/middleware-core-DycsbHUd.d.mts +17 -0
  66. package/dist/middleware-core-DycsbHUd.d.mts.map +1 -0
  67. package/dist/middleware-edge.d.mts +16 -14
  68. package/dist/middleware-edge.d.mts.map +1 -0
  69. package/dist/middleware-edge.d.ts +16 -14
  70. package/dist/middleware-edge.d.ts.map +1 -0
  71. package/dist/middleware-edge.js +94 -355
  72. package/dist/middleware-edge.mjs +90 -347
  73. package/dist/middleware-edge.mjs.map +1 -0
  74. package/dist/middleware-node-context-591KsiYg.js +39 -0
  75. package/dist/middleware-node-context-D_ZyUEcA.mjs +35 -0
  76. package/dist/middleware-node-context-D_ZyUEcA.mjs.map +1 -0
  77. package/dist/middleware-node.d.mts +10 -25
  78. package/dist/middleware-node.d.mts.map +1 -0
  79. package/dist/middleware-node.d.ts +10 -25
  80. package/dist/middleware-node.d.ts.map +1 -0
  81. package/dist/middleware-node.js +21 -351
  82. package/dist/middleware-node.mjs +19 -347
  83. package/dist/middleware-node.mjs.map +1 -0
  84. package/dist/posture-outcome-Ccujx_9V.d.mts +14 -0
  85. package/dist/posture-outcome-Ccujx_9V.d.mts.map +1 -0
  86. package/dist/posture-outcome-Ccujx_9V.d.ts +14 -0
  87. package/dist/posture-outcome-Ccujx_9V.d.ts.map +1 -0
  88. package/dist/posture-outcome-DQ4fdMfq.mjs +21 -0
  89. package/dist/posture-outcome-DQ4fdMfq.mjs.map +1 -0
  90. package/dist/posture-outcome-DddsJtL8.js +25 -0
  91. package/dist/public-api-CYsHrTQI.d.ts +5 -0
  92. package/dist/public-api-DkG7pRE0.d.mts +5 -0
  93. package/dist/rolldown-runtime-C24Yb2bl.js +33 -0
  94. package/dist/utils-Cnwg0VrY.mjs +88 -0
  95. package/dist/utils-Cnwg0VrY.mjs.map +1 -0
  96. package/dist/utils-dyFxqlFI.js +104 -0
  97. package/package.json +31 -66
  98. package/EDGE_RUNTIME_WASM_SETUP.md +0 -342
  99. package/bin/setup-edge-wasm.js +0 -513
  100. package/dist/adapt.d.mts +0 -39
  101. package/dist/adapt.d.ts +0 -39
  102. package/dist/adapt.js +0 -59
  103. package/dist/adapt.mjs +0 -57
  104. package/dist/composed-policy.d.mts +0 -115
  105. package/dist/composed-policy.d.ts +0 -115
  106. package/dist/composed-policy.js +0 -103
  107. package/dist/composed-policy.mjs +0 -97
  108. package/dist/config-CleTeeoW.d.mts +0 -233
  109. package/dist/config-XMOIkxFN.d.ts +0 -233
  110. package/dist/create-middleware.d.mts +0 -22
  111. package/dist/create-middleware.d.ts +0 -22
  112. package/dist/create-middleware.js +0 -34
  113. package/dist/create-middleware.mjs +0 -31
  114. package/dist/edge/index.d.mts +0 -112
  115. package/dist/edge/index.d.ts +0 -112
  116. package/dist/edge/index.js +0 -277
  117. package/dist/edge/index.mjs +0 -275
  118. package/dist/edge-runtime-loader.d.mts +0 -77
  119. package/dist/edge-runtime-loader.d.ts +0 -77
  120. package/dist/edge-runtime-loader.js +0 -242
  121. package/dist/edge-runtime-loader.mjs +0 -235
  122. package/dist/edge-wasm-middleware.d.mts +0 -62
  123. package/dist/edge-wasm-middleware.d.ts +0 -62
  124. package/dist/edge-wasm-middleware.js +0 -26
  125. package/dist/edge-wasm-middleware.mjs +0 -22
  126. package/dist/middleware.d.mts +0 -45
  127. package/dist/middleware.d.ts +0 -45
  128. package/dist/middleware.js +0 -19
  129. package/dist/middleware.mjs +0 -15
  130. package/dist/nodejs-wasm-loader.d.mts +0 -41
  131. package/dist/nodejs-wasm-loader.d.ts +0 -41
  132. package/dist/nodejs-wasm-loader.js +0 -36
  133. package/dist/nodejs-wasm-loader.mjs +0 -30
  134. package/dist/policy.d.mts +0 -162
  135. package/dist/policy.d.ts +0 -162
  136. package/dist/policy.js +0 -198
  137. package/dist/policy.mjs +0 -174
  138. package/dist/session-tracker.d.mts +0 -55
  139. package/dist/session-tracker.d.ts +0 -55
  140. package/dist/session-tracker.js +0 -172
  141. package/dist/session-tracker.mjs +0 -169
  142. package/dist/translate.d.mts +0 -63
  143. package/dist/translate.d.ts +0 -63
  144. package/dist/translate.js +0 -44
  145. package/dist/translate.mjs +0 -42
  146. package/dist/types-Chgi5TFC.d.mts +0 -105
  147. package/dist/types-Chgi5TFC.d.ts +0 -105
  148. package/dist/wasm-setup.d.mts +0 -46
  149. package/dist/wasm-setup.d.ts +0 -46
  150. package/dist/wasm-setup.js +0 -122
  151. package/dist/wasm-setup.mjs +0 -118
  152. package/wasm/agentshield_wasm.d.ts +0 -479
  153. package/wasm/agentshield_wasm.js +0 -1536
  154. package/wasm/agentshield_wasm_bg.wasm +0 -0
  155. package/wasm/package.json +0 -30
  156. package/wasm.d.ts +0 -21
@@ -0,0 +1,97 @@
1
+ import { BLOCKED_PATH, VERDICT_COOKIE_NAME, acceptsHtml, classifyResponseShape, encodeVerdictCookie } from "@kya-os/checkpoint-shared/host-adapter";
2
+ import { NextResponse } from "next/server.js";
3
+ import { FAIL_CLOSED_BODY, FAIL_CLOSED_HEADERS, FAIL_CLOSED_STATUS } from "@kya-os/checkpoint-shared/policy/enforcement";
4
+
5
+ //#region src/adapt.ts
6
+ /**
7
+ * D.3 — `RenderedResponse` → `NextResponse` adapter.
8
+ *
9
+ * The host wrapper's *only* job on the outbound path: take the
10
+ * transport-agnostic `RenderedResponse` Phase C's
11
+ * `renderDecisionAsResponse` produces and translate it to a
12
+ * `NextResponse`. Zero verdict decisions, zero engine I/O.
13
+ *
14
+ * Shared between the Node-runtime and Edge-runtime entries. The
15
+ * branching here is identical in both — Next.js `NextResponse` has the
16
+ * same API surface across runtimes; only the underlying response
17
+ * primitive differs (Node http.ServerResponse vs Edge `Response`).
18
+ *
19
+ * Architectural pins per architect § 4.3 / § 4.4:
20
+ *
21
+ * 1. **Verdict-cookie format is contract.** Sites-1's Sonner toast
22
+ * depends on `__checkpoint_verdict=%7B%22verdict%22%3A%22<v>%22...
23
+ * %7D` (single URL-encoded JSON). Byte-format pinned by adapt.test.
24
+ *
25
+ * 2. **HTML-accepting clients → `/blocked` rewrite at status 200**
26
+ * (so the page renders with the verdict cookie set; Sonner picks
27
+ * up the cookie and shows the toast). Non-HTML clients → JSON 4xx.
28
+ *
29
+ * 3. **`X-Checkpoint-Engine` carries `result.engineInfo.name`** —
30
+ * `checkpoint-engine-wasm` after Phase D ships. Brian's Sites-2
31
+ * deviation note confirmed the `X-Checkpoint-*` prefix is canon.
32
+ */
33
+ /**
34
+ * 503 (Retry-After, no-store) for an engine/pipeline fault. Never a 500 and
35
+ * never a pass-through: an unverifiable request is not allowed.
36
+ */
37
+ function failClosedResponse() {
38
+ return NextResponse.json(FAIL_CLOSED_BODY, {
39
+ status: FAIL_CLOSED_STATUS,
40
+ headers: FAIL_CLOSED_HEADERS
41
+ });
42
+ }
43
+ /**
44
+ * Convert the engine's transport-agnostic `RenderedResponse` into a
45
+ * `NextResponse`. Sites-1's Playwright suite is the regression gate;
46
+ * any drift here is caught downstream.
47
+ */
48
+ function adaptToNextResponse(rendered, req) {
49
+ const clientAcceptsHtml = acceptsHtml(req.headers);
50
+ const verdictCookie = encodeVerdictCookie(rendered);
51
+ switch (classifyResponseShape(rendered, clientAcceptsHtml)) {
52
+ case "pass-through": {
53
+ const res = NextResponse.next();
54
+ applyHeaders(res, rendered);
55
+ setVerdictCookie(res, verdictCookie);
56
+ return res;
57
+ }
58
+ case "redirect": {
59
+ const target = new URL(rendered.headers.Location);
60
+ const res = NextResponse.redirect(target);
61
+ applyHeaders(res, rendered);
62
+ setVerdictCookie(res, verdictCookie);
63
+ return res;
64
+ }
65
+ case "html-block": {
66
+ const blockedUrl = new URL(BLOCKED_PATH, req.url);
67
+ const res = NextResponse.rewrite(blockedUrl, { status: 200 });
68
+ applyHeaders(res, rendered);
69
+ setVerdictCookie(res, verdictCookie);
70
+ return res;
71
+ }
72
+ case "json-block": {
73
+ const body = rendered.body ?? {};
74
+ const res = NextResponse.json(body, { status: rendered.status });
75
+ applyHeaders(res, rendered);
76
+ setVerdictCookie(res, verdictCookie);
77
+ return res;
78
+ }
79
+ }
80
+ }
81
+ function setVerdictCookie(res, value) {
82
+ res.cookies.set({
83
+ name: VERDICT_COOKIE_NAME,
84
+ value,
85
+ path: "/",
86
+ sameSite: "lax",
87
+ httpOnly: false
88
+ });
89
+ }
90
+ function applyHeaders(res, rendered) {
91
+ for (const [key, value] of Object.entries(rendered.headers)) res.headers.set(key, value);
92
+ for (const [key, value] of rendered.headerEntries ?? []) res.headers.append(key, value);
93
+ }
94
+
95
+ //#endregion
96
+ export { failClosedResponse as n, adaptToNextResponse as t };
97
+ //# sourceMappingURL=adapt-BdQ9R7Iw.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"adapt-BdQ9R7Iw.mjs","names":[],"sources":["../src/adapt.ts"],"sourcesContent":["/**\n * D.3 — `RenderedResponse` → `NextResponse` adapter.\n *\n * The host wrapper's *only* job on the outbound path: take the\n * transport-agnostic `RenderedResponse` Phase C's\n * `renderDecisionAsResponse` produces and translate it to a\n * `NextResponse`. Zero verdict decisions, zero engine I/O.\n *\n * Shared between the Node-runtime and Edge-runtime entries. The\n * branching here is identical in both — Next.js `NextResponse` has the\n * same API surface across runtimes; only the underlying response\n * primitive differs (Node http.ServerResponse vs Edge `Response`).\n *\n * Architectural pins per architect § 4.3 / § 4.4:\n *\n * 1. **Verdict-cookie format is contract.** Sites-1's Sonner toast\n * depends on `__checkpoint_verdict=%7B%22verdict%22%3A%22<v>%22...\n * %7D` (single URL-encoded JSON). Byte-format pinned by adapt.test.\n *\n * 2. **HTML-accepting clients → `/blocked` rewrite at status 200**\n * (so the page renders with the verdict cookie set; Sonner picks\n * up the cookie and shows the toast). Non-HTML clients → JSON 4xx.\n *\n * 3. **`X-Checkpoint-Engine` carries `result.engineInfo.name`** —\n * `checkpoint-engine-wasm` after Phase D ships. Brian's Sites-2\n * deviation note confirmed the `X-Checkpoint-*` prefix is canon.\n */\n\nimport { type NextRequest, NextResponse } from 'next/server.js';\n\nimport type { RenderedResponse } from '@kya-os/checkpoint-wasm-runtime/orchestrator';\nimport {\n VERDICT_COOKIE_NAME,\n BLOCKED_PATH,\n encodeVerdictCookie,\n acceptsHtml,\n classifyResponseShape,\n} from '@kya-os/checkpoint-shared/host-adapter';\nimport {\n FAIL_CLOSED_BODY,\n FAIL_CLOSED_HEADERS,\n FAIL_CLOSED_STATUS,\n} from '@kya-os/checkpoint-shared/policy/enforcement';\n\n/**\n * 503 (Retry-After, no-store) for an engine/pipeline fault. Never a 500 and\n * never a pass-through: an unverifiable request is not allowed.\n */\nexport function failClosedResponse(): NextResponse {\n return NextResponse.json(FAIL_CLOSED_BODY, {\n status: FAIL_CLOSED_STATUS,\n headers: FAIL_CLOSED_HEADERS,\n });\n}\n\n/**\n * Convert the engine's transport-agnostic `RenderedResponse` into a\n * `NextResponse`. Sites-1's Playwright suite is the regression gate;\n * any drift here is caught downstream.\n */\nexport function adaptToNextResponse(rendered: RenderedResponse, req: NextRequest): NextResponse {\n const clientAcceptsHtml = acceptsHtml(req.headers);\n const verdictCookie = encodeVerdictCookie(rendered);\n const shape = classifyResponseShape(rendered, clientAcceptsHtml);\n\n switch (shape) {\n case 'pass-through': {\n // Permit OR Observe-mode any-verdict.\n const res = NextResponse.next();\n applyHeaders(res, rendered);\n setVerdictCookie(res, verdictCookie);\n return res;\n }\n\n case 'redirect': {\n // Decision::Redirect → 302 + Location.\n const target = new URL(rendered.headers.Location!);\n const res = NextResponse.redirect(target);\n applyHeaders(res, rendered);\n setVerdictCookie(res, verdictCookie);\n return res;\n }\n\n case 'html-block': {\n // Sites-1 contract: HTML clients (browsers) need a renderable page\n // to show the rejection UI. The verdict cookie carries the reason;\n // the /blocked route reads it and renders the toast.\n const blockedUrl = new URL(BLOCKED_PATH, req.url);\n const res = NextResponse.rewrite(blockedUrl, { status: 200 });\n applyHeaders(res, rendered);\n setVerdictCookie(res, verdictCookie);\n return res;\n }\n\n case 'json-block': {\n // The orchestrator's RenderedResponse already supplies the correct\n // status (401/403/422/...); we just need to materialise the body.\n // application/problem+json (Instruct) uses the Content-Type from\n // rendered.headers; defaults to application/json for everything else.\n const body = rendered.body ?? {};\n const res = NextResponse.json(body, { status: rendered.status as number });\n applyHeaders(res, rendered);\n setVerdictCookie(res, verdictCookie);\n return res;\n }\n }\n}\n\n// -----------------------------------------------------------------------------\n// Helpers — Next.js-specific glue. The framework-agnostic primitives\n// (encodeVerdictCookie, acceptsHtml, classifyResponseShape,\n// VERDICT_COOKIE_NAME, BLOCKED_PATH) live in `@kya-os/checkpoint-shared`\n// so checkpoint-express + future host wrappers produce byte-identical\n// cookies and route HTML/JSON branching the same way.\n// -----------------------------------------------------------------------------\n\nfunction setVerdictCookie(res: NextResponse, value: string): void {\n // Path / SameSite / HttpOnly chosen for the Sonner-bridge use case:\n // path=/ so any route can read it, SameSite=Lax so first-party\n // navigations carry it, HttpOnly=false so the client-side toast JS\n // can read it (it's verdict UX, not a session token).\n res.cookies.set({\n name: VERDICT_COOKIE_NAME,\n value,\n path: '/',\n sameSite: 'lax',\n httpOnly: false,\n });\n}\n\nfunction applyHeaders(res: NextResponse, rendered: RenderedResponse): void {\n // NextResponse.next() / rewrite() / json() return responses with\n // some default headers; orchestrator headers (X-Checkpoint-*, Location)\n // override. We don't strip pre-existing headers — only set new ones.\n for (const [key, value] of Object.entries(rendered.headers)) {\n res.headers.set(key, value);\n }\n for (const [key, value] of rendered.headerEntries ?? []) {\n res.headers.append(key, value);\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAgDA,SAAgB,qBAAmC;CACjD,OAAO,aAAa,KAAK,kBAAkB;EACzC,QAAQ;EACR,SAAS;CACX,CAAC;AACH;;;;;;AAOA,SAAgB,oBAAoB,UAA4B,KAAgC;CAC9F,MAAM,oBAAoB,YAAY,IAAI,OAAO;CACjD,MAAM,gBAAgB,oBAAoB,QAAQ;CAGlD,QAFc,sBAAsB,UAAU,iBAElC,GAAZ;EACE,KAAK,gBAAgB;GAEnB,MAAM,MAAM,aAAa,KAAK;GAC9B,aAAa,KAAK,QAAQ;GAC1B,iBAAiB,KAAK,aAAa;GACnC,OAAO;EACT;EAEA,KAAK,YAAY;GAEf,MAAM,SAAS,IAAI,IAAI,SAAS,QAAQ,QAAS;GACjD,MAAM,MAAM,aAAa,SAAS,MAAM;GACxC,aAAa,KAAK,QAAQ;GAC1B,iBAAiB,KAAK,aAAa;GACnC,OAAO;EACT;EAEA,KAAK,cAAc;GAIjB,MAAM,aAAa,IAAI,IAAI,cAAc,IAAI,GAAG;GAChD,MAAM,MAAM,aAAa,QAAQ,YAAY,EAAE,QAAQ,IAAI,CAAC;GAC5D,aAAa,KAAK,QAAQ;GAC1B,iBAAiB,KAAK,aAAa;GACnC,OAAO;EACT;EAEA,KAAK,cAAc;GAKjB,MAAM,OAAO,SAAS,QAAQ,CAAC;GAC/B,MAAM,MAAM,aAAa,KAAK,MAAM,EAAE,QAAQ,SAAS,OAAiB,CAAC;GACzE,aAAa,KAAK,QAAQ;GAC1B,iBAAiB,KAAK,aAAa;GACnC,OAAO;EACT;CACF;AACF;AAUA,SAAS,iBAAiB,KAAmB,OAAqB;CAKhE,IAAI,QAAQ,IAAI;EACd,MAAM;EACN;EACA,MAAM;EACN,UAAU;EACV,UAAU;CACZ,CAAC;AACH;AAEA,SAAS,aAAa,KAAmB,UAAkC;CAIzE,KAAK,MAAM,CAAC,KAAK,UAAU,OAAO,QAAQ,SAAS,OAAO,GACxD,IAAI,QAAQ,IAAI,KAAK,KAAK;CAE5B,KAAK,MAAM,CAAC,KAAK,UAAU,SAAS,iBAAiB,CAAC,GACpD,IAAI,QAAQ,OAAO,KAAK,KAAK;AAEjC"}
@@ -0,0 +1,107 @@
1
+ let _kya_os_checkpoint_shared_host_adapter = require("@kya-os/checkpoint-shared/host-adapter");
2
+ let next_server_js = require("next/server.js");
3
+ let _kya_os_checkpoint_shared_policy_enforcement = require("@kya-os/checkpoint-shared/policy/enforcement");
4
+
5
+ //#region src/adapt.ts
6
+ /**
7
+ * D.3 — `RenderedResponse` → `NextResponse` adapter.
8
+ *
9
+ * The host wrapper's *only* job on the outbound path: take the
10
+ * transport-agnostic `RenderedResponse` Phase C's
11
+ * `renderDecisionAsResponse` produces and translate it to a
12
+ * `NextResponse`. Zero verdict decisions, zero engine I/O.
13
+ *
14
+ * Shared between the Node-runtime and Edge-runtime entries. The
15
+ * branching here is identical in both — Next.js `NextResponse` has the
16
+ * same API surface across runtimes; only the underlying response
17
+ * primitive differs (Node http.ServerResponse vs Edge `Response`).
18
+ *
19
+ * Architectural pins per architect § 4.3 / § 4.4:
20
+ *
21
+ * 1. **Verdict-cookie format is contract.** Sites-1's Sonner toast
22
+ * depends on `__checkpoint_verdict=%7B%22verdict%22%3A%22<v>%22...
23
+ * %7D` (single URL-encoded JSON). Byte-format pinned by adapt.test.
24
+ *
25
+ * 2. **HTML-accepting clients → `/blocked` rewrite at status 200**
26
+ * (so the page renders with the verdict cookie set; Sonner picks
27
+ * up the cookie and shows the toast). Non-HTML clients → JSON 4xx.
28
+ *
29
+ * 3. **`X-Checkpoint-Engine` carries `result.engineInfo.name`** —
30
+ * `checkpoint-engine-wasm` after Phase D ships. Brian's Sites-2
31
+ * deviation note confirmed the `X-Checkpoint-*` prefix is canon.
32
+ */
33
+ /**
34
+ * 503 (Retry-After, no-store) for an engine/pipeline fault. Never a 500 and
35
+ * never a pass-through: an unverifiable request is not allowed.
36
+ */
37
+ function failClosedResponse() {
38
+ return next_server_js.NextResponse.json(_kya_os_checkpoint_shared_policy_enforcement.FAIL_CLOSED_BODY, {
39
+ status: _kya_os_checkpoint_shared_policy_enforcement.FAIL_CLOSED_STATUS,
40
+ headers: _kya_os_checkpoint_shared_policy_enforcement.FAIL_CLOSED_HEADERS
41
+ });
42
+ }
43
+ /**
44
+ * Convert the engine's transport-agnostic `RenderedResponse` into a
45
+ * `NextResponse`. Sites-1's Playwright suite is the regression gate;
46
+ * any drift here is caught downstream.
47
+ */
48
+ function adaptToNextResponse(rendered, req) {
49
+ const clientAcceptsHtml = (0, _kya_os_checkpoint_shared_host_adapter.acceptsHtml)(req.headers);
50
+ const verdictCookie = (0, _kya_os_checkpoint_shared_host_adapter.encodeVerdictCookie)(rendered);
51
+ switch ((0, _kya_os_checkpoint_shared_host_adapter.classifyResponseShape)(rendered, clientAcceptsHtml)) {
52
+ case "pass-through": {
53
+ const res = next_server_js.NextResponse.next();
54
+ applyHeaders(res, rendered);
55
+ setVerdictCookie(res, verdictCookie);
56
+ return res;
57
+ }
58
+ case "redirect": {
59
+ const target = new URL(rendered.headers.Location);
60
+ const res = next_server_js.NextResponse.redirect(target);
61
+ applyHeaders(res, rendered);
62
+ setVerdictCookie(res, verdictCookie);
63
+ return res;
64
+ }
65
+ case "html-block": {
66
+ const blockedUrl = new URL(_kya_os_checkpoint_shared_host_adapter.BLOCKED_PATH, req.url);
67
+ const res = next_server_js.NextResponse.rewrite(blockedUrl, { status: 200 });
68
+ applyHeaders(res, rendered);
69
+ setVerdictCookie(res, verdictCookie);
70
+ return res;
71
+ }
72
+ case "json-block": {
73
+ const body = rendered.body ?? {};
74
+ const res = next_server_js.NextResponse.json(body, { status: rendered.status });
75
+ applyHeaders(res, rendered);
76
+ setVerdictCookie(res, verdictCookie);
77
+ return res;
78
+ }
79
+ }
80
+ }
81
+ function setVerdictCookie(res, value) {
82
+ res.cookies.set({
83
+ name: _kya_os_checkpoint_shared_host_adapter.VERDICT_COOKIE_NAME,
84
+ value,
85
+ path: "/",
86
+ sameSite: "lax",
87
+ httpOnly: false
88
+ });
89
+ }
90
+ function applyHeaders(res, rendered) {
91
+ for (const [key, value] of Object.entries(rendered.headers)) res.headers.set(key, value);
92
+ for (const [key, value] of rendered.headerEntries ?? []) res.headers.append(key, value);
93
+ }
94
+
95
+ //#endregion
96
+ Object.defineProperty(exports, 'adaptToNextResponse', {
97
+ enumerable: true,
98
+ get: function () {
99
+ return adaptToNextResponse;
100
+ }
101
+ });
102
+ Object.defineProperty(exports, 'failClosedResponse', {
103
+ enumerable: true,
104
+ get: function () {
105
+ return failClosedResponse;
106
+ }
107
+ });
@@ -0,0 +1,222 @@
1
+ import { DetectionDetail, EnforcementAction, EngineInfo } from "@kya-os/checkpoint-shared";
2
+ //#region src/api-client.d.ts
3
+ /**
4
+ * API client configuration
5
+ */
6
+ interface CheckpointApiClientConfig {
7
+ /** API key for authentication */
8
+ apiKey: string;
9
+ /** API base URL (defaults to production) */
10
+ baseUrl?: string;
11
+ /**
12
+ * Use edge detection for lower latency (~30-50ms vs ~150ms) and better coverage.
13
+ * Edge detection can identify non-JS clients (curl, Python, Claude Code WebFetch)
14
+ * that the pixel cannot detect since they don't execute JavaScript.
15
+ * @default true
16
+ */
17
+ useEdge?: boolean;
18
+ /** Request timeout in milliseconds (default: 5000) */
19
+ timeout?: number;
20
+ /** Enable debug logging */
21
+ debug?: boolean;
22
+ }
23
+ /**
24
+ * Enforcement decision from the API
25
+ */
26
+ interface EnforcementDecision {
27
+ action: EnforcementAction;
28
+ reason: string;
29
+ isAgent: boolean;
30
+ confidence: number;
31
+ agentName?: string;
32
+ agentType?: string;
33
+ redirectUrl?: string;
34
+ message?: string;
35
+ metadata?: {
36
+ policyVersion?: string;
37
+ signatureVerified?: boolean;
38
+ denyListMatch?: {
39
+ clientDid?: string;
40
+ agentDid?: string;
41
+ clientName?: string;
42
+ reason?: string;
43
+ };
44
+ rateLimit?: {
45
+ limit?: number;
46
+ retryAfterSeconds?: number;
47
+ wouldBlock?: boolean;
48
+ skipped?: 'no_client_ip';
49
+ };
50
+ };
51
+ }
52
+ /**
53
+ * Detection detail (optional in response)
54
+ */
55
+ type ApiDetectionDetail = Omit<DetectionDetail, 'detectionClass' | 'verificationMethod' | 'reasons' | 'signals' | 'timestamp'> & {
56
+ agentName?: string;
57
+ /** Detection class: 'human', 'ai_agent', 'bot', 'incomplete_data' */
58
+ detectionClass?: string;
59
+ verificationMethod?: string;
60
+ reasons?: string[];
61
+ signals?: DetectionDetail['signals'];
62
+ timestamp?: DetectionDetail['timestamp'];
63
+ /** Detection engine used: 'wasm' or 'javascript-fallback' */
64
+ detectionMethod?: string;
65
+ detectionBasis?: string;
66
+ /**
67
+ * Ordered `detection/*` reason codes (KYA-1107 signal exposure; carried
68
+ * on every enforce detection payload alongside the populated `signals`
69
+ * list). Deliberately `string[]` rather than
70
+ * the registry union so an older SDK build tolerates codes added
71
+ * server-side later; the registry lives in
72
+ * `@kya-os/checkpoint-shared` (`DETECTION_REASON_CODES`).
73
+ */
74
+ reasonCodes?: string[];
75
+ };
76
+ /**
77
+ * Enforce API response
78
+ */
79
+ interface EnforceResponse {
80
+ success: boolean;
81
+ data?: {
82
+ decision: EnforcementDecision;
83
+ processingTimeMs: number;
84
+ requestId: string;
85
+ detection?: ApiDetectionDetail;
86
+ /**
87
+ * Self-identification of the engine that ran the detection (the gateway
88
+ * worker's `checkpoint-worker-wasm`). Absent when the gateway could not
89
+ * compute it.
90
+ */
91
+ engine?: EngineInfo;
92
+ };
93
+ error?: {
94
+ code: string;
95
+ message: string;
96
+ };
97
+ }
98
+ /**
99
+ * Request input for enforce API
100
+ */
101
+ interface EnforceInput {
102
+ /** HTTP headers from the incoming request */
103
+ headers?: Record<string, string>;
104
+ /** User-Agent header */
105
+ userAgent?: string;
106
+ /** Client IP address */
107
+ ipAddress?: string;
108
+ /** Request path */
109
+ path?: string;
110
+ /** Request URL */
111
+ url?: string;
112
+ /** HTTP method */
113
+ method?: string;
114
+ /** Request ID for tracing */
115
+ requestId?: string;
116
+ /** Options */
117
+ options?: {
118
+ /** Include full detection result */
119
+ includeDetectionResult?: boolean;
120
+ /** Cache TTL override */
121
+ cacheTTL?: number;
122
+ };
123
+ }
124
+ /**
125
+ * Input for logging a detection result
126
+ */
127
+ interface LogDetectionInput {
128
+ /** Detection result from Gateway */
129
+ detection: ApiDetectionDetail;
130
+ /** Request context */
131
+ context: {
132
+ userAgent?: string;
133
+ ipAddress?: string;
134
+ path?: string;
135
+ url?: string;
136
+ method?: string;
137
+ };
138
+ /** Source of the detection */
139
+ source?: 'gateway' | 'middleware';
140
+ /** The enforcement decision the caller received and acted on. */
141
+ enforcement?: {
142
+ action: EnforcementAction;
143
+ };
144
+ /** Self-identification of the engine that produced the detection. */
145
+ engine?: EngineInfo;
146
+ /** Self-identification of the reporting SDK: package, version and Next.js runtime. */
147
+ sdk?: {
148
+ name: string;
149
+ version: string;
150
+ runtime: 'node' | 'edge';
151
+ };
152
+ }
153
+ /**
154
+ * Checkpoint API Client
155
+ *
156
+ * @example
157
+ * ```typescript
158
+ * const client = new CheckpointApiClient({
159
+ * apiKey: process.env.CHECKPOINT_API_KEY!,
160
+ * });
161
+ *
162
+ * const result = await client.enforce({
163
+ * headers: Object.fromEntries(request.headers),
164
+ * path: request.nextUrl.pathname,
165
+ * method: request.method,
166
+ * });
167
+ *
168
+ * if (result.decision.action === 'block') {
169
+ * return new Response('Access denied', { status: 403 });
170
+ * }
171
+ * ```
172
+ */
173
+ declare class CheckpointApiClient {
174
+ private apiKey;
175
+ private baseUrl;
176
+ private useEdge;
177
+ private timeout;
178
+ private debug;
179
+ constructor(config: CheckpointApiClientConfig);
180
+ /**
181
+ * Call the enforce API to check if a request should be allowed
182
+ */
183
+ enforce(input: EnforceInput): Promise<EnforceResponse>;
184
+ /**
185
+ * Quick check - returns just the action without full response parsing
186
+ * Useful for very fast middleware that just needs allow/block. On an API
187
+ * error the action is `block` (fail-closed) with `error` set.
188
+ */
189
+ quickCheck(input: EnforceInput): Promise<{
190
+ action: EnforcementAction;
191
+ error?: string;
192
+ }>;
193
+ /**
194
+ * Check if this client is using edge detection (Gateway Worker)
195
+ */
196
+ isUsingEdge(): boolean;
197
+ /**
198
+ * Log a detection result to the Checkpoint database.
199
+ * Use after Gateway Worker detection to persist results.
200
+ * Fire-and-forget - returns immediately without waiting for DB write.
201
+ *
202
+ * @example
203
+ * ```typescript
204
+ * // After receiving Gateway response
205
+ * if (client.isUsingEdge() && response.data?.detection) {
206
+ * client.logDetection({
207
+ * detection: response.data.detection,
208
+ * context: { userAgent, ipAddress, path, url, method }
209
+ * }).catch(err => console.error('Log failed:', err));
210
+ * }
211
+ * ```
212
+ */
213
+ logDetection(input: LogDetectionInput): Promise<void>;
214
+ }
215
+ declare function getCheckpointApiClient(config?: Partial<CheckpointApiClientConfig>): CheckpointApiClient;
216
+ /**
217
+ * Reset the singleton client (useful for testing)
218
+ */
219
+ declare function resetCheckpointApiClient(): void;
220
+ //#endregion
221
+ export { EnforceResponse as a, LogDetectionInput as c, EnforceInput as i, getCheckpointApiClient as l, CheckpointApiClient as n, EnforcementAction as o, CheckpointApiClientConfig as r, EnforcementDecision as s, ApiDetectionDetail as t, resetCheckpointApiClient as u };
222
+ //# sourceMappingURL=api-client-JHOrRUA_.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"api-client-JHOrRUA_.d.mts","names":[],"sources":["../src/api-client.ts"],"mappings":";;;;;UAiBiB;;EAEf;;EAEA;;;;;;;EAOA;;EAEA;;EAEA;;;;;UAgBe;EACf,QAAQ;EACR;EACA;EACA;EACA;EACA;EACA;EACA;EACA;IACE;IACA;IACA;MACE;MACA;MACA;MACA;;IAEF;MACE;MACA;MACA;MACA;;;;;;;KAQM,qBAAqB,KAC/B;EAGA;;EAEA;EACA;EACA;EACA,UAAU;EACV,YAAY;;EAEZ;EACA;;;;;;;;;EASA;;;;;UAMe;EACf;EACA;IACE,UAAU;IACV;IACA;IACA,YAAY;;;;;;IAMZ,SAAS;;EAEX;IACE;IACA;;;;;;UAOa;;EAEf,UAAU;;EAEV;;EAEA;;EAEA;;EAEA;;EAEA;;EAEA;;EAEA;;IAEE;;IAEA;;;;;;UAOa;;EAEf,WAAW;;EAEX;IACE;IACA;IACA;IACA;IACA;;;EAGF;;EAEA;IAAgB,QAAQ;;;EAExB,SAAS;;EAET;IAAQ;IAAc;IAAiB;;;;;;;;;;;;;;;;;;;;;;;cA+B5B;UACH;UACA;UACA;UACA;UACA;EAEI,YAAA,QAAQ;;;;EAgBd,QAAQ,OAAO,eAAe,QAAQ;;;;;;EA4FtC,WAAW,OAAO,eAAe;IACrC,QAAQ;IACR;;;;;EAmBF;;;;;;;;;;;;;;;;;EAoBM,aAAa,OAAO,oBAAoB;;iBA2GhC,uBACd,SAAS,QAAQ,6BAChB;;;;iBAgBa"}
@@ -0,0 +1,222 @@
1
+ import { DetectionDetail, EnforcementAction, EngineInfo } from "@kya-os/checkpoint-shared";
2
+ //#region src/api-client.d.ts
3
+ /**
4
+ * API client configuration
5
+ */
6
+ interface CheckpointApiClientConfig {
7
+ /** API key for authentication */
8
+ apiKey: string;
9
+ /** API base URL (defaults to production) */
10
+ baseUrl?: string;
11
+ /**
12
+ * Use edge detection for lower latency (~30-50ms vs ~150ms) and better coverage.
13
+ * Edge detection can identify non-JS clients (curl, Python, Claude Code WebFetch)
14
+ * that the pixel cannot detect since they don't execute JavaScript.
15
+ * @default true
16
+ */
17
+ useEdge?: boolean;
18
+ /** Request timeout in milliseconds (default: 5000) */
19
+ timeout?: number;
20
+ /** Enable debug logging */
21
+ debug?: boolean;
22
+ }
23
+ /**
24
+ * Enforcement decision from the API
25
+ */
26
+ interface EnforcementDecision {
27
+ action: EnforcementAction;
28
+ reason: string;
29
+ isAgent: boolean;
30
+ confidence: number;
31
+ agentName?: string;
32
+ agentType?: string;
33
+ redirectUrl?: string;
34
+ message?: string;
35
+ metadata?: {
36
+ policyVersion?: string;
37
+ signatureVerified?: boolean;
38
+ denyListMatch?: {
39
+ clientDid?: string;
40
+ agentDid?: string;
41
+ clientName?: string;
42
+ reason?: string;
43
+ };
44
+ rateLimit?: {
45
+ limit?: number;
46
+ retryAfterSeconds?: number;
47
+ wouldBlock?: boolean;
48
+ skipped?: 'no_client_ip';
49
+ };
50
+ };
51
+ }
52
+ /**
53
+ * Detection detail (optional in response)
54
+ */
55
+ type ApiDetectionDetail = Omit<DetectionDetail, 'detectionClass' | 'verificationMethod' | 'reasons' | 'signals' | 'timestamp'> & {
56
+ agentName?: string;
57
+ /** Detection class: 'human', 'ai_agent', 'bot', 'incomplete_data' */
58
+ detectionClass?: string;
59
+ verificationMethod?: string;
60
+ reasons?: string[];
61
+ signals?: DetectionDetail['signals'];
62
+ timestamp?: DetectionDetail['timestamp'];
63
+ /** Detection engine used: 'wasm' or 'javascript-fallback' */
64
+ detectionMethod?: string;
65
+ detectionBasis?: string;
66
+ /**
67
+ * Ordered `detection/*` reason codes (KYA-1107 signal exposure; carried
68
+ * on every enforce detection payload alongside the populated `signals`
69
+ * list). Deliberately `string[]` rather than
70
+ * the registry union so an older SDK build tolerates codes added
71
+ * server-side later; the registry lives in
72
+ * `@kya-os/checkpoint-shared` (`DETECTION_REASON_CODES`).
73
+ */
74
+ reasonCodes?: string[];
75
+ };
76
+ /**
77
+ * Enforce API response
78
+ */
79
+ interface EnforceResponse {
80
+ success: boolean;
81
+ data?: {
82
+ decision: EnforcementDecision;
83
+ processingTimeMs: number;
84
+ requestId: string;
85
+ detection?: ApiDetectionDetail;
86
+ /**
87
+ * Self-identification of the engine that ran the detection (the gateway
88
+ * worker's `checkpoint-worker-wasm`). Absent when the gateway could not
89
+ * compute it.
90
+ */
91
+ engine?: EngineInfo;
92
+ };
93
+ error?: {
94
+ code: string;
95
+ message: string;
96
+ };
97
+ }
98
+ /**
99
+ * Request input for enforce API
100
+ */
101
+ interface EnforceInput {
102
+ /** HTTP headers from the incoming request */
103
+ headers?: Record<string, string>;
104
+ /** User-Agent header */
105
+ userAgent?: string;
106
+ /** Client IP address */
107
+ ipAddress?: string;
108
+ /** Request path */
109
+ path?: string;
110
+ /** Request URL */
111
+ url?: string;
112
+ /** HTTP method */
113
+ method?: string;
114
+ /** Request ID for tracing */
115
+ requestId?: string;
116
+ /** Options */
117
+ options?: {
118
+ /** Include full detection result */
119
+ includeDetectionResult?: boolean;
120
+ /** Cache TTL override */
121
+ cacheTTL?: number;
122
+ };
123
+ }
124
+ /**
125
+ * Input for logging a detection result
126
+ */
127
+ interface LogDetectionInput {
128
+ /** Detection result from Gateway */
129
+ detection: ApiDetectionDetail;
130
+ /** Request context */
131
+ context: {
132
+ userAgent?: string;
133
+ ipAddress?: string;
134
+ path?: string;
135
+ url?: string;
136
+ method?: string;
137
+ };
138
+ /** Source of the detection */
139
+ source?: 'gateway' | 'middleware';
140
+ /** The enforcement decision the caller received and acted on. */
141
+ enforcement?: {
142
+ action: EnforcementAction;
143
+ };
144
+ /** Self-identification of the engine that produced the detection. */
145
+ engine?: EngineInfo;
146
+ /** Self-identification of the reporting SDK: package, version and Next.js runtime. */
147
+ sdk?: {
148
+ name: string;
149
+ version: string;
150
+ runtime: 'node' | 'edge';
151
+ };
152
+ }
153
+ /**
154
+ * Checkpoint API Client
155
+ *
156
+ * @example
157
+ * ```typescript
158
+ * const client = new CheckpointApiClient({
159
+ * apiKey: process.env.CHECKPOINT_API_KEY!,
160
+ * });
161
+ *
162
+ * const result = await client.enforce({
163
+ * headers: Object.fromEntries(request.headers),
164
+ * path: request.nextUrl.pathname,
165
+ * method: request.method,
166
+ * });
167
+ *
168
+ * if (result.decision.action === 'block') {
169
+ * return new Response('Access denied', { status: 403 });
170
+ * }
171
+ * ```
172
+ */
173
+ declare class CheckpointApiClient {
174
+ private apiKey;
175
+ private baseUrl;
176
+ private useEdge;
177
+ private timeout;
178
+ private debug;
179
+ constructor(config: CheckpointApiClientConfig);
180
+ /**
181
+ * Call the enforce API to check if a request should be allowed
182
+ */
183
+ enforce(input: EnforceInput): Promise<EnforceResponse>;
184
+ /**
185
+ * Quick check - returns just the action without full response parsing
186
+ * Useful for very fast middleware that just needs allow/block. On an API
187
+ * error the action is `block` (fail-closed) with `error` set.
188
+ */
189
+ quickCheck(input: EnforceInput): Promise<{
190
+ action: EnforcementAction;
191
+ error?: string;
192
+ }>;
193
+ /**
194
+ * Check if this client is using edge detection (Gateway Worker)
195
+ */
196
+ isUsingEdge(): boolean;
197
+ /**
198
+ * Log a detection result to the Checkpoint database.
199
+ * Use after Gateway Worker detection to persist results.
200
+ * Fire-and-forget - returns immediately without waiting for DB write.
201
+ *
202
+ * @example
203
+ * ```typescript
204
+ * // After receiving Gateway response
205
+ * if (client.isUsingEdge() && response.data?.detection) {
206
+ * client.logDetection({
207
+ * detection: response.data.detection,
208
+ * context: { userAgent, ipAddress, path, url, method }
209
+ * }).catch(err => console.error('Log failed:', err));
210
+ * }
211
+ * ```
212
+ */
213
+ logDetection(input: LogDetectionInput): Promise<void>;
214
+ }
215
+ declare function getCheckpointApiClient(config?: Partial<CheckpointApiClientConfig>): CheckpointApiClient;
216
+ /**
217
+ * Reset the singleton client (useful for testing)
218
+ */
219
+ declare function resetCheckpointApiClient(): void;
220
+ //#endregion
221
+ export { EnforceResponse as a, LogDetectionInput as c, EnforceInput as i, getCheckpointApiClient as l, CheckpointApiClient as n, EnforcementAction as o, CheckpointApiClientConfig as r, EnforcementDecision as s, ApiDetectionDetail as t, resetCheckpointApiClient as u };
222
+ //# sourceMappingURL=api-client-JHOrRUA_.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"api-client-JHOrRUA_.d.ts","names":[],"sources":["../src/api-client.ts"],"mappings":";;;;;UAiBiB;;EAEf;;EAEA;;;;;;;EAOA;;EAEA;;EAEA;;;;;UAgBe;EACf,QAAQ;EACR;EACA;EACA;EACA;EACA;EACA;EACA;EACA;IACE;IACA;IACA;MACE;MACA;MACA;MACA;;IAEF;MACE;MACA;MACA;MACA;;;;;;;KAQM,qBAAqB,KAC/B;EAGA;;EAEA;EACA;EACA;EACA,UAAU;EACV,YAAY;;EAEZ;EACA;;;;;;;;;EASA;;;;;UAMe;EACf;EACA;IACE,UAAU;IACV;IACA;IACA,YAAY;;;;;;IAMZ,SAAS;;EAEX;IACE;IACA;;;;;;UAOa;;EAEf,UAAU;;EAEV;;EAEA;;EAEA;;EAEA;;EAEA;;EAEA;;EAEA;;IAEE;;IAEA;;;;;;UAOa;;EAEf,WAAW;;EAEX;IACE;IACA;IACA;IACA;IACA;;;EAGF;;EAEA;IAAgB,QAAQ;;;EAExB,SAAS;;EAET;IAAQ;IAAc;IAAiB;;;;;;;;;;;;;;;;;;;;;;;cA+B5B;UACH;UACA;UACA;UACA;UACA;EAEI,YAAA,QAAQ;;;;EAgBd,QAAQ,OAAO,eAAe,QAAQ;;;;;;EA4FtC,WAAW,OAAO,eAAe;IACrC,QAAQ;IACR;;;;;EAmBF;;;;;;;;;;;;;;;;;EAoBM,aAAa,OAAO,oBAAoB;;iBA2GhC,uBACd,SAAS,QAAQ,6BAChB;;;;iBAgBa"}