@webdecoy/node 0.1.1 → 0.2.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.d.mts +308 -5
- package/dist/index.d.ts +308 -5
- package/dist/index.js +956 -19
- package/dist/index.mjs +950 -18
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -1,7 +1,148 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
2
|
+
* Rules Engine Types
|
|
3
|
+
* Core interfaces for the WebDecoy rules engine
|
|
4
4
|
*/
|
|
5
|
+
/**
|
|
6
|
+
* Context available to rules during evaluation
|
|
7
|
+
*/
|
|
8
|
+
interface RuleContext {
|
|
9
|
+
/** Client IP address */
|
|
10
|
+
ip: string;
|
|
11
|
+
/** Request path */
|
|
12
|
+
path: string;
|
|
13
|
+
/** HTTP method */
|
|
14
|
+
method: string;
|
|
15
|
+
/** User-Agent string */
|
|
16
|
+
userAgent?: string;
|
|
17
|
+
/** Request headers (lowercase keys) */
|
|
18
|
+
headers: Record<string, string>;
|
|
19
|
+
/** Request timestamp */
|
|
20
|
+
timestamp: number;
|
|
21
|
+
/** IP enrichment data (populated async when available) */
|
|
22
|
+
enrichment?: IPEnrichmentData;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Result of evaluating a single rule
|
|
26
|
+
*/
|
|
27
|
+
interface RuleResult {
|
|
28
|
+
/** Action to take */
|
|
29
|
+
action: 'ALLOW' | 'DENY' | 'THROTTLE';
|
|
30
|
+
/** Name of the rule that produced this result */
|
|
31
|
+
rule: string;
|
|
32
|
+
/** Human-readable reason */
|
|
33
|
+
reason?: string;
|
|
34
|
+
/** Additional metadata */
|
|
35
|
+
metadata?: Record<string, any>;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* A rule that can evaluate request context and produce a decision
|
|
39
|
+
*/
|
|
40
|
+
interface Rule {
|
|
41
|
+
/** Unique name for this rule */
|
|
42
|
+
name: string;
|
|
43
|
+
/** Evaluate the rule against request context */
|
|
44
|
+
evaluate(context: RuleContext): RuleResult;
|
|
45
|
+
/** Clean up resources (timers, etc.) */
|
|
46
|
+
destroy?(): void;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Configuration for rate limiting
|
|
50
|
+
*/
|
|
51
|
+
interface RateLimitConfig {
|
|
52
|
+
/** Maximum requests allowed in the window */
|
|
53
|
+
max: number;
|
|
54
|
+
/** Window size in seconds */
|
|
55
|
+
window: number;
|
|
56
|
+
/** Algorithm: 'fixed' (default) or 'sliding' */
|
|
57
|
+
algorithm?: 'fixed' | 'sliding';
|
|
58
|
+
/** Custom key derivation function. Default: by IP */
|
|
59
|
+
keyBy?: (context: RuleContext) => string;
|
|
60
|
+
/** Action when limit is exceeded: 'DENY' or 'THROTTLE' (default: 'THROTTLE') */
|
|
61
|
+
action?: 'DENY' | 'THROTTLE';
|
|
62
|
+
/** Dry run mode: log violations but don't block */
|
|
63
|
+
dryRun?: boolean;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Configuration for filter rules
|
|
67
|
+
*/
|
|
68
|
+
interface FilterConfig {
|
|
69
|
+
/** Filter expression (e.g., 'ip.vpn or ip.tor') */
|
|
70
|
+
expression: string;
|
|
71
|
+
/** Action when filter matches: 'DENY' (default) or 'THROTTLE' */
|
|
72
|
+
action?: 'DENY' | 'THROTTLE';
|
|
73
|
+
/** Dry run mode: log violations but don't block */
|
|
74
|
+
dryRun?: boolean;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* A violation event recorded when a rule triggers
|
|
78
|
+
*/
|
|
79
|
+
interface ViolationEvent {
|
|
80
|
+
/** Rule that triggered */
|
|
81
|
+
rule: string;
|
|
82
|
+
/** Action taken */
|
|
83
|
+
action: 'DENY' | 'THROTTLE' | 'ALLOW';
|
|
84
|
+
/** Client IP */
|
|
85
|
+
ip: string;
|
|
86
|
+
/** Request path */
|
|
87
|
+
path?: string;
|
|
88
|
+
/** HTTP method */
|
|
89
|
+
method?: string;
|
|
90
|
+
/** User-Agent */
|
|
91
|
+
userAgent?: string;
|
|
92
|
+
/** Reason for the violation */
|
|
93
|
+
reason?: string;
|
|
94
|
+
/** Additional metadata */
|
|
95
|
+
metadata?: Record<string, any>;
|
|
96
|
+
/** Whether this was a dry run */
|
|
97
|
+
dryRun: boolean;
|
|
98
|
+
/** When the violation occurred (ISO 8601) */
|
|
99
|
+
timestamp: string;
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* IP enrichment data from the WebDecoy API
|
|
103
|
+
*/
|
|
104
|
+
interface IPEnrichmentData {
|
|
105
|
+
ip: string;
|
|
106
|
+
security: {
|
|
107
|
+
vpn: boolean;
|
|
108
|
+
proxy: boolean;
|
|
109
|
+
tor: boolean;
|
|
110
|
+
relay: boolean;
|
|
111
|
+
hosting: boolean;
|
|
112
|
+
};
|
|
113
|
+
location: {
|
|
114
|
+
country: string;
|
|
115
|
+
country_name: string;
|
|
116
|
+
city: string;
|
|
117
|
+
timezone: string;
|
|
118
|
+
};
|
|
119
|
+
network: {
|
|
120
|
+
asn: number;
|
|
121
|
+
asn_org: string;
|
|
122
|
+
};
|
|
123
|
+
reputation: {
|
|
124
|
+
abuse_score: number;
|
|
125
|
+
total_reports: number;
|
|
126
|
+
is_high_risk: boolean;
|
|
127
|
+
};
|
|
128
|
+
categories: string[];
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Combined result from evaluating all rules
|
|
132
|
+
*/
|
|
133
|
+
interface RuleEngineResult {
|
|
134
|
+
/** Final action (first DENY/THROTTLE wins, or ALLOW) */
|
|
135
|
+
action: 'ALLOW' | 'DENY' | 'THROTTLE';
|
|
136
|
+
/** The rule that caused the action (null if all allowed) */
|
|
137
|
+
rule?: string;
|
|
138
|
+
/** Reason from the deciding rule */
|
|
139
|
+
reason?: string;
|
|
140
|
+
/** Metadata from the deciding rule */
|
|
141
|
+
metadata?: Record<string, any>;
|
|
142
|
+
/** All violations generated during evaluation */
|
|
143
|
+
violations: ViolationEvent[];
|
|
144
|
+
}
|
|
145
|
+
|
|
5
146
|
/**
|
|
6
147
|
* Configuration options for the Web Decoy SDK
|
|
7
148
|
*/
|
|
@@ -9,8 +150,9 @@ interface WebDecoyConfig {
|
|
|
9
150
|
/**
|
|
10
151
|
* API key for authentication (sk_live_xxxxx format)
|
|
11
152
|
* Get this from your Web Decoy dashboard
|
|
153
|
+
* Optional — when omitted, SDK runs in local-only mode (rules still evaluate)
|
|
12
154
|
*/
|
|
13
|
-
apiKey
|
|
155
|
+
apiKey?: string;
|
|
14
156
|
/**
|
|
15
157
|
* API URL for the Web Decoy ingest service
|
|
16
158
|
* @default 'https://api.webdecoy.com'
|
|
@@ -43,6 +185,11 @@ interface WebDecoyConfig {
|
|
|
43
185
|
* @default true
|
|
44
186
|
*/
|
|
45
187
|
tlsRejectUnauthorized?: boolean;
|
|
188
|
+
/**
|
|
189
|
+
* Rules to evaluate before calling the detection API
|
|
190
|
+
* Rules are evaluated in order; first DENY/THROTTLE wins
|
|
191
|
+
*/
|
|
192
|
+
rules?: Rule[];
|
|
46
193
|
}
|
|
47
194
|
/**
|
|
48
195
|
* TLS connection information from the request
|
|
@@ -137,6 +284,8 @@ interface ProtectResult {
|
|
|
137
284
|
detection: SDKDetectionResponse;
|
|
138
285
|
/** Error message if the request failed */
|
|
139
286
|
error?: string;
|
|
287
|
+
/** Rule engine result (if rules were evaluated) */
|
|
288
|
+
ruleResult?: RuleEngineResult;
|
|
140
289
|
}
|
|
141
290
|
/**
|
|
142
291
|
* Options for the protect() method
|
|
@@ -150,6 +299,40 @@ interface ProtectOptions {
|
|
|
150
299
|
metadata?: Record<string, any>;
|
|
151
300
|
}
|
|
152
301
|
|
|
302
|
+
/**
|
|
303
|
+
* Web Decoy API Client
|
|
304
|
+
* Handles communication with the ingest service
|
|
305
|
+
*/
|
|
306
|
+
|
|
307
|
+
interface ClientConfig {
|
|
308
|
+
apiKey: string;
|
|
309
|
+
apiUrl: string;
|
|
310
|
+
timeout: number;
|
|
311
|
+
debug: boolean;
|
|
312
|
+
tlsRejectUnauthorized: boolean;
|
|
313
|
+
}
|
|
314
|
+
declare class WebDecoyClient {
|
|
315
|
+
private axios;
|
|
316
|
+
private config;
|
|
317
|
+
constructor(config: ClientConfig);
|
|
318
|
+
/**
|
|
319
|
+
* Send a detection request to the ingest service
|
|
320
|
+
*/
|
|
321
|
+
detect(request: SDKDetectionRequest): Promise<SDKDetectionResponse>;
|
|
322
|
+
/**
|
|
323
|
+
* Send violation events to the ingest service
|
|
324
|
+
*/
|
|
325
|
+
sendViolations(events: ViolationEvent[]): Promise<void>;
|
|
326
|
+
/**
|
|
327
|
+
* Get IP enrichment data from the ingest service
|
|
328
|
+
*/
|
|
329
|
+
getIPEnrichment(ip: string): Promise<IPEnrichmentData | null>;
|
|
330
|
+
/**
|
|
331
|
+
* Validate the API key by making a test request
|
|
332
|
+
*/
|
|
333
|
+
validateAPIKey(): Promise<boolean>;
|
|
334
|
+
}
|
|
335
|
+
|
|
153
336
|
/**
|
|
154
337
|
* Web Decoy SDK
|
|
155
338
|
* Main SDK class for bot detection and protection
|
|
@@ -158,7 +341,25 @@ interface ProtectOptions {
|
|
|
158
341
|
declare class WebDecoy {
|
|
159
342
|
private client;
|
|
160
343
|
private config;
|
|
344
|
+
private ruleEngine;
|
|
345
|
+
private violationReporter;
|
|
346
|
+
private ipEnrichmentClient;
|
|
347
|
+
private _hasFilterRules;
|
|
161
348
|
constructor(config: WebDecoyConfig);
|
|
349
|
+
/**
|
|
350
|
+
* Evaluate rules against request metadata (synchronous).
|
|
351
|
+
* Returns null if no rules are configured.
|
|
352
|
+
*/
|
|
353
|
+
evaluateRules(metadata: RequestMetadata): RuleEngineResult | null;
|
|
354
|
+
/**
|
|
355
|
+
* Evaluate rules with async IP enrichment pre-fetch.
|
|
356
|
+
* Use this instead of evaluateRules() when filter rules are present.
|
|
357
|
+
*/
|
|
358
|
+
evaluateRulesAsync(metadata: RequestMetadata): Promise<RuleEngineResult | null>;
|
|
359
|
+
/**
|
|
360
|
+
* Whether this SDK instance has filter rules that need async enrichment
|
|
361
|
+
*/
|
|
362
|
+
get hasFilterRules(): boolean;
|
|
162
363
|
/**
|
|
163
364
|
* Protect a request by checking it against Web Decoy's bot detection
|
|
164
365
|
*
|
|
@@ -175,10 +376,112 @@ declare class WebDecoy {
|
|
|
175
376
|
valid: boolean;
|
|
176
377
|
error?: string;
|
|
177
378
|
}>;
|
|
379
|
+
/**
|
|
380
|
+
* Attach a violation reporter for sending violations to the backend.
|
|
381
|
+
* Called internally when apiKey is present and rules are configured.
|
|
382
|
+
*/
|
|
383
|
+
setViolationReporter(reporter: {
|
|
384
|
+
report(violations: ViolationEvent[]): void;
|
|
385
|
+
flush(): Promise<void>;
|
|
386
|
+
destroy(): Promise<void>;
|
|
387
|
+
}): void;
|
|
388
|
+
/**
|
|
389
|
+
* Get the API client (for use by violation reporter, enrichment, etc.)
|
|
390
|
+
*/
|
|
391
|
+
getClient(): WebDecoyClient | null;
|
|
178
392
|
/**
|
|
179
393
|
* Get the current configuration
|
|
180
394
|
*/
|
|
181
|
-
getConfig(): Readonly<
|
|
395
|
+
getConfig(): Readonly<typeof this.config>;
|
|
396
|
+
/**
|
|
397
|
+
* Clean up resources (timers, flush pending violations)
|
|
398
|
+
*/
|
|
399
|
+
destroy(): Promise<void>;
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
/**
|
|
403
|
+
* Rule Engine
|
|
404
|
+
* Evaluates rules in order — first DENY/THROTTLE wins
|
|
405
|
+
*/
|
|
406
|
+
|
|
407
|
+
declare class RuleEngine {
|
|
408
|
+
private rules;
|
|
409
|
+
constructor(rules: Rule[]);
|
|
410
|
+
/**
|
|
411
|
+
* Evaluate all rules against the request context.
|
|
412
|
+
* First DENY or THROTTLE result wins. Violations are recorded for all non-ALLOW results.
|
|
413
|
+
*/
|
|
414
|
+
evaluate(context: RuleContext): RuleEngineResult;
|
|
415
|
+
/**
|
|
416
|
+
* Clean up all rule resources
|
|
417
|
+
*/
|
|
418
|
+
destroy(): void;
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
/**
|
|
422
|
+
* Rate Limit Rule
|
|
423
|
+
* Implements the Rule interface using InMemoryRateLimiter
|
|
424
|
+
*/
|
|
425
|
+
|
|
426
|
+
declare class RateLimitRule implements Rule {
|
|
427
|
+
readonly name: string;
|
|
428
|
+
private limiter;
|
|
429
|
+
private config;
|
|
430
|
+
constructor(config: RateLimitConfig);
|
|
431
|
+
evaluate(context: RuleContext): RuleResult;
|
|
432
|
+
destroy(): void;
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
/**
|
|
436
|
+
* Filter Rule
|
|
437
|
+
* Implements the Rule interface using the filter expression language
|
|
438
|
+
*/
|
|
439
|
+
|
|
440
|
+
declare class FilterRule implements Rule {
|
|
441
|
+
readonly name: string;
|
|
442
|
+
private ast;
|
|
443
|
+
private action;
|
|
444
|
+
private dryRun;
|
|
445
|
+
private expression;
|
|
446
|
+
constructor(config: FilterConfig);
|
|
447
|
+
evaluate(context: RuleContext): RuleResult;
|
|
182
448
|
}
|
|
183
449
|
|
|
184
|
-
|
|
450
|
+
/**
|
|
451
|
+
* Rules Engine - Barrel exports
|
|
452
|
+
*/
|
|
453
|
+
|
|
454
|
+
/**
|
|
455
|
+
* Factory function to create a rate limit rule
|
|
456
|
+
*
|
|
457
|
+
* @example
|
|
458
|
+
* ```typescript
|
|
459
|
+
* import { rateLimit } from '@webdecoy/node';
|
|
460
|
+
*
|
|
461
|
+
* const sdk = new WebDecoy({
|
|
462
|
+
* rules: [
|
|
463
|
+
* rateLimit({ max: 100, window: 60 }),
|
|
464
|
+
* ],
|
|
465
|
+
* });
|
|
466
|
+
* ```
|
|
467
|
+
*/
|
|
468
|
+
declare function rateLimit(config: RateLimitConfig): Rule;
|
|
469
|
+
/**
|
|
470
|
+
* Factory function to create a filter rule
|
|
471
|
+
*
|
|
472
|
+
* @example
|
|
473
|
+
* ```typescript
|
|
474
|
+
* import { filter } from '@webdecoy/node';
|
|
475
|
+
*
|
|
476
|
+
* const sdk = new WebDecoy({
|
|
477
|
+
* apiKey: process.env.WEBDECOY_API_KEY,
|
|
478
|
+
* rules: [
|
|
479
|
+
* filter({ expression: 'ip.vpn or ip.tor', action: 'DENY' }),
|
|
480
|
+
* filter({ expression: 'ip.country in ["CN", "RU"]', action: 'DENY' }),
|
|
481
|
+
* ],
|
|
482
|
+
* });
|
|
483
|
+
* ```
|
|
484
|
+
*/
|
|
485
|
+
declare function filter(config: FilterConfig): Rule;
|
|
486
|
+
|
|
487
|
+
export { type FilterConfig, FilterRule, type IPEnrichmentData, type LocalAnalysis, type ProtectOptions, type ProtectResult, type RateLimitConfig, RateLimitRule, type RequestMetadata, type Rule, type RuleContext, RuleEngine, type RuleEngineResult, type RuleResult, type SDKDetectionRequest, type SDKDetectionResponse, type TLSInfo, type ViolationEvent, WebDecoy, type WebDecoyConfig, filter, rateLimit };
|