26 lines
768 B
TypeScript
26 lines
768 B
TypeScript
import * as http from 'http';
|
|
import saveResponse from './funcs/saveResponse';
|
|
|
|
/** Downloads the given URL and saves it to disk. Returns the location where file is saved under. Throws error if download fails. */
|
|
export default function downloadUrlContents(
|
|
host: string,
|
|
path: string,
|
|
tempFileName: string,
|
|
resolve: (fileName: string) => void,
|
|
reject: (reason: Error | string) => void,
|
|
): void {
|
|
//Create HTTP request
|
|
const request = http.request({
|
|
family: 4,
|
|
hostname: host,
|
|
path,
|
|
}, saveResponse.bind(null, tempFileName, resolve, (reason: string) => { request.abort(); reject(reason); }));
|
|
|
|
//In case of connection errors, exit early
|
|
request.on('error', (error) => {
|
|
request.abort();
|
|
reject(error);
|
|
});
|
|
|
|
request.end();
|
|
}
|