-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathworker.js
More file actions
39 lines (31 loc) · 988 Bytes
/
worker.js
File metadata and controls
39 lines (31 loc) · 988 Bytes
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
import { getRepo } from './lib/get-repo-worker.js'
import { getHomePage } from './lib/home-worker.js'
export default {
async fetch(request, env, ctx) {
const url = new URL(request.url)
// Handle favicon
if (url.pathname === '/favicon.ico') {
return new Response(null, { status: 404 })
}
// Handle home page
if (url.pathname === '/') {
return getHomePage()
}
// Handle package redirects
const packageName = url.pathname.replace(/^\//, '')
if (packageName) {
const repoUrl = await getRepo(packageName)
// Log request data in production
if (env.NODE_ENV === 'production') {
console.log(`query: ${JSON.stringify({
name: packageName,
domain: url.hostname,
headers: Object.fromEntries(request.headers),
date: new Date().toISOString()
})}`)
}
return Response.redirect(repoUrl, 302)
}
return new Response('Not Found', { status: 404 })
}
}