diff --git a/Dockerfile b/Dockerfile index cbf4e79..aea35dd 100644 --- a/Dockerfile +++ b/Dockerfile @@ -6,7 +6,7 @@ RUN apt-get install -y build-essential cmake git libssl-dev WORKDIR /app COPY . . -RUN npm install +RUN npm ci --quiet RUN npm run build EXPOSE 8080 CMD [ "npm", "start" ] diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..499fab2 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,9 @@ +version: "3.8" + +services: + server: + build: . + expose: + - "8080:8080" + environment: + - DEBUG="helia-server*" diff --git a/src/heliaFetch.ts b/src/heliaFetch.ts index 52e35b4..758a5e7 100644 --- a/src/heliaFetch.ts +++ b/src/heliaFetch.ts @@ -93,6 +93,7 @@ export class HeliaFetch { public async fetch (path: string): Promise> { try { await this.ready + this.log('Fetching:', path) const { namespace, address, relativePath } = this.parsePath(path) this.log('Processing Fetch:', { namespace, address, relativePath }) switch (namespace) { @@ -131,12 +132,13 @@ export class HeliaFetch { * Fetch IPNS content. */ private async fetchIpns (address: string, options?: Parameters[1]): Promise> { - this.log('Fetching from Delegate Routing:', address) if (!this.ipnsResolutionCache.has(address)) { + this.log('Fetching from Delegate Routing:', address) const { Path } = await (await fetch(this.delegatedRoutingApi + address)).json() this.ipnsResolutionCache.set(address, Path ?? 'not-found') } if (this.ipnsResolutionCache.get(address) === 'not-found') { + this.log('No Path found:', address) throw new Error(`Could not resolve IPNS address: ${address}`) } const finalPath = `${this.ipnsResolutionCache.get(address)}${options?.path ?? ''}` diff --git a/src/heliaServer.ts b/src/heliaServer.ts index 68e34bb..cf3534b 100644 --- a/src/heliaServer.ts +++ b/src/heliaServer.ts @@ -67,16 +67,22 @@ export class HeliaServer { * Handles redirecting to the relative path */ private async redirectRelative ({ request, response }: IRouteHandler): Promise { + this.log('Redirecting to relative path:', request.path, request.headers) const referrerPath = new URL(request.headers.referer ?? '').pathname if (referrerPath !== undefined) { - this.log('Redirecting to relative path:', referrerPath) - let relativeRedirectPath = `${referrerPath}${request.path}` - const { namespace, address } = this.heliaFetch.parsePath(referrerPath) - if (namespace === 'ipns') { - relativeRedirectPath = `/${namespace}/${address}${request.path}` + try { + let relativeRedirectPath = `${referrerPath}${request.path}` + const { namespace, address } = this.heliaFetch.parsePath(referrerPath) + if (namespace === 'ipns') { + relativeRedirectPath = `/${namespace}/${address}${request.path}` + } + // absolute redirect + this.log('Redirecting to relative path:', referrerPath) + response.redirect(301, relativeRedirectPath) + } catch (error) { + this.log('Error redirecting to relative path:', error) + response.status(500).end() } - // absolute redirect - response.redirect(301, relativeRedirectPath) } } @@ -127,6 +133,7 @@ export class HeliaServer { async fetchIpns ({ request, response }: IRouteHandler): Promise { try { await this.isReady + this.log('Requesting content from IPNS:', request.path) const { namespace: reqNamespace,