@riavzon/bot-detector 1.0.5 → 1.0.8

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/main.cjs CHANGED
@@ -3120,6 +3120,83 @@ async function runGeneration() {
3120
3120
  return runCycle();
3121
3121
  }
3122
3122
 
3123
+ //#endregion
3124
+ //#region src/botDetector/db/schema.ts
3125
+ function visitorIdDefault(db) {
3126
+ if (isMySQL(db)) return "NOT NULL DEFAULT (UUID())";
3127
+ if (db.dialect === "postgresql") return "NOT NULL DEFAULT gen_random_uuid()";
3128
+ return "NOT NULL";
3129
+ }
3130
+ function lastSeenDef(db) {
3131
+ if (isMySQL(db)) return "TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP";
3132
+ if (isSQLite(db)) return "TEXT DEFAULT CURRENT_TIMESTAMP";
3133
+ return "TIMESTAMP DEFAULT CURRENT_TIMESTAMP";
3134
+ }
3135
+ function tableOptions(db) {
3136
+ return isMySQL(db) ? "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci" : "";
3137
+ }
3138
+ function timestampType(db) {
3139
+ return isSQLite(db) ? "TEXT" : "TIMESTAMP";
3140
+ }
3141
+ async function createTables(db) {
3142
+ const visitorId = visitorIdDefault(db);
3143
+ const lastSeen = lastSeenDef(db);
3144
+ const tblOpts = tableOptions(db);
3145
+ const createVisitorsTable = `
3146
+ CREATE TABLE IF NOT EXISTS visitors (
3147
+ visitor_id CHAR(36) ${visitorId},
3148
+ canary_id VARCHAR(64) PRIMARY KEY,
3149
+ ip_address VARCHAR(45),
3150
+ user_agent TEXT,
3151
+ country VARCHAR(64),
3152
+ region VARCHAR(64),
3153
+ region_name VARCHAR(350),
3154
+ city VARCHAR(64),
3155
+ district VARCHAR(260),
3156
+ lat VARCHAR(150),
3157
+ lon VARCHAR(150),
3158
+ timezone VARCHAR(64),
3159
+ currency VARCHAR(64),
3160
+ isp VARCHAR(64),
3161
+ org VARCHAR(64),
3162
+ as_org VARCHAR(64),
3163
+ device_type VARCHAR(64),
3164
+ browser VARCHAR(64),
3165
+ proxy BOOLEAN,
3166
+ hosting BOOLEAN,
3167
+ is_bot BOOLEAN DEFAULT false,
3168
+ first_seen ${timestampType(db)} DEFAULT CURRENT_TIMESTAMP,
3169
+ last_seen ${lastSeen},
3170
+ request_count INT DEFAULT 1,
3171
+ deviceVendor VARCHAR(64) DEFAULT 'unknown',
3172
+ deviceModel VARCHAR(64) DEFAULT 'unknown',
3173
+ browserType VARCHAR(64) DEFAULT 'unknown',
3174
+ browserVersion VARCHAR(64) DEFAULT 'unknown',
3175
+ os VARCHAR(64) DEFAULT 'unknown',
3176
+ suspicious_activity_score INT DEFAULT 0
3177
+ ) ${tblOpts}
3178
+ `;
3179
+ const createBannedTable = `
3180
+ CREATE TABLE IF NOT EXISTS banned (
3181
+ canary_id VARCHAR(64) PRIMARY KEY,
3182
+ ip_address VARCHAR(45),
3183
+ country VARCHAR(64),
3184
+ user_agent TEXT,
3185
+ reason TEXT,
3186
+ score INT DEFAULT NULL,
3187
+ FOREIGN KEY (canary_id) REFERENCES visitors(canary_id)
3188
+ )
3189
+ `;
3190
+ try {
3191
+ await db.exec(createVisitorsTable);
3192
+ await db.exec(createBannedTable);
3193
+ consola.default.success("Tables created successfully.");
3194
+ } catch (error) {
3195
+ consola.default.error("Error creating tables:", error);
3196
+ throw error;
3197
+ }
3198
+ }
3199
+
3123
3200
  //#endregion
3124
3201
  //#region src/botDetector/db/warmUp.ts
3125
3202
  async function warmUp() {
@@ -3221,10 +3298,12 @@ exports.BadBotDetected = BadBotDetected;
3221
3298
  exports.CheckerRegistry = CheckerRegistry;
3222
3299
  exports.GoodBotDetected = GoodBotDetected;
3223
3300
  exports.banIp = banIp;
3301
+ exports.createTables = createTables;
3224
3302
  exports.defineConfiguration = configuration;
3225
3303
  exports.detectBots = validator;
3226
3304
  exports.getBatchQueue = getBatchQueue;
3227
3305
  exports.getDataSources = getDataSources;
3306
+ exports.getDb = getDb;
3228
3307
  exports.getGeoData = getData;
3229
3308
  exports.getStorage = getStorage;
3230
3309
  exports.parseUA = parseUA;