aicrawlers 0.0.0 → 1.1.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.cjs ADDED
@@ -0,0 +1,288 @@
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/index.ts
21
+ var src_exports = {};
22
+ __export(src_exports, {
23
+ aiCrawlerPattern: () => aiCrawlerPattern,
24
+ aiCrawlers: () => aiCrawlers,
25
+ apacheRules: () => apacheRules,
26
+ blockedCrawlers: () => blockedCrawlers,
27
+ caddyRules: () => caddyRules,
28
+ cloudflareExpression: () => cloudflareExpression,
29
+ cloudflareExpressions: () => cloudflareExpressions,
30
+ getAiCrawler: () => getAiCrawler,
31
+ isAiBot: () => isAiBot,
32
+ isAiCrawler: () => isAiCrawler,
33
+ listUpdated: () => listUpdated,
34
+ nextProxy: () => nextProxy,
35
+ nginxMap: () => nginxMap,
36
+ purposes: () => PURPOSES,
37
+ robotsTxt: () => robotsTxt,
38
+ version: () => version
39
+ });
40
+ module.exports = __toCommonJS(src_exports);
41
+
42
+ // src/crawlers.ts
43
+ var GOOGLE_RDNS = ["googlebot.com", "google.com", "googleusercontent.com"];
44
+ var LIST = [
45
+ // OpenAI
46
+ { name: "GPTBot", operator: "OpenAI", purpose: "training", ipRanges: "https://openai.com/gptbot.json" },
47
+ { name: "OAI-SearchBot", operator: "OpenAI", purpose: "search", ipRanges: "https://openai.com/searchbot.json" },
48
+ { name: "ChatGPT-User", operator: "OpenAI", purpose: "user", ipRanges: "https://openai.com/chatgpt-user.json" },
49
+ { name: "ChatGPT Agent", operator: "OpenAI", purpose: "user" },
50
+ { name: "OAI-AdsBot", operator: "OpenAI", purpose: "other" },
51
+ // Anthropic
52
+ { name: "ClaudeBot", operator: "Anthropic", purpose: "training" },
53
+ { name: "Claude-SearchBot", operator: "Anthropic", purpose: "search" },
54
+ { name: "Claude-User", operator: "Anthropic", purpose: "user" },
55
+ { name: "Claude-Web", operator: "Anthropic", purpose: "other" },
56
+ { name: "anthropic-ai", operator: "Anthropic", purpose: "training" },
57
+ { name: "Claude-Code", operator: "Anthropic", purpose: "user" },
58
+ { name: "Claude-Agent", operator: "Anthropic", purpose: "user" },
59
+ // Perplexity
60
+ { name: "PerplexityBot", operator: "Perplexity", purpose: "search", ipRanges: "https://www.perplexity.com/perplexitybot.json" },
61
+ { name: "Perplexity-User", operator: "Perplexity", purpose: "user", ipRanges: "https://www.perplexity.com/perplexity-user.json" },
62
+ // Google
63
+ { name: "Google-Extended", operator: "Google", purpose: "training", robotsOnly: true },
64
+ { name: "Google-CloudVertexBot", operator: "Google", purpose: "other", rdns: GOOGLE_RDNS },
65
+ { name: "GoogleOther", operator: "Google", purpose: "other", rdns: GOOGLE_RDNS },
66
+ { name: "GoogleOther-Image", operator: "Google", purpose: "other", rdns: GOOGLE_RDNS },
67
+ { name: "GoogleOther-Video", operator: "Google", purpose: "other", rdns: GOOGLE_RDNS },
68
+ { name: "Gemini-Deep-Research", operator: "Google", purpose: "user" },
69
+ { name: "Google-NotebookLM", operator: "Google", purpose: "user" },
70
+ { name: "GoogleAgent-Mariner", operator: "Google", purpose: "user" },
71
+ { name: "Google-Agent", operator: "Google", purpose: "user" },
72
+ // Apple
73
+ { name: "Applebot", operator: "Apple", purpose: "search", rdns: ["applebot.apple.com"] },
74
+ { name: "Applebot-Extended", operator: "Apple", purpose: "training", robotsOnly: true },
75
+ // Meta
76
+ { name: "Meta-ExternalAgent", operator: "Meta", purpose: "training" },
77
+ { name: "Meta-ExternalFetcher", operator: "Meta", purpose: "user" },
78
+ { name: "FacebookBot", operator: "Meta", purpose: "training" },
79
+ { name: "meta-webindexer", operator: "Meta", purpose: "search" },
80
+ // Amazon
81
+ { name: "Amazonbot", operator: "Amazon", purpose: "search", rdns: ["crawl.amazonbot.amazon"] },
82
+ { name: "Amzn-SearchBot", operator: "Amazon", purpose: "search" },
83
+ { name: "Amzn-User", operator: "Amazon", purpose: "user" },
84
+ { name: "NovaAct", operator: "Amazon", purpose: "user" },
85
+ { name: "bedrockbot", operator: "Amazon", purpose: "other" },
86
+ // Microsoft
87
+ { name: "AzureAI-SearchBot", operator: "Microsoft", purpose: "search" },
88
+ // Mistral
89
+ { name: "MistralAI-Training", operator: "Mistral", purpose: "training" },
90
+ { name: "MistralAI-Index", operator: "Mistral", purpose: "search" },
91
+ { name: "MistralAI-User", operator: "Mistral", purpose: "user" },
92
+ // xAI
93
+ { name: "xAI-Grok", operator: "xAI", purpose: "training" },
94
+ { name: "GrokBot", operator: "xAI", purpose: "other" },
95
+ // ByteDance
96
+ { name: "Bytespider", operator: "ByteDance", purpose: "training" },
97
+ { name: "TikTokSpider", operator: "ByteDance", purpose: "training" },
98
+ { name: "DoubaoBot", operator: "ByteDance", purpose: "other" },
99
+ // Other AI companies
100
+ { name: "DeepSeekBot", operator: "DeepSeek", purpose: "training" },
101
+ { name: "QwenBot", operator: "Alibaba", purpose: "training" },
102
+ { name: "KimiBot", operator: "Moonshot AI", purpose: "training" },
103
+ { name: "MoonshotBot", operator: "Moonshot AI", purpose: "training" },
104
+ { name: "Kimi-SearchBot", operator: "Moonshot AI", purpose: "search" },
105
+ { name: "Kimi-User", operator: "Moonshot AI", purpose: "user" },
106
+ { name: "TongyiBot", operator: "Alibaba", purpose: "user" },
107
+ { name: "Hunyuan", operator: "Tencent", purpose: "training" },
108
+ { name: "ERNIEBot", operator: "Baidu", purpose: "training" },
109
+ { name: "YiyanBot", operator: "Baidu", purpose: "user" },
110
+ { name: "YiBot", operator: "01.AI", purpose: "training" },
111
+ { name: "CCBot", operator: "Common Crawl", purpose: "training" },
112
+ { name: "DuckAssistBot", operator: "DuckDuckGo", purpose: "search" },
113
+ { name: "cohere-training-data-crawler", operator: "Cohere", purpose: "training" },
114
+ { name: "cohere-ai", operator: "Cohere", purpose: "other" },
115
+ { name: "AI2Bot", operator: "Allen Institute for AI", purpose: "training" },
116
+ { name: "Ai2Bot-Dolma", operator: "Allen Institute for AI", purpose: "training" },
117
+ { name: "PanguBot", operator: "Huawei", purpose: "training" },
118
+ { name: "PetalBot", operator: "Huawei", purpose: "search" },
119
+ { name: "YandexAdditional", operator: "Yandex", purpose: "other", match: ["yandexadditionalbot"] },
120
+ { name: "Bravebot", operator: "Brave", purpose: "search" },
121
+ { name: "kagi-fetcher", operator: "Kagi", purpose: "user" },
122
+ { name: "ExaBot", operator: "Exa", purpose: "search" },
123
+ { name: "TavilyBot", operator: "Tavily", purpose: "search" },
124
+ { name: "LinerBot", operator: "Liner", purpose: "other" },
125
+ { name: "Manus-User", operator: "Manus", purpose: "user" },
126
+ { name: "Diffbot", operator: "Diffbot", purpose: "training" },
127
+ { name: "YouBot", operator: "You.com", purpose: "search" },
128
+ { name: "PhindBot", operator: "Phind", purpose: "search" },
129
+ { name: "iaskspider", operator: "iAsk.ai", purpose: "search" },
130
+ { name: "Timpibot", operator: "Timpi", purpose: "training" },
131
+ { name: "ImagesiftBot", operator: "Hive", purpose: "training" },
132
+ { name: "Kangaroo Bot", operator: "Kangaroo LLM", purpose: "training" },
133
+ { name: "img2dataset", operator: "img2dataset", purpose: "training" },
134
+ { name: "FirecrawlAgent", operator: "Firecrawl", purpose: "other" },
135
+ { name: "Crawl4AI", operator: "Crawl4AI (open source)", purpose: "other" },
136
+ { name: "Brightbot", operator: "Bright Data", purpose: "other" },
137
+ { name: "Cloudflare-AutoRAG", operator: "Cloudflare", purpose: "search" },
138
+ { name: "LAIONDownloader", operator: "LAION", purpose: "training" },
139
+ { name: "SemrushBot-OCOB", operator: "Semrush", purpose: "other" },
140
+ { name: "Cotoyogi", operator: "ROIS (Japan)", purpose: "training" },
141
+ { name: "ICC-Crawler", operator: "NICT (Japan)", purpose: "training" },
142
+ { name: "omgili", operator: "Webz.io", purpose: "training", match: ["omgilibot"] },
143
+ { name: "Webzio-Extended", operator: "Webz.io", purpose: "training", robotsOnly: true }
144
+ ];
145
+ var listUpdated = "2026-12-01";
146
+ var aiCrawlers = Object.freeze(LIST.map((c) => Object.freeze(c)));
147
+ var PURPOSES = ["training", "search", "user", "other"];
148
+ function isPurpose(value) {
149
+ return PURPOSES.includes(value);
150
+ }
151
+
152
+ // src/match.ts
153
+ var version = true ? "1.1.0" : "0.0.0-dev";
154
+ var escapeRegex = (s) => s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
155
+ var tokensOf = (c) => [c.name.toLowerCase(), ...c.match ?? []];
156
+ var BY_TOKEN = /* @__PURE__ */ new Map();
157
+ for (const c of aiCrawlers) if (!c.robotsOnly) for (const t of tokensOf(c)) BY_TOKEN.set(t, c);
158
+ var ALTERNATION = [...BY_TOKEN.keys()].sort((a, b) => b.length - a.length).map(escapeRegex).join("|");
159
+ var aiCrawlerPattern = new RegExp(`(?<![a-z0-9])(?:${ALTERNATION})(?![a-z0-9])`, "i");
160
+ function purposeFilter(purpose) {
161
+ if (purpose === void 0) return () => true;
162
+ const list = typeof purpose === "string" ? [purpose] : purpose;
163
+ for (const p of list) if (!isPurpose(p)) throw new TypeError(`Unknown purpose "${p}". Use: ${PURPOSES.join(", ")}.`);
164
+ return (c) => list.includes(c.purpose);
165
+ }
166
+ function getAiCrawler(userAgent, options = {}) {
167
+ if (!userAgent) return void 0;
168
+ const found = aiCrawlerPattern.exec(userAgent);
169
+ const crawler = found ? BY_TOKEN.get(found[0].toLowerCase()) : void 0;
170
+ if (!crawler || !purposeFilter(options.purpose)(crawler)) return void 0;
171
+ if (options.allow?.some((n) => n.toLowerCase() === crawler.name.toLowerCase())) return void 0;
172
+ return crawler;
173
+ }
174
+ function isAiCrawler(userAgent, options = {}) {
175
+ return getAiCrawler(userAgent, options) !== void 0;
176
+ }
177
+ var isAiBot = isAiCrawler;
178
+
179
+ // src/rules.ts
180
+ function blockedCrawlers(options = {}) {
181
+ const block = options.block ?? ["training"];
182
+ const inPurpose = block === "all" ? () => true : purposeFilter(block);
183
+ const allow = new Set((options.allow ?? []).map((n) => n.toLowerCase()));
184
+ return aiCrawlers.filter((c) => inPurpose(c) && !allow.has(c.name.toLowerCase()));
185
+ }
186
+ function header(options, comment = "#") {
187
+ const block = options.block ?? ["training"];
188
+ const what = block === "all" ? "all AI crawlers" : block.length ? block.join(", ") : "nothing";
189
+ const allowed = options.allow?.length ? ` \xB7 Allowed anyway: ${options.allow.join(", ")}` : "";
190
+ return `${comment} AI crawler rules, generated by aicrawlers ${version}
191
+ ${comment} Blocked: ${what}${allowed}
192
+ `;
193
+ }
194
+ function blockedTokens(options) {
195
+ return blockedCrawlers(options).filter((c) => !c.robotsOnly).flatMap(tokensOf).sort((a, b) => b.length - a.length);
196
+ }
197
+ function portableRegex(tokens, space) {
198
+ return `(^|[^a-z0-9])(${tokens.map((t) => escapeRegex(t).replace(/ /g, space)).join("|")})([^a-z0-9]|$)`;
199
+ }
200
+ function robotsTxt(options = {}) {
201
+ const crawlers = blockedCrawlers(options);
202
+ if (!crawlers.length) return header(options);
203
+ return `${header(options)}${crawlers.map((c) => `User-agent: ${c.name}`).join("\n")}
204
+ Disallow: /
205
+ `;
206
+ }
207
+ function nginxMap(options = {}) {
208
+ const tokens = blockedTokens(options).map((t) => escapeRegex(t).replace(/ /g, "\\s"));
209
+ const rule = tokens.length ? ` "~*(?<![a-z0-9])(?:${tokens.join("|")})(?![a-z0-9])" 1;
210
+ ` : "";
211
+ return `${header(options)}map $http_user_agent $ai_crawler {
212
+ default 0;
213
+ ${rule}}
214
+ `;
215
+ }
216
+ function apacheRules(options = {}) {
217
+ const tokens = blockedTokens(options);
218
+ if (!tokens.length) return header(options);
219
+ return `${header(options)}<IfModule mod_rewrite.c>
220
+ RewriteEngine On
221
+ RewriteCond %{HTTP_USER_AGENT} ${portableRegex(tokens, "\\s")} [NC]
222
+ RewriteRule ^ - [F,L]
223
+ </IfModule>
224
+ `;
225
+ }
226
+ function caddyRules(options = {}) {
227
+ const tokens = blockedTokens(options);
228
+ if (!tokens.length) return header(options);
229
+ return `${header(options)}@aicrawlers header_regexp User-Agent "(?i)${portableRegex(tokens, " ")}"
230
+ respond @aicrawlers 403
231
+ `;
232
+ }
233
+ function cloudflareExpression(options = {}) {
234
+ return cloudflareChecks(options).join(" or ");
235
+ }
236
+ var CLOUDFLARE_MAX = 4096;
237
+ var cloudflareChecks = (options) => blockedTokens(options).map((t) => `(lower(http.user_agent) contains "${t.replace(/["\\]/g, "\\$&")}")`);
238
+ function cloudflareExpressions(options = {}) {
239
+ const out = [];
240
+ let current = "";
241
+ for (const check of cloudflareChecks(options)) {
242
+ const next = current ? `${current} or ${check}` : check;
243
+ if (next.length > CLOUDFLARE_MAX && current) {
244
+ out.push(current);
245
+ current = check;
246
+ } else current = next;
247
+ }
248
+ if (current) out.push(current);
249
+ return out;
250
+ }
251
+ function nextProxy(options = {}) {
252
+ const block = options.block ?? ["training"];
253
+ const opts = [];
254
+ if (block !== "all") opts.push(`purpose: ${JSON.stringify(block)}`);
255
+ if (options.allow?.length) opts.push(`allow: ${JSON.stringify(options.allow)}`);
256
+ const args = opts.length ? `, { ${opts.join(", ")} }` : "";
257
+ return `${header(options, "//")}// Save as proxy.ts in your project root (Next.js 16+). For Next.js 15 and earlier, save it
258
+ // as middleware.ts and rename the function to middleware. Run: npm install aicrawlers
259
+ import { NextResponse, type NextRequest } from "next/server";
260
+ import { isAiCrawler } from "aicrawlers";
261
+
262
+ export function proxy(request: NextRequest) {
263
+ if (isAiCrawler(request.headers.get("user-agent")${args})) {
264
+ return new NextResponse("Forbidden", { status: 403 });
265
+ }
266
+ return NextResponse.next();
267
+ }
268
+ `;
269
+ }
270
+ // Annotate the CommonJS export names for ESM import in node:
271
+ 0 && (module.exports = {
272
+ aiCrawlerPattern,
273
+ aiCrawlers,
274
+ apacheRules,
275
+ blockedCrawlers,
276
+ caddyRules,
277
+ cloudflareExpression,
278
+ cloudflareExpressions,
279
+ getAiCrawler,
280
+ isAiBot,
281
+ isAiCrawler,
282
+ listUpdated,
283
+ nextProxy,
284
+ nginxMap,
285
+ purposes,
286
+ robotsTxt,
287
+ version
288
+ });
@@ -0,0 +1,73 @@
1
+ import { P as Purpose, A as AiCrawler } from './crawlers-eCgm_1OB.cjs';
2
+ export { a as aiCrawlers, l as listUpdated, b as purposes } from './crawlers-eCgm_1OB.cjs';
3
+
4
+ /** Version of this package. */
5
+ declare const version: string;
6
+ /**
7
+ * Matches the User-Agent of any known AI crawler, case-insensitively, as a whole token:
8
+ * "GPTBot/1.2" matches, "NotGPTBotX" does not. Use it where a regular expression is all you can
9
+ * pass, such as a web server or CDN rule.
10
+ */
11
+ declare const aiCrawlerPattern: RegExp;
12
+ interface MatchOptions {
13
+ /** Only count crawlers with these purposes, e.g. `["training"]`. */
14
+ purpose?: Purpose | readonly Purpose[];
15
+ /** Crawler names never to count, e.g. `["OAI-SearchBot"]`. Case-insensitive. */
16
+ allow?: readonly string[];
17
+ }
18
+ /**
19
+ * The AI crawler a User-Agent belongs to, or `undefined` when it isn't one.
20
+ *
21
+ * This reads the User-Agent only, which anyone can fake. To be sure a request really comes from
22
+ * the crawler, use `verifyAiCrawler` from "aicrawlers/verify".
23
+ */
24
+ declare function getAiCrawler(userAgent: string | null | undefined, options?: MatchOptions): Readonly<AiCrawler> | undefined;
25
+ /** Whether a User-Agent belongs to a known AI crawler. */
26
+ declare function isAiCrawler(userAgent: string | null | undefined, options?: MatchOptions): boolean;
27
+ /** Same as `isAiCrawler`. */
28
+ declare const isAiBot: typeof isAiCrawler;
29
+
30
+ interface RuleOptions {
31
+ /** Purposes to block. `"all"` blocks every AI crawler. Default: `["training"]`. */
32
+ block?: readonly Purpose[] | "all";
33
+ /** Crawler names to leave out even when their purpose is blocked, e.g. `["OAI-SearchBot"]`. */
34
+ allow?: readonly string[];
35
+ }
36
+ /** The crawlers a set of rule options blocks. */
37
+ declare function blockedCrawlers(options?: RuleOptions): Readonly<AiCrawler>[];
38
+ /**
39
+ * robots.txt rules that block AI crawlers. Paste them into robots.txt, or append them to your
40
+ * own rules. Polite crawlers obey robots.txt; to stop the rest, block them on the server too.
41
+ *
42
+ * ```js
43
+ * robotsTxt({ block: ["training"] }) // keep AI search, block training
44
+ * ```
45
+ */
46
+ declare function robotsTxt(options?: RuleOptions): string;
47
+ /**
48
+ * An nginx `map` that sets `$ai_crawler` to 1 for blocked crawlers. Put it in the `http` block,
49
+ * then add `if ($ai_crawler) { return 403; }` to a `server` or `location`.
50
+ */
51
+ declare function nginxMap(options?: RuleOptions): string;
52
+ /** Apache rules (`.htaccess` or the server config) that answer blocked crawlers with 403 Forbidden. Needs mod_rewrite. */
53
+ declare function apacheRules(options?: RuleOptions): string;
54
+ /** A Caddyfile snippet that answers blocked crawlers with 403. Paste it inside a site block. */
55
+ declare function caddyRules(options?: RuleOptions): string;
56
+ /**
57
+ * A Cloudflare WAF custom-rule expression matching blocked crawlers. In the dashboard, go to
58
+ * Security → WAF → Custom rules → Create rule → Edit expression, paste it, and choose Block.
59
+ * It uses `contains`, which every Cloudflare plan supports.
60
+ */
61
+ declare function cloudflareExpression(options?: RuleOptions): string;
62
+ /**
63
+ * The same rule as `cloudflareExpression`, split into as few expressions as fit Cloudflare's
64
+ * 4096-character limit. Create one custom rule per expression, each with the Block action.
65
+ */
66
+ declare function cloudflareExpressions(options?: RuleOptions): string[];
67
+ /**
68
+ * A Next.js `proxy.ts` (Next.js 16+; `middleware.ts` before that) that answers blocked
69
+ * crawlers with 403. It uses the aicrawlers package, so the list stays up to date with npm updates.
70
+ */
71
+ declare function nextProxy(options?: RuleOptions): string;
72
+
73
+ export { AiCrawler, type MatchOptions, Purpose, type RuleOptions, aiCrawlerPattern, apacheRules, blockedCrawlers, caddyRules, cloudflareExpression, cloudflareExpressions, getAiCrawler, isAiBot, isAiCrawler, nextProxy, nginxMap, robotsTxt, version };
@@ -0,0 +1,73 @@
1
+ import { P as Purpose, A as AiCrawler } from './crawlers-eCgm_1OB.js';
2
+ export { a as aiCrawlers, l as listUpdated, b as purposes } from './crawlers-eCgm_1OB.js';
3
+
4
+ /** Version of this package. */
5
+ declare const version: string;
6
+ /**
7
+ * Matches the User-Agent of any known AI crawler, case-insensitively, as a whole token:
8
+ * "GPTBot/1.2" matches, "NotGPTBotX" does not. Use it where a regular expression is all you can
9
+ * pass, such as a web server or CDN rule.
10
+ */
11
+ declare const aiCrawlerPattern: RegExp;
12
+ interface MatchOptions {
13
+ /** Only count crawlers with these purposes, e.g. `["training"]`. */
14
+ purpose?: Purpose | readonly Purpose[];
15
+ /** Crawler names never to count, e.g. `["OAI-SearchBot"]`. Case-insensitive. */
16
+ allow?: readonly string[];
17
+ }
18
+ /**
19
+ * The AI crawler a User-Agent belongs to, or `undefined` when it isn't one.
20
+ *
21
+ * This reads the User-Agent only, which anyone can fake. To be sure a request really comes from
22
+ * the crawler, use `verifyAiCrawler` from "aicrawlers/verify".
23
+ */
24
+ declare function getAiCrawler(userAgent: string | null | undefined, options?: MatchOptions): Readonly<AiCrawler> | undefined;
25
+ /** Whether a User-Agent belongs to a known AI crawler. */
26
+ declare function isAiCrawler(userAgent: string | null | undefined, options?: MatchOptions): boolean;
27
+ /** Same as `isAiCrawler`. */
28
+ declare const isAiBot: typeof isAiCrawler;
29
+
30
+ interface RuleOptions {
31
+ /** Purposes to block. `"all"` blocks every AI crawler. Default: `["training"]`. */
32
+ block?: readonly Purpose[] | "all";
33
+ /** Crawler names to leave out even when their purpose is blocked, e.g. `["OAI-SearchBot"]`. */
34
+ allow?: readonly string[];
35
+ }
36
+ /** The crawlers a set of rule options blocks. */
37
+ declare function blockedCrawlers(options?: RuleOptions): Readonly<AiCrawler>[];
38
+ /**
39
+ * robots.txt rules that block AI crawlers. Paste them into robots.txt, or append them to your
40
+ * own rules. Polite crawlers obey robots.txt; to stop the rest, block them on the server too.
41
+ *
42
+ * ```js
43
+ * robotsTxt({ block: ["training"] }) // keep AI search, block training
44
+ * ```
45
+ */
46
+ declare function robotsTxt(options?: RuleOptions): string;
47
+ /**
48
+ * An nginx `map` that sets `$ai_crawler` to 1 for blocked crawlers. Put it in the `http` block,
49
+ * then add `if ($ai_crawler) { return 403; }` to a `server` or `location`.
50
+ */
51
+ declare function nginxMap(options?: RuleOptions): string;
52
+ /** Apache rules (`.htaccess` or the server config) that answer blocked crawlers with 403 Forbidden. Needs mod_rewrite. */
53
+ declare function apacheRules(options?: RuleOptions): string;
54
+ /** A Caddyfile snippet that answers blocked crawlers with 403. Paste it inside a site block. */
55
+ declare function caddyRules(options?: RuleOptions): string;
56
+ /**
57
+ * A Cloudflare WAF custom-rule expression matching blocked crawlers. In the dashboard, go to
58
+ * Security → WAF → Custom rules → Create rule → Edit expression, paste it, and choose Block.
59
+ * It uses `contains`, which every Cloudflare plan supports.
60
+ */
61
+ declare function cloudflareExpression(options?: RuleOptions): string;
62
+ /**
63
+ * The same rule as `cloudflareExpression`, split into as few expressions as fit Cloudflare's
64
+ * 4096-character limit. Create one custom rule per expression, each with the Block action.
65
+ */
66
+ declare function cloudflareExpressions(options?: RuleOptions): string[];
67
+ /**
68
+ * A Next.js `proxy.ts` (Next.js 16+; `middleware.ts` before that) that answers blocked
69
+ * crawlers with 403. It uses the aicrawlers package, so the list stays up to date with npm updates.
70
+ */
71
+ declare function nextProxy(options?: RuleOptions): string;
72
+
73
+ export { AiCrawler, type MatchOptions, Purpose, type RuleOptions, aiCrawlerPattern, apacheRules, blockedCrawlers, caddyRules, cloudflareExpression, cloudflareExpressions, getAiCrawler, isAiBot, isAiCrawler, nextProxy, nginxMap, robotsTxt, version };
package/dist/index.js ADDED
@@ -0,0 +1,123 @@
1
+ import {
2
+ PURPOSES,
3
+ aiCrawlerPattern,
4
+ aiCrawlers,
5
+ escapeRegex,
6
+ getAiCrawler,
7
+ isAiBot,
8
+ isAiCrawler,
9
+ listUpdated,
10
+ purposeFilter,
11
+ tokensOf,
12
+ version
13
+ } from "./chunk-UCVDZUSO.js";
14
+
15
+ // src/rules.ts
16
+ function blockedCrawlers(options = {}) {
17
+ const block = options.block ?? ["training"];
18
+ const inPurpose = block === "all" ? () => true : purposeFilter(block);
19
+ const allow = new Set((options.allow ?? []).map((n) => n.toLowerCase()));
20
+ return aiCrawlers.filter((c) => inPurpose(c) && !allow.has(c.name.toLowerCase()));
21
+ }
22
+ function header(options, comment = "#") {
23
+ const block = options.block ?? ["training"];
24
+ const what = block === "all" ? "all AI crawlers" : block.length ? block.join(", ") : "nothing";
25
+ const allowed = options.allow?.length ? ` \xB7 Allowed anyway: ${options.allow.join(", ")}` : "";
26
+ return `${comment} AI crawler rules, generated by aicrawlers ${version}
27
+ ${comment} Blocked: ${what}${allowed}
28
+ `;
29
+ }
30
+ function blockedTokens(options) {
31
+ return blockedCrawlers(options).filter((c) => !c.robotsOnly).flatMap(tokensOf).sort((a, b) => b.length - a.length);
32
+ }
33
+ function portableRegex(tokens, space) {
34
+ return `(^|[^a-z0-9])(${tokens.map((t) => escapeRegex(t).replace(/ /g, space)).join("|")})([^a-z0-9]|$)`;
35
+ }
36
+ function robotsTxt(options = {}) {
37
+ const crawlers = blockedCrawlers(options);
38
+ if (!crawlers.length) return header(options);
39
+ return `${header(options)}${crawlers.map((c) => `User-agent: ${c.name}`).join("\n")}
40
+ Disallow: /
41
+ `;
42
+ }
43
+ function nginxMap(options = {}) {
44
+ const tokens = blockedTokens(options).map((t) => escapeRegex(t).replace(/ /g, "\\s"));
45
+ const rule = tokens.length ? ` "~*(?<![a-z0-9])(?:${tokens.join("|")})(?![a-z0-9])" 1;
46
+ ` : "";
47
+ return `${header(options)}map $http_user_agent $ai_crawler {
48
+ default 0;
49
+ ${rule}}
50
+ `;
51
+ }
52
+ function apacheRules(options = {}) {
53
+ const tokens = blockedTokens(options);
54
+ if (!tokens.length) return header(options);
55
+ return `${header(options)}<IfModule mod_rewrite.c>
56
+ RewriteEngine On
57
+ RewriteCond %{HTTP_USER_AGENT} ${portableRegex(tokens, "\\s")} [NC]
58
+ RewriteRule ^ - [F,L]
59
+ </IfModule>
60
+ `;
61
+ }
62
+ function caddyRules(options = {}) {
63
+ const tokens = blockedTokens(options);
64
+ if (!tokens.length) return header(options);
65
+ return `${header(options)}@aicrawlers header_regexp User-Agent "(?i)${portableRegex(tokens, " ")}"
66
+ respond @aicrawlers 403
67
+ `;
68
+ }
69
+ function cloudflareExpression(options = {}) {
70
+ return cloudflareChecks(options).join(" or ");
71
+ }
72
+ var CLOUDFLARE_MAX = 4096;
73
+ var cloudflareChecks = (options) => blockedTokens(options).map((t) => `(lower(http.user_agent) contains "${t.replace(/["\\]/g, "\\$&")}")`);
74
+ function cloudflareExpressions(options = {}) {
75
+ const out = [];
76
+ let current = "";
77
+ for (const check of cloudflareChecks(options)) {
78
+ const next = current ? `${current} or ${check}` : check;
79
+ if (next.length > CLOUDFLARE_MAX && current) {
80
+ out.push(current);
81
+ current = check;
82
+ } else current = next;
83
+ }
84
+ if (current) out.push(current);
85
+ return out;
86
+ }
87
+ function nextProxy(options = {}) {
88
+ const block = options.block ?? ["training"];
89
+ const opts = [];
90
+ if (block !== "all") opts.push(`purpose: ${JSON.stringify(block)}`);
91
+ if (options.allow?.length) opts.push(`allow: ${JSON.stringify(options.allow)}`);
92
+ const args = opts.length ? `, { ${opts.join(", ")} }` : "";
93
+ return `${header(options, "//")}// Save as proxy.ts in your project root (Next.js 16+). For Next.js 15 and earlier, save it
94
+ // as middleware.ts and rename the function to middleware. Run: npm install aicrawlers
95
+ import { NextResponse, type NextRequest } from "next/server";
96
+ import { isAiCrawler } from "aicrawlers";
97
+
98
+ export function proxy(request: NextRequest) {
99
+ if (isAiCrawler(request.headers.get("user-agent")${args})) {
100
+ return new NextResponse("Forbidden", { status: 403 });
101
+ }
102
+ return NextResponse.next();
103
+ }
104
+ `;
105
+ }
106
+ export {
107
+ aiCrawlerPattern,
108
+ aiCrawlers,
109
+ apacheRules,
110
+ blockedCrawlers,
111
+ caddyRules,
112
+ cloudflareExpression,
113
+ cloudflareExpressions,
114
+ getAiCrawler,
115
+ isAiBot,
116
+ isAiCrawler,
117
+ listUpdated,
118
+ nextProxy,
119
+ nginxMap,
120
+ PURPOSES as purposes,
121
+ robotsTxt,
122
+ version
123
+ };