@webdecoy/nextjs 0.5.0 → 0.6.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/index.d.mts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { NextRequest, NextResponse } from 'next/server';
2
- import { ProtectOptions, WebDecoyConfig, CaptchaEndpointsOptions } from '@webdecoy/node';
3
- export { CaptchaEndpointsOptions, ProtectResult, RequestMetadata, SDKDetectionResponse, WebDecoyConfig } from '@webdecoy/node';
2
+ import { ProtectOptions, WebDecoyConfig, EdgeVerdict, CaptchaEndpointsOptions } from '@webdecoy/node';
3
+ export { CaptchaEndpointsOptions, EdgeClass, EdgeVerdict, ProtectResult, RequestMetadata, SDKDetectionResponse, WebDecoyConfig } from '@webdecoy/node';
4
4
 
5
5
  /**
6
6
  * Web Decoy Next.js Middleware
@@ -79,6 +79,62 @@ interface WithBotProtectionOptions extends WebDecoyMiddlewareOptions {
79
79
  */
80
80
  declare function withBotProtection<T extends (...args: any[]) => any>(handler: T, config: WebDecoyConfig & WithBotProtectionOptions): T;
81
81
 
82
+ /**
83
+ * Read the edge validator's verdict inside a Next.js app (#481).
84
+ *
85
+ * The Cloudflare clearance worker annotates every forwarded request with
86
+ * `x-wd-clearance` and, when the sensor classified the client, `x-wd-class`.
87
+ * Nothing in this SDK read either header until now, so the only thing an
88
+ * application could do with a scoped route was let the edge 403 it — which on
89
+ * public content means 403ing every client that cannot run JavaScript.
90
+ *
91
+ * This is the read side. It works in middleware, route handlers, server
92
+ * components and server actions, because all it needs is a header bag.
93
+ */
94
+
95
+ /**
96
+ * Anything that can hand us request headers: a `NextRequest`, a `Request`, the
97
+ * `Headers` object, or the result of `await headers()` in a server component.
98
+ */
99
+ type HeaderSource = {
100
+ headers: Headers;
101
+ } | Headers | {
102
+ get(name: string): string | null;
103
+ };
104
+ /**
105
+ * Parse the edge validator's annotations from a request.
106
+ *
107
+ * @example
108
+ * ```typescript
109
+ * // app/api/search/route.ts
110
+ * import { getEdgeVerdict } from '@webdecoy/nextjs';
111
+ *
112
+ * export async function GET(req: Request) {
113
+ * const edge = getEdgeVerdict(req);
114
+ *
115
+ * // Skip the expensive path for scripted traffic — without 403ing it, and
116
+ * // without touching what a verified crawler sees.
117
+ * if (edge.isScript) {
118
+ * return Response.json(await cheapResults(), {
119
+ * headers: { 'cache-control': 'private, no-store' },
120
+ * });
121
+ * }
122
+ * return Response.json(await fullResults());
123
+ * }
124
+ * ```
125
+ *
126
+ * `present: false` means the edge did not front this request. That is no
127
+ * information — not "human". Do not treat it as a pass.
128
+ *
129
+ * IMPORTANT, and the reason this returns data rather than a response: do NOT vary
130
+ * a **cacheable response body** on this. Cloudflare's default cache key excludes
131
+ * arbitrary request headers and header-based cache keys are Enterprise-only, so
132
+ * the first variant cached for a URL is served to everyone, Googlebot included —
133
+ * and on a cache hit your origin never runs at all. Vary behaviour, or mark the
134
+ * response private, as above.
135
+ */
136
+ declare function getEdgeVerdict(source: HeaderSource): EdgeVerdict;
137
+
82
138
  /**
83
139
  * Next.js App Router captcha route handlers.
84
140
  *
@@ -99,4 +155,4 @@ interface NextCaptchaHandlers {
99
155
  }
100
156
  declare function createCaptchaHandler(options?: CaptchaEndpointsOptions): NextCaptchaHandlers;
101
157
 
102
- export { type NextCaptchaHandlers, type WebDecoyMiddlewareOptions, type WithBotProtectionOptions, createCaptchaHandler, withBotProtection, withWebDecoy };
158
+ export { type NextCaptchaHandlers, type WebDecoyMiddlewareOptions, type WithBotProtectionOptions, createCaptchaHandler, getEdgeVerdict, withBotProtection, withWebDecoy };
package/dist/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { NextRequest, NextResponse } from 'next/server';
2
- import { ProtectOptions, WebDecoyConfig, CaptchaEndpointsOptions } from '@webdecoy/node';
3
- export { CaptchaEndpointsOptions, ProtectResult, RequestMetadata, SDKDetectionResponse, WebDecoyConfig } from '@webdecoy/node';
2
+ import { ProtectOptions, WebDecoyConfig, EdgeVerdict, CaptchaEndpointsOptions } from '@webdecoy/node';
3
+ export { CaptchaEndpointsOptions, EdgeClass, EdgeVerdict, ProtectResult, RequestMetadata, SDKDetectionResponse, WebDecoyConfig } from '@webdecoy/node';
4
4
 
5
5
  /**
6
6
  * Web Decoy Next.js Middleware
@@ -79,6 +79,62 @@ interface WithBotProtectionOptions extends WebDecoyMiddlewareOptions {
79
79
  */
80
80
  declare function withBotProtection<T extends (...args: any[]) => any>(handler: T, config: WebDecoyConfig & WithBotProtectionOptions): T;
81
81
 
82
+ /**
83
+ * Read the edge validator's verdict inside a Next.js app (#481).
84
+ *
85
+ * The Cloudflare clearance worker annotates every forwarded request with
86
+ * `x-wd-clearance` and, when the sensor classified the client, `x-wd-class`.
87
+ * Nothing in this SDK read either header until now, so the only thing an
88
+ * application could do with a scoped route was let the edge 403 it — which on
89
+ * public content means 403ing every client that cannot run JavaScript.
90
+ *
91
+ * This is the read side. It works in middleware, route handlers, server
92
+ * components and server actions, because all it needs is a header bag.
93
+ */
94
+
95
+ /**
96
+ * Anything that can hand us request headers: a `NextRequest`, a `Request`, the
97
+ * `Headers` object, or the result of `await headers()` in a server component.
98
+ */
99
+ type HeaderSource = {
100
+ headers: Headers;
101
+ } | Headers | {
102
+ get(name: string): string | null;
103
+ };
104
+ /**
105
+ * Parse the edge validator's annotations from a request.
106
+ *
107
+ * @example
108
+ * ```typescript
109
+ * // app/api/search/route.ts
110
+ * import { getEdgeVerdict } from '@webdecoy/nextjs';
111
+ *
112
+ * export async function GET(req: Request) {
113
+ * const edge = getEdgeVerdict(req);
114
+ *
115
+ * // Skip the expensive path for scripted traffic — without 403ing it, and
116
+ * // without touching what a verified crawler sees.
117
+ * if (edge.isScript) {
118
+ * return Response.json(await cheapResults(), {
119
+ * headers: { 'cache-control': 'private, no-store' },
120
+ * });
121
+ * }
122
+ * return Response.json(await fullResults());
123
+ * }
124
+ * ```
125
+ *
126
+ * `present: false` means the edge did not front this request. That is no
127
+ * information — not "human". Do not treat it as a pass.
128
+ *
129
+ * IMPORTANT, and the reason this returns data rather than a response: do NOT vary
130
+ * a **cacheable response body** on this. Cloudflare's default cache key excludes
131
+ * arbitrary request headers and header-based cache keys are Enterprise-only, so
132
+ * the first variant cached for a URL is served to everyone, Googlebot included —
133
+ * and on a cache hit your origin never runs at all. Vary behaviour, or mark the
134
+ * response private, as above.
135
+ */
136
+ declare function getEdgeVerdict(source: HeaderSource): EdgeVerdict;
137
+
82
138
  /**
83
139
  * Next.js App Router captcha route handlers.
84
140
  *
@@ -99,4 +155,4 @@ interface NextCaptchaHandlers {
99
155
  }
100
156
  declare function createCaptchaHandler(options?: CaptchaEndpointsOptions): NextCaptchaHandlers;
101
157
 
102
- export { type NextCaptchaHandlers, type WebDecoyMiddlewareOptions, type WithBotProtectionOptions, createCaptchaHandler, withBotProtection, withWebDecoy };
158
+ export { type NextCaptchaHandlers, type WebDecoyMiddlewareOptions, type WithBotProtectionOptions, createCaptchaHandler, getEdgeVerdict, withBotProtection, withWebDecoy };
package/dist/index.js CHANGED
@@ -21,6 +21,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
21
21
  var index_exports = {};
22
22
  __export(index_exports, {
23
23
  createCaptchaHandler: () => createCaptchaHandler,
24
+ getEdgeVerdict: () => getEdgeVerdict,
24
25
  withBotProtection: () => withBotProtection,
25
26
  withWebDecoy: () => withWebDecoy
26
27
  });
@@ -126,12 +127,14 @@ function withWebDecoy(config) {
126
127
  }
127
128
  }
128
129
  if (result.allowed) {
129
- const response = import_server.NextResponse.next();
130
+ const requestHeaders = new Headers(req.headers);
131
+ requestHeaders.delete("x-webdecoy-decision");
132
+ requestHeaders.delete("x-webdecoy-detection-id");
130
133
  if (result.detection) {
131
- response.headers.set("x-webdecoy-decision", result.detection.decision || "");
132
- response.headers.set("x-webdecoy-detection-id", result.detection.detection_id || "");
134
+ requestHeaders.set("x-webdecoy-decision", result.detection.decision || "");
135
+ requestHeaders.set("x-webdecoy-detection-id", result.detection.detection_id || "");
133
136
  }
134
- return response;
137
+ return import_server.NextResponse.next({ request: { headers: requestHeaders } });
135
138
  } else {
136
139
  return onBlocked(req, result.detection);
137
140
  }
@@ -189,8 +192,24 @@ function withBotProtection(handler, config) {
189
192
  });
190
193
  }
191
194
 
192
- // src/captcha.ts
195
+ // src/edge-verdict.ts
193
196
  var import_node2 = require("@webdecoy/node");
197
+ var EDGE_HEADERS = ["x-wd-clearance", "x-wd-class"];
198
+ function bagFrom(source) {
199
+ const getter = "headers" in source && !("get" in source) ? source.headers : source;
200
+ const bag = {};
201
+ for (const name of EDGE_HEADERS) {
202
+ const value = getter.get(name);
203
+ if (value) bag[name] = value;
204
+ }
205
+ return bag;
206
+ }
207
+ function getEdgeVerdict(source) {
208
+ return (0, import_node2.readEdgeVerdict)(bagFrom(source));
209
+ }
210
+
211
+ // src/captcha.ts
212
+ var import_node3 = require("@webdecoy/node");
194
213
  function getIP(headers) {
195
214
  const forwarded = headers.get("x-forwarded-for");
196
215
  if (forwarded) return forwarded.split(",")[0].trim();
@@ -204,7 +223,7 @@ function headersToRecord(headers) {
204
223
  return out;
205
224
  }
206
225
  function createCaptchaHandler(options) {
207
- const endpoints = (0, import_node2.createCaptchaEndpoints)(options);
226
+ const endpoints = (0, import_node3.createCaptchaEndpoints)(options);
208
227
  async function handler(req) {
209
228
  const url = new URL(req.url);
210
229
  const query = {};
@@ -244,6 +263,7 @@ function createCaptchaHandler(options) {
244
263
  // Annotate the CommonJS export names for ESM import in node:
245
264
  0 && (module.exports = {
246
265
  createCaptchaHandler,
266
+ getEdgeVerdict,
247
267
  withBotProtection,
248
268
  withWebDecoy
249
269
  });
package/dist/index.mjs CHANGED
@@ -98,12 +98,14 @@ function withWebDecoy(config) {
98
98
  }
99
99
  }
100
100
  if (result.allowed) {
101
- const response = NextResponse.next();
101
+ const requestHeaders = new Headers(req.headers);
102
+ requestHeaders.delete("x-webdecoy-decision");
103
+ requestHeaders.delete("x-webdecoy-detection-id");
102
104
  if (result.detection) {
103
- response.headers.set("x-webdecoy-decision", result.detection.decision || "");
104
- response.headers.set("x-webdecoy-detection-id", result.detection.detection_id || "");
105
+ requestHeaders.set("x-webdecoy-decision", result.detection.decision || "");
106
+ requestHeaders.set("x-webdecoy-detection-id", result.detection.detection_id || "");
105
107
  }
106
- return response;
108
+ return NextResponse.next({ request: { headers: requestHeaders } });
107
109
  } else {
108
110
  return onBlocked(req, result.detection);
109
111
  }
@@ -161,6 +163,22 @@ function withBotProtection(handler, config) {
161
163
  });
162
164
  }
163
165
 
166
+ // src/edge-verdict.ts
167
+ import { readEdgeVerdict } from "@webdecoy/node";
168
+ var EDGE_HEADERS = ["x-wd-clearance", "x-wd-class"];
169
+ function bagFrom(source) {
170
+ const getter = "headers" in source && !("get" in source) ? source.headers : source;
171
+ const bag = {};
172
+ for (const name of EDGE_HEADERS) {
173
+ const value = getter.get(name);
174
+ if (value) bag[name] = value;
175
+ }
176
+ return bag;
177
+ }
178
+ function getEdgeVerdict(source) {
179
+ return readEdgeVerdict(bagFrom(source));
180
+ }
181
+
164
182
  // src/captcha.ts
165
183
  import { createCaptchaEndpoints } from "@webdecoy/node";
166
184
  function getIP(headers) {
@@ -215,6 +233,7 @@ function createCaptchaHandler(options) {
215
233
  }
216
234
  export {
217
235
  createCaptchaHandler,
236
+ getEdgeVerdict,
218
237
  withBotProtection,
219
238
  withWebDecoy
220
239
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@webdecoy/nextjs",
3
- "version": "0.5.0",
3
+ "version": "0.6.0",
4
4
  "description": "Web Decoy middleware for Next.js",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
@@ -42,7 +42,7 @@
42
42
  "url": "https://github.com/WebDecoy/node/issues"
43
43
  },
44
44
  "dependencies": {
45
- "@webdecoy/node": "^0.5.0"
45
+ "@webdecoy/node": "^0.6.0"
46
46
  },
47
47
  "peerDependencies": {
48
48
  "next": ">=13.0.0"