Skip to content

Commit

Permalink
fix: convert CIDv0 to CIDv1 subdomains (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
SgtPooki authored Oct 26, 2023
1 parent 372611b commit bce0558
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/heliaServer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { type FastifyReply, type FastifyRequest } from 'fastify'
import { CID } from 'multiformats'
import { DEFAULT_MIME_TYPE, parseContentType } from './contentType.js'
import { getCustomHelia } from './getCustomHelia.js'
import { HeliaFetch } from './heliaFetch.js'
Expand All @@ -21,6 +22,7 @@ export class HeliaServer {
private heliaFetch!: HeliaFetch
private heliaVersionInfo!: { Version: string, Commit: string }
private readonly HOST_PART_REGEX = /^(?<address>.+)\.(?<namespace>ip[fn]s)\..+$/
private readonly HAS_UPPERCASE_REGEX = /[A-Z]/
private readonly log: debug.Debugger
public isReady: Promise<void>
public routes: RouteEntry[]
Expand Down Expand Up @@ -73,7 +75,11 @@ export class HeliaServer {
*/
private async redirectToSubdomainGW ({ request, reply }: RouteHandler): Promise<void> {
const { namespace, address, relativePath } = this.heliaFetch.parsePath(request.url)
const finalUrl = `//${address}.${namespace}.${request.hostname}${relativePath}`
let cidv1Address: string | null = null
if (this.HAS_UPPERCASE_REGEX.test(address)) {
cidv1Address = CID.parse(address).toV1().toString()
}
const finalUrl = `//${cidv1Address ?? address}.${namespace}.${request.hostname}${relativePath}`
this.log('Redirecting to final URL:', finalUrl)
await reply.redirect(finalUrl)
}
Expand Down

0 comments on commit bce0558

Please sign in to comment.