askorama / orama

🌌 Fast, dependency-free, full-text and vector search engine with typo tolerance, filters, facets, stemming, and more. Works with any JavaScript runtime, browser, server, service!

Home Page:https://docs.askorama.ai

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Updating Object.prototype causes search to fail

SpongeBed81 opened this issue · comments

Describe the bug

Adding a new property to Object.prototype causes search function to fail

To Reproduce

1 - Import necessary functions of Orama(insert, search, create)
2 - Add a property to object prototype
3 - Create an example schema
4 - Insert some data into it
5 - Search

Here is an example:

import { insert, search, create } from '@orama/orama';

Object.prototype.justTesting = () => {};

const schema = {
    name: 'string',
};

const orama = await create({
    schema,
});

await insert(orama, {
    name: 'John Doe',
});

const result = await search(orama, {
    term: 'John Doe',
});

console.log(result);

Code above throws an error saying "TypeError: searchResult[key] is not iterable" and after some debugging we can clearly see the issue:

    const { exact , tolerance  } = context.params;
    const searchResult = radixFind(node, {
        term,
        exact,
        tolerance
    });
    const ids = new Set();
    console.log(searchResult)
    for(const key in searchResult){
        console.log(key, searchResult[key])
        for (const id of searchResult[key]){
            ids.add(id);
        }
    }
    return context.index.calculateResultScores(context, index, prop, term, Array.from(ids));

Here is the code snippet from Orama's source code that I took which caused the issue. I added some console.logs to see what was the problem and second console.log prints out: "justTesting [Function (anonymous)]" and that causes searchResult[key] being not iterable.

Expected behavior

Search function should properly return a value without any issues even if Object.prototype has changed.

Environment Info

OS: Windows 10 Version 22H2 OS Build 19045.2486
Node: v20.10.0
Orama: 2.0.0-beta.12 (also tried it with 1.0.0-beta.16 and got the same result)

Affected areas

Search

Additional context

No response

Fixed it with adding a one line code into the for loop. I will open a PR about this in a free time 😃

@SpongeBed81 awesome, thank you! Looking forward to the PR