@edgeone/opennextjs-pages 0.1.6 → 0.1.7
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/build/functions/middleware/compiler.js +51 -18
- package/dist/build/functions/middleware/wrapper.js +51 -32
- package/dist/build/plugin-context.js +1 -1
- package/dist/build/route-utils.js +699 -0
- package/dist/build/routes.js +251 -98
- package/dist/run/handlers/tags-handler.cjs +1 -1
- package/package.json +1 -1
|
@@ -3539,28 +3539,61 @@ function matchesPath(pathname, matcher) {
|
|
|
3539
3539
|
continue;
|
|
3540
3540
|
}
|
|
3541
3541
|
} else {
|
|
3542
|
-
// \u8DEF\u5F84\u6A21\u5F0F\uFF0C\u9700\u8981\u8F6C\u6362
|
|
3543
|
-
|
|
3544
|
-
|
|
3545
|
-
|
|
3546
|
-
|
|
3547
|
-
regexPattern =
|
|
3548
|
-
|
|
3549
|
-
|
|
3550
|
-
|
|
3551
|
-
|
|
3552
|
-
|
|
3553
|
-
|
|
3554
|
-
|
|
3555
|
-
|
|
3542
|
+
// \u8DEF\u5F84\u6A21\u5F0F\uFF0C\u9700\u8981\u8F6C\u6362\u4E3A\u6B63\u5219\u8868\u8FBE\u5F0F
|
|
3543
|
+
// Next.js middleware matcher \u4F7F\u7528\u7C7B\u4F3C path-to-regexp \u7684\u8BED\u6CD5:
|
|
3544
|
+
// /dashboard/:path* -> \u5339\u914D /dashboard \u548C /dashboard/xxx\uFF08\u96F6\u4E2A\u6216\u591A\u4E2A\u8DEF\u5F84\u6BB5\uFF09
|
|
3545
|
+
// /api/:path -> \u5339\u914D /api/xxx\uFF08\u4E00\u4E2A\u8DEF\u5F84\u6BB5\uFF09
|
|
3546
|
+
// /api/:path+ -> \u5339\u914D /api/xxx/yyy\uFF08\u4E00\u4E2A\u6216\u591A\u4E2A\u8DEF\u5F84\u6BB5\uFF09
|
|
3547
|
+
let regexPattern = '';
|
|
3548
|
+
let ci = 0;
|
|
3549
|
+
const p = pattern;
|
|
3550
|
+
while (ci < p.length) {
|
|
3551
|
+
if (p[ci] === ':') {
|
|
3552
|
+
// \u89E3\u6790\u53C2\u6570\u540D
|
|
3553
|
+
ci++;
|
|
3554
|
+
while (ci < p.length && /[a-zA-Z0-9_]/.test(p[ci])) {
|
|
3555
|
+
ci++;
|
|
3556
|
+
}
|
|
3557
|
+
// \u68C0\u67E5\u4FEE\u9970\u7B26
|
|
3558
|
+
if (ci < p.length && p[ci] === '*') {
|
|
3559
|
+
// :param* -> \u96F6\u4E2A\u6216\u591A\u4E2A\u8DEF\u5F84\u6BB5\uFF0C\u524D\u9762\u7684 / \u4E5F\u53D8\u4E3A\u53EF\u9009
|
|
3560
|
+
if (regexPattern.endsWith('\\\\/')) {
|
|
3561
|
+
regexPattern = regexPattern.slice(0, -2);
|
|
3562
|
+
}
|
|
3563
|
+
regexPattern += '(?:\\\\/.*)?';
|
|
3564
|
+
ci++;
|
|
3565
|
+
} else if (ci < p.length && p[ci] === '+') {
|
|
3566
|
+
// :param+ -> \u4E00\u4E2A\u6216\u591A\u4E2A\u8DEF\u5F84\u6BB5
|
|
3567
|
+
regexPattern += '.+';
|
|
3568
|
+
ci++;
|
|
3569
|
+
} else {
|
|
3570
|
+
// :param -> \u5339\u914D\u4E00\u4E2A\u8DEF\u5F84\u6BB5
|
|
3571
|
+
regexPattern += '[^/]+';
|
|
3572
|
+
}
|
|
3573
|
+
} else if (p[ci] === '/') {
|
|
3574
|
+
regexPattern += '\\\\/';
|
|
3575
|
+
ci++;
|
|
3576
|
+
} else if (p[ci] === '*') {
|
|
3577
|
+
if (ci + 1 < p.length && p[ci + 1] === '*') {
|
|
3578
|
+
regexPattern += '.*';
|
|
3579
|
+
ci += 2;
|
|
3580
|
+
} else {
|
|
3581
|
+
regexPattern += '[^/]*';
|
|
3582
|
+
ci++;
|
|
3583
|
+
}
|
|
3556
3584
|
} else {
|
|
3557
|
-
|
|
3585
|
+
// \u8F6C\u4E49\u6B63\u5219\u7279\u6B8A\u5B57\u7B26
|
|
3586
|
+
const ch = p[ci];
|
|
3587
|
+
const specialChars = '.+?^$|(){}[]\\\\';
|
|
3588
|
+
if (specialChars.indexOf(ch) !== -1) {
|
|
3589
|
+
regexPattern += '\\\\' + ch;
|
|
3590
|
+
} else {
|
|
3591
|
+
regexPattern += ch;
|
|
3592
|
+
}
|
|
3593
|
+
ci++;
|
|
3558
3594
|
}
|
|
3559
3595
|
}
|
|
3560
3596
|
|
|
3561
|
-
regexPattern = regexPattern.split('**').join('.*');
|
|
3562
|
-
regexPattern = regexPattern.split('\\\\*').join('[^/]*');
|
|
3563
|
-
|
|
3564
3597
|
regex = new RegExp('^' + regexPattern + '$');
|
|
3565
3598
|
}
|
|
3566
3599
|
|
|
@@ -258,42 +258,61 @@ function matchesPath(pathname, matcher) {
|
|
|
258
258
|
continue;
|
|
259
259
|
}
|
|
260
260
|
} else {
|
|
261
|
-
// \u8DEF\u5F84\u6A21\u5F0F\uFF0C\u9700\u8981\u8F6C\u6362
|
|
262
|
-
//
|
|
263
|
-
//
|
|
264
|
-
// \
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
261
|
+
// \u8DEF\u5F84\u6A21\u5F0F\uFF0C\u9700\u8981\u8F6C\u6362\u4E3A\u6B63\u5219\u8868\u8FBE\u5F0F
|
|
262
|
+
// Next.js middleware matcher \u4F7F\u7528\u7C7B\u4F3C path-to-regexp \u7684\u8BED\u6CD5:
|
|
263
|
+
// /dashboard/:path* -> \u5339\u914D /dashboard \u548C /dashboard/xxx\uFF08\u96F6\u4E2A\u6216\u591A\u4E2A\u8DEF\u5F84\u6BB5\uFF09
|
|
264
|
+
// /api/:path -> \u5339\u914D /api/xxx\uFF08\u4E00\u4E2A\u8DEF\u5F84\u6BB5\uFF09
|
|
265
|
+
// /api/:path+ -> \u5339\u914D /api/xxx/yyy\uFF08\u4E00\u4E2A\u6216\u591A\u4E2A\u8DEF\u5F84\u6BB5\uFF09
|
|
266
|
+
let regexPattern = '';
|
|
267
|
+
let ci = 0;
|
|
268
|
+
const p = pattern;
|
|
269
|
+
while (ci < p.length) {
|
|
270
|
+
if (p[ci] === ':') {
|
|
271
|
+
// \u89E3\u6790\u53C2\u6570\u540D
|
|
272
|
+
ci++;
|
|
273
|
+
while (ci < p.length && /[a-zA-Z0-9_]/.test(p[ci])) {
|
|
274
|
+
ci++;
|
|
275
|
+
}
|
|
276
|
+
// \u68C0\u67E5\u4FEE\u9970\u7B26
|
|
277
|
+
if (ci < p.length && p[ci] === '*') {
|
|
278
|
+
// :param* -> \u96F6\u4E2A\u6216\u591A\u4E2A\u8DEF\u5F84\u6BB5\uFF0C\u524D\u9762\u7684 / \u4E5F\u53D8\u4E3A\u53EF\u9009
|
|
279
|
+
if (regexPattern.endsWith('\\/')) {
|
|
280
|
+
regexPattern = regexPattern.slice(0, -2);
|
|
281
|
+
}
|
|
282
|
+
regexPattern += '(?:\\/.*)?';
|
|
283
|
+
ci++;
|
|
284
|
+
} else if (ci < p.length && p[ci] === '+') {
|
|
285
|
+
// :param+ -> \u4E00\u4E2A\u6216\u591A\u4E2A\u8DEF\u5F84\u6BB5
|
|
286
|
+
regexPattern += '.+';
|
|
287
|
+
ci++;
|
|
288
|
+
} else {
|
|
289
|
+
// :param -> \u5339\u914D\u4E00\u4E2A\u8DEF\u5F84\u6BB5
|
|
290
|
+
regexPattern += '[^/]+';
|
|
291
|
+
}
|
|
292
|
+
} else if (p[ci] === '/') {
|
|
293
|
+
regexPattern += '\\/';
|
|
294
|
+
ci++;
|
|
295
|
+
} else if (p[ci] === '*') {
|
|
296
|
+
if (ci + 1 < p.length && p[ci + 1] === '*') {
|
|
297
|
+
regexPattern += '.*';
|
|
298
|
+
ci += 2;
|
|
299
|
+
} else {
|
|
300
|
+
regexPattern += '[^/]*';
|
|
301
|
+
ci++;
|
|
302
|
+
}
|
|
285
303
|
} else {
|
|
286
|
-
//
|
|
287
|
-
|
|
304
|
+
// \u8F6C\u4E49\u6B63\u5219\u7279\u6B8A\u5B57\u7B26
|
|
305
|
+
const ch = p[ci];
|
|
306
|
+
const specialChars = '.+?^$|(){}[]\\\\';
|
|
307
|
+
if (specialChars.indexOf(ch) !== -1) {
|
|
308
|
+
regexPattern += '\\' + ch;
|
|
309
|
+
} else {
|
|
310
|
+
regexPattern += ch;
|
|
311
|
+
}
|
|
312
|
+
ci++;
|
|
288
313
|
}
|
|
289
314
|
}
|
|
290
315
|
|
|
291
|
-
// ** -> .*
|
|
292
|
-
regexPattern = regexPattern.split('**').join('.*');
|
|
293
|
-
// * -> [^/]* (\u4F46\u4E0D\u5F71\u54CD\u5DF2\u7ECF\u8F6C\u6362\u7684 .*)
|
|
294
|
-
// \u7B80\u5355\u5904\u7406\uFF1A\u53EA\u66FF\u6362\u72EC\u7ACB\u7684 *
|
|
295
|
-
regexPattern = regexPattern.split('\\*').join('[^/]*');
|
|
296
|
-
|
|
297
316
|
regex = new RegExp('^' + regexPattern + '$');
|
|
298
317
|
}
|
|
299
318
|
|
|
@@ -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 = "
|
|
25
|
+
var SERVER_HANDLER_NAME = "cloud-functions/ssr-node";
|
|
26
26
|
var EDGE_HANDLER_NAME = "edgeone-edge-handler";
|
|
27
27
|
var PluginContext = class {
|
|
28
28
|
edgeoneConfig;
|
|
@@ -0,0 +1,699 @@
|
|
|
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
|
+
has: [{ type: "header", key: "x-nextjs-data" }],
|
|
673
|
+
headers: { Location: "/$1/" },
|
|
674
|
+
status
|
|
675
|
+
});
|
|
676
|
+
routes.push({
|
|
677
|
+
src: "^/((?:[^/]+/)*[^/]+\\.\\w+)/$",
|
|
678
|
+
headers: { Location: "/$1" },
|
|
679
|
+
status
|
|
680
|
+
});
|
|
681
|
+
} else {
|
|
682
|
+
routes.push({
|
|
683
|
+
src: "^/(.*)\\/$",
|
|
684
|
+
headers: { Location: "/$1" },
|
|
685
|
+
status
|
|
686
|
+
});
|
|
687
|
+
}
|
|
688
|
+
return routes;
|
|
689
|
+
}
|
|
690
|
+
export {
|
|
691
|
+
collectHasSegments,
|
|
692
|
+
convertHeaders,
|
|
693
|
+
convertRedirects,
|
|
694
|
+
convertRewrites,
|
|
695
|
+
convertTrailingSlash,
|
|
696
|
+
normalizeHasKeys,
|
|
697
|
+
safelyCompile,
|
|
698
|
+
sourceToRegex
|
|
699
|
+
};
|
package/dist/build/routes.js
CHANGED
|
@@ -9,18 +9,130 @@ 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
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
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;
|
|
91
|
+
}
|
|
92
|
+
function getServerRoutes(staticRoutes, prerenderManifest, basePath = "") {
|
|
93
|
+
const routes = [];
|
|
94
|
+
const prerenderRoutes = prerenderManifest?.routes || {};
|
|
95
|
+
for (const route of staticRoutes) {
|
|
96
|
+
if (route.page.startsWith("/_")) {
|
|
97
|
+
continue;
|
|
98
|
+
}
|
|
99
|
+
const prerenderInfo = prerenderRoutes[route.page];
|
|
100
|
+
if (prerenderInfo && prerenderInfo.initialRevalidateSeconds === false) {
|
|
101
|
+
continue;
|
|
102
|
+
}
|
|
103
|
+
if (route.namedRegex) {
|
|
104
|
+
let src = convertNamedRegexToSrc(route.namedRegex, basePath);
|
|
105
|
+
if (src) {
|
|
106
|
+
src = src.replace(/\(\?:\/\)\?\$$/, "$");
|
|
107
|
+
routes.push({ src });
|
|
108
|
+
}
|
|
109
|
+
} else if (route.regex) {
|
|
110
|
+
if (isRE2Compatible(route.regex)) {
|
|
111
|
+
let src = route.regex;
|
|
112
|
+
if (basePath && !src.startsWith(`^${basePath}`)) {
|
|
113
|
+
src = src.replace(/^\^/, `^${basePath}`);
|
|
114
|
+
}
|
|
115
|
+
src = src.replace(/\(\?:\/\)\?\$$/, "$");
|
|
116
|
+
routes.push({ src });
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
return routes;
|
|
121
|
+
}
|
|
122
|
+
function getApiRoutes(appPathsManifest, basePath = "") {
|
|
123
|
+
if (!appPathsManifest) {
|
|
124
|
+
return [];
|
|
125
|
+
}
|
|
126
|
+
const routes = [];
|
|
127
|
+
for (const routePath of Object.keys(appPathsManifest)) {
|
|
128
|
+
if (routePath.includes("/api/") && routePath.endsWith("/route")) {
|
|
129
|
+
const apiPath = routePath.replace(/\/route$/, "");
|
|
130
|
+
const escapedPath = apiPath.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
131
|
+
const src = `^${basePath}${escapedPath}$`;
|
|
132
|
+
routes.push({ src });
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
return routes;
|
|
24
136
|
}
|
|
25
137
|
async function getMiddlewareConfig(ctx) {
|
|
26
138
|
try {
|
|
@@ -91,8 +203,8 @@ async function getMiddlewareConfig(ctx) {
|
|
|
91
203
|
return null;
|
|
92
204
|
}
|
|
93
205
|
}
|
|
94
|
-
function
|
|
95
|
-
const metaJsonPath = path.join(process.cwd(), ".edgeone/edge-functions/
|
|
206
|
+
function updateEdgeFunctionsConfigJson(middlewareConfig) {
|
|
207
|
+
const metaJsonPath = path.join(process.cwd(), ".edgeone/edge-functions/config.json");
|
|
96
208
|
let meta = { routes: [] };
|
|
97
209
|
if (fs.existsSync(metaJsonPath)) {
|
|
98
210
|
try {
|
|
@@ -110,101 +222,139 @@ function updateEdgeFunctionsMetaJson(middlewareConfig) {
|
|
|
110
222
|
}
|
|
111
223
|
fs.writeFileSync(metaJsonPath, JSON.stringify(meta, null, 2), "utf-8");
|
|
112
224
|
}
|
|
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
|
-
};
|
|
135
225
|
var createRouteMeta = async (ctx) => {
|
|
136
|
-
const
|
|
137
|
-
const
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
226
|
+
const routes = [];
|
|
227
|
+
const routesManifest = await ctx.getRoutesManifest();
|
|
228
|
+
const appPathRoutesManifest = await ctx.getAppPathRoutesManifest();
|
|
229
|
+
const isAppRouter = hasAppRouter(appPathRoutesManifest);
|
|
230
|
+
const basePath = routesManifest?.basePath || "";
|
|
231
|
+
const trailingSlash = ctx.requiredServerFiles?.config?.trailingSlash ?? false;
|
|
232
|
+
const redirects = routesManifest?.redirects || [];
|
|
233
|
+
const headers = routesManifest?.headers || [];
|
|
234
|
+
const rewrites = routesManifest?.rewrites || { beforeFiles: [], afterFiles: [], fallback: [] };
|
|
235
|
+
let beforeFilesRewrites = [];
|
|
236
|
+
let afterFilesRewrites = [];
|
|
237
|
+
let fallbackRewrites = [];
|
|
238
|
+
if (Array.isArray(rewrites)) {
|
|
239
|
+
afterFilesRewrites = rewrites;
|
|
240
|
+
} else {
|
|
241
|
+
beforeFilesRewrites = rewrites.beforeFiles || [];
|
|
242
|
+
afterFilesRewrites = rewrites.afterFiles || [];
|
|
243
|
+
fallbackRewrites = rewrites.fallback || [];
|
|
148
244
|
}
|
|
149
|
-
const
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
245
|
+
const dynamicRoutes = routesManifest?.dynamicRoutes || [];
|
|
246
|
+
const dataRoutes = routesManifest?.dataRoutes || [];
|
|
247
|
+
const staticRoutes = routesManifest?.staticRoutes || [];
|
|
248
|
+
const appPathsManifest = await ctx.getAppPathsManifest?.() || null;
|
|
249
|
+
const prerenderManifest = await ctx.getPrerenderManifest?.() || null;
|
|
250
|
+
const buildId = getBuildId(ctx);
|
|
251
|
+
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)/.+`;
|
|
252
|
+
routes.push({
|
|
253
|
+
src: staticCacheRegex,
|
|
254
|
+
headers: {
|
|
255
|
+
"cache-control": "public,max-age=31536000,immutable"
|
|
256
|
+
},
|
|
257
|
+
continue: true
|
|
258
|
+
});
|
|
259
|
+
if (trailingSlash) {
|
|
260
|
+
routes.push(...convertTrailingSlash(true));
|
|
261
|
+
} else {
|
|
262
|
+
routes.push(...convertTrailingSlash(false));
|
|
263
|
+
}
|
|
264
|
+
if (headers.length > 0) {
|
|
265
|
+
try {
|
|
266
|
+
routes.push(...convertHeaders(headers));
|
|
267
|
+
} catch (e) {
|
|
268
|
+
console.warn("[opennext] Warning: Failed to convert some headers:", e);
|
|
158
269
|
}
|
|
159
270
|
}
|
|
160
|
-
const
|
|
161
|
-
if (
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
271
|
+
const userRedirects = redirects.filter((r) => !r.internal);
|
|
272
|
+
if (userRedirects.length > 0) {
|
|
273
|
+
try {
|
|
274
|
+
routes.push(...convertRedirects(userRedirects));
|
|
275
|
+
} catch (e) {
|
|
276
|
+
console.warn("[opennext] Warning: Failed to convert some redirects:", e);
|
|
166
277
|
}
|
|
167
278
|
}
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
routeMap[dataRouteRegex] = {
|
|
174
|
-
isStatic: routeMap[page]?.isStatic || false
|
|
175
|
-
};
|
|
176
|
-
}
|
|
279
|
+
if (beforeFilesRewrites.length > 0) {
|
|
280
|
+
try {
|
|
281
|
+
routes.push(...convertRewrites(beforeFilesRewrites));
|
|
282
|
+
} catch (e) {
|
|
283
|
+
console.warn("[opennext] Warning: Failed to convert beforeFiles rewrites:", e);
|
|
177
284
|
}
|
|
178
285
|
}
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
286
|
+
if (isAppRouter) {
|
|
287
|
+
const rscVary = "RSC, Next-Router-State-Tree, Next-Router-Prefetch, Next-Router-Segment-Prefetch";
|
|
288
|
+
const rscContentType = "text/x-component";
|
|
289
|
+
routes.push({
|
|
290
|
+
src: `^${basePath}/?$`,
|
|
291
|
+
has: [{ type: "header", key: "rsc", value: "1" }],
|
|
292
|
+
dest: `${basePath}/index.rsc`,
|
|
293
|
+
headers: { vary: rscVary, "content-type": rscContentType },
|
|
294
|
+
continue: true
|
|
295
|
+
});
|
|
296
|
+
routes.push({
|
|
297
|
+
src: `^${basePath}/(.+?)(?:/)?$`,
|
|
298
|
+
exclude: "\\.rsc/?$",
|
|
299
|
+
has: [{ type: "header", key: "rsc", value: "1" }],
|
|
300
|
+
dest: `${basePath}/$1.rsc`,
|
|
301
|
+
headers: { vary: rscVary, "content-type": rscContentType },
|
|
302
|
+
continue: true
|
|
303
|
+
});
|
|
304
|
+
}
|
|
305
|
+
routes.push({
|
|
306
|
+
src: `^${basePath}/404/?$`,
|
|
307
|
+
status: 404,
|
|
308
|
+
continue: true,
|
|
309
|
+
missing: [{ type: "header", key: "x-prerender-revalidate" }]
|
|
310
|
+
});
|
|
311
|
+
routes.push({
|
|
312
|
+
src: `^${basePath}/500$`,
|
|
313
|
+
status: 500,
|
|
314
|
+
continue: true
|
|
315
|
+
});
|
|
316
|
+
routes.push({
|
|
317
|
+
src: `^${basePath}/((?:[^/]+/)*[^/.]+)/$`,
|
|
318
|
+
dest: `${basePath}/$1`,
|
|
319
|
+
continue: true
|
|
320
|
+
});
|
|
321
|
+
routes.push({ handle: "filesystem" });
|
|
322
|
+
if (afterFilesRewrites.length > 0) {
|
|
323
|
+
try {
|
|
324
|
+
routes.push(...convertRewrites(afterFilesRewrites));
|
|
325
|
+
} catch (e) {
|
|
326
|
+
console.warn("[opennext] Warning: Failed to convert afterFiles rewrites:", e);
|
|
184
327
|
}
|
|
185
328
|
}
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
pathsToDelete.push(routePath);
|
|
192
|
-
convertedRouteMap[convertedPath] = routeConfig;
|
|
329
|
+
if (fallbackRewrites.length > 0) {
|
|
330
|
+
try {
|
|
331
|
+
routes.push(...convertRewrites(fallbackRewrites));
|
|
332
|
+
} catch (e) {
|
|
333
|
+
console.warn("[opennext] Warning: Failed to convert fallback rewrites:", e);
|
|
193
334
|
}
|
|
194
335
|
}
|
|
195
|
-
|
|
196
|
-
|
|
336
|
+
if (staticRoutes.length > 0) {
|
|
337
|
+
routes.push(...getServerRoutes(staticRoutes, prerenderManifest, basePath));
|
|
338
|
+
}
|
|
339
|
+
if (dynamicRoutes.length > 0) {
|
|
340
|
+
routes.push(...getDynamicRoutes(dynamicRoutes, basePath));
|
|
197
341
|
}
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
342
|
+
if (dataRoutes.length > 0) {
|
|
343
|
+
routes.push(...getDataRoutes(dataRoutes, basePath));
|
|
344
|
+
}
|
|
345
|
+
const apiRoutes = getApiRoutes(appPathsManifest, basePath);
|
|
346
|
+
if (apiRoutes.length > 0) {
|
|
347
|
+
routes.push(...apiRoutes);
|
|
348
|
+
}
|
|
349
|
+
routes.push({
|
|
350
|
+
src: `^${basePath}/.*$`
|
|
351
|
+
});
|
|
203
352
|
const serverHandlerDir = ctx.serverHandlerRootDir;
|
|
204
353
|
if (!fs.existsSync(serverHandlerDir)) {
|
|
205
354
|
fs.mkdirSync(serverHandlerDir, { recursive: true });
|
|
206
355
|
}
|
|
207
|
-
const
|
|
356
|
+
const nextVersion = ctx.nextVersion || null;
|
|
357
|
+
const imagesManifest = await ctx.getImagesManifest();
|
|
208
358
|
const updatedRedirects = [];
|
|
209
359
|
if (imagesManifest?.images?.path) {
|
|
210
360
|
const imagePath = imagesManifest.images.path;
|
|
@@ -215,21 +365,24 @@ var createRouteMeta = async (ctx) => {
|
|
|
215
365
|
};
|
|
216
366
|
updatedRedirects.push(nextImageRedirect);
|
|
217
367
|
}
|
|
218
|
-
const
|
|
368
|
+
const config = {
|
|
369
|
+
version: 3,
|
|
370
|
+
routes,
|
|
219
371
|
conf: {
|
|
220
372
|
redirects: updatedRedirects
|
|
221
373
|
},
|
|
222
|
-
|
|
374
|
+
...nextVersion ? { framework: { version: nextVersion } } : {}
|
|
223
375
|
};
|
|
376
|
+
const configFilePath = path.join(serverHandlerDir, "config.json");
|
|
224
377
|
fs.writeFileSync(
|
|
225
|
-
|
|
226
|
-
JSON.stringify(
|
|
378
|
+
configFilePath,
|
|
379
|
+
JSON.stringify(config, null, 2),
|
|
227
380
|
"utf-8"
|
|
228
381
|
);
|
|
382
|
+
console.log(`[opennext] Generated ${configFilePath} with ${routes.length} routes`);
|
|
229
383
|
const middlewareConfig = await getMiddlewareConfig(ctx);
|
|
230
|
-
|
|
384
|
+
updateEdgeFunctionsConfigJson(middlewareConfig);
|
|
231
385
|
};
|
|
232
386
|
export {
|
|
233
|
-
convertNextRoutePattern,
|
|
234
387
|
createRouteMeta
|
|
235
388
|
};
|
|
@@ -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.
|
|
31
|
+
var version = "0.1.7";
|
|
32
32
|
|
|
33
33
|
// src/run/handlers/tags-handler.cts
|
|
34
34
|
var import_request_context = require("./request-context.cjs");
|