Catch path-to-regexp errors and fall through to minimatch#227
Open
linpengzhang wants to merge 1 commit intovercel:mainfrom
Open
Catch path-to-regexp errors and fall through to minimatch#227linpengzhang wants to merge 1 commit intovercel:mainfrom
linpengzhang wants to merge 1 commit intovercel:mainfrom
Conversation
The `sourceMatches` function tries path-to-regexp first and falls back to minimatch. However, if the source pattern uses syntax that path-to-regexp cannot parse (e.g. extglob negation like `**/!(*.css|*.js)`), path-to-regexp throws an exception instead of returning null. This prevents the minimatch fallback from ever running. Wrap the path-to-regexp block in a try/catch so that incompatible patterns gracefully fall through to minimatch, which does support them. Made-with: Cursor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The
sourceMatchesfunction triespath-to-regexpfirst and falls back tominimatchfor pattern matching. However, if the source pattern uses syntax thatpath-to-regexpcannot parse (e.g. extglob negation like**/!(*.css|*.js)),path-to-regexpthrows an exception instead of returningnull. This prevents theminimatchfallback from ever running.This PR wraps the
path-to-regexpblock in a try/catch so that incompatible patterns gracefully fall through tominimatch, which does support them.The problem
For a rewrite config like:
{ "source": "**/!(*.css|*.js)", "destination": "/index.html" }.replace('*', '(.*)')mangles the pattern into(.*)*/!(*.css|*.js)pathToRegExptries to compile this and throws:Invalid regular expression: Nothing to repeatminimatch(which handles this pattern correctly) is never calledserveHandler()promise rejectsThis caused a production incident for us where the asset server silently failed to respond to any HTTP request, because the unhandled rejection meant no response was ever sent.
The fix
Wrap lines 46-56 in a try/catch. If
path-to-regexpthrows, clear the keys array and fall through to theminimatchcheck on line 59, which is what the code already intends as a fallback.Tests
Added an integration test that verifies a rewrite with an extglob negation pattern works correctly (previously this would have thrown).
All 67 tests pass.
Made with Cursor