Skip to content

Commit

Permalink
chore: fileSize is public property, but should not be used
Browse files Browse the repository at this point in the history
  • Loading branch information
SgtPooki committed Mar 15, 2024
1 parent ac621a2 commit 46dc133
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 15 deletions.
37 changes: 24 additions & 13 deletions packages/verified-fetch/src/utils/byte-range-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<number | null | undefined>
private readonly _contentRangeHeaderValue: string | undefined
private _body: SupportedBodyTypes | null = null
private readonly _rangeRequestHeader: string | undefined
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -290,7 +301,7 @@ export class ByteRangeContext {
return getContentRangeHeader({
byteStart: this.byteStart,
byteEnd: this.byteEnd,
byteSize: this._fileSize ?? undefined
byteSize: this.fileSize ?? undefined
})
}
}
2 changes: 1 addition & 1 deletion packages/verified-fetch/src/verified-fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down

0 comments on commit 46dc133

Please sign in to comment.