@webdecoy/nextjs 0.13.0 → 0.14.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.js CHANGED
@@ -32,12 +32,7 @@ module.exports = __toCommonJS(index_exports);
32
32
  var import_server = require("next/server");
33
33
  var import_node = require("@webdecoy/node");
34
34
  function resolveIP(req, trustProxy) {
35
- const fromChain = (0, import_node.resolveClientIp)({
36
- headers: req.headers,
37
- trustProxy: trustProxy ?? 1
38
- });
39
- if (fromChain) return fromChain;
40
- return (0, import_node.normalizeIp)(req.headers.get("x-real-ip")) ?? (0, import_node.normalizeIp)(req.headers.get("x-vercel-forwarded-for")?.split(",").pop()) ?? "127.0.0.1";
35
+ return (0, import_node.resolveClientIp)({ headers: req.headers, trustProxy: trustProxy ?? 1 }) ?? "127.0.0.1";
41
36
  }
42
37
  function defaultOnBlocked(req, detection) {
43
38
  return import_server.NextResponse.json(
@@ -53,17 +48,6 @@ function defaultOnError(req, error) {
53
48
  console.error("[WebDecoy] Middleware error:", error);
54
49
  return null;
55
50
  }
56
- function shouldSkipPath(path, skipPaths) {
57
- if (!skipPaths || skipPaths.length === 0) {
58
- return false;
59
- }
60
- return skipPaths.some((pattern) => {
61
- if (typeof pattern === "string") {
62
- return path === pattern || path.startsWith(pattern);
63
- }
64
- return pattern.test(path);
65
- });
66
- }
67
51
  function withWebDecoy(config) {
68
52
  const sdk = new import_node.WebDecoy(config);
69
53
  const getIP2 = config.getIP || ((req) => resolveIP(req, config.trustProxy));
@@ -74,7 +58,7 @@ function withWebDecoy(config) {
74
58
  return async (req) => {
75
59
  try {
76
60
  const pathname = req.nextUrl.pathname;
77
- if (shouldSkipPath(pathname, skipPaths)) {
61
+ if ((0, import_node.shouldSkipPath)(pathname, skipPaths)) {
78
62
  return import_server.NextResponse.next();
79
63
  }
80
64
  const headers = {};
@@ -109,32 +93,12 @@ function withWebDecoy(config) {
109
93
  monitorHeaders.set("x-webdecoy-would-block", String(!result.allowed));
110
94
  return import_server.NextResponse.next({ request: { headers: monitorHeaders } });
111
95
  }
112
- if (!result.allowed && result.ruleResult) {
113
- const rr = result.ruleResult;
114
- if (rr.action === "THROTTLE") {
115
- const retryAfter = rr.metadata?.retryAfter ?? 60;
116
- return import_server.NextResponse.json(
117
- {
118
- error: "Too Many Requests",
119
- message: rr.reason || "Rate limit exceeded",
120
- retry_after: retryAfter
121
- },
122
- {
123
- status: 429,
124
- headers: { "Retry-After": String(retryAfter) }
125
- }
126
- );
127
- }
128
- if (rr.action === "DENY") {
129
- return import_server.NextResponse.json(
130
- {
131
- error: "Forbidden",
132
- message: rr.reason || "Access denied by rule",
133
- rule: rr.rule
134
- },
135
- { status: 403 }
136
- );
137
- }
96
+ const block = (0, import_node.ruleBlockResponse)(result);
97
+ if (block) {
98
+ return import_server.NextResponse.json(block.body, {
99
+ status: block.status,
100
+ headers: block.headers
101
+ });
138
102
  }
139
103
  if (result.allowed) {
140
104
  const requestHeaders = new Headers(req.headers);
@@ -182,14 +146,12 @@ function withBotProtection(handler, config) {
182
146
  metadata: config.metadata
183
147
  });
184
148
  if (!result.allowed) {
185
- if (result.ruleResult?.action === "THROTTLE") {
186
- const retryAfter = result.ruleResult.metadata?.retryAfter ?? 60;
187
- res.setHeader("Retry-After", String(retryAfter));
188
- return res.status(429).json({
189
- error: "Too Many Requests",
190
- message: result.ruleResult.reason || "Rate limit exceeded",
191
- retry_after: retryAfter
192
- });
149
+ const block = (0, import_node.ruleBlockResponse)(result);
150
+ if (block) {
151
+ for (const [name, value] of Object.entries(block.headers)) {
152
+ res.setHeader(name, value);
153
+ }
154
+ return res.status(block.status).json(block.body);
193
155
  }
194
156
  return res.status(403).json({
195
157
  error: "Forbidden",
@@ -239,9 +201,7 @@ function honeytokenLink(apiKey, options = {}) {
239
201
  // src/captcha.ts
240
202
  var import_node4 = require("@webdecoy/node");
241
203
  function getIP(headers, trustProxy) {
242
- const fromChain = (0, import_node4.resolveClientIp)({ headers, trustProxy: trustProxy ?? 1 });
243
- if (fromChain) return fromChain;
244
- return (0, import_node4.normalizeIp)(headers.get("x-real-ip")) ?? (0, import_node4.normalizeIp)(headers.get("x-vercel-forwarded-for")?.split(",").pop()) ?? "127.0.0.1";
204
+ return (0, import_node4.resolveClientIp)({ headers, trustProxy: trustProxy ?? 1 }) ?? "127.0.0.1";
245
205
  }
246
206
  function headersToRecord(headers) {
247
207
  const out = {};
package/dist/index.mjs CHANGED
@@ -3,15 +3,12 @@ import { NextResponse } from "next/server";
3
3
  import {
4
4
  WebDecoy,
5
5
  resolveClientIp,
6
- normalizeIp
6
+ normalizeIp,
7
+ shouldSkipPath,
8
+ ruleBlockResponse
7
9
  } from "@webdecoy/node";
8
10
  function resolveIP(req, trustProxy) {
9
- const fromChain = resolveClientIp({
10
- headers: req.headers,
11
- trustProxy: trustProxy ?? 1
12
- });
13
- if (fromChain) return fromChain;
14
- return normalizeIp(req.headers.get("x-real-ip")) ?? normalizeIp(req.headers.get("x-vercel-forwarded-for")?.split(",").pop()) ?? "127.0.0.1";
11
+ return resolveClientIp({ headers: req.headers, trustProxy: trustProxy ?? 1 }) ?? "127.0.0.1";
15
12
  }
16
13
  function defaultOnBlocked(req, detection) {
17
14
  return NextResponse.json(
@@ -27,17 +24,6 @@ function defaultOnError(req, error) {
27
24
  console.error("[WebDecoy] Middleware error:", error);
28
25
  return null;
29
26
  }
30
- function shouldSkipPath(path, skipPaths) {
31
- if (!skipPaths || skipPaths.length === 0) {
32
- return false;
33
- }
34
- return skipPaths.some((pattern) => {
35
- if (typeof pattern === "string") {
36
- return path === pattern || path.startsWith(pattern);
37
- }
38
- return pattern.test(path);
39
- });
40
- }
41
27
  function withWebDecoy(config) {
42
28
  const sdk = new WebDecoy(config);
43
29
  const getIP2 = config.getIP || ((req) => resolveIP(req, config.trustProxy));
@@ -83,32 +69,12 @@ function withWebDecoy(config) {
83
69
  monitorHeaders.set("x-webdecoy-would-block", String(!result.allowed));
84
70
  return NextResponse.next({ request: { headers: monitorHeaders } });
85
71
  }
86
- if (!result.allowed && result.ruleResult) {
87
- const rr = result.ruleResult;
88
- if (rr.action === "THROTTLE") {
89
- const retryAfter = rr.metadata?.retryAfter ?? 60;
90
- return NextResponse.json(
91
- {
92
- error: "Too Many Requests",
93
- message: rr.reason || "Rate limit exceeded",
94
- retry_after: retryAfter
95
- },
96
- {
97
- status: 429,
98
- headers: { "Retry-After": String(retryAfter) }
99
- }
100
- );
101
- }
102
- if (rr.action === "DENY") {
103
- return NextResponse.json(
104
- {
105
- error: "Forbidden",
106
- message: rr.reason || "Access denied by rule",
107
- rule: rr.rule
108
- },
109
- { status: 403 }
110
- );
111
- }
72
+ const block = ruleBlockResponse(result);
73
+ if (block) {
74
+ return NextResponse.json(block.body, {
75
+ status: block.status,
76
+ headers: block.headers
77
+ });
112
78
  }
113
79
  if (result.allowed) {
114
80
  const requestHeaders = new Headers(req.headers);
@@ -156,14 +122,12 @@ function withBotProtection(handler, config) {
156
122
  metadata: config.metadata
157
123
  });
158
124
  if (!result.allowed) {
159
- if (result.ruleResult?.action === "THROTTLE") {
160
- const retryAfter = result.ruleResult.metadata?.retryAfter ?? 60;
161
- res.setHeader("Retry-After", String(retryAfter));
162
- return res.status(429).json({
163
- error: "Too Many Requests",
164
- message: result.ruleResult.reason || "Rate limit exceeded",
165
- retry_after: retryAfter
166
- });
125
+ const block = ruleBlockResponse(result);
126
+ if (block) {
127
+ for (const [name, value] of Object.entries(block.headers)) {
128
+ res.setHeader(name, value);
129
+ }
130
+ return res.status(block.status).json(block.body);
167
131
  }
168
132
  return res.status(403).json({
169
133
  error: "Forbidden",
@@ -213,13 +177,10 @@ function honeytokenLink(apiKey, options = {}) {
213
177
  // src/captcha.ts
214
178
  import {
215
179
  createCaptchaEndpoints,
216
- resolveClientIp as resolveClientIp2,
217
- normalizeIp as normalizeIp2
180
+ resolveClientIp as resolveClientIp2
218
181
  } from "@webdecoy/node";
219
182
  function getIP(headers, trustProxy) {
220
- const fromChain = resolveClientIp2({ headers, trustProxy: trustProxy ?? 1 });
221
- if (fromChain) return fromChain;
222
- return normalizeIp2(headers.get("x-real-ip")) ?? normalizeIp2(headers.get("x-vercel-forwarded-for")?.split(",").pop()) ?? "127.0.0.1";
183
+ return resolveClientIp2({ headers, trustProxy: trustProxy ?? 1 }) ?? "127.0.0.1";
223
184
  }
224
185
  function headersToRecord(headers) {
225
186
  const out = {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@webdecoy/nextjs",
3
- "version": "0.13.0",
3
+ "version": "0.14.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.13.0"
45
+ "@webdecoy/node": "^0.14.0"
46
46
  },
47
47
  "peerDependencies": {
48
48
  "next": ">=13.0.0"