@adwait12345/telemetry-next 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 +89 -0
- package/dist/index.mjs +68 -0
- package/package.json +32 -0
- package/src/index.ts +85 -0
- package/tsconfig.json +9 -0
package/dist/index.d.mts
ADDED
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
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) {
|
|
29
|
+
const userAgent = req.headers.get("user-agent") || "";
|
|
30
|
+
const ip = req.ip || req.headers.get("x-forwarded-for") || "";
|
|
31
|
+
const url = new URL(req.url);
|
|
32
|
+
const path = url.pathname;
|
|
33
|
+
if (config.ignorePaths) {
|
|
34
|
+
for (const ignorePath of config.ignorePaths) {
|
|
35
|
+
if (typeof ignorePath === "string" && path === ignorePath) {
|
|
36
|
+
return null;
|
|
37
|
+
} else if (ignorePath instanceof RegExp && ignorePath.test(path)) {
|
|
38
|
+
return null;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
const headersObj = {};
|
|
43
|
+
req.headers.forEach((value, key) => {
|
|
44
|
+
headersObj[key] = value;
|
|
45
|
+
});
|
|
46
|
+
const normalizedReq = {
|
|
47
|
+
userAgent,
|
|
48
|
+
ip: ip.split(",")[0],
|
|
49
|
+
path,
|
|
50
|
+
method: req.method,
|
|
51
|
+
referrer: req.headers.get("referer") || null,
|
|
52
|
+
acceptLanguage: req.headers.get("accept-language") || null,
|
|
53
|
+
acceptEncoding: req.headers.get("accept-encoding") || null,
|
|
54
|
+
secFetchSite: req.headers.get("sec-fetch-site") || null,
|
|
55
|
+
httpVersion: null,
|
|
56
|
+
// Next.js middleware doesn't easily expose http version
|
|
57
|
+
automationHeaders: (0, import_telemetry_core.extractAutomationHeaders)(headersObj)
|
|
58
|
+
};
|
|
59
|
+
const result = (0, import_telemetry_core.detectBot)(normalizedReq, config.customBots);
|
|
60
|
+
const shouldTrackBot = result.isBot && (config.trackSearchBots !== false || result.botCategory !== "search");
|
|
61
|
+
if (config.trackAll || shouldTrackBot) {
|
|
62
|
+
const payload = {
|
|
63
|
+
projectId: config.projectId,
|
|
64
|
+
path: normalizedReq.path,
|
|
65
|
+
method: normalizedReq.method,
|
|
66
|
+
referrer: normalizedReq.referrer,
|
|
67
|
+
userAgent: normalizedReq.userAgent,
|
|
68
|
+
ip: normalizedReq.ip,
|
|
69
|
+
isBot: result.isBot,
|
|
70
|
+
botName: result.botName,
|
|
71
|
+
botCategory: result.botCategory,
|
|
72
|
+
confidence: result.confidence,
|
|
73
|
+
detectionMethod: result.method,
|
|
74
|
+
source: "server-middleware",
|
|
75
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
76
|
+
};
|
|
77
|
+
(0, import_telemetry_core.sendToTelemetry)(payload, config).catch((err) => {
|
|
78
|
+
if (config.debug) {
|
|
79
|
+
console.error("[Telemetry Middleware] Failed to send to telemetry:", err);
|
|
80
|
+
}
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
return null;
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
87
|
+
0 && (module.exports = {
|
|
88
|
+
telemetryMiddleware
|
|
89
|
+
});
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
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) {
|
|
9
|
+
const userAgent = req.headers.get("user-agent") || "";
|
|
10
|
+
const ip = req.ip || req.headers.get("x-forwarded-for") || "";
|
|
11
|
+
const url = new URL(req.url);
|
|
12
|
+
const path = url.pathname;
|
|
13
|
+
if (config.ignorePaths) {
|
|
14
|
+
for (const ignorePath of config.ignorePaths) {
|
|
15
|
+
if (typeof ignorePath === "string" && path === ignorePath) {
|
|
16
|
+
return null;
|
|
17
|
+
} else if (ignorePath instanceof RegExp && ignorePath.test(path)) {
|
|
18
|
+
return null;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
const headersObj = {};
|
|
23
|
+
req.headers.forEach((value, key) => {
|
|
24
|
+
headersObj[key] = value;
|
|
25
|
+
});
|
|
26
|
+
const normalizedReq = {
|
|
27
|
+
userAgent,
|
|
28
|
+
ip: ip.split(",")[0],
|
|
29
|
+
path,
|
|
30
|
+
method: req.method,
|
|
31
|
+
referrer: req.headers.get("referer") || null,
|
|
32
|
+
acceptLanguage: req.headers.get("accept-language") || null,
|
|
33
|
+
acceptEncoding: req.headers.get("accept-encoding") || null,
|
|
34
|
+
secFetchSite: req.headers.get("sec-fetch-site") || null,
|
|
35
|
+
httpVersion: null,
|
|
36
|
+
// Next.js middleware doesn't easily expose http version
|
|
37
|
+
automationHeaders: extractAutomationHeaders(headersObj)
|
|
38
|
+
};
|
|
39
|
+
const result = detectBot(normalizedReq, config.customBots);
|
|
40
|
+
const shouldTrackBot = result.isBot && (config.trackSearchBots !== false || result.botCategory !== "search");
|
|
41
|
+
if (config.trackAll || shouldTrackBot) {
|
|
42
|
+
const payload = {
|
|
43
|
+
projectId: config.projectId,
|
|
44
|
+
path: normalizedReq.path,
|
|
45
|
+
method: normalizedReq.method,
|
|
46
|
+
referrer: normalizedReq.referrer,
|
|
47
|
+
userAgent: normalizedReq.userAgent,
|
|
48
|
+
ip: normalizedReq.ip,
|
|
49
|
+
isBot: result.isBot,
|
|
50
|
+
botName: result.botName,
|
|
51
|
+
botCategory: result.botCategory,
|
|
52
|
+
confidence: result.confidence,
|
|
53
|
+
detectionMethod: result.method,
|
|
54
|
+
source: "server-middleware",
|
|
55
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
56
|
+
};
|
|
57
|
+
sendToTelemetry(payload, config).catch((err) => {
|
|
58
|
+
if (config.debug) {
|
|
59
|
+
console.error("[Telemetry Middleware] Failed to send to telemetry:", err);
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
return null;
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
export {
|
|
67
|
+
telemetryMiddleware
|
|
68
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@adwait12345/telemetry-next",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Next.js 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
|
+
"next": "^14.0.0",
|
|
20
|
+
"tsup": "^8.0.0",
|
|
21
|
+
"typescript": "^5.4.0"
|
|
22
|
+
},
|
|
23
|
+
"peerDependencies": {
|
|
24
|
+
"next": ">=13.0.0"
|
|
25
|
+
},
|
|
26
|
+
"scripts": {
|
|
27
|
+
"build": "tsup src/index.ts --format esm,cjs --dts --clean",
|
|
28
|
+
"dev": "tsup src/index.ts --format esm,cjs --dts --watch",
|
|
29
|
+
"typecheck": "tsc --noEmit",
|
|
30
|
+
"clean": "rm -rf dist"
|
|
31
|
+
}
|
|
32
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { ServerTrackPayload } from "@adwait12345/telemetry-core";
|
|
2
|
+
import { type NextRequest, type NextResponse } from "next/server";
|
|
3
|
+
import {
|
|
4
|
+
detectBot,
|
|
5
|
+
extractAutomationHeaders,
|
|
6
|
+
sendToTelemetry,
|
|
7
|
+
type TelemetryConfig,
|
|
8
|
+
type NormalizedRequest,
|
|
9
|
+
} from "@adwait12345/telemetry-core";
|
|
10
|
+
|
|
11
|
+
export function telemetryMiddleware(config: TelemetryConfig) {
|
|
12
|
+
return function (req: NextRequest) {
|
|
13
|
+
const userAgent = req.headers.get("user-agent") || "";
|
|
14
|
+
const ip = req.ip || req.headers.get("x-forwarded-for") || "";
|
|
15
|
+
const url = new URL(req.url);
|
|
16
|
+
const path = url.pathname;
|
|
17
|
+
|
|
18
|
+
if (config.ignorePaths) {
|
|
19
|
+
for (const ignorePath of config.ignorePaths) {
|
|
20
|
+
if (typeof ignorePath === "string" && path === ignorePath) {
|
|
21
|
+
return null;
|
|
22
|
+
} else if (ignorePath instanceof RegExp && ignorePath.test(path)) {
|
|
23
|
+
return null;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// Convert headers to a standard object for extractAutomationHeaders
|
|
29
|
+
const headersObj: Record<string, string> = {};
|
|
30
|
+
req.headers.forEach((value, key) => {
|
|
31
|
+
headersObj[key] = value;
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
const normalizedReq: NormalizedRequest = {
|
|
35
|
+
userAgent,
|
|
36
|
+
ip: ip.split(",")[0],
|
|
37
|
+
path,
|
|
38
|
+
method: req.method,
|
|
39
|
+
referrer: req.headers.get("referer") || null,
|
|
40
|
+
acceptLanguage: req.headers.get("accept-language") || null,
|
|
41
|
+
acceptEncoding: req.headers.get("accept-encoding") || null,
|
|
42
|
+
secFetchSite: req.headers.get("sec-fetch-site") || null,
|
|
43
|
+
httpVersion: null, // Next.js middleware doesn't easily expose http version
|
|
44
|
+
automationHeaders: extractAutomationHeaders(headersObj),
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
const result = detectBot(normalizedReq, config.customBots);
|
|
48
|
+
|
|
49
|
+
const shouldTrackBot =
|
|
50
|
+
result.isBot &&
|
|
51
|
+
(config.trackSearchBots !== false || result.botCategory !== "search");
|
|
52
|
+
|
|
53
|
+
if (config.trackAll || shouldTrackBot) {
|
|
54
|
+
const payload: ServerTrackPayload = {
|
|
55
|
+
projectId: config.projectId,
|
|
56
|
+
path: normalizedReq.path,
|
|
57
|
+
method: normalizedReq.method,
|
|
58
|
+
referrer: normalizedReq.referrer,
|
|
59
|
+
userAgent: normalizedReq.userAgent,
|
|
60
|
+
ip: normalizedReq.ip,
|
|
61
|
+
isBot: result.isBot,
|
|
62
|
+
botName: result.botName,
|
|
63
|
+
botCategory: result.botCategory,
|
|
64
|
+
confidence: result.confidence,
|
|
65
|
+
detectionMethod: result.method,
|
|
66
|
+
source: "server-middleware",
|
|
67
|
+
timestamp: new Date().toISOString(),
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
// Ensure fetch is not blocking
|
|
71
|
+
// next/server context allows non-blocking promises implicitly sometimes
|
|
72
|
+
// but in Next.js middleware you often need e.waitUntil
|
|
73
|
+
sendToTelemetry(payload, config).catch((err: any) => {
|
|
74
|
+
if (config.debug) {
|
|
75
|
+
console.error(
|
|
76
|
+
"[Telemetry Middleware] Failed to send to telemetry:",
|
|
77
|
+
err,
|
|
78
|
+
);
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
return null;
|
|
84
|
+
};
|
|
85
|
+
}
|