@edgeone/opennextjs-pages 0.1.4 → 0.1.5-beta.2
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 +43 -4
- package/dist/build/functions/middleware/middleware.js +12 -7
- package/dist/build/functions/middleware/polyfills/index.js +24 -0
- package/dist/build/functions/middleware/wrapper.js +116 -55
- package/dist/build/plugin-context.js +44 -13
- package/dist/build/route-utils.js +698 -0
- package/dist/build/routes.js +203 -100
- package/dist/run/handlers/tags-handler.cjs +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,698 @@
|
|
|
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
|
+
};
|