Repository navigation
fix(orama): don't throw on very long tokens in replaceDiacritics - #1055
Open
OsamaAnsar wants to merge 1 commit into
Open
OsamaAnsar wants to merge 1 commit into
OsamaAnsar wants to merge 1 commit into
Conversation
replaceDiacritics passed every character code of the token to String.fromCharCode(...codes), which throws "RangeError: Maximum call stack size exceeded" once the token is long enough (it worked at 50000 characters and threw at 150000). Indexing a document with such a token, for example a long base64 blob, failed with that error. Convert the string in chunks instead.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What was wrong
replaceDiacritics(packages/orama/src/components/tokenizer/diacritics.ts) collects the char codes of the whole token in an array and then does:Spreading a very large array into function arguments exceeds the engine's argument/stack limit, so the call throws
RangeError: Maximum call stack size exceeded. The tokenizer calls it for every token, so inserting a document with one very long token (a long unbroken blob of text such as base64 or minified content) throws on insert.Reproduction (script run against
mainat b030e1b, then against this branch)(Node v24.19.0; the exact threshold depends on the engine and stack size.)
Fix
Convert the string in chunks of 8192 characters and concatenate the results. The mapping itself (
replaceChar) is unchanged, and the output for short strings is identical.Tests
Added to
packages/orama/tests/diacritics.test.ts: a 500000-character string with diacritics (pattern length 3, so it does not line up with the chunk size) is fully converted; lengths 0, 1, 8191, 8192, 8193, 16384 and 16385; a 150000-character string without diacritics is returned unchanged; and a document with a 200000-character token can be inserted and found.Verification I ran (Node v24.19.0,
tapon the TypeScript sources):diacritics.ts(confirmed withgrep -c REPLACE_CHUNK_SIZEgoing from 3 to 0) and kept the new tests: the three long-string tests fail (3 of 13 assertions; the insert test throws before it can assert). Restored the fix: 14 of 14 pass in that file.packages/oramasuite (tap tests/*.test.ts) with the fix: 1492 total, 1489 pass, 3 skipped (pre-existing skips), 0 fail. Onmainthe same command gave 1481 total, 1478 pass, 3 skipped. This needed@orama/stopwordsand@orama/stemmersbuilt locally first.tsc --noEmit -p packages/orama/tsconfig.json: clean. Formatting checked with Biome 1.9.4 against the repobiome.json: clean. The packagelintscript isexit 0, so there is no linter to run. I did not runtest:types(tsd) or the browser/smoke tests.Related spread sites I did not change
I left these alone to keep this PR to the one failing path, and did not test them with large inputs:
Math.max(...distances)incomponents/index.ts(geosearch),results.push(...updateResults)/results.push(...insertResults)inmethods/upsert.ts, andMath.max.apply(Math, ...)over the result scores inmethods/search-hybrid.ts. They could hit the same limit with very large arrays.utils.tsalready has a chunkedpushhelper (MAX_ARGUMENT_FOR_STACK) if you want to follow that pattern.I did not add a changeset or CHANGELOG entry since recent fix PRs do not; happy to add one if you prefer. I am also happy to move the discussion to an issue if that is how you would like to handle it.