@adwait12345/telemetry-express 0.1.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 +6 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +83 -0
- package/dist/index.mjs +62 -0
- package/package.json +33 -0
- package/src/index.ts +83 -0
- package/tsconfig.json +9 -0
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { Request, Response, NextFunction } from 'express';
|
|
2
|
+
import { TelemetryConfig } from '@adwait12345/telemetry-core';
|
|
3
|
+
|
|
4
|
+
declare function telemetryMiddleware(config: TelemetryConfig): (req: Request, res: Response, next: NextFunction) => void;
|
|
5
|
+
|
|
6
|
+
export { telemetryMiddleware };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { Request, Response, NextFunction } from 'express';
|
|
2
|
+
import { TelemetryConfig } from '@adwait12345/telemetry-core';
|
|
3
|
+
|
|
4
|
+
declare function telemetryMiddleware(config: TelemetryConfig): (req: Request, res: Response, next: NextFunction) => void;
|
|
5
|
+
|
|
6
|
+
export { telemetryMiddleware };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
telemetryMiddleware: () => telemetryMiddleware
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(index_exports);
|
|
26
|
+
var import_telemetry_core = require("@adwait12345/telemetry-core");
|
|
27
|
+
function telemetryMiddleware(config) {
|
|
28
|
+
return function(req, res, next) {
|
|
29
|
+
const userAgent = req.get("user-agent") || "";
|
|
30
|
+
const ip = req.ip || req.headers["x-forwarded-for"] || "";
|
|
31
|
+
const path = req.originalUrl || req.url;
|
|
32
|
+
if (config.ignorePaths) {
|
|
33
|
+
for (const ignorePath of config.ignorePaths) {
|
|
34
|
+
if (typeof ignorePath === "string" && path === ignorePath) {
|
|
35
|
+
return next();
|
|
36
|
+
} else if (ignorePath instanceof RegExp && ignorePath.test(path)) {
|
|
37
|
+
return next();
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
const normalizedReq = {
|
|
42
|
+
userAgent,
|
|
43
|
+
ip: Array.isArray(ip) ? ip[0] : ip.split(",")[0],
|
|
44
|
+
path,
|
|
45
|
+
method: req.method,
|
|
46
|
+
referrer: req.get("referrer") || null,
|
|
47
|
+
acceptLanguage: req.get("accept-language") || null,
|
|
48
|
+
acceptEncoding: req.get("accept-encoding") || null,
|
|
49
|
+
secFetchSite: req.get("sec-fetch-site") || null,
|
|
50
|
+
httpVersion: req.httpVersion || null,
|
|
51
|
+
automationHeaders: (0, import_telemetry_core.extractAutomationHeaders)(req.headers)
|
|
52
|
+
};
|
|
53
|
+
const result = (0, import_telemetry_core.detectBot)(normalizedReq, config.customBots);
|
|
54
|
+
const shouldTrackBot = result.isBot && (config.trackSearchBots !== false || result.botCategory !== "search");
|
|
55
|
+
if (config.trackAll || shouldTrackBot) {
|
|
56
|
+
const payload = {
|
|
57
|
+
projectId: config.projectId,
|
|
58
|
+
path: normalizedReq.path,
|
|
59
|
+
method: normalizedReq.method,
|
|
60
|
+
referrer: normalizedReq.referrer,
|
|
61
|
+
userAgent: normalizedReq.userAgent,
|
|
62
|
+
ip: normalizedReq.ip,
|
|
63
|
+
isBot: result.isBot,
|
|
64
|
+
botName: result.botName,
|
|
65
|
+
botCategory: result.botCategory,
|
|
66
|
+
confidence: result.confidence,
|
|
67
|
+
detectionMethod: result.method,
|
|
68
|
+
source: "server-middleware",
|
|
69
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
70
|
+
};
|
|
71
|
+
(0, import_telemetry_core.sendToTelemetry)(payload, config).catch((err) => {
|
|
72
|
+
if (config.debug) {
|
|
73
|
+
console.error("[Telemetry Middleware] Failed to send to telemetry:", err);
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
next();
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
81
|
+
0 && (module.exports = {
|
|
82
|
+
telemetryMiddleware
|
|
83
|
+
});
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
import {
|
|
3
|
+
detectBot,
|
|
4
|
+
extractAutomationHeaders,
|
|
5
|
+
sendToTelemetry
|
|
6
|
+
} from "@adwait12345/telemetry-core";
|
|
7
|
+
function telemetryMiddleware(config) {
|
|
8
|
+
return function(req, res, next) {
|
|
9
|
+
const userAgent = req.get("user-agent") || "";
|
|
10
|
+
const ip = req.ip || req.headers["x-forwarded-for"] || "";
|
|
11
|
+
const path = req.originalUrl || req.url;
|
|
12
|
+
if (config.ignorePaths) {
|
|
13
|
+
for (const ignorePath of config.ignorePaths) {
|
|
14
|
+
if (typeof ignorePath === "string" && path === ignorePath) {
|
|
15
|
+
return next();
|
|
16
|
+
} else if (ignorePath instanceof RegExp && ignorePath.test(path)) {
|
|
17
|
+
return next();
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
const normalizedReq = {
|
|
22
|
+
userAgent,
|
|
23
|
+
ip: Array.isArray(ip) ? ip[0] : ip.split(",")[0],
|
|
24
|
+
path,
|
|
25
|
+
method: req.method,
|
|
26
|
+
referrer: req.get("referrer") || null,
|
|
27
|
+
acceptLanguage: req.get("accept-language") || null,
|
|
28
|
+
acceptEncoding: req.get("accept-encoding") || null,
|
|
29
|
+
secFetchSite: req.get("sec-fetch-site") || null,
|
|
30
|
+
httpVersion: req.httpVersion || null,
|
|
31
|
+
automationHeaders: extractAutomationHeaders(req.headers)
|
|
32
|
+
};
|
|
33
|
+
const result = detectBot(normalizedReq, config.customBots);
|
|
34
|
+
const shouldTrackBot = result.isBot && (config.trackSearchBots !== false || result.botCategory !== "search");
|
|
35
|
+
if (config.trackAll || shouldTrackBot) {
|
|
36
|
+
const payload = {
|
|
37
|
+
projectId: config.projectId,
|
|
38
|
+
path: normalizedReq.path,
|
|
39
|
+
method: normalizedReq.method,
|
|
40
|
+
referrer: normalizedReq.referrer,
|
|
41
|
+
userAgent: normalizedReq.userAgent,
|
|
42
|
+
ip: normalizedReq.ip,
|
|
43
|
+
isBot: result.isBot,
|
|
44
|
+
botName: result.botName,
|
|
45
|
+
botCategory: result.botCategory,
|
|
46
|
+
confidence: result.confidence,
|
|
47
|
+
detectionMethod: result.method,
|
|
48
|
+
source: "server-middleware",
|
|
49
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
50
|
+
};
|
|
51
|
+
sendToTelemetry(payload, config).catch((err) => {
|
|
52
|
+
if (config.debug) {
|
|
53
|
+
console.error("[Telemetry Middleware] Failed to send to telemetry:", err);
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
next();
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
export {
|
|
61
|
+
telemetryMiddleware
|
|
62
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@adwait12345/telemetry-express",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Express middleware for Telemetry SDK - track AI bots and crawlers",
|
|
5
|
+
"main": "./dist/index.js",
|
|
6
|
+
"module": "./dist/index.mjs",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"import": "./dist/index.mjs",
|
|
11
|
+
"require": "./dist/index.js",
|
|
12
|
+
"types": "./dist/index.d.ts"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"dependencies": {
|
|
16
|
+
"@adwait12345/telemetry-core": "0.1.0"
|
|
17
|
+
},
|
|
18
|
+
"devDependencies": {
|
|
19
|
+
"@types/express": "^4.17.21",
|
|
20
|
+
"express": "^4.18.2",
|
|
21
|
+
"tsup": "^8.0.0",
|
|
22
|
+
"typescript": "^5.4.0"
|
|
23
|
+
},
|
|
24
|
+
"peerDependencies": {
|
|
25
|
+
"express": ">=4.0.0"
|
|
26
|
+
},
|
|
27
|
+
"scripts": {
|
|
28
|
+
"build": "tsup src/index.ts --format esm,cjs --dts --clean",
|
|
29
|
+
"dev": "tsup src/index.ts --format esm,cjs --dts --watch",
|
|
30
|
+
"typecheck": "tsc --noEmit",
|
|
31
|
+
"clean": "rm -rf dist"
|
|
32
|
+
}
|
|
33
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { ServerTrackPayload } from "@adwait12345/telemetry-core";
|
|
2
|
+
|
|
3
|
+
// Mocking some request structures here just so we can write the package
|
|
4
|
+
import type { Request, Response, NextFunction } from "express";
|
|
5
|
+
import {
|
|
6
|
+
detectBot,
|
|
7
|
+
extractAutomationHeaders,
|
|
8
|
+
sendToTelemetry,
|
|
9
|
+
type TelemetryConfig,
|
|
10
|
+
type NormalizedRequest,
|
|
11
|
+
} from "@adwait12345/telemetry-core";
|
|
12
|
+
|
|
13
|
+
export function telemetryMiddleware(config: TelemetryConfig) {
|
|
14
|
+
return function (req: Request, res: Response, next: NextFunction) {
|
|
15
|
+
// 1. Normalize the request
|
|
16
|
+
const userAgent = req.get("user-agent") || "";
|
|
17
|
+
const ip = req.ip || (req.headers["x-forwarded-for"] as string) || "";
|
|
18
|
+
const path = req.originalUrl || req.url;
|
|
19
|
+
|
|
20
|
+
// Check ignore paths first
|
|
21
|
+
if (config.ignorePaths) {
|
|
22
|
+
for (const ignorePath of config.ignorePaths) {
|
|
23
|
+
if (typeof ignorePath === "string" && path === ignorePath) {
|
|
24
|
+
return next();
|
|
25
|
+
} else if (ignorePath instanceof RegExp && ignorePath.test(path)) {
|
|
26
|
+
return next();
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const normalizedReq: NormalizedRequest = {
|
|
32
|
+
userAgent,
|
|
33
|
+
ip: Array.isArray(ip) ? ip[0] : ip.split(",")[0],
|
|
34
|
+
path,
|
|
35
|
+
method: req.method,
|
|
36
|
+
referrer: req.get("referrer") || null,
|
|
37
|
+
acceptLanguage: req.get("accept-language") || null,
|
|
38
|
+
acceptEncoding: req.get("accept-encoding") || null,
|
|
39
|
+
secFetchSite: req.get("sec-fetch-site") || null,
|
|
40
|
+
httpVersion: req.httpVersion || null,
|
|
41
|
+
automationHeaders: extractAutomationHeaders(req.headers as any),
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
// 2. Detect bots
|
|
45
|
+
const result = detectBot(normalizedReq, config.customBots);
|
|
46
|
+
|
|
47
|
+
// 3. Check if we should track
|
|
48
|
+
const shouldTrackBot =
|
|
49
|
+
result.isBot &&
|
|
50
|
+
(config.trackSearchBots !== false || result.botCategory !== "search");
|
|
51
|
+
|
|
52
|
+
if (config.trackAll || shouldTrackBot) {
|
|
53
|
+
const payload: ServerTrackPayload = {
|
|
54
|
+
projectId: config.projectId,
|
|
55
|
+
path: normalizedReq.path,
|
|
56
|
+
method: normalizedReq.method,
|
|
57
|
+
referrer: normalizedReq.referrer,
|
|
58
|
+
userAgent: normalizedReq.userAgent,
|
|
59
|
+
ip: normalizedReq.ip,
|
|
60
|
+
isBot: result.isBot,
|
|
61
|
+
botName: result.botName,
|
|
62
|
+
botCategory: result.botCategory,
|
|
63
|
+
confidence: result.confidence,
|
|
64
|
+
detectionMethod: result.method,
|
|
65
|
+
source: "server-middleware",
|
|
66
|
+
timestamp: new Date().toISOString(),
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
// Fire and forget
|
|
70
|
+
sendToTelemetry(payload, config).catch((err: any) => {
|
|
71
|
+
if (config.debug) {
|
|
72
|
+
console.error(
|
|
73
|
+
"[Telemetry Middleware] Failed to send to telemetry:",
|
|
74
|
+
err,
|
|
75
|
+
);
|
|
76
|
+
}
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// Always continue the request
|
|
81
|
+
next();
|
|
82
|
+
};
|
|
83
|
+
}
|