From c987f501b3575126dff5b87270338f2801541694 Mon Sep 17 00:00:00 2001 From: Xavier Fournet <461943+xfournet@users.noreply.github.com> Date: Thu, 3 Oct 2024 21:57:27 +0200 Subject: [PATCH] Fix crash when executing browser with CSP script-src that don't permit unsafe-eval --- lib/quick-sort.js | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/quick-sort.js b/lib/quick-sort.js index 23f9eda5..c9ffe55d 100644 --- a/lib/quick-sort.js +++ b/lib/quick-sort.js @@ -112,6 +112,15 @@ function cloneSort(comparator) { return templateFn(comparator); } +let isEvalAllowed = (function () { + try { + new Function('return 0')(); + return true; + } catch { + return false; + } +})(); + /** * Sort the given array in-place with the given comparator function. * @@ -123,7 +132,7 @@ function cloneSort(comparator) { let sortCache = new WeakMap(); exports.quickSort = function (ary, comparator, start = 0) { - let doQuickSort = sortCache.get(comparator); + let doQuickSort = isEvalAllowed ? sortCache.get(comparator) : SortTemplate(comparator); if (doQuickSort === void 0) { doQuickSort = cloneSort(comparator); sortCache.set(comparator, doQuickSort);