diff options
Diffstat (limited to 'packages/integrations/cloudflare/src/utils/deduplicatePatterns.ts')
-rw-r--r-- | packages/integrations/cloudflare/src/utils/deduplicatePatterns.ts | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/packages/integrations/cloudflare/src/utils/deduplicatePatterns.ts b/packages/integrations/cloudflare/src/utils/deduplicatePatterns.ts new file mode 100644 index 000000000..37743fe55 --- /dev/null +++ b/packages/integrations/cloudflare/src/utils/deduplicatePatterns.ts @@ -0,0 +1,26 @@ +/** + * Remove duplicates and redundant patterns from an `include` or `exclude` list. + * Otherwise Cloudflare will throw an error on deployment. Plus, it saves more entries. + * E.g. `['/foo/*', '/foo/*', '/foo/bar'] => ['/foo/*']` + * @param patterns a list of `include` or `exclude` patterns + * @returns a deduplicated list of patterns + */ +export function deduplicatePatterns(patterns: string[]) { + const openPatterns: RegExp[] = []; + + // A value in the set may only occur once; it is unique in the set's collection. + // ref: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set + return [...new Set(patterns)] + .sort((a, b) => a.length - b.length) + .filter((pattern) => { + if (openPatterns.some((p) => p.test(pattern))) { + return false; + } + + if (pattern.endsWith('*')) { + openPatterns.push(new RegExp(`^${pattern.replace(/(\*\/)*\*$/g, '.*')}`)); + } + + return true; + }); +} |