Skip to content

Commit 174074f

Browse files
committed
Stop messing with manual parsing of paths in the downloads handler, when hono can do all of this and more for us. Align with the other workers and use that framework.
1 parent 23283dd commit 174074f

File tree

7 files changed

+76
-57
lines changed

7 files changed

+76
-57
lines changed

tools/bug-search/package-lock.json

Lines changed: 15 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tools/bug-search/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
"devDependencies": {
1111
"@cloudflare/workers-types": "^4.20251014.0",
1212
"typescript": "^5.9.3",
13-
"wrangler": "^4.44.0"
13+
"wrangler": "^4.45.0"
1414
},
1515
"dependencies": {
16-
"hono": "^4.10.2"
16+
"hono": "^4.10.3"
1717
}
1818
}

tools/bug-search/wrangler.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ name = "lms-bug-search"
22
main = "src/index.ts"
33
compatibility_date = "2024-04-05"
44
compatibility_flags = ["nodejs_compat"]
5+
workers_dev = true
6+
preview_urls = false
57

68
[observability]
79
enabled = true

tools/downloads-handler/package-lock.json

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tools/downloads-handler/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,8 @@
1111
"@cloudflare/workers-types": "^4.20251014.0",
1212
"typescript": "^5.9.3",
1313
"wrangler": "^4.45.0"
14+
},
15+
"dependencies": {
16+
"hono": "^4.10.3"
1417
}
1518
}
Lines changed: 39 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
// see https://developers.cloudflare.com/r2/examples/demo-worker/
1+
import { Hono, Context } from 'hono'
2+
3+
const urlPrefix = 'downloads/listing/archive';
24

35
interface Env {
46
DOWNLOADS_BUCKET: R2Bucket
57
}
68

7-
const allowedPrefixes = [
9+
const allowedPrefixes: Array<string> = [
810
'LyrionMusicServer_v.*',
911
'LogitechMediaServer_v.*',
1012
'SqueezeboxServer_v.*',
@@ -15,52 +17,51 @@ const allowedPrefixes = [
1517
'docs/*',
1618
]
1719

18-
const corsHeaders = {
20+
const corsHeaders: Record<string, string> = {
1921
"Access-Control-Allow-Origin": "*",
2022
"Access-Control-Allow-Methods": "GET, OPTIONS, HEAD",
2123
"Access-Control-Max-Age": "86400",
2224
}
2325

24-
const urlPrefix = 'downloads/listing/archive/';
25-
26-
export default {
27-
async fetch(request, env): Promise<Response> {
28-
const url = new URL(request.url)
29-
const prefix = url.pathname.slice(1).replace(urlPrefix, '')
26+
const app = new Hono()
3027

31-
if (!prefix) {
32-
return Response.redirect('https://lyrion.org/getting-started')
33-
}
28+
app.on('GET', [`/${urlPrefix}/:prefix`, '/:prefix'], async (c: Context, ) => {
29+
const prefix = c.req.param('prefix');
3430

35-
if (request.method === 'GET' && prefix && allowedPrefixes.some(p => new RegExp(p).test(prefix))) {
36-
const listing = await env.DOWNLOADS_BUCKET.list({ prefix })
31+
if (!prefix) {
32+
return Response.redirect('https://lyrion.org/getting-started')
33+
}
3734

38-
// we only want object with the given prefix, but no sub-folders
39-
const filterRe = new RegExp(prefix + (prefix.match(new RegExp("/$")) ? "" : "/") + "[^\/]+$")
35+
if (prefix && allowedPrefixes.some(p => new RegExp(p).test(prefix))) {
36+
const listing = await c.env.DOWNLOADS_BUCKET.list({ prefix })
4037

41-
const body = listing.objects
42-
.filter(i => filterRe.test(i.key))
43-
.map(i => ({
44-
key: i.key,
45-
size: i.size,
46-
}))
38+
// we only want objects with the given prefix, but no sub-folders
39+
const filterRe = new RegExp(prefix + (prefix.match(new RegExp("/$")) ? "" : "/") + "[^\/]+$")
4740

48-
return new Response(JSON.stringify(body), {
49-
headers: {
50-
...corsHeaders,
51-
'Access-Control-Allow-Headers': request.headers.get(
52-
"Access-Control-Request-Headers",
53-
) || '',
54-
'content-type': 'application/json; charset=UTF-8',
55-
}
56-
})
57-
}
41+
const body = listing.objects
42+
.filter((i: any) => filterRe.test(i.key))
43+
.map((i: any) => ({
44+
key: i.key,
45+
size: i.size,
46+
}))
5847

59-
// we don't want to reveal a path doesn't exist - return valid, but empty result set
60-
return new Response('[]', {
61-
headers: {
62-
'content-type': 'application/json; charset=UTF-8',
63-
}
48+
Object.keys(corsHeaders).forEach(key => {
49+
c.header(key, corsHeaders[key]);
6450
})
51+
52+
c.header('Access-Control-Allow-Headers', c.req.header(
53+
"Access-Control-Request-Headers",
54+
) || '')
55+
56+
return c.json(body)
6557
}
66-
} satisfies ExportedHandler<Env>
58+
59+
// we don't want to reveal a path doesn't exist - return valid, but empty result set
60+
return new Response('[]', {
61+
headers: {
62+
'content-type': 'application/json; charset=UTF-8',
63+
}
64+
})
65+
})
66+
67+
export default app

tools/downloads-handler/wrangler.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name = "lms-downloads-handler"
22
main = "src/index.ts"
33
compatibility_date = "2024-04-05"
44
compatibility_flags = ["nodejs_compat"]
5-
workers_dev = true
5+
workers_dev = false
66
preview_urls = false
77

88
[observability]
@@ -21,4 +21,5 @@ zone_name="lyrion.org"
2121
binding = 'DOWNLOADS_BUCKET'
2222
bucket_name = 'downloads'
2323
preview_bucket_name = 'downloads'
24-
jurisdiction = "eu"
24+
jurisdiction = "eu"
25+
remote = true

0 commit comments

Comments
 (0)