@kya-os/checkpoint-nextjs 1.0.1 → 1.1.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.
package/dist/translate.js CHANGED
@@ -1,21 +1,28 @@
1
1
  'use strict';
2
2
 
3
3
  // src/translate.ts
4
- function nextRequestToHttpLike(req) {
4
+ async function nextRequestToHttpLike(req, opts = {}) {
5
5
  const url = new URL(req.url);
6
+ const body = await tryDrainJsonBody(req, opts);
6
7
  return {
7
8
  method: req.method,
8
9
  // Path + query only — orchestrator's URL parsing expects no scheme/host.
9
10
  url: url.pathname + url.search,
10
11
  headers: headersToRecord(req.headers),
11
- // NextRequest.body is a ReadableStream; we don't drain it here.
12
- // The orchestrator routes to PlainHttp when body is falsy, which
13
- // is the right call for streaming middlewares that don't want to
14
- // buffer the request body just to detect agents.
15
- body: null,
12
+ body,
16
13
  remoteAddress: extractRemoteAddress(req)
17
14
  };
18
15
  }
16
+ async function tryDrainJsonBody(req, opts) {
17
+ if (opts.drainJsonBody === false) return null;
18
+ const contentType = req.headers.get("content-type") ?? "";
19
+ if (!contentType.toLowerCase().includes("application/json")) return null;
20
+ try {
21
+ return await req.clone().text();
22
+ } catch {
23
+ return null;
24
+ }
25
+ }
19
26
  function headersToRecord(headers) {
20
27
  const out = {};
21
28
  headers.forEach((value, key) => {
@@ -1,19 +1,26 @@
1
1
  // src/translate.ts
2
- function nextRequestToHttpLike(req) {
2
+ async function nextRequestToHttpLike(req, opts = {}) {
3
3
  const url = new URL(req.url);
4
+ const body = await tryDrainJsonBody(req, opts);
4
5
  return {
5
6
  method: req.method,
6
7
  // Path + query only — orchestrator's URL parsing expects no scheme/host.
7
8
  url: url.pathname + url.search,
8
9
  headers: headersToRecord(req.headers),
9
- // NextRequest.body is a ReadableStream; we don't drain it here.
10
- // The orchestrator routes to PlainHttp when body is falsy, which
11
- // is the right call for streaming middlewares that don't want to
12
- // buffer the request body just to detect agents.
13
- body: null,
10
+ body,
14
11
  remoteAddress: extractRemoteAddress(req)
15
12
  };
16
13
  }
14
+ async function tryDrainJsonBody(req, opts) {
15
+ if (opts.drainJsonBody === false) return null;
16
+ const contentType = req.headers.get("content-type") ?? "";
17
+ if (!contentType.toLowerCase().includes("application/json")) return null;
18
+ try {
19
+ return await req.clone().text();
20
+ } catch {
21
+ return null;
22
+ }
23
+ }
17
24
  function headersToRecord(headers) {
18
25
  const out = {};
19
26
  headers.forEach((value, key) => {
@@ -26,22 +26,41 @@ interface AgentShieldConfig {
26
26
  };
27
27
  }
28
28
  /**
29
- * Create a WASM-enabled AgentShield middleware
30
- * This must be used with proper WASM module import at the top of middleware.ts
29
+ * Create a WASM-enabled Checkpoint middleware (**pattern-detection only**).
31
30
  *
32
- * @example
31
+ * **This factory runs UA/header pattern matching only.** It does NOT
32
+ * verify MCP-I signed envelopes — no JWS verification, no DID
33
+ * resolution, no orchestrator stages. Use it when your only enforcement
34
+ * concern is "is this request from a known bot pattern."
35
+ *
36
+ * **For envelope verification, use {@link withCheckpoint} instead** —
37
+ * exported from `@kya-os/checkpoint-nextjs` (Node runtime) or
38
+ * `@kya-os/checkpoint-nextjs/edge` (Edge runtime). `withCheckpoint`
39
+ * routes every request through the kya-os-engine via WASM and supports
40
+ * both `_meta.proof.jws` body envelopes (default) and the legacy
41
+ * `KYA-Delegation` header form (opt-in via `legacyEnvelopeFallback`).
42
+ * See SDK-Envelope-Plumbing-1 (#2594) for the migration context.
43
+ *
44
+ * @example pattern-only (this factory)
33
45
  * ```typescript
34
- * // middleware.ts
35
46
  * import wasmModule from '@kya-os/checkpoint/wasm?module';
36
- * import { createWasmAgentShieldMiddleware } from '@kya-os/checkpoint-nextjs';
47
+ * import { createCheckpointWasmMiddleware } from '@kya-os/checkpoint-nextjs';
37
48
  *
38
49
  * const wasmInstance = await WebAssembly.instantiate(wasmModule);
39
- *
40
- * export const middleware = createWasmAgentShieldMiddleware({
50
+ * export const middleware = createCheckpointWasmMiddleware({
41
51
  * wasmInstance,
42
- * onAgentDetected: (result) => {
43
- * console.log(`Detected ${result.agent} with ${result.confidence * 100}% confidence`);
44
- * }
52
+ * confidenceThreshold: 80,
53
+ * });
54
+ * ```
55
+ *
56
+ * @example envelope verification (use `withCheckpoint` instead)
57
+ * ```typescript
58
+ * import { withCheckpoint } from '@kya-os/checkpoint-nextjs';
59
+ *
60
+ * export default withCheckpoint({
61
+ * tenantHost: 'acme.checkpoint.example',
62
+ * legacyEnvelopeFallback: true, // accept `KYA-Delegation` header form
63
+ * // drainJsonBody defaults to true; spec-form `_meta.proof.jws` works out of the box
45
64
  * });
46
65
  * ```
47
66
  */
@@ -26,22 +26,41 @@ interface AgentShieldConfig {
26
26
  };
27
27
  }
28
28
  /**
29
- * Create a WASM-enabled AgentShield middleware
30
- * This must be used with proper WASM module import at the top of middleware.ts
29
+ * Create a WASM-enabled Checkpoint middleware (**pattern-detection only**).
31
30
  *
32
- * @example
31
+ * **This factory runs UA/header pattern matching only.** It does NOT
32
+ * verify MCP-I signed envelopes — no JWS verification, no DID
33
+ * resolution, no orchestrator stages. Use it when your only enforcement
34
+ * concern is "is this request from a known bot pattern."
35
+ *
36
+ * **For envelope verification, use {@link withCheckpoint} instead** —
37
+ * exported from `@kya-os/checkpoint-nextjs` (Node runtime) or
38
+ * `@kya-os/checkpoint-nextjs/edge` (Edge runtime). `withCheckpoint`
39
+ * routes every request through the kya-os-engine via WASM and supports
40
+ * both `_meta.proof.jws` body envelopes (default) and the legacy
41
+ * `KYA-Delegation` header form (opt-in via `legacyEnvelopeFallback`).
42
+ * See SDK-Envelope-Plumbing-1 (#2594) for the migration context.
43
+ *
44
+ * @example pattern-only (this factory)
33
45
  * ```typescript
34
- * // middleware.ts
35
46
  * import wasmModule from '@kya-os/checkpoint/wasm?module';
36
- * import { createWasmAgentShieldMiddleware } from '@kya-os/checkpoint-nextjs';
47
+ * import { createCheckpointWasmMiddleware } from '@kya-os/checkpoint-nextjs';
37
48
  *
38
49
  * const wasmInstance = await WebAssembly.instantiate(wasmModule);
39
- *
40
- * export const middleware = createWasmAgentShieldMiddleware({
50
+ * export const middleware = createCheckpointWasmMiddleware({
41
51
  * wasmInstance,
42
- * onAgentDetected: (result) => {
43
- * console.log(`Detected ${result.agent} with ${result.confidence * 100}% confidence`);
44
- * }
52
+ * confidenceThreshold: 80,
53
+ * });
54
+ * ```
55
+ *
56
+ * @example envelope verification (use `withCheckpoint` instead)
57
+ * ```typescript
58
+ * import { withCheckpoint } from '@kya-os/checkpoint-nextjs';
59
+ *
60
+ * export default withCheckpoint({
61
+ * tenantHost: 'acme.checkpoint.example',
62
+ * legacyEnvelopeFallback: true, // accept `KYA-Delegation` header form
63
+ * // drainJsonBody defaults to true; spec-form `_meta.proof.jws` works out of the box
45
64
  * });
46
65
  * ```
47
66
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kya-os/checkpoint-nextjs",
3
- "version": "1.0.1",
3
+ "version": "1.1.0",
4
4
  "description": "Checkpoint Next.js middleware for AI agent detection (formerly @kya-os/agentshield-nextjs)",
5
5
  "keywords": [
6
6
  "nextjs",