Skip to content

Commit d37ccda

Browse files
committed
feat: add KV_NAMESPACE_ID support for Cloudflare Workers and update deployment scripts
1 parent 386c0a2 commit d37ccda

5 files changed

Lines changed: 32 additions & 8 deletions

File tree

.github/workflows/build.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,9 @@ jobs:
110110
env:
111111
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
112112
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
113+
KV_NAMESPACE_ID: ${{ secrets.KV_NAMESPACE_ID }}
113114
run: |
115+
sed -i "s/\"KV_NAMESPACE_ID\"/\"${KV_NAMESPACE_ID}\"/" ../wrangler.jsonc
114116
npm ci
117+
npm run upload
115118
npm run deploy

.github/workflows/deploy-worker.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ jobs:
1919
env:
2020
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
2121
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
22+
KV_NAMESPACE_ID: ${{ secrets.KV_NAMESPACE_ID }}
2223
HAS_CF: ${{ secrets.CLOUDFLARE_API_TOKEN != '' }}
2324
steps:
2425
- uses: actions/checkout@v4
@@ -41,5 +42,7 @@ jobs:
4142
if: env.HAS_CF == 'true'
4243
working-directory: worker
4344
run: |
45+
sed -i "s/\"KV_NAMESPACE_ID\"/\"${KV_NAMESPACE_ID}\"/" ../wrangler.jsonc
4446
npm ci
47+
npm run upload
4548
npm run deploy

worker/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"lookup": "tsx scripts/lookup.ts",
99
"typecheck": "tsc --noEmit",
1010
"format": "prettier --write .",
11+
"upload": "wrangler kv key put --config ../wrangler.jsonc --binding INTEL --remote intel.bin --path ../intel.bin",
1112
"deploy": "wrangler deploy --config ../wrangler.jsonc",
1213
"dev": "wrangler dev --config ../wrangler.jsonc"
1314
},

worker/src/worker.ts

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,40 @@
1-
import bin from "../../intel.bin"
21
import { IntelDb } from "./lib/intel"
32

4-
const db = new IntelDb(bin as ArrayBuffer)
5-
db.lookup("8.8.8.8") // warm v4 path
6-
db.lookup("2606:4700:4700::1111") // warm v6 path
3+
type Env = { INTEL: KVNamespace; INTEL_KEY?: string }
4+
5+
let db: IntelDb | null = null
6+
let loading: Promise<IntelDb> | null = null
7+
8+
async function getDb(env: Env): Promise<IntelDb> {
9+
if (db) return db
10+
if (!loading) loading = load(env)
11+
return loading
12+
}
13+
14+
async function load(env: Env): Promise<IntelDb> {
15+
const buffer = await env.INTEL.get(env.INTEL_KEY || "intel.bin", "arrayBuffer")
16+
if (!buffer) throw new Error("intel.bin missing from KV")
17+
const loaded = new IntelDb(buffer)
18+
loaded.lookup("8.8.8.8") // warm v4 path
19+
loaded.lookup("2606:4700:4700::1111") // warm v6 path
20+
db = loaded
21+
return loaded
22+
}
723

824
export default {
9-
async fetch(request: Request, _env: unknown, ctx: ExecutionContext): Promise<Response> {
25+
async fetch(request: Request, env: Env, ctx: ExecutionContext): Promise<Response> {
1026
const url = new URL(request.url)
1127
const queryIp = url.searchParams.get("ip")
1228
const ip = queryIp || request.headers.get("CF-Connecting-IP") || ""
1329
if (!ip) return json({ error: "no ip" }, 400, "no-store")
1430

15-
if (!queryIp) return json(db.lookup(ip), 200, "private, max-age=3600")
31+
const intel = await getDb(env)
32+
if (!queryIp) return json(intel.lookup(ip), 200, "private, max-age=3600")
1633

1734
const cached = await caches.default.match(request)
1835
if (cached) return cached
1936

20-
const response = json(db.lookup(ip), 200, "public, max-age=3600")
37+
const response = json(intel.lookup(ip), 200, "public, max-age=3600")
2138
ctx.waitUntil(caches.default.put(request, response.clone()))
2239
return response
2340
},

wrangler.jsonc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"name": "ipintel",
44
"main": "worker/src/worker.ts",
55
"compatibility_date": "2026-06-06",
6-
"rules": [{ "type": "Data", "globs": ["**/*.bin"], "fallthrough": true }],
6+
"kv_namespaces": [{ "binding": "INTEL", "id": "KV_NAMESPACE_ID" }],
77
"observability": { "enabled": true },
88
"workers_dev": false,
99
"preview_urls": false

0 commit comments

Comments
 (0)