@webdecoy/nextjs 0.19.1 → 0.21.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
- import { NextRequest, NextResponse } from 'next/server';
2
- import { ProtectOptions, TrustedProxies, SDKDetectionResponse, ProtectResult, WebDecoyConfig, EdgeVerdict, SiteHoneytoken, CaptchaEndpointsOptions } from '@webdecoy/node';
3
- export { CaptchaEndpointsOptions, EdgeClass, EdgeVerdict, ProtectResult, RequestMetadata, SDKDetectionResponse, SiteHoneytoken, WebDecoyConfig } from '@webdecoy/node';
1
+ import { NextRequest, NextResponse, NextFetchEvent } from 'next/server';
2
+ import { ProtectOptions, TrustedProxies, SDKDetectionResponse, ProtectResult, WebDecoyConfig, ClearanceGateOptions, EdgeVerdict, SiteHoneytoken, CaptchaEndpointsOptions } from '@webdecoy/node';
3
+ export { CaptchaEndpointsOptions, ClearanceGateOptions, EdgeClass, EdgeVerdict, ProtectResult, RequestMetadata, SDKDetectionResponse, SiteHoneytoken, WebDecoyConfig } from '@webdecoy/node';
4
4
 
5
5
  /**
6
6
  * Web Decoy Next.js Middleware
@@ -124,6 +124,35 @@ interface WithBotProtectionOptions extends WebDecoyMiddlewareOptions {
124
124
  */
125
125
  declare function withBotProtection<T extends (...args: any[]) => any>(handler: T, config: WebDecoyConfig & WithBotProtectionOptions): T;
126
126
 
127
+ /**
128
+ * WebDecoy clearance in Next.js middleware.
129
+ *
130
+ * The clearance gate from @webdecoy/node, the same decision WebDecoy's
131
+ * Cloudflare, AWS and Netlify validators make, run on Vercel (or any host
132
+ * running Next.js middleware) with no proxy in front. Which paths need a
133
+ * token, and whether the site monitors or enforces, is set in WebDecoy.
134
+ *
135
+ * @example
136
+ * ```typescript
137
+ * // middleware.ts
138
+ * import { withClearance } from '@webdecoy/nextjs';
139
+ *
140
+ * // Reads WEBDECOY_SITE_KEY, WEBDECOY_SCANNER_ID and WEBDECOY_SENSOR_KEY.
141
+ * export default withClearance();
142
+ *
143
+ * export const config = {
144
+ * // Everything but static assets. Keep /.well-known: the challenge uses it.
145
+ * matcher: ['/((?!_next/static|_next/image|favicon.ico).*)'],
146
+ * };
147
+ * ```
148
+ *
149
+ * A page the gate lets through receives the verdict as `x-wd-clearance`;
150
+ * `getEdgeVerdict()` reads it in a route handler or server component.
151
+ */
152
+
153
+ /** Next.js middleware that runs the clearance gate. Options default to the environment. */
154
+ declare function withClearance(options?: ClearanceGateOptions): (req: NextRequest, event?: NextFetchEvent) => Promise<Response>;
155
+
127
156
  /**
128
157
  * Read the edge validator's verdict inside a Next.js app.
129
158
  *
@@ -271,4 +300,4 @@ interface NextCaptchaHandlers {
271
300
  }
272
301
  declare function createCaptchaHandler(options?: NextCaptchaOptions): NextCaptchaHandlers;
273
302
 
274
- export { type NextCaptchaHandlers, type NextCaptchaOptions, type WebDecoyMiddlewareOptions, type WithBotProtectionOptions, createCaptchaHandler, getEdgeVerdict, honeytokenLink, withBotProtection, withWebDecoy };
303
+ export { type NextCaptchaHandlers, type NextCaptchaOptions, type WebDecoyMiddlewareOptions, type WithBotProtectionOptions, createCaptchaHandler, getEdgeVerdict, honeytokenLink, withBotProtection, withClearance, withWebDecoy };
package/dist/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
- import { NextRequest, NextResponse } from 'next/server';
2
- import { ProtectOptions, TrustedProxies, SDKDetectionResponse, ProtectResult, WebDecoyConfig, EdgeVerdict, SiteHoneytoken, CaptchaEndpointsOptions } from '@webdecoy/node';
3
- export { CaptchaEndpointsOptions, EdgeClass, EdgeVerdict, ProtectResult, RequestMetadata, SDKDetectionResponse, SiteHoneytoken, WebDecoyConfig } from '@webdecoy/node';
1
+ import { NextRequest, NextResponse, NextFetchEvent } from 'next/server';
2
+ import { ProtectOptions, TrustedProxies, SDKDetectionResponse, ProtectResult, WebDecoyConfig, ClearanceGateOptions, EdgeVerdict, SiteHoneytoken, CaptchaEndpointsOptions } from '@webdecoy/node';
3
+ export { CaptchaEndpointsOptions, ClearanceGateOptions, EdgeClass, EdgeVerdict, ProtectResult, RequestMetadata, SDKDetectionResponse, SiteHoneytoken, WebDecoyConfig } from '@webdecoy/node';
4
4
 
5
5
  /**
6
6
  * Web Decoy Next.js Middleware
@@ -124,6 +124,35 @@ interface WithBotProtectionOptions extends WebDecoyMiddlewareOptions {
124
124
  */
125
125
  declare function withBotProtection<T extends (...args: any[]) => any>(handler: T, config: WebDecoyConfig & WithBotProtectionOptions): T;
126
126
 
127
+ /**
128
+ * WebDecoy clearance in Next.js middleware.
129
+ *
130
+ * The clearance gate from @webdecoy/node, the same decision WebDecoy's
131
+ * Cloudflare, AWS and Netlify validators make, run on Vercel (or any host
132
+ * running Next.js middleware) with no proxy in front. Which paths need a
133
+ * token, and whether the site monitors or enforces, is set in WebDecoy.
134
+ *
135
+ * @example
136
+ * ```typescript
137
+ * // middleware.ts
138
+ * import { withClearance } from '@webdecoy/nextjs';
139
+ *
140
+ * // Reads WEBDECOY_SITE_KEY, WEBDECOY_SCANNER_ID and WEBDECOY_SENSOR_KEY.
141
+ * export default withClearance();
142
+ *
143
+ * export const config = {
144
+ * // Everything but static assets. Keep /.well-known: the challenge uses it.
145
+ * matcher: ['/((?!_next/static|_next/image|favicon.ico).*)'],
146
+ * };
147
+ * ```
148
+ *
149
+ * A page the gate lets through receives the verdict as `x-wd-clearance`;
150
+ * `getEdgeVerdict()` reads it in a route handler or server component.
151
+ */
152
+
153
+ /** Next.js middleware that runs the clearance gate. Options default to the environment. */
154
+ declare function withClearance(options?: ClearanceGateOptions): (req: NextRequest, event?: NextFetchEvent) => Promise<Response>;
155
+
127
156
  /**
128
157
  * Read the edge validator's verdict inside a Next.js app.
129
158
  *
@@ -271,4 +300,4 @@ interface NextCaptchaHandlers {
271
300
  }
272
301
  declare function createCaptchaHandler(options?: NextCaptchaOptions): NextCaptchaHandlers;
273
302
 
274
- export { type NextCaptchaHandlers, type NextCaptchaOptions, type WebDecoyMiddlewareOptions, type WithBotProtectionOptions, createCaptchaHandler, getEdgeVerdict, honeytokenLink, withBotProtection, withWebDecoy };
303
+ export { type NextCaptchaHandlers, type NextCaptchaOptions, type WebDecoyMiddlewareOptions, type WithBotProtectionOptions, createCaptchaHandler, getEdgeVerdict, honeytokenLink, withBotProtection, withClearance, withWebDecoy };
package/dist/index.js CHANGED
@@ -24,6 +24,7 @@ __export(index_exports, {
24
24
  getEdgeVerdict: () => getEdgeVerdict,
25
25
  honeytokenLink: () => honeytokenLink,
26
26
  withBotProtection: () => withBotProtection,
27
+ withClearance: () => withClearance,
27
28
  withWebDecoy: () => withWebDecoy
28
29
  });
29
30
  module.exports = __toCommonJS(index_exports);
@@ -170,8 +171,25 @@ function withBotProtection(handler, config) {
170
171
  });
171
172
  }
172
173
 
173
- // src/edge-verdict.ts
174
+ // src/clearance.ts
175
+ var import_server2 = require("next/server");
174
176
  var import_node2 = require("@webdecoy/node");
177
+ function withClearance(options = {}) {
178
+ const gate = (0, import_node2.createClearanceGate)(options);
179
+ return async (req, event) => {
180
+ try {
181
+ const outcome = await gate(req, { waitUntil: event ? (p) => event.waitUntil(p) : void 0 });
182
+ if (outcome.action === "respond") return outcome.response;
183
+ if (!outcome.headers) return import_server2.NextResponse.next();
184
+ return import_server2.NextResponse.next({ request: { headers: outcome.headers } });
185
+ } catch {
186
+ return import_server2.NextResponse.next();
187
+ }
188
+ };
189
+ }
190
+
191
+ // src/edge-verdict.ts
192
+ var import_node3 = require("@webdecoy/node");
175
193
  var EDGE_HEADERS = ["x-wd-clearance", "x-wd-class"];
176
194
  function bagFrom(source) {
177
195
  const getter = "headers" in source && !("get" in source) ? source.headers : source;
@@ -183,26 +201,26 @@ function bagFrom(source) {
183
201
  return bag;
184
202
  }
185
203
  function getEdgeVerdict(source) {
186
- return (0, import_node2.readEdgeVerdict)(bagFrom(source));
204
+ return (0, import_node3.readEdgeVerdict)(bagFrom(source));
187
205
  }
188
206
 
189
207
  // src/honeytoken.ts
190
- var import_node3 = require("@webdecoy/node");
208
+ var import_node4 = require("@webdecoy/node");
191
209
  var cache = /* @__PURE__ */ new Map();
192
210
  function honeytokenLink(apiKey, options = {}) {
193
211
  const key = `${apiKey}|${options.rotate ? "rot" : "stable"}|${options.text ?? "."}`;
194
212
  let existing = cache.get(key);
195
213
  if (!existing) {
196
- existing = (0, import_node3.siteHoneytoken)({ secret: apiKey, ...options });
214
+ existing = (0, import_node4.siteHoneytoken)({ secret: apiKey, ...options });
197
215
  cache.set(key, existing);
198
216
  }
199
217
  return existing;
200
218
  }
201
219
 
202
220
  // src/captcha.ts
203
- var import_node4 = require("@webdecoy/node");
221
+ var import_node5 = require("@webdecoy/node");
204
222
  function getIP(headers, trustProxy) {
205
- return (0, import_node4.resolveClientIp)({ headers, trustProxy: trustProxy ?? 1 }) ?? "127.0.0.1";
223
+ return (0, import_node5.resolveClientIp)({ headers, trustProxy: trustProxy ?? 1 }) ?? "127.0.0.1";
206
224
  }
207
225
  function headersToRecord(headers) {
208
226
  const out = {};
@@ -212,7 +230,7 @@ function headersToRecord(headers) {
212
230
  return out;
213
231
  }
214
232
  function createCaptchaHandler(options) {
215
- const endpoints = (0, import_node4.createCaptchaEndpoints)(options);
233
+ const endpoints = (0, import_node5.createCaptchaEndpoints)(options);
216
234
  async function handler(req) {
217
235
  const url = new URL(req.url);
218
236
  const query = {};
@@ -255,5 +273,6 @@ function createCaptchaHandler(options) {
255
273
  getEdgeVerdict,
256
274
  honeytokenLink,
257
275
  withBotProtection,
276
+ withClearance,
258
277
  withWebDecoy
259
278
  });
package/dist/index.mjs CHANGED
@@ -146,6 +146,23 @@ function withBotProtection(handler, config) {
146
146
  });
147
147
  }
148
148
 
149
+ // src/clearance.ts
150
+ import { NextResponse as NextResponse2 } from "next/server";
151
+ import { createClearanceGate } from "@webdecoy/node";
152
+ function withClearance(options = {}) {
153
+ const gate = createClearanceGate(options);
154
+ return async (req, event) => {
155
+ try {
156
+ const outcome = await gate(req, { waitUntil: event ? (p) => event.waitUntil(p) : void 0 });
157
+ if (outcome.action === "respond") return outcome.response;
158
+ if (!outcome.headers) return NextResponse2.next();
159
+ return NextResponse2.next({ request: { headers: outcome.headers } });
160
+ } catch {
161
+ return NextResponse2.next();
162
+ }
163
+ };
164
+ }
165
+
149
166
  // src/edge-verdict.ts
150
167
  import { readEdgeVerdict } from "@webdecoy/node";
151
168
  var EDGE_HEADERS = ["x-wd-clearance", "x-wd-class"];
@@ -233,5 +250,6 @@ export {
233
250
  getEdgeVerdict,
234
251
  honeytokenLink,
235
252
  withBotProtection,
253
+ withClearance,
236
254
  withWebDecoy
237
255
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@webdecoy/nextjs",
3
- "version": "0.19.1",
3
+ "version": "0.21.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.19.1"
45
+ "@webdecoy/node": "^0.21.0"
46
46
  },
47
47
  "peerDependencies": {
48
48
  "next": ">=13.0.0"