From 3b370223940242c8c0777a5ce43acf45d735a953 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Grie=C3=9Fhaber?= Date: Thu, 18 Feb 2016 07:43:00 +0100 Subject: [PATCH 1/2] refactored all add() calls from SortedSet to only pass single values before this fix, the index passed all tokens via an apply call, pushing all parameters on the stack. This behaviour causes a RangeError when more tokens are added than the JS implementation can handle (Firefox 44 -> 262143, Node.js -> 65535, evaluated using this test: http://stackoverflow.com/questions/22747068/is-there-a-max-number-of-arguments-javascript-functions-can-accept) this fix changes the code so that now only a single value gets passed per call. This way no change to the current API is neccesary. the code was refactored in a way that it now passes only a single argument at a time, avoiding the usage of apply The only parts of the code that still use apply are the EventEmitters and the Plugin subsystem which should be non critical for this type of error. --- lib/index.js | 8 ++++++-- lib/sorted_set.js | 4 +++- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/lib/index.js b/lib/index.js index 1e4dbac0..83bc5471 100644 --- a/lib/index.js +++ b/lib/index.js @@ -151,11 +151,15 @@ lunr.Index.prototype.add = function (doc, emitEvent) { var fieldTokens = this.pipeline.run(lunr.tokenizer(doc[field.name])) docTokens[field.name] = fieldTokens - lunr.SortedSet.prototype.add.apply(allDocumentTokens, fieldTokens) + + for (var i = 0; i < fieldTokens.length; i++) { + var token = fieldTokens[i] + allDocumentTokens.add(token) + this.corpusTokens.add(token) + } }, this) this.documentStore.set(docRef, allDocumentTokens) - lunr.SortedSet.prototype.add.apply(this.corpusTokens, allDocumentTokens.toArray()) for (var i = 0; i < allDocumentTokens.length; i++) { var token = allDocumentTokens.elements[i] diff --git a/lib/sorted_set.js b/lib/sorted_set.js index 2f0cd6d2..43a7b0f2 100644 --- a/lib/sorted_set.js +++ b/lib/sorted_set.js @@ -224,7 +224,9 @@ lunr.SortedSet.prototype.union = function (otherSet) { unionSet = longSet.clone() - unionSet.add.apply(unionSet, shortSet.toArray()) + for(var i = 0, shortSetElements = shortSet.toArray(); i < shortSetElements.length; i++){ + unionSet.add(shortSetElements[i]); + } return unionSet } From eda358f850f7fecc86e05b348dfc2ad09751a8a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Grie=C3=9Fhaber?= Date: Tue, 8 Mar 2016 07:53:39 +0100 Subject: [PATCH 2/2] removed semicolons to match coding style the rest of the project is coded in a semicolon free style. this commit makes a change to match that style in one location --- lib/sorted_set.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/sorted_set.js b/lib/sorted_set.js index 43a7b0f2..a8422940 100644 --- a/lib/sorted_set.js +++ b/lib/sorted_set.js @@ -225,7 +225,7 @@ lunr.SortedSet.prototype.union = function (otherSet) { unionSet = longSet.clone() for(var i = 0, shortSetElements = shortSet.toArray(); i < shortSetElements.length; i++){ - unionSet.add(shortSetElements[i]); + unionSet.add(shortSetElements[i]) } return unionSet