@edgeone/opennextjs-pages 0.1.5-beta.1 → 0.1.6

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.
@@ -22,7 +22,7 @@ import { fileURLToPath } from "node:url";
22
22
  var MODULE_DIR = fileURLToPath(new URL(".", import.meta.url));
23
23
  var PLUGIN_DIR = join(MODULE_DIR, "../..");
24
24
  var DEFAULT_PUBLISH_DIR = ".next";
25
- var SERVER_HANDLER_NAME = "cloud-functions/ssr-node";
25
+ var SERVER_HANDLER_NAME = "server-handler";
26
26
  var EDGE_HANDLER_NAME = "edgeone-edge-handler";
27
27
  var PluginContext = class {
28
28
  edgeoneConfig;
@@ -9,85 +9,18 @@ import "../esm-chunks/chunk-6BT4RYQJ.js";
9
9
  // src/build/routes.ts
10
10
  import * as fs from "fs";
11
11
  import * as path from "path";
12
- import {
13
- convertRedirects,
14
- convertRewrites,
15
- convertHeaders,
16
- convertTrailingSlash
17
- } from "./route-utils.js";
18
- function hasAppRouter(appPathRoutesManifest) {
19
- return appPathRoutesManifest && Object.keys(appPathRoutesManifest).length > 0;
20
- }
21
- function getBuildId(ctx) {
22
- try {
23
- const buildIdPath = path.join(ctx.publishDir, "BUILD_ID");
24
- if (fs.existsSync(buildIdPath)) {
25
- return fs.readFileSync(buildIdPath, "utf-8").trim();
26
- }
27
- } catch {
28
- }
29
- return null;
30
- }
31
- function isRE2Compatible(regex) {
32
- if (/\(\?[=!<]/.test(regex)) {
33
- return false;
34
- }
35
- if (/\\[1-9]/.test(regex)) {
36
- return false;
37
- }
38
- return true;
39
- }
40
- function convertNamedRegexToSrc(namedRegex, basePath = "") {
41
- const regexWithoutNamedGroups = namedRegex.replace(/\(\?<[a-zA-Z][a-zA-Z0-9_]*>/g, "(");
42
- if (!isRE2Compatible(regexWithoutNamedGroups)) {
43
- console.warn(`[opennext] Warning: Regex not RE2 compatible, skipping: ${namedRegex}`);
44
- return null;
45
- }
46
- let src = namedRegex.replace(/\(\?<[a-zA-Z][a-zA-Z0-9_]*>/g, "(");
47
- if (basePath && !src.startsWith(`^${basePath}`)) {
48
- src = src.replace(/^\^/, `^${basePath}`);
49
- }
50
- return src;
51
- }
52
- function getDynamicRoutes(dynamicRoutes, basePath = "") {
53
- const routes = [];
54
- for (const route of dynamicRoutes) {
55
- if (route.namedRegex) {
56
- const src = convertNamedRegexToSrc(route.namedRegex, basePath);
57
- if (src) {
58
- routes.push({ src });
59
- }
60
- } else if (route.regex) {
61
- if (isRE2Compatible(route.regex)) {
62
- let src = route.regex;
63
- if (basePath && !src.startsWith(`^${basePath}`)) {
64
- src = src.replace(/^\^/, `^${basePath}`);
65
- }
66
- routes.push({ src });
67
- }
68
- }
69
- }
70
- return routes;
71
- }
72
- function getDataRoutes(dataRoutes, basePath = "") {
73
- const routes = [];
74
- for (const route of dataRoutes) {
75
- if (route.namedDataRouteRegex) {
76
- const src = convertNamedRegexToSrc(route.namedDataRouteRegex, basePath);
77
- if (src) {
78
- routes.push({ src });
79
- }
80
- } else if (route.dataRouteRegex) {
81
- if (isRE2Compatible(route.dataRouteRegex)) {
82
- let src = route.dataRouteRegex;
83
- if (basePath && !src.startsWith(`^${basePath}`)) {
84
- src = src.replace(/^\^/, `^${basePath}`);
85
- }
86
- routes.push({ src });
87
- }
88
- }
89
- }
90
- return routes;
12
+ function isRE2Compatible(regexSource) {
13
+ const unsupported = [
14
+ /\(\?[=!<]/,
15
+ // 断言(前瞻、后顾)
16
+ /\(\?>/,
17
+ // 原子组
18
+ /\\(\d+)/,
19
+ // 反向引用 \1 \2
20
+ /\(\?\(/
21
+ // 条件表达式
22
+ ];
23
+ return !unsupported.some((r) => r.test(regexSource));
91
24
  }
92
25
  async function getMiddlewareConfig(ctx) {
93
26
  try {
@@ -158,8 +91,8 @@ async function getMiddlewareConfig(ctx) {
158
91
  return null;
159
92
  }
160
93
  }
161
- function updateEdgeFunctionsConfigJson(middlewareConfig) {
162
- const metaJsonPath = path.join(process.cwd(), ".edgeone/edge-functions/config.json");
94
+ function updateEdgeFunctionsMetaJson(middlewareConfig) {
95
+ const metaJsonPath = path.join(process.cwd(), ".edgeone/edge-functions/meta.json");
163
96
  let meta = { routes: [] };
164
97
  if (fs.existsSync(metaJsonPath)) {
165
98
  try {
@@ -177,128 +110,101 @@ function updateEdgeFunctionsConfigJson(middlewareConfig) {
177
110
  }
178
111
  fs.writeFileSync(metaJsonPath, JSON.stringify(meta, null, 2), "utf-8");
179
112
  }
113
+ var convertNextRoutePattern = (path2) => {
114
+ if (!path2.includes("[")) {
115
+ return path2;
116
+ }
117
+ let convertedPath = path2;
118
+ const optionalCatchAllMatch = path2.match(/\[\[\.\.\.([^\]]+)\]\]/);
119
+ if (optionalCatchAllMatch) {
120
+ const paramName = optionalCatchAllMatch[1];
121
+ convertedPath = convertedPath.replace(/\[\[\.\.\.([^\]]+)\]\]/g, `:${paramName}*`);
122
+ }
123
+ const catchAllMatch = path2.match(/\[\.\.\.([^\]]+)\]/);
124
+ if (catchAllMatch) {
125
+ const paramName = catchAllMatch[1];
126
+ convertedPath = convertedPath.replace(/\[\.\.\.([^\]]+)\]/g, `:${paramName}*`);
127
+ }
128
+ const dynamicMatch = path2.match(/\[([^\]]+)\]/);
129
+ if (dynamicMatch) {
130
+ const paramName = dynamicMatch[1];
131
+ convertedPath = convertedPath.replace(/\[([^\]]+)\]/g, `:${paramName}`);
132
+ }
133
+ return convertedPath;
134
+ };
180
135
  var createRouteMeta = async (ctx) => {
181
- const routes = [];
182
- const routesManifest = await ctx.getRoutesManifest();
183
- const appPathRoutesManifest = await ctx.getAppPathRoutesManifest();
184
- const isAppRouter = hasAppRouter(appPathRoutesManifest);
185
- const basePath = routesManifest?.basePath || "";
186
- const trailingSlash = ctx.requiredServerFiles?.config?.trailingSlash ?? false;
187
- const redirects = routesManifest?.redirects || [];
188
- const headers = routesManifest?.headers || [];
189
- const rewrites = routesManifest?.rewrites || { beforeFiles: [], afterFiles: [], fallback: [] };
190
- let beforeFilesRewrites = [];
191
- let afterFilesRewrites = [];
192
- let fallbackRewrites = [];
193
- if (Array.isArray(rewrites)) {
194
- afterFilesRewrites = rewrites;
195
- } else {
196
- beforeFilesRewrites = rewrites.beforeFiles || [];
197
- afterFilesRewrites = rewrites.afterFiles || [];
198
- fallbackRewrites = rewrites.fallback || [];
199
- }
200
- const dynamicRoutes = routesManifest?.dynamicRoutes || [];
201
- const dataRoutes = routesManifest?.dataRoutes || [];
202
- const buildId = getBuildId(ctx);
203
- const staticCacheRegex = buildId ? `^${basePath}/_next/static/(?:[^/]+/pages|pages|chunks|runtime|css|image|media|${buildId.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")})/.+` : `^${basePath}/_next/static/(?:[^/]+/pages|pages|chunks|runtime|css|image|media)/.+`;
204
- routes.push({
205
- src: staticCacheRegex,
206
- headers: {
207
- "cache-control": "public,max-age=31536000,immutable"
208
- },
209
- continue: true
210
- });
211
- if (trailingSlash) {
212
- routes.push(...convertTrailingSlash(true));
213
- } else {
214
- routes.push(...convertTrailingSlash(false));
215
- }
216
- if (headers.length > 0) {
217
- try {
218
- routes.push(...convertHeaders(headers));
219
- } catch (e) {
220
- console.warn("[opennext] Warning: Failed to convert some headers:", e);
136
+ const routeMap = {};
137
+ const manifest = await ctx.getPrerenderManifest();
138
+ if (manifest?.routes) {
139
+ for (const [route, routeInfo] of Object.entries(manifest.routes)) {
140
+ routeMap[route] = {
141
+ // 提取关键信息到routeMap
142
+ isStatic: routeInfo.initialRevalidateSeconds === false,
143
+ initialRevalidateSeconds: routeInfo.initialRevalidateSeconds || void 0,
144
+ srcRoute: routeInfo.srcRoute || void 0,
145
+ dataRoute: routeInfo.dataRoute || void 0
146
+ };
221
147
  }
222
148
  }
223
- const userRedirects = redirects.filter((r) => !r.internal);
224
- if (userRedirects.length > 0) {
225
- try {
226
- routes.push(...convertRedirects(userRedirects));
227
- } catch (e) {
228
- console.warn("[opennext] Warning: Failed to convert some redirects:", e);
149
+ const pagesManifest = await ctx.getPagesManifest();
150
+ if (pagesManifest) {
151
+ for (const [route, filePath] of Object.entries(pagesManifest)) {
152
+ if (!routeMap[route]) {
153
+ routeMap[route] = {};
154
+ }
155
+ if (filePath.startsWith("pages") && filePath.endsWith(".html")) {
156
+ routeMap[route].isStatic = true;
157
+ }
229
158
  }
230
159
  }
231
- if (beforeFilesRewrites.length > 0) {
232
- try {
233
- routes.push(...convertRewrites(beforeFilesRewrites));
234
- } catch (e) {
235
- console.warn("[opennext] Warning: Failed to convert beforeFiles rewrites:", e);
160
+ const appPathRoutesManifest = await ctx.getAppPathRoutesManifest();
161
+ if (appPathRoutesManifest) {
162
+ for (const [route, actualRoute] of Object.entries(appPathRoutesManifest)) {
163
+ if (!routeMap[actualRoute]) {
164
+ routeMap[actualRoute] = {};
165
+ }
236
166
  }
237
167
  }
238
- if (isAppRouter) {
239
- const rscVary = "RSC, Next-Router-State-Tree, Next-Router-Prefetch, Next-Router-Segment-Prefetch";
240
- routes.push({
241
- src: `^${basePath}/?$`,
242
- has: [{ type: "header", key: "rsc", value: "1" }],
243
- dest: `${basePath}/index.rsc`,
244
- headers: { vary: rscVary },
245
- continue: true
246
- });
247
- routes.push({
248
- src: `^${basePath}/(.+?)(?:/)?$`,
249
- exclude: "\\.rsc/?$",
250
- has: [{ type: "header", key: "rsc", value: "1" }],
251
- dest: `${basePath}/$1.rsc`,
252
- headers: { vary: rscVary },
253
- continue: true
254
- });
255
- }
256
- routes.push({
257
- src: `^${basePath}/404/?$`,
258
- status: 404,
259
- continue: true,
260
- missing: [{ type: "header", key: "x-prerender-revalidate" }]
261
- });
262
- routes.push({
263
- src: `^${basePath}/500$`,
264
- status: 500,
265
- continue: true
266
- });
267
- routes.push({
268
- src: `^${basePath}/((?:[^/]+/)*[^/.]+)/$`,
269
- dest: `${basePath}/$1`,
270
- continue: true
271
- });
272
- routes.push({ handle: "filesystem" });
273
- if (afterFilesRewrites.length > 0) {
274
- try {
275
- routes.push(...convertRewrites(afterFilesRewrites));
276
- } catch (e) {
277
- console.warn("[opennext] Warning: Failed to convert afterFiles rewrites:", e);
168
+ const routesManifest = await ctx.getRoutesManifest();
169
+ if (routesManifest) {
170
+ const dataRoutes = routesManifest.dataRoutes;
171
+ if (dataRoutes) {
172
+ for (const { page, dataRouteRegex } of dataRoutes) {
173
+ routeMap[dataRouteRegex] = {
174
+ isStatic: routeMap[page]?.isStatic || false
175
+ };
176
+ }
278
177
  }
279
178
  }
280
- if (fallbackRewrites.length > 0) {
281
- try {
282
- routes.push(...convertRewrites(fallbackRewrites));
283
- } catch (e) {
284
- console.warn("[opennext] Warning: Failed to convert fallback rewrites:", e);
179
+ const imagesManifest = await ctx.getImagesManifest();
180
+ if (imagesManifest) {
181
+ if (imagesManifest.images) {
182
+ const imageConfig = imagesManifest.images;
183
+ routeMap[imageConfig.path] = {};
285
184
  }
286
185
  }
287
- if (dynamicRoutes.length > 0) {
288
- routes.push(...getDynamicRoutes(dynamicRoutes, basePath));
186
+ const convertedRouteMap = {};
187
+ const pathsToDelete = [];
188
+ for (const [routePath, routeConfig] of Object.entries(routeMap)) {
189
+ const convertedPath = convertNextRoutePattern(routePath);
190
+ if (convertedPath !== routePath) {
191
+ pathsToDelete.push(routePath);
192
+ convertedRouteMap[convertedPath] = routeConfig;
193
+ }
289
194
  }
290
- if (dataRoutes.length > 0) {
291
- routes.push(...getDataRoutes(dataRoutes, basePath));
195
+ for (const pathToDelete of pathsToDelete) {
196
+ delete routeMap[pathToDelete];
292
197
  }
293
- routes.push({
294
- src: `^${basePath}/.*$`
295
- });
198
+ Object.assign(routeMap, convertedRouteMap);
199
+ const routesArray = Object.entries(routeMap).map(([path2, config]) => ({
200
+ path: path2,
201
+ ...config
202
+ }));
296
203
  const serverHandlerDir = ctx.serverHandlerRootDir;
297
204
  if (!fs.existsSync(serverHandlerDir)) {
298
205
  fs.mkdirSync(serverHandlerDir, { recursive: true });
299
206
  }
300
- const nextVersion = ctx.nextVersion || null;
301
- const imagesManifest = await ctx.getImagesManifest();
207
+ const metaFilePath = path.join(serverHandlerDir, "meta.json");
302
208
  const updatedRedirects = [];
303
209
  if (imagesManifest?.images?.path) {
304
210
  const imagePath = imagesManifest.images.path;
@@ -309,24 +215,21 @@ var createRouteMeta = async (ctx) => {
309
215
  };
310
216
  updatedRedirects.push(nextImageRedirect);
311
217
  }
312
- const config = {
313
- version: 3,
314
- routes,
218
+ const metaData = {
315
219
  conf: {
316
220
  redirects: updatedRedirects
317
221
  },
318
- ...nextVersion ? { framework: { version: nextVersion } } : {}
222
+ nextRoutes: routesArray
319
223
  };
320
- const configFilePath = path.join(serverHandlerDir, "config.json");
321
224
  fs.writeFileSync(
322
- configFilePath,
323
- JSON.stringify(config, null, 2),
225
+ metaFilePath,
226
+ JSON.stringify(metaData, null, 2),
324
227
  "utf-8"
325
228
  );
326
- console.log(`[opennext] Generated ${configFilePath} with ${routes.length} routes`);
327
229
  const middlewareConfig = await getMiddlewareConfig(ctx);
328
- updateEdgeFunctionsConfigJson(middlewareConfig);
230
+ updateEdgeFunctionsMetaJson(middlewareConfig);
329
231
  };
330
232
  export {
233
+ convertNextRoutePattern,
331
234
  createRouteMeta
332
235
  };
@@ -28,7 +28,7 @@ module.exports = __toCommonJS(tags_handler_exports);
28
28
 
29
29
  // package.json
30
30
  var name = "@edgeone/opennextjs-pages";
31
- var version = "0.1.5-beta.1";
31
+ var version = "0.1.6";
32
32
 
33
33
  // src/run/handlers/tags-handler.cts
34
34
  var import_request_context = require("./request-context.cjs");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@edgeone/opennextjs-pages",
3
- "version": "0.1.5-beta.1",
3
+ "version": "0.1.6",
4
4
  "description": "",
5
5
  "main": "./dist/index.js",
6
6
  "type": "module",
@@ -1,698 +0,0 @@
1
-
2
- var require = await (async () => {
3
- var { createRequire } = await import("node:module");
4
- return createRequire(import.meta.url);
5
- })();
6
-
7
- import {
8
- __commonJS,
9
- __toESM
10
- } from "../esm-chunks/chunk-6BT4RYQJ.js";
11
-
12
- // node_modules/path-to-regexp/dist/index.js
13
- var require_dist = __commonJS({
14
- "node_modules/path-to-regexp/dist/index.js"(exports) {
15
- "use strict";
16
- Object.defineProperty(exports, "__esModule", { value: true });
17
- exports.pathToRegexp = exports.tokensToRegexp = exports.regexpToFunction = exports.match = exports.tokensToFunction = exports.compile = exports.parse = void 0;
18
- function lexer(str) {
19
- var tokens = [];
20
- var i = 0;
21
- while (i < str.length) {
22
- var char = str[i];
23
- if (char === "*" || char === "+" || char === "?") {
24
- tokens.push({ type: "MODIFIER", index: i, value: str[i++] });
25
- continue;
26
- }
27
- if (char === "\\") {
28
- tokens.push({ type: "ESCAPED_CHAR", index: i++, value: str[i++] });
29
- continue;
30
- }
31
- if (char === "{") {
32
- tokens.push({ type: "OPEN", index: i, value: str[i++] });
33
- continue;
34
- }
35
- if (char === "}") {
36
- tokens.push({ type: "CLOSE", index: i, value: str[i++] });
37
- continue;
38
- }
39
- if (char === ":") {
40
- var name = "";
41
- var j = i + 1;
42
- while (j < str.length) {
43
- var code = str.charCodeAt(j);
44
- if (
45
- // `0-9`
46
- code >= 48 && code <= 57 || // `A-Z`
47
- code >= 65 && code <= 90 || // `a-z`
48
- code >= 97 && code <= 122 || // `_`
49
- code === 95
50
- ) {
51
- name += str[j++];
52
- continue;
53
- }
54
- break;
55
- }
56
- if (!name)
57
- throw new TypeError("Missing parameter name at ".concat(i));
58
- tokens.push({ type: "NAME", index: i, value: name });
59
- i = j;
60
- continue;
61
- }
62
- if (char === "(") {
63
- var count = 1;
64
- var pattern = "";
65
- var j = i + 1;
66
- if (str[j] === "?") {
67
- throw new TypeError('Pattern cannot start with "?" at '.concat(j));
68
- }
69
- while (j < str.length) {
70
- if (str[j] === "\\") {
71
- pattern += str[j++] + str[j++];
72
- continue;
73
- }
74
- if (str[j] === ")") {
75
- count--;
76
- if (count === 0) {
77
- j++;
78
- break;
79
- }
80
- } else if (str[j] === "(") {
81
- count++;
82
- if (str[j + 1] !== "?") {
83
- throw new TypeError("Capturing groups are not allowed at ".concat(j));
84
- }
85
- }
86
- pattern += str[j++];
87
- }
88
- if (count)
89
- throw new TypeError("Unbalanced pattern at ".concat(i));
90
- if (!pattern)
91
- throw new TypeError("Missing pattern at ".concat(i));
92
- tokens.push({ type: "PATTERN", index: i, value: pattern });
93
- i = j;
94
- continue;
95
- }
96
- tokens.push({ type: "CHAR", index: i, value: str[i++] });
97
- }
98
- tokens.push({ type: "END", index: i, value: "" });
99
- return tokens;
100
- }
101
- function parse(str, options) {
102
- if (options === void 0) {
103
- options = {};
104
- }
105
- var tokens = lexer(str);
106
- var _a = options.prefixes, prefixes = _a === void 0 ? "./" : _a, _b = options.delimiter, delimiter = _b === void 0 ? "/#?" : _b;
107
- var result = [];
108
- var key = 0;
109
- var i = 0;
110
- var path = "";
111
- var tryConsume = function(type) {
112
- if (i < tokens.length && tokens[i].type === type)
113
- return tokens[i++].value;
114
- };
115
- var mustConsume = function(type) {
116
- var value2 = tryConsume(type);
117
- if (value2 !== void 0)
118
- return value2;
119
- var _a2 = tokens[i], nextType = _a2.type, index = _a2.index;
120
- throw new TypeError("Unexpected ".concat(nextType, " at ").concat(index, ", expected ").concat(type));
121
- };
122
- var consumeText = function() {
123
- var result2 = "";
124
- var value2;
125
- while (value2 = tryConsume("CHAR") || tryConsume("ESCAPED_CHAR")) {
126
- result2 += value2;
127
- }
128
- return result2;
129
- };
130
- var isSafe = function(value2) {
131
- for (var _i = 0, delimiter_1 = delimiter; _i < delimiter_1.length; _i++) {
132
- var char2 = delimiter_1[_i];
133
- if (value2.indexOf(char2) > -1)
134
- return true;
135
- }
136
- return false;
137
- };
138
- var safePattern = function(prefix2) {
139
- var prev = result[result.length - 1];
140
- var prevText = prefix2 || (prev && typeof prev === "string" ? prev : "");
141
- if (prev && !prevText) {
142
- throw new TypeError('Must have text between two parameters, missing text after "'.concat(prev.name, '"'));
143
- }
144
- if (!prevText || isSafe(prevText))
145
- return "[^".concat(escapeString(delimiter), "]+?");
146
- return "(?:(?!".concat(escapeString(prevText), ")[^").concat(escapeString(delimiter), "])+?");
147
- };
148
- while (i < tokens.length) {
149
- var char = tryConsume("CHAR");
150
- var name = tryConsume("NAME");
151
- var pattern = tryConsume("PATTERN");
152
- if (name || pattern) {
153
- var prefix = char || "";
154
- if (prefixes.indexOf(prefix) === -1) {
155
- path += prefix;
156
- prefix = "";
157
- }
158
- if (path) {
159
- result.push(path);
160
- path = "";
161
- }
162
- result.push({
163
- name: name || key++,
164
- prefix,
165
- suffix: "",
166
- pattern: pattern || safePattern(prefix),
167
- modifier: tryConsume("MODIFIER") || ""
168
- });
169
- continue;
170
- }
171
- var value = char || tryConsume("ESCAPED_CHAR");
172
- if (value) {
173
- path += value;
174
- continue;
175
- }
176
- if (path) {
177
- result.push(path);
178
- path = "";
179
- }
180
- var open = tryConsume("OPEN");
181
- if (open) {
182
- var prefix = consumeText();
183
- var name_1 = tryConsume("NAME") || "";
184
- var pattern_1 = tryConsume("PATTERN") || "";
185
- var suffix = consumeText();
186
- mustConsume("CLOSE");
187
- result.push({
188
- name: name_1 || (pattern_1 ? key++ : ""),
189
- pattern: name_1 && !pattern_1 ? safePattern(prefix) : pattern_1,
190
- prefix,
191
- suffix,
192
- modifier: tryConsume("MODIFIER") || ""
193
- });
194
- continue;
195
- }
196
- mustConsume("END");
197
- }
198
- return result;
199
- }
200
- exports.parse = parse;
201
- function compile2(str, options) {
202
- return tokensToFunction(parse(str, options), options);
203
- }
204
- exports.compile = compile2;
205
- function tokensToFunction(tokens, options) {
206
- if (options === void 0) {
207
- options = {};
208
- }
209
- var reFlags = flags(options);
210
- var _a = options.encode, encode = _a === void 0 ? function(x) {
211
- return x;
212
- } : _a, _b = options.validate, validate = _b === void 0 ? true : _b;
213
- var matches = tokens.map(function(token) {
214
- if (typeof token === "object") {
215
- return new RegExp("^(?:".concat(token.pattern, ")$"), reFlags);
216
- }
217
- });
218
- return function(data) {
219
- var path = "";
220
- for (var i = 0; i < tokens.length; i++) {
221
- var token = tokens[i];
222
- if (typeof token === "string") {
223
- path += token;
224
- continue;
225
- }
226
- var value = data ? data[token.name] : void 0;
227
- var optional = token.modifier === "?" || token.modifier === "*";
228
- var repeat = token.modifier === "*" || token.modifier === "+";
229
- if (Array.isArray(value)) {
230
- if (!repeat) {
231
- throw new TypeError('Expected "'.concat(token.name, '" to not repeat, but got an array'));
232
- }
233
- if (value.length === 0) {
234
- if (optional)
235
- continue;
236
- throw new TypeError('Expected "'.concat(token.name, '" to not be empty'));
237
- }
238
- for (var j = 0; j < value.length; j++) {
239
- var segment = encode(value[j], token);
240
- if (validate && !matches[i].test(segment)) {
241
- throw new TypeError('Expected all "'.concat(token.name, '" to match "').concat(token.pattern, '", but got "').concat(segment, '"'));
242
- }
243
- path += token.prefix + segment + token.suffix;
244
- }
245
- continue;
246
- }
247
- if (typeof value === "string" || typeof value === "number") {
248
- var segment = encode(String(value), token);
249
- if (validate && !matches[i].test(segment)) {
250
- throw new TypeError('Expected "'.concat(token.name, '" to match "').concat(token.pattern, '", but got "').concat(segment, '"'));
251
- }
252
- path += token.prefix + segment + token.suffix;
253
- continue;
254
- }
255
- if (optional)
256
- continue;
257
- var typeOfMessage = repeat ? "an array" : "a string";
258
- throw new TypeError('Expected "'.concat(token.name, '" to be ').concat(typeOfMessage));
259
- }
260
- return path;
261
- };
262
- }
263
- exports.tokensToFunction = tokensToFunction;
264
- function match(str, options) {
265
- var keys = [];
266
- var re = pathToRegexp2(str, keys, options);
267
- return regexpToFunction(re, keys, options);
268
- }
269
- exports.match = match;
270
- function regexpToFunction(re, keys, options) {
271
- if (options === void 0) {
272
- options = {};
273
- }
274
- var _a = options.decode, decode = _a === void 0 ? function(x) {
275
- return x;
276
- } : _a;
277
- return function(pathname) {
278
- var m = re.exec(pathname);
279
- if (!m)
280
- return false;
281
- var path = m[0], index = m.index;
282
- var params = /* @__PURE__ */ Object.create(null);
283
- var _loop_1 = function(i2) {
284
- if (m[i2] === void 0)
285
- return "continue";
286
- var key = keys[i2 - 1];
287
- if (key.modifier === "*" || key.modifier === "+") {
288
- params[key.name] = m[i2].split(key.prefix + key.suffix).map(function(value) {
289
- return decode(value, key);
290
- });
291
- } else {
292
- params[key.name] = decode(m[i2], key);
293
- }
294
- };
295
- for (var i = 1; i < m.length; i++) {
296
- _loop_1(i);
297
- }
298
- return { path, index, params };
299
- };
300
- }
301
- exports.regexpToFunction = regexpToFunction;
302
- function escapeString(str) {
303
- return str.replace(/([.+*?=^!:${}()[\]|/\\])/g, "\\$1");
304
- }
305
- function flags(options) {
306
- return options && options.sensitive ? "" : "i";
307
- }
308
- function regexpToRegexp(path, keys) {
309
- if (!keys)
310
- return path;
311
- var groupsRegex = /\((?:\?<(.*?)>)?(?!\?)/g;
312
- var index = 0;
313
- var execResult = groupsRegex.exec(path.source);
314
- while (execResult) {
315
- keys.push({
316
- // Use parenthesized substring match if available, index otherwise
317
- name: execResult[1] || index++,
318
- prefix: "",
319
- suffix: "",
320
- modifier: "",
321
- pattern: ""
322
- });
323
- execResult = groupsRegex.exec(path.source);
324
- }
325
- return path;
326
- }
327
- function arrayToRegexp(paths, keys, options) {
328
- var parts = paths.map(function(path) {
329
- return pathToRegexp2(path, keys, options).source;
330
- });
331
- return new RegExp("(?:".concat(parts.join("|"), ")"), flags(options));
332
- }
333
- function stringToRegexp(path, keys, options) {
334
- return tokensToRegexp(parse(path, options), keys, options);
335
- }
336
- function tokensToRegexp(tokens, keys, options) {
337
- if (options === void 0) {
338
- options = {};
339
- }
340
- var _a = options.strict, strict = _a === void 0 ? false : _a, _b = options.start, start = _b === void 0 ? true : _b, _c = options.end, end = _c === void 0 ? true : _c, _d = options.encode, encode = _d === void 0 ? function(x) {
341
- return x;
342
- } : _d, _e = options.delimiter, delimiter = _e === void 0 ? "/#?" : _e, _f = options.endsWith, endsWith = _f === void 0 ? "" : _f;
343
- var endsWithRe = "[".concat(escapeString(endsWith), "]|$");
344
- var delimiterRe = "[".concat(escapeString(delimiter), "]");
345
- var route = start ? "^" : "";
346
- for (var _i = 0, tokens_1 = tokens; _i < tokens_1.length; _i++) {
347
- var token = tokens_1[_i];
348
- if (typeof token === "string") {
349
- route += escapeString(encode(token));
350
- } else {
351
- var prefix = escapeString(encode(token.prefix));
352
- var suffix = escapeString(encode(token.suffix));
353
- if (token.pattern) {
354
- if (keys)
355
- keys.push(token);
356
- if (prefix || suffix) {
357
- if (token.modifier === "+" || token.modifier === "*") {
358
- var mod = token.modifier === "*" ? "?" : "";
359
- route += "(?:".concat(prefix, "((?:").concat(token.pattern, ")(?:").concat(suffix).concat(prefix, "(?:").concat(token.pattern, "))*)").concat(suffix, ")").concat(mod);
360
- } else {
361
- route += "(?:".concat(prefix, "(").concat(token.pattern, ")").concat(suffix, ")").concat(token.modifier);
362
- }
363
- } else {
364
- if (token.modifier === "+" || token.modifier === "*") {
365
- throw new TypeError('Can not repeat "'.concat(token.name, '" without a prefix and suffix'));
366
- }
367
- route += "(".concat(token.pattern, ")").concat(token.modifier);
368
- }
369
- } else {
370
- route += "(?:".concat(prefix).concat(suffix, ")").concat(token.modifier);
371
- }
372
- }
373
- }
374
- if (end) {
375
- if (!strict)
376
- route += "".concat(delimiterRe, "?");
377
- route += !options.endsWith ? "$" : "(?=".concat(endsWithRe, ")");
378
- } else {
379
- var endToken = tokens[tokens.length - 1];
380
- var isEndDelimited = typeof endToken === "string" ? delimiterRe.indexOf(endToken[endToken.length - 1]) > -1 : endToken === void 0;
381
- if (!strict) {
382
- route += "(?:".concat(delimiterRe, "(?=").concat(endsWithRe, "))?");
383
- }
384
- if (!isEndDelimited) {
385
- route += "(?=".concat(delimiterRe, "|").concat(endsWithRe, ")");
386
- }
387
- }
388
- return new RegExp(route, flags(options));
389
- }
390
- exports.tokensToRegexp = tokensToRegexp;
391
- function pathToRegexp2(path, keys, options) {
392
- if (path instanceof RegExp)
393
- return regexpToRegexp(path, keys);
394
- if (Array.isArray(path))
395
- return arrayToRegexp(path, keys, options);
396
- return stringToRegexp(path, keys, options);
397
- }
398
- exports.pathToRegexp = pathToRegexp2;
399
- }
400
- });
401
-
402
- // src/build/route-utils.ts
403
- var import_path_to_regexp = __toESM(require_dist(), 1);
404
- import { parse as parseUrl, format as formatUrl } from "url";
405
- var UN_NAMED_SEGMENT = "__UN_NAMED_SEGMENT__";
406
- function sourceToRegex(source) {
407
- const keys = [];
408
- const r = (0, import_path_to_regexp.pathToRegexp)(source, keys, {
409
- strict: true,
410
- sensitive: true,
411
- delimiter: "/"
412
- });
413
- const segments = keys.map((k) => k.name).map((name) => {
414
- if (typeof name !== "string") {
415
- return UN_NAMED_SEGMENT;
416
- }
417
- return name;
418
- });
419
- return { src: r.source, segments };
420
- }
421
- var namedGroupsRegex = /\(\?<([a-zA-Z][a-zA-Z0-9_]*)>/g;
422
- function normalizeHasKeys(hasItems = []) {
423
- for (const hasItem of hasItems) {
424
- if ("key" in hasItem && hasItem.type === "header") {
425
- hasItem.key = hasItem.key.toLowerCase();
426
- }
427
- }
428
- return hasItems;
429
- }
430
- function collectHasSegments(has) {
431
- const hasSegments = /* @__PURE__ */ new Set();
432
- for (const hasItem of has || []) {
433
- if (!hasItem.value && "key" in hasItem) {
434
- hasSegments.add(hasItem.key);
435
- }
436
- if (typeof hasItem.value === "string") {
437
- for (const match of hasItem.value.matchAll(namedGroupsRegex)) {
438
- if (match[1]) {
439
- hasSegments.add(match[1]);
440
- }
441
- }
442
- if (hasItem.type === "host") {
443
- hasSegments.add("host");
444
- }
445
- }
446
- }
447
- return [...hasSegments];
448
- }
449
- function toSegmentDest(index) {
450
- return "$" + (index + 1).toString();
451
- }
452
- var escapeSegment = (str, segmentName) => str.replace(
453
- new RegExp(`:${segmentName}`, "g"),
454
- `__ESC_COLON_${segmentName}`
455
- );
456
- var unescapeSegments = (str) => str.replace(/__ESC_COLON_/gi, ":");
457
- function safelyCompile(value, indexes, attemptDirectCompile) {
458
- if (!value) {
459
- return value;
460
- }
461
- if (attemptDirectCompile) {
462
- try {
463
- return (0, import_path_to_regexp.compile)(value, { validate: false })(indexes);
464
- } catch (_) {
465
- }
466
- }
467
- for (const key of Object.keys(indexes)) {
468
- if (value.includes(`:${key}`)) {
469
- value = value.replace(
470
- new RegExp(`:${key}\\*`, "g"),
471
- `:${key}--ESCAPED_PARAM_ASTERISK`
472
- ).replace(
473
- new RegExp(`:${key}\\?`, "g"),
474
- `:${key}--ESCAPED_PARAM_QUESTION`
475
- ).replace(
476
- new RegExp(`:${key}\\+`, "g"),
477
- `:${key}--ESCAPED_PARAM_PLUS`
478
- ).replace(
479
- new RegExp(`:${key}(?!\\w)`, "g"),
480
- `--ESCAPED_PARAM_COLON${key}`
481
- );
482
- }
483
- }
484
- value = value.replace(/(:|\\*|\\?|\\+|\\(|\\)|\\{|\\})/g, "\\$1").replace(/--ESCAPED_PARAM_PLUS/g, "+").replace(/--ESCAPED_PARAM_COLON/g, ":").replace(/--ESCAPED_PARAM_QUESTION/g, "?").replace(/--ESCAPED_PARAM_ASTERISK/g, "*");
485
- return (0, import_path_to_regexp.compile)(`/${value}`, { validate: false })(indexes).slice(1);
486
- }
487
- function replaceSegments(segments, hasItemSegments, destination, isRedirect, internalParamNames) {
488
- const namedSegments = segments.filter((name) => name !== UN_NAMED_SEGMENT);
489
- const canNeedReplacing = destination.includes(":") && namedSegments.length > 0 || hasItemSegments.length > 0 || !isRedirect;
490
- if (!canNeedReplacing) {
491
- return destination;
492
- }
493
- let escapedDestination = destination;
494
- const indexes = {};
495
- segments.forEach((name, index) => {
496
- indexes[name] = toSegmentDest(index);
497
- escapedDestination = escapeSegment(escapedDestination, name);
498
- });
499
- hasItemSegments.forEach((name) => {
500
- indexes[name] = "$" + name;
501
- escapedDestination = escapeSegment(escapedDestination, name);
502
- });
503
- const parsedDestination = parseUrl(escapedDestination, true);
504
- delete parsedDestination.href;
505
- delete parsedDestination.path;
506
- delete parsedDestination.search;
507
- delete parsedDestination.host;
508
- let { pathname, hash, query, hostname, ...rest } = parsedDestination;
509
- pathname = unescapeSegments(pathname || "");
510
- hash = unescapeSegments(hash || "");
511
- hostname = unescapeSegments(hostname || "");
512
- let destParams = /* @__PURE__ */ new Set();
513
- const pathnameKeys = [];
514
- const hashKeys = [];
515
- const hostnameKeys = [];
516
- try {
517
- (0, import_path_to_regexp.pathToRegexp)(pathname, pathnameKeys);
518
- (0, import_path_to_regexp.pathToRegexp)(hash || "", hashKeys);
519
- (0, import_path_to_regexp.pathToRegexp)(hostname || "", hostnameKeys);
520
- } catch (_) {
521
- }
522
- destParams = new Set(
523
- [...pathnameKeys, ...hashKeys, ...hostnameKeys].map((key) => key.name).filter((val) => typeof val === "string")
524
- );
525
- pathname = safelyCompile(pathname, indexes, true);
526
- hash = hash ? safelyCompile(hash, indexes, true) : null;
527
- hostname = hostname ? safelyCompile(hostname, indexes, true) : null;
528
- for (const [key, strOrArray] of Object.entries(query)) {
529
- if (Array.isArray(strOrArray)) {
530
- query[key] = strOrArray.map(
531
- (str) => safelyCompile(unescapeSegments(str), indexes, true)
532
- );
533
- } else {
534
- query[key] = safelyCompile(
535
- unescapeSegments(strOrArray),
536
- indexes,
537
- true
538
- );
539
- }
540
- }
541
- const paramKeys = Object.keys(indexes);
542
- const needsQueryUpdating = !isRedirect && !paramKeys.some(
543
- (param) => !(internalParamNames && internalParamNames.includes(param)) && destParams.has(param)
544
- );
545
- if (needsQueryUpdating) {
546
- for (const param of paramKeys) {
547
- if (!(param in query) && param !== UN_NAMED_SEGMENT) {
548
- query[param] = indexes[param];
549
- }
550
- }
551
- }
552
- destination = formatUrl({
553
- ...rest,
554
- hostname,
555
- pathname,
556
- query,
557
- hash
558
- });
559
- return destination.replace(/%24/g, "$");
560
- }
561
- function convertRedirects(redirects, defaultStatus = 308) {
562
- return redirects.map((r) => {
563
- const { src, segments } = sourceToRegex(r.source);
564
- const hasSegments = collectHasSegments(r.has);
565
- normalizeHasKeys(r.has);
566
- normalizeHasKeys(r.missing);
567
- try {
568
- const loc = replaceSegments(segments, hasSegments, r.destination, true);
569
- let status;
570
- if (typeof r.permanent === "boolean") {
571
- status = r.permanent ? 308 : 307;
572
- } else if (r.statusCode) {
573
- status = r.statusCode;
574
- } else {
575
- status = defaultStatus;
576
- }
577
- const route = {
578
- src,
579
- headers: { Location: loc },
580
- status
581
- };
582
- if (r.has) {
583
- route.has = r.has;
584
- }
585
- if (r.missing) {
586
- route.missing = r.missing;
587
- }
588
- return route;
589
- } catch (e) {
590
- throw new Error(`Failed to parse redirect: ${JSON.stringify(r)}`);
591
- }
592
- });
593
- }
594
- function convertRewrites(rewrites, internalParamNames) {
595
- return rewrites.map((r) => {
596
- const { src, segments } = sourceToRegex(r.source);
597
- const hasSegments = collectHasSegments(r.has);
598
- normalizeHasKeys(r.has);
599
- normalizeHasKeys(r.missing);
600
- try {
601
- const dest = replaceSegments(
602
- segments,
603
- hasSegments,
604
- r.destination,
605
- false,
606
- internalParamNames
607
- );
608
- const route = { src, dest, check: true };
609
- if (r.has) {
610
- route.has = r.has;
611
- }
612
- if (r.missing) {
613
- route.missing = r.missing;
614
- }
615
- if (r.statusCode) {
616
- route.status = r.statusCode;
617
- }
618
- return route;
619
- } catch (e) {
620
- throw new Error(`Failed to parse rewrite: ${JSON.stringify(r)}`);
621
- }
622
- });
623
- }
624
- function convertHeaders(headers) {
625
- return headers.map((h) => {
626
- const obj = {};
627
- const { src, segments } = sourceToRegex(h.source);
628
- const hasSegments = collectHasSegments(h.has);
629
- normalizeHasKeys(h.has);
630
- normalizeHasKeys(h.missing);
631
- const namedSegments = segments.filter((name) => name !== UN_NAMED_SEGMENT);
632
- const indexes = {};
633
- segments.forEach((name, index) => {
634
- indexes[name] = toSegmentDest(index);
635
- });
636
- hasSegments.forEach((name) => {
637
- indexes[name] = "$" + name;
638
- });
639
- h.headers.forEach(({ key, value }) => {
640
- if (namedSegments.length > 0 || hasSegments.length > 0) {
641
- if (key.includes(":")) {
642
- key = safelyCompile(key, indexes);
643
- }
644
- if (value.includes(":")) {
645
- value = safelyCompile(value, indexes);
646
- }
647
- }
648
- obj[key] = value;
649
- });
650
- const route = {
651
- src,
652
- headers: obj,
653
- continue: true
654
- };
655
- if (h.has) {
656
- route.has = h.has;
657
- }
658
- if (h.missing) {
659
- route.missing = h.missing;
660
- }
661
- return route;
662
- });
663
- }
664
- function convertTrailingSlash(enable, status = 308) {
665
- const routes = [];
666
- if (enable) {
667
- routes.push({
668
- src: "^\\/\\.well-known(?:\\/.*)?$"
669
- });
670
- routes.push({
671
- src: "^/((?:[^/]+/)*[^/\\.]+)$",
672
- headers: { Location: "/$1/" },
673
- status
674
- });
675
- routes.push({
676
- src: "^/((?:[^/]+/)*[^/]+\\.\\w+)/$",
677
- headers: { Location: "/$1" },
678
- status
679
- });
680
- } else {
681
- routes.push({
682
- src: "^/(.*)\\/$",
683
- headers: { Location: "/$1" },
684
- status
685
- });
686
- }
687
- return routes;
688
- }
689
- export {
690
- collectHasSegments,
691
- convertHeaders,
692
- convertRedirects,
693
- convertRewrites,
694
- convertTrailingSlash,
695
- normalizeHasKeys,
696
- safelyCompile,
697
- sourceToRegex
698
- };