@webdecoy/node 0.19.1 → 0.20.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.mjs CHANGED
@@ -55,153 +55,14 @@ import {
55
55
  upstashRateLimitStore,
56
56
  webBotAuth
57
57
  } from "./chunk-MQIYUGYQ.mjs";
58
-
59
- // src/client-ip.ts
60
- var IPV4 = /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/;
61
- function ipv4ToBytes(ip) {
62
- const m = IPV4.exec(ip);
63
- if (!m) return null;
64
- const out = new Uint8Array(4);
65
- for (let i = 0; i < 4; i++) {
66
- const part = m[i + 1];
67
- if (part.length > 1 && part[0] === "0") return null;
68
- const n = Number(part);
69
- if (n > 255) return null;
70
- out[i] = n;
71
- }
72
- return out;
73
- }
74
- function ipv6ToBytes(input) {
75
- let s = input.toLowerCase();
76
- const lastColon = s.lastIndexOf(":");
77
- if (lastColon !== -1 && s.slice(lastColon + 1).includes(".")) {
78
- const tail = ipv4ToBytes(s.slice(lastColon + 1));
79
- if (!tail) return null;
80
- const g1 = (tail[0] << 8 | tail[1]).toString(16);
81
- const g2 = (tail[2] << 8 | tail[3]).toString(16);
82
- s = `${s.slice(0, lastColon + 1)}${g1}:${g2}`;
83
- }
84
- const halves = s.split("::");
85
- if (halves.length > 2) return null;
86
- const parseGroups = (part) => {
87
- if (part === "") return [];
88
- const groups2 = part.split(":");
89
- const out = [];
90
- for (const g of groups2) {
91
- if (!/^[0-9a-f]{1,4}$/.test(g)) return null;
92
- out.push(parseInt(g, 16));
93
- }
94
- return out;
95
- };
96
- const head = parseGroups(halves[0]);
97
- if (head === null) return null;
98
- let groups;
99
- if (halves.length === 2) {
100
- const tail = parseGroups(halves[1]);
101
- if (tail === null) return null;
102
- const fill = 8 - head.length - tail.length;
103
- if (fill < 1) return null;
104
- groups = [...head, ...new Array(fill).fill(0), ...tail];
105
- } else {
106
- groups = head;
107
- }
108
- if (groups.length !== 8) return null;
109
- const bytes = new Uint8Array(16);
110
- for (let i = 0; i < 8; i++) {
111
- bytes[i * 2] = groups[i] >> 8;
112
- bytes[i * 2 + 1] = groups[i] & 255;
113
- }
114
- return bytes;
115
- }
116
- function ipToBytes(ip) {
117
- return ipv4ToBytes(ip) ?? ipv6ToBytes(ip);
118
- }
119
- function normalizeIp(raw) {
120
- if (!raw) return null;
121
- let v = raw.trim();
122
- if (!v) return null;
123
- if (v.startsWith("[")) {
124
- const close = v.indexOf("]");
125
- if (close === -1) return null;
126
- v = v.slice(1, close);
127
- } else if (v.includes(".") && v.split(":").length === 2) {
128
- v = v.slice(0, v.indexOf(":"));
129
- }
130
- const pct = v.indexOf("%");
131
- if (pct !== -1) v = v.slice(0, pct);
132
- if (ipv4ToBytes(v)) return v;
133
- const bytes = ipv6ToBytes(v);
134
- if (!bytes) return null;
135
- const mapped = bytes.subarray(0, 10).every((b) => b === 0) && bytes[10] === 255 && bytes[11] === 255;
136
- if (mapped) return `${bytes[12]}.${bytes[13]}.${bytes[14]}.${bytes[15]}`;
137
- return v.toLowerCase();
138
- }
139
- function ipInCidr(ip, cidr) {
140
- const slash = cidr.indexOf("/");
141
- const network = normalizeIp(slash === -1 ? cidr : cidr.slice(0, slash));
142
- const address = normalizeIp(ip);
143
- if (!network || !address) return false;
144
- const netBytes = ipToBytes(network);
145
- const addrBytes = ipToBytes(address);
146
- if (!netBytes || !addrBytes || netBytes.length !== addrBytes.length) return false;
147
- const totalBits = netBytes.length * 8;
148
- let bits = totalBits;
149
- if (slash !== -1) {
150
- const raw = cidr.slice(slash + 1);
151
- bits = Number(raw);
152
- if (raw === "" || !Number.isInteger(bits) || bits < 0 || bits > totalBits) return false;
153
- }
154
- const wholeBytes = bits >> 3;
155
- for (let i = 0; i < wholeBytes; i++) {
156
- if (netBytes[i] !== addrBytes[i]) return false;
157
- }
158
- const remainder = bits & 7;
159
- if (remainder) {
160
- const mask = 255 << 8 - remainder & 255;
161
- if ((netBytes[wholeBytes] & mask) !== (addrBytes[wholeBytes] & mask)) return false;
162
- }
163
- return true;
164
- }
165
- function readHeader(headers, name) {
166
- if (typeof headers.get === "function") {
167
- return headers.get(name) ?? void 0;
168
- }
169
- const bag = headers;
170
- const value = bag[name] ?? bag[name.toLowerCase()];
171
- if (Array.isArray(value)) return value.join(",");
172
- return value;
173
- }
174
- function forwardedChain(headers) {
175
- const raw = readHeader(headers, "x-forwarded-for");
176
- if (!raw) return [];
177
- return raw.split(",").map((part) => part.trim()).filter((part) => part !== "").map((part) => normalizeIp(part));
178
- }
179
- function resolveClientIp(options) {
180
- const { headers, trustProxy = false } = options;
181
- const peer = normalizeIp(options.peer) ?? void 0;
182
- if (trustProxy === false || trustProxy === 0) return peer;
183
- if (trustProxy === "cloudflare") {
184
- return normalizeIp(readHeader(headers, "cf-connecting-ip")) ?? peer;
185
- }
186
- if (trustProxy === "railway") {
187
- return resolveClientIp({ headers, peer: options.peer, trustProxy: 2 });
188
- }
189
- const chain = forwardedChain(headers);
190
- const singleHeaderFallback = () => normalizeIp(readHeader(headers, "x-real-ip")) ?? normalizeIp(readHeader(headers, "x-vercel-forwarded-for")?.split(",").pop()) ?? void 0;
191
- if (typeof trustProxy === "number") {
192
- if (!Number.isInteger(trustProxy) || trustProxy < 0) return peer;
193
- const index = chain.length - trustProxy;
194
- if (index < 0 || index >= chain.length) return singleHeaderFallback() ?? peer;
195
- return chain[index] ?? singleHeaderFallback() ?? peer;
196
- }
197
- const full = peer ? [...chain, peer] : [...chain];
198
- for (let i = full.length - 1; i >= 0; i--) {
199
- const candidate = full[i];
200
- if (candidate === null) return peer;
201
- if (!trustProxy.some((range) => ipInCidr(candidate, range))) return candidate;
202
- }
203
- return full[0] ?? void 0 ?? singleHeaderFallback() ?? peer;
204
- }
58
+ import {
59
+ createClearanceGate,
60
+ ipInCidr,
61
+ normalizeIp,
62
+ re,
63
+ resolveClientIp,
64
+ resolveGateEnv
65
+ } from "./chunk-2G4TLK3R.mjs";
205
66
 
206
67
  // src/client-signals.ts
207
68
  var MemoryClientSignalStore = class {
@@ -2427,6 +2288,7 @@ export {
2427
2288
  BOT_REGISTRY,
2428
2289
  BotPolicy,
2429
2290
  BotRule,
2291
+ re as CLEARANCE_GATE_BUILD,
2430
2292
  CLIENT_SESSION_COOKIE,
2431
2293
  CLIENT_SESSION_HEADER,
2432
2294
  Captcha,
@@ -2470,6 +2332,7 @@ export {
2470
2332
  consoleLogger,
2471
2333
  createAgentVerifier,
2472
2334
  createCaptchaEndpoints,
2335
+ createClearanceGate,
2473
2336
  createFetchGuard,
2474
2337
  deriveAndArm,
2475
2338
  deriveKey,
@@ -2491,6 +2354,7 @@ export {
2491
2354
  readEdgeVerdict,
2492
2355
  recommend,
2493
2356
  resolveClientIp,
2357
+ resolveGateEnv,
2494
2358
  resolveSecret,
2495
2359
  ruleBlockResponse,
2496
2360
  shouldSkipPath,
@@ -0,0 +1,42 @@
1
+ import { a as ClearanceGateOptions, C as ClearanceContext, b as GateOutcome } from './index-GXXXr_mb.mjs';
2
+
3
+ /**
4
+ * WebDecoy clearance for Vercel Routing Middleware, any framework.
5
+ *
6
+ * Astro, SvelteKit, Nuxt, Remix or a static site on Vercel: put this in
7
+ * `middleware.ts` at the project root and set WEBDECOY_SITE_KEY,
8
+ * WEBDECOY_SCANNER_ID and WEBDECOY_SENSOR_KEY in the project's environment.
9
+ *
10
+ * ```ts
11
+ * // middleware.ts
12
+ * export { default } from '@webdecoy/node/vercel';
13
+ *
14
+ * export const config = {
15
+ * // Everything but static assets. Keep /.well-known: the challenge uses it.
16
+ * matcher: ['/((?!_astro/|_next/static/|favicon.ico|.*\\.(?:css|js|png|jpg|svg|webp|woff2?)$).*)'],
17
+ * };
18
+ * ```
19
+ *
20
+ * Next.js sites use `withClearance()` from @webdecoy/nextjs instead.
21
+ *
22
+ * Starts in whatever mode the site has in WebDecoy, monitor by default, and
23
+ * enforces only once it is switched there and paths are protected.
24
+ */
25
+
26
+ /**
27
+ * Turn the gate's answer into what Vercel middleware returns.
28
+ *
29
+ * Continuing with changed request headers is a response carrying
30
+ * instructions: `x-middleware-next` and the full replacement header set
31
+ * under `x-middleware-request-*`, listed in `x-middleware-override-headers`.
32
+ * That is what `next({ request: { headers } })` from @vercel/functions
33
+ * sends; spelled out here so the SDK needs no Vercel package. Returning
34
+ * nothing continues the request untouched.
35
+ */
36
+ declare function toMiddlewareResponse(outcome: GateOutcome): Response | undefined;
37
+ /** Routing Middleware bound to one site. Options default to the environment. */
38
+ declare function clearanceMiddleware(options?: ClearanceGateOptions): (request: Request, context?: ClearanceContext) => Promise<Response | undefined>;
39
+ /** The middleware configured from WEBDECOY_* environment variables. */
40
+ declare const middleware: (request: Request, context?: ClearanceContext) => Promise<Response | undefined>;
41
+
42
+ export { ClearanceContext, ClearanceGateOptions, clearanceMiddleware, middleware as default, toMiddlewareResponse };
@@ -0,0 +1,42 @@
1
+ import { a as ClearanceGateOptions, C as ClearanceContext, b as GateOutcome } from './index-GXXXr_mb.js';
2
+
3
+ /**
4
+ * WebDecoy clearance for Vercel Routing Middleware, any framework.
5
+ *
6
+ * Astro, SvelteKit, Nuxt, Remix or a static site on Vercel: put this in
7
+ * `middleware.ts` at the project root and set WEBDECOY_SITE_KEY,
8
+ * WEBDECOY_SCANNER_ID and WEBDECOY_SENSOR_KEY in the project's environment.
9
+ *
10
+ * ```ts
11
+ * // middleware.ts
12
+ * export { default } from '@webdecoy/node/vercel';
13
+ *
14
+ * export const config = {
15
+ * // Everything but static assets. Keep /.well-known: the challenge uses it.
16
+ * matcher: ['/((?!_astro/|_next/static/|favicon.ico|.*\\.(?:css|js|png|jpg|svg|webp|woff2?)$).*)'],
17
+ * };
18
+ * ```
19
+ *
20
+ * Next.js sites use `withClearance()` from @webdecoy/nextjs instead.
21
+ *
22
+ * Starts in whatever mode the site has in WebDecoy, monitor by default, and
23
+ * enforces only once it is switched there and paths are protected.
24
+ */
25
+
26
+ /**
27
+ * Turn the gate's answer into what Vercel middleware returns.
28
+ *
29
+ * Continuing with changed request headers is a response carrying
30
+ * instructions: `x-middleware-next` and the full replacement header set
31
+ * under `x-middleware-request-*`, listed in `x-middleware-override-headers`.
32
+ * That is what `next({ request: { headers } })` from @vercel/functions
33
+ * sends; spelled out here so the SDK needs no Vercel package. Returning
34
+ * nothing continues the request untouched.
35
+ */
36
+ declare function toMiddlewareResponse(outcome: GateOutcome): Response | undefined;
37
+ /** Routing Middleware bound to one site. Options default to the environment. */
38
+ declare function clearanceMiddleware(options?: ClearanceGateOptions): (request: Request, context?: ClearanceContext) => Promise<Response | undefined>;
39
+ /** The middleware configured from WEBDECOY_* environment variables. */
40
+ declare const middleware: (request: Request, context?: ClearanceContext) => Promise<Response | undefined>;
41
+
42
+ export { ClearanceContext, ClearanceGateOptions, clearanceMiddleware, middleware as default, toMiddlewareResponse };