@netlify/edge-bundler 11.2.0 → 11.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -17,5 +17,10 @@ type DeclarationWithPattern = BaseDeclaration & {
17
17
  };
18
18
  export type Declaration = DeclarationWithPath | DeclarationWithPattern;
19
19
  export declare const mergeDeclarations: (tomlDeclarations: Declaration[], userFunctionsConfig: Record<string, FunctionConfig>, internalFunctionsConfig: Record<string, FunctionConfig>, deployConfigDeclarations: Declaration[], _featureFlags?: FeatureFlags) => Declaration[];
20
- export declare const parsePattern: (pattern: string) => string;
20
+ /**
21
+ * Normalizes a regular expression, ensuring it has a leading `^` and trailing
22
+ * `$` characters. It also converts the regular expression from PCRE to RE2 if
23
+ * needed.
24
+ */
25
+ export declare const parsePattern: (pattern: string, pcreRegexpEngine: boolean) => string;
21
26
  export {};
@@ -66,15 +66,29 @@ const createDeclarationsFromFunctionConfigs = (functionConfigs, functionsVisited
66
66
  }
67
67
  return declarations;
68
68
  };
69
- // Validates and normalizes a pattern so that it's a valid regular expression
70
- // in Go, which is the engine used by our edge nodes.
71
- export const parsePattern = (pattern) => {
69
+ /**
70
+ * Normalizes a regular expression, ensuring it has a leading `^` and trailing
71
+ * `$` characters. It also converts the regular expression from PCRE to RE2 if
72
+ * needed.
73
+ */
74
+ export const parsePattern = (pattern, pcreRegexpEngine) => {
72
75
  let enclosedPattern = pattern;
73
- if (!pattern.startsWith('^'))
76
+ if (!pattern.startsWith('^')) {
74
77
  enclosedPattern = `^${enclosedPattern}`;
75
- if (!pattern.endsWith('$'))
78
+ }
79
+ if (!pattern.endsWith('$')) {
76
80
  enclosedPattern = `${enclosedPattern}$`;
81
+ }
77
82
  const regexp = new RegExp(enclosedPattern);
83
+ const regexpString = pcreRegexpEngine ? regexp.toString() : transformPCRERegexp(regexp);
84
+ // Strip leading and forward slashes.
85
+ return regexpString.slice(1, -1);
86
+ };
87
+ /**
88
+ * Transforms a PCRE regular expression into a RE2 expression, compatible
89
+ * with the Go engine used in our edge nodes.
90
+ */
91
+ const transformPCRERegexp = (regexp) => {
78
92
  const newRegexp = regexpAST.transform(regexp, {
79
93
  Assertion(path) {
80
94
  // Lookaheads are not supported. If we find one, throw an error.
@@ -95,6 +109,5 @@ export const parsePattern = (pattern) => {
95
109
  }
96
110
  },
97
111
  });
98
- // Strip leading and forward slashes.
99
- return newRegexp.toString().slice(1, -1);
112
+ return newRegexp.toString();
100
113
  };
@@ -117,18 +117,18 @@ test('netlify.toml-defined excludedPath are respected', () => {
117
117
  test('Does not escape front slashes in a regex pattern if they are already escaped', () => {
118
118
  const regexPattern = '^(?:\\/(_next\\/data\\/[^/]{1,}))?(?:\\/([^/.]{1,}))\\/shows(?:\\/(.*))(.json)?[\\/#\\?]?$';
119
119
  const expected = '^(?:\\/(_next\\/data\\/[^/]{1,}))?(?:\\/([^/.]{1,}))\\/shows(?:\\/(.*))(.json)?[\\/#\\?]?$';
120
- const actual = parsePattern(regexPattern);
121
- expect(actual).toEqual(expected);
120
+ expect(parsePattern(regexPattern, false)).toEqual(expected);
121
+ expect(parsePattern(regexPattern, true)).toEqual(expected);
122
122
  });
123
123
  test('Escapes front slashes in a regex pattern', () => {
124
124
  const regexPattern = '^(?:/(_next/data/[^/]{1,}))?(?:/([^/.]{1,}))/shows(?:/(.*))(.json)?[/#\\?]?$';
125
125
  const expected = '^(?:\\/(_next\\/data\\/[^/]{1,}))?(?:\\/([^/.]{1,}))\\/shows(?:\\/(.*))(.json)?[/#\\?]?$';
126
- const actual = parsePattern(regexPattern);
127
- expect(actual).toEqual(expected);
126
+ expect(parsePattern(regexPattern, false)).toEqual(expected);
127
+ expect(parsePattern(regexPattern, true)).toEqual(expected);
128
128
  });
129
129
  test('Ensures pattern match on the whole path', () => {
130
130
  const regexPattern = '/foo/.*/bar';
131
131
  const expected = '^\\/foo\\/.*\\/bar$';
132
- const actual = parsePattern(regexPattern);
133
- expect(actual).toEqual(expected);
132
+ expect(parsePattern(regexPattern, false)).toEqual(expected);
133
+ expect(parsePattern(regexPattern, true)).toEqual(expected);
134
134
  });
@@ -137,11 +137,8 @@ const pathToRegularExpression = (path) => {
137
137
  };
138
138
  const getRegularExpression = (declaration, pcreRegexpEngine) => {
139
139
  if ('pattern' in declaration) {
140
- if (pcreRegexpEngine) {
141
- return declaration.pattern;
142
- }
143
140
  try {
144
- return parsePattern(declaration.pattern);
141
+ return parsePattern(declaration.pattern, pcreRegexpEngine);
145
142
  }
146
143
  catch (error) {
147
144
  throw wrapBundleError(new Error(`Could not parse path declaration of function '${declaration.function}': ${error.message}`));
@@ -154,12 +151,9 @@ const getExcludedRegularExpressions = (declaration, pcreRegexpEngine) => {
154
151
  const excludedPatterns = Array.isArray(declaration.excludedPattern)
155
152
  ? declaration.excludedPattern
156
153
  : [declaration.excludedPattern];
157
- if (pcreRegexpEngine) {
158
- return excludedPatterns;
159
- }
160
154
  return excludedPatterns.map((excludedPattern) => {
161
155
  try {
162
- return parsePattern(excludedPattern);
156
+ return parsePattern(excludedPattern, pcreRegexpEngine);
163
157
  }
164
158
  catch (error) {
165
159
  throw wrapBundleError(new Error(`Could not parse path declaration of function '${declaration.function}': ${error.message}`));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@netlify/edge-bundler",
3
- "version": "11.2.0",
3
+ "version": "11.2.1",
4
4
  "description": "Intelligently prepare Netlify Edge Functions for deployment",
5
5
  "type": "module",
6
6
  "main": "./dist/node/index.js",