Skip to content

Commit

Permalink
chore: ByteRangeContext getBody cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
SgtPooki committed Mar 8, 2024
1 parent b0b6a4a commit f399bed
Showing 1 changed file with 5 additions and 7 deletions.
12 changes: 5 additions & 7 deletions packages/verified-fetch/src/utils/byte-range-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,22 +100,20 @@ export class ByteRangeContext {
const byteEnd = this.byteEnd
const byteSize = this.byteSize
if (byteStart != null || byteEnd != null) {
this.log.trace('returning body with byteStart %o byteEnd %o byteSize', byteStart, byteEnd, byteSize)
if (body instanceof ArrayBuffer || body instanceof Blob || body instanceof Uint8Array) {
this.log.trace('body is Uint8Array')
return this.getSlicedBody(body)
} else if (body instanceof ReadableStream) {
// stream should already be spliced by dagPb/unixfs
this.log.trace('returning body with byteStart=%o, byteEnd=%o, byteSize=%o', byteStart, byteEnd, byteSize)
if (body instanceof ReadableStream) {
// stream should already be spliced by `unixfs.cat`
return body
}
return this.getSlicedBody(body)
}

// we should not reach this point, but return body untouched.
this.log.error('returning unmofified body for valid range request')
return body
}

Check warning on line 114 in packages/verified-fetch/src/utils/byte-range-context.ts

View check run for this annotation

Codecov / codecov/patch

packages/verified-fetch/src/utils/byte-range-context.ts#L110-L114

Added lines #L110 - L114 were not covered by tests

private getSlicedBody <T extends Uint8Array | ArrayBuffer | Blob>(body: T): T {
private getSlicedBody <T extends Uint8Array | ArrayBuffer | Blob | string>(body: T): T {
if (this.isPrefixLengthRequest) {
this.log.trace('sliced body with byteStart %o', this.byteStart)
return body.slice(this.offset) as T
Expand Down

0 comments on commit f399bed

Please sign in to comment.