init commit, dynamically create robot txt with dark visitors api
This commit is contained in:
commit
9a53b10c8e
|
@ -0,0 +1,3 @@
|
||||||
|
dist/
|
||||||
|
node_modules/
|
||||||
|
.envrc
|
|
@ -0,0 +1 @@
|
||||||
|
nodejs 22.13.1
|
|
@ -0,0 +1,7 @@
|
||||||
|
Copyright 2024 silentsilas
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
@ -0,0 +1,54 @@
|
||||||
|
# @silentsilas/vite-plugin-ai-robots
|
||||||
|
|
||||||
|
[](https://www.npmjs.com/package/@silentsilas/vite-plugin-ai-robots)
|
||||||
|
[](https://opensource.org/licenses/MIT)
|
||||||
|
|
||||||
|
Vite plugin that automatically generates and updates a `robots.txt` file blocking AI agents using the [Dark Visitors](https://darkvisitors.com/) API.
|
||||||
|
|
||||||
|
## Installation
|
||||||
|
|
||||||
|
Create a free account at [Dark Visitors](https://darkvisitors.com/) and obtain your access token.
|
||||||
|
|
||||||
|
```bash
|
||||||
|
npm install @silentsilas/vite-plugin-ai-robots --save-dev
|
||||||
|
```
|
||||||
|
|
||||||
|
Add the token to your environment, and pass it to the plugin's config. You should now see a `robots.txt` file in your output directory during builds.
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
|
||||||
|
### Basic Configuration
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// vite.config.ts
|
||||||
|
import { defineConfig } from "vite";
|
||||||
|
import { aiRobots } from "@silentsilas/vite-plugin-ai-robots";
|
||||||
|
|
||||||
|
export default defineConfig({
|
||||||
|
plugins: [
|
||||||
|
aiRobots({
|
||||||
|
accessToken: process.env.DARK_VISITORS_TOKEN,
|
||||||
|
}),
|
||||||
|
],
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
## Configuration Options
|
||||||
|
|
||||||
|
| Option | Type | Default | Description |
|
||||||
|
| --------------- | ---------- | ---------------------------------------------- | ----------------------- |
|
||||||
|
| **accessToken** | `string` | _Required_ | Dark Visitors API token |
|
||||||
|
| `agentTypes` | `string[]` | `["AI Data Scraper", "Undocumented AI Agent"]` | Agent types to block |
|
||||||
|
| `disallow` | `string` | `"/"` | Paths to disallow |
|
||||||
|
| `cacheHours` | `number` | `24` | Cache duration in hours |
|
||||||
|
| `outputDir` | `string` | `"static"` | Output directory |
|
||||||
|
| `debug` | `boolean` | `false` | Enable debug logging |
|
||||||
|
|
||||||
|
## Troubleshooting
|
||||||
|
|
||||||
|
**Common Issues**:
|
||||||
|
|
||||||
|
- `401 Unauthorized`: Check your access token
|
||||||
|
- Empty robots.txt: Enable `debug: true`
|
||||||
|
- Cache not updating: Delete `.ai-robots-cache.json`
|
||||||
|
- You may want to add this file to your .gitignore
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,23 @@
|
||||||
|
{
|
||||||
|
"name": "@silentsilas/vite-plugin-ai-robots",
|
||||||
|
"version": "1.0.0",
|
||||||
|
"description": "Vite plugin to automatically update your robots.txt via Dark Visitor API to block the latest list of AI user agents",
|
||||||
|
"main": "dist/index.js",
|
||||||
|
"types": "dist/index.d.ts",
|
||||||
|
"scripts": {
|
||||||
|
"build": "tsup src/index.ts --dts",
|
||||||
|
"dev": "tsup src/index.ts --dts --watch",
|
||||||
|
"prepare": "npm run build"
|
||||||
|
},
|
||||||
|
"author": "silentsilas",
|
||||||
|
"license": "MIT",
|
||||||
|
"devDependencies": {
|
||||||
|
"@types/node": "^22.10.10",
|
||||||
|
"tsup": "^8.3.6",
|
||||||
|
"typescript": "^5.7.3",
|
||||||
|
"vite": "^5.4.4"
|
||||||
|
},
|
||||||
|
"peerDependencies": {
|
||||||
|
"vite": "^5.0.0 || ^6.0.0"
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,134 @@
|
||||||
|
import type { Plugin } from "vite";
|
||||||
|
import fs from "fs/promises";
|
||||||
|
import path from "path";
|
||||||
|
import https from "https";
|
||||||
|
|
||||||
|
interface PluginOptions {
|
||||||
|
accessToken: string;
|
||||||
|
agentTypes?: string[];
|
||||||
|
disallow?: string;
|
||||||
|
cacheHours?: number;
|
||||||
|
debug?: boolean;
|
||||||
|
outputDir?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface ApiResponse {
|
||||||
|
status: number;
|
||||||
|
text: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface CacheData {
|
||||||
|
timestamp: number;
|
||||||
|
content: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export const aiRobots = (options: PluginOptions): Plugin => {
|
||||||
|
const CACHE_FILENAME = ".ai-robots-cache.json";
|
||||||
|
const DEFAULT_DISALLOW = "/";
|
||||||
|
const DEFAULT_CACHE_HOURS = 24;
|
||||||
|
|
||||||
|
return {
|
||||||
|
name: "vite-plugin-ai-robots",
|
||||||
|
async configResolved(config) {
|
||||||
|
try {
|
||||||
|
const outputDir = options.outputDir || "static";
|
||||||
|
const outputPath = path.join(config.root, outputDir, "robots.txt");
|
||||||
|
|
||||||
|
// Read cache if available
|
||||||
|
let cachedContent: string | null = null;
|
||||||
|
try {
|
||||||
|
const cacheFile = await fs.readFile(CACHE_FILENAME, "utf-8");
|
||||||
|
const cacheData: CacheData = JSON.parse(cacheFile);
|
||||||
|
|
||||||
|
if (
|
||||||
|
Date.now() - cacheData.timestamp <
|
||||||
|
(options.cacheHours || DEFAULT_CACHE_HOURS) * 3600 * 1000
|
||||||
|
) {
|
||||||
|
cachedContent = cacheData.content;
|
||||||
|
}
|
||||||
|
} catch {}
|
||||||
|
|
||||||
|
if (!cachedContent) {
|
||||||
|
const response = await fetchRobotsTxt({
|
||||||
|
accessToken: options.accessToken,
|
||||||
|
agentTypes: options.agentTypes || [
|
||||||
|
"AI Data Scraper",
|
||||||
|
"Undocumented AI Agent",
|
||||||
|
],
|
||||||
|
disallow: options.disallow || DEFAULT_DISALLOW,
|
||||||
|
});
|
||||||
|
|
||||||
|
if (response.status !== 200) {
|
||||||
|
throw new Error(
|
||||||
|
`API request failed: ${response.status} - ${response.text}`
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
cachedContent = response.text;
|
||||||
|
|
||||||
|
// Update cache
|
||||||
|
await fs.writeFile(
|
||||||
|
CACHE_FILENAME,
|
||||||
|
JSON.stringify({
|
||||||
|
timestamp: Date.now(),
|
||||||
|
content: cachedContent,
|
||||||
|
})
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ensure output directory exists
|
||||||
|
await fs.mkdir(path.dirname(outputPath), { recursive: true });
|
||||||
|
|
||||||
|
// Write robots.txt
|
||||||
|
await fs.writeFile(outputPath, cachedContent);
|
||||||
|
|
||||||
|
if (options.debug) {
|
||||||
|
console.log(`Generated robots.txt at: ${outputPath}`);
|
||||||
|
console.log(`Content:\n${cachedContent}`);
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error("AI Robots.txt plugin error:");
|
||||||
|
console.error(error);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
async function fetchRobotsTxt(params: {
|
||||||
|
accessToken: string;
|
||||||
|
agentTypes: string[];
|
||||||
|
disallow: string;
|
||||||
|
}): Promise<ApiResponse> {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
const postData = JSON.stringify({
|
||||||
|
agent_types: params.agentTypes,
|
||||||
|
disallow: params.disallow,
|
||||||
|
});
|
||||||
|
|
||||||
|
const req = https.request(
|
||||||
|
"https://api.darkvisitors.com/robots-txts",
|
||||||
|
{
|
||||||
|
method: "POST",
|
||||||
|
headers: {
|
||||||
|
Authorization: `Bearer ${params.accessToken}`,
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
"Content-Length": postData.length,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
(res) => {
|
||||||
|
let data = "";
|
||||||
|
res.on("data", (chunk) => (data += chunk));
|
||||||
|
res.on("end", () => {
|
||||||
|
resolve({
|
||||||
|
status: res.statusCode || 500,
|
||||||
|
text: data,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
req.on("error", reject);
|
||||||
|
req.write(postData);
|
||||||
|
req.end();
|
||||||
|
});
|
||||||
|
}
|
|
@ -0,0 +1,13 @@
|
||||||
|
{
|
||||||
|
"compilerOptions": {
|
||||||
|
"module": "ESNext",
|
||||||
|
"target": "ESNext",
|
||||||
|
"lib": ["ESNext"],
|
||||||
|
"esModuleInterop": true,
|
||||||
|
"strict": true,
|
||||||
|
"strictNullChecks": true,
|
||||||
|
"moduleResolution": "Node",
|
||||||
|
"resolveJsonModule": true
|
||||||
|
},
|
||||||
|
"exclude": ["**/dist", "**/node_modules", "**/test"]
|
||||||
|
}
|
Loading…
Reference in New Issue