23 lines
718 B
TypeScript
23 lines
718 B
TypeScript
import * as fs from 'fs';
|
|
import * as http from 'http';
|
|
import saveResponse from './funcs/saveResponse';
|
|
|
|
/** Downloads the given URL and saves it to disk. Throws error if download fails. */
|
|
export default function downloadUrlContents({ host, path }: {host: string, path: string}): Promise<fs.ReadStream> {
|
|
return new Promise((resolve, reject) => {
|
|
//Create HTTP request
|
|
const request = http.request({
|
|
family: 4,
|
|
host,
|
|
path,
|
|
}, saveResponse.bind(null, resolve, (reason: string) => { request.abort(); reject(reason); }));
|
|
|
|
//In case of connection errors, exit early
|
|
request.on('error', (error) => {
|
|
request.abort();
|
|
reject(error);
|
|
});
|
|
|
|
request.end();
|
|
});
|
|
}
|