MCPcopy Create free account
hub / github.com/anomalyco/models.dev / fetch

Function fetch

packages/function/src/worker.ts:9–143  ·  view source on GitHub ↗
(
    request: Request,
    env: Env,
    ctx: ExecutionContext,
  )

Source from the content-addressed store, hash-verified

7
8export default {
9 async fetch(
10 request: Request,
11 env: Env,
12 ctx: ExecutionContext,
13 ): Promise<Response> {
14 const url = new URL(request.url);
15 const ip = request.headers.get("cf-connecting-ip") ?? undefined;
16 const country = request.headers.get("cf-ipcountry") ?? undefined;
17 const agent = request.headers.get("user-agent") ?? undefined;
18 const time = new Date().toISOString();
19 if (agent?.includes("opencode") || agent?.includes("bun")) {
20 ctx.waitUntil(
21 fetch("https://us.i.posthog.com/i/v0/e/", {
22 method: "POST",
23 headers: {
24 "Content-Type": "application/json",
25 },
26 body: JSON.stringify({
27 api_key: JSON.parse(env.PosthogToken).value,
28 event: "hit",
29 distinct_id: ip ?? "unknown",
30 properties: {
31 $process_person_profile: false,
32 user_agent: agent ?? "unknown",
33 country: country ?? "unknown",
34 path: url.pathname,
35 },
36 }),
37 }),
38 );
39
40 ctx.waitUntil(
41 fetch(JSON.parse(env.LakeUrl).value, {
42 method: "POST",
43 headers: {
44 "Content-Type": "application/json",
45 Authorization: `Bearer ${JSON.parse(env.LakeSecret).value}`,
46 },
47 body: JSON.stringify({
48 events: [
49 {
50 _datalake_key: "inference.event",
51 event_timestamp: time,
52 event_date: time.slice(0, 10),
53 event_type: "models.hit",
54 ip: string(ip),
55 ip_prefix: string(ipPrefix(ip)),
56 user_agent: string(agent),
57 cf_country: string(country),
58 path: string(url.pathname),
59 },
60 ],
61 }),
62 }),
63 );
64 }
65
66 if (url.pathname === "/model-schema.json") {

Callers

nothing calls this directly

Calls 4

stringFunction · 0.85
ipPrefixFunction · 0.85
isHtmlRouteFunction · 0.85
htmlRouteAssetPathFunction · 0.85

Tested by

no test coverage detected