aicrawlers 1.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 CHANGED
@@ -22,10 +22,16 @@ var src_exports = {};
22
22
  __export(src_exports, {
23
23
  aiCrawlerPattern: () => aiCrawlerPattern,
24
24
  aiCrawlers: () => aiCrawlers,
25
+ apacheRules: () => apacheRules,
25
26
  blockedCrawlers: () => blockedCrawlers,
27
+ caddyRules: () => caddyRules,
28
+ cloudflareExpression: () => cloudflareExpression,
29
+ cloudflareExpressions: () => cloudflareExpressions,
26
30
  getAiCrawler: () => getAiCrawler,
27
31
  isAiBot: () => isAiBot,
28
32
  isAiCrawler: () => isAiCrawler,
33
+ listUpdated: () => listUpdated,
34
+ nextProxy: () => nextProxy,
29
35
  nginxMap: () => nginxMap,
30
36
  purposes: () => PURPOSES,
31
37
  robotsTxt: () => robotsTxt,
@@ -40,12 +46,16 @@ var LIST = [
40
46
  { name: "GPTBot", operator: "OpenAI", purpose: "training", ipRanges: "https://openai.com/gptbot.json" },
41
47
  { name: "OAI-SearchBot", operator: "OpenAI", purpose: "search", ipRanges: "https://openai.com/searchbot.json" },
42
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" },
43
51
  // Anthropic
44
52
  { name: "ClaudeBot", operator: "Anthropic", purpose: "training" },
45
53
  { name: "Claude-SearchBot", operator: "Anthropic", purpose: "search" },
46
54
  { name: "Claude-User", operator: "Anthropic", purpose: "user" },
47
55
  { name: "Claude-Web", operator: "Anthropic", purpose: "other" },
48
56
  { name: "anthropic-ai", operator: "Anthropic", purpose: "training" },
57
+ { name: "Claude-Code", operator: "Anthropic", purpose: "user" },
58
+ { name: "Claude-Agent", operator: "Anthropic", purpose: "user" },
49
59
  // Perplexity
50
60
  { name: "PerplexityBot", operator: "Perplexity", purpose: "search", ipRanges: "https://www.perplexity.com/perplexitybot.json" },
51
61
  { name: "Perplexity-User", operator: "Perplexity", purpose: "user", ipRanges: "https://www.perplexity.com/perplexity-user.json" },
@@ -53,6 +63,12 @@ var LIST = [
53
63
  { name: "Google-Extended", operator: "Google", purpose: "training", robotsOnly: true },
54
64
  { name: "Google-CloudVertexBot", operator: "Google", purpose: "other", rdns: GOOGLE_RDNS },
55
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" },
56
72
  // Apple
57
73
  { name: "Applebot", operator: "Apple", purpose: "search", rdns: ["applebot.apple.com"] },
58
74
  { name: "Applebot-Extended", operator: "Apple", purpose: "training", robotsOnly: true },
@@ -60,17 +76,53 @@ var LIST = [
60
76
  { name: "Meta-ExternalAgent", operator: "Meta", purpose: "training" },
61
77
  { name: "Meta-ExternalFetcher", operator: "Meta", purpose: "user" },
62
78
  { name: "FacebookBot", operator: "Meta", purpose: "training" },
63
- // Others
64
- { name: "Amazonbot", operator: "Amazon", purpose: "search" },
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
65
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" },
66
111
  { name: "CCBot", operator: "Common Crawl", purpose: "training" },
67
112
  { name: "DuckAssistBot", operator: "DuckDuckGo", purpose: "search" },
68
- { name: "MistralAI-User", operator: "Mistral", purpose: "user" },
69
113
  { name: "cohere-training-data-crawler", operator: "Cohere", purpose: "training" },
70
114
  { name: "cohere-ai", operator: "Cohere", purpose: "other" },
71
115
  { name: "AI2Bot", operator: "Allen Institute for AI", purpose: "training" },
72
116
  { name: "Ai2Bot-Dolma", operator: "Allen Institute for AI", purpose: "training" },
73
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" },
74
126
  { name: "Diffbot", operator: "Diffbot", purpose: "training" },
75
127
  { name: "YouBot", operator: "You.com", purpose: "search" },
76
128
  { name: "PhindBot", operator: "Phind", purpose: "search" },
@@ -80,22 +132,30 @@ var LIST = [
80
132
  { name: "Kangaroo Bot", operator: "Kangaroo LLM", purpose: "training" },
81
133
  { name: "img2dataset", operator: "img2dataset", purpose: "training" },
82
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" },
83
142
  { name: "omgili", operator: "Webz.io", purpose: "training", match: ["omgilibot"] },
84
143
  { name: "Webzio-Extended", operator: "Webz.io", purpose: "training", robotsOnly: true }
85
144
  ];
145
+ var listUpdated = "2026-12-01";
86
146
  var aiCrawlers = Object.freeze(LIST.map((c) => Object.freeze(c)));
87
147
  var PURPOSES = ["training", "search", "user", "other"];
88
148
  function isPurpose(value) {
89
149
  return PURPOSES.includes(value);
90
150
  }
91
151
 
92
- // src/index.ts
93
- var version = true ? "1.0.0" : "0.0.0-dev";
94
- var escape = (s) => s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
152
+ // src/match.ts
153
+ var version = true ? "1.1.0" : "0.0.0-dev";
154
+ var escapeRegex = (s) => s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
95
155
  var tokensOf = (c) => [c.name.toLowerCase(), ...c.match ?? []];
96
156
  var BY_TOKEN = /* @__PURE__ */ new Map();
97
157
  for (const c of aiCrawlers) if (!c.robotsOnly) for (const t of tokensOf(c)) BY_TOKEN.set(t, c);
98
- var ALTERNATION = [...BY_TOKEN.keys()].sort((a, b) => b.length - a.length).map(escape).join("|");
158
+ var ALTERNATION = [...BY_TOKEN.keys()].sort((a, b) => b.length - a.length).map(escapeRegex).join("|");
99
159
  var aiCrawlerPattern = new RegExp(`(?<![a-z0-9])(?:${ALTERNATION})(?![a-z0-9])`, "i");
100
160
  function purposeFilter(purpose) {
101
161
  if (purpose === void 0) return () => true;
@@ -107,54 +167,120 @@ function getAiCrawler(userAgent, options = {}) {
107
167
  if (!userAgent) return void 0;
108
168
  const found = aiCrawlerPattern.exec(userAgent);
109
169
  const crawler = found ? BY_TOKEN.get(found[0].toLowerCase()) : void 0;
110
- return crawler && purposeFilter(options.purpose)(crawler) ? crawler : 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;
111
173
  }
112
174
  function isAiCrawler(userAgent, options = {}) {
113
175
  return getAiCrawler(userAgent, options) !== void 0;
114
176
  }
115
177
  var isAiBot = isAiCrawler;
178
+
179
+ // src/rules.ts
116
180
  function blockedCrawlers(options = {}) {
117
181
  const block = options.block ?? ["training"];
118
182
  const inPurpose = block === "all" ? () => true : purposeFilter(block);
119
183
  const allow = new Set((options.allow ?? []).map((n) => n.toLowerCase()));
120
184
  return aiCrawlers.filter((c) => inPurpose(c) && !allow.has(c.name.toLowerCase()));
121
185
  }
122
- function describe(options) {
186
+ function header(options, comment = "#") {
123
187
  const block = options.block ?? ["training"];
124
188
  const what = block === "all" ? "all AI crawlers" : block.length ? block.join(", ") : "nothing";
125
189
  const allowed = options.allow?.length ? ` \xB7 Allowed anyway: ${options.allow.join(", ")}` : "";
126
- return `Blocked: ${what}${allowed}`;
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]|$)`;
127
199
  }
128
200
  function robotsTxt(options = {}) {
129
201
  const crawlers = blockedCrawlers(options);
130
- const head = `# AI crawler rules, generated by aicrawlers ${version}
131
- # ${describe(options)}
132
- `;
133
- if (!crawlers.length) return head;
134
- return `${head}${crawlers.map((c) => `User-agent: ${c.name}`).join("\n")}
202
+ if (!crawlers.length) return header(options);
203
+ return `${header(options)}${crawlers.map((c) => `User-agent: ${c.name}`).join("\n")}
135
204
  Disallow: /
136
205
  `;
137
206
  }
138
207
  function nginxMap(options = {}) {
139
- const tokens = blockedCrawlers(options).filter((c) => !c.robotsOnly).flatMap(tokensOf).sort((a, b) => b.length - a.length).map((t) => escape(t).replace(/ /g, "\\s"));
140
- const head = `# AI crawler rules, generated by aicrawlers ${version}
141
- # ${describe(options)}
142
- `;
208
+ const tokens = blockedTokens(options).map((t) => escapeRegex(t).replace(/ /g, "\\s"));
143
209
  const rule = tokens.length ? ` "~*(?<![a-z0-9])(?:${tokens.join("|")})(?![a-z0-9])" 1;
144
210
  ` : "";
145
- return `${head}map $http_user_agent $ai_crawler {
211
+ return `${header(options)}map $http_user_agent $ai_crawler {
146
212
  default 0;
147
213
  ${rule}}
148
214
  `;
149
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
+ }
150
270
  // Annotate the CommonJS export names for ESM import in node:
151
271
  0 && (module.exports = {
152
272
  aiCrawlerPattern,
153
273
  aiCrawlers,
274
+ apacheRules,
154
275
  blockedCrawlers,
276
+ caddyRules,
277
+ cloudflareExpression,
278
+ cloudflareExpressions,
155
279
  getAiCrawler,
156
280
  isAiBot,
157
281
  isAiCrawler,
282
+ listUpdated,
283
+ nextProxy,
158
284
  nginxMap,
159
285
  purposes,
160
286
  robotsTxt,
package/dist/index.d.cts CHANGED
@@ -1,29 +1,5 @@
1
- type Purpose = "training" | "search" | "user" | "other";
2
- interface AiCrawler {
3
- /** Product token: what the crawler sends in its User-Agent and obeys in robots.txt. */
4
- name: string;
5
- /** Company or project that runs it. */
6
- operator: string;
7
- /**
8
- * What it collects pages for, from the operator's documentation:
9
- * - "training": data to train AI models
10
- * - "search": an AI search or answer product
11
- * - "user": fetches a page because a person asked an assistant about it
12
- * - "other": anything else, or not documented
13
- */
14
- purpose: Purpose;
15
- /** Extra lowercase strings matched in the User-Agent. The name always matches. */
16
- match?: string[];
17
- /** Only used in robots.txt: it never appears in a User-Agent. */
18
- robotsOnly?: boolean;
19
- /** URL of the operator's official JSON list of IP ranges, to tell real requests from fakes. */
20
- ipRanges?: string;
21
- /** Reverse-DNS hostname suffixes of real requests, confirmed with a forward lookup. */
22
- rdns?: string[];
23
- }
24
- /** Known AI crawlers and fetchers. Frozen: copy it before changing anything. */
25
- declare const aiCrawlers: readonly Readonly<AiCrawler>[];
26
- declare const PURPOSES: readonly Purpose[];
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';
27
3
 
28
4
  /** Version of this package. */
29
5
  declare const version: string;
@@ -36,18 +12,21 @@ declare const aiCrawlerPattern: RegExp;
36
12
  interface MatchOptions {
37
13
  /** Only count crawlers with these purposes, e.g. `["training"]`. */
38
14
  purpose?: Purpose | readonly Purpose[];
15
+ /** Crawler names never to count, e.g. `["OAI-SearchBot"]`. Case-insensitive. */
16
+ allow?: readonly string[];
39
17
  }
40
18
  /**
41
19
  * The AI crawler a User-Agent belongs to, or `undefined` when it isn't one.
42
20
  *
43
21
  * This reads the User-Agent only, which anyone can fake. To be sure a request really comes from
44
- * the crawler, check its IP against `ipRanges` or `rdns` where the crawler has them.
22
+ * the crawler, use `verifyAiCrawler` from "aicrawlers/verify".
45
23
  */
46
24
  declare function getAiCrawler(userAgent: string | null | undefined, options?: MatchOptions): Readonly<AiCrawler> | undefined;
47
25
  /** Whether a User-Agent belongs to a known AI crawler. */
48
26
  declare function isAiCrawler(userAgent: string | null | undefined, options?: MatchOptions): boolean;
49
27
  /** Same as `isAiCrawler`. */
50
28
  declare const isAiBot: typeof isAiCrawler;
29
+
51
30
  interface RuleOptions {
52
31
  /** Purposes to block. `"all"` blocks every AI crawler. Default: `["training"]`. */
53
32
  block?: readonly Purpose[] | "all";
@@ -70,5 +49,25 @@ declare function robotsTxt(options?: RuleOptions): string;
70
49
  * then add `if ($ai_crawler) { return 403; }` to a `server` or `location`.
71
50
  */
72
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;
73
72
 
74
- export { type AiCrawler, type MatchOptions, type Purpose, type RuleOptions, aiCrawlerPattern, aiCrawlers, blockedCrawlers, getAiCrawler, isAiBot, isAiCrawler, nginxMap, PURPOSES as purposes, robotsTxt, version };
73
+ export { AiCrawler, type MatchOptions, Purpose, type RuleOptions, aiCrawlerPattern, apacheRules, blockedCrawlers, caddyRules, cloudflareExpression, cloudflareExpressions, getAiCrawler, isAiBot, isAiCrawler, nextProxy, nginxMap, robotsTxt, version };
package/dist/index.d.ts CHANGED
@@ -1,29 +1,5 @@
1
- type Purpose = "training" | "search" | "user" | "other";
2
- interface AiCrawler {
3
- /** Product token: what the crawler sends in its User-Agent and obeys in robots.txt. */
4
- name: string;
5
- /** Company or project that runs it. */
6
- operator: string;
7
- /**
8
- * What it collects pages for, from the operator's documentation:
9
- * - "training": data to train AI models
10
- * - "search": an AI search or answer product
11
- * - "user": fetches a page because a person asked an assistant about it
12
- * - "other": anything else, or not documented
13
- */
14
- purpose: Purpose;
15
- /** Extra lowercase strings matched in the User-Agent. The name always matches. */
16
- match?: string[];
17
- /** Only used in robots.txt: it never appears in a User-Agent. */
18
- robotsOnly?: boolean;
19
- /** URL of the operator's official JSON list of IP ranges, to tell real requests from fakes. */
20
- ipRanges?: string;
21
- /** Reverse-DNS hostname suffixes of real requests, confirmed with a forward lookup. */
22
- rdns?: string[];
23
- }
24
- /** Known AI crawlers and fetchers. Frozen: copy it before changing anything. */
25
- declare const aiCrawlers: readonly Readonly<AiCrawler>[];
26
- declare const PURPOSES: readonly Purpose[];
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';
27
3
 
28
4
  /** Version of this package. */
29
5
  declare const version: string;
@@ -36,18 +12,21 @@ declare const aiCrawlerPattern: RegExp;
36
12
  interface MatchOptions {
37
13
  /** Only count crawlers with these purposes, e.g. `["training"]`. */
38
14
  purpose?: Purpose | readonly Purpose[];
15
+ /** Crawler names never to count, e.g. `["OAI-SearchBot"]`. Case-insensitive. */
16
+ allow?: readonly string[];
39
17
  }
40
18
  /**
41
19
  * The AI crawler a User-Agent belongs to, or `undefined` when it isn't one.
42
20
  *
43
21
  * This reads the User-Agent only, which anyone can fake. To be sure a request really comes from
44
- * the crawler, check its IP against `ipRanges` or `rdns` where the crawler has them.
22
+ * the crawler, use `verifyAiCrawler` from "aicrawlers/verify".
45
23
  */
46
24
  declare function getAiCrawler(userAgent: string | null | undefined, options?: MatchOptions): Readonly<AiCrawler> | undefined;
47
25
  /** Whether a User-Agent belongs to a known AI crawler. */
48
26
  declare function isAiCrawler(userAgent: string | null | undefined, options?: MatchOptions): boolean;
49
27
  /** Same as `isAiCrawler`. */
50
28
  declare const isAiBot: typeof isAiCrawler;
29
+
51
30
  interface RuleOptions {
52
31
  /** Purposes to block. `"all"` blocks every AI crawler. Default: `["training"]`. */
53
32
  block?: readonly Purpose[] | "all";
@@ -70,5 +49,25 @@ declare function robotsTxt(options?: RuleOptions): string;
70
49
  * then add `if ($ai_crawler) { return 403; }` to a `server` or `location`.
71
50
  */
72
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;
73
72
 
74
- export { type AiCrawler, type MatchOptions, type Purpose, type RuleOptions, aiCrawlerPattern, aiCrawlers, blockedCrawlers, getAiCrawler, isAiBot, isAiCrawler, nginxMap, PURPOSES as purposes, robotsTxt, version };
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 CHANGED
@@ -1,124 +1,121 @@
1
- // src/crawlers.ts
2
- var GOOGLE_RDNS = ["googlebot.com", "google.com", "googleusercontent.com"];
3
- var LIST = [
4
- // OpenAI
5
- { name: "GPTBot", operator: "OpenAI", purpose: "training", ipRanges: "https://openai.com/gptbot.json" },
6
- { name: "OAI-SearchBot", operator: "OpenAI", purpose: "search", ipRanges: "https://openai.com/searchbot.json" },
7
- { name: "ChatGPT-User", operator: "OpenAI", purpose: "user", ipRanges: "https://openai.com/chatgpt-user.json" },
8
- // Anthropic
9
- { name: "ClaudeBot", operator: "Anthropic", purpose: "training" },
10
- { name: "Claude-SearchBot", operator: "Anthropic", purpose: "search" },
11
- { name: "Claude-User", operator: "Anthropic", purpose: "user" },
12
- { name: "Claude-Web", operator: "Anthropic", purpose: "other" },
13
- { name: "anthropic-ai", operator: "Anthropic", purpose: "training" },
14
- // Perplexity
15
- { name: "PerplexityBot", operator: "Perplexity", purpose: "search", ipRanges: "https://www.perplexity.com/perplexitybot.json" },
16
- { name: "Perplexity-User", operator: "Perplexity", purpose: "user", ipRanges: "https://www.perplexity.com/perplexity-user.json" },
17
- // Google
18
- { name: "Google-Extended", operator: "Google", purpose: "training", robotsOnly: true },
19
- { name: "Google-CloudVertexBot", operator: "Google", purpose: "other", rdns: GOOGLE_RDNS },
20
- { name: "GoogleOther", operator: "Google", purpose: "other", rdns: GOOGLE_RDNS },
21
- // Apple
22
- { name: "Applebot", operator: "Apple", purpose: "search", rdns: ["applebot.apple.com"] },
23
- { name: "Applebot-Extended", operator: "Apple", purpose: "training", robotsOnly: true },
24
- // Meta
25
- { name: "Meta-ExternalAgent", operator: "Meta", purpose: "training" },
26
- { name: "Meta-ExternalFetcher", operator: "Meta", purpose: "user" },
27
- { name: "FacebookBot", operator: "Meta", purpose: "training" },
28
- // Others
29
- { name: "Amazonbot", operator: "Amazon", purpose: "search" },
30
- { name: "Bytespider", operator: "ByteDance", purpose: "training" },
31
- { name: "CCBot", operator: "Common Crawl", purpose: "training" },
32
- { name: "DuckAssistBot", operator: "DuckDuckGo", purpose: "search" },
33
- { name: "MistralAI-User", operator: "Mistral", purpose: "user" },
34
- { name: "cohere-training-data-crawler", operator: "Cohere", purpose: "training" },
35
- { name: "cohere-ai", operator: "Cohere", purpose: "other" },
36
- { name: "AI2Bot", operator: "Allen Institute for AI", purpose: "training" },
37
- { name: "Ai2Bot-Dolma", operator: "Allen Institute for AI", purpose: "training" },
38
- { name: "PanguBot", operator: "Huawei", purpose: "training" },
39
- { name: "Diffbot", operator: "Diffbot", purpose: "training" },
40
- { name: "YouBot", operator: "You.com", purpose: "search" },
41
- { name: "PhindBot", operator: "Phind", purpose: "search" },
42
- { name: "iaskspider", operator: "iAsk.ai", purpose: "search" },
43
- { name: "Timpibot", operator: "Timpi", purpose: "training" },
44
- { name: "ImagesiftBot", operator: "Hive", purpose: "training" },
45
- { name: "Kangaroo Bot", operator: "Kangaroo LLM", purpose: "training" },
46
- { name: "img2dataset", operator: "img2dataset", purpose: "training" },
47
- { name: "FirecrawlAgent", operator: "Firecrawl", purpose: "other" },
48
- { name: "omgili", operator: "Webz.io", purpose: "training", match: ["omgilibot"] },
49
- { name: "Webzio-Extended", operator: "Webz.io", purpose: "training", robotsOnly: true }
50
- ];
51
- var aiCrawlers = Object.freeze(LIST.map((c) => Object.freeze(c)));
52
- var PURPOSES = ["training", "search", "user", "other"];
53
- function isPurpose(value) {
54
- return PURPOSES.includes(value);
55
- }
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";
56
14
 
57
- // src/index.ts
58
- var version = true ? "1.0.0" : "0.0.0-dev";
59
- var escape = (s) => s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
60
- var tokensOf = (c) => [c.name.toLowerCase(), ...c.match ?? []];
61
- var BY_TOKEN = /* @__PURE__ */ new Map();
62
- for (const c of aiCrawlers) if (!c.robotsOnly) for (const t of tokensOf(c)) BY_TOKEN.set(t, c);
63
- var ALTERNATION = [...BY_TOKEN.keys()].sort((a, b) => b.length - a.length).map(escape).join("|");
64
- var aiCrawlerPattern = new RegExp(`(?<![a-z0-9])(?:${ALTERNATION})(?![a-z0-9])`, "i");
65
- function purposeFilter(purpose) {
66
- if (purpose === void 0) return () => true;
67
- const list = typeof purpose === "string" ? [purpose] : purpose;
68
- for (const p of list) if (!isPurpose(p)) throw new TypeError(`Unknown purpose "${p}". Use: ${PURPOSES.join(", ")}.`);
69
- return (c) => list.includes(c.purpose);
70
- }
71
- function getAiCrawler(userAgent, options = {}) {
72
- if (!userAgent) return void 0;
73
- const found = aiCrawlerPattern.exec(userAgent);
74
- const crawler = found ? BY_TOKEN.get(found[0].toLowerCase()) : void 0;
75
- return crawler && purposeFilter(options.purpose)(crawler) ? crawler : void 0;
76
- }
77
- function isAiCrawler(userAgent, options = {}) {
78
- return getAiCrawler(userAgent, options) !== void 0;
79
- }
80
- var isAiBot = isAiCrawler;
15
+ // src/rules.ts
81
16
  function blockedCrawlers(options = {}) {
82
17
  const block = options.block ?? ["training"];
83
18
  const inPurpose = block === "all" ? () => true : purposeFilter(block);
84
19
  const allow = new Set((options.allow ?? []).map((n) => n.toLowerCase()));
85
20
  return aiCrawlers.filter((c) => inPurpose(c) && !allow.has(c.name.toLowerCase()));
86
21
  }
87
- function describe(options) {
22
+ function header(options, comment = "#") {
88
23
  const block = options.block ?? ["training"];
89
24
  const what = block === "all" ? "all AI crawlers" : block.length ? block.join(", ") : "nothing";
90
25
  const allowed = options.allow?.length ? ` \xB7 Allowed anyway: ${options.allow.join(", ")}` : "";
91
- return `Blocked: ${what}${allowed}`;
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]|$)`;
92
35
  }
93
36
  function robotsTxt(options = {}) {
94
37
  const crawlers = blockedCrawlers(options);
95
- const head = `# AI crawler rules, generated by aicrawlers ${version}
96
- # ${describe(options)}
97
- `;
98
- if (!crawlers.length) return head;
99
- return `${head}${crawlers.map((c) => `User-agent: ${c.name}`).join("\n")}
38
+ if (!crawlers.length) return header(options);
39
+ return `${header(options)}${crawlers.map((c) => `User-agent: ${c.name}`).join("\n")}
100
40
  Disallow: /
101
41
  `;
102
42
  }
103
43
  function nginxMap(options = {}) {
104
- const tokens = blockedCrawlers(options).filter((c) => !c.robotsOnly).flatMap(tokensOf).sort((a, b) => b.length - a.length).map((t) => escape(t).replace(/ /g, "\\s"));
105
- const head = `# AI crawler rules, generated by aicrawlers ${version}
106
- # ${describe(options)}
107
- `;
44
+ const tokens = blockedTokens(options).map((t) => escapeRegex(t).replace(/ /g, "\\s"));
108
45
  const rule = tokens.length ? ` "~*(?<![a-z0-9])(?:${tokens.join("|")})(?![a-z0-9])" 1;
109
46
  ` : "";
110
- return `${head}map $http_user_agent $ai_crawler {
47
+ return `${header(options)}map $http_user_agent $ai_crawler {
111
48
  default 0;
112
49
  ${rule}}
113
50
  `;
114
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
+ }
115
106
  export {
116
107
  aiCrawlerPattern,
117
108
  aiCrawlers,
109
+ apacheRules,
118
110
  blockedCrawlers,
111
+ caddyRules,
112
+ cloudflareExpression,
113
+ cloudflareExpressions,
119
114
  getAiCrawler,
120
115
  isAiBot,
121
116
  isAiCrawler,
117
+ listUpdated,
118
+ nextProxy,
122
119
  nginxMap,
123
120
  PURPOSES as purposes,
124
121
  robotsTxt,