@adwait12345/telemetry-core 0.1.3 → 0.1.4
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/README.md +127 -0
- package/dist/index.js +6 -2
- package/dist/index.mjs +6 -2
- package/package.json +3 -2
package/README.md
ADDED
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
# `@adwait12345/telemetry-core`
|
|
2
|
+
|
|
3
|
+
The shared core of the Telemetry SDK. Contains bot detection logic, the send function with retry support, and all shared TypeScript types used by the framework adapters (`telemetry-next`, `telemetry-express`, `telemetry-astro`).
|
|
4
|
+
|
|
5
|
+
You typically do **not** install this directly — install the adapter for your framework instead. Use this package if you are building a custom adapter.
|
|
6
|
+
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
## Installation
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
npm install @adwait12345/telemetry-core
|
|
13
|
+
# or
|
|
14
|
+
pnpm add @adwait12345/telemetry-core
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
---
|
|
18
|
+
|
|
19
|
+
## What's included
|
|
20
|
+
|
|
21
|
+
### `detectBot(req, customBots?)`
|
|
22
|
+
|
|
23
|
+
Runs multi-layer bot detection against a normalized request object.
|
|
24
|
+
|
|
25
|
+
**Detection layers (in priority order):**
|
|
26
|
+
|
|
27
|
+
| Layer | Method | Confidence |
|
|
28
|
+
|-------|--------|------------|
|
|
29
|
+
| 1 | Automation headers (`x-selenium`, `x-puppeteer`, `x-playwright`, etc.) | `certain` |
|
|
30
|
+
| 2 | HTTP/1.0 — no modern browser uses this | `high` |
|
|
31
|
+
| 3 | Named bot UA match (100+ known bots across 16 categories) | `certain` |
|
|
32
|
+
| 4 | Generic bot UA patterns (`/bot/i`, `/crawler/i`, empty/short UA, etc.) | `high` |
|
|
33
|
+
| 5 | Header anomaly — claims modern browser but missing `sec-fetch-site` / `accept-language` | `medium`–`high` |
|
|
34
|
+
|
|
35
|
+
```typescript
|
|
36
|
+
import { detectBot } from "@adwait12345/telemetry-core";
|
|
37
|
+
|
|
38
|
+
const result = detectBot({
|
|
39
|
+
userAgent: "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)",
|
|
40
|
+
ip: "66.249.66.1",
|
|
41
|
+
path: "/",
|
|
42
|
+
method: "GET",
|
|
43
|
+
referrer: null,
|
|
44
|
+
acceptLanguage: null,
|
|
45
|
+
acceptEncoding: "gzip",
|
|
46
|
+
secFetchSite: null,
|
|
47
|
+
httpVersion: "1.1",
|
|
48
|
+
automationHeaders: [],
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
// result.isBot → true
|
|
52
|
+
// result.botName → "Googlebot"
|
|
53
|
+
// result.botCategory → "search"
|
|
54
|
+
// result.confidence → "certain"
|
|
55
|
+
// result.method → "ua-match"
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
### `extractAutomationHeaders(headers)`
|
|
59
|
+
|
|
60
|
+
Checks a plain headers object for known automation tool headers. Returns the list of matched header names. Call this before `detectBot` to populate `automationHeaders`.
|
|
61
|
+
|
|
62
|
+
```typescript
|
|
63
|
+
import { extractAutomationHeaders } from "@adwait12345/telemetry-core";
|
|
64
|
+
|
|
65
|
+
const matched = extractAutomationHeaders(req.headers);
|
|
66
|
+
// e.g. ["x-playwright"] if Playwright is driving the browser
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
### `sendToTelemetry(payload, config)`
|
|
70
|
+
|
|
71
|
+
Sends a tracking payload to the Telemetry API. Includes:
|
|
72
|
+
- **3 attempts** with exponential backoff (200 ms → 400 ms)
|
|
73
|
+
- **5 second timeout** per attempt via `AbortController`
|
|
74
|
+
- **4xx errors skip retry** (they will not succeed on retry)
|
|
75
|
+
- Automatic `Authorization: Bearer {serverSecret}` header
|
|
76
|
+
|
|
77
|
+
```typescript
|
|
78
|
+
import { sendToTelemetry } from "@adwait12345/telemetry-core";
|
|
79
|
+
|
|
80
|
+
await sendToTelemetry(payload, {
|
|
81
|
+
projectId: "your-project-id",
|
|
82
|
+
apiUrl: "https://telemetry-uqd3.onrender.com",
|
|
83
|
+
serverSecret: "sk_...",
|
|
84
|
+
});
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
---
|
|
88
|
+
|
|
89
|
+
## Bot categories
|
|
90
|
+
|
|
91
|
+
The 100+ built-in bots are organized into 16 categories:
|
|
92
|
+
|
|
93
|
+
| Category | Examples |
|
|
94
|
+
|----------|---------|
|
|
95
|
+
| `ai-crawler` | GPTBot, ClaudeBot, PerplexityBot, Applebot |
|
|
96
|
+
| `ai-assistant` | ChatGPT-User, Claude-Web, Copilot |
|
|
97
|
+
| `search` | Googlebot, Bingbot, DuckDuckBot, Yandex |
|
|
98
|
+
| `seo` | AhrefsBot, SemrushBot, MajesticSEO |
|
|
99
|
+
| `advertising` | Mediapartners-Google, AdsBot-Google |
|
|
100
|
+
| `monitor` | UptimeRobot, Pingdom, StatusCake |
|
|
101
|
+
| `preview` | Slackbot, Twitterbot, facebookexternalhit |
|
|
102
|
+
| `webhook` | Stripe-Webhook, GitHub-Hookshot, Shopify |
|
|
103
|
+
| `feed` | Feedly, Inoreader, NewsBlur |
|
|
104
|
+
| `ecommerce` | Shopify, Wix |
|
|
105
|
+
| `verification` | Let's Encrypt, SSL Labs |
|
|
106
|
+
| `analytics` | New Relic, Datadog |
|
|
107
|
+
| `social` | LinkedInBot, Pinterest |
|
|
108
|
+
| `accessibility` | ChromeVox |
|
|
109
|
+
| `scraper` | Scrapy, HTTrack |
|
|
110
|
+
| `unknown` | Anything matching generic bot patterns |
|
|
111
|
+
|
|
112
|
+
---
|
|
113
|
+
|
|
114
|
+
## `TelemetryConfig` reference
|
|
115
|
+
|
|
116
|
+
| Option | Type | Default | Description |
|
|
117
|
+
|--------|------|---------|-------------|
|
|
118
|
+
| `projectId` | `string` | **required** | Your Telemetry project ID |
|
|
119
|
+
| `apiUrl` | `string` | hosted service | API base URL |
|
|
120
|
+
| `serverSecret` | `string` | — | Secret key for server-side auth (`Bearer {serverSecret}`) |
|
|
121
|
+
| `authorizationHeader` | `string` | — | Fully custom `Authorization` header value, overrides `serverSecret` |
|
|
122
|
+
| `headers` | `Record<string, string>` | — | Extra headers merged into every telemetry request |
|
|
123
|
+
| `trackAll` | `boolean` | `false` | Track all requests, not just bots |
|
|
124
|
+
| `trackSearchBots` | `boolean` | `true` | Include Googlebot, Bingbot etc. |
|
|
125
|
+
| `ignorePaths` | `(string \| RegExp)[]` | — | Paths to skip (exact strings or regex) |
|
|
126
|
+
| `customBots` | `Array<{name, pattern, category?}>` | — | Add your own bot definitions |
|
|
127
|
+
| `debug` | `boolean` | `false` | Enable verbose console logging |
|
package/dist/index.js
CHANGED
|
@@ -1151,7 +1151,9 @@ async function sendWithRetry(endpoint, payload, config, retries = 2) {
|
|
|
1151
1151
|
clearTimeout(timeout);
|
|
1152
1152
|
if (config.debug) {
|
|
1153
1153
|
if (error?.name === "AbortError") {
|
|
1154
|
-
console.error(
|
|
1154
|
+
console.error(
|
|
1155
|
+
`[Telemetry] Attempt ${attempt + 1} timed out after 5s.`
|
|
1156
|
+
);
|
|
1155
1157
|
} else {
|
|
1156
1158
|
console.error(`[Telemetry] Attempt ${attempt + 1} error:`, error);
|
|
1157
1159
|
}
|
|
@@ -1168,7 +1170,9 @@ async function sendWithRetry(endpoint, payload, config, retries = 2) {
|
|
|
1168
1170
|
async function sendToTelemetry(payload, config) {
|
|
1169
1171
|
if (!config.apiUrl) {
|
|
1170
1172
|
if (config.debug) {
|
|
1171
|
-
console.warn(
|
|
1173
|
+
console.warn(
|
|
1174
|
+
"[Telemetry] No apiUrl configured \u2014 skipping telemetry send."
|
|
1175
|
+
);
|
|
1172
1176
|
}
|
|
1173
1177
|
return;
|
|
1174
1178
|
}
|
package/dist/index.mjs
CHANGED
|
@@ -1107,7 +1107,9 @@ async function sendWithRetry(endpoint, payload, config, retries = 2) {
|
|
|
1107
1107
|
clearTimeout(timeout);
|
|
1108
1108
|
if (config.debug) {
|
|
1109
1109
|
if (error?.name === "AbortError") {
|
|
1110
|
-
console.error(
|
|
1110
|
+
console.error(
|
|
1111
|
+
`[Telemetry] Attempt ${attempt + 1} timed out after 5s.`
|
|
1112
|
+
);
|
|
1111
1113
|
} else {
|
|
1112
1114
|
console.error(`[Telemetry] Attempt ${attempt + 1} error:`, error);
|
|
1113
1115
|
}
|
|
@@ -1124,7 +1126,9 @@ async function sendWithRetry(endpoint, payload, config, retries = 2) {
|
|
|
1124
1126
|
async function sendToTelemetry(payload, config) {
|
|
1125
1127
|
if (!config.apiUrl) {
|
|
1126
1128
|
if (config.debug) {
|
|
1127
|
-
console.warn(
|
|
1129
|
+
console.warn(
|
|
1130
|
+
"[Telemetry] No apiUrl configured \u2014 skipping telemetry send."
|
|
1131
|
+
);
|
|
1128
1132
|
}
|
|
1129
1133
|
return;
|
|
1130
1134
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@adwait12345/telemetry-core",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.4",
|
|
4
4
|
"description": "Framework-agnostic core for Telemetry SDK — bot detection and server-side tracking",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"module": "./dist/index.mjs",
|
|
@@ -13,7 +13,8 @@
|
|
|
13
13
|
}
|
|
14
14
|
},
|
|
15
15
|
"files": [
|
|
16
|
-
"dist"
|
|
16
|
+
"dist",
|
|
17
|
+
"README.md"
|
|
17
18
|
],
|
|
18
19
|
"devDependencies": {
|
|
19
20
|
"tsup": "^8.0.0",
|