Skip to content

Commit

Permalink
fix(debug): other improvements.
Browse files Browse the repository at this point in the history
Signed-off-by: Nishant Arora <[email protected]>
  • Loading branch information
whizzzkid committed Oct 7, 2023
1 parent 0ef117a commit 6aa5c9a
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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" ]
9 changes: 9 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
version: "3.8"

services:
server:
build: .
expose:
- "8080:8080"
environment:
- DEBUG="helia-server*"
4 changes: 3 additions & 1 deletion src/heliaFetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ export class HeliaFetch {
public async fetch (path: string): Promise<AsyncIterable<Uint8Array>> {
try {
await this.ready
this.log('Fetching:', path)
const { namespace, address, relativePath } = this.parsePath(path)
this.log('Processing Fetch:', { namespace, address, relativePath })
switch (namespace) {
Expand Down Expand Up @@ -131,12 +132,13 @@ export class HeliaFetch {
* Fetch IPNS content.
*/
private async fetchIpns (address: string, options?: Parameters<UnixFS['cat']>[1]): Promise<AsyncIterable<Uint8Array>> {
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 ?? ''}`
Expand Down
21 changes: 14 additions & 7 deletions src/heliaServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,22 @@ export class HeliaServer {
* Handles redirecting to the relative path
*/
private async redirectRelative ({ request, response }: IRouteHandler): Promise<void> {
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)
}
}

Expand Down Expand Up @@ -127,6 +133,7 @@ export class HeliaServer {
async fetchIpns ({ request, response }: IRouteHandler): Promise<void> {
try {
await this.isReady
this.log('Requesting content from IPNS:', request.path)

const {
namespace: reqNamespace,
Expand Down

0 comments on commit 6aa5c9a

Please sign in to comment.