diff --git a/node/declaration.test.ts b/node/declaration.test.ts index ca700b26..59f59016 100644 --- a/node/declaration.test.ts +++ b/node/declaration.test.ts @@ -1,7 +1,7 @@ import { test, expect } from 'vitest' import { FunctionConfig } from './config.js' -import { Declaration, mergeDeclarations } from './declaration.js' +import { Declaration, mergeDeclarations, parsePattern } from './declaration.js' const deployConfigDeclarations: Declaration[] = [] @@ -191,3 +191,19 @@ test('netlify.toml-defined excludedPath are respected', () => { expect(declarations).toEqual(expectedDeclarations) }) + +test('Does not escape front slashes in a regex pattern if they are already escaped', () => { + const regexPattern = '^(?:\\/(_next\\/data\\/[^/]{1,}))?(?:\\/([^/.]{1,}))\\/shows(?:\\/(.*))(.json)?[\\/#\\?]?$' + const expected = '^(?:\\/(_next\\/data\\/[^\\/]{1,}))?(?:\\/([^\\/.]{1,}))\\/shows(?:\\/(.*))(.json)?[\\/#\\?]?$' + const actual = parsePattern(regexPattern) + + expect(actual).toEqual(expected) +}) + +test('Escapes front slashes in a regex pattern', () => { + const regexPattern = '^(?:/(_next/data/[^/]{1,}))?(?:/([^/.]{1,}))/shows(?:/(.*))(.json)?[/#\\?]?$' + const expected = '^(?:\\/(_next\\/data\\/[^\\/]{1,}))?(?:\\/([^\\/.]{1,}))\\/shows(?:\\/(.*))(.json)?[\\/#\\?]?$' + const actual = parsePattern(regexPattern) + + expect(actual).toEqual(expected) +}) diff --git a/node/declaration.ts b/node/declaration.ts index 143645af..9e284c3f 100644 --- a/node/declaration.ts +++ b/node/declaration.ts @@ -130,8 +130,9 @@ const createDeclarationsFromFunctionConfigs = ( // Validates and normalizes a pattern so that it's a valid regular expression // in Go, which is the engine used by our edge nodes. export const parsePattern = (pattern: string) => { - // Escaping forward slashes with back slashes. - const normalizedPattern = pattern.replace(/\//g, '\\/') + // Only escape front slashes if they haven't been escaped already. + const normalizedPattern = pattern.replace(/(?