55
66import * as https from 'https' ;
77import * as util from '../common' ;
8- import * as fs from 'fs' ;
98import { EventStream } from "../EventStream" ;
109import { DownloadSuccess , DownloadStart , DownloadFallBack , DownloadFailure , DownloadProgress , DownloadSizeObtained } from "../omnisharp/loggingEvents" ;
1110import { NestedError } from "../NestedError" ;
@@ -17,8 +16,9 @@ export async function DownloadFile(destinationFileDescriptor: number, descriptio
1716 eventStream . post ( new DownloadStart ( description ) ) ;
1817
1918 try {
20- await downloadFile ( destinationFileDescriptor , description , url , eventStream , networkSettingsProvider ) ;
19+ let buffer = await downloadFile ( destinationFileDescriptor , description , url , eventStream , networkSettingsProvider ) ;
2120 eventStream . post ( new DownloadSuccess ( ` Done!` ) ) ;
21+ return buffer ;
2222 }
2323 catch ( primaryUrlError ) {
2424 // If the package has a fallback Url, and downloading from the primary Url failed, try again from
@@ -27,8 +27,9 @@ export async function DownloadFile(destinationFileDescriptor: number, descriptio
2727 if ( fallbackUrl ) {
2828 eventStream . post ( new DownloadFallBack ( fallbackUrl ) ) ;
2929 try {
30- await downloadFile ( destinationFileDescriptor , description , fallbackUrl , eventStream , networkSettingsProvider ) ;
30+ let buffer = await downloadFile ( destinationFileDescriptor , description , fallbackUrl , eventStream , networkSettingsProvider ) ;
3131 eventStream . post ( new DownloadSuccess ( ' Done!' ) ) ;
32+ return buffer ;
3233 }
3334 catch ( fallbackUrlError ) {
3435 throw primaryUrlError ;
@@ -40,7 +41,7 @@ export async function DownloadFile(destinationFileDescriptor: number, descriptio
4041 }
4142}
4243
43- async function downloadFile ( fd : number , description : string , urlString : string , eventStream : EventStream , networkSettingsProvider : NetworkSettingsProvider ) : Promise < void > {
44+ async function downloadFile ( fd : number , description : string , urlString : string , eventStream : EventStream , networkSettingsProvider : NetworkSettingsProvider ) : Promise < Buffer > {
4445 const url = parseUrl ( urlString ) ;
4546 const networkSettings = networkSettingsProvider ( ) ;
4647 const proxy = networkSettings . proxy ;
@@ -53,7 +54,9 @@ async function downloadFile(fd: number, description: string, urlString: string,
5354 rejectUnauthorized : util . isBoolean ( strictSSL ) ? strictSSL : true
5455 } ;
5556
56- return new Promise < void > ( ( resolve , reject ) => {
57+ let buffers : any [ ] = [ ] ;
58+
59+ return new Promise < Buffer > ( ( resolve , reject ) => {
5760 if ( fd == 0 ) {
5861 reject ( new NestedError ( "Temporary package file unavailable" ) ) ;
5962 }
@@ -74,12 +77,13 @@ async function downloadFile(fd: number, description: string, urlString: string,
7477 let packageSize = parseInt ( response . headers [ 'content-length' ] , 10 ) ;
7578 let downloadedBytes = 0 ;
7679 let downloadPercentage = 0 ;
77- let tmpFile = fs . createWriteStream ( null , { fd } ) ;
80+ // let tmpFile = fs.createWriteStream(null, { fd });
7881
7982 eventStream . post ( new DownloadSizeObtained ( packageSize ) ) ;
8083
8184 response . on ( 'data' , data => {
8285 downloadedBytes += data . length ;
86+ buffers . push ( data ) ;
8387
8488 // Update status bar item with percentage
8589 let newPercentage = Math . ceil ( 100 * ( downloadedBytes / packageSize ) ) ;
@@ -90,15 +94,15 @@ async function downloadFile(fd: number, description: string, urlString: string,
9094 } ) ;
9195
9296 response . on ( 'end' , ( ) => {
93- resolve ( ) ;
97+ resolve ( Buffer . concat ( buffers ) ) ;
9498 } ) ;
9599
96100 response . on ( 'error' , err => {
97101 reject ( new NestedError ( `Reponse error: ${ err . message || 'NONE' } ` , err ) ) ;
98102 } ) ;
99103
100104 // Begin piping data from the response to the package file
101- response . pipe ( tmpFile , { end : false } ) ;
105+ // response.pipe(tmpFile, { end: false });
102106 } ) ;
103107
104108 request . on ( 'error' , err => {
0 commit comments