@riavzon/bot-detector-create 1.0.9 → 1.0.11
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 +24 -13
- package/dist/create.js +17 -0
- package/package.json +1 -1
- package/src/create.ts +3 -1
- package/src/default.ts +16 -0
package/README.md
CHANGED
|
@@ -12,16 +12,17 @@ Run this in the root of your Express project:
|
|
|
12
12
|
npx @riavzon/bot-detector-create
|
|
13
13
|
```
|
|
14
14
|
|
|
15
|
-
The command does the following
|
|
15
|
+
The command does the following:
|
|
16
16
|
|
|
17
17
|
1. Installs `@riavzon/bot-detector`, `express`, `cookie-parser`, and
|
|
18
18
|
`better-sqlite3` into your project.
|
|
19
|
-
2. Triggers the data source installer
|
|
20
|
-
intelligence feeds
|
|
21
|
-
fast local MMDB/LMDB files.
|
|
19
|
+
2. Triggers the data source installer and downloads and compiles all threat
|
|
20
|
+
intelligence feeds.
|
|
22
21
|
3. Writes a `botDetectorConfig.ts` file at your project root with all 17
|
|
23
22
|
checkers pre-configured at their default values.
|
|
24
|
-
4.
|
|
23
|
+
4. Writes a `mainBotDetector.ts` file at your project root, a ready-to-run
|
|
24
|
+
Express entry point that imports the config and mounts the middleware.
|
|
25
|
+
5. Runs `load-schema` to create the `visitors` and `banned` tables in a local
|
|
25
26
|
`bot_detector.sqlite` file.
|
|
26
27
|
|
|
27
28
|
## What you get
|
|
@@ -43,8 +44,7 @@ await defineConfiguration({
|
|
|
43
44
|
});
|
|
44
45
|
```
|
|
45
46
|
|
|
46
|
-
The defaults use SQLite and in-process memory cache
|
|
47
|
-
required. When you're ready for production, swap the adapters:
|
|
47
|
+
The defaults use SQLite and in-process memory cache, When you're ready for production, swap the adapters:
|
|
48
48
|
|
|
49
49
|
```ts
|
|
50
50
|
// MySQL
|
|
@@ -57,28 +57,39 @@ storage: { driver: 'redis', url: process.env.REDIS_URL }
|
|
|
57
57
|
storage: { driver: 'upstash', url: process.env.UPSTASH_URL, token: process.env.UPSTASH_TOKEN }
|
|
58
58
|
```
|
|
59
59
|
|
|
60
|
-
##
|
|
60
|
+
## What you get
|
|
61
61
|
|
|
62
|
-
|
|
63
|
-
|
|
62
|
+
`mainBotDetector.ts` is a ready-to-run Express entry point. It imports the
|
|
63
|
+
config, mounts the middleware, and starts the server:
|
|
64
64
|
|
|
65
65
|
```ts
|
|
66
66
|
import './botDetectorConfig.js';
|
|
67
|
-
import { detectBots } from '@riavzon/bot-detector';
|
|
68
|
-
import cookieParser from 'cookie-parser';
|
|
69
67
|
import express from 'express';
|
|
68
|
+
import cookieParser from 'cookie-parser';
|
|
69
|
+
import { detectBots } from '@riavzon/bot-detector';
|
|
70
70
|
|
|
71
71
|
const app = express();
|
|
72
72
|
app.use(cookieParser());
|
|
73
73
|
app.use(detectBots());
|
|
74
74
|
|
|
75
75
|
app.get('/', (req, res) => {
|
|
76
|
-
res.
|
|
76
|
+
res.json({ banned: req.botDetection?.banned });
|
|
77
77
|
});
|
|
78
78
|
|
|
79
79
|
app.listen(3000);
|
|
80
80
|
```
|
|
81
81
|
|
|
82
|
+
## Mount the middleware
|
|
83
|
+
|
|
84
|
+
`botDetectorConfig.ts` must be imported before any routes. If you prefer to
|
|
85
|
+
wire it into your own entry point instead of using `mainBotDetector.ts`:
|
|
86
|
+
|
|
87
|
+
```ts
|
|
88
|
+
import './botDetectorConfig.js';
|
|
89
|
+
import { detectBots } from '@riavzon/bot-detector';
|
|
90
|
+
// ... rest of your app
|
|
91
|
+
```
|
|
92
|
+
|
|
82
93
|
## Keep data sources fresh
|
|
83
94
|
|
|
84
95
|
Threat intelligence feeds update continuously. Run a refresh at least once
|
package/dist/create.js
CHANGED
|
@@ -9,6 +9,21 @@ const defaultStore = { main: {
|
|
|
9
9
|
driver: "sqlite",
|
|
10
10
|
name: "./bot_detector.sqlite"
|
|
11
11
|
} };
|
|
12
|
+
const mainContent = `import './botDetectorConfig.js';
|
|
13
|
+
import express from 'express';
|
|
14
|
+
import cookieParser from 'cookie-parser';
|
|
15
|
+
import { detectBots } from '@riavzon/bot-detector';
|
|
16
|
+
|
|
17
|
+
const app = express();
|
|
18
|
+
app.use(cookieParser());
|
|
19
|
+
app.use(detectBots());
|
|
20
|
+
|
|
21
|
+
app.get('/', (req, res) => {
|
|
22
|
+
res.json({ banned: req.botDetection?.banned });
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
app.listen(3000);
|
|
26
|
+
`;
|
|
12
27
|
const content = `import { defineConfiguration } from '@riavzon/bot-detector';
|
|
13
28
|
|
|
14
29
|
/**
|
|
@@ -300,6 +315,7 @@ const start = defineCommand({
|
|
|
300
315
|
await run("npx", ["@riavzon/bot-detector", "init"]);
|
|
301
316
|
consola.start("Writing botDetectorConfig.ts...");
|
|
302
317
|
await fs.writeFile(output, content, "utf-8");
|
|
318
|
+
await fs.writeFile(path.resolve(process.cwd(), "mainBotDetector.ts"), mainContent, "utf-8");
|
|
303
319
|
consola.success("botDetectorConfig.ts created");
|
|
304
320
|
consola.start("Creating database tables...");
|
|
305
321
|
const { defineConfiguration, createTables, getDb } = await import(path.resolve(process.cwd(), "node_modules/@riavzon/bot-detector/dist/main.mjs"));
|
|
@@ -309,6 +325,7 @@ const start = defineCommand({
|
|
|
309
325
|
consola.log("");
|
|
310
326
|
consola.log("Keep data sources fresh (run daily or via cron):");
|
|
311
327
|
consola.log(" npx @riavzon/bot-detector refresh");
|
|
328
|
+
process.exit(0);
|
|
312
329
|
}
|
|
313
330
|
});
|
|
314
331
|
await runMain(start);
|
package/package.json
CHANGED
package/src/create.ts
CHANGED
|
@@ -6,7 +6,7 @@ import { defineCommand, runMain } from 'citty';
|
|
|
6
6
|
import fs from 'node:fs/promises';
|
|
7
7
|
import path from 'node:path';
|
|
8
8
|
import { spawn } from 'node:child_process';
|
|
9
|
-
import { content, defaultStore } from './default.js';
|
|
9
|
+
import { content, mainContent, defaultStore } from './default.js';
|
|
10
10
|
|
|
11
11
|
function run(cmd: string, args: string[]): Promise<void> {
|
|
12
12
|
return new Promise((resolve, reject) => {
|
|
@@ -37,6 +37,7 @@ export const start = defineCommand({
|
|
|
37
37
|
|
|
38
38
|
consola.start('Writing botDetectorConfig.ts...');
|
|
39
39
|
await fs.writeFile(output, content, 'utf-8');
|
|
40
|
+
await fs.writeFile(path.resolve(process.cwd(), 'mainBotDetector.ts'), mainContent, 'utf-8');
|
|
40
41
|
consola.success('botDetectorConfig.ts created');
|
|
41
42
|
|
|
42
43
|
consola.start('Creating database tables...');
|
|
@@ -52,6 +53,7 @@ export const start = defineCommand({
|
|
|
52
53
|
consola.log('');
|
|
53
54
|
consola.log('Keep data sources fresh (run daily or via cron):');
|
|
54
55
|
consola.log(' npx @riavzon/bot-detector refresh');
|
|
56
|
+
process.exit(0);
|
|
55
57
|
}
|
|
56
58
|
});
|
|
57
59
|
await runMain(start);
|
package/src/default.ts
CHANGED
|
@@ -1,5 +1,21 @@
|
|
|
1
1
|
export const defaultStore = { main: { driver: 'sqlite' as const, name: './bot_detector.sqlite' } };
|
|
2
2
|
|
|
3
|
+
export const mainContent = `import './botDetectorConfig.js';
|
|
4
|
+
import express from 'express';
|
|
5
|
+
import cookieParser from 'cookie-parser';
|
|
6
|
+
import { detectBots } from '@riavzon/bot-detector';
|
|
7
|
+
|
|
8
|
+
const app = express();
|
|
9
|
+
app.use(cookieParser());
|
|
10
|
+
app.use(detectBots());
|
|
11
|
+
|
|
12
|
+
app.get('/', (req, res) => {
|
|
13
|
+
res.json({ banned: req.botDetection?.banned });
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
app.listen(3000);
|
|
17
|
+
`;
|
|
18
|
+
|
|
3
19
|
export const content = `import { defineConfiguration } from '@riavzon/bot-detector';
|
|
4
20
|
|
|
5
21
|
/**
|