@kya-os/checkpoint-nextjs 1.1.4 → 1.7.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 (43) hide show
  1. package/CHANGELOG.md +190 -0
  2. package/dist/composed-policy.d.mts +108 -0
  3. package/dist/composed-policy.d.ts +108 -0
  4. package/dist/composed-policy.js +91 -0
  5. package/dist/composed-policy.mjs +85 -0
  6. package/dist/config-_nfPN3E3.d.mts +205 -0
  7. package/dist/config-kxFihzR_.d.ts +205 -0
  8. package/dist/create-middleware.js +0 -2
  9. package/dist/create-middleware.mjs +0 -2
  10. package/dist/edge-runtime-loader.js +3 -1
  11. package/dist/edge-runtime-loader.mjs +3 -1
  12. package/dist/edge-wasm-middleware.d.mts +6 -6
  13. package/dist/edge-wasm-middleware.d.ts +6 -6
  14. package/dist/index.d.mts +6 -14
  15. package/dist/index.d.ts +6 -14
  16. package/dist/index.js +162 -9
  17. package/dist/index.mjs +163 -10
  18. package/dist/middleware-edge.d.mts +7 -3
  19. package/dist/middleware-edge.d.ts +7 -3
  20. package/dist/middleware-edge.js +159 -4
  21. package/dist/middleware-edge.mjs +156 -4
  22. package/dist/middleware-node.d.mts +39 -101
  23. package/dist/middleware-node.d.ts +39 -101
  24. package/dist/middleware-node.js +166 -4
  25. package/dist/middleware-node.mjs +163 -5
  26. package/dist/middleware.d.mts +10 -1
  27. package/dist/middleware.d.ts +10 -1
  28. package/dist/middleware.js +6 -0
  29. package/dist/middleware.mjs +6 -1
  30. package/dist/nodejs-wasm-loader.d.mts +3 -4
  31. package/dist/nodejs-wasm-loader.d.ts +3 -4
  32. package/dist/nodejs-wasm-loader.js +1 -1
  33. package/dist/nodejs-wasm-loader.mjs +1 -1
  34. package/dist/signature-verifier.js +2 -2
  35. package/dist/signature-verifier.mjs +2 -2
  36. package/dist/wasm-setup.js +1 -1
  37. package/dist/wasm-setup.mjs +1 -1
  38. package/package.json +8 -11
  39. package/dist/wasm-middleware.d.mts +0 -98
  40. package/dist/wasm-middleware.d.ts +0 -98
  41. package/dist/wasm-middleware.js +0 -125
  42. package/dist/wasm-middleware.mjs +0 -121
  43. package/templates/middleware-wasm-100.ts +0 -161
@@ -1,161 +0,0 @@
1
- /**
2
- * Checkpoint Middleware Template — Legacy WASM Factory
3
- *
4
- * ⚠️ **DEPRECATED TEMPLATE (AgentDetector-Deletion-1):** this template
5
- * uses `createWasmAgentShieldMiddleware`, which is deprecated and
6
- * will be removed in the next minor. For new projects, use
7
- * `withCheckpoint` instead — it's engine-backed (PDM-1 #2560), runs
8
- * MCP-I envelope verification, and is the canonical Phase-D
9
- * replacement. See the `withCheckpoint` recipe in
10
- * `packages/checkpoint-nextjs/README.md`.
11
- *
12
- * This template stays in-tree for one release as a migration reference;
13
- * the deprecated factory still works (with a dev-only console.warn).
14
- *
15
- * Installation:
16
- * 1. Copy this file to your project root as `middleware.ts`
17
- * 2. Install packages: npm install @kya-os/checkpoint @kya-os/checkpoint-nextjs
18
- * 3. Deploy to Vercel for Edge Runtime support
19
- */
20
-
21
- import { NextResponse } from 'next/server';
22
- import type { NextRequest } from 'next/server';
23
-
24
- // CRITICAL: Import WASM module with ?module suffix for Edge Runtime
25
- // This MUST be at the top of the file, before any other AgentShield imports
26
- import wasmModule from '@kya-os/checkpoint/wasm?module';
27
-
28
- // Now import the middleware creator
29
- import {
30
- createWasmAgentShieldMiddleware,
31
- instantiateWasm,
32
- } from '@kya-os/checkpoint-nextjs/wasm-middleware';
33
-
34
- // Initialize WASM module once at startup
35
- let wasmInstancePromise: Promise<WebAssembly.Instance> | null = null;
36
-
37
- async function getWasmInstance() {
38
- if (!wasmInstancePromise) {
39
- wasmInstancePromise = instantiateWasm(wasmModule);
40
- }
41
- return wasmInstancePromise;
42
- }
43
-
44
- export async function middleware(request: NextRequest) {
45
- try {
46
- // Get or create WASM instance
47
- const wasmInstance = await getWasmInstance();
48
-
49
- // Create middleware with WASM support
50
- const agentShieldMiddleware = createWasmAgentShieldMiddleware({
51
- wasmInstance,
52
-
53
- // Skip authentication and static assets
54
- skipPaths: ['/api/auth', '/_next', '/favicon.ico', '/public'],
55
-
56
- // What to do when agent is detected
57
- onAgentDetected: async (result) => {
58
- // With WASM: 95-100% confidence for cryptographically verified agents
59
- console.log(`🤖 AI Agent detected:`, {
60
- agent: result.agent,
61
- confidence: `${Math.round(result.confidence * 100)}%`,
62
- verification: result.verificationMethod, // 'signature' with WASM, 'pattern' without
63
- risk: result.riskLevel,
64
- timestamp: result.timestamp,
65
- });
66
-
67
- // You can add custom logic here:
68
- // - Log to analytics
69
- // - Send alerts
70
- // - Apply rate limiting
71
- // - etc.
72
- },
73
-
74
- // Set to true to block AI agents
75
- blockOnHighConfidence: false, // Change to true to block agents
76
-
77
- // Minimum confidence to trigger blocking (0.8 = 80%)
78
- confidenceThreshold: 0.8,
79
-
80
- // Custom response when blocking
81
- blockedResponse: {
82
- status: 403,
83
- message: 'AI agent access restricted',
84
- headers: {
85
- 'Content-Type': 'application/json',
86
- 'X-Blocked-Reason': 'ai-agent-detected',
87
- },
88
- },
89
- });
90
-
91
- // Run AgentShield detection
92
- const response = await agentShieldMiddleware(request);
93
-
94
- // Add security headers to all responses
95
- response.headers.set('X-Frame-Options', 'DENY');
96
- response.headers.set('X-Content-Type-Options', 'nosniff');
97
- response.headers.set('Referrer-Policy', 'strict-origin-when-cross-origin');
98
-
99
- return response;
100
- } catch (error) {
101
- // If WASM fails to load, fall back to pattern detection (85% confidence)
102
- console.warn('⚠️ WASM initialization failed, using pattern detection:', error);
103
-
104
- // You could use the regular middleware here as fallback
105
- // For now, just continue
106
- return NextResponse.next();
107
- }
108
- }
109
-
110
- // Configure which paths the middleware runs on
111
- export const config = {
112
- matcher: [
113
- /*
114
- * Match all request paths except for the ones starting with:
115
- * - _next/static (static files)
116
- * - _next/image (image optimization files)
117
- * - favicon.ico (favicon file)
118
- * - public folder
119
- */
120
- {
121
- source: '/((?!_next/static|_next/image|favicon.ico|public).*)',
122
- missing: [
123
- { type: 'header', key: 'next-router-prefetch' },
124
- { type: 'header', key: 'purpose', value: 'prefetch' },
125
- ],
126
- },
127
- ],
128
- };
129
-
130
- /**
131
- * TypeScript Support
132
- *
133
- * Add this to a `types/wasm.d.ts` file in your project:
134
- *
135
- * declare module '@kya-os/checkpoint/wasm?module' {
136
- * const value: WebAssembly.Module;
137
- * export default value;
138
- * }
139
- */
140
-
141
- /**
142
- * What You'll See in Logs:
143
- *
144
- * With WASM (95-100% confidence):
145
- * 🤖 AI Agent detected: {
146
- * agent: 'ChatGPT-User',
147
- * confidence: '100%',
148
- * verification: 'signature', // Cryptographically verified!
149
- * risk: 'high',
150
- * timestamp: '2024-01-01T00:00:00.000Z'
151
- * }
152
- *
153
- * Without WASM (85% confidence):
154
- * 🤖 AI Agent detected: {
155
- * agent: 'ChatGPT-User',
156
- * confidence: '85%',
157
- * verification: 'pattern', // Pattern matching only
158
- * risk: 'medium',
159
- * timestamp: '2024-01-01T00:00:00.000Z'
160
- * }
161
- */