-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.js
More file actions
125 lines (117 loc) · 6.35 KB
/
Copy pathindex.js
File metadata and controls
125 lines (117 loc) · 6.35 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/**
* GoodWebTools edge Worker.
*
* Serves ML model files from an R2 bucket at /models/* (no 25 MB asset limit,
* same-origin so the privacy promise holds), and delegates every other request
* to the static Astro build via the ASSETS binding. Cloudflare only invokes
* this Worker for requests that don't match a prebuilt static asset, so static
* pages are still served directly.
*
* It also hosts a tiny WebRTC signaling rendezvous at /api/signal/<roomId> (a
* Durable Object), used by the P2P tools to exchange the ~2KB SDP/ICE handshake.
* Media and file bytes travel peer-to-peer and never reach this Worker.
*/
export { SignalRoom } from './signal-room.js';
export default {
async fetch(request, env) {
const url = new URL(request.url);
if (url.pathname.startsWith('/api/signal/')) {
if (request.headers.get('Upgrade') !== 'websocket') {
return new Response('Expected WebSocket', { status: 426 });
}
const roomId = url.pathname.slice('/api/signal/'.length);
if (!/^[a-z0-9]{6,32}$/.test(roomId)) {
return new Response('Bad room id', { status: 400 });
}
return env.SIGNAL.getByName(roomId).fetch(request);
}
// Same-origin proxy for Hugging Face model files. transformers.js fetches
// Whisper weights from huggingface.co, which 302-redirects large files to a
// CDN — the browser/Workbox can't reliably cache those redirected responses,
// so the model re-downloaded on every refresh. Proxying resolves the redirect
// server-side and returns the bytes same-origin with an immutable cache header,
// so the browser HTTP-caches them and they survive refreshes.
if (url.pathname.startsWith('/hf/')) {
if (request.method !== 'GET' && request.method !== 'HEAD') {
return new Response('Method not allowed', { status: 405 });
}
const upstream = 'https://huggingface.co/' + url.pathname.slice('/hf/'.length) + url.search;
const res = await fetch(upstream, {
headers: { 'user-agent': 'goodwebtools-proxy' },
cf: { cacheEverything: true, cacheTtl: 31536000 },
});
if (!res.ok && res.status !== 206) {
return new Response('Upstream model fetch failed', { status: res.status || 502 });
}
const headers = new Headers(res.headers);
headers.set('cache-control', 'public, max-age=31536000, immutable');
headers.set('access-control-allow-origin', '*');
headers.delete('set-cookie');
return new Response(res.body, { status: res.status, headers });
}
// Same-origin proxy for OpenFreeMap tile/style/glyph requests. The OFM CDN
// (Cloudflare Singapore edge) sometimes returns corrupt responses to users in
// Southeast Asia — both style JSON and vector PBF tiles. Proxying server-side
// through our Worker fetches from OFM's origin over CF's backbone, bypassing
// the broken edge node entirely. Tiles are versioned by timestamp in the URL
// so they're safe to cache long-term; style/TileJSON use a short TTL.
if (url.pathname.startsWith('/ofm/')) {
if (request.method !== 'GET' && request.method !== 'HEAD') {
return new Response('Method not allowed', { status: 405 });
}
const upstream = 'https://tiles.openfreemap.org/' + url.pathname.slice('/ofm/'.length) + url.search;
// Fetch WITHOUT cf.cacheEverything so we bypass Cloudflare's edge cache.
// The Singapore edge had corrupted cached responses for OFM PBF tile URLs
// (HTTP 200 with wrong body), which were being served by the edge cache
// when cacheEverything:true was set. Going directly to OFM's origin via
// CF backbone avoids those stale entries. The browser's own HTTP cache
// (driven by the cache-control header we set below) handles client-side
// caching without touching the broken edge entries.
const res = await fetch(upstream, {
headers: { 'user-agent': 'goodwebtools-ofm-proxy' },
});
if (!res.ok) {
return new Response('Upstream OFM fetch failed', { status: res.status || 502 });
}
const headers = new Headers(res.headers);
headers.set('access-control-allow-origin', '*');
headers.delete('set-cookie');
const isPbf = url.pathname.endsWith('.pbf');
headers.set('cache-control', isPbf ? 'public, max-age=2592000, immutable' : 'public, max-age=3600');
return new Response(res.body, { status: res.status, headers });
}
if (url.pathname.startsWith('/models/')) {
if (request.method !== 'GET' && request.method !== 'HEAD') {
return new Response('Method not allowed', { status: 405 });
}
const key = decodeURIComponent(url.pathname.slice('/models/'.length));
const object = await env.MODELS.get(key);
if (!object || !object.body) return new Response('Model not found', { status: 404 });
const headers = new Headers();
object.writeHttpMetadata(headers);
headers.set('etag', object.httpEtag);
// Models are immutable and versioned by filename — cache hard.
headers.set('cache-control', 'public, max-age=31536000, immutable');
return new Response(object.body, { headers });
}
// Geo language hint: send first-time Indonesian visitors to the Bahasa (/id/)
// version of localized public pages. Humans only — crawlers must see both trees,
// and a manual choice (gwt.lang cookie, set by the header switcher) always wins.
if (request.method === 'GET') {
const accept = request.headers.get('accept') || '';
const p = url.pathname;
const isLocalized = p === '/' || p.startsWith('/tools/') || p.startsWith('/category/') || p === '/about' || p.startsWith('/about/') || p === '/privacy' || p.startsWith('/privacy/');
const alreadyId = p === '/id' || p.startsWith('/id/');
const cookie = request.headers.get('cookie') || '';
const hasChoice = /(?:^|;\s*)gwt\.lang=/.test(cookie);
const ua = request.headers.get('user-agent') || '';
const isBot = /bot|crawl|spider|slurp|bingpreview|facebookexternalhit|embedly|preview|whatsapp|telegram|discord/i.test(ua);
const country = request.cf && request.cf.country;
if (accept.includes('text/html') && isLocalized && !alreadyId && !hasChoice && !isBot && country === 'ID') {
const to = '/id' + (p === '/' ? '' : p);
return Response.redirect(url.origin + to + url.search, 302);
}
}
return env.ASSETS.fetch(request);
},
};