Skip to content

Commit bce0558

Browse files
authored
fix: convert CIDv0 to CIDv1 subdomains (#38)
1 parent 372611b commit bce0558

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

src/heliaServer.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { type FastifyReply, type FastifyRequest } from 'fastify'
2+
import { CID } from 'multiformats'
23
import { DEFAULT_MIME_TYPE, parseContentType } from './contentType.js'
34
import { getCustomHelia } from './getCustomHelia.js'
45
import { HeliaFetch } from './heliaFetch.js'
@@ -21,6 +22,7 @@ export class HeliaServer {
2122
private heliaFetch!: HeliaFetch
2223
private heliaVersionInfo!: { Version: string, Commit: string }
2324
private readonly HOST_PART_REGEX = /^(?<address>.+)\.(?<namespace>ip[fn]s)\..+$/
25+
private readonly HAS_UPPERCASE_REGEX = /[A-Z]/
2426
private readonly log: debug.Debugger
2527
public isReady: Promise<void>
2628
public routes: RouteEntry[]
@@ -73,7 +75,11 @@ export class HeliaServer {
7375
*/
7476
private async redirectToSubdomainGW ({ request, reply }: RouteHandler): Promise<void> {
7577
const { namespace, address, relativePath } = this.heliaFetch.parsePath(request.url)
76-
const finalUrl = `//${address}.${namespace}.${request.hostname}${relativePath}`
78+
let cidv1Address: string | null = null
79+
if (this.HAS_UPPERCASE_REGEX.test(address)) {
80+
cidv1Address = CID.parse(address).toV1().toString()
81+
}
82+
const finalUrl = `//${cidv1Address ?? address}.${namespace}.${request.hostname}${relativePath}`
7783
this.log('Redirecting to final URL:', finalUrl)
7884
await reply.redirect(finalUrl)
7985
}

0 commit comments

Comments
 (0)