Skip to content

Commit

Permalink
fix: Should failed if the request is failed
Browse files Browse the repository at this point in the history
  • Loading branch information
ci010 committed Dec 30, 2023
1 parent d3490a2 commit 524c044
Showing 1 changed file with 20 additions and 9 deletions.
29 changes: 20 additions & 9 deletions packages/file-transfer/range.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export async function * range(

while (true) {
try {
await stream(url, {
const { opaque } = await stream(url, {
method: 'GET',
dispatcher,
headers: {
Expand All @@ -66,14 +66,25 @@ export async function * range(
throwOnError: true,
maxRedirections: 2,
signal: abortSignal,
opaque: fileStream,
onInfo({ headers }) {
if (typeof headers['content-length'] === 'string') {
contentLength = Number.parseInt(headers['content-length'] ?? '0')
segment.end = contentLength
}
},
}, ({ opaque }) => opaque as Writable)
opaque: { fileStream },
}, ({ opaque, headers, statusCode }) => {
if (statusCode >= 300) {
(opaque as any).error = Object.assign(new Error(`Unexpected status code ${statusCode} from ${url}`), {
name: 'UnexpectedStatusCodeError',
range: segment.end < 0 ? undefined : `bytes=${segment.start}-${(segment.end) ?? ''}`,
})
// return a run-away writable
return new Writable({ write(chunk, en, cb) { cb() } })
}
if (typeof headers['content-length'] === 'string') {
contentLength = Number.parseInt(headers['content-length'] ?? '0')
segment.end = contentLength
}
return (opaque as any).fileStream as Writable
})
if ((opaque as any).error) {
throw (opaque as any).error
}
return
} catch (e) {
yield e
Expand Down

0 comments on commit 524c044

Please sign in to comment.