Skip to content

Commit

Permalink
fix: handle bubbling errors
Browse files Browse the repository at this point in the history
  • Loading branch information
SgtPooki committed Nov 8, 2023
1 parent 383f5f5 commit ab4a0f3
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions src/heliaServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,18 +168,25 @@ export class HeliaServer {
this.log('Fetching from Helia:', { address, namespace, relativePath })
let type: string | undefined
// raw response is needed to respond with the correct content type.
for await (const chunk of await this.heliaFetch.fetch({ address, namespace, relativePath, signal })) {
if (type === undefined) {
type = await parseContentType({ bytes: chunk, path: relativePath })
// this needs to happen first.
reply.raw.writeHead(200, {
'Content-Type': type ?? DEFAULT_MIME_TYPE,
'Cache-Control': 'public, max-age=31536000, immutable'
})
try {
for await (const chunk of await this.heliaFetch.fetch({ address, namespace, relativePath, signal })) {
if (type === undefined) {
type = await parseContentType({ bytes: chunk, path: relativePath })
// this needs to happen first.
reply.raw.writeHead(200, {
'Content-Type': type ?? DEFAULT_MIME_TYPE,
'Cache-Control': 'public, max-age=31536000, immutable'
})
}
reply.raw.write(Buffer.from(chunk))
}
reply.raw.write(Buffer.from(chunk))
} catch (err) {
this.log('Error fetching from Helia:', err)
// TODO: If we failed here and we already wrote the headers, we need to handle that.
// await reply.code(500)
} finally {
reply.raw.end()
}
reply.raw.end()
}

/**
Expand Down

0 comments on commit ab4a0f3

Please sign in to comment.