@vercel/routing-utils 2.2.1 → 3.1.0

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.
package/dist/index.js CHANGED
@@ -1,318 +1,375 @@
1
1
  "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
15
17
  };
16
- Object.defineProperty(exports, "__esModule", { value: true });
17
- exports.getTransformedRoutes = exports.normalizeRoutes = exports.isValidHandleValue = exports.isHandler = exports.getCleanUrls = exports.mergeRoutes = exports.appendRoutesToPhase = void 0;
18
- const url_1 = require("url");
19
- const superstatic_1 = require("./superstatic");
20
- var append_1 = require("./append");
21
- Object.defineProperty(exports, "appendRoutesToPhase", { enumerable: true, get: function () { return append_1.appendRoutesToPhase; } });
22
- var merge_1 = require("./merge");
23
- Object.defineProperty(exports, "mergeRoutes", { enumerable: true, get: function () { return merge_1.mergeRoutes; } });
24
- __exportStar(require("./schemas"), exports);
25
- var superstatic_2 = require("./superstatic");
26
- Object.defineProperty(exports, "getCleanUrls", { enumerable: true, get: function () { return superstatic_2.getCleanUrls; } });
27
- __exportStar(require("./types"), exports);
18
+ var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
19
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
20
+ var src_exports = {};
21
+ __export(src_exports, {
22
+ appendRoutesToPhase: () => import_append.appendRoutesToPhase,
23
+ getCleanUrls: () => import_superstatic2.getCleanUrls,
24
+ getTransformedRoutes: () => getTransformedRoutes,
25
+ isHandler: () => isHandler,
26
+ isValidHandleValue: () => isValidHandleValue,
27
+ mergeRoutes: () => import_merge.mergeRoutes,
28
+ normalizeRoutes: () => normalizeRoutes
29
+ });
30
+ module.exports = __toCommonJS(src_exports);
31
+ var import_url = require("url");
32
+ var import_superstatic = require("./superstatic");
33
+ var import_append = require("./append");
34
+ var import_merge = require("./merge");
35
+ __reExport(src_exports, require("./schemas"), module.exports);
36
+ var import_superstatic2 = require("./superstatic");
37
+ __reExport(src_exports, require("./types"), module.exports);
28
38
  const VALID_HANDLE_VALUES = [
29
- 'filesystem',
30
- 'hit',
31
- 'miss',
32
- 'rewrite',
33
- 'error',
34
- 'resource',
39
+ "filesystem",
40
+ "hit",
41
+ "miss",
42
+ "rewrite",
43
+ "error",
44
+ "resource"
35
45
  ];
36
46
  const validHandleValues = new Set(VALID_HANDLE_VALUES);
37
47
  function isHandler(route) {
38
- return typeof route.handle !== 'undefined';
48
+ return typeof route.handle !== "undefined";
39
49
  }
40
- exports.isHandler = isHandler;
41
50
  function isValidHandleValue(handle) {
42
- return validHandleValues.has(handle);
51
+ return validHandleValues.has(handle);
43
52
  }
44
- exports.isValidHandleValue = isValidHandleValue;
45
53
  function normalizeRoutes(inputRoutes) {
46
- if (!inputRoutes || inputRoutes.length === 0) {
47
- return { routes: inputRoutes, error: null };
48
- }
49
- const routes = [];
50
- const handling = [];
51
- const errors = [];
52
- inputRoutes.forEach((r, i) => {
53
- const route = { ...r };
54
- routes.push(route);
55
- const keys = Object.keys(route);
56
- if (isHandler(route)) {
57
- const { handle } = route;
58
- if (keys.length !== 1) {
59
- const unknownProp = keys.find(prop => prop !== 'handle');
60
- errors.push(`Route at index ${i} has unknown property \`${unknownProp}\`.`);
61
- }
62
- else if (!isValidHandleValue(handle)) {
63
- errors.push(`Route at index ${i} has unknown handle value \`handle: ${handle}\`.`);
64
- }
65
- else if (handling.includes(handle)) {
66
- errors.push(`Route at index ${i} is a duplicate. Please use one \`handle: ${handle}\` at most.`);
67
- }
68
- else {
69
- handling.push(handle);
70
- }
54
+ if (!inputRoutes || inputRoutes.length === 0) {
55
+ return { routes: inputRoutes, error: null };
56
+ }
57
+ const routes = [];
58
+ const handling = [];
59
+ const errors = [];
60
+ inputRoutes.forEach((r, i) => {
61
+ const route = { ...r };
62
+ routes.push(route);
63
+ const keys = Object.keys(route);
64
+ if (isHandler(route)) {
65
+ const { handle } = route;
66
+ if (keys.length !== 1) {
67
+ const unknownProp = keys.find((prop) => prop !== "handle");
68
+ errors.push(
69
+ `Route at index ${i} has unknown property \`${unknownProp}\`.`
70
+ );
71
+ } else if (!isValidHandleValue(handle)) {
72
+ errors.push(
73
+ `Route at index ${i} has unknown handle value \`handle: ${handle}\`.`
74
+ );
75
+ } else if (handling.includes(handle)) {
76
+ errors.push(
77
+ `Route at index ${i} is a duplicate. Please use one \`handle: ${handle}\` at most.`
78
+ );
79
+ } else {
80
+ handling.push(handle);
81
+ }
82
+ } else if (route.src) {
83
+ if (!route.src.startsWith("^")) {
84
+ route.src = `^${route.src}`;
85
+ }
86
+ if (!route.src.endsWith("$")) {
87
+ route.src = `${route.src}$`;
88
+ }
89
+ route.src = route.src.replace(/\\\//g, "/");
90
+ const regError = checkRegexSyntax("Route", i, route.src);
91
+ if (regError) {
92
+ errors.push(regError);
93
+ }
94
+ const handleValue = handling[handling.length - 1];
95
+ if (handleValue === "hit") {
96
+ if (route.dest) {
97
+ errors.push(
98
+ `Route at index ${i} cannot define \`dest\` after \`handle: hit\`.`
99
+ );
71
100
  }
72
- else if (route.src) {
73
- // Route src should always start with a '^'
74
- if (!route.src.startsWith('^')) {
75
- route.src = `^${route.src}`;
76
- }
77
- // Route src should always end with a '$'
78
- if (!route.src.endsWith('$')) {
79
- route.src = `${route.src}$`;
80
- }
81
- // Route src should strip escaped forward slash, its not special
82
- route.src = route.src.replace(/\\\//g, '/');
83
- const regError = checkRegexSyntax('Route', i, route.src);
84
- if (regError) {
85
- errors.push(regError);
86
- }
87
- // The last seen handling is the current handler
88
- const handleValue = handling[handling.length - 1];
89
- if (handleValue === 'hit') {
90
- if (route.dest) {
91
- errors.push(`Route at index ${i} cannot define \`dest\` after \`handle: hit\`.`);
92
- }
93
- if (route.status) {
94
- errors.push(`Route at index ${i} cannot define \`status\` after \`handle: hit\`.`);
95
- }
96
- if (!route.continue) {
97
- errors.push(`Route at index ${i} must define \`continue: true\` after \`handle: hit\`.`);
98
- }
99
- }
100
- else if (handleValue === 'miss') {
101
- if (route.dest && !route.check) {
102
- errors.push(`Route at index ${i} must define \`check: true\` after \`handle: miss\`.`);
103
- }
104
- else if (!route.dest && !route.continue) {
105
- errors.push(`Route at index ${i} must define \`continue: true\` after \`handle: miss\`.`);
106
- }
107
- }
101
+ if (route.status) {
102
+ errors.push(
103
+ `Route at index ${i} cannot define \`status\` after \`handle: hit\`.`
104
+ );
108
105
  }
109
- else {
110
- errors.push(`Route at index ${i} must define either \`handle\` or \`src\` property.`);
106
+ if (!route.continue) {
107
+ errors.push(
108
+ `Route at index ${i} must define \`continue: true\` after \`handle: hit\`.`
109
+ );
111
110
  }
112
- });
113
- const error = errors.length > 0
114
- ? createError('invalid_route', errors, 'https://vercel.link/routes-json', 'Learn More')
115
- : null;
116
- return { routes, error };
111
+ } else if (handleValue === "miss") {
112
+ if (route.dest && !route.check) {
113
+ errors.push(
114
+ `Route at index ${i} must define \`check: true\` after \`handle: miss\`.`
115
+ );
116
+ } else if (!route.dest && !route.continue) {
117
+ errors.push(
118
+ `Route at index ${i} must define \`continue: true\` after \`handle: miss\`.`
119
+ );
120
+ }
121
+ }
122
+ } else {
123
+ errors.push(
124
+ `Route at index ${i} must define either \`handle\` or \`src\` property.`
125
+ );
126
+ }
127
+ });
128
+ const error = errors.length > 0 ? createError(
129
+ "invalid_route",
130
+ errors,
131
+ "https://vercel.link/routes-json",
132
+ "Learn More"
133
+ ) : null;
134
+ return { routes, error };
117
135
  }
118
- exports.normalizeRoutes = normalizeRoutes;
119
136
  function checkRegexSyntax(type, index, src) {
120
- try {
121
- new RegExp(src);
122
- }
123
- catch (err) {
124
- const prop = type === 'Route' ? 'src' : 'source';
125
- return `${type} at index ${index} has invalid \`${prop}\` regular expression "${src}".`;
126
- }
127
- return null;
137
+ try {
138
+ new RegExp(src);
139
+ } catch (err) {
140
+ const prop = type === "Route" ? "src" : "source";
141
+ return `${type} at index ${index} has invalid \`${prop}\` regular expression "${src}".`;
142
+ }
143
+ return null;
128
144
  }
129
- function checkPatternSyntax(type, index, { source, destination, has, }) {
130
- let sourceSegments = new Set();
131
- const destinationSegments = new Set();
145
+ function checkPatternSyntax(type, index, {
146
+ source,
147
+ destination,
148
+ has
149
+ }) {
150
+ let sourceSegments = /* @__PURE__ */ new Set();
151
+ const destinationSegments = /* @__PURE__ */ new Set();
152
+ try {
153
+ sourceSegments = new Set((0, import_superstatic.sourceToRegex)(source).segments);
154
+ } catch (err) {
155
+ return {
156
+ message: `${type} at index ${index} has invalid \`source\` pattern "${source}".`,
157
+ link: "https://vercel.link/invalid-route-source-pattern"
158
+ };
159
+ }
160
+ if (destination) {
132
161
  try {
133
- sourceSegments = new Set((0, superstatic_1.sourceToRegex)(source).segments);
162
+ const { hostname, pathname, query } = (0, import_url.parse)(destination, true);
163
+ (0, import_superstatic.sourceToRegex)(hostname || "").segments.forEach(
164
+ (name) => destinationSegments.add(name)
165
+ );
166
+ (0, import_superstatic.sourceToRegex)(pathname || "").segments.forEach(
167
+ (name) => destinationSegments.add(name)
168
+ );
169
+ for (const strOrArray of Object.values(query)) {
170
+ const value = Array.isArray(strOrArray) ? strOrArray[0] : strOrArray;
171
+ (0, import_superstatic.sourceToRegex)(value || "").segments.forEach(
172
+ (name) => destinationSegments.add(name)
173
+ );
174
+ }
175
+ } catch (err) {
134
176
  }
135
- catch (err) {
177
+ const hasSegments = (0, import_superstatic.collectHasSegments)(has);
178
+ for (const segment of destinationSegments) {
179
+ if (!sourceSegments.has(segment) && !hasSegments.includes(segment)) {
136
180
  return {
137
- message: `${type} at index ${index} has invalid \`source\` pattern "${source}".`,
138
- link: 'https://vercel.link/invalid-route-source-pattern',
181
+ message: `${type} at index ${index} has segment ":${segment}" in \`destination\` property but not in \`source\` or \`has\` property.`,
182
+ link: "https://vercel.link/invalid-route-destination-segment"
139
183
  };
184
+ }
140
185
  }
141
- if (destination) {
142
- try {
143
- const { hostname, pathname, query } = (0, url_1.parse)(destination, true);
144
- (0, superstatic_1.sourceToRegex)(hostname || '').segments.forEach(name => destinationSegments.add(name));
145
- (0, superstatic_1.sourceToRegex)(pathname || '').segments.forEach(name => destinationSegments.add(name));
146
- for (const strOrArray of Object.values(query)) {
147
- const value = Array.isArray(strOrArray) ? strOrArray[0] : strOrArray;
148
- (0, superstatic_1.sourceToRegex)(value || '').segments.forEach(name => destinationSegments.add(name));
149
- }
150
- }
151
- catch (err) {
152
- // Since checkPatternSyntax() is a validation helper, we don't want to
153
- // replicate all possible URL parsing here so we consume the error.
154
- // If this really is an error, we'll throw later in convertRedirects().
155
- }
156
- const hasSegments = (0, superstatic_1.collectHasSegments)(has);
157
- for (const segment of destinationSegments) {
158
- if (!sourceSegments.has(segment) && !hasSegments.includes(segment)) {
159
- return {
160
- message: `${type} at index ${index} has segment ":${segment}" in \`destination\` property but not in \`source\` or \`has\` property.`,
161
- link: 'https://vercel.link/invalid-route-destination-segment',
162
- };
163
- }
164
- }
165
- }
166
- return null;
186
+ }
187
+ return null;
167
188
  }
168
189
  function checkRedirect(r, index) {
169
- if (typeof r.permanent !== 'undefined' &&
170
- typeof r.statusCode !== 'undefined') {
171
- return `Redirect at index ${index} cannot define both \`permanent\` and \`statusCode\` properties.`;
172
- }
173
- return null;
190
+ if (typeof r.permanent !== "undefined" && typeof r.statusCode !== "undefined") {
191
+ return `Redirect at index ${index} cannot define both \`permanent\` and \`statusCode\` properties.`;
192
+ }
193
+ return null;
174
194
  }
175
195
  function createError(code, allErrors, link, action) {
176
- const errors = Array.isArray(allErrors) ? allErrors : [allErrors];
177
- const message = errors[0];
178
- const error = {
179
- name: 'RouteApiError',
180
- code,
181
- message,
182
- link,
183
- action,
184
- errors,
185
- };
186
- return error;
196
+ const errors = Array.isArray(allErrors) ? allErrors : [allErrors];
197
+ const message = errors[0];
198
+ const error = {
199
+ name: "RouteApiError",
200
+ code,
201
+ message,
202
+ link,
203
+ action,
204
+ errors
205
+ };
206
+ return error;
187
207
  }
188
208
  function notEmpty(value) {
189
- return value !== null && value !== undefined;
209
+ return value !== null && value !== void 0;
190
210
  }
191
211
  function getTransformedRoutes(vercelConfig) {
192
- const { cleanUrls, rewrites, redirects, headers, trailingSlash } = vercelConfig;
193
- let { routes = null } = vercelConfig;
194
- if (routes) {
195
- const hasNewProperties = typeof cleanUrls !== 'undefined' ||
196
- typeof trailingSlash !== 'undefined' ||
197
- typeof redirects !== 'undefined' ||
198
- typeof headers !== 'undefined' ||
199
- typeof rewrites !== 'undefined';
200
- if (hasNewProperties) {
201
- const error = createError('invalid_mixed_routes', 'If `rewrites`, `redirects`, `headers`, `cleanUrls` or `trailingSlash` are used, then `routes` cannot be present.', 'https://vercel.link/mix-routing-props', 'Learn More');
202
- return { routes, error };
203
- }
204
- return normalizeRoutes(routes);
212
+ const { cleanUrls, rewrites, redirects, headers, trailingSlash } = vercelConfig;
213
+ let { routes = null } = vercelConfig;
214
+ if (routes) {
215
+ const hasNewProperties = typeof cleanUrls !== "undefined" || typeof trailingSlash !== "undefined" || typeof redirects !== "undefined" || typeof headers !== "undefined" || typeof rewrites !== "undefined";
216
+ if (hasNewProperties) {
217
+ const error = createError(
218
+ "invalid_mixed_routes",
219
+ "If `rewrites`, `redirects`, `headers`, `cleanUrls` or `trailingSlash` are used, then `routes` cannot be present.",
220
+ "https://vercel.link/mix-routing-props",
221
+ "Learn More"
222
+ );
223
+ return { routes, error };
205
224
  }
206
- if (typeof cleanUrls !== 'undefined') {
207
- const normalized = normalizeRoutes((0, superstatic_1.convertCleanUrls)(cleanUrls, trailingSlash));
208
- if (normalized.error) {
209
- normalized.error.code = 'invalid_clean_urls';
210
- return { routes, error: normalized.error };
211
- }
212
- routes = routes || [];
213
- routes.push(...(normalized.routes || []));
225
+ return normalizeRoutes(routes);
226
+ }
227
+ if (typeof cleanUrls !== "undefined") {
228
+ const normalized = normalizeRoutes(
229
+ (0, import_superstatic.convertCleanUrls)(cleanUrls, trailingSlash)
230
+ );
231
+ if (normalized.error) {
232
+ normalized.error.code = "invalid_clean_urls";
233
+ return { routes, error: normalized.error };
214
234
  }
215
- if (typeof trailingSlash !== 'undefined') {
216
- const normalized = normalizeRoutes((0, superstatic_1.convertTrailingSlash)(trailingSlash));
217
- if (normalized.error) {
218
- normalized.error.code = 'invalid_trailing_slash';
219
- return { routes, error: normalized.error };
220
- }
221
- routes = routes || [];
222
- routes.push(...(normalized.routes || []));
235
+ routes = routes || [];
236
+ routes.push(...normalized.routes || []);
237
+ }
238
+ if (typeof trailingSlash !== "undefined") {
239
+ const normalized = normalizeRoutes((0, import_superstatic.convertTrailingSlash)(trailingSlash));
240
+ if (normalized.error) {
241
+ normalized.error.code = "invalid_trailing_slash";
242
+ return { routes, error: normalized.error };
223
243
  }
224
- if (typeof redirects !== 'undefined') {
225
- const code = 'invalid_redirect';
226
- const regexErrorMessage = redirects
227
- .map((r, i) => checkRegexSyntax('Redirect', i, r.source))
228
- .find(notEmpty);
229
- if (regexErrorMessage) {
230
- return {
231
- routes,
232
- error: createError('invalid_redirect', regexErrorMessage, 'https://vercel.link/invalid-route-source-pattern', 'Learn More'),
233
- };
234
- }
235
- const patternError = redirects
236
- .map((r, i) => checkPatternSyntax('Redirect', i, r))
237
- .find(notEmpty);
238
- if (patternError) {
239
- return {
240
- routes,
241
- error: createError(code, patternError.message, patternError.link, 'Learn More'),
242
- };
243
- }
244
- const redirectErrorMessage = redirects.map(checkRedirect).find(notEmpty);
245
- if (redirectErrorMessage) {
246
- return {
247
- routes,
248
- error: createError(code, redirectErrorMessage, 'https://vercel.link/redirects-json', 'Learn More'),
249
- };
250
- }
251
- const normalized = normalizeRoutes((0, superstatic_1.convertRedirects)(redirects));
252
- if (normalized.error) {
253
- normalized.error.code = code;
254
- return { routes, error: normalized.error };
255
- }
256
- routes = routes || [];
257
- routes.push(...(normalized.routes || []));
244
+ routes = routes || [];
245
+ routes.push(...normalized.routes || []);
246
+ }
247
+ if (typeof redirects !== "undefined") {
248
+ const code = "invalid_redirect";
249
+ const regexErrorMessage = redirects.map((r, i) => checkRegexSyntax("Redirect", i, r.source)).find(notEmpty);
250
+ if (regexErrorMessage) {
251
+ return {
252
+ routes,
253
+ error: createError(
254
+ "invalid_redirect",
255
+ regexErrorMessage,
256
+ "https://vercel.link/invalid-route-source-pattern",
257
+ "Learn More"
258
+ )
259
+ };
258
260
  }
259
- if (typeof headers !== 'undefined') {
260
- const code = 'invalid_header';
261
- const regexErrorMessage = headers
262
- .map((r, i) => checkRegexSyntax('Header', i, r.source))
263
- .find(notEmpty);
264
- if (regexErrorMessage) {
265
- return {
266
- routes,
267
- error: createError(code, regexErrorMessage, 'https://vercel.link/invalid-route-source-pattern', 'Learn More'),
268
- };
269
- }
270
- const patternError = headers
271
- .map((r, i) => checkPatternSyntax('Header', i, r))
272
- .find(notEmpty);
273
- if (patternError) {
274
- return {
275
- routes,
276
- error: createError(code, patternError.message, patternError.link, 'Learn More'),
277
- };
278
- }
279
- const normalized = normalizeRoutes((0, superstatic_1.convertHeaders)(headers));
280
- if (normalized.error) {
281
- normalized.error.code = code;
282
- return { routes, error: normalized.error };
283
- }
284
- routes = routes || [];
285
- routes.push(...(normalized.routes || []));
261
+ const patternError = redirects.map((r, i) => checkPatternSyntax("Redirect", i, r)).find(notEmpty);
262
+ if (patternError) {
263
+ return {
264
+ routes,
265
+ error: createError(
266
+ code,
267
+ patternError.message,
268
+ patternError.link,
269
+ "Learn More"
270
+ )
271
+ };
286
272
  }
287
- if (typeof rewrites !== 'undefined') {
288
- const code = 'invalid_rewrite';
289
- const regexErrorMessage = rewrites
290
- .map((r, i) => checkRegexSyntax('Rewrite', i, r.source))
291
- .find(notEmpty);
292
- if (regexErrorMessage) {
293
- return {
294
- routes,
295
- error: createError(code, regexErrorMessage, 'https://vercel.link/invalid-route-source-pattern', 'Learn More'),
296
- };
297
- }
298
- const patternError = rewrites
299
- .map((r, i) => checkPatternSyntax('Rewrite', i, r))
300
- .find(notEmpty);
301
- if (patternError) {
302
- return {
303
- routes,
304
- error: createError(code, patternError.message, patternError.link, 'Learn More'),
305
- };
306
- }
307
- const normalized = normalizeRoutes((0, superstatic_1.convertRewrites)(rewrites));
308
- if (normalized.error) {
309
- normalized.error.code = code;
310
- return { routes, error: normalized.error };
311
- }
312
- routes = routes || [];
313
- routes.push({ handle: 'filesystem' });
314
- routes.push(...(normalized.routes || []));
273
+ const redirectErrorMessage = redirects.map(checkRedirect).find(notEmpty);
274
+ if (redirectErrorMessage) {
275
+ return {
276
+ routes,
277
+ error: createError(
278
+ code,
279
+ redirectErrorMessage,
280
+ "https://vercel.link/redirects-json",
281
+ "Learn More"
282
+ )
283
+ };
284
+ }
285
+ const normalized = normalizeRoutes((0, import_superstatic.convertRedirects)(redirects));
286
+ if (normalized.error) {
287
+ normalized.error.code = code;
288
+ return { routes, error: normalized.error };
289
+ }
290
+ routes = routes || [];
291
+ routes.push(...normalized.routes || []);
292
+ }
293
+ if (typeof headers !== "undefined") {
294
+ const code = "invalid_header";
295
+ const regexErrorMessage = headers.map((r, i) => checkRegexSyntax("Header", i, r.source)).find(notEmpty);
296
+ if (regexErrorMessage) {
297
+ return {
298
+ routes,
299
+ error: createError(
300
+ code,
301
+ regexErrorMessage,
302
+ "https://vercel.link/invalid-route-source-pattern",
303
+ "Learn More"
304
+ )
305
+ };
306
+ }
307
+ const patternError = headers.map((r, i) => checkPatternSyntax("Header", i, r)).find(notEmpty);
308
+ if (patternError) {
309
+ return {
310
+ routes,
311
+ error: createError(
312
+ code,
313
+ patternError.message,
314
+ patternError.link,
315
+ "Learn More"
316
+ )
317
+ };
318
+ }
319
+ const normalized = normalizeRoutes((0, import_superstatic.convertHeaders)(headers));
320
+ if (normalized.error) {
321
+ normalized.error.code = code;
322
+ return { routes, error: normalized.error };
323
+ }
324
+ routes = routes || [];
325
+ routes.push(...normalized.routes || []);
326
+ }
327
+ if (typeof rewrites !== "undefined") {
328
+ const code = "invalid_rewrite";
329
+ const regexErrorMessage = rewrites.map((r, i) => checkRegexSyntax("Rewrite", i, r.source)).find(notEmpty);
330
+ if (regexErrorMessage) {
331
+ return {
332
+ routes,
333
+ error: createError(
334
+ code,
335
+ regexErrorMessage,
336
+ "https://vercel.link/invalid-route-source-pattern",
337
+ "Learn More"
338
+ )
339
+ };
340
+ }
341
+ const patternError = rewrites.map((r, i) => checkPatternSyntax("Rewrite", i, r)).find(notEmpty);
342
+ if (patternError) {
343
+ return {
344
+ routes,
345
+ error: createError(
346
+ code,
347
+ patternError.message,
348
+ patternError.link,
349
+ "Learn More"
350
+ )
351
+ };
352
+ }
353
+ const normalized = normalizeRoutes((0, import_superstatic.convertRewrites)(rewrites));
354
+ if (normalized.error) {
355
+ normalized.error.code = code;
356
+ return { routes, error: normalized.error };
315
357
  }
316
- return { routes, error: null };
358
+ routes = routes || [];
359
+ routes.push({ handle: "filesystem" });
360
+ routes.push(...normalized.routes || []);
361
+ }
362
+ return { routes, error: null };
317
363
  }
318
- exports.getTransformedRoutes = getTransformedRoutes;
364
+ // Annotate the CommonJS export names for ESM import in node:
365
+ 0 && (module.exports = {
366
+ appendRoutesToPhase,
367
+ getCleanUrls,
368
+ getTransformedRoutes,
369
+ isHandler,
370
+ isValidHandleValue,
371
+ mergeRoutes,
372
+ normalizeRoutes,
373
+ ...require("./schemas"),
374
+ ...require("./types")
375
+ });