@edgeone/opennextjs-pages 0.0.9-beta.1 → 0.0.9
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 +3 -10
- package/dist/build/functions/middleware/middleware.js +3 -2
- package/dist/build/functions/middleware/wrapper.js +28 -16
- package/dist/build/functions/server.js +0 -2
- package/dist/build/routes.js +1 -102
- package/dist/index.js +3 -4
- package/dist/run/handlers/tags-handler.cjs +1 -1
- package/package.json +1 -1
|
@@ -2356,17 +2356,10 @@ function transformForEdgeOneRuntime(code) {
|
|
|
2356
2356
|
const patchedConstructor = `class ${className} extends ${parentClass}{constructor(${paramName}){super(${paramName}.input,${paramName}.init),this.sourcePage=${paramName}.page;var __k="__edgeone_nextInternal",__self=this;if(this[__k]){Object.defineProperty(this,"nextUrl",{get:function(){return __self[__k]?__self[__k].nextUrl:undefined},enumerable:true,configurable:true});Object.defineProperty(this,"cookies",{get:function(){return __self[__k]?__self[__k].cookies:undefined},enumerable:true,configurable:true})}}`;
|
|
2357
2357
|
return patchedConstructor;
|
|
2358
2358
|
});
|
|
2359
|
-
const baseClassInternalAssignPattern = /this\[(\w+)\]\s*=\s*\{\s*cookies\s*:\s*new\s
|
|
2360
|
-
transformed = transformed.replace(baseClassInternalAssignPattern, (match, internalVar,
|
|
2359
|
+
const baseClassInternalAssignPattern = /this\[(\w+)\]\s*=\s*\{\s*cookies\s*:\s*new\s+\w+\.RequestCookies\s*\(\s*this\.headers\s*\)\s*,\s*nextUrl\s*:\s*(\w+)\s*,\s*url\s*:\s*\2\.toString\s*\(\s*\)\s*\}/g;
|
|
2360
|
+
transformed = transformed.replace(baseClassInternalAssignPattern, (match, internalVar, _nextUrlVar) => {
|
|
2361
2361
|
transformCount++;
|
|
2362
|
-
const patchedAssignment =
|
|
2363
|
-
return patchedAssignment;
|
|
2364
|
-
});
|
|
2365
|
-
const baseClassInternalAssignPatternMultiline = /this\[(\w+)\]\s*=\s*\{\s*cookies\s*:\s*new\s+[\w.]+\.RequestCookies\s*\(\s*this\.headers\s*\)\s*,\s*geo\s*:\s*[^,]+,\s*ip\s*:\s*[^,]+,\s*nextUrl\s*,\s*url\s*:\s*[^}]+\}/g;
|
|
2366
|
-
transformed = transformed.replace(baseClassInternalAssignPatternMultiline, (match, internalVar) => {
|
|
2367
|
-
transformCount++;
|
|
2368
|
-
const objContent = match.replace(/^this\[\w+\]\s*=\s*/, "");
|
|
2369
|
-
const patchedAssignment = `Object.defineProperty(this,${internalVar},{value:${objContent},writable:true,enumerable:true,configurable:true});var __self=this;Object.defineProperty(this,"nextUrl",{get:function(){return __self[${internalVar}]?__self[${internalVar}].nextUrl:undefined},enumerable:true,configurable:true});Object.defineProperty(this,"cookies",{get:function(){return __self[${internalVar}]?__self[${internalVar}].cookies:undefined},enumerable:true,configurable:true});Object.defineProperty(this,"geo",{get:function(){return __self[${internalVar}]?__self[${internalVar}].geo:undefined},enumerable:true,configurable:true});Object.defineProperty(this,"ip",{get:function(){return __self[${internalVar}]?__self[${internalVar}].ip:undefined},enumerable:true,configurable:true})`;
|
|
2362
|
+
const patchedAssignment = match + `;var __self=this;Object.defineProperty(this,"nextUrl",{get:function(){return __self[${internalVar}]?__self[${internalVar}].nextUrl:undefined},enumerable:true,configurable:true});Object.defineProperty(this,"cookies",{get:function(){return __self[${internalVar}]?__self[${internalVar}].cookies:undefined},enumerable:true,configurable:true})`;
|
|
2370
2363
|
return patchedAssignment;
|
|
2371
2364
|
});
|
|
2372
2365
|
const responseInternalAssignPattern = /this\[(\w+)\]\s*=\s*\{\s*cookies\s*:\s*(\w+)\s*,\s*url\s*:\s*([^}]+)\}/g;
|
|
@@ -32,8 +32,9 @@ var compileMiddleware = async (ctx) => {
|
|
|
32
32
|
}
|
|
33
33
|
}
|
|
34
34
|
if (!middlewareFilePath) {
|
|
35
|
-
console.
|
|
36
|
-
|
|
35
|
+
console.error("[Middleware Compiler] Could not find middleware.js in any of the expected locations:");
|
|
36
|
+
possiblePaths.forEach((p) => console.error(" -", p));
|
|
37
|
+
throw new Error("middleware.js not found");
|
|
37
38
|
}
|
|
38
39
|
const result = await compile(middlewareFilePath, {
|
|
39
40
|
env: { DEBUG: "true" }
|
|
@@ -292,36 +292,48 @@ async function executeMiddleware({request}) {
|
|
|
292
292
|
// \u83B7\u53D6 middleware \u51FD\u6570
|
|
293
293
|
const middlewareFn = await getMiddleware();
|
|
294
294
|
|
|
295
|
-
// \
|
|
296
|
-
//
|
|
297
|
-
const
|
|
298
|
-
request.headers.forEach((value, key) => {
|
|
299
|
-
headersObject[key] = value;
|
|
300
|
-
});
|
|
295
|
+
// \u6784\u9020 Next.js middleware \u671F\u671B\u7684\u8BF7\u6C42\u683C\u5F0F
|
|
296
|
+
// \u6CE8\u610F\uFF1Aheaders \u5FC5\u987B\u662F Headers \u5B9E\u4F8B\uFF0C\u56E0\u4E3A\u7F16\u8BD1\u540E\u7684\u4EE3\u7801\u4F1A\u8C03\u7528 headers.forEach()
|
|
297
|
+
const headersInstance = new Headers(request.headers);
|
|
301
298
|
|
|
302
|
-
|
|
303
|
-
// \u53C2\u8003 Next.js \u6E90\u7801\u4E2D\u7684 adapter \u51FD\u6570
|
|
304
|
-
const middlewareParams = {
|
|
299
|
+
const middlewareRequest = {
|
|
305
300
|
request: {
|
|
306
301
|
url: request.url,
|
|
307
302
|
method: request.method,
|
|
308
|
-
headers:
|
|
303
|
+
headers: headersInstance, // \u5FC5\u987B\u662F Headers \u5B9E\u4F8B\uFF0C\u652F\u6301 forEach
|
|
309
304
|
body: request.body,
|
|
310
305
|
nextConfig: {
|
|
311
306
|
basePath: '',
|
|
312
307
|
i18n: null,
|
|
313
308
|
trailingSlash: false
|
|
314
309
|
},
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
signal: request.signal || null
|
|
310
|
+
signal: request.signal || null,
|
|
311
|
+
waitUntil: (promise) => {}
|
|
318
312
|
},
|
|
319
313
|
page: '/',
|
|
320
|
-
|
|
321
|
-
|
|
314
|
+
waitUntil: (promise) => {},
|
|
315
|
+
bypassNextUrl: false,
|
|
316
|
+
// \u6DFB\u52A0 rewrite \u65B9\u6CD5 - \u7F16\u8BD1\u540E\u7684 middleware \u9700\u8981\u8C03\u7528\u6B64\u65B9\u6CD5
|
|
317
|
+
rewrite: (url) => {
|
|
318
|
+
const headers = new Headers();
|
|
319
|
+
headers.set("x-middleware-rewrite", typeof url === 'string' ? url : url.toString());
|
|
320
|
+
return new Response(null, { headers });
|
|
321
|
+
},
|
|
322
|
+
// \u6DFB\u52A0 redirect \u65B9\u6CD5
|
|
323
|
+
redirect: (url, status = 307) => {
|
|
324
|
+
return new Response(null, {
|
|
325
|
+
status,
|
|
326
|
+
headers: { Location: typeof url === 'string' ? url : url.toString() }
|
|
327
|
+
});
|
|
328
|
+
},
|
|
329
|
+
// \u6DFB\u52A0 next \u65B9\u6CD5
|
|
330
|
+
next: () => {
|
|
331
|
+
return new Response(null, { headers: { 'x-middleware-next': '1' } });
|
|
332
|
+
},
|
|
333
|
+
IncrementalCache: null
|
|
322
334
|
};
|
|
323
335
|
|
|
324
|
-
const result = await middlewareFn(
|
|
336
|
+
const result = await middlewareFn(middlewareRequest);
|
|
325
337
|
|
|
326
338
|
// Webpack \u6A21\u5F0F\u8FD4\u56DE\u7684\u662F { response: Response, waitUntil: {} } \u683C\u5F0F
|
|
327
339
|
// \u9700\u8981\u63D0\u53D6\u5B9E\u9645\u7684 Response \u5BF9\u8C61
|
|
@@ -100,8 +100,6 @@ var getHandlerFile = async (ctx) => {
|
|
|
100
100
|
};
|
|
101
101
|
if (ctx.relativeAppDir.length !== 0) {
|
|
102
102
|
const template2 = await readFile(join(templatesDir, "handler-monorepo.tmpl.js"), "utf-8");
|
|
103
|
-
console.log("ctx.lambdaWorkingDirectory", ctx.lambdaWorkingDirectory);
|
|
104
|
-
console.log("ctx.nextServerHandler", ctx.nextServerHandler);
|
|
105
103
|
templateVariables["{{cwd}}"] = posixJoin(ctx.lambdaWorkingDirectory);
|
|
106
104
|
templateVariables["{{nextServerHandler}}"] = posixJoin(ctx.nextServerHandler);
|
|
107
105
|
return applyTemplateVariables(template2, templateVariables);
|
package/dist/build/routes.js
CHANGED
|
@@ -9,105 +9,6 @@ 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
|
-
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));
|
|
24
|
-
}
|
|
25
|
-
async function getMiddlewareConfig(ctx) {
|
|
26
|
-
try {
|
|
27
|
-
const manifest = await ctx.getMiddlewareManifest();
|
|
28
|
-
if (manifest && manifest.middleware && manifest.middleware["/"]) {
|
|
29
|
-
const middlewareInfo = manifest.middleware["/"];
|
|
30
|
-
const matchers = middlewareInfo.matchers || [];
|
|
31
|
-
const normalizedMatchers = matchers.map((m) => ({
|
|
32
|
-
source: m.originalSource || "/:path*",
|
|
33
|
-
regex: m.regexp
|
|
34
|
-
}));
|
|
35
|
-
const hasIncompatibleRegex = normalizedMatchers.some(
|
|
36
|
-
(item) => item.regex && !isRE2Compatible(item.regex)
|
|
37
|
-
);
|
|
38
|
-
if (hasIncompatibleRegex) {
|
|
39
|
-
return {
|
|
40
|
-
runtime: "edge",
|
|
41
|
-
matcher: [{ source: "/:path*" }]
|
|
42
|
-
};
|
|
43
|
-
}
|
|
44
|
-
return {
|
|
45
|
-
runtime: "edge",
|
|
46
|
-
matcher: normalizedMatchers.map((item) => ({ source: item.source }))
|
|
47
|
-
};
|
|
48
|
-
}
|
|
49
|
-
const possibleFunctionsConfigPaths = [
|
|
50
|
-
path.join(process.cwd(), ".next/server/functions-config-manifest.json"),
|
|
51
|
-
path.join(ctx.distDir, "server/functions-config-manifest.json")
|
|
52
|
-
];
|
|
53
|
-
let functionsConfigPath = "";
|
|
54
|
-
for (const p of possibleFunctionsConfigPaths) {
|
|
55
|
-
if (fs.existsSync(p)) {
|
|
56
|
-
functionsConfigPath = p;
|
|
57
|
-
break;
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
if (functionsConfigPath) {
|
|
61
|
-
const functionsConfig = JSON.parse(fs.readFileSync(functionsConfigPath, "utf-8"));
|
|
62
|
-
const middlewareConfig = functionsConfig?.functions?.["/_middleware"];
|
|
63
|
-
if (middlewareConfig && middlewareConfig.matchers) {
|
|
64
|
-
const matchers = middlewareConfig.matchers;
|
|
65
|
-
const normalizedMatchers = matchers.map((m) => ({
|
|
66
|
-
source: m.originalSource || "/:path*",
|
|
67
|
-
regex: m.regexp
|
|
68
|
-
}));
|
|
69
|
-
const hasIncompatibleRegex = normalizedMatchers.some(
|
|
70
|
-
(item) => item.regex && !isRE2Compatible(item.regex)
|
|
71
|
-
);
|
|
72
|
-
if (hasIncompatibleRegex) {
|
|
73
|
-
return {
|
|
74
|
-
runtime: middlewareConfig.runtime || "edge",
|
|
75
|
-
matcher: [{ source: "/:path*" }]
|
|
76
|
-
};
|
|
77
|
-
}
|
|
78
|
-
return {
|
|
79
|
-
runtime: middlewareConfig.runtime || "edge",
|
|
80
|
-
matcher: normalizedMatchers.map((item) => ({ source: item.source }))
|
|
81
|
-
};
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
return null;
|
|
85
|
-
} catch (error) {
|
|
86
|
-
console.log("[Middleware] Failed to read middleware config:", error);
|
|
87
|
-
return null;
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
function updateEdgeFunctionsMetaJson(middlewareConfig) {
|
|
91
|
-
const metaJsonPath = path.join(process.cwd(), ".edgeone/edge-functions/meta.json");
|
|
92
|
-
let meta = { routes: [] };
|
|
93
|
-
if (fs.existsSync(metaJsonPath)) {
|
|
94
|
-
try {
|
|
95
|
-
const content = fs.readFileSync(metaJsonPath, "utf-8");
|
|
96
|
-
meta = JSON.parse(content);
|
|
97
|
-
} catch (error) {
|
|
98
|
-
console.log("[Middleware] Failed to parse existing meta.json, creating new one");
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
if (middlewareConfig) {
|
|
102
|
-
meta.middleware = middlewareConfig;
|
|
103
|
-
console.log("[Middleware] Updated meta.json with middleware config:", JSON.stringify(middlewareConfig.matcher));
|
|
104
|
-
}
|
|
105
|
-
const edgeFunctionsDir = path.dirname(metaJsonPath);
|
|
106
|
-
if (!fs.existsSync(edgeFunctionsDir)) {
|
|
107
|
-
fs.mkdirSync(edgeFunctionsDir, { recursive: true });
|
|
108
|
-
}
|
|
109
|
-
fs.writeFileSync(metaJsonPath, JSON.stringify(meta, null, 2), "utf-8");
|
|
110
|
-
}
|
|
111
12
|
var convertNextRoutePattern = (path2) => {
|
|
112
13
|
if (!path2.includes("[")) {
|
|
113
14
|
return path2;
|
|
@@ -196,7 +97,7 @@ var createRouteMeta = async (ctx) => {
|
|
|
196
97
|
path: path2,
|
|
197
98
|
...config
|
|
198
99
|
}));
|
|
199
|
-
const serverHandlerDir = ctx.
|
|
100
|
+
const serverHandlerDir = ctx.serverHandlerDir;
|
|
200
101
|
if (!fs.existsSync(serverHandlerDir)) {
|
|
201
102
|
fs.mkdirSync(serverHandlerDir, { recursive: true });
|
|
202
103
|
}
|
|
@@ -209,8 +110,6 @@ var createRouteMeta = async (ctx) => {
|
|
|
209
110
|
JSON.stringify(metaData, null, 2),
|
|
210
111
|
"utf-8"
|
|
211
112
|
);
|
|
212
|
-
const middlewareConfig = await getMiddlewareConfig(ctx);
|
|
213
|
-
updateEdgeFunctionsMetaJson(middlewareConfig);
|
|
214
113
|
};
|
|
215
114
|
export {
|
|
216
115
|
convertNextRoutePattern,
|
package/dist/index.js
CHANGED
|
@@ -36,14 +36,13 @@ var onBuild = async (options) => {
|
|
|
36
36
|
// copyStaticAssets(ctx),
|
|
37
37
|
copyStaticContent(ctx),
|
|
38
38
|
copyPrerenderedContent(ctx),
|
|
39
|
-
createServerHandler(ctx)
|
|
39
|
+
createServerHandler(ctx),
|
|
40
|
+
createRouteMeta(ctx)
|
|
40
41
|
]);
|
|
41
|
-
await createRouteMeta(ctx);
|
|
42
42
|
};
|
|
43
43
|
var onPostBuild = async (options) => {
|
|
44
|
-
console.log("onPostBuild");
|
|
45
44
|
const ctx = new PluginContext(options);
|
|
46
|
-
|
|
45
|
+
compileMiddleware(ctx);
|
|
47
46
|
};
|
|
48
47
|
export {
|
|
49
48
|
onBuild,
|
|
@@ -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.0.9
|
|
31
|
+
var version = "0.0.9";
|
|
32
32
|
|
|
33
33
|
// src/run/handlers/tags-handler.cts
|
|
34
34
|
var import_request_context = require("./request-context.cjs");
|