@kya-os/checkpoint-nextjs 1.12.0 → 1.14.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 (75) hide show
  1. package/CHANGELOG.md +51 -0
  2. package/README.md +13 -9
  3. package/dist/{adapt-BvqYgrAk.mjs → adapt-BGEyLf44.mjs} +12 -2
  4. package/dist/adapt-BGEyLf44.mjs.map +1 -0
  5. package/dist/{adapt-DUEIfZnx.js → adapt-D2-OBY9M.js} +16 -0
  6. package/dist/{api-client-JHOrRUA_.d.ts → api-client-yjr43Itz.d.mts} +6 -17
  7. package/dist/api-client-yjr43Itz.d.mts.map +1 -0
  8. package/dist/{api-client-JHOrRUA_.d.mts → api-client-yjr43Itz.d.ts} +6 -17
  9. package/dist/api-client-yjr43Itz.d.ts.map +1 -0
  10. package/dist/api-client.d.mts +1 -1
  11. package/dist/api-client.d.ts +1 -1
  12. package/dist/api-client.js +44 -58
  13. package/dist/api-client.mjs +44 -58
  14. package/dist/api-client.mjs.map +1 -1
  15. package/dist/{api-middleware-beNlXd5P.js → api-middleware-BVXf7qCM.js} +5 -3
  16. package/dist/{api-middleware-DvmkJ_Li.mjs → api-middleware-CAarHFKk.mjs} +6 -4
  17. package/dist/api-middleware-CAarHFKk.mjs.map +1 -0
  18. package/dist/{api-middleware-BtsYJU5u.d.mts → api-middleware-CIXuGurw.d.mts} +4 -2
  19. package/dist/{api-middleware-BtsYJU5u.d.mts.map → api-middleware-CIXuGurw.d.mts.map} +1 -1
  20. package/dist/{api-middleware-DyLDqOYF.d.ts → api-middleware-CpAbnkfa.d.ts} +4 -2
  21. package/dist/{api-middleware-DyLDqOYF.d.ts.map → api-middleware-CpAbnkfa.d.ts.map} +1 -1
  22. package/dist/api-middleware.d.mts +1 -1
  23. package/dist/api-middleware.d.ts +1 -1
  24. package/dist/api-middleware.js +1 -1
  25. package/dist/api-middleware.mjs +1 -1
  26. package/dist/cedar-wasm.d.mts +1 -1
  27. package/dist/cedar-wasm.d.ts +1 -1
  28. package/dist/cedar-wasm.js +1 -1
  29. package/dist/cedar-wasm.mjs +1 -1
  30. package/dist/cedar-wasm.mjs.map +1 -1
  31. package/dist/{config-DkXvl4nH.d.ts → config-CVqpjhwT.d.mts} +12 -6
  32. package/dist/config-CVqpjhwT.d.mts.map +1 -0
  33. package/dist/{config-D7h2w_it.d.mts → config-Dn2ptSA2.d.ts} +12 -6
  34. package/dist/config-Dn2ptSA2.d.ts.map +1 -0
  35. package/dist/index-edge.d.mts +5 -5
  36. package/dist/index-edge.d.ts +5 -5
  37. package/dist/index-edge.js +2 -2
  38. package/dist/index-edge.mjs +2 -2
  39. package/dist/index.d.mts +5 -5
  40. package/dist/index.d.ts +5 -5
  41. package/dist/index.js +2 -2
  42. package/dist/index.mjs +2 -2
  43. package/dist/middleware-bundler.d.mts +1 -1
  44. package/dist/middleware-bundler.d.ts +1 -1
  45. package/dist/middleware-bundler.js +1 -1
  46. package/dist/middleware-bundler.mjs +1 -1
  47. package/dist/{middleware-core-BmW2kH8J.d.ts → middleware-core-BLCAErYE.d.ts} +3 -3
  48. package/dist/middleware-core-BLCAErYE.d.ts.map +1 -0
  49. package/dist/{middleware-core-WZeMSRqx.mjs → middleware-core-C4xi1I9I.mjs} +61 -27
  50. package/dist/middleware-core-C4xi1I9I.mjs.map +1 -0
  51. package/dist/{middleware-core-CnPUoD2v.d.mts → middleware-core-D88q8dIt.d.mts} +3 -3
  52. package/dist/middleware-core-D88q8dIt.d.mts.map +1 -0
  53. package/dist/{middleware-core-B1GuspOY.js → middleware-core-tGGPv727.js} +60 -26
  54. package/dist/middleware-edge.d.mts +1 -1
  55. package/dist/middleware-edge.d.ts +1 -1
  56. package/dist/middleware-edge.js +1 -1
  57. package/dist/middleware-edge.mjs +1 -1
  58. package/dist/middleware-node.d.mts +2 -2
  59. package/dist/middleware-node.d.ts +2 -2
  60. package/dist/middleware-node.js +1 -1
  61. package/dist/middleware-node.mjs +1 -1
  62. package/dist/public-api-CBQzjriI.d.mts +5 -0
  63. package/dist/public-api-Ea4jgcwD.d.ts +5 -0
  64. package/package.json +3 -3
  65. package/dist/adapt-BvqYgrAk.mjs.map +0 -1
  66. package/dist/api-client-JHOrRUA_.d.mts.map +0 -1
  67. package/dist/api-client-JHOrRUA_.d.ts.map +0 -1
  68. package/dist/api-middleware-DvmkJ_Li.mjs.map +0 -1
  69. package/dist/config-D7h2w_it.d.mts.map +0 -1
  70. package/dist/config-DkXvl4nH.d.ts.map +0 -1
  71. package/dist/middleware-core-BmW2kH8J.d.ts.map +0 -1
  72. package/dist/middleware-core-CnPUoD2v.d.mts.map +0 -1
  73. package/dist/middleware-core-WZeMSRqx.mjs.map +0 -1
  74. package/dist/public-api-Cw7Zj290.d.ts +0 -5
  75. package/dist/public-api-DgCw1uo6.d.mts +0 -5
@@ -1 +0,0 @@
1
- {"version":3,"file":"middleware-core-WZeMSRqx.mjs","names":["waitUntil","vercelWaitUntil"],"sources":["../src/translate.ts","../src/middleware-core.ts"],"sourcesContent":["/**\n * D.2 — `NextRequest` → `IncomingHttpLike` translator.\n *\n * The host wrapper's *only* job on the inbound path: take Next.js's\n * native request shape and produce the transport-agnostic\n * `IncomingHttpLike` Phase C's orchestrator consumes. Zero verification\n * logic, zero adapter calls, zero engine I/O.\n *\n * Shared between the Node-runtime and Edge-runtime entries. Next.js\n * `NextRequest` is the same shape in both runtimes — `req.headers` is\n * a `Headers` instance, `req.body` is a `ReadableStream`, `req.ip` is\n * a getter (only present in some deployment surfaces; forwarding headers\n * are considered only when the caller selects a matching trust policy).\n */\n\nimport type { NextRequest } from 'next/server.js';\n\nimport type { IncomingHttpLike } from '@kya-os/checkpoint-wasm-runtime/orchestrator';\nimport type { ClientIpTrustPolicy } from '@kya-os/checkpoint-shared';\n\nimport { getClientIp } from './utils';\n\n/**\n * Options controlling translator side effects.\n *\n * `drainJsonBody` (default `true`) controls whether the translator\n * reads the request body so the orchestrator can extract a KYA-OS\n * envelope from `_meta.proof.jws` (the canonical spec-form transport).\n *\n * Trade-off — body draining consumes the request stream. We use\n * `req.clone().text()` so the original `req.body` stream is preserved\n * for downstream Next.js handlers. Cloning has memory overhead\n * proportional to the body size; for large request bodies (>1 MB) or\n * streaming-sensitive routes, set `drainJsonBody: false` and verify\n * envelopes via the `KYA-Delegation` header transport instead (the\n * legacy form, requires `legacyEnvelopeFallback: true` on\n * `withCheckpoint`).\n */\nexport interface NextRequestToHttpLikeOpts {\n /**\n * Read the request body when `content-type` is `application/json` so\n * the orchestrator can extract a KYA-OS envelope from\n * `_meta.proof.jws`. Default `true`. Set `false` for streaming\n * middlewares that can't tolerate the clone/read overhead.\n */\n drainJsonBody?: boolean;\n /** Deployment trust profile. Defaults to direct/peer-only fail-closed behavior. */\n clientIpPolicy?: ClientIpTrustPolicy;\n}\n\n/**\n * Translate a Next.js `NextRequest` into the orchestrator's\n * `IncomingHttpLike` shape.\n *\n * When `opts.drainJsonBody !== false` and the request is JSON, the\n * translator clones the request and reads the body into a string so\n * the orchestrator's `buildAgentRequest` can extract a\n * `_meta.proof.jws` envelope. The original `req.body` stream is\n * preserved for downstream handlers via `req.clone()`.\n *\n * When the body can't be read (drain disabled, non-JSON, or a stream\n * error) we pass `body: null` and the orchestrator routes to\n * PlainHttp — same behavior as the pre-1.1.0 default.\n */\nexport async function nextRequestToHttpLike(\n req: NextRequest,\n opts: NextRequestToHttpLikeOpts = {}\n): Promise<IncomingHttpLike> {\n const url = new URL(req.url);\n const body = await tryDrainJsonBody(req, opts);\n return {\n method: req.method,\n // Path + query only — orchestrator's URL parsing expects no scheme/host.\n url: url.pathname + url.search,\n headers: headersToRecord(req.headers),\n body,\n remoteAddress: getClientIp(req, opts.clientIpPolicy),\n };\n}\n\n/**\n * Read the request body as a UTF-8 string when JSON and the caller\n * has opted in (default). Returns `null` on any failure — the\n * orchestrator handles `null` as \"no body present\" (→ PlainHttp).\n *\n * Clone-first means the original `req.body` ReadableStream stays\n * unconsumed and downstream handlers can re-read it. Without the\n * clone, calling `req.text()` in middleware would block any route\n * handler that needs the body.\n */\nasync function tryDrainJsonBody(\n req: NextRequest,\n opts: NextRequestToHttpLikeOpts\n): Promise<string | null> {\n if (opts.drainJsonBody === false) return null;\n const contentType = req.headers.get('content-type') ?? '';\n if (!contentType.toLowerCase().includes('application/json')) return null;\n try {\n return await req.clone().text();\n } catch {\n // Body unreadable (stream errored, content-length mismatch, etc.).\n // Falling through to null routes the request as PlainHttp —\n // identical behavior to the pre-1.1.0 default.\n return null;\n }\n}\n\n/**\n * Convert a `Headers` instance into a lowercase-keyed plain object.\n * HTTP header names are case-insensitive (RFC 9110 § 5.1); the\n * orchestrator does case-sensitive lookups, so we normalise to\n * lowercase here. Multi-value headers (Set-Cookie, Accept) are\n * surfaced as their `Headers.get()` view — a single string with\n * comma-joined values, matching what other host adapters produce.\n */\nfunction headersToRecord(headers: Headers): Record<string, string> {\n const out: Record<string, string> = {};\n headers.forEach((value, key) => {\n out[key.toLowerCase()] = value;\n });\n return out;\n}\n","/**\n * The Next.js side of the shared request pipeline\n * (`@kya-os/checkpoint-wasm-runtime/pipeline`). Node, bundler and Edge hosts\n * inject their engine and Cedar compiler; runtime-specific WASM initialization\n * stays in each host entry. Public withCheckpoint exports live there too.\n */\n\nimport {\n logPipelineFailure,\n makeCheckpointPipeline,\n type PipelineEngine,\n type PolicyCompile,\n} from '@kya-os/checkpoint-wasm-runtime/pipeline';\nimport type { ReporterContext } from '@kya-os/checkpoint-wasm-runtime/reporter';\nimport type { ClientIpTrustPolicy } from '@kya-os/checkpoint-shared';\n\nimport { type NextFetchEvent, type NextRequest, type NextResponse } from 'next/server.js';\n\nimport { waitUntil as vercelWaitUntil } from '@vercel/functions';\n\nimport { adaptToNextResponse, failClosedResponse } from './adapt';\nimport type { CheckpointConfig } from './config';\nimport { nextRequestToHttpLike } from './translate';\nimport { getClientIp } from './utils';\n\n// `CheckpointConfig` lives in `./config`; the public import paths (`.`,\n// `/node`, `/edge`) stay stable via this re-export.\nexport type { CheckpointConfig } from './config';\n\n/** The engine a host runs: one `./orchestrator` build of wasm-runtime. */\nexport type EngineEntry = PipelineEngine;\n\nexport interface NextMiddlewareHost {\n runtime: 'node' | 'edge';\n /**\n * Loads the engine. Hosts import it lazily, so a failure to load the engine\n * (a missing or unreadable `.wasm`) rejects here, inside the request\n * pipeline, and is answered 503 instead of failing the middleware module.\n */\n loadEngine: () => Promise<EngineEntry>;\n /** The Cedar compiler this runtime can load, for the project's composed policy. */\n policyCompiler: (config: CheckpointConfig) => PolicyCompile;\n /** Host-specific setup, run once with the rest; it may throw. */\n start?: (config: CheckpointConfig) => void;\n}\n\ntype CheckpointMiddleware = (\n req: NextRequest,\n event?: Pick<NextFetchEvent, 'waitUntil'>\n) => Promise<NextResponse>;\n\n/** npm package name this SDK self-reports to the detection reporter. */\nconst SDK_NAME = '@kya-os/checkpoint-nextjs';\n\n/**\n * Installed SDK version, self-reported to the detection reporter so the\n * dashboard can version-gate \"composed policy enforces here\". MUST equal\n * `package.json` version — pinned by a unit test (the old hardcoded `0.1.0` had\n * drifted). Re-exported as `VERSION` from the package index.\n */\nexport const VERSION = '1.12.0';\n\n/**\n * Build the Checkpoint middleware. Returns a function `(req) => NextResponse`\n * suitable for `export default withCheckpoint({...})` in `middleware.ts`.\n *\n * Every verification decision flows through the Rust `kya-os-engine`\n * via WASM. The TS layer translates request shape, runs the shared\n * pipeline, and translates the verdict to `NextResponse`. No\n * verification logic lives in this file.\n *\n * It never throws. An error in the configuration or in loading the engine\n * would otherwise fail the middleware module, and a Next.js middleware that\n * fails to load can let requests through unchecked. Every such error is\n * answered 503 instead (fail closed) and logged.\n */\nexport function makeNextMiddleware(\n config: CheckpointConfig,\n host: NextMiddlewareHost\n): CheckpointMiddleware {\n try {\n return makeMiddleware(config, host);\n } catch (error) {\n return async function misconfiguredCheckpointMiddleware() {\n console.error('[checkpoint] withCheckpoint is misconfigured; responding 503:', error);\n return failClosedResponse();\n };\n }\n}\n\nfunction makeMiddleware(config: CheckpointConfig, host: NextMiddlewareHost): CheckpointMiddleware {\n const pipeline = makeCheckpointPipeline<NextRequest, CheckpointConfig>(config, {\n // Self-identify (incl. node-vs-edge) so the dashboard can version-gate\n // enforcement. Next.js EDGE composed enforcement is opt-in (needs a Cedar\n // module), so the dashboard shows it as opt-in, never \"Enforcing\".\n sdk: { name: SDK_NAME, version: VERSION, runtime: host.runtime },\n loadEngine: host.loadEngine,\n policyCompiler: host.policyCompiler,\n start: host.start,\n });\n const translateOpts = {\n drainJsonBody: config.drainJsonBody,\n clientIpPolicy: config.clientIpPolicy,\n };\n return async function checkpointMiddleware(\n req: NextRequest,\n event?: Pick<NextFetchEvent, 'waitUntil'>\n ): Promise<NextResponse> {\n // Work that outlives the response (the engine's vendor key feed round, the\n // detection report) must be registered, or Vercel and Workers cancel it\n // once the response is sent. Next.js passes the event to middleware; a\n // wrapper that drops it falls back to Vercel's request context.\n const waitUntil = event\n ? (promise: Promise<unknown>) => event.waitUntil(promise)\n : vercelWaitUntil;\n try {\n const { rendered } = await pipeline({\n request: req,\n translate: () => nextRequestToHttpLike(req, translateOpts),\n headers: req.headers,\n // The query-free path verification saw.\n path: req.nextUrl.pathname,\n clientIp: () => getClientIp(req, config.clientIpPolicy),\n reportContext: () => extractReporterContext(req, config.clientIpPolicy),\n waitUntil,\n });\n return adaptToNextResponse(rendered, req);\n } catch (err) {\n logPipelineFailure(err, config.debug);\n return failClosedResponse();\n }\n };\n}\n\n/**\n * Pull the request context the dashboard's `/api/v1/log-detection`\n * endpoint expects out of a `NextRequest`. Works under both Node and\n * Edge runtimes — no runtime-specific APIs.\n */\nfunction extractReporterContext(\n req: NextRequest,\n policy: ClientIpTrustPolicy | undefined\n): ReporterContext {\n return {\n userAgent: req.headers.get('user-agent') ?? undefined,\n ipAddress: getClientIp(req, policy),\n path: req.nextUrl.pathname,\n url: req.nextUrl.href,\n method: req.method,\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAgEA,eAAsB,sBACpB,KACA,OAAkC,CA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,OACuB;EAKvB,MAAMA,cAAY,SACb,YAA8B,MAAM,UAAU,OAAO,IACtDC;EACJ,IAAI;GACF,MAAM,EAAE,aAAa,MAAM,SAAS;IAClC,SAAS;IACT,iBAAiB,sBAAsB,KAAK,aAAa;IACzD,SAAS,IAAI;IAEb,MAAM,IAAI,QAAQ;IAClB,gBAAgB,YAAY,KAAK,OAAO,cAAc;IACtD,qBAAqB,uBAAuB,KAAK,OAAO,cAAc;IACtE;GACF,CAAC;GACD,OAAO,oBAAoB,UAAU,GAAG;EAC1C,SAAS,KAAK;GACZ,mBAAmB,KAAK,OAAO,KAAK;GACpC,OAAO,mBAAmB;EAC5B;CACF;AACF;;;;;;AAOA,SAAS,uBACP,KACA,QACiB;CACjB,OAAO;EACL,WAAW,IAAI,QAAQ,IAAI,YAAY,KAAK;EAC5C,WAAW,YAAY,KAAK,MAAM;EAClC,MAAM,IAAI,QAAQ;EAClB,KAAK,IAAI,QAAQ;EACjB,QAAQ,IAAI;CACd;AACF"}
@@ -1,5 +0,0 @@
1
- import "./api-client-JHOrRUA_.js";
2
- import "./api-middleware-DyLDqOYF.js";
3
- import "./config-DkXvl4nH.js";
4
- import "./posture-outcome-Ccujx_9V.js";
5
- import "./middleware-core-BmW2kH8J.js";
@@ -1,5 +0,0 @@
1
- import "./api-client-JHOrRUA_.mjs";
2
- import "./api-middleware-BtsYJU5u.mjs";
3
- import "./config-D7h2w_it.mjs";
4
- import "./posture-outcome-Ccujx_9V.mjs";
5
- import "./middleware-core-CnPUoD2v.mjs";