@parrotrank/edge 0.0.0-stage → 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/README.md +45 -2
- package/dist/edge.test.d.ts +2 -0
- package/dist/edge.test.d.ts.map +1 -0
- package/dist/edge.test.js +80 -0
- package/dist/edge.test.js.map +1 -0
- package/dist/index.d.ts +36 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +137 -0
- package/dist/index.js.map +1 -0
- package/package.json +47 -4
package/README.md
CHANGED
|
@@ -1,3 +1,46 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @parrotrank/edge
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Zero-latency Edge middleware for **ParrotRank** — proxy your hosted dynamic `/llms.txt` & `/llms-full.txt` and asynchronously track AI crawler traffic (`GPTBot`, `OAI-SearchBot`, `ChatGPT-User`, `PerplexityBot`, `ClaudeBot`, `Claude-Web`, `Google-Extended`, `Applebot-Extended`) in Next.js or Cloudflare Workers.
|
|
4
|
+
|
|
5
|
+
- Website: https://parrotrank.com
|
|
6
|
+
- Documentation: https://parrotrank.com/docs/edge
|
|
7
|
+
- Source Code: https://github.com/coduy96/parrotrank
|
|
8
|
+
|
|
9
|
+
## Install
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
npm install @parrotrank/edge
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Next.js Middleware (`middleware.ts` / `proxy.ts`)
|
|
16
|
+
|
|
17
|
+
```ts
|
|
18
|
+
import { createParrotRankNextMiddleware } from "@parrotrank/edge";
|
|
19
|
+
|
|
20
|
+
export const middleware = createParrotRankNextMiddleware({
|
|
21
|
+
projectSlug: "your-project-slug",
|
|
22
|
+
ingestToken: process.env.PARROTRANK_EDGE_TOKEN!,
|
|
23
|
+
// Optional for self-hosted deployments (defaults to https://parrotrank.com):
|
|
24
|
+
// apiBaseUrl: "https://your-self-hosted-parrotrank.example.com",
|
|
25
|
+
});
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## Cloudflare Worker
|
|
29
|
+
|
|
30
|
+
```ts
|
|
31
|
+
import { withParrotRankCloudflareWorker } from "@parrotrank/edge";
|
|
32
|
+
|
|
33
|
+
export default withParrotRankCloudflareWorker(
|
|
34
|
+
{
|
|
35
|
+
projectSlug: "your-project-slug",
|
|
36
|
+
ingestToken: "pr_edge_...",
|
|
37
|
+
},
|
|
38
|
+
(request) => fetch(request)
|
|
39
|
+
);
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
## Features
|
|
43
|
+
|
|
44
|
+
- **Fail-open `/llms.txt` & `/llms-full.txt` proxy** with `Cache-Control: public, s-maxage=3600, stale-while-revalidate=86400` and 5s upstream timeout.
|
|
45
|
+
- **0ms added latency for AI bot logging** using `ctx.waitUntil(...)` non-blocking beacons to `/ingest/bot`.
|
|
46
|
+
- **Sanitized User-Agent parsing** with zero external runtime dependencies.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"edge.test.d.ts","sourceRoot":"","sources":["../src/edge.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import test from "node:test";
|
|
2
|
+
import assert from "node:assert/strict";
|
|
3
|
+
import { createParrotRankNextMiddleware, DEFAULT_API_BASE_URL, detectAiCrawler, sanitizeUserAgent, withParrotRankCloudflareWorker, } from "./index.js";
|
|
4
|
+
test("detectAiCrawler identifies LLM bots accurately", () => {
|
|
5
|
+
assert.equal(detectAiCrawler("Mozilla/5.0 (compatible; GPTBot/1.2)"), "GPTBot");
|
|
6
|
+
assert.equal(detectAiCrawler("ClaudeBot/1.0"), "ClaudeBot");
|
|
7
|
+
assert.equal(detectAiCrawler("Mozilla/5.0 Safari/605.1.15"), null);
|
|
8
|
+
});
|
|
9
|
+
test("sanitizeUserAgent strips CR/LF/control chars and caps length at 256", () => {
|
|
10
|
+
assert.equal(sanitizeUserAgent("PerplexityBot/1.0\r\nX-Injected: evil\nbar"), "PerplexityBot/1.0 X-Injected: evil bar");
|
|
11
|
+
const longUa = "A".repeat(400);
|
|
12
|
+
assert.equal(sanitizeUserAgent(longUa).length, 256);
|
|
13
|
+
assert.equal(DEFAULT_API_BASE_URL, "https://parrotrank.com");
|
|
14
|
+
});
|
|
15
|
+
test("Next.js middleware proxies /llms.txt with nosniff, timeout signal, and sanitized UA", async () => {
|
|
16
|
+
const calls = [];
|
|
17
|
+
const fakeFetch = async (input, init) => {
|
|
18
|
+
calls.push({
|
|
19
|
+
url: String(input),
|
|
20
|
+
headers: init?.headers,
|
|
21
|
+
hasSignal: Boolean(init?.signal),
|
|
22
|
+
});
|
|
23
|
+
return new Response("# Demo Brand\n", { status: 200 });
|
|
24
|
+
};
|
|
25
|
+
const mw = createParrotRankNextMiddleware({
|
|
26
|
+
projectSlug: "acme-1234",
|
|
27
|
+
ingestToken: "pr_edge_test",
|
|
28
|
+
fetchImpl: fakeFetch,
|
|
29
|
+
});
|
|
30
|
+
const res = await mw(new Request("https://acme.io/llms.txt", {
|
|
31
|
+
headers: { "User-Agent": "PerplexityBot/1.0" },
|
|
32
|
+
}));
|
|
33
|
+
assert.ok(res);
|
|
34
|
+
assert.equal(res.status, 200);
|
|
35
|
+
assert.equal(res.headers.get("X-Content-Type-Options"), "nosniff");
|
|
36
|
+
assert.equal(await res.text(), "# Demo Brand\n");
|
|
37
|
+
assert.equal(calls[0]?.url, "https://parrotrank.com/h/acme-1234/llms.txt");
|
|
38
|
+
assert.equal(calls[0]?.hasSignal, true);
|
|
39
|
+
});
|
|
40
|
+
test("Next.js middleware falls through cleanly when /llms.txt upstream fails or times out", async () => {
|
|
41
|
+
const failingFetch = async () => {
|
|
42
|
+
throw new Error("upstream timeout");
|
|
43
|
+
};
|
|
44
|
+
let nextCalled = false;
|
|
45
|
+
const mw = createParrotRankNextMiddleware({
|
|
46
|
+
projectSlug: "acme-1234",
|
|
47
|
+
ingestToken: "pr_edge_test",
|
|
48
|
+
fetchImpl: failingFetch,
|
|
49
|
+
}, () => {
|
|
50
|
+
nextCalled = true;
|
|
51
|
+
return new Response("origin fallback", { status: 200 });
|
|
52
|
+
});
|
|
53
|
+
const res = await mw(new Request("https://acme.io/llms.txt"));
|
|
54
|
+
assert.equal(nextCalled, true);
|
|
55
|
+
assert.ok(res);
|
|
56
|
+
assert.equal(await res.text(), "origin fallback");
|
|
57
|
+
});
|
|
58
|
+
test("Cloudflare worker logs AI bots asynchronously via ctx.waitUntil without blocking origin", async () => {
|
|
59
|
+
const beacons = [];
|
|
60
|
+
const pending = [];
|
|
61
|
+
const fakeFetch = async (input, init) => {
|
|
62
|
+
beacons.push({ url: String(input), body: String(init?.body ?? "") });
|
|
63
|
+
return new Response("ok", { status: 202 });
|
|
64
|
+
};
|
|
65
|
+
const worker = withParrotRankCloudflareWorker({
|
|
66
|
+
projectSlug: "acme-1234",
|
|
67
|
+
ingestToken: "pr_edge_secret",
|
|
68
|
+
apiBaseUrl: "http://localhost:4000",
|
|
69
|
+
fetchImpl: fakeFetch,
|
|
70
|
+
}, () => new Response("origin-html", { status: 200 }));
|
|
71
|
+
const response = await worker.fetch(new Request("https://acme.io/pricing", {
|
|
72
|
+
headers: { "User-Agent": "Mozilla/5.0 (compatible; OAI-SearchBot/1.0)" },
|
|
73
|
+
}), {}, { waitUntil: (p) => pending.push(p) });
|
|
74
|
+
assert.equal(await response.text(), "origin-html");
|
|
75
|
+
await Promise.all(pending);
|
|
76
|
+
assert.equal(beacons.length, 1);
|
|
77
|
+
assert.equal(beacons[0]?.url, "http://localhost:4000/ingest/bot");
|
|
78
|
+
assert.match(beacons[0]?.body ?? "", /OAI-SearchBot/);
|
|
79
|
+
});
|
|
80
|
+
//# sourceMappingURL=edge.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"edge.test.js","sourceRoot":"","sources":["../src/edge.test.ts"],"names":[],"mappings":"AAAA,OAAO,IAAI,MAAM,WAAW,CAAC;AAC7B,OAAO,MAAM,MAAM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AE,EACF,EAAE,SAAS,EAAE,CAAC,CAAC,EAAE,EAAE,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CACtC,CAAC;IAEF,MAAM,CAAC,KAAK,CAAC,MAAM,QAAQ,CAAC,IAAI,EAAE,EAAE,aAAa,CAAC,CAAC;IACnD,MAAM,OAAO,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;IAC3B,MAAM,CAAC,KAAK,CAAC,OAAO,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC;IAChC,MAAM,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,GAAG,EAAE,kCAAkC,CAAC,CAAC;IAClE,MAAM,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,IAAI,IAAI,EAAE,EAAE,eAAe,CAAC,CAAC;AACxD,CAAC,CAAC,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
export declare const DEFAULT_API_BASE_URL = "https://parrotrank.com";
|
|
2
|
+
export declare const AI_BOT_SIGNATURES: readonly ["GPTBot", "OAI-SearchBot", "ChatGPT-User", "PerplexityBot", "ClaudeBot", "Claude-Web", "Google-Extended", "Applebot-Extended"];
|
|
3
|
+
export interface ParrotRankEdgeConfig {
|
|
4
|
+
projectSlug: string;
|
|
5
|
+
ingestToken: string;
|
|
6
|
+
apiBaseUrl?: string;
|
|
7
|
+
proxyLlmsTxt?: boolean;
|
|
8
|
+
trackAiBots?: boolean;
|
|
9
|
+
fetchImpl?: typeof fetch;
|
|
10
|
+
}
|
|
11
|
+
export interface WaitUntilContext {
|
|
12
|
+
waitUntil?: (promise: Promise<unknown>) => void;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Strips CR/LF and control characters from User-Agent headers and caps length at 256 chars.
|
|
16
|
+
*/
|
|
17
|
+
export declare function sanitizeUserAgent(raw: string): string;
|
|
18
|
+
export declare function detectAiCrawler(userAgent: string): string | null;
|
|
19
|
+
/**
|
|
20
|
+
* Core Fetch API handler shared by Next.js Middleware / proxy.ts and Cloudflare Workers.
|
|
21
|
+
* Returns a Response when intercepting /llms.txt or /llms-full.txt, or null to continue normally.
|
|
22
|
+
*/
|
|
23
|
+
export declare function handleParrotRankEdgeRequest(request: Request, config: ParrotRankEdgeConfig, ctx?: WaitUntilContext): Promise<Response | null>;
|
|
24
|
+
/**
|
|
25
|
+
* Drop-in Next.js Middleware / proxy.ts wrapper.
|
|
26
|
+
* Example:
|
|
27
|
+
* export const middleware = createParrotRankNextMiddleware({ projectSlug, ingestToken });
|
|
28
|
+
*/
|
|
29
|
+
export declare function createParrotRankNextMiddleware(config: ParrotRankEdgeConfig, nextHandler?: (request: Request, event?: WaitUntilContext) => Promise<Response> | Response): (request: Request, event?: WaitUntilContext) => Promise<Response | undefined>;
|
|
30
|
+
/**
|
|
31
|
+
* Drop-in Cloudflare Worker wrapper.
|
|
32
|
+
*/
|
|
33
|
+
export declare function withParrotRankCloudflareWorker<Env = unknown>(config: ParrotRankEdgeConfig, originFetch: (request: Request, env: Env, ctx: WaitUntilContext) => Promise<Response> | Response): {
|
|
34
|
+
fetch(request: Request, env: Env, ctx: WaitUntilContext): Promise<Response>;
|
|
35
|
+
};
|
|
36
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,oBAAoB,2BAA2B,CAAC;AAE7D,eAAO,MAAM,iBAAiB,0IASpB,CAAC;AAEX,MAAM,WAAW,oBAAoB;IACnC,WAAW,EAAE,MAAM,CAAC;IACpB,WAAW,EAAE,MAAM,CAAC;IACpB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,YAAY,CAAC,EAAE,OAAO,CAAC;IACvB,WAAW,CAAC,EAAE,OAAO,CAAC;IACtB,SAAS,CAAC,EAAE,OAAO,KAAK,CAAC;CAC1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
6
|
+
"PerplexityBot",
|
|
7
|
+
"ClaudeBot",
|
|
8
|
+
"Claude-Web",
|
|
9
|
+
"Google-Extended",
|
|
10
|
+
"Applebot-Extended",
|
|
11
|
+
];
|
|
12
|
+
/**
|
|
13
|
+
* Strips CR/LF and control characters from User-Agent headers and caps length at 256 chars.
|
|
14
|
+
*/
|
|
15
|
+
export function sanitizeUserAgent(raw) {
|
|
16
|
+
return raw
|
|
17
|
+
.replace(/[\r\n\u0000-\u001f\u007f]+/g, " ")
|
|
18
|
+
.trim()
|
|
19
|
+
.slice(0, 256);
|
|
20
|
+
}
|
|
21
|
+
function createTimeoutSignal(ms) {
|
|
22
|
+
if (typeof AbortSignal !== "undefined" &&
|
|
23
|
+
typeof AbortSignal.timeout === "function") {
|
|
24
|
+
return AbortSignal.timeout(ms);
|
|
25
|
+
}
|
|
26
|
+
return undefined;
|
|
27
|
+
}
|
|
28
|
+
export function detectAiCrawler(userAgent) {
|
|
29
|
+
const lower = userAgent.toLowerCase();
|
|
30
|
+
for (const bot of AI_BOT_SIGNATURES) {
|
|
31
|
+
if (lower.includes(bot.toLowerCase())) {
|
|
32
|
+
return bot;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
return null;
|
|
36
|
+
}
|
|
37
|
+
function sendBotBeacon(config, pathname, botName, userAgent, statusCode = 200) {
|
|
38
|
+
const apiBase = (config.apiBaseUrl ?? DEFAULT_API_BASE_URL).replace(/\/+$/, "");
|
|
39
|
+
const fetcher = config.fetchImpl ?? fetch;
|
|
40
|
+
return fetcher(`${apiBase}/ingest/bot`, {
|
|
41
|
+
method: "POST",
|
|
42
|
+
signal: createTimeoutSignal(5_000),
|
|
43
|
+
headers: {
|
|
44
|
+
"Content-Type": "application/json",
|
|
45
|
+
"X-ParrotRank-Token": config.ingestToken,
|
|
46
|
+
},
|
|
47
|
+
body: JSON.stringify({
|
|
48
|
+
projectSlug: config.projectSlug,
|
|
49
|
+
eventType: "bot_crawl",
|
|
50
|
+
botOrSource: botName,
|
|
51
|
+
path: pathname,
|
|
52
|
+
statusCode,
|
|
53
|
+
userAgent: sanitizeUserAgent(userAgent),
|
|
54
|
+
}),
|
|
55
|
+
})
|
|
56
|
+
.then(() => undefined)
|
|
57
|
+
.catch(() => undefined);
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Core Fetch API handler shared by Next.js Middleware / proxy.ts and Cloudflare Workers.
|
|
61
|
+
* Returns a Response when intercepting /llms.txt or /llms-full.txt, or null to continue normally.
|
|
62
|
+
*/
|
|
63
|
+
export async function handleParrotRankEdgeRequest(request, config, ctx) {
|
|
64
|
+
const url = new URL(request.url);
|
|
65
|
+
const pathname = url.pathname;
|
|
66
|
+
const userAgent = sanitizeUserAgent(request.headers.get("user-agent") ?? "");
|
|
67
|
+
const proxyLlms = config.proxyLlmsTxt ?? true;
|
|
68
|
+
const trackBots = config.trackAiBots ?? true;
|
|
69
|
+
const apiBase = (config.apiBaseUrl ?? DEFAULT_API_BASE_URL).replace(/\/+$/, "");
|
|
70
|
+
const fetcher = config.fetchImpl ?? fetch;
|
|
71
|
+
if (proxyLlms && (pathname === "/llms.txt" || pathname === "/llms-full.txt")) {
|
|
72
|
+
try {
|
|
73
|
+
const upstream = await fetcher(`${apiBase}/h/${encodeURIComponent(config.projectSlug)}${pathname}`, {
|
|
74
|
+
signal: createTimeoutSignal(5_000),
|
|
75
|
+
headers: {
|
|
76
|
+
"User-Agent": userAgent,
|
|
77
|
+
},
|
|
78
|
+
});
|
|
79
|
+
const text = await upstream.text();
|
|
80
|
+
return new Response(text, {
|
|
81
|
+
status: upstream.status,
|
|
82
|
+
headers: {
|
|
83
|
+
"Content-Type": "text/plain; charset=utf-8",
|
|
84
|
+
"X-Content-Type-Options": "nosniff",
|
|
85
|
+
"Cache-Control": "public, s-maxage=3600, stale-while-revalidate=86400",
|
|
86
|
+
"X-Powered-By": "ParrotRank-Edge",
|
|
87
|
+
},
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
catch {
|
|
91
|
+
// Fail open to origin/next handler on upstream timeout or network error
|
|
92
|
+
return null;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
if (trackBots) {
|
|
96
|
+
const bot = detectAiCrawler(userAgent);
|
|
97
|
+
if (bot) {
|
|
98
|
+
const beacon = sendBotBeacon(config, pathname, bot, userAgent, 200);
|
|
99
|
+
if (ctx?.waitUntil) {
|
|
100
|
+
ctx.waitUntil(beacon);
|
|
101
|
+
}
|
|
102
|
+
else {
|
|
103
|
+
void beacon;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
return null;
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Drop-in Next.js Middleware / proxy.ts wrapper.
|
|
111
|
+
* Example:
|
|
112
|
+
* export const middleware = createParrotRankNextMiddleware({ projectSlug, ingestToken });
|
|
113
|
+
*/
|
|
114
|
+
export function createParrotRankNextMiddleware(config, nextHandler) {
|
|
115
|
+
return async function parrotRankMiddleware(request, event) {
|
|
116
|
+
const intercepted = await handleParrotRankEdgeRequest(request, config, event);
|
|
117
|
+
if (intercepted)
|
|
118
|
+
return intercepted;
|
|
119
|
+
if (nextHandler)
|
|
120
|
+
return nextHandler(request, event);
|
|
121
|
+
return undefined;
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Drop-in Cloudflare Worker wrapper.
|
|
126
|
+
*/
|
|
127
|
+
export function withParrotRankCloudflareWorker(config, originFetch) {
|
|
128
|
+
return {
|
|
129
|
+
async fetch(request, env, ctx) {
|
|
130
|
+
const intercepted = await handleParrotRankEdgeRequest(request, config, ctx);
|
|
131
|
+
if (intercepted)
|
|
132
|
+
return intercepted;
|
|
133
|
+
return originFetch(request, env, ctx);
|
|
134
|
+
},
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot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AAM,2BAA2B,CAAC,OAAO,EAAE,MAAM,EAAE,KAAK,CAAC,CAAC;QAC9E,IAAI,WAAW;YAAE,OAAO,WAAW,CAAC;QACpC,IAAI,WAAW;YAAE,OAAO,WAAW,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC;QACpD,OAAO,SAAS,CAAC;IACnB,CAAC,CAAC;AACJ,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,8BAA8B,CAC5C,MAA4B,EAC5B,WAIiC;IAEjC,OAAO;QACL,KAAK,CAAC,KAAK,CACT,OAAgB,EAChB,GAAQ,EACR,GAAqB;YAErB,MAAM,WAAW,GAAG,MAAM,2BAA2B,CAAC,OAAO,EAAE,MAAM,EAAE,GAAG,CAAC,CAAC;YAC5E,IAAI,WAAW;gBAAE,OAAO,WAAW,CAAC;YACpC,OAAO,WAAW,CAAC,OAAO,EAAE,GAAG,EAAE,GAAG,CAAC,CAAC;QACxC,CAAC;KACF,CAAC;AACJ,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,49 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@parrotrank/edge",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Zero-latency Edge middleware for hosted /llms.txt and real-time AI Bot Analytics (Next.js & Cloudflare Workers)",
|
|
5
|
+
"homepage": "https://parrotrank.com/docs/edge",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/coduy96/parrotrank.git",
|
|
9
|
+
"directory": "packages/edge"
|
|
10
|
+
},
|
|
11
|
+
"bugs": {
|
|
12
|
+
"url": "https://github.com/coduy96/parrotrank/issues"
|
|
13
|
+
},
|
|
14
|
+
"keywords": [
|
|
15
|
+
"parrotrank",
|
|
16
|
+
"llms-txt",
|
|
17
|
+
"aeo",
|
|
18
|
+
"geo",
|
|
19
|
+
"ai-bot-analytics",
|
|
20
|
+
"nextjs-middleware",
|
|
21
|
+
"cloudflare-workers"
|
|
22
|
+
],
|
|
23
|
+
"type": "module",
|
|
24
|
+
"license": "MIT",
|
|
25
|
+
"publishConfig": {
|
|
26
|
+
"access": "public"
|
|
27
|
+
},
|
|
28
|
+
"exports": {
|
|
29
|
+
".": {
|
|
30
|
+
"types": "./dist/index.d.ts",
|
|
31
|
+
"import": "./dist/index.js",
|
|
32
|
+
"default": "./dist/index.js"
|
|
33
|
+
}
|
|
34
|
+
},
|
|
35
|
+
"files": [
|
|
36
|
+
"dist",
|
|
37
|
+
"README.md"
|
|
38
|
+
],
|
|
39
|
+
"scripts": {
|
|
40
|
+
"build": "tsc -p tsconfig.json",
|
|
41
|
+
"typecheck": "tsc -p tsconfig.json --noEmit",
|
|
42
|
+
"test": "node --import tsx --test src/*.test.ts"
|
|
43
|
+
},
|
|
44
|
+
"devDependencies": {
|
|
45
|
+
"@types/node": "^22.15.0",
|
|
46
|
+
"tsx": "^4.23.15",
|
|
47
|
+
"typescript": "^5.8.3"
|
|
48
|
+
}
|
|
49
|
+
}
|