@shieldpress/tracker 1.0.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.
@@ -0,0 +1,379 @@
1
+ /** Configuration options for ShieldPress Tracker */
2
+ interface TrackerConfig {
3
+ /** API key from ShieldPress dashboard */
4
+ apiKey: string;
5
+ /** Site ID registered in ShieldPress */
6
+ siteId: string;
7
+ /** ShieldPress API base URL */
8
+ apiUrl?: string;
9
+ /** Reporting interval in seconds (default: 60) */
10
+ reportInterval?: number;
11
+ /** Enable system metrics collection — CPU, RAM, disk (default: true) */
12
+ systemMetrics?: boolean;
13
+ /** Enable HTTP request tracking (default: true) */
14
+ httpTracking?: boolean;
15
+ /** Enable error tracking (default: true) */
16
+ errorTracking?: boolean;
17
+ /** Enable security monitoring (default: true) */
18
+ securityTracking?: boolean;
19
+ /** Enable dependency vulnerability audit (default: true) */
20
+ depAudit?: boolean;
21
+ /** Dependency audit interval in seconds (default: 3600 = 1 hour) */
22
+ depAuditInterval?: number;
23
+ /** Enable runtime performance tracking — event loop lag, GC (default: true) */
24
+ runtimeMetrics?: boolean;
25
+ /** Enable environment security scan (default: true) */
26
+ envSecurity?: boolean;
27
+ /** Enable uptime heartbeat (default: true) */
28
+ heartbeat?: boolean;
29
+ /** Custom app name for identification */
30
+ appName?: string;
31
+ /** App version string */
32
+ appVersion?: string;
33
+ /** Environment label (e.g. production, staging) */
34
+ environment?: string;
35
+ /** Custom tags for filtering */
36
+ tags?: Record<string, string>;
37
+ /** Paths to ignore from HTTP tracking (glob patterns) */
38
+ ignorePaths?: string[];
39
+ /** Max error events buffered before flush (default: 100) */
40
+ maxErrorBuffer?: number;
41
+ /** Max security events buffered before flush (default: 500) */
42
+ maxSecurityBuffer?: number;
43
+ /** Debug mode — logs to console (default: false) */
44
+ debug?: boolean;
45
+ }
46
+ /** Resolved config with defaults applied */
47
+ interface ResolvedConfig extends Required<Omit<TrackerConfig, 'tags'>> {
48
+ tags: Record<string, string>;
49
+ }
50
+ /** System-level metrics snapshot */
51
+ interface SystemMetrics {
52
+ cpuPercent: number;
53
+ cpuCount: number;
54
+ memUsedMb: number;
55
+ memTotalMb: number;
56
+ memPercent: number;
57
+ heapUsedMb: number;
58
+ heapTotalMb: number;
59
+ externalMb: number;
60
+ rss: number;
61
+ uptimeSeconds: number;
62
+ loadAvg: [number, number, number];
63
+ platform: string;
64
+ nodeVersion: string;
65
+ pid: number;
66
+ osName: string;
67
+ osVersion: string;
68
+ osKernel: string;
69
+ osArch: string;
70
+ osRelease: string;
71
+ diskUsedGb: number;
72
+ diskTotalGb: number;
73
+ diskPercent: number;
74
+ publicIp: string;
75
+ privateIp: string;
76
+ hostname: string;
77
+ ppid: number;
78
+ uid: number | null;
79
+ cwd: string;
80
+ execPath: string;
81
+ nodeArgs: string;
82
+ }
83
+ /** Aggregated HTTP metrics for a reporting window */
84
+ interface HttpMetrics {
85
+ totalRequests: number;
86
+ totalErrors: number;
87
+ avgResponseMs: number;
88
+ p50ResponseMs: number;
89
+ p95ResponseMs: number;
90
+ p99ResponseMs: number;
91
+ maxResponseMs: number;
92
+ statusCodes: Record<string, number>;
93
+ topPaths: PathMetric[];
94
+ requestsPerSecond: number;
95
+ }
96
+ /** Per-path HTTP metrics */
97
+ interface PathMetric {
98
+ path: string;
99
+ method: string;
100
+ count: number;
101
+ avgMs: number;
102
+ p95Ms: number;
103
+ errorCount: number;
104
+ }
105
+ /** Error event captured by the tracker */
106
+ interface ErrorEvent {
107
+ message: string;
108
+ stack?: string;
109
+ type: string;
110
+ timestamp: string;
111
+ url?: string;
112
+ method?: string;
113
+ statusCode?: number;
114
+ meta?: Record<string, unknown>;
115
+ }
116
+ /** Security event severity levels */
117
+ type SecuritySeverity = "critical" | "high" | "medium" | "low" | "info";
118
+ /** Types of security events */
119
+ type SecurityEventType = "auth_failure" | "auth_brute_force" | "rate_limit_hit" | "suspicious_path" | "sql_injection" | "xss_attempt" | "command_injection" | "oversized_payload" | "invalid_content_type" | "bot_detected" | "cors_violation" | "csrf_violation" | "api_key_exposed" | "sensitive_data_exposed" | "custom";
120
+ /** Individual security event */
121
+ interface SecurityEvent {
122
+ type: SecurityEventType;
123
+ severity: SecuritySeverity;
124
+ message: string;
125
+ timestamp: string;
126
+ ip?: string;
127
+ userAgent?: string;
128
+ url?: string;
129
+ method?: string;
130
+ payload?: string;
131
+ meta?: Record<string, unknown>;
132
+ }
133
+ /** Aggregated security metrics for a reporting window */
134
+ interface SecurityMetrics {
135
+ totalEvents: number;
136
+ bySeverity: Record<SecuritySeverity, number>;
137
+ byType: Record<string, number>;
138
+ topOffendingIps: Array<{
139
+ ip: string;
140
+ count: number;
141
+ types: string[];
142
+ }>;
143
+ events: SecurityEvent[];
144
+ }
145
+ /** Vulnerability found in a dependency */
146
+ interface DepVulnerability {
147
+ package: string;
148
+ installedVersion: string;
149
+ severity: "critical" | "high" | "moderate" | "low";
150
+ title: string;
151
+ url?: string;
152
+ patchedVersions?: string;
153
+ cwe?: string[];
154
+ }
155
+ /** Dependency audit result */
156
+ interface DepAuditResult {
157
+ totalDeps: number;
158
+ totalVulnerabilities: number;
159
+ bySeverity: Record<string, number>;
160
+ vulnerabilities: DepVulnerability[];
161
+ outdatedCount: number;
162
+ lastAuditAt: string;
163
+ }
164
+ /** Event loop and GC metrics */
165
+ interface RuntimeMetrics {
166
+ eventLoopLagMs: number;
167
+ eventLoopLagP99Ms: number;
168
+ activeHandles: number;
169
+ activeRequests: number;
170
+ gcPausesMsTotal: number;
171
+ gcPausesCount: number;
172
+ heapSpaces?: HeapSpaceInfo[];
173
+ }
174
+ /** V8 heap space detail */
175
+ interface HeapSpaceInfo {
176
+ name: string;
177
+ sizeMb: number;
178
+ usedMb: number;
179
+ availableMb: number;
180
+ }
181
+ /** Environment security check result */
182
+ interface EnvSecurityReport {
183
+ nodeVersion: string;
184
+ nodeVersionSecure: boolean;
185
+ npmVersion?: string;
186
+ frameworkName?: string;
187
+ frameworkVersion?: string;
188
+ securityHeaders: SecurityHeaderCheck[];
189
+ envVarWarnings: string[];
190
+ tlsVersions?: string[];
191
+ debugModeEnabled: boolean;
192
+ sourceMapExposed: boolean;
193
+ v8Version?: string;
194
+ uvVersion?: string;
195
+ opensslVersion?: string;
196
+ zlibVersion?: string;
197
+ icuVersion?: string;
198
+ nodeEnv?: string;
199
+ tz?: string;
200
+ memoryLimitMb?: number | null;
201
+ port?: number | null;
202
+ host?: string;
203
+ dnsServers?: string[];
204
+ }
205
+ /** Security header check result */
206
+ interface SecurityHeaderCheck {
207
+ header: string;
208
+ present: boolean;
209
+ value?: string;
210
+ recommendation?: string;
211
+ }
212
+ /** Full report payload sent to ShieldPress API */
213
+ interface ReportPayload {
214
+ siteId: string;
215
+ appName: string;
216
+ appVersion: string;
217
+ environment: string;
218
+ tags: Record<string, string>;
219
+ timestamp: string;
220
+ system?: SystemMetrics;
221
+ http?: HttpMetrics;
222
+ errors?: ErrorEvent[];
223
+ security?: SecurityMetrics;
224
+ depAudit?: DepAuditResult;
225
+ runtime?: RuntimeMetrics;
226
+ envSecurity?: EnvSecurityReport;
227
+ heartbeat?: boolean;
228
+ }
229
+
230
+ declare class HttpCollector {
231
+ private records;
232
+ private windowStart;
233
+ record(req: {
234
+ path: string;
235
+ method: string;
236
+ statusCode: number;
237
+ durationMs: number;
238
+ }): void;
239
+ flush(): HttpMetrics | null;
240
+ private _flush;
241
+ }
242
+
243
+ declare class ErrorCollector {
244
+ private buffer;
245
+ private maxBuffer;
246
+ constructor(maxBuffer: number);
247
+ capture(error: Error | string, meta?: {
248
+ url?: string;
249
+ method?: string;
250
+ statusCode?: number;
251
+ [key: string]: unknown;
252
+ }): void;
253
+ flush(): ErrorEvent[];
254
+ get size(): number;
255
+ }
256
+
257
+ declare class SecurityCollector {
258
+ private events;
259
+ private maxBuffer;
260
+ private ipCounter;
261
+ constructor(maxBuffer: number);
262
+ /** Record a security event directly */
263
+ record(event: Omit<SecurityEvent, "timestamp">): void;
264
+ /** Analyze an incoming HTTP request for security threats */
265
+ analyzeRequest(req: {
266
+ url: string;
267
+ method: string;
268
+ headers: Record<string, string | string[] | undefined>;
269
+ body?: string;
270
+ ip?: string;
271
+ query?: Record<string, unknown>;
272
+ }): void;
273
+ private _analyzeRequest;
274
+ /** Analyze response for sensitive data leaks */
275
+ analyzeResponse(data: {
276
+ url: string;
277
+ statusCode: number;
278
+ body?: string;
279
+ headers?: Record<string, string>;
280
+ }): void;
281
+ /** Record an authentication failure */
282
+ recordAuthFailure(data: {
283
+ ip?: string;
284
+ userAgent?: string;
285
+ url?: string;
286
+ method?: string;
287
+ reason?: string;
288
+ }): void;
289
+ /** Record a rate limit event */
290
+ recordRateLimit(data: {
291
+ ip?: string;
292
+ url?: string;
293
+ method?: string;
294
+ limit?: number;
295
+ }): void;
296
+ /** Record a CORS violation */
297
+ recordCorsViolation(data: {
298
+ ip?: string;
299
+ origin?: string;
300
+ url?: string;
301
+ }): void;
302
+ /** Record a custom security event */
303
+ recordCustom(type: SecurityEventType, severity: SecuritySeverity, message: string, meta?: Record<string, unknown>): void;
304
+ /** Flush and return aggregated security metrics */
305
+ flush(): SecurityMetrics | null;
306
+ private trackIp;
307
+ private checkBruteForce;
308
+ }
309
+
310
+ declare class ShieldPressTracker {
311
+ readonly config: ResolvedConfig;
312
+ readonly httpCollector: HttpCollector;
313
+ readonly errorCollector: ErrorCollector;
314
+ readonly securityCollector: SecurityCollector;
315
+ private logger;
316
+ private reporter;
317
+ private depAuditCollector;
318
+ private envSecurityCollector;
319
+ private timer;
320
+ private lagTimer;
321
+ private started;
322
+ private headersChecked;
323
+ constructor(userConfig: TrackerConfig);
324
+ /** Start the tracker — begins periodic reporting */
325
+ start(): this;
326
+ /** Stop the tracker */
327
+ stop(): void;
328
+ /** Manually record an HTTP request */
329
+ recordRequest(data: {
330
+ path: string;
331
+ method: string;
332
+ statusCode: number;
333
+ durationMs: number;
334
+ }): void;
335
+ /** Analyze an incoming request for security threats */
336
+ analyzeRequest(req: {
337
+ url: string;
338
+ method: string;
339
+ headers: Record<string, string | string[] | undefined>;
340
+ body?: string;
341
+ ip?: string;
342
+ query?: Record<string, unknown>;
343
+ }): void;
344
+ /** Analyze a response for security issues (headers, data leaks) */
345
+ analyzeResponse(data: {
346
+ url: string;
347
+ statusCode: number;
348
+ headers?: Record<string, string>;
349
+ body?: string;
350
+ }): void;
351
+ /** Record an authentication failure */
352
+ recordAuthFailure(data: {
353
+ ip?: string;
354
+ userAgent?: string;
355
+ url?: string;
356
+ method?: string;
357
+ reason?: string;
358
+ }): void;
359
+ /** Record a rate limit event */
360
+ recordRateLimit(data: {
361
+ ip?: string;
362
+ url?: string;
363
+ method?: string;
364
+ limit?: number;
365
+ }): void;
366
+ /** Record a CORS violation */
367
+ recordCorsViolation(data: {
368
+ ip?: string;
369
+ origin?: string;
370
+ url?: string;
371
+ }): void;
372
+ /** Manually capture an error */
373
+ captureError(error: Error | string, meta?: Record<string, unknown>): void;
374
+ /** Flush all collected data and send report */
375
+ flush(): Promise<boolean>;
376
+ private shouldIgnorePath;
377
+ }
378
+
379
+ export { type DepAuditResult as D, type EnvSecurityReport as E, type HeapSpaceInfo as H, type PathMetric as P, type ResolvedConfig as R, ShieldPressTracker as S, type TrackerConfig as T, type SystemMetrics as a, type RuntimeMetrics as b, type SecurityHeaderCheck as c, type ReportPayload as d, type DepVulnerability as e, ErrorCollector as f, type ErrorEvent as g, HttpCollector as h, type HttpMetrics as i, SecurityCollector as j, type SecurityEvent as k, type SecurityEventType as l, type SecurityMetrics as m, type SecuritySeverity as n };
package/package.json ADDED
@@ -0,0 +1,74 @@
1
+ {
2
+ "name": "@shieldpress/tracker",
3
+ "version": "1.0.0",
4
+ "description": "Website tracking & monitoring SDK for Node.js, Next.js, Express — reports metrics to ShieldPress",
5
+ "main": "dist/index.js",
6
+ "module": "dist/index.mjs",
7
+ "types": "dist/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./dist/index.d.ts",
11
+ "import": "./dist/index.mjs",
12
+ "require": "./dist/index.js"
13
+ },
14
+ "./express": {
15
+ "types": "./dist/express.d.ts",
16
+ "import": "./dist/express.mjs",
17
+ "require": "./dist/express.js"
18
+ },
19
+ "./nextjs": {
20
+ "types": "./dist/nextjs.d.ts",
21
+ "import": "./dist/nextjs.mjs",
22
+ "require": "./dist/nextjs.js"
23
+ }
24
+ },
25
+ "files": [
26
+ "dist"
27
+ ],
28
+ "scripts": {
29
+ "build": "tsup",
30
+ "dev": "tsup --watch",
31
+ "typecheck": "tsc --noEmit",
32
+ "prepublishOnly": "npm run build"
33
+ },
34
+ "keywords": [
35
+ "shieldpress",
36
+ "monitoring",
37
+ "tracking",
38
+ "uptime",
39
+ "nodejs",
40
+ "nextjs",
41
+ "express",
42
+ "apm",
43
+ "metrics",
44
+ "performance"
45
+ ],
46
+ "author": "ShieldPress",
47
+ "license": "MIT",
48
+ "devDependencies": {
49
+ "@types/express": "^5.0.6",
50
+ "@types/node": "^22.0.0",
51
+ "next": "^16.2.10",
52
+ "tsup": "^8.4.0",
53
+ "typescript": "^5.7.0"
54
+ },
55
+ "peerDependencies": {
56
+ "express": ">=4.0.0",
57
+ "next": ">=13.0.0"
58
+ },
59
+ "peerDependenciesMeta": {
60
+ "next": {
61
+ "optional": true
62
+ },
63
+ "express": {
64
+ "optional": true
65
+ }
66
+ },
67
+ "engines": {
68
+ "node": ">=14.0.0"
69
+ },
70
+ "repository": {
71
+ "type": "git",
72
+ "url": "https://github.com/shieldpress/tracker"
73
+ }
74
+ }