- 
                Notifications
    You must be signed in to change notification settings 
- Fork 178
refactor: use utility for cross-platform path regex construction #701
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
          
     Merged
      
      
            conico974
  merged 11 commits into
  opennextjs:main
from
james-elicx:james/cross-platform-path-utility
  
      
      
   
  Jan 17, 2025 
      
    
  
     Merged
                    Changes from all commits
      Commits
    
    
            Show all changes
          
          
            11 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      6a6eadb
              
                create utility
              
              
                james-elicx d1e63c5
              
                replace usage with the utility
              
              
                james-elicx cef92ca
              
                changeset
              
              
                james-elicx 3380a07
              
                fix formatting
              
              
                james-elicx b71a91d
              
                add comment
              
              
                james-elicx f125552
              
                add some additional special chars and use string.raw
              
              
                james-elicx 3a5c6fd
              
                add extra tests
              
              
                james-elicx b6bc25d
              
                add another string.raw
              
              
                james-elicx 76554cc
              
                use string.raw in comment example too
              
              
                james-elicx f46f4bd
              
                Update packages/open-next/src/utils/regex.ts
              
              
                james-elicx 8d603ae
              
                change options
              
              
                james-elicx File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      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
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@opennextjs/aws": patch | ||
| --- | ||
|  | ||
| refactor: use utility for cross-platform path regex construction | 
  
    
      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
    
  
  
    
              
              
  
    
      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
    
  
  
    
              
              
  
    
      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
    
  
  
    
              
  
    
      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
    
  
  
    
              
              
  
    
      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
    
  
  
    
              
              
  
    
      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
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| type Options = { | ||
| escape?: boolean; | ||
| flags?: string; | ||
| }; | ||
|  | ||
| /** | ||
| * Constructs a regular expression for a path that supports separators for multiple platforms | ||
| * - Uses posix separators (`/`) as the input that should be made cross-platform. | ||
| * - Special characters are escaped by default but can be controlled through opts.escape. | ||
| * - Posix separators are always escaped. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * getCrossPlatformPathRegex("./middleware.mjs") | ||
| * getCrossPlatformPathRegex(String.raw`\./middleware\.(mjs|cjs)`, { escape: false }) | ||
| * ``` | ||
| */ | ||
| export function getCrossPlatformPathRegex( | ||
|         
                  james-elicx marked this conversation as resolved.
              Show resolved
            Hide resolved | ||
| regex: string, | ||
| { escape: shouldEscape = true, flags = "g" }: Options = {}, | ||
| ) { | ||
| const newExpr = ( | ||
| shouldEscape ? regex.replace(/([[\]().*+?^$|{}\\])/g, "\\$1") : regex | ||
| ).replaceAll("/", String.raw`(?:\/|\\)`); | ||
|  | ||
| return new RegExp(newExpr, flags); | ||
| } | ||
  
    
      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
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| import { getCrossPlatformPathRegex } from "@opennextjs/aws/utils/regex.js"; | ||
|  | ||
| const specialChars = "^([123]+|[123]{1,3})*\\?$"; | ||
|  | ||
| describe("getCrossPlatformPathRegex", () => { | ||
|         
                  james-elicx marked this conversation as resolved.
              Show resolved
            Hide resolved | ||
| it("should return a regex without escaping characters", () => { | ||
| const regexp = getCrossPlatformPathRegex(specialChars, { escape: false }); | ||
| expect(regexp.source).toEqual(specialChars); | ||
| }); | ||
|  | ||
| it("should always create cross-platform separators", () => { | ||
| [true, false].forEach((v) => { | ||
| const regexp = getCrossPlatformPathRegex("test/path", { escape: v }); | ||
| expect(regexp.source).toEqual(String.raw`test(?:\/|\\)path`); | ||
| }); | ||
| }); | ||
|  | ||
| it("should return a regex with escaped characters", () => { | ||
| const regexp = getCrossPlatformPathRegex(specialChars, { escape: true }); | ||
| expect(regexp.source).toEqual( | ||
| String.raw`\^\(\[123\]\+\|\[123\]\{1,3\}\)\*\\\?\$`, | ||
| ); | ||
| }); | ||
|  | ||
| it("should return cross-platform paths with escaped special characters", () => { | ||
| [ | ||
| ["core/resolve.js", String.raw`core(?:\/|\\)resolve\.js`], | ||
| ["./middleware.mjs", String.raw`\.(?:\/|\\)middleware\.mjs`], | ||
| ].forEach(([input, output]) => | ||
| expect(getCrossPlatformPathRegex(input).source).toEqual(output), | ||
| ); | ||
| }); | ||
|  | ||
| it("should return cross-platform paths without escaping special characters", () => { | ||
| const regex = getCrossPlatformPathRegex( | ||
| String.raw`\./middleware\.(mjs|cjs)`, | ||
| { escape: false }, | ||
| ); | ||
| expect(regex.source).toEqual(String.raw`\.(?:\/|\\)middleware\.(mjs|cjs)`); | ||
| }); | ||
| }); | ||
      
      Oops, something went wrong.
        
    
  
  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.
  
    
  
    
Uh oh!
There was an error while loading. Please reload this page.