11import { type FastifyReply , type FastifyRequest } from 'fastify'
2+ import { CID } from 'multiformats'
23import { DEFAULT_MIME_TYPE , parseContentType } from './contentType.js'
34import { getCustomHelia } from './getCustomHelia.js'
45import { 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 > i p [ f n ] 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