@adwait12345/telemetry-next 0.1.0 → 0.1.2
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 +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +13 -6
- package/dist/index.mjs +13 -6
- package/package.json +2 -2
- package/src/index.ts +13 -9
package/dist/index.d.mts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { NextRequest } from 'next/server';
|
|
2
2
|
import { TelemetryConfig } from '@adwait12345/telemetry-core';
|
|
3
3
|
|
|
4
|
-
declare function telemetryMiddleware(config: TelemetryConfig): (req: NextRequest) =>
|
|
4
|
+
declare function telemetryMiddleware(config: TelemetryConfig): (req: NextRequest) => Promise<void>;
|
|
5
5
|
|
|
6
6
|
export { telemetryMiddleware };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { NextRequest } from 'next/server';
|
|
2
2
|
import { TelemetryConfig } from '@adwait12345/telemetry-core';
|
|
3
3
|
|
|
4
|
-
declare function telemetryMiddleware(config: TelemetryConfig): (req: NextRequest) =>
|
|
4
|
+
declare function telemetryMiddleware(config: TelemetryConfig): (req: NextRequest) => Promise<void>;
|
|
5
5
|
|
|
6
6
|
export { telemetryMiddleware };
|
package/dist/index.js
CHANGED
|
@@ -25,7 +25,7 @@ __export(index_exports, {
|
|
|
25
25
|
module.exports = __toCommonJS(index_exports);
|
|
26
26
|
var import_telemetry_core = require("@adwait12345/telemetry-core");
|
|
27
27
|
function telemetryMiddleware(config) {
|
|
28
|
-
return function(req) {
|
|
28
|
+
return async function(req) {
|
|
29
29
|
const userAgent = req.headers.get("user-agent") || "";
|
|
30
30
|
const ip = req.ip || req.headers.get("x-forwarded-for") || "";
|
|
31
31
|
const url = new URL(req.url);
|
|
@@ -33,9 +33,9 @@ function telemetryMiddleware(config) {
|
|
|
33
33
|
if (config.ignorePaths) {
|
|
34
34
|
for (const ignorePath of config.ignorePaths) {
|
|
35
35
|
if (typeof ignorePath === "string" && path === ignorePath) {
|
|
36
|
-
return
|
|
36
|
+
return;
|
|
37
37
|
} else if (ignorePath instanceof RegExp && ignorePath.test(path)) {
|
|
38
|
-
return
|
|
38
|
+
return;
|
|
39
39
|
}
|
|
40
40
|
}
|
|
41
41
|
}
|
|
@@ -74,13 +74,20 @@ function telemetryMiddleware(config) {
|
|
|
74
74
|
source: "server-middleware",
|
|
75
75
|
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
76
76
|
};
|
|
77
|
-
(0, import_telemetry_core.sendToTelemetry)(payload, config).catch((err) => {
|
|
77
|
+
const sendPromise = (0, import_telemetry_core.sendToTelemetry)(payload, config).catch((err) => {
|
|
78
78
|
if (config.debug) {
|
|
79
|
-
console.error(
|
|
79
|
+
console.error(
|
|
80
|
+
"[Telemetry Middleware] Failed to send to telemetry:",
|
|
81
|
+
err
|
|
82
|
+
);
|
|
80
83
|
}
|
|
81
84
|
});
|
|
85
|
+
if (typeof globalThis !== "undefined" && globalThis.waitUntil) {
|
|
86
|
+
globalThis.waitUntil(sendPromise);
|
|
87
|
+
} else {
|
|
88
|
+
await sendPromise;
|
|
89
|
+
}
|
|
82
90
|
}
|
|
83
|
-
return null;
|
|
84
91
|
};
|
|
85
92
|
}
|
|
86
93
|
// Annotate the CommonJS export names for ESM import in node:
|
package/dist/index.mjs
CHANGED
|
@@ -5,7 +5,7 @@ import {
|
|
|
5
5
|
sendToTelemetry
|
|
6
6
|
} from "@adwait12345/telemetry-core";
|
|
7
7
|
function telemetryMiddleware(config) {
|
|
8
|
-
return function(req) {
|
|
8
|
+
return async function(req) {
|
|
9
9
|
const userAgent = req.headers.get("user-agent") || "";
|
|
10
10
|
const ip = req.ip || req.headers.get("x-forwarded-for") || "";
|
|
11
11
|
const url = new URL(req.url);
|
|
@@ -13,9 +13,9 @@ function telemetryMiddleware(config) {
|
|
|
13
13
|
if (config.ignorePaths) {
|
|
14
14
|
for (const ignorePath of config.ignorePaths) {
|
|
15
15
|
if (typeof ignorePath === "string" && path === ignorePath) {
|
|
16
|
-
return
|
|
16
|
+
return;
|
|
17
17
|
} else if (ignorePath instanceof RegExp && ignorePath.test(path)) {
|
|
18
|
-
return
|
|
18
|
+
return;
|
|
19
19
|
}
|
|
20
20
|
}
|
|
21
21
|
}
|
|
@@ -54,13 +54,20 @@ function telemetryMiddleware(config) {
|
|
|
54
54
|
source: "server-middleware",
|
|
55
55
|
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
56
56
|
};
|
|
57
|
-
sendToTelemetry(payload, config).catch((err) => {
|
|
57
|
+
const sendPromise = sendToTelemetry(payload, config).catch((err) => {
|
|
58
58
|
if (config.debug) {
|
|
59
|
-
console.error(
|
|
59
|
+
console.error(
|
|
60
|
+
"[Telemetry Middleware] Failed to send to telemetry:",
|
|
61
|
+
err
|
|
62
|
+
);
|
|
60
63
|
}
|
|
61
64
|
});
|
|
65
|
+
if (typeof globalThis !== "undefined" && globalThis.waitUntil) {
|
|
66
|
+
globalThis.waitUntil(sendPromise);
|
|
67
|
+
} else {
|
|
68
|
+
await sendPromise;
|
|
69
|
+
}
|
|
62
70
|
}
|
|
63
|
-
return null;
|
|
64
71
|
};
|
|
65
72
|
}
|
|
66
73
|
export {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@adwait12345/telemetry-next",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.2",
|
|
4
4
|
"description": "Next.js middleware for Telemetry SDK - track AI bots and crawlers",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"module": "./dist/index.mjs",
|
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
}
|
|
14
14
|
},
|
|
15
15
|
"dependencies": {
|
|
16
|
-
"@adwait12345/telemetry-core": "0.1.0"
|
|
16
|
+
"@adwait12345/telemetry-core": ">=0.1.0"
|
|
17
17
|
},
|
|
18
18
|
"devDependencies": {
|
|
19
19
|
"next": "^14.0.0",
|
package/src/index.ts
CHANGED
|
@@ -9,7 +9,7 @@ import {
|
|
|
9
9
|
} from "@adwait12345/telemetry-core";
|
|
10
10
|
|
|
11
11
|
export function telemetryMiddleware(config: TelemetryConfig) {
|
|
12
|
-
return function (req: NextRequest) {
|
|
12
|
+
return async function (req: NextRequest): Promise<void> {
|
|
13
13
|
const userAgent = req.headers.get("user-agent") || "";
|
|
14
14
|
const ip = req.ip || req.headers.get("x-forwarded-for") || "";
|
|
15
15
|
const url = new URL(req.url);
|
|
@@ -18,9 +18,9 @@ export function telemetryMiddleware(config: TelemetryConfig) {
|
|
|
18
18
|
if (config.ignorePaths) {
|
|
19
19
|
for (const ignorePath of config.ignorePaths) {
|
|
20
20
|
if (typeof ignorePath === "string" && path === ignorePath) {
|
|
21
|
-
return
|
|
21
|
+
return;
|
|
22
22
|
} else if (ignorePath instanceof RegExp && ignorePath.test(path)) {
|
|
23
|
-
return
|
|
23
|
+
return;
|
|
24
24
|
}
|
|
25
25
|
}
|
|
26
26
|
}
|
|
@@ -67,10 +67,8 @@ export function telemetryMiddleware(config: TelemetryConfig) {
|
|
|
67
67
|
timestamp: new Date().toISOString(),
|
|
68
68
|
};
|
|
69
69
|
|
|
70
|
-
//
|
|
71
|
-
|
|
72
|
-
// but in Next.js middleware you often need e.waitUntil
|
|
73
|
-
sendToTelemetry(payload, config).catch((err: any) => {
|
|
70
|
+
// Use waitUntil if available (Vercel edge runtime), otherwise fire-and-forget
|
|
71
|
+
const sendPromise = sendToTelemetry(payload, config).catch((err: any) => {
|
|
74
72
|
if (config.debug) {
|
|
75
73
|
console.error(
|
|
76
74
|
"[Telemetry Middleware] Failed to send to telemetry:",
|
|
@@ -78,8 +76,14 @@ export function telemetryMiddleware(config: TelemetryConfig) {
|
|
|
78
76
|
);
|
|
79
77
|
}
|
|
80
78
|
});
|
|
81
|
-
}
|
|
82
79
|
|
|
83
|
-
|
|
80
|
+
// @ts-ignore — waitUntil is available on Vercel/Cloudflare edge runtimes
|
|
81
|
+
if (typeof globalThis !== "undefined" && (globalThis as any).waitUntil) {
|
|
82
|
+
// @ts-ignore
|
|
83
|
+
(globalThis as any).waitUntil(sendPromise);
|
|
84
|
+
} else {
|
|
85
|
+
await sendPromise;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
84
88
|
};
|
|
85
89
|
}
|