🗑️ Remove download with vanilla Node.js

This commit is contained in:
C-3PO 2018-11-16 00:23:25 +01:00
parent 1326f5b5f4
commit e5bfdd6e95
Signed by: c3po
GPG key ID: 62993C4BB4D86F24
3 changed files with 13 additions and 47 deletions

View file

@ -1,26 +0,0 @@
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();
}

View file

@ -1,12 +1,11 @@
import * as os from 'os'; import * as os from 'os';
import * as nodePath from 'path'; import * as nodePath from 'path';
import downloadUrlContents from './downloadUrlContents';
import downloadWithCurl from './downloadWithCurl'; import downloadWithCurl from './downloadWithCurl';
import checkLocalCache from './funcs/checkLocalCache'; import checkLocalCache from './funcs/checkLocalCache';
import createDirRecursively from './funcs/createDirRecursively'; import createDirRecursively from './funcs/createDirRecursively';
/** Downloads the given URL and saves it to disk. Returns the location where file is saved under. Throws error if download fails. */ /** Downloads the given URL and saves it to disk. Returns the location where file is saved under. Throws error if download fails. */
export default async function downloadWrapper({ host, path, size, useCurl = false }: {host: string, path: string, size: number, useCurl: boolean}): Promise<string> { export default async function downloadWrapper({ host, path, size }: {host: string, path: string, size: number, useCurl: boolean}): Promise<string> {
//Generate file name we want to save it under //Generate file name we want to save it under
//e.g. on Linux: /tmp/patcher/cdn-patch.swtor.com/patch/assets_swtor_main/assets_swtor_main_-1to0/assets_swtor_main_-1to0.zip //e.g. on Linux: /tmp/patcher/cdn-patch.swtor.com/patch/assets_swtor_main/assets_swtor_main_-1to0/assets_swtor_main_-1to0.zip
const tempFileName = nodePath.join(os.tmpdir(), 'patcher', host, path); const tempFileName = nodePath.join(os.tmpdir(), 'patcher', host, path);
@ -21,24 +20,17 @@ export default async function downloadWrapper({ host, path, size, useCurl = fals
return tempFileName; return tempFileName;
} }
//Download either via curl or natively with Node //Download via curl
if (useCurl) { //Try up to three times
//Try up to three times for (let i = 0; i < 3; i += 1) {
for (let i = 0; i < 3; i += 1) { try {
try { const downloadResult = await downloadWithCurl({ host, path, tempFileName });
const downloadResult = await downloadWithCurl({ host, path, tempFileName }); return downloadResult;
return downloadResult; } catch (err) {
} catch (err) { console.error(err);
console.error(err); //ignore error and try again
//ignore error and try again
}
} }
//Download failed, throw error
throw new Error('Could not download with curl');
} else {
const downloadResult = await new Promise((resolve, reject) => {
downloadUrlContents(host, path, tempFileName, resolve, reject);
}) as string;
return downloadResult;
} }
//Download failed, throw error
throw new Error('Could not download with curl');
} }

View file

@ -62,7 +62,7 @@ export default async function installPatch({ product, from, to, sourceDirectory,
//start download, making sure that .zip file downloads first //start download, making sure that .zip file downloads first
const indexOfLastFile = solidpkg.files.length - 1; const indexOfLastFile = solidpkg.files.length - 1;
const zipFile = getUrlContents(createUrlObject(solidpkg.files[indexOfLastFile])); const zipFile = getUrlContents(createUrlObject(solidpkg.files[indexOfLastFile]));
const diskFiles = solidpkg.files.slice(0, indexOfLastFile).map((file) => downloadWrapper.bind(null, { ...createUrlObject(file), useCurl: true })); const diskFiles = solidpkg.files.slice(0, indexOfLastFile).map((file) => downloadWrapper.bind(null, createUrlObject(file)));
//we can parse the file entries as soon as the .zip file is downloaded //we can parse the file entries as soon as the .zip file is downloaded
const fileEntries = readSsnFile(await zipFile); const fileEntries = readSsnFile(await zipFile);