From 807f639212b48f9fa1814eb1fe21a46a6e8b4221 Mon Sep 17 00:00:00 2001 From: Osama Ansar Date: Tue, 13 Oct 2026 22:09:16 +0500 Subject: [PATCH] fix(orama): don't throw on very long tokens in replaceDiacritics 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. --- .../src/components/tokenizer/diacritics.ts | 17 ++++++--- packages/orama/tests/diacritics.test.ts | 36 +++++++++++++++++++ 2 files changed, 49 insertions(+), 4 deletions(-) diff --git a/packages/orama/src/components/tokenizer/diacritics.ts b/packages/orama/src/components/tokenizer/diacritics.ts index d798b55cb..e6dfa6ace 100644 --- a/packages/orama/src/components/tokenizer/diacritics.ts +++ b/packages/orama/src/components/tokenizer/diacritics.ts @@ -203,10 +203,19 @@ function replaceChar(charCode: number): number { return CHARCODE_REPLACE_MAPPING[charCode - DIACRITICS_CHARCODE_START] || charCode } +// Spreading a very large array into String.fromCharCode(...) throws a RangeError (the arguments +// don't fit in the call stack), so long strings are converted in chunks. +const REPLACE_CHUNK_SIZE = 8192 + export function replaceDiacritics(str: string): string { - const stringCharCode: number[] = [] - for (let idx = 0; idx < str.length; idx++) { - stringCharCode[idx] = replaceChar(str.charCodeAt(idx)) + let result = '' + for (let start = 0; start < str.length; start += REPLACE_CHUNK_SIZE) { + const end = Math.min(start + REPLACE_CHUNK_SIZE, str.length) + const stringCharCode: number[] = [] + for (let idx = start; idx < end; idx++) { + stringCharCode.push(replaceChar(str.charCodeAt(idx))) + } + result += String.fromCharCode(...stringCharCode) } - return String.fromCharCode(...stringCharCode) + return result } diff --git a/packages/orama/tests/diacritics.test.ts b/packages/orama/tests/diacritics.test.ts index 053fe2f2e..809ec5793 100644 --- a/packages/orama/tests/diacritics.test.ts +++ b/packages/orama/tests/diacritics.test.ts @@ -1,5 +1,6 @@ import t from 'tap' import { replaceDiacritics } from '../src/components/tokenizer/diacritics.js' +import { create, insert, search } from '../src/index.js' t.test('Diacritics Replacer', async (t) => { t.test('should replace diacritics', async (t) => { @@ -16,3 +17,38 @@ t.test('Diacritics Replacer', async (t) => { t.equal(O3, `aaaaeeeiiooooucnAAAAEEIIOOOOUCN`) }) }) + +t.test('Diacritics Replacer with long strings', async (t) => { + t.test('should replace diacritics in a string of 500000 characters', async (t) => { + const length = 500_000 + // A pattern of 3 characters does not align with the chunk boundaries + const input = 'aéü'.repeat(Math.ceil(length / 3)).slice(0, length) + const expected = 'aeu'.repeat(Math.ceil(length / 3)).slice(0, length) + + const output = replaceDiacritics(input) + + t.equal(output.length, length) + t.ok(output === expected, 'every character is replaced') + }) + + t.test('should handle strings around the chunk size', async (t) => { + for (const length of [0, 1, 8191, 8192, 8193, 16384, 16385]) { + t.equal(replaceDiacritics('é'.repeat(length)), 'e'.repeat(length), `length ${length}`) + } + }) + + t.test('should leave a long string without diacritics unchanged', async (t) => { + const input = 'x'.repeat(150_000) + t.ok(replaceDiacritics(input) === input) + }) + + t.test('should be able to index and find a document with a very long token', async (t) => { + const db = create({ schema: { text: 'string' } as const }) + const token = 'x'.repeat(200_000) + + await insert(db, { text: token }) + const results = await search(db, { term: token }) + + t.equal(results.count, 1) + }) +})