-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.ts
77 lines (63 loc) · 2.58 KB
/
middleware.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import { rewrite, next } from '@vercel/edge';
import { Options, RequestCookies, ResponseCookies } from '@edge-runtime/cookies';
import { AES, enc } from 'crypto-js'
export const config = {
matcher: [
'/'
]
}
const canaryLookupTarget = 'canary-lookup.namedpython.dev'
const defaultOptions: Options = { maxAge: 60 * 60 * 24 * 5, sameSite: 'lax', secure: false }
export async function middleware(request: Request) {
const url = new URL(request.url)
const cookies = new RequestCookies(request)
const path = url.pathname
const isCanaryCookieName = `x-is-canary-${path}`
const rewritePathCookieName = `x-rewrite-path-${path}`
const encryptKey = process.env.COOKIES_ENCRYPT_KEY || 'key'
console.log(encryptKey)
const { encrypt, decrypt } = AES
const { Utf8 } = enc
const rewritePath = decrypt(cookies.get(rewritePathCookieName) || '', encryptKey).toString(Utf8)
const isCanary = decrypt(cookies.get(isCanaryCookieName) || '', encryptKey).toString(Utf8)
if (rewritePath) {
console.log(`cookie for early rewrite found: ${rewritePath}, canary: ${isCanary}`)
return rewrite(new URL(rewritePath, url))
}
try {
console.time('DNS lookup')
const canaryLookupResult = await fetch(`https://cloudflare-dns.com/dns-query?name=${canaryLookupTarget}&type=TXT`, {
headers: {
'accept': 'application/dns-json'
}
})
console.timeEnd('DNS lookup')
const data = (await canaryLookupResult.json())?.['Answer']?.[0]?.['data'] as unknown as string
const rules = data.replace(/"/g, '').split(';').filter(v => v)
const matched = rules.find((r) => r.startsWith(path))
const def = matched?.split(' ')
if (!def || def.length !== 3) {
return next()
}
const [_target, canary, chance] = [def[0], def[1], parseFloat(def[2])]
const beCanary = routeToCanary(chance)
const res = beCanary ? rewrite(new URL(canary, url)) : next()
const resCookies = new ResponseCookies(res)
if (beCanary) {
console.log(`route to canary: ${canary}`)
} else {
console.log(`route to default: ${path}`)
}
// set cookies for early return
resCookies.set(isCanaryCookieName, encrypt(beCanary ? 'true' : 'false', encryptKey).toString(), defaultOptions)
resCookies.set(rewritePathCookieName, encrypt(beCanary ? canary : path, encryptKey).toString(), defaultOptions)
return res
} catch {
return next()
}
}
function routeToCanary(chance: number): boolean {
const randVal = crypto.getRandomValues(new Uint32Array(1))[0] / (0xffffffff + 1)
console.log(`chance: ${chance}, randVal: ${randVal}`)
return randVal <= chance
}