-
Notifications
You must be signed in to change notification settings - Fork 143
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: use utility for cross-platform path regex construction (#701)
* create utility * replace usage with the utility * changeset * fix formatting * add comment * add some additional special chars and use string.raw * add extra tests * add another string.raw * use string.raw in comment example too * Update packages/open-next/src/utils/regex.ts * change options
- Loading branch information
1 parent
25d317c
commit 00ce837
Showing
8 changed files
with
121 additions
and
36 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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( | ||
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 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", () => { | ||
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)`); | ||
}); | ||
}); |