@@ -59,7 +59,7 @@ class Task extends EventEmitter {
5959 __init ( ) {
6060 this . status = STATUS . init
6161 const { path, startByte, endByte } = this . chunkInfo
62- if ( startByte ) this . requestOptions . headers . range = `bytes=${ startByte } -${ endByte } `
62+ if ( startByte != null ) this . requestOptions . headers . range = `bytes=${ startByte } -${ endByte } `
6363 return new Promise ( ( resolve , reject ) => {
6464 if ( ! path ) return resolve ( )
6565 fs . stat ( path , ( errStat , stats ) => {
@@ -110,6 +110,16 @@ class Task extends EventEmitter {
110110 this . request = request ( url , options )
111111 . on ( 'response' , response => {
112112 if ( response . statusCode !== 200 && response . statusCode !== 206 ) {
113+ if ( response . statusCode == 416 ) {
114+ fs . unlink ( this . chunkInfo . path , async err => {
115+ await this . __handleError ( new Error ( response . statusMessage ) )
116+ this . chunkInfo . startByte = 0
117+ this . resumeLastChunk = null
118+ this . progress . downloaded = 0
119+ if ( err ) this . __handleError ( err )
120+ } )
121+ return
122+ }
113123 this . status = STATUS . failed
114124 this . emit ( 'fail' , response )
115125 this . __closeRequest ( )
@@ -154,9 +164,14 @@ class Task extends EventEmitter {
154164 this . ws = fs . createWriteStream ( this . chunkInfo . path , options )
155165
156166 this . ws . on ( 'finish' , ( ) => this . __closeWriteStream ( ) )
157- this . ws . on ( 'error' , async err => {
158- await this . __handleError ( err )
159- fs . unlink ( this . chunkInfo . path , ( ) => this . __handleError ( err ) )
167+ this . ws . on ( 'error' , err => {
168+ fs . unlink ( this . chunkInfo . path , async unlinkErr => {
169+ await this . __handleError ( err )
170+ this . chunkInfo . startByte = 0
171+ this . resumeLastChunk = null
172+ this . progress . downloaded = 0
173+ if ( unlinkErr ) this . __handleError ( unlinkErr )
174+ } )
160175 } )
161176 }
162177
0 commit comments