@webdecoy/nextjs 0.21.2 → 0.22.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/README.md +204 -0
- package/dist/vercel.d.mts +67 -0
- package/dist/vercel.d.ts +67 -0
- package/dist/vercel.js +49 -0
- package/dist/vercel.mjs +24 -0
- package/package.json +17 -5
package/README.md
ADDED
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
# @webdecoy/nextjs
|
|
2
|
+
|
|
3
|
+
WebDecoy for Next.js: bot-detection middleware with tripwires and rate limits, clearance-token enforcement on Vercel, and helpers for route handlers. Starts in monitor mode.
|
|
4
|
+
|
|
5
|
+
[](https://www.npmjs.com/package/@webdecoy/nextjs)
|
|
6
|
+
[](https://opensource.org/licenses/MIT)
|
|
7
|
+
|
|
8
|
+
## Installation
|
|
9
|
+
|
|
10
|
+
```bash
|
|
11
|
+
npm install @webdecoy/nextjs @webdecoy/node
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
`@webdecoy/node` is the core SDK. Install it directly: rules such as `tripwire()` and `rateLimit()` are imported from it. Requires Next.js 13 or later.
|
|
15
|
+
|
|
16
|
+
## Quick start on Vercel: one middleware, from the environment
|
|
17
|
+
|
|
18
|
+
Detection and clearance enforcement in one file, configured entirely by environment variables:
|
|
19
|
+
|
|
20
|
+
```ts
|
|
21
|
+
// middleware.ts (Next.js 13-15), or proxy.ts (Next.js 16)
|
|
22
|
+
import webdecoy from '@webdecoy/nextjs/vercel';
|
|
23
|
+
|
|
24
|
+
export default webdecoy;
|
|
25
|
+
|
|
26
|
+
export const config = {
|
|
27
|
+
// Everything but static assets. Keep /.well-known: the challenge uses it.
|
|
28
|
+
matcher: ['/((?!_next/static|_next/image|favicon.ico).*)'],
|
|
29
|
+
};
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
Write it as import then export. Next.js 16 does not follow `export { default } from '@webdecoy/nextjs/vercel'` when it checks the file for a default export, and fails the build. `config` must be declared in the file itself.
|
|
33
|
+
|
|
34
|
+
| Variable | Turns on | Secret? |
|
|
35
|
+
|---|---|---|
|
|
36
|
+
| `WEBDECOY_API_KEY` | Detection: bots, tripwire hits and agents the bot registry names are reported to your dashboard. From the **API Keys** page. | Yes |
|
|
37
|
+
| `WEBDECOY_SITE_KEY`, `WEBDECOY_SCANNER_ID` | The clearance gate, with the sensor key. From **Enforcement, Setup**. | No |
|
|
38
|
+
| `WEBDECOY_SENSOR_KEY` | The clearance gate. From **Enforcement, Setup**. | Yes |
|
|
39
|
+
|
|
40
|
+
Either half works without the other; with neither, every request passes. Detection starts in monitor mode and passes its verdict to your app as request headers (see [Monitor mode](#monitor-mode)); the gate follows the site's mode in WebDecoy, monitor until you switch it, and hands the page its verdict as `x-wd-clearance` (read it with `getEdgeVerdict()`). Every failure, WebDecoy unreachable included, lets the request through.
|
|
41
|
+
|
|
42
|
+
To customize, use `createVercelMiddleware(options)` from the same path. It takes the core SDK options (`apiKey`, `rules`, `threatScoreThreshold`, ...), `mode`, `skipPaths`, `trustProxy`, `onBlocked` as `(request, decision) => Response`, and the gate's `siteKey`, `scannerId`, `sensorKey` and `ingest`; the key and the gate's identity come from the environment when not passed.
|
|
43
|
+
|
|
44
|
+
```ts
|
|
45
|
+
// middleware.ts
|
|
46
|
+
import { createVercelMiddleware } from '@webdecoy/nextjs/vercel';
|
|
47
|
+
import { tripwire } from '@webdecoy/node';
|
|
48
|
+
|
|
49
|
+
export default createVercelMiddleware({
|
|
50
|
+
rules: [tripwire({ paths: ['/admin-backup.zip'] })],
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
export const config = { matcher: ['/((?!_next/static|_next/image|favicon.ico).*)'] };
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
## Detection middleware
|
|
57
|
+
|
|
58
|
+
```ts
|
|
59
|
+
// middleware.ts (project root, or src/ if you use it)
|
|
60
|
+
import { withWebDecoy } from '@webdecoy/nextjs';
|
|
61
|
+
|
|
62
|
+
export default withWebDecoy({
|
|
63
|
+
apiKey: process.env.WEBDECOY_API_KEY!,
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
// Protect the routes that matter and leave static assets alone.
|
|
67
|
+
export const config = {
|
|
68
|
+
matcher: ['/api/:path*', '/checkout/:path*'],
|
|
69
|
+
};
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
### API key
|
|
73
|
+
|
|
74
|
+
`apiKey` is optional. Without it the middleware runs local rules only (the built-in tripwire paths by default) and nothing reports to your dashboard. `withWebDecoy` does **not** read `WEBDECOY_API_KEY` from the environment by itself: pass it as `apiKey`, as above. (`@webdecoy/nextjs/vercel` does read it.)
|
|
75
|
+
|
|
76
|
+
To get a key: sign up at [app.webdecoy.com](https://app.webdecoy.com), add your site, then create a key on the **API Keys** page. Keys start with `sk_live_` or `sk_test_`. Set it in your deployment's environment (for example the Vercel project settings), never in committed code.
|
|
77
|
+
|
|
78
|
+
### Monitor mode
|
|
79
|
+
|
|
80
|
+
`withWebDecoy` starts in **monitor mode**: it serves every request and passes the verdict to your app as request headers, which route handlers and server components can read:
|
|
81
|
+
|
|
82
|
+
| Request header | Value |
|
|
83
|
+
|---|---|
|
|
84
|
+
| `x-webdecoy-decision` | The detection's `decision` (`allow`, `block` or `challenge`). |
|
|
85
|
+
| `x-webdecoy-detection-id` | The detection id. |
|
|
86
|
+
| `x-webdecoy-would-block` | `true` when enforce mode would have refused the request. |
|
|
87
|
+
|
|
88
|
+
Inbound copies of these headers are stripped first, so a client cannot set them. Review what would have been blocked, then set `mode: 'enforce'`. In enforce mode a rule refusal answers `403` (naming the rule), a rate-limit throttle answers `429` with `Retry-After`, and a request blocked by the server-side score goes to `onBlocked` (default `403`).
|
|
89
|
+
|
|
90
|
+
## Local rules
|
|
91
|
+
|
|
92
|
+
With no `rules` option the middleware arms `tripwire()` with its built-in scanner-bait paths (`/.env`, `/wp-config.php` and similar). Pass `rules: []` to opt out, or add your own:
|
|
93
|
+
|
|
94
|
+
```ts
|
|
95
|
+
// middleware.ts
|
|
96
|
+
import { withWebDecoy } from '@webdecoy/nextjs';
|
|
97
|
+
import { tripwire, rateLimit } from '@webdecoy/node';
|
|
98
|
+
|
|
99
|
+
export default withWebDecoy({
|
|
100
|
+
apiKey: process.env.WEBDECOY_API_KEY,
|
|
101
|
+
rules: [
|
|
102
|
+
tripwire({ paths: ['/admin-backup.zip'] }),
|
|
103
|
+
rateLimit({ max: 100, window: 60 }),
|
|
104
|
+
],
|
|
105
|
+
});
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
`rateLimit()` counts in memory per instance. On Vercel or any multi-instance deployment, pass a shared store such as `upstashRateLimitStore()` from `@webdecoy/node`. See the [@webdecoy/node README](https://www.npmjs.com/package/@webdecoy/node) for every rule.
|
|
109
|
+
|
|
110
|
+
## Options
|
|
111
|
+
|
|
112
|
+
`withWebDecoy` accepts every core SDK option (`apiKey`, `rules`, `apiUrl`, `threatScoreThreshold`, `timeout`, `debug`, ...) and the per-request options `threshold`, `skipLocalAnalysis` and `metadata`, plus:
|
|
113
|
+
|
|
114
|
+
| Option | Default | Description |
|
|
115
|
+
|---|---|---|
|
|
116
|
+
| `mode` | `'monitor'` | `'monitor'` records and serves every request; `'enforce'` blocks. |
|
|
117
|
+
| `skipPaths` | none | Paths to skip, as strings or regular expressions. |
|
|
118
|
+
| `trustProxy` | `1` | How much of `X-Forwarded-For` to believe: a hop count, `'cloudflare'`, `'railway'`, or your proxies' CIDRs. `1` is right on Vercel. |
|
|
119
|
+
| `getIP` | derived from `trustProxy` | `(req) => string`. Overrides `trustProxy`. |
|
|
120
|
+
| `onBlocked` | `403` JSON | `(req, detection, decision) => NextResponse`. Enforce mode, server-score blocks. |
|
|
121
|
+
| `onError` | log and continue | `(req, error) => NextResponse \| null`. The middleware fails open. |
|
|
122
|
+
|
|
123
|
+
Use Next's own `export const config = { matcher }` to choose which routes the middleware runs on.
|
|
124
|
+
|
|
125
|
+
## Clearance enforcement on Vercel
|
|
126
|
+
|
|
127
|
+
With a WebDecoy account, the paths you protect in WebDecoy can require a clearance token, checked in middleware before the page renders. A real browser earns one automatically; a client that never runs JavaScript does not get the page. It follows the site's mode in WebDecoy (monitor until you switch it) and fails open.
|
|
128
|
+
|
|
129
|
+
```ts
|
|
130
|
+
// middleware.ts
|
|
131
|
+
import { withClearance } from '@webdecoy/nextjs';
|
|
132
|
+
|
|
133
|
+
export default withClearance();
|
|
134
|
+
|
|
135
|
+
export const config = {
|
|
136
|
+
// Everything but static assets. Keep /.well-known: the challenge uses it.
|
|
137
|
+
matcher: ['/((?!_next/static|_next/image|favicon.ico).*)'],
|
|
138
|
+
};
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
`withClearance()` reads these environment variables (or the matching options `siteKey`, `scannerId`, `sensorKey`, `ingest`):
|
|
142
|
+
|
|
143
|
+
| Variable | Where it comes from | Secret? |
|
|
144
|
+
|---|---|---|
|
|
145
|
+
| `WEBDECOY_SITE_KEY` | **Enforcement, Setup** in the dashboard | No |
|
|
146
|
+
| `WEBDECOY_SCANNER_ID` | **Enforcement, Setup** in the dashboard | No |
|
|
147
|
+
| `WEBDECOY_SENSOR_KEY` | **Enforcement, Setup** in the dashboard | Yes |
|
|
148
|
+
| `WEBDECOY_INGEST` | Optional override of the ingest endpoint | No |
|
|
149
|
+
|
|
150
|
+
If any of the first three is missing, the gate passes every request and logs a warning once. Guide: [Vercel middleware](https://docs.webdecoy.com/installation/vercel-middleware/).
|
|
151
|
+
|
|
152
|
+
A project has one `middleware.ts`. `@webdecoy/nextjs/vercel` runs detection and the gate together; `withWebDecoy` and `withClearance` are each one half.
|
|
153
|
+
|
|
154
|
+
## Pages Router API routes
|
|
155
|
+
|
|
156
|
+
`withBotProtection` wraps a Pages Router API handler. Like the middleware it defaults to monitor mode; the handler always runs and receives `req.webdecoy` and `req.webdecoyDecision`.
|
|
157
|
+
|
|
158
|
+
```ts
|
|
159
|
+
// pages/api/protected.ts
|
|
160
|
+
import type { NextApiRequest, NextApiResponse } from 'next';
|
|
161
|
+
import { withBotProtection } from '@webdecoy/nextjs';
|
|
162
|
+
|
|
163
|
+
async function handler(req: NextApiRequest, res: NextApiResponse) {
|
|
164
|
+
res.json({ data: 'protected' });
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
export default withBotProtection(handler, {
|
|
168
|
+
apiKey: process.env.WEBDECOY_API_KEY,
|
|
169
|
+
blockThreshold: 80, // default
|
|
170
|
+
});
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
It reads the socket address and trusts no forwarding header unless you pass `trustProxy`.
|
|
174
|
+
|
|
175
|
+
## Other helpers
|
|
176
|
+
|
|
177
|
+
- `getEdgeVerdict(req)`: what a WebDecoy edge validator (the clearance middleware or the Cloudflare edge sensor) said about a request, readable in a route handler or server component. `present: false` means no validator fronted the request.
|
|
178
|
+
- `honeytokenLink(apiKey)`: the site's hidden trap link (`linkProps`, `activePaths`). Render the link in your root layout and arm it with `tripwire({ paths: bait.activePaths })`. The middleware cannot inject it automatically because App Router responses stream.
|
|
179
|
+
- `createCaptchaHandler({ secret })`: route handlers for the self-hosted captcha used by [`@webdecoy/client`](https://www.npmjs.com/package/@webdecoy/client). Mount at `app/__webdecoy/[...webdecoy]/route.ts` as `export const { GET, POST } = createCaptchaHandler({ secret: process.env.WEBDECOY_SECRET });`. `secret` is required when `NODE_ENV` is `production`.
|
|
180
|
+
|
|
181
|
+
## Environment variables
|
|
182
|
+
|
|
183
|
+
`@webdecoy/nextjs/vercel` reads `WEBDECOY_API_KEY` and the clearance variables; `withClearance()` reads the clearance variables (table above). Elsewhere `WEBDECOY_API_KEY` and `WEBDECOY_SECRET` are naming conventions: pass their values as `apiKey` and `secret`.
|
|
184
|
+
|
|
185
|
+
## Verify the install
|
|
186
|
+
|
|
187
|
+
With an `apiKey` configured, send one request with the reserved test User-Agent to a path your `matcher` covers:
|
|
188
|
+
|
|
189
|
+
```bash
|
|
190
|
+
curl -A "WebDecoy-Test/1.0" http://localhost:3000/api/health
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
A detection labeled **Test** appears on the **Detections** page within a few seconds.
|
|
194
|
+
|
|
195
|
+
## Documentation
|
|
196
|
+
|
|
197
|
+
- [Next.js middleware](https://docs.webdecoy.com/sdk-plugins/nextjs/)
|
|
198
|
+
- [Vercel middleware (clearance)](https://docs.webdecoy.com/installation/vercel-middleware/)
|
|
199
|
+
- [Install with an AI assistant](https://docs.webdecoy.com/installation/install-with-ai/): copy-paste snippets for every stack
|
|
200
|
+
- [Verify your install](https://docs.webdecoy.com/getting-started/verify-installation/)
|
|
201
|
+
|
|
202
|
+
## License
|
|
203
|
+
|
|
204
|
+
MIT
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { NextRequest, NextFetchEvent } from 'next/server';
|
|
2
|
+
import { DetectAndGateOptions } from '@webdecoy/node';
|
|
3
|
+
export { DetectAndGateOptions } from '@webdecoy/node';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* WebDecoy for Next.js on Vercel: detection and the clearance gate in one
|
|
7
|
+
* middleware, configured from the environment.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```typescript
|
|
11
|
+
* // middleware.ts (Next.js 13-15), or proxy.ts (Next.js 16)
|
|
12
|
+
* import webdecoy from '@webdecoy/nextjs/vercel';
|
|
13
|
+
*
|
|
14
|
+
* export default webdecoy;
|
|
15
|
+
*
|
|
16
|
+
* export const config = {
|
|
17
|
+
* // Everything but static assets. Keep /.well-known: the challenge uses it.
|
|
18
|
+
* matcher: ['/((?!_next/static|_next/image|favicon.ico).*)'],
|
|
19
|
+
* };
|
|
20
|
+
* ```
|
|
21
|
+
*
|
|
22
|
+
* Import then export, rather than `export { default } from ...`: Next.js 16
|
|
23
|
+
* looks for the default export without following a re-export and fails the
|
|
24
|
+
* build on that one-line form, in `proxy.ts` and `middleware.ts` alike. And
|
|
25
|
+
* `config` has to be written in the file itself, because Next reads it
|
|
26
|
+
* statically.
|
|
27
|
+
*
|
|
28
|
+
* Environment:
|
|
29
|
+
*
|
|
30
|
+
* - `WEBDECOY_API_KEY` turns on detection: bots are reported to WebDecoy,
|
|
31
|
+
* including the reserved test trigger (`curl -A "WebDecoy-Test/1.0" ...`),
|
|
32
|
+
* and the verdict reaches the app as request headers
|
|
33
|
+
* (`x-webdecoy-decision`, `x-webdecoy-detection-id`, `x-webdecoy-would-block`).
|
|
34
|
+
* - `WEBDECOY_SITE_KEY`, `WEBDECOY_SCANNER_ID` and `WEBDECOY_SENSOR_KEY` turn
|
|
35
|
+
* on the clearance gate for the paths the site protects in WebDecoy; the
|
|
36
|
+
* page receives its verdict as `x-wd-clearance` (see `getEdgeVerdict()`).
|
|
37
|
+
*
|
|
38
|
+
* Either half works without the other; with neither, every request passes.
|
|
39
|
+
* Detection starts in monitor mode and the gate follows the site's mode in
|
|
40
|
+
* WebDecoy (monitor by default), so installing this changes no response until
|
|
41
|
+
* you choose to enforce. Every failure lets the request through.
|
|
42
|
+
*
|
|
43
|
+
* To customize, use {@link createVercelMiddleware}:
|
|
44
|
+
*
|
|
45
|
+
* ```typescript
|
|
46
|
+
* // middleware.ts
|
|
47
|
+
* import { createVercelMiddleware } from '@webdecoy/nextjs/vercel';
|
|
48
|
+
* import { tripwire } from '@webdecoy/node';
|
|
49
|
+
*
|
|
50
|
+
* export default createVercelMiddleware({
|
|
51
|
+
* rules: [tripwire({ paths: ['/admin-backup.zip'] })],
|
|
52
|
+
* });
|
|
53
|
+
*
|
|
54
|
+
* export const config = { matcher: ['/((?!_next/static|_next/image|favicon.ico).*)'] };
|
|
55
|
+
* ```
|
|
56
|
+
*/
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Detection and the clearance gate as Next.js middleware. Options default to
|
|
60
|
+
* the environment (`WEBDECOY_API_KEY`, `WEBDECOY_SITE_KEY`,
|
|
61
|
+
* `WEBDECOY_SCANNER_ID`, `WEBDECOY_SENSOR_KEY`).
|
|
62
|
+
*/
|
|
63
|
+
declare function createVercelMiddleware(options?: DetectAndGateOptions): (req: NextRequest, event?: NextFetchEvent) => Promise<Response>;
|
|
64
|
+
/** The middleware configured from WEBDECOY_* environment variables. */
|
|
65
|
+
declare const middleware: (req: NextRequest, event?: NextFetchEvent) => Promise<Response>;
|
|
66
|
+
|
|
67
|
+
export { createVercelMiddleware, middleware as default };
|
package/dist/vercel.d.ts
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { NextRequest, NextFetchEvent } from 'next/server';
|
|
2
|
+
import { DetectAndGateOptions } from '@webdecoy/node';
|
|
3
|
+
export { DetectAndGateOptions } from '@webdecoy/node';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* WebDecoy for Next.js on Vercel: detection and the clearance gate in one
|
|
7
|
+
* middleware, configured from the environment.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```typescript
|
|
11
|
+
* // middleware.ts (Next.js 13-15), or proxy.ts (Next.js 16)
|
|
12
|
+
* import webdecoy from '@webdecoy/nextjs/vercel';
|
|
13
|
+
*
|
|
14
|
+
* export default webdecoy;
|
|
15
|
+
*
|
|
16
|
+
* export const config = {
|
|
17
|
+
* // Everything but static assets. Keep /.well-known: the challenge uses it.
|
|
18
|
+
* matcher: ['/((?!_next/static|_next/image|favicon.ico).*)'],
|
|
19
|
+
* };
|
|
20
|
+
* ```
|
|
21
|
+
*
|
|
22
|
+
* Import then export, rather than `export { default } from ...`: Next.js 16
|
|
23
|
+
* looks for the default export without following a re-export and fails the
|
|
24
|
+
* build on that one-line form, in `proxy.ts` and `middleware.ts` alike. And
|
|
25
|
+
* `config` has to be written in the file itself, because Next reads it
|
|
26
|
+
* statically.
|
|
27
|
+
*
|
|
28
|
+
* Environment:
|
|
29
|
+
*
|
|
30
|
+
* - `WEBDECOY_API_KEY` turns on detection: bots are reported to WebDecoy,
|
|
31
|
+
* including the reserved test trigger (`curl -A "WebDecoy-Test/1.0" ...`),
|
|
32
|
+
* and the verdict reaches the app as request headers
|
|
33
|
+
* (`x-webdecoy-decision`, `x-webdecoy-detection-id`, `x-webdecoy-would-block`).
|
|
34
|
+
* - `WEBDECOY_SITE_KEY`, `WEBDECOY_SCANNER_ID` and `WEBDECOY_SENSOR_KEY` turn
|
|
35
|
+
* on the clearance gate for the paths the site protects in WebDecoy; the
|
|
36
|
+
* page receives its verdict as `x-wd-clearance` (see `getEdgeVerdict()`).
|
|
37
|
+
*
|
|
38
|
+
* Either half works without the other; with neither, every request passes.
|
|
39
|
+
* Detection starts in monitor mode and the gate follows the site's mode in
|
|
40
|
+
* WebDecoy (monitor by default), so installing this changes no response until
|
|
41
|
+
* you choose to enforce. Every failure lets the request through.
|
|
42
|
+
*
|
|
43
|
+
* To customize, use {@link createVercelMiddleware}:
|
|
44
|
+
*
|
|
45
|
+
* ```typescript
|
|
46
|
+
* // middleware.ts
|
|
47
|
+
* import { createVercelMiddleware } from '@webdecoy/nextjs/vercel';
|
|
48
|
+
* import { tripwire } from '@webdecoy/node';
|
|
49
|
+
*
|
|
50
|
+
* export default createVercelMiddleware({
|
|
51
|
+
* rules: [tripwire({ paths: ['/admin-backup.zip'] })],
|
|
52
|
+
* });
|
|
53
|
+
*
|
|
54
|
+
* export const config = { matcher: ['/((?!_next/static|_next/image|favicon.ico).*)'] };
|
|
55
|
+
* ```
|
|
56
|
+
*/
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Detection and the clearance gate as Next.js middleware. Options default to
|
|
60
|
+
* the environment (`WEBDECOY_API_KEY`, `WEBDECOY_SITE_KEY`,
|
|
61
|
+
* `WEBDECOY_SCANNER_ID`, `WEBDECOY_SENSOR_KEY`).
|
|
62
|
+
*/
|
|
63
|
+
declare function createVercelMiddleware(options?: DetectAndGateOptions): (req: NextRequest, event?: NextFetchEvent) => Promise<Response>;
|
|
64
|
+
/** The middleware configured from WEBDECOY_* environment variables. */
|
|
65
|
+
declare const middleware: (req: NextRequest, event?: NextFetchEvent) => Promise<Response>;
|
|
66
|
+
|
|
67
|
+
export { createVercelMiddleware, middleware as default };
|
package/dist/vercel.js
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/vercel.ts
|
|
21
|
+
var vercel_exports = {};
|
|
22
|
+
__export(vercel_exports, {
|
|
23
|
+
createVercelMiddleware: () => createVercelMiddleware,
|
|
24
|
+
default: () => vercel_default
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(vercel_exports);
|
|
27
|
+
var import_server = require("next/server");
|
|
28
|
+
var import_node = require("@webdecoy/node");
|
|
29
|
+
function createVercelMiddleware(options = {}) {
|
|
30
|
+
const decide = (0, import_node.createDetectAndGate)(options);
|
|
31
|
+
return async (req, event) => {
|
|
32
|
+
try {
|
|
33
|
+
const { outcome } = await decide(req, {
|
|
34
|
+
waitUntil: event ? (p) => event.waitUntil(p) : void 0
|
|
35
|
+
});
|
|
36
|
+
if (outcome.action === "respond") return outcome.response;
|
|
37
|
+
if (!outcome.headers) return import_server.NextResponse.next();
|
|
38
|
+
return import_server.NextResponse.next({ request: { headers: outcome.headers } });
|
|
39
|
+
} catch {
|
|
40
|
+
return import_server.NextResponse.next();
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
var middleware = createVercelMiddleware();
|
|
45
|
+
var vercel_default = middleware;
|
|
46
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
47
|
+
0 && (module.exports = {
|
|
48
|
+
createVercelMiddleware
|
|
49
|
+
});
|
package/dist/vercel.mjs
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
// src/vercel.ts
|
|
2
|
+
import { NextResponse } from "next/server";
|
|
3
|
+
import { createDetectAndGate } from "@webdecoy/node";
|
|
4
|
+
function createVercelMiddleware(options = {}) {
|
|
5
|
+
const decide = createDetectAndGate(options);
|
|
6
|
+
return async (req, event) => {
|
|
7
|
+
try {
|
|
8
|
+
const { outcome } = await decide(req, {
|
|
9
|
+
waitUntil: event ? (p) => event.waitUntil(p) : void 0
|
|
10
|
+
});
|
|
11
|
+
if (outcome.action === "respond") return outcome.response;
|
|
12
|
+
if (!outcome.headers) return NextResponse.next();
|
|
13
|
+
return NextResponse.next({ request: { headers: outcome.headers } });
|
|
14
|
+
} catch {
|
|
15
|
+
return NextResponse.next();
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
var middleware = createVercelMiddleware();
|
|
20
|
+
var vercel_default = middleware;
|
|
21
|
+
export {
|
|
22
|
+
createVercelMiddleware,
|
|
23
|
+
vercel_default as default
|
|
24
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@webdecoy/nextjs",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.22.0",
|
|
4
4
|
"description": "Web Decoy middleware for Next.js",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"types": "./dist/index.d.ts",
|
|
@@ -9,18 +9,30 @@
|
|
|
9
9
|
"require": "./dist/index.js",
|
|
10
10
|
"import": "./dist/index.mjs",
|
|
11
11
|
"types": "./dist/index.d.ts"
|
|
12
|
+
},
|
|
13
|
+
"./vercel": {
|
|
14
|
+
"require": "./dist/vercel.js",
|
|
15
|
+
"import": "./dist/vercel.mjs",
|
|
16
|
+
"types": "./dist/vercel.d.ts"
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
"typesVersions": {
|
|
20
|
+
"*": {
|
|
21
|
+
"vercel": [
|
|
22
|
+
"./dist/vercel.d.ts"
|
|
23
|
+
]
|
|
12
24
|
}
|
|
13
25
|
},
|
|
14
26
|
"files": [
|
|
15
27
|
"dist"
|
|
16
28
|
],
|
|
17
29
|
"scripts": {
|
|
18
|
-
"build": "tsup src/index.ts --format cjs,esm --dts --clean",
|
|
19
|
-
"dev": "tsup src/index.ts --format cjs,esm --dts --watch",
|
|
30
|
+
"build": "tsup src/index.ts src/vercel.ts --format cjs,esm --dts --clean",
|
|
31
|
+
"dev": "tsup src/index.ts src/vercel.ts --format cjs,esm --dts --watch",
|
|
20
32
|
"test": "jest --passWithNoTests",
|
|
21
33
|
"lint": "eslint src --max-warnings 2",
|
|
22
34
|
"clean": "rm -rf dist",
|
|
23
|
-
"check:edge": "node ../../scripts/check-edge.mjs src/index.ts"
|
|
35
|
+
"check:edge": "node ../../scripts/check-edge.mjs src/index.ts src/vercel.ts"
|
|
24
36
|
},
|
|
25
37
|
"keywords": [
|
|
26
38
|
"web-decoy",
|
|
@@ -42,7 +54,7 @@
|
|
|
42
54
|
"url": "https://github.com/WebDecoy/node/issues"
|
|
43
55
|
},
|
|
44
56
|
"dependencies": {
|
|
45
|
-
"@webdecoy/node": "^0.
|
|
57
|
+
"@webdecoy/node": "^0.22.0"
|
|
46
58
|
},
|
|
47
59
|
"peerDependencies": {
|
|
48
60
|
"next": ">=13.0.0"
|