Skip to content

Commit

Permalink
fix: Should correctly handle range download n+1 issue
Browse files Browse the repository at this point in the history
  • Loading branch information
ci010 committed Jan 10, 2024
1 parent c56cc6f commit 333b718
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
2 changes: 1 addition & 1 deletion packages/file-transfer/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export class DownloadAgent {
const { contentLength, isAcceptRanges } = parseRangeInfo(response.headers)
ranges = contentLength && isAcceptRanges
? this.rangePolicy.computeRanges(contentLength)
: [{ start: 0, end: contentLength }]
: [{ start: 0, end: contentLength - 1 }]
targetUrl = new URL(location)
total = contentLength
await handle.truncate(total)
Expand Down
9 changes: 8 additions & 1 deletion packages/file-transfer/range.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ export async function * range(

let nextUrl = url
while (true) {
if (segment.start >= segment.end) {
// the segment is finished, just ignore it
return
}
try {
const { opaque } = await stream(nextUrl, {
method: 'GET',
Expand Down Expand Up @@ -84,7 +88,10 @@ export async function * range(
}
if (typeof responseHeaders['content-length'] === 'string') {
contentLength = Number.parseInt(responseHeaders['content-length'] ?? '0')
segment.end = contentLength
const end = segment.start + contentLength - 1
if (end !== segment.end) {
segment.end = segment.start + contentLength
}
}
return (opaque as any).fileStream as Writable
})
Expand Down

0 comments on commit 333b718

Please sign in to comment.