diff --git a/src/ssn/getPatch.ts b/src/ssn/getPatch.ts index b32e934..4e210d7 100644 --- a/src/ssn/getPatch.ts +++ b/src/ssn/getPatch.ts @@ -62,7 +62,7 @@ export default async function getPatch({ product, from, to, sourceDirectory, tar //start download, making sure that .zip file downloads first const indexOfLastFile = solidpkg.files.length - 1; const zipFile = getUrlContents(createUrlObject(solidpkg.files[indexOfLastFile])); - const diskFiles = solidpkg.files.slice(0, indexOfLastFile).map((file) => downloadWrapper({ ...createUrlObject(file), useCurl: true })); + const diskFiles = solidpkg.files.slice(0, indexOfLastFile).map((file) => downloadWrapper.bind(null, { ...createUrlObject(file), useCurl: true })); //we can parse the file entries as soon as the .zip file is downloaded const fileEntries = readSsnFile(await zipFile); @@ -72,7 +72,7 @@ export default async function getPatch({ product, from, to, sourceDirectory, tar //Then we need to wait for disks to finish download before we can extract individual files //TODO: we can optimize this to already extract some files as soon as their relevant parts are downloaded - const diskFilenames = await Promise.all(diskFiles); + const diskFilenames: string[] = await taskManager(diskFiles, 3); //max. of 3 concurrent downloads //TODO: Verify that downloaded files match the hash in `solidpkg.pieces` diff --git a/src/ssn/installation/taskManager.ts b/src/ssn/installation/taskManager.ts index ebd1008..4cfedb9 100644 --- a/src/ssn/installation/taskManager.ts +++ b/src/ssn/installation/taskManager.ts @@ -1,5 +1,6 @@ -export default function taskManager(tasks: Array<() => Promise>, maxConcurrentTasks: number): Promise { +export default function taskManager(tasks: Array<() => Promise>, maxConcurrentTasks: number): Promise { return new Promise((resolve, reject) => { + const returnValues = Array(tasks.length); const remainingTasks = tasks; let currentlyRunningTasks = 0; @@ -7,13 +8,15 @@ export default function taskManager(tasks: Array<() => Promise>, maxConcur //Exit if we completed all tasks if (remainingTasks.length === 0) { if (currentlyRunningTasks === 0) { - return resolve(); + return resolve(returnValues); } } else { //If there is at least one task left, complete it const curTask = remainingTasks.pop() as () => Promise; + const curTaskIndex = remainingTasks.length; currentlyRunningTasks += 1; - curTask().then(() => { + curTask().then((...result) => { + returnValues[curTaskIndex] = result; currentlyRunningTasks -= 1; return startNewTask(); }).catch((error) => {