diff --git a/packages/verified-fetch/src/utils/responses.ts b/packages/verified-fetch/src/utils/responses.ts index a8ce2336..667318c6 100644 --- a/packages/verified-fetch/src/utils/responses.ts +++ b/packages/verified-fetch/src/utils/responses.ts @@ -1,5 +1,6 @@ import type { ByteRangeContext } from './byte-range-context' import type { SupportedBodyTypes } from '../types.js' +import type { Logger } from '@libp2p/interface' function setField (response: Response, name: string, value: string | boolean): void { Object.defineProperty(response, name, { @@ -116,9 +117,10 @@ export function movedPermanentlyResponse (url: string, location: string, init?: interface RangeOptions { byteRangeContext: ByteRangeContext + log?: Logger } -export function okRangeResponse (url: string, body: SupportedBodyTypes, { byteRangeContext }: RangeOptions, init?: ResponseOptions): Response { +export function okRangeResponse (url: string, body: SupportedBodyTypes, { byteRangeContext, log }: RangeOptions, init?: ResponseOptions): Response { if (!byteRangeContext.isRangeRequest) { return okResponse(url, body, init) } @@ -138,10 +140,8 @@ export function okRangeResponse (url: string, body: SupportedBodyTypes, { byteRa 'content-range': byteRangeContext.contentRangeHeaderValue } }) - } catch (e) { - // TODO: should we return a different status code here? - // eslint-disable-next-line no-console - console.error('Invalid range request', e) + } catch (e: any) { + log?.error('failed to create range response', e) return badRangeResponse(url, body, init) } diff --git a/packages/verified-fetch/src/verified-fetch.ts b/packages/verified-fetch/src/verified-fetch.ts index 782c4616..1301dbe5 100644 --- a/packages/verified-fetch/src/verified-fetch.ts +++ b/packages/verified-fetch/src/verified-fetch.ts @@ -357,7 +357,7 @@ export class VerifiedFetch { }) byteRangeContext.setBody(stream) // if not a valid range request, okRangeRequest will call okResponse - const response = okRangeResponse(resource, byteRangeContext.getBody(), { byteRangeContext }, { + const response = okRangeResponse(resource, byteRangeContext.getBody(), { byteRangeContext, log: this.log }, { redirected }) @@ -381,13 +381,9 @@ export class VerifiedFetch { const byteRangeContext = new ByteRangeContext(this.helia.logger, options?.headers) const result = await this.helia.blockstore.get(cid, options) byteRangeContext.setBody(result) - const response = okRangeResponse(resource, byteRangeContext.getBody(), { byteRangeContext }, { + const response = okRangeResponse(resource, byteRangeContext.getBody(), { byteRangeContext, log: this.log }, { redirected: false }) - // if (byteRangeContext.isRangeRequest) { - // } else { - // response = okResponse(resource, result) - // } // if the user has specified an `Accept` header that corresponds to a raw // type, honour that header, so for example they don't request