@kya-os/checkpoint-nextjs 1.0.0 → 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.
Files changed (91) hide show
  1. package/CHANGELOG.md +165 -0
  2. package/dist/.tsbuildinfo +1 -1
  3. package/dist/adapt.js +0 -2
  4. package/dist/adapt.mjs +0 -2
  5. package/dist/api-client.js +38 -24
  6. package/dist/api-client.mjs +38 -24
  7. package/dist/api-middleware.js +48 -28
  8. package/dist/api-middleware.mjs +48 -28
  9. package/dist/create-middleware.d.mts +1 -1
  10. package/dist/create-middleware.d.ts +1 -1
  11. package/dist/create-middleware.js +0 -2
  12. package/dist/create-middleware.mjs +0 -2
  13. package/dist/edge/index.d.mts +1 -1
  14. package/dist/edge/index.d.ts +1 -1
  15. package/dist/edge/index.js +4 -6
  16. package/dist/edge/index.mjs +4 -6
  17. package/dist/edge-runtime-loader.js +7 -2
  18. package/dist/edge-runtime-loader.mjs +7 -2
  19. package/dist/edge-wasm-middleware.js +0 -2
  20. package/dist/edge-wasm-middleware.mjs +0 -2
  21. package/dist/index.d.mts +1 -1
  22. package/dist/index.d.ts +1 -1
  23. package/dist/index.js +77 -42
  24. package/dist/index.mjs +77 -42
  25. package/dist/middleware-edge.js +17 -10
  26. package/dist/middleware-edge.mjs +17 -10
  27. package/dist/middleware-node.d.mts +36 -0
  28. package/dist/middleware-node.d.ts +36 -0
  29. package/dist/middleware-node.js +17 -10
  30. package/dist/middleware-node.mjs +17 -10
  31. package/dist/middleware.d.mts +1 -1
  32. package/dist/middleware.d.ts +1 -1
  33. package/dist/middleware.js +0 -2
  34. package/dist/middleware.mjs +0 -2
  35. package/dist/nodejs-wasm-loader.js +0 -2
  36. package/dist/nodejs-wasm-loader.mjs +0 -2
  37. package/dist/policy.js +10 -6
  38. package/dist/policy.mjs +10 -6
  39. package/dist/session-tracker.js +1 -3
  40. package/dist/session-tracker.mjs +1 -3
  41. package/dist/signature-verifier.js +0 -2
  42. package/dist/signature-verifier.mjs +0 -2
  43. package/dist/translate.d.mts +36 -9
  44. package/dist/translate.d.ts +36 -9
  45. package/dist/translate.js +13 -8
  46. package/dist/translate.mjs +13 -8
  47. package/dist/{types-C-xCUNTr.d.mts → types-D9RQvPNy.d.mts} +1 -1
  48. package/dist/{types-C-xCUNTr.d.ts → types-D9RQvPNy.d.ts} +1 -1
  49. package/dist/wasm-middleware.d.mts +29 -10
  50. package/dist/wasm-middleware.d.ts +29 -10
  51. package/dist/wasm-middleware.js +0 -2
  52. package/dist/wasm-middleware.mjs +0 -2
  53. package/dist/wasm-setup.js +0 -2
  54. package/dist/wasm-setup.mjs +0 -2
  55. package/package.json +3 -3
  56. package/dist/adapt.js.map +0 -1
  57. package/dist/adapt.mjs.map +0 -1
  58. package/dist/api-client.js.map +0 -1
  59. package/dist/api-client.mjs.map +0 -1
  60. package/dist/api-middleware.js.map +0 -1
  61. package/dist/api-middleware.mjs.map +0 -1
  62. package/dist/create-middleware.js.map +0 -1
  63. package/dist/create-middleware.mjs.map +0 -1
  64. package/dist/edge/index.js.map +0 -1
  65. package/dist/edge/index.mjs.map +0 -1
  66. package/dist/edge-runtime-loader.js.map +0 -1
  67. package/dist/edge-runtime-loader.mjs.map +0 -1
  68. package/dist/edge-wasm-middleware.js.map +0 -1
  69. package/dist/edge-wasm-middleware.mjs.map +0 -1
  70. package/dist/index.js.map +0 -1
  71. package/dist/index.mjs.map +0 -1
  72. package/dist/middleware-edge.js.map +0 -1
  73. package/dist/middleware-edge.mjs.map +0 -1
  74. package/dist/middleware-node.js.map +0 -1
  75. package/dist/middleware-node.mjs.map +0 -1
  76. package/dist/middleware.js.map +0 -1
  77. package/dist/middleware.mjs.map +0 -1
  78. package/dist/nodejs-wasm-loader.js.map +0 -1
  79. package/dist/nodejs-wasm-loader.mjs.map +0 -1
  80. package/dist/policy.js.map +0 -1
  81. package/dist/policy.mjs.map +0 -1
  82. package/dist/session-tracker.js.map +0 -1
  83. package/dist/session-tracker.mjs.map +0 -1
  84. package/dist/signature-verifier.js.map +0 -1
  85. package/dist/signature-verifier.mjs.map +0 -1
  86. package/dist/translate.js.map +0 -1
  87. package/dist/translate.mjs.map +0 -1
  88. package/dist/wasm-middleware.js.map +0 -1
  89. package/dist/wasm-middleware.mjs.map +0 -1
  90. package/dist/wasm-setup.js.map +0 -1
  91. package/dist/wasm-setup.mjs.map +0 -1
@@ -91,7 +91,7 @@ async function createDetector(config) {
91
91
  });
92
92
  await wasmDetector.ensureReady();
93
93
  if (config.debug) {
94
- console.debug("[AgentShield] WASM detector initialized in Edge Runtime", {
94
+ console.debug("[Checkpoint] WASM detector initialized in Edge Runtime", {
95
95
  policyEnabled: !!config.apiKey
96
96
  });
97
97
  }
@@ -118,7 +118,7 @@ async function createDetector(config) {
118
118
  };
119
119
  } catch (error) {
120
120
  if (config.debug) {
121
- console.warn("[AgentShield] WASM runtime not available, using pattern detection:", error);
121
+ console.warn("[Checkpoint] WASM runtime not available, using pattern detection:", error);
122
122
  }
123
123
  detector = {
124
124
  detect: patternDetect,
@@ -127,7 +127,7 @@ async function createDetector(config) {
127
127
  }
128
128
  } else {
129
129
  if (config.debug) {
130
- console.debug("[AgentShield] No WASM module provided, using pattern detection");
130
+ console.debug("[Checkpoint] No WASM module provided, using pattern detection");
131
131
  }
132
132
  detector = {
133
133
  detect: patternDetect,
@@ -178,7 +178,7 @@ function createEdgeMiddleware(config = {}) {
178
178
  const result = await detector.detect(input);
179
179
  if (result.shouldBlock) {
180
180
  if (debug) {
181
- console.debug("[AgentShield] Blocked by policy", {
181
+ console.debug("[Checkpoint] Blocked by policy", {
182
182
  reason: result.blockReason,
183
183
  agent: result.detectedAgent?.name,
184
184
  confidence: result.confidence,
@@ -271,5 +271,3 @@ function createEdgeMiddleware(config = {}) {
271
271
  }
272
272
 
273
273
  export { createEdgeMiddleware };
274
- //# sourceMappingURL=index.mjs.map
275
- //# sourceMappingURL=index.mjs.map
@@ -145,6 +145,13 @@ var EdgeRuntimeAgentShield = class {
145
145
  { pattern: /gemini/i, name: "Google Gemini", confidence: 0.85 },
146
146
  { pattern: /perplexity/i, name: "Perplexity", confidence: 0.85 },
147
147
  // Fallback
148
+ // UA-context-only pattern — `\b` is the correct anchor for UA
149
+ // substring matching. PR #2591's tightened `[\s;()]` form
150
+ // dropped `/`, which broke legit UA shapes like `you.com/1.0`
151
+ // and `+https://you.com)` (cursor catch on the same PR — see
152
+ // sibling agents.ts comment for the full rationale + UA shapes).
153
+ // CodeQL js/regex/missing-regexp-anchor speculates about URL
154
+ // misuse; this codebase only applies the pattern to UA strings.
148
155
  { pattern: /\byou\.com\b/i, name: "You.com", confidence: 0.8 },
149
156
  { pattern: /\bphind\b/i, name: "Phind", confidence: 0.8 }
150
157
  ];
@@ -200,5 +207,3 @@ function getDefaultAgentShield(config) {
200
207
 
201
208
  exports.createEdgeAgentShield = createEdgeAgentShield;
202
209
  exports.getDefaultAgentShield = getDefaultAgentShield;
203
- //# sourceMappingURL=edge-runtime-loader.js.map
204
- //# sourceMappingURL=edge-runtime-loader.js.map
@@ -143,6 +143,13 @@ var EdgeRuntimeAgentShield = class {
143
143
  { pattern: /gemini/i, name: "Google Gemini", confidence: 0.85 },
144
144
  { pattern: /perplexity/i, name: "Perplexity", confidence: 0.85 },
145
145
  // Fallback
146
+ // UA-context-only pattern — `\b` is the correct anchor for UA
147
+ // substring matching. PR #2591's tightened `[\s;()]` form
148
+ // dropped `/`, which broke legit UA shapes like `you.com/1.0`
149
+ // and `+https://you.com)` (cursor catch on the same PR — see
150
+ // sibling agents.ts comment for the full rationale + UA shapes).
151
+ // CodeQL js/regex/missing-regexp-anchor speculates about URL
152
+ // misuse; this codebase only applies the pattern to UA strings.
146
153
  { pattern: /\byou\.com\b/i, name: "You.com", confidence: 0.8 },
147
154
  { pattern: /\bphind\b/i, name: "Phind", confidence: 0.8 }
148
155
  ];
@@ -197,5 +204,3 @@ function getDefaultAgentShield(config) {
197
204
  }
198
205
 
199
206
  export { createEdgeAgentShield, getDefaultAgentShield };
200
- //# sourceMappingURL=edge-runtime-loader.mjs.map
201
- //# sourceMappingURL=edge-runtime-loader.mjs.map
@@ -314,5 +314,3 @@ function createEdgeWasmMiddleware(config) {
314
314
 
315
315
  exports.createEdgeWasmMiddleware = createEdgeWasmMiddleware;
316
316
  exports.initializeEdgeWasm = initializeEdgeWasm;
317
- //# sourceMappingURL=edge-wasm-middleware.js.map
318
- //# sourceMappingURL=edge-wasm-middleware.js.map
@@ -311,5 +311,3 @@ function createEdgeWasmMiddleware(config) {
311
311
  }
312
312
 
313
313
  export { createEdgeWasmMiddleware, initializeEdgeWasm };
314
- //# sourceMappingURL=edge-wasm-middleware.mjs.map
315
- //# sourceMappingURL=edge-wasm-middleware.mjs.map
package/dist/index.d.mts CHANGED
@@ -4,7 +4,7 @@ export { AgentDetectionEvent, AgentSession, AgentShieldMiddlewareConfig, Checkpo
4
4
  export { createAgentShieldMiddleware as createAgentShieldMiddlewareBase } from './middleware.mjs';
5
5
  export { EdgeSessionTracker, SessionData, SessionTrackingConfig, StatelessSessionChecker } from './session-tracker.mjs';
6
6
  export { AgentShieldClient, AgentShieldClientConfig, CheckpointApiClient, CheckpointApiClientConfig, EnforceInput, EnforceResponse, EnforcementDecision, LogDetectionInput, getAgentShieldClient, getCheckpointApiClient, resetAgentShieldClient, resetCheckpointApiClient } from './api-client.mjs';
7
- export { A as AgentShieldRequest, D as DetectionContext, N as NextJSMiddlewareConfig } from './types-C-xCUNTr.mjs';
7
+ export { A as AgentShieldRequest, D as DetectionContext, N as NextJSMiddlewareConfig } from './types-D9RQvPNy.mjs';
8
8
  export { NextJSPolicyMiddlewareConfig, PolicyMiddlewareConfig, applyPolicy, buildBlockedResponse as buildPolicyBlockedResponse, buildRedirectResponse as buildPolicyRedirectResponse, createContextFromDetection, evaluatePolicyForDetection, getPolicy, handlePolicyDecision } from './policy.mjs';
9
9
  export { DEFAULT_POLICY, ENFORCEMENT_ACTIONS, EnforcementAction, PolicyConfig, PolicyEvaluationContext, PolicyEvaluationResult, createEvaluationContext, evaluatePolicy } from '@kya-os/checkpoint-shared';
10
10
  import '@kya-os/checkpoint-wasm-runtime/adapters';
package/dist/index.d.ts CHANGED
@@ -4,7 +4,7 @@ export { AgentDetectionEvent, AgentSession, AgentShieldMiddlewareConfig, Checkpo
4
4
  export { createAgentShieldMiddleware as createAgentShieldMiddlewareBase } from './middleware.js';
5
5
  export { EdgeSessionTracker, SessionData, SessionTrackingConfig, StatelessSessionChecker } from './session-tracker.js';
6
6
  export { AgentShieldClient, AgentShieldClientConfig, CheckpointApiClient, CheckpointApiClientConfig, EnforceInput, EnforceResponse, EnforcementDecision, LogDetectionInput, getAgentShieldClient, getCheckpointApiClient, resetAgentShieldClient, resetCheckpointApiClient } from './api-client.js';
7
- export { A as AgentShieldRequest, D as DetectionContext, N as NextJSMiddlewareConfig } from './types-C-xCUNTr.js';
7
+ export { A as AgentShieldRequest, D as DetectionContext, N as NextJSMiddlewareConfig } from './types-D9RQvPNy.js';
8
8
  export { NextJSPolicyMiddlewareConfig, PolicyMiddlewareConfig, applyPolicy, buildBlockedResponse as buildPolicyBlockedResponse, buildRedirectResponse as buildPolicyRedirectResponse, createContextFromDetection, evaluatePolicyForDetection, getPolicy, handlePolicyDecision } from './policy.js';
9
9
  export { DEFAULT_POLICY, ENFORCEMENT_ACTIONS, EnforcementAction, PolicyConfig, PolicyEvaluationContext, PolicyEvaluationResult, createEvaluationContext, evaluatePolicy } from '@kya-os/checkpoint-shared';
10
10
  import '@kya-os/checkpoint-wasm-runtime/adapters';
package/dist/index.js CHANGED
@@ -56,21 +56,28 @@ function applyHeaders(res, headers) {
56
56
  }
57
57
 
58
58
  // src/translate.ts
59
- function nextRequestToHttpLike(req) {
59
+ async function nextRequestToHttpLike(req, opts = {}) {
60
60
  const url = new URL(req.url);
61
+ const body = await tryDrainJsonBody(req, opts);
61
62
  return {
62
63
  method: req.method,
63
64
  // Path + query only — orchestrator's URL parsing expects no scheme/host.
64
65
  url: url.pathname + url.search,
65
66
  headers: headersToRecord(req.headers),
66
- // NextRequest.body is a ReadableStream; we don't drain it here.
67
- // The orchestrator routes to PlainHttp when body is falsy, which
68
- // is the right call for streaming middlewares that don't want to
69
- // buffer the request body just to detect agents.
70
- body: null,
67
+ body,
71
68
  remoteAddress: extractRemoteAddress(req)
72
69
  };
73
70
  }
71
+ async function tryDrainJsonBody(req, opts) {
72
+ if (opts.drainJsonBody === false) return null;
73
+ const contentType = req.headers.get("content-type") ?? "";
74
+ if (!contentType.toLowerCase().includes("application/json")) return null;
75
+ try {
76
+ return await req.clone().text();
77
+ } catch {
78
+ return null;
79
+ }
80
+ }
74
81
  function headersToRecord(headers) {
75
82
  const out = {};
76
83
  headers.forEach((value, key) => {
@@ -91,8 +98,9 @@ function extractRemoteAddress(req) {
91
98
  // src/middleware-node.ts
92
99
  function withCheckpoint(config) {
93
100
  const opts = buildVerifyOpts(config);
101
+ const translateOpts = { drainJsonBody: config.drainJsonBody };
94
102
  return async function checkpointMiddleware(req) {
95
- const httpLike = nextRequestToHttpLike(req);
103
+ const httpLike = await nextRequestToHttpLike(req, translateOpts);
96
104
  const result = await orchestrator.verifyRequest(httpLike, opts);
97
105
  await dispatchOnResult(config, result, req);
98
106
  const rendered = orchestrator.renderDecisionAsResponse(result);
@@ -110,7 +118,8 @@ function buildVerifyOpts(config) {
110
118
  tenantHost: config.tenantHost,
111
119
  enforcementMode: config.enforcementMode ?? "enforce",
112
120
  reputationBaseline: config.reputationBaseline,
113
- argusUrl: config.argusUrl
121
+ argusUrl: config.argusUrl,
122
+ legacyEnvelopeFallback: config.legacyEnvelopeFallback ?? false
114
123
  };
115
124
  }
116
125
  async function dispatchOnResult(config, result, req) {
@@ -161,7 +170,7 @@ var CheckpointApiClient = class {
161
170
  debug;
162
171
  constructor(config) {
163
172
  if (!config.apiKey) {
164
- throw new Error("AgentShield API key is required");
173
+ throw new Error("Checkpoint API key is required");
165
174
  }
166
175
  this.apiKey = config.apiKey;
167
176
  this.useEdge = config.useEdge !== false;
@@ -192,7 +201,7 @@ var CheckpointApiClient = class {
192
201
  clearTimeout(timeoutId);
193
202
  const data = await response.json();
194
203
  if (this.debug) {
195
- console.log("[AgentShield] Enforce response:", {
204
+ console.log("[Checkpoint] Enforce response:", {
196
205
  status: response.status,
197
206
  action: data.data?.decision.action,
198
207
  processingTimeMs: Date.now() - startTime
@@ -215,7 +224,7 @@ var CheckpointApiClient = class {
215
224
  } catch (error) {
216
225
  if (error instanceof Error && error.name === "AbortError") {
217
226
  if (this.debug) {
218
- console.warn("[AgentShield] Request timed out");
227
+ console.warn("[Checkpoint] Request timed out");
219
228
  }
220
229
  return {
221
230
  success: false,
@@ -226,7 +235,7 @@ var CheckpointApiClient = class {
226
235
  };
227
236
  }
228
237
  if (this.debug) {
229
- console.error("[AgentShield] Request failed:", error);
238
+ console.error("[Checkpoint] Request failed:", error);
230
239
  }
231
240
  return {
232
241
  success: false,
@@ -304,7 +313,7 @@ var CheckpointApiClient = class {
304
313
  });
305
314
  clearTimeout(timeoutId);
306
315
  if (!response.ok && this.debug) {
307
- console.warn("[AgentShield] Log detection returned non-2xx:", response.status);
316
+ console.warn("[Checkpoint] Log detection returned non-2xx:", response.status);
308
317
  }
309
318
  } catch (error) {
310
319
  clearTimeout(timeoutId);
@@ -312,34 +321,50 @@ var CheckpointApiClient = class {
312
321
  }
313
322
  } catch (error) {
314
323
  if (this.debug) {
315
- console.error("[AgentShield] Log detection failed:", error);
324
+ console.error("[Checkpoint] Log detection failed:", error);
316
325
  }
317
326
  throw error;
318
327
  }
319
328
  }
320
329
  };
321
- var clientInstance = null;
330
+ var clientInstances = /* @__PURE__ */ new Map();
331
+ function resolveClientConfig(config) {
332
+ const apiKey = config?.apiKey || process.env.CHECKPOINT_API_KEY;
333
+ if (!apiKey) {
334
+ throw new Error(
335
+ "Checkpoint API key is required. Set CHECKPOINT_API_KEY environment variable or pass apiKey in config."
336
+ );
337
+ }
338
+ return {
339
+ apiKey,
340
+ baseUrl: config?.baseUrl || process.env.CHECKPOINT_API_URL,
341
+ // Default to edge detection unless explicitly disabled
342
+ useEdge: config?.useEdge ?? process.env.CHECKPOINT_USE_EDGE !== "false",
343
+ timeout: config?.timeout,
344
+ debug: config?.debug || process.env.CHECKPOINT_DEBUG === "true"
345
+ };
346
+ }
347
+ function clientCacheKey(config) {
348
+ return JSON.stringify([
349
+ config.apiKey,
350
+ config.baseUrl ?? "",
351
+ config.useEdge ?? true,
352
+ config.timeout ?? DEFAULT_TIMEOUT,
353
+ config.debug ?? false
354
+ ]);
355
+ }
322
356
  function getCheckpointApiClient(config) {
357
+ const resolvedConfig = resolveClientConfig(config);
358
+ const key = clientCacheKey(resolvedConfig);
359
+ let clientInstance = clientInstances.get(key);
323
360
  if (!clientInstance) {
324
- const apiKey = config?.apiKey || process.env.CHECKPOINT_API_KEY;
325
- if (!apiKey) {
326
- throw new Error(
327
- "AgentShield API key is required. Set CHECKPOINT_API_KEY environment variable or pass apiKey in config."
328
- );
329
- }
330
- clientInstance = new CheckpointApiClient({
331
- apiKey,
332
- baseUrl: config?.baseUrl || process.env.AGENTSHIELD_API_URL,
333
- // Default to edge detection unless explicitly disabled
334
- useEdge: config?.useEdge ?? process.env.AGENTSHIELD_USE_EDGE !== "false",
335
- timeout: config?.timeout,
336
- debug: config?.debug || process.env.AGENTSHIELD_DEBUG === "true"
337
- });
361
+ clientInstance = new CheckpointApiClient(resolvedConfig);
362
+ clientInstances.set(key, clientInstance);
338
363
  }
339
364
  return clientInstance;
340
365
  }
341
366
  function resetCheckpointApiClient() {
342
- clientInstance = null;
367
+ clientInstances.clear();
343
368
  }
344
369
  var AgentShieldClient = CheckpointApiClient;
345
370
  var getAgentShieldClient = getCheckpointApiClient;
@@ -558,7 +583,7 @@ function withCheckpointApi(config = {}) {
558
583
  });
559
584
  if (!result.success || !result.data) {
560
585
  if (config.debug) {
561
- console.warn("[AgentShield] API error:", result.error);
586
+ console.warn("[Checkpoint] API error:", result.error);
562
587
  }
563
588
  if (failOpen) {
564
589
  return server.NextResponse.next();
@@ -570,7 +595,7 @@ function withCheckpointApi(config = {}) {
570
595
  }
571
596
  const decision = result.data.decision;
572
597
  if (config.debug) {
573
- console.log("[AgentShield] Decision:", {
598
+ console.log("[Checkpoint] Decision:", {
574
599
  path,
575
600
  action: decision.action,
576
601
  isAgent: decision.isAgent,
@@ -586,12 +611,18 @@ function withCheckpointApi(config = {}) {
586
611
  context: { userAgent, ipAddress, path, url: request.url, method: request.method }
587
612
  }).catch((err) => {
588
613
  if (config.debug) {
589
- console.error("[AgentShield] Log detection failed:", err);
614
+ console.error("[Checkpoint] Log detection failed:", err);
590
615
  }
591
616
  });
592
617
  }
593
618
  if (decision.isAgent && config.onAgentDetected) {
594
- await config.onAgentDetected(request, decision);
619
+ try {
620
+ await config.onAgentDetected(request, decision);
621
+ } catch (error) {
622
+ if (config.debug) {
623
+ console.error("[Checkpoint] onAgentDetected callback failed:", error);
624
+ }
625
+ }
595
626
  }
596
627
  const redirectMode = config.redirectMode ?? "instruct";
597
628
  switch (decision.action) {
@@ -631,7 +662,7 @@ function withCheckpointApi(config = {}) {
631
662
  }
632
663
  } catch (error) {
633
664
  if (config.debug) {
634
- console.error("[AgentShield] Middleware error:", error);
665
+ console.error("[Checkpoint] Middleware error:", error);
635
666
  }
636
667
  if (failOpen) {
637
668
  return server.NextResponse.next();
@@ -659,7 +690,7 @@ var EdgeSessionTracker = class {
659
690
  cookieName: config.cookieName || "__agentshield_session",
660
691
  cookieMaxAge: config.cookieMaxAge || 3600,
661
692
  // 1 hour default
662
- encryptionKey: config.encryptionKey || process.env.AGENTSHIELD_SECRET || "agentshield-default-key"
693
+ encryptionKey: config.encryptionKey || process.env.CHECKPOINT_SECRET || "agentshield-default-key"
663
694
  };
664
695
  }
665
696
  /**
@@ -881,7 +912,7 @@ async function handlePolicyDecision(request, decision, config, detection) {
881
912
  case checkpointShared.ENFORCEMENT_ACTIONS.CHALLENGE:
882
913
  return buildChallengeResponse(request, decision, config, detection);
883
914
  case checkpointShared.ENFORCEMENT_ACTIONS.LOG:
884
- console.log("[AgentShield] Policy decision (log):", {
915
+ console.log("[Checkpoint] Policy decision (log):", {
885
916
  path: request.nextUrl.pathname,
886
917
  action: decision.action,
887
918
  reason: decision.reason,
@@ -926,7 +957,7 @@ async function getPolicy(config) {
926
957
  return await fetcher.getPolicy(config.fetchPolicy.projectId);
927
958
  } catch (error) {
928
959
  if (config.debug) {
929
- console.warn("[AgentShield] Policy fetch failed, using fallback:", error);
960
+ console.warn("[Checkpoint] Policy fetch failed, using fallback:", error);
930
961
  }
931
962
  return checkpointShared.PolicyConfigSchema.parse({
932
963
  ...checkpointShared.DEFAULT_POLICY,
@@ -951,12 +982,18 @@ async function applyPolicy(request, detection, config) {
951
982
  const context = createContextFromDetection(detection, request);
952
983
  const decision = checkpointShared.evaluatePolicy(policy, context);
953
984
  if (config.onPolicyDecision) {
954
- await config.onPolicyDecision(request, decision, context);
985
+ try {
986
+ await config.onPolicyDecision(request, decision, context);
987
+ } catch (error) {
988
+ if (config.debug) {
989
+ console.error("[Checkpoint] onPolicyDecision callback failed:", error);
990
+ }
991
+ }
955
992
  }
956
993
  return await handlePolicyDecision(request, decision, config, detection);
957
994
  } catch (error) {
958
995
  if (config.debug) {
959
- console.error("[AgentShield] Policy evaluation error:", error);
996
+ console.error("[Checkpoint] Policy evaluation error:", error);
960
997
  }
961
998
  if (config.failOpen !== false) {
962
999
  return null;
@@ -1015,5 +1052,3 @@ exports.resetCheckpointApiClient = resetCheckpointApiClient;
1015
1052
  exports.withAgentShield = withAgentShield;
1016
1053
  exports.withCheckpoint = withCheckpoint;
1017
1054
  exports.withCheckpointApi = withCheckpointApi;
1018
- //# sourceMappingURL=index.js.map
1019
- //# sourceMappingURL=index.js.map
package/dist/index.mjs CHANGED
@@ -55,21 +55,28 @@ function applyHeaders(res, headers) {
55
55
  }
56
56
 
57
57
  // src/translate.ts
58
- function nextRequestToHttpLike(req) {
58
+ async function nextRequestToHttpLike(req, opts = {}) {
59
59
  const url = new URL(req.url);
60
+ const body = await tryDrainJsonBody(req, opts);
60
61
  return {
61
62
  method: req.method,
62
63
  // Path + query only — orchestrator's URL parsing expects no scheme/host.
63
64
  url: url.pathname + url.search,
64
65
  headers: headersToRecord(req.headers),
65
- // NextRequest.body is a ReadableStream; we don't drain it here.
66
- // The orchestrator routes to PlainHttp when body is falsy, which
67
- // is the right call for streaming middlewares that don't want to
68
- // buffer the request body just to detect agents.
69
- body: null,
66
+ body,
70
67
  remoteAddress: extractRemoteAddress(req)
71
68
  };
72
69
  }
70
+ async function tryDrainJsonBody(req, opts) {
71
+ if (opts.drainJsonBody === false) return null;
72
+ const contentType = req.headers.get("content-type") ?? "";
73
+ if (!contentType.toLowerCase().includes("application/json")) return null;
74
+ try {
75
+ return await req.clone().text();
76
+ } catch {
77
+ return null;
78
+ }
79
+ }
73
80
  function headersToRecord(headers) {
74
81
  const out = {};
75
82
  headers.forEach((value, key) => {
@@ -90,8 +97,9 @@ function extractRemoteAddress(req) {
90
97
  // src/middleware-node.ts
91
98
  function withCheckpoint(config) {
92
99
  const opts = buildVerifyOpts(config);
100
+ const translateOpts = { drainJsonBody: config.drainJsonBody };
93
101
  return async function checkpointMiddleware(req) {
94
- const httpLike = nextRequestToHttpLike(req);
102
+ const httpLike = await nextRequestToHttpLike(req, translateOpts);
95
103
  const result = await verifyRequest(httpLike, opts);
96
104
  await dispatchOnResult(config, result, req);
97
105
  const rendered = renderDecisionAsResponse(result);
@@ -109,7 +117,8 @@ function buildVerifyOpts(config) {
109
117
  tenantHost: config.tenantHost,
110
118
  enforcementMode: config.enforcementMode ?? "enforce",
111
119
  reputationBaseline: config.reputationBaseline,
112
- argusUrl: config.argusUrl
120
+ argusUrl: config.argusUrl,
121
+ legacyEnvelopeFallback: config.legacyEnvelopeFallback ?? false
113
122
  };
114
123
  }
115
124
  async function dispatchOnResult(config, result, req) {
@@ -160,7 +169,7 @@ var CheckpointApiClient = class {
160
169
  debug;
161
170
  constructor(config) {
162
171
  if (!config.apiKey) {
163
- throw new Error("AgentShield API key is required");
172
+ throw new Error("Checkpoint API key is required");
164
173
  }
165
174
  this.apiKey = config.apiKey;
166
175
  this.useEdge = config.useEdge !== false;
@@ -191,7 +200,7 @@ var CheckpointApiClient = class {
191
200
  clearTimeout(timeoutId);
192
201
  const data = await response.json();
193
202
  if (this.debug) {
194
- console.log("[AgentShield] Enforce response:", {
203
+ console.log("[Checkpoint] Enforce response:", {
195
204
  status: response.status,
196
205
  action: data.data?.decision.action,
197
206
  processingTimeMs: Date.now() - startTime
@@ -214,7 +223,7 @@ var CheckpointApiClient = class {
214
223
  } catch (error) {
215
224
  if (error instanceof Error && error.name === "AbortError") {
216
225
  if (this.debug) {
217
- console.warn("[AgentShield] Request timed out");
226
+ console.warn("[Checkpoint] Request timed out");
218
227
  }
219
228
  return {
220
229
  success: false,
@@ -225,7 +234,7 @@ var CheckpointApiClient = class {
225
234
  };
226
235
  }
227
236
  if (this.debug) {
228
- console.error("[AgentShield] Request failed:", error);
237
+ console.error("[Checkpoint] Request failed:", error);
229
238
  }
230
239
  return {
231
240
  success: false,
@@ -303,7 +312,7 @@ var CheckpointApiClient = class {
303
312
  });
304
313
  clearTimeout(timeoutId);
305
314
  if (!response.ok && this.debug) {
306
- console.warn("[AgentShield] Log detection returned non-2xx:", response.status);
315
+ console.warn("[Checkpoint] Log detection returned non-2xx:", response.status);
307
316
  }
308
317
  } catch (error) {
309
318
  clearTimeout(timeoutId);
@@ -311,34 +320,50 @@ var CheckpointApiClient = class {
311
320
  }
312
321
  } catch (error) {
313
322
  if (this.debug) {
314
- console.error("[AgentShield] Log detection failed:", error);
323
+ console.error("[Checkpoint] Log detection failed:", error);
315
324
  }
316
325
  throw error;
317
326
  }
318
327
  }
319
328
  };
320
- var clientInstance = null;
329
+ var clientInstances = /* @__PURE__ */ new Map();
330
+ function resolveClientConfig(config) {
331
+ const apiKey = config?.apiKey || process.env.CHECKPOINT_API_KEY;
332
+ if (!apiKey) {
333
+ throw new Error(
334
+ "Checkpoint API key is required. Set CHECKPOINT_API_KEY environment variable or pass apiKey in config."
335
+ );
336
+ }
337
+ return {
338
+ apiKey,
339
+ baseUrl: config?.baseUrl || process.env.CHECKPOINT_API_URL,
340
+ // Default to edge detection unless explicitly disabled
341
+ useEdge: config?.useEdge ?? process.env.CHECKPOINT_USE_EDGE !== "false",
342
+ timeout: config?.timeout,
343
+ debug: config?.debug || process.env.CHECKPOINT_DEBUG === "true"
344
+ };
345
+ }
346
+ function clientCacheKey(config) {
347
+ return JSON.stringify([
348
+ config.apiKey,
349
+ config.baseUrl ?? "",
350
+ config.useEdge ?? true,
351
+ config.timeout ?? DEFAULT_TIMEOUT,
352
+ config.debug ?? false
353
+ ]);
354
+ }
321
355
  function getCheckpointApiClient(config) {
356
+ const resolvedConfig = resolveClientConfig(config);
357
+ const key = clientCacheKey(resolvedConfig);
358
+ let clientInstance = clientInstances.get(key);
322
359
  if (!clientInstance) {
323
- const apiKey = config?.apiKey || process.env.CHECKPOINT_API_KEY;
324
- if (!apiKey) {
325
- throw new Error(
326
- "AgentShield API key is required. Set CHECKPOINT_API_KEY environment variable or pass apiKey in config."
327
- );
328
- }
329
- clientInstance = new CheckpointApiClient({
330
- apiKey,
331
- baseUrl: config?.baseUrl || process.env.AGENTSHIELD_API_URL,
332
- // Default to edge detection unless explicitly disabled
333
- useEdge: config?.useEdge ?? process.env.AGENTSHIELD_USE_EDGE !== "false",
334
- timeout: config?.timeout,
335
- debug: config?.debug || process.env.AGENTSHIELD_DEBUG === "true"
336
- });
360
+ clientInstance = new CheckpointApiClient(resolvedConfig);
361
+ clientInstances.set(key, clientInstance);
337
362
  }
338
363
  return clientInstance;
339
364
  }
340
365
  function resetCheckpointApiClient() {
341
- clientInstance = null;
366
+ clientInstances.clear();
342
367
  }
343
368
  var AgentShieldClient = CheckpointApiClient;
344
369
  var getAgentShieldClient = getCheckpointApiClient;
@@ -557,7 +582,7 @@ function withCheckpointApi(config = {}) {
557
582
  });
558
583
  if (!result.success || !result.data) {
559
584
  if (config.debug) {
560
- console.warn("[AgentShield] API error:", result.error);
585
+ console.warn("[Checkpoint] API error:", result.error);
561
586
  }
562
587
  if (failOpen) {
563
588
  return NextResponse.next();
@@ -569,7 +594,7 @@ function withCheckpointApi(config = {}) {
569
594
  }
570
595
  const decision = result.data.decision;
571
596
  if (config.debug) {
572
- console.log("[AgentShield] Decision:", {
597
+ console.log("[Checkpoint] Decision:", {
573
598
  path,
574
599
  action: decision.action,
575
600
  isAgent: decision.isAgent,
@@ -585,12 +610,18 @@ function withCheckpointApi(config = {}) {
585
610
  context: { userAgent, ipAddress, path, url: request.url, method: request.method }
586
611
  }).catch((err) => {
587
612
  if (config.debug) {
588
- console.error("[AgentShield] Log detection failed:", err);
613
+ console.error("[Checkpoint] Log detection failed:", err);
589
614
  }
590
615
  });
591
616
  }
592
617
  if (decision.isAgent && config.onAgentDetected) {
593
- await config.onAgentDetected(request, decision);
618
+ try {
619
+ await config.onAgentDetected(request, decision);
620
+ } catch (error) {
621
+ if (config.debug) {
622
+ console.error("[Checkpoint] onAgentDetected callback failed:", error);
623
+ }
624
+ }
594
625
  }
595
626
  const redirectMode = config.redirectMode ?? "instruct";
596
627
  switch (decision.action) {
@@ -630,7 +661,7 @@ function withCheckpointApi(config = {}) {
630
661
  }
631
662
  } catch (error) {
632
663
  if (config.debug) {
633
- console.error("[AgentShield] Middleware error:", error);
664
+ console.error("[Checkpoint] Middleware error:", error);
634
665
  }
635
666
  if (failOpen) {
636
667
  return NextResponse.next();
@@ -658,7 +689,7 @@ var EdgeSessionTracker = class {
658
689
  cookieName: config.cookieName || "__agentshield_session",
659
690
  cookieMaxAge: config.cookieMaxAge || 3600,
660
691
  // 1 hour default
661
- encryptionKey: config.encryptionKey || process.env.AGENTSHIELD_SECRET || "agentshield-default-key"
692
+ encryptionKey: config.encryptionKey || process.env.CHECKPOINT_SECRET || "agentshield-default-key"
662
693
  };
663
694
  }
664
695
  /**
@@ -880,7 +911,7 @@ async function handlePolicyDecision(request, decision, config, detection) {
880
911
  case ENFORCEMENT_ACTIONS.CHALLENGE:
881
912
  return buildChallengeResponse(request, decision, config, detection);
882
913
  case ENFORCEMENT_ACTIONS.LOG:
883
- console.log("[AgentShield] Policy decision (log):", {
914
+ console.log("[Checkpoint] Policy decision (log):", {
884
915
  path: request.nextUrl.pathname,
885
916
  action: decision.action,
886
917
  reason: decision.reason,
@@ -925,7 +956,7 @@ async function getPolicy(config) {
925
956
  return await fetcher.getPolicy(config.fetchPolicy.projectId);
926
957
  } catch (error) {
927
958
  if (config.debug) {
928
- console.warn("[AgentShield] Policy fetch failed, using fallback:", error);
959
+ console.warn("[Checkpoint] Policy fetch failed, using fallback:", error);
929
960
  }
930
961
  return PolicyConfigSchema.parse({
931
962
  ...DEFAULT_POLICY,
@@ -950,12 +981,18 @@ async function applyPolicy(request, detection, config) {
950
981
  const context = createContextFromDetection(detection, request);
951
982
  const decision = evaluatePolicy(policy, context);
952
983
  if (config.onPolicyDecision) {
953
- await config.onPolicyDecision(request, decision, context);
984
+ try {
985
+ await config.onPolicyDecision(request, decision, context);
986
+ } catch (error) {
987
+ if (config.debug) {
988
+ console.error("[Checkpoint] onPolicyDecision callback failed:", error);
989
+ }
990
+ }
954
991
  }
955
992
  return await handlePolicyDecision(request, decision, config, detection);
956
993
  } catch (error) {
957
994
  if (config.debug) {
958
- console.error("[AgentShield] Policy evaluation error:", error);
995
+ console.error("[Checkpoint] Policy evaluation error:", error);
959
996
  }
960
997
  if (config.failOpen !== false) {
961
998
  return null;
@@ -975,5 +1012,3 @@ var VERSION = "0.1.0";
975
1012
  */
976
1013
 
977
1014
  export { AgentShieldClient, CheckpointApiClient, EdgeSessionTracker, StatelessSessionChecker, VERSION, agentShieldMiddleware, applyPolicy, buildBlockedResponse2 as buildPolicyBlockedResponse, buildRedirectResponse2 as buildPolicyRedirectResponse, createAgentShieldMiddleware2 as createAgentShieldMiddleware, createAgentShieldMiddleware as createAgentShieldMiddlewareBase, createContextFromDetection, createEnhancedAgentShieldMiddleware, createAgentShieldMiddleware2 as createMiddleware, evaluatePolicyForDetection, getAgentShieldClient, getCheckpointApiClient, getPolicy, handlePolicyDecision, resetAgentShieldClient, resetCheckpointApiClient, withAgentShield, withCheckpoint, withCheckpointApi };
978
- //# sourceMappingURL=index.mjs.map
979
- //# sourceMappingURL=index.mjs.map