@webdecoy/nextjs 0.2.1 → 0.4.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 +23 -3
- package/dist/index.d.ts +23 -3
- package/dist/index.js +57 -2
- package/dist/index.mjs +56 -2
- package/package.json +2 -2
package/dist/index.d.mts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { NextRequest, NextResponse } from 'next/server';
|
|
2
|
-
import { ProtectOptions, WebDecoyConfig } from '@webdecoy/node';
|
|
3
|
-
export { ProtectResult, RequestMetadata, SDKDetectionResponse, WebDecoyConfig } from '@webdecoy/node';
|
|
2
|
+
import { ProtectOptions, WebDecoyConfig, CaptchaEndpointsOptions } from '@webdecoy/node';
|
|
3
|
+
export { CaptchaEndpointsOptions, ProtectResult, RequestMetadata, SDKDetectionResponse, WebDecoyConfig } from '@webdecoy/node';
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* Web Decoy Next.js Middleware
|
|
@@ -79,4 +79,24 @@ 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
|
-
|
|
82
|
+
/**
|
|
83
|
+
* Next.js App Router captcha route handlers.
|
|
84
|
+
*
|
|
85
|
+
* Returns Web `Request` → `Response` handlers for a catch-all route. Mount at
|
|
86
|
+
* `app/__webdecoy/[...webdecoy]/route.ts`:
|
|
87
|
+
*
|
|
88
|
+
* @example
|
|
89
|
+
* ```ts
|
|
90
|
+
* // app/__webdecoy/[...webdecoy]/route.ts
|
|
91
|
+
* import { createCaptchaHandler } from '@webdecoy/nextjs';
|
|
92
|
+
* export const { GET, POST } = createCaptchaHandler({ secret: process.env.WEBDECOY_SECRET });
|
|
93
|
+
* ```
|
|
94
|
+
*/
|
|
95
|
+
|
|
96
|
+
interface NextCaptchaHandlers {
|
|
97
|
+
GET: (req: Request) => Promise<Response>;
|
|
98
|
+
POST: (req: Request) => Promise<Response>;
|
|
99
|
+
}
|
|
100
|
+
declare function createCaptchaHandler(options?: CaptchaEndpointsOptions): NextCaptchaHandlers;
|
|
101
|
+
|
|
102
|
+
export { type NextCaptchaHandlers, type WebDecoyMiddlewareOptions, type WithBotProtectionOptions, createCaptchaHandler, withBotProtection, withWebDecoy };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { NextRequest, NextResponse } from 'next/server';
|
|
2
|
-
import { ProtectOptions, WebDecoyConfig } from '@webdecoy/node';
|
|
3
|
-
export { ProtectResult, RequestMetadata, SDKDetectionResponse, WebDecoyConfig } from '@webdecoy/node';
|
|
2
|
+
import { ProtectOptions, WebDecoyConfig, CaptchaEndpointsOptions } from '@webdecoy/node';
|
|
3
|
+
export { CaptchaEndpointsOptions, ProtectResult, RequestMetadata, SDKDetectionResponse, WebDecoyConfig } from '@webdecoy/node';
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* Web Decoy Next.js Middleware
|
|
@@ -79,4 +79,24 @@ 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
|
-
|
|
82
|
+
/**
|
|
83
|
+
* Next.js App Router captcha route handlers.
|
|
84
|
+
*
|
|
85
|
+
* Returns Web `Request` → `Response` handlers for a catch-all route. Mount at
|
|
86
|
+
* `app/__webdecoy/[...webdecoy]/route.ts`:
|
|
87
|
+
*
|
|
88
|
+
* @example
|
|
89
|
+
* ```ts
|
|
90
|
+
* // app/__webdecoy/[...webdecoy]/route.ts
|
|
91
|
+
* import { createCaptchaHandler } from '@webdecoy/nextjs';
|
|
92
|
+
* export const { GET, POST } = createCaptchaHandler({ secret: process.env.WEBDECOY_SECRET });
|
|
93
|
+
* ```
|
|
94
|
+
*/
|
|
95
|
+
|
|
96
|
+
interface NextCaptchaHandlers {
|
|
97
|
+
GET: (req: Request) => Promise<Response>;
|
|
98
|
+
POST: (req: Request) => Promise<Response>;
|
|
99
|
+
}
|
|
100
|
+
declare function createCaptchaHandler(options?: CaptchaEndpointsOptions): NextCaptchaHandlers;
|
|
101
|
+
|
|
102
|
+
export { type NextCaptchaHandlers, type WebDecoyMiddlewareOptions, type WithBotProtectionOptions, createCaptchaHandler, withBotProtection, withWebDecoy };
|
package/dist/index.js
CHANGED
|
@@ -20,6 +20,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
20
20
|
// src/index.ts
|
|
21
21
|
var index_exports = {};
|
|
22
22
|
__export(index_exports, {
|
|
23
|
+
createCaptchaHandler: () => createCaptchaHandler,
|
|
23
24
|
withBotProtection: () => withBotProtection,
|
|
24
25
|
withWebDecoy: () => withWebDecoy
|
|
25
26
|
});
|
|
@@ -70,7 +71,7 @@ function shouldSkipPath(path, skipPaths) {
|
|
|
70
71
|
}
|
|
71
72
|
function withWebDecoy(config) {
|
|
72
73
|
const sdk = new import_node.WebDecoy(config);
|
|
73
|
-
const
|
|
74
|
+
const getIP2 = config.getIP || defaultGetIP;
|
|
74
75
|
const onBlocked = config.onBlocked || defaultOnBlocked;
|
|
75
76
|
const onError = config.onError || defaultOnError;
|
|
76
77
|
const skipPaths = config.skipPaths;
|
|
@@ -87,7 +88,7 @@ function withWebDecoy(config) {
|
|
|
87
88
|
const metadata = {
|
|
88
89
|
method: req.method,
|
|
89
90
|
path: pathname,
|
|
90
|
-
ip:
|
|
91
|
+
ip: getIP2(req),
|
|
91
92
|
user_agent: req.headers.get("user-agent") || void 0,
|
|
92
93
|
headers,
|
|
93
94
|
timestamp: Date.now()
|
|
@@ -187,8 +188,62 @@ function withBotProtection(handler, config) {
|
|
|
187
188
|
return handler(...args);
|
|
188
189
|
});
|
|
189
190
|
}
|
|
191
|
+
|
|
192
|
+
// src/captcha.ts
|
|
193
|
+
var import_node2 = require("@webdecoy/node");
|
|
194
|
+
function getIP(headers) {
|
|
195
|
+
const forwarded = headers.get("x-forwarded-for");
|
|
196
|
+
if (forwarded) return forwarded.split(",")[0].trim();
|
|
197
|
+
return headers.get("x-real-ip") || headers.get("x-vercel-forwarded-for") || "127.0.0.1";
|
|
198
|
+
}
|
|
199
|
+
function headersToRecord(headers) {
|
|
200
|
+
const out = {};
|
|
201
|
+
headers.forEach((value, key) => {
|
|
202
|
+
out[key.toLowerCase()] = value;
|
|
203
|
+
});
|
|
204
|
+
return out;
|
|
205
|
+
}
|
|
206
|
+
function createCaptchaHandler(options) {
|
|
207
|
+
const endpoints = (0, import_node2.createCaptchaEndpoints)(options);
|
|
208
|
+
async function handler(req) {
|
|
209
|
+
const url = new URL(req.url);
|
|
210
|
+
const query = {};
|
|
211
|
+
url.searchParams.forEach((value, key) => {
|
|
212
|
+
query[key] = value;
|
|
213
|
+
});
|
|
214
|
+
let body;
|
|
215
|
+
if (req.method.toUpperCase() === "POST") {
|
|
216
|
+
try {
|
|
217
|
+
body = await req.json();
|
|
218
|
+
} catch {
|
|
219
|
+
body = void 0;
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
const headers = headersToRecord(req.headers);
|
|
223
|
+
const result = await endpoints.handle({
|
|
224
|
+
method: req.method,
|
|
225
|
+
pathname: url.pathname,
|
|
226
|
+
query,
|
|
227
|
+
headers,
|
|
228
|
+
body,
|
|
229
|
+
ip: getIP(req.headers)
|
|
230
|
+
});
|
|
231
|
+
if (!result) {
|
|
232
|
+
return new Response(JSON.stringify({ error: "not_found" }), {
|
|
233
|
+
status: 404,
|
|
234
|
+
headers: { "content-type": "application/json" }
|
|
235
|
+
});
|
|
236
|
+
}
|
|
237
|
+
return new Response(JSON.stringify(result.body), {
|
|
238
|
+
status: result.status,
|
|
239
|
+
headers: result.headers
|
|
240
|
+
});
|
|
241
|
+
}
|
|
242
|
+
return { GET: handler, POST: handler };
|
|
243
|
+
}
|
|
190
244
|
// Annotate the CommonJS export names for ESM import in node:
|
|
191
245
|
0 && (module.exports = {
|
|
246
|
+
createCaptchaHandler,
|
|
192
247
|
withBotProtection,
|
|
193
248
|
withWebDecoy
|
|
194
249
|
});
|
package/dist/index.mjs
CHANGED
|
@@ -43,7 +43,7 @@ function shouldSkipPath(path, skipPaths) {
|
|
|
43
43
|
}
|
|
44
44
|
function withWebDecoy(config) {
|
|
45
45
|
const sdk = new WebDecoy(config);
|
|
46
|
-
const
|
|
46
|
+
const getIP2 = config.getIP || defaultGetIP;
|
|
47
47
|
const onBlocked = config.onBlocked || defaultOnBlocked;
|
|
48
48
|
const onError = config.onError || defaultOnError;
|
|
49
49
|
const skipPaths = config.skipPaths;
|
|
@@ -60,7 +60,7 @@ function withWebDecoy(config) {
|
|
|
60
60
|
const metadata = {
|
|
61
61
|
method: req.method,
|
|
62
62
|
path: pathname,
|
|
63
|
-
ip:
|
|
63
|
+
ip: getIP2(req),
|
|
64
64
|
user_agent: req.headers.get("user-agent") || void 0,
|
|
65
65
|
headers,
|
|
66
66
|
timestamp: Date.now()
|
|
@@ -160,7 +160,61 @@ function withBotProtection(handler, config) {
|
|
|
160
160
|
return handler(...args);
|
|
161
161
|
});
|
|
162
162
|
}
|
|
163
|
+
|
|
164
|
+
// src/captcha.ts
|
|
165
|
+
import { createCaptchaEndpoints } from "@webdecoy/node";
|
|
166
|
+
function getIP(headers) {
|
|
167
|
+
const forwarded = headers.get("x-forwarded-for");
|
|
168
|
+
if (forwarded) return forwarded.split(",")[0].trim();
|
|
169
|
+
return headers.get("x-real-ip") || headers.get("x-vercel-forwarded-for") || "127.0.0.1";
|
|
170
|
+
}
|
|
171
|
+
function headersToRecord(headers) {
|
|
172
|
+
const out = {};
|
|
173
|
+
headers.forEach((value, key) => {
|
|
174
|
+
out[key.toLowerCase()] = value;
|
|
175
|
+
});
|
|
176
|
+
return out;
|
|
177
|
+
}
|
|
178
|
+
function createCaptchaHandler(options) {
|
|
179
|
+
const endpoints = createCaptchaEndpoints(options);
|
|
180
|
+
async function handler(req) {
|
|
181
|
+
const url = new URL(req.url);
|
|
182
|
+
const query = {};
|
|
183
|
+
url.searchParams.forEach((value, key) => {
|
|
184
|
+
query[key] = value;
|
|
185
|
+
});
|
|
186
|
+
let body;
|
|
187
|
+
if (req.method.toUpperCase() === "POST") {
|
|
188
|
+
try {
|
|
189
|
+
body = await req.json();
|
|
190
|
+
} catch {
|
|
191
|
+
body = void 0;
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
const headers = headersToRecord(req.headers);
|
|
195
|
+
const result = await endpoints.handle({
|
|
196
|
+
method: req.method,
|
|
197
|
+
pathname: url.pathname,
|
|
198
|
+
query,
|
|
199
|
+
headers,
|
|
200
|
+
body,
|
|
201
|
+
ip: getIP(req.headers)
|
|
202
|
+
});
|
|
203
|
+
if (!result) {
|
|
204
|
+
return new Response(JSON.stringify({ error: "not_found" }), {
|
|
205
|
+
status: 404,
|
|
206
|
+
headers: { "content-type": "application/json" }
|
|
207
|
+
});
|
|
208
|
+
}
|
|
209
|
+
return new Response(JSON.stringify(result.body), {
|
|
210
|
+
status: result.status,
|
|
211
|
+
headers: result.headers
|
|
212
|
+
});
|
|
213
|
+
}
|
|
214
|
+
return { GET: handler, POST: handler };
|
|
215
|
+
}
|
|
163
216
|
export {
|
|
217
|
+
createCaptchaHandler,
|
|
164
218
|
withBotProtection,
|
|
165
219
|
withWebDecoy
|
|
166
220
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@webdecoy/nextjs",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"description": "Web Decoy middleware for Next.js",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"types": "./dist/index.d.ts",
|
|
@@ -41,7 +41,7 @@
|
|
|
41
41
|
"url": "https://github.com/WebDecoy/node-sdk/issues"
|
|
42
42
|
},
|
|
43
43
|
"dependencies": {
|
|
44
|
-
"@webdecoy/node": "^0.
|
|
44
|
+
"@webdecoy/node": "^0.4.0"
|
|
45
45
|
},
|
|
46
46
|
"peerDependencies": {
|
|
47
47
|
"next": ">=13.0.0"
|