diff --git a/packages/verified-fetch/src/utils/byte-range-context.ts b/packages/verified-fetch/src/utils/byte-range-context.ts index 1979ad1..fe8f4a2 100644 --- a/packages/verified-fetch/src/utils/byte-range-context.ts +++ b/packages/verified-fetch/src/utils/byte-range-context.ts @@ -42,7 +42,13 @@ function getByteRangeFromHeader (rangeHeader: string): { start: string, end: str export class ByteRangeContext { private readonly _isRangeRequest: boolean - private _fileSize: number | null | undefined + + /** + * This property should only be set by calling `setFileSize` or `setBody`. + * + * @access private + */ + public fileSize: Readonly private readonly _contentRangeHeaderValue: string | undefined private _body: SupportedBodyTypes | null = null private readonly _rangeRequestHeader: string | undefined @@ -80,12 +86,15 @@ export class ByteRangeContext { } } + /** + * When you get a body, it should be set here, and we will calculate the fileSize if possible. + */ public setBody (body: SupportedBodyTypes): void { this._body = body // if fileSize was already set, don't recalculate it - this.fileSize = this.fileSize ?? getBodySizeSync(body) + this.setFileSize(this.fileSize ?? getBodySizeSync(body)) - this.log.trace('set request body with fileSize %o', this._fileSize) + this.log.trace('set request body with fileSize %o', this.fileSize) } public getBody (): SupportedBodyTypes { @@ -139,18 +148,20 @@ export class ByteRangeContext { return this.requestRangeStart != null && this.requestRangeEnd == null } - // sometimes, we need to set the fileSize explicitly because we can't calculate the size of the body (e.g. for unixfs content where we call .stat) - public set fileSize (size: number | bigint | null) { - this._fileSize = size != null ? Number(size) : null - this.log.trace('set _fileSize to %o', this._fileSize) + /** + * Sometimes, we need to set the fileSize explicitly because we can't calculate + * the size of the body (e.g. for unixfs content where we call .stat). + * + * This fileSize should otherwise only be called from `setBody`, and `.fileSize` + * should not be set directly. + */ + public setFileSize (size: number | bigint | null): void { + this.fileSize = size != null ? Number(size) : null + this.log.trace('set _fileSize to %o', this.fileSize) // when fileSize changes, we need to recalculate the offset details this.setOffsetDetails() } - public get fileSize (): number | null | undefined { - return this._fileSize - } - public get isRangeRequest (): boolean { return this._isRangeRequest } @@ -259,7 +270,7 @@ export class ByteRangeContext { return } - const { start, end, byteSize } = calculateByteRangeIndexes(this.requestRangeStart ?? undefined, this.requestRangeEnd ?? undefined, this._fileSize ?? undefined) + const { start, end, byteSize } = calculateByteRangeIndexes(this.requestRangeStart ?? undefined, this.requestRangeEnd ?? undefined, this.fileSize ?? undefined) this.log.trace('set byteStart to %o, byteEnd to %o, byteSize to %o', start, end, byteSize) this.byteStart = start this.byteEnd = end @@ -290,7 +301,7 @@ export class ByteRangeContext { return getContentRangeHeader({ byteStart: this.byteStart, byteEnd: this.byteEnd, - byteSize: this._fileSize ?? undefined + byteSize: this.fileSize ?? undefined }) } } diff --git a/packages/verified-fetch/src/verified-fetch.ts b/packages/verified-fetch/src/verified-fetch.ts index c6161dd..4c75180 100644 --- a/packages/verified-fetch/src/verified-fetch.ts +++ b/packages/verified-fetch/src/verified-fetch.ts @@ -334,7 +334,7 @@ export class VerifiedFetch { // we have to .stat before .cat so we can get the size and make sure byte offsets are calculated properly if (byteRangeContext.isRangeRequest && byteRangeContext.isValidRangeRequest && terminalElement.type === 'file') { - byteRangeContext.fileSize = terminalElement.unixfs.fileSize() + byteRangeContext.setFileSize(terminalElement.unixfs.fileSize()) this.log.trace('fileSize for rangeRequest %d', byteRangeContext.fileSize) } diff --git a/packages/verified-fetch/test/utils/byte-range-context.spec.ts b/packages/verified-fetch/test/utils/byte-range-context.spec.ts index e70fa79..abf7ef9 100644 --- a/packages/verified-fetch/test/utils/byte-range-context.spec.ts +++ b/packages/verified-fetch/test/utils/byte-range-context.spec.ts @@ -126,7 +126,7 @@ describe('ByteRangeContext', () => { leafType: 'file' }) const stat = await fs.stat(cid) - context.fileSize = stat.fileSize + context.setFileSize(stat.fileSize) context.setBody(await getBodyStream(context.offset, context.length)) const response = new Response(context.getBody())