Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RangeError: Maximum call stack size exceeded when coloring already colored long string #63

Closed
hi-ogawa opened this issue May 10, 2024 · 0 comments · Fixed by #64
Closed

Comments

@hi-ogawa
Copy link
Contributor

Due to the recursive implementation of replaceColors:

picocolors/picocolors.js

Lines 21 to 26 in b626148

let replaceClose = (string, close, replace, index) => {
let start = string.substring(0, index) + replace
let end = string.substring(index + close.length)
let nextIndex = end.indexOf(close)
return ~nextIndex ? start + replaceClose(end, close, replace, nextIndex) : start + end
}

picocolors tends to throw maximum stack error when it's used with already colored long string such as error messages coming from dom-testing-library. Here are few known issues in down stream:

And here is a simple reproduction:
https://github.com/hi-ogawa/reproductions/tree/main/picocolors-repalceClose-stackoverflow

import c from "picocolors";
console.log(c.blue(c.red("x").repeat(10000)));
$ node repro.mjs
(...dir...)/node_modules/picocolors/picocolors.js:22
        let start = string.substring(0, index) + replace
                           ^
RangeError: Maximum call stack size exceeded
    at replaceClose ((...dir...)/node_modules/picocolors/picocolors.js:22:21)
    at replaceClose ((...dir...)/node_modules/picocolors/picocolors.js:25:30)
....

It's certainly possible for down stream projects to workaround this. However, I'm wondering if you would consider rewriting recursive replaceClose with iterative implementation so that picocolors would be free from call stack error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant