@@ -17,18 +17,20 @@ import { NestedError } from '../nestedError';
1717import { parse as parseUrl } from 'url' ;
1818import { getProxyAgent } from './proxy' ;
1919import { NetworkSettingsProvider } from '../networkSettings' ;
20+ import { CancellationToken } from 'vscode' ;
2021
2122export async function DownloadFile (
2223 description : string ,
2324 eventStream : EventStream ,
2425 networkSettingsProvider : NetworkSettingsProvider ,
2526 url : string ,
26- fallbackUrl ?: string
27+ fallbackUrl ?: string ,
28+ token ?: CancellationToken
2729) : Promise < Buffer > {
2830 eventStream . post ( new DownloadStart ( description ) ) ;
2931
3032 try {
31- const buffer = await downloadFile ( description , url , eventStream , networkSettingsProvider ) ;
33+ const buffer = await downloadFile ( description , url , eventStream , networkSettingsProvider , token ) ;
3234 eventStream . post ( new DownloadSuccess ( ` Done!` ) ) ;
3335 return buffer ;
3436 } catch ( primaryUrlError ) {
@@ -54,7 +56,8 @@ async function downloadFile(
5456 description : string ,
5557 urlString : string ,
5658 eventStream : EventStream ,
57- networkSettingsProvider : NetworkSettingsProvider
59+ networkSettingsProvider : NetworkSettingsProvider ,
60+ token ?: CancellationToken
5861) : Promise < Buffer > {
5962 const url = parseUrl ( urlString ) ;
6063 const networkSettings = networkSettingsProvider ( ) ;
@@ -66,12 +69,15 @@ async function downloadFile(
6669 agent : getProxyAgent ( url , proxy , strictSSL ) ,
6770 port : url . port ,
6871 rejectUnauthorized : strictSSL ,
69- timeout : 5 * 60 * 1000 , // timeout is in milliseconds
7072 } ;
7173
7274 const buffers : any [ ] = [ ] ;
7375
7476 return new Promise < Buffer > ( ( resolve , reject ) => {
77+ token ?. onCancellationRequested ( ( ) => {
78+ return reject ( new NestedError ( `Cancelled downloading ${ urlString } .` ) ) ;
79+ } ) ;
80+
7581 const request = https . request ( options , ( response ) => {
7682 if ( response . statusCode === 301 || response . statusCode === 302 ) {
7783 // Redirect - download from new location
@@ -82,7 +88,7 @@ async function downloadFile(
8288 return reject ( new NestedError ( 'Missing location' ) ) ;
8389 }
8490 return resolve (
85- downloadFile ( description , response . headers . location , eventStream , networkSettingsProvider )
91+ downloadFile ( description , response . headers . location , eventStream , networkSettingsProvider , token )
8692 ) ;
8793 } else if ( response . statusCode !== 200 ) {
8894 // Download failed - print error message
@@ -130,10 +136,6 @@ async function downloadFile(
130136 } ) ;
131137 } ) ;
132138
133- request . on ( 'timeout' , ( ) => {
134- request . destroy ( new Error ( `Timed out trying to download ${ urlString } .` ) ) ;
135- } ) ;
136-
137139 request . on ( 'error' , ( err ) => {
138140 reject ( new NestedError ( `Request error: ${ err . message || 'NONE' } ` , err ) ) ;
139141 } ) ;
0 commit comments