2027-track 0.1.7 → 0.1.9
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 +10 -0
- package/dist/chunk-GFWSQUMF.mjs +74 -0
- package/dist/cloudflare.mjs +1 -1
- package/dist/express.d.mts +15 -0
- package/dist/express.d.ts +15 -0
- package/dist/express.js +93 -0
- package/dist/express.mjs +26 -0
- package/dist/index.mjs +1 -1
- package/dist/next.js +1995 -1
- package/dist/next.mjs +1986 -3
- package/dist/vercel.mjs +1 -1
- package/package.json +9 -11
package/README.md
CHANGED
|
@@ -104,6 +104,16 @@ export default {
|
|
|
104
104
|
};
|
|
105
105
|
```
|
|
106
106
|
|
|
107
|
+
## Express / Node.js
|
|
108
|
+
|
|
109
|
+
```ts
|
|
110
|
+
import express from "express";
|
|
111
|
+
import { withAIAnalytics } from "2027-track/express";
|
|
112
|
+
|
|
113
|
+
const app = express();
|
|
114
|
+
app.use(withAIAnalytics());
|
|
115
|
+
```
|
|
116
|
+
|
|
107
117
|
## Generic Usage
|
|
108
118
|
|
|
109
119
|
```ts
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __commonJS = (cb, mod) => function __require() {
|
|
8
|
+
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
|
|
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 __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
19
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
20
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
21
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
22
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
23
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
24
|
+
mod
|
|
25
|
+
));
|
|
26
|
+
|
|
27
|
+
// src/index.ts
|
|
28
|
+
var DEFAULT_ENDPOINT = "https://ai-docs-analytics-api.theisease.workers.dev/track";
|
|
29
|
+
var TIMEOUT_MS = 2500;
|
|
30
|
+
function getEndpoint() {
|
|
31
|
+
const env = typeof process !== "undefined" ? process.env.AI_ANALYTICS_ENDPOINT : void 0;
|
|
32
|
+
if (env === "") return null;
|
|
33
|
+
return env || DEFAULT_ENDPOINT;
|
|
34
|
+
}
|
|
35
|
+
function isPageView(accept) {
|
|
36
|
+
const a = accept.toLowerCase();
|
|
37
|
+
return a.includes("text/html") || a.includes("text/markdown");
|
|
38
|
+
}
|
|
39
|
+
async function trackVisit(options) {
|
|
40
|
+
const endpoint = getEndpoint();
|
|
41
|
+
if (!endpoint) {
|
|
42
|
+
return { ok: true, skipped: "disabled" };
|
|
43
|
+
}
|
|
44
|
+
if (!isPageView(options.accept)) {
|
|
45
|
+
return { ok: true, skipped: "not-page-view" };
|
|
46
|
+
}
|
|
47
|
+
const controller = new AbortController();
|
|
48
|
+
const timeoutId = setTimeout(() => controller.abort(), TIMEOUT_MS);
|
|
49
|
+
try {
|
|
50
|
+
const response = await fetch(endpoint, {
|
|
51
|
+
method: "POST",
|
|
52
|
+
headers: { "Content-Type": "application/json" },
|
|
53
|
+
body: JSON.stringify({
|
|
54
|
+
host: options.host,
|
|
55
|
+
path: options.path,
|
|
56
|
+
user_agent: options.userAgent,
|
|
57
|
+
accept: options.accept,
|
|
58
|
+
country: options.country || "unknown"
|
|
59
|
+
}),
|
|
60
|
+
signal: controller.signal
|
|
61
|
+
});
|
|
62
|
+
clearTimeout(timeoutId);
|
|
63
|
+
return await response.json();
|
|
64
|
+
} catch (e) {
|
|
65
|
+
clearTimeout(timeoutId);
|
|
66
|
+
return { ok: false, error: e instanceof Error ? e.message : "unknown error" };
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export {
|
|
71
|
+
__commonJS,
|
|
72
|
+
__toESM,
|
|
73
|
+
trackVisit
|
|
74
|
+
};
|
package/dist/cloudflare.mjs
CHANGED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export { trackVisit } from './index.mjs';
|
|
2
|
+
|
|
3
|
+
interface ExpressRequest {
|
|
4
|
+
hostname?: string;
|
|
5
|
+
path: string;
|
|
6
|
+
headers: Record<string, string | string[] | undefined>;
|
|
7
|
+
get?: (name: string) => string | undefined;
|
|
8
|
+
}
|
|
9
|
+
interface ExpressResponse {
|
|
10
|
+
[key: string]: unknown;
|
|
11
|
+
}
|
|
12
|
+
type NextFunction = () => void;
|
|
13
|
+
declare function withAIAnalytics(): (req: ExpressRequest, _res: ExpressResponse, next: NextFunction) => void;
|
|
14
|
+
|
|
15
|
+
export { withAIAnalytics };
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export { trackVisit } from './index.js';
|
|
2
|
+
|
|
3
|
+
interface ExpressRequest {
|
|
4
|
+
hostname?: string;
|
|
5
|
+
path: string;
|
|
6
|
+
headers: Record<string, string | string[] | undefined>;
|
|
7
|
+
get?: (name: string) => string | undefined;
|
|
8
|
+
}
|
|
9
|
+
interface ExpressResponse {
|
|
10
|
+
[key: string]: unknown;
|
|
11
|
+
}
|
|
12
|
+
type NextFunction = () => void;
|
|
13
|
+
declare function withAIAnalytics(): (req: ExpressRequest, _res: ExpressResponse, next: NextFunction) => void;
|
|
14
|
+
|
|
15
|
+
export { withAIAnalytics };
|
package/dist/express.js
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
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/express.ts
|
|
21
|
+
var express_exports = {};
|
|
22
|
+
__export(express_exports, {
|
|
23
|
+
trackVisit: () => trackVisit,
|
|
24
|
+
withAIAnalytics: () => withAIAnalytics
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(express_exports);
|
|
27
|
+
|
|
28
|
+
// src/index.ts
|
|
29
|
+
var DEFAULT_ENDPOINT = "https://ai-docs-analytics-api.theisease.workers.dev/track";
|
|
30
|
+
var TIMEOUT_MS = 2500;
|
|
31
|
+
function getEndpoint() {
|
|
32
|
+
const env = typeof process !== "undefined" ? process.env.AI_ANALYTICS_ENDPOINT : void 0;
|
|
33
|
+
if (env === "") return null;
|
|
34
|
+
return env || DEFAULT_ENDPOINT;
|
|
35
|
+
}
|
|
36
|
+
function isPageView(accept) {
|
|
37
|
+
const a = accept.toLowerCase();
|
|
38
|
+
return a.includes("text/html") || a.includes("text/markdown");
|
|
39
|
+
}
|
|
40
|
+
async function trackVisit(options) {
|
|
41
|
+
const endpoint = getEndpoint();
|
|
42
|
+
if (!endpoint) {
|
|
43
|
+
return { ok: true, skipped: "disabled" };
|
|
44
|
+
}
|
|
45
|
+
if (!isPageView(options.accept)) {
|
|
46
|
+
return { ok: true, skipped: "not-page-view" };
|
|
47
|
+
}
|
|
48
|
+
const controller = new AbortController();
|
|
49
|
+
const timeoutId = setTimeout(() => controller.abort(), TIMEOUT_MS);
|
|
50
|
+
try {
|
|
51
|
+
const response = await fetch(endpoint, {
|
|
52
|
+
method: "POST",
|
|
53
|
+
headers: { "Content-Type": "application/json" },
|
|
54
|
+
body: JSON.stringify({
|
|
55
|
+
host: options.host,
|
|
56
|
+
path: options.path,
|
|
57
|
+
user_agent: options.userAgent,
|
|
58
|
+
accept: options.accept,
|
|
59
|
+
country: options.country || "unknown"
|
|
60
|
+
}),
|
|
61
|
+
signal: controller.signal
|
|
62
|
+
});
|
|
63
|
+
clearTimeout(timeoutId);
|
|
64
|
+
return await response.json();
|
|
65
|
+
} catch (e) {
|
|
66
|
+
clearTimeout(timeoutId);
|
|
67
|
+
return { ok: false, error: e instanceof Error ? e.message : "unknown error" };
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// src/express.ts
|
|
72
|
+
function getHeader(req, name) {
|
|
73
|
+
if (req.get) return req.get(name) || "";
|
|
74
|
+
const val = req.headers[name.toLowerCase()];
|
|
75
|
+
return Array.isArray(val) ? val[0] || "" : val || "";
|
|
76
|
+
}
|
|
77
|
+
function withAIAnalytics() {
|
|
78
|
+
return (req, _res, next) => {
|
|
79
|
+
trackVisit({
|
|
80
|
+
host: req.hostname || getHeader(req, "host"),
|
|
81
|
+
path: req.path,
|
|
82
|
+
userAgent: getHeader(req, "user-agent"),
|
|
83
|
+
accept: getHeader(req, "accept")
|
|
84
|
+
}).catch(() => {
|
|
85
|
+
});
|
|
86
|
+
next();
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
90
|
+
0 && (module.exports = {
|
|
91
|
+
trackVisit,
|
|
92
|
+
withAIAnalytics
|
|
93
|
+
});
|
package/dist/express.mjs
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import {
|
|
2
|
+
trackVisit
|
|
3
|
+
} from "./chunk-GFWSQUMF.mjs";
|
|
4
|
+
|
|
5
|
+
// src/express.ts
|
|
6
|
+
function getHeader(req, name) {
|
|
7
|
+
if (req.get) return req.get(name) || "";
|
|
8
|
+
const val = req.headers[name.toLowerCase()];
|
|
9
|
+
return Array.isArray(val) ? val[0] || "" : val || "";
|
|
10
|
+
}
|
|
11
|
+
function withAIAnalytics() {
|
|
12
|
+
return (req, _res, next) => {
|
|
13
|
+
trackVisit({
|
|
14
|
+
host: req.hostname || getHeader(req, "host"),
|
|
15
|
+
path: req.path,
|
|
16
|
+
userAgent: getHeader(req, "user-agent"),
|
|
17
|
+
accept: getHeader(req, "accept")
|
|
18
|
+
}).catch(() => {
|
|
19
|
+
});
|
|
20
|
+
next();
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
export {
|
|
24
|
+
trackVisit,
|
|
25
|
+
withAIAnalytics
|
|
26
|
+
};
|