kontent-ai / kontent-delivery-node-parser

Rich text element parser for node.js & javascript delivery SDK.
MIT License
0 stars 2 forks source link

RangeError: Maximum call stack size exceeded with certain content #6

Closed tveimo closed 1 year ago

tveimo commented 1 year ago

Brief bug description

I'm trying to use the node parser for url resolution, but am encountering RangeError in the striptags dependency used by it.

Repro steps

Here's a gist: https://gist.github.com/tveimo/9ed80c181d80c449cc95dcf079c47cd6

run with ts-node --esm test.mts

Test environment

We're using nuxt, typescript, node v18.12.1.

tveimo commented 1 year ago

exception;

/Users/thor/node/src/pixelpixel/sunshinecoast/node_modules/@kontent-ai/delivery-node-parser/node_modules/striptags/src/striptags.js:220
        let match = NORMALIZE_TAG_REGEX.exec(tag_buffer);
                                        ^
RangeError: Maximum call stack size exceeded
    at RegExp.exec (<anonymous>)
    at normalize_tag (/Users/thor/node/src/pixelpixel/sunshinecoast/node_modules/@kontent-ai/delivery-node-parser/node_modules/striptags/src/striptags.js:220:41)
    at striptags_internal (/Users/thor/node/src/pixelpixel/sunshinecoast/node_modules/@kontent-ai/delivery-node-parser/node_modules/striptags/src/striptags.js:118:48)
    at striptags (/Users/thor/node/src/pixelpixel/sunshinecoast/node_modules/@kontent-ai/delivery-node-parser/node_modules/striptags/src/striptags.js:28:16)
    at convertToParserElement (/Users/thor/node/src/pixelpixel/sunshinecoast/node_modules/@kontent-ai/delivery-node-parser/lib/parser/implementation/shared.ts:160:15)
    at convertToParserElement (/Users/thor/node/src/pixelpixel/sunshinecoast/node_modules/@kontent-ai/delivery-node-parser/lib/parser/implementation/shared.ts:163:15)
    at NodeParser.processLink (/Users/thor/node/src/pixelpixel/sunshinecoast/node_modules/@kontent-ai/delivery-node-parser/lib/parser/implementation/node-sync-parser.ts:243:35)
    at NodeParser.processNodes (/Users/thor/node/src/pixelpixel/sunshinecoast/node_modules/@kontent-ai/delivery-node-parser/lib/parser/implementation/node-sync-parser.ts:115:26)
    at NodeParser.processNodes (/Users/thor/node/src/pixelpixel/sunshinecoast/node_modules/@kontent-ai/delivery-node-parser/lib/parser/implementation/node-sync-parser.ts:143:26)
    at NodeParser.processNodes (/Users/thor/node/src/pixelpixel/sunshinecoastnode_modules/@kontent-ai/delivery-node-parser/lib/parser/implementation/node-sync-parser.ts:143:26)
Enngage commented 1 year ago

Hi @tveimo,

There are 2 issues with your code - you should either use linkHtml or linkUrl, but not both. However, the main reason this ends up in infinite recursion exception is because you 'preserving' the data-item-id="${linkId}" which causes the parser to resolve it infinitely in recursion. You should omit this in your resolved link. See working sample at https://stackblitz.com/edit/stackblitz-starters-txywxr?description=Starter%20project%20for%20Node.js,%20a%20JavaScript%20runtime%20built%20on%20Chrome%27s%20V8%20JavaScript%20engine&file=index.js,package.json&title=node.new%20Starter

tveimo commented 1 year ago

Thanks!