-
Notifications
You must be signed in to change notification settings - Fork 41
Description
I have an API that runs in nodejs, and regexp matchers fail to run properly. I figured something is making instanceof RegExp to fail. If I do the following from the cli:
node
> anymatch([/conv/], 'conversations') // => trueeverything is working as intended, but in the app console:
> anymatch([/conv/], 'conversations') // => falseI managed to isolate the issue to this line:
Line 30 in 0aeecc2
| if (matcher instanceof RegExp) { |
it seems like that is producing false even if it is a regexp. So what I did was install another small lib, 'kind-of' and do this on that line and everything works fine again.
// from this ...
if (matcher instanceof RegExp) { //...
// to ...
if (kindOf(matcher) === 'regexp') { //...This resolves that issue.
I know this issue is not related to the lib per se, but I have tried to isolate code in the API and it seems like there is something deep in some lib in npm that is causing this issue on the RegExp class. It seems time wasting and futile to try and correct it that way, rather than make this lib detect the regexp another way. Will update if I somehow find out what is causing it, coz that in itself is a huge issue that something is messing with the Regexp prototype and causing this issue.
Note that most of the libs used in the backend and frontend are similar, and anymatch runs well in the browser ( we share code in the back and front ends of the whole stack) in which anymatch is used. So this leaves server specific libs faulty, of which there is nothing we are doing to mess with the Regxp prototype.
However, the reason why I am letting you guys know about this is that this lib may suffer with this bug just because of other libs that mess that prototype, meaning it could probably be common in many apps and people just don't know this as the cause. So instead, this lib should just avoid that pitfall and use another way to detect regexps, or just directly do this in this lib:
// shamelessly steal this util function to this lib 😉 beauty of open-source
function isRegexp(val) {
if (val instanceof RegExp) return true;
return typeof val.flags === 'string'
&& typeof val.ignoreCase === 'boolean'
&& typeof val.multiline === 'boolean'
&& typeof val.global === 'boolean';
}
// then use it on that offending line
if (isRegexp(matcher)) { //...From the above code in kind-of, it clearly shows that, yes, they understood that instanceof is not perfect, it will fall through somehow and they then make it brutally get the type by checking known RegExp props. This modification will not impact performance under normal circumstances, just that it will make this lib more RELIABLE, though it will take just a few lines of performance hit when RegExp prototype is not working correctly with instanceof.