🐛 Use numerical sort to fix stack overflow

This commit is contained in:
C-3PO 2018-10-07 20:41:17 +02:00
parent 7c95adfc15
commit 7f63a97ebb
Signed by: c3po
GPG key ID: 62993C4BB4D86F24
2 changed files with 8 additions and 3 deletions

View file

@ -1,5 +1,11 @@
{ {
"name": "patcher", "name": "patcher",
"license": "AGPL-3.0-or-later",
"homepage": "https://git.jedipedia.net/swtor/patcher",
"repository": {
"type": "git",
"url": "https://git.jedipedia.net/swtor/patcher.git"
},
"main": "dist/index.js", "main": "dist/index.js",
"scripts": { "scripts": {
"start": "rm -rf dist && tsc && cd ../patch-installer && ./build.sh && cd ../patcher && mkdir dist/lib && cp ../patch-installer/patch-installer dist/lib", "start": "rm -rf dist && tsc && cd ../patch-installer && ./build.sh && cd ../patcher && mkdir dist/lib && cp ../patch-installer/patch-installer dist/lib",

View file

@ -54,7 +54,7 @@ function getFromList({ product, to: releaseTo}: {product: Product, to: number})
* Does not actually look at the manifest.xml file but theorizes on possible patches based on the usually created patches, allowing * Does not actually look at the manifest.xml file but theorizes on possible patches based on the usually created patches, allowing
* us to find release paths for products that have not been released yet. * us to find release paths for products that have not been released yet.
*/ */
export default function findReleasePath({ product, from, to}: {product: Product, from: number, to: number}): Array<[number, number]> { export default function findReleasePath({ product, from, to }: {product: Product, from: number, to: number}): Array<[number, number]> {
//Verify function arguments //Verify function arguments
if (!verifyProductName(product)) { if (!verifyProductName(product)) {
throw new Error(`"${product}" is not a valid product.`); throw new Error(`"${product}" is not a valid product.`);
@ -75,11 +75,10 @@ export default function findReleasePath({ product, from, to}: {product: Product,
//Otherwise, check all from values recursively, by checking interim releases //Otherwise, check all from values recursively, by checking interim releases
const smallerFromList = fromList.filter((num) => num > from); const smallerFromList = fromList.filter((num) => num > from);
//Always prefer shortest release paths (e.g. 1->3 vs. 1->2->3) by ensuring we check smallest from values first //Always prefer shortest release paths (e.g. 1->3 vs. 1->2->3) by ensuring we check smallest from values first
smallerFromList.sort(); smallerFromList.sort((a, b) => (a < b) ? -1 : (a > b) ? 1 : 0);
for (let i = 0, il = smallerFromList.length; i < il; i += 1) { for (let i = 0, il = smallerFromList.length; i < il; i += 1) {
const interim = smallerFromList[i]; const interim = smallerFromList[i];
//FIXME: This sometimes causes a "Maximum call stack size exceeded" error, e.g. for `{ product: '*', from: 1, to: 11 }`
const releasePath = findReleasePath({ product, from, to: interim} ); const releasePath = findReleasePath({ product, from, to: interim} );
if (releasePath.length > 0) { if (releasePath.length > 0) {
return [...releasePath, [interim, to]]; return [...releasePath, [interim, to]];