@edgeone/opennextjs-pages 0.0.2 → 0.0.3-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.
|
@@ -98,18 +98,18 @@ var getHandlerFile = async (ctx) => {
|
|
|
98
98
|
const templateVariables = {
|
|
99
99
|
"{{useRegionalBlobs}}": ctx.useRegionalBlobs.toString()
|
|
100
100
|
};
|
|
101
|
+
let runtime_shim = "";
|
|
102
|
+
const getRuntimeShim = ctx.getRuntimeShim;
|
|
103
|
+
if (getRuntimeShim) {
|
|
104
|
+
runtime_shim = getRuntimeShim();
|
|
105
|
+
}
|
|
101
106
|
if (ctx.relativeAppDir.length !== 0) {
|
|
102
107
|
const template2 = await readFile(join(templatesDir, "handler-monorepo.tmpl.js"), "utf-8");
|
|
103
108
|
console.log("ctx.lambdaWorkingDirectory", ctx.lambdaWorkingDirectory);
|
|
104
109
|
console.log("ctx.nextServerHandler", ctx.nextServerHandler);
|
|
105
110
|
templateVariables["{{cwd}}"] = posixJoin(ctx.lambdaWorkingDirectory);
|
|
106
111
|
templateVariables["{{nextServerHandler}}"] = posixJoin(ctx.nextServerHandler);
|
|
107
|
-
return applyTemplateVariables(template2, templateVariables);
|
|
108
|
-
}
|
|
109
|
-
let runtime_shim = "";
|
|
110
|
-
const getRuntimeShim = ctx.getRuntimeShim;
|
|
111
|
-
if (getRuntimeShim) {
|
|
112
|
-
runtime_shim = getRuntimeShim();
|
|
112
|
+
return applyTemplateVariables(runtime_shim + template2, templateVariables);
|
|
113
113
|
}
|
|
114
114
|
const template = await readFile(join(templatesDir, "handler.tmpl.js"), "utf-8");
|
|
115
115
|
return applyTemplateVariables(runtime_shim + template, templateVariables);
|
|
@@ -16,8 +16,14 @@ process.env.USE_REGIONAL_BLOBS = '{{useRegionalBlobs}}'
|
|
|
16
16
|
async function handleResponse(res, response, passHeaders = {}) {
|
|
17
17
|
const startTime = Date.now();
|
|
18
18
|
|
|
19
|
+
// 没有响应 - 404 拦截
|
|
19
20
|
if (!response) {
|
|
20
|
-
|
|
21
|
+
const requestId = passHeaders['functions-request-id'] || '';
|
|
22
|
+
res.writeHead(404, {
|
|
23
|
+
'Functions-Request-Id': requestId,
|
|
24
|
+
'eo-pages-inner-scf-status': '404',
|
|
25
|
+
'eo-pages-inner-status-intercept': 'true'
|
|
26
|
+
});
|
|
21
27
|
res.end(JSON.stringify({
|
|
22
28
|
error: "Not Found",
|
|
23
29
|
message: "The requested path does not exist"
|
|
@@ -29,8 +35,25 @@ async function handleResponse(res, response, passHeaders = {}) {
|
|
|
29
35
|
|
|
30
36
|
try {
|
|
31
37
|
if (response instanceof Response) {
|
|
38
|
+
const requestId = passHeaders['functions-request-id'] || '';
|
|
39
|
+
const responseStatus = response.status;
|
|
40
|
+
|
|
32
41
|
const headers = Object.fromEntries(response.headers);
|
|
33
42
|
Object.assign(headers, passHeaders);
|
|
43
|
+
|
|
44
|
+
// 添加状态码区分的 headers
|
|
45
|
+
headers['Functions-Request-Id'] = requestId;
|
|
46
|
+
|
|
47
|
+
// 如果 Response 中已经设置了,使用它的值;否则使用 responseStatus
|
|
48
|
+
if (!headers['eo-pages-inner-scf-status']) {
|
|
49
|
+
headers['eo-pages-inner-scf-status'] = String(responseStatus);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// 如果 Response 中已经设置了,使用它的值;否则默认为 false
|
|
53
|
+
if (!headers['eo-pages-inner-status-intercept']) {
|
|
54
|
+
headers['eo-pages-inner-status-intercept'] = 'false';
|
|
55
|
+
}
|
|
56
|
+
|
|
34
57
|
if (response.headers.get('eop-client-geo')) {
|
|
35
58
|
// 删除 eop-client-geo 头部
|
|
36
59
|
response.headers.delete('eop-client-geo');
|
|
@@ -91,15 +114,24 @@ async function handleResponse(res, response, passHeaders = {}) {
|
|
|
91
114
|
}
|
|
92
115
|
} else {
|
|
93
116
|
// 非 Response 对象,直接返回 JSON
|
|
117
|
+
const requestId = passHeaders['functions-request-id'] || '';
|
|
94
118
|
res.writeHead(200, {
|
|
95
|
-
'Content-Type': 'application/json'
|
|
119
|
+
'Content-Type': 'application/json',
|
|
120
|
+
'Functions-Request-Id': requestId,
|
|
121
|
+
'eo-pages-inner-scf-status': '200',
|
|
122
|
+
'eo-pages-inner-status-intercept': 'false'
|
|
96
123
|
});
|
|
97
124
|
res.end(JSON.stringify(response));
|
|
98
125
|
}
|
|
99
126
|
} catch (error) {
|
|
100
127
|
console.error('HandleResponse error', error);
|
|
101
|
-
//
|
|
102
|
-
|
|
128
|
+
// 用户函数内部错误 内部502 - 拦截
|
|
129
|
+
const requestId = passHeaders['functions-request-id'] || '';
|
|
130
|
+
res.writeHead(502, {
|
|
131
|
+
'Functions-Request-Id': requestId,
|
|
132
|
+
'eo-pages-inner-scf-status': '502',
|
|
133
|
+
'eo-pages-inner-status-intercept': 'true'
|
|
134
|
+
});
|
|
103
135
|
res.end(JSON.stringify({
|
|
104
136
|
error: "Internal Server Error",
|
|
105
137
|
message: error.message
|
|
@@ -191,7 +223,7 @@ const server = createServer(async (req, res) => {
|
|
|
191
223
|
|
|
192
224
|
const response = await eoHandler(handlerReq, {});
|
|
193
225
|
|
|
194
|
-
response.headers.set('functions-request-id', req.headers['x-scf-request-id'] || '');
|
|
226
|
+
// response.headers.set('functions-request-id', req.headers['x-scf-request-id'] || '');
|
|
195
227
|
|
|
196
228
|
const requestEndTime = Date.now();
|
|
197
229
|
const url = new URL(req.url, `http://${req.headers.host}`);
|
|
@@ -203,8 +235,13 @@ const server = createServer(async (req, res) => {
|
|
|
203
235
|
return;
|
|
204
236
|
} catch (error) {
|
|
205
237
|
console.error('SSR Error:', error);
|
|
206
|
-
|
|
238
|
+
// 用户函数内部错误 内部502 - 拦截
|
|
239
|
+
const requestId = req.headers['x-scf-request-id'] || '';
|
|
240
|
+
res.statusCode = 502;
|
|
207
241
|
res.setHeader('Content-Type', 'text/html; charset=utf-8');
|
|
242
|
+
res.setHeader('Functions-Request-Id', requestId);
|
|
243
|
+
res.setHeader('eo-pages-inner-scf-status', '502');
|
|
244
|
+
res.setHeader('eo-pages-inner-status-intercept', 'true');
|
|
208
245
|
res.end('<html><body><h1>Error</h1><p>'+error.message+'</p></body></html>');
|
|
209
246
|
}
|
|
210
247
|
});
|
|
@@ -11,29 +11,51 @@ import { getTracer } from './.edgeone/dist/run/handlers/tracer.cjs';
|
|
|
11
11
|
async function handleResponse(res, response, passHeaders = {}) {
|
|
12
12
|
const startTime = Date.now();
|
|
13
13
|
|
|
14
|
+
// 没有响应 - 404 拦截
|
|
14
15
|
if (!response) {
|
|
15
|
-
|
|
16
|
+
const requestId = passHeaders['functions-request-id'] || '';
|
|
17
|
+
res.writeHead(404, {
|
|
18
|
+
'Functions-Request-Id': requestId,
|
|
19
|
+
'eo-pages-inner-scf-status': '404',
|
|
20
|
+
'eo-pages-inner-status-intercept': 'true'
|
|
21
|
+
});
|
|
16
22
|
res.end(JSON.stringify({
|
|
17
23
|
error: "Not Found",
|
|
18
24
|
message: "The requested path does not exist"
|
|
19
25
|
}));
|
|
20
26
|
const endTime = Date.now();
|
|
21
|
-
console.log(`
|
|
27
|
+
console.log(`Pages response status: 404`);
|
|
22
28
|
return;
|
|
23
29
|
}
|
|
24
30
|
|
|
25
31
|
try {
|
|
26
32
|
if (response instanceof Response) {
|
|
33
|
+
const requestId = passHeaders['functions-request-id'] || '';
|
|
34
|
+
const responseStatus = response.status;
|
|
35
|
+
|
|
27
36
|
const headers = Object.fromEntries(response.headers);
|
|
28
37
|
Object.assign(headers, passHeaders);
|
|
38
|
+
|
|
39
|
+
// 添加状态码区分的 headers
|
|
40
|
+
headers['Functions-Request-Id'] = requestId;
|
|
41
|
+
|
|
42
|
+
// 如果 Response 中已经设置了,使用它的值;否则使用 responseStatus
|
|
43
|
+
if (!headers['eo-pages-inner-scf-status']) {
|
|
44
|
+
headers['eo-pages-inner-scf-status'] = String(responseStatus);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// 如果 Response 中已经设置了,使用它的值;否则默认为 false
|
|
48
|
+
if (!headers['eo-pages-inner-status-intercept']) {
|
|
49
|
+
headers['eo-pages-inner-status-intercept'] = 'false';
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// 删除内部 header
|
|
29
53
|
if (response.headers.get('eop-client-geo')) {
|
|
30
|
-
// 删除 eop-client-geo 头部
|
|
31
54
|
response.headers.delete('eop-client-geo');
|
|
32
55
|
}
|
|
33
56
|
// 处理 set-cookie 头部
|
|
34
57
|
if (response.headers.has('set-cookie')) {
|
|
35
58
|
const cookieArr = response.headers.getSetCookie();
|
|
36
|
-
|
|
37
59
|
headers['set-cookie'] = cookieArr;
|
|
38
60
|
}
|
|
39
61
|
|
|
@@ -76,8 +98,11 @@ import { getTracer } from './.edgeone/dist/run/handlers/tracer.cjs';
|
|
|
76
98
|
}
|
|
77
99
|
} finally {
|
|
78
100
|
reader.releaseLock();
|
|
79
|
-
|
|
80
|
-
|
|
101
|
+
// scf可能会立即冻结环境上下文,导致后续日志无法输出,通过延时来确保日志输出
|
|
102
|
+
setTimeout(() => {
|
|
103
|
+
res.end();
|
|
104
|
+
}, 1);
|
|
105
|
+
}
|
|
81
106
|
}
|
|
82
107
|
} else {
|
|
83
108
|
// 普通响应
|
|
@@ -87,22 +112,30 @@ import { getTracer } from './.edgeone/dist/run/handlers/tracer.cjs';
|
|
|
87
112
|
}
|
|
88
113
|
} else {
|
|
89
114
|
// 非 Response 对象,直接返回 JSON
|
|
115
|
+
const requestId = passHeaders['functions-request-id'] || '';
|
|
90
116
|
res.writeHead(200, {
|
|
91
|
-
'Content-Type': 'application/json'
|
|
117
|
+
'Content-Type': 'application/json',
|
|
118
|
+
'Functions-Request-Id': requestId,
|
|
119
|
+
'eo-pages-inner-scf-status': '200',
|
|
120
|
+
'eo-pages-inner-status-intercept': 'false'
|
|
92
121
|
});
|
|
93
122
|
res.end(JSON.stringify(response));
|
|
94
123
|
}
|
|
95
124
|
} catch (error) {
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
res.writeHead(
|
|
125
|
+
// 用户函数内部错误 内部502 - 拦截
|
|
126
|
+
const requestId = passHeaders['functions-request-id'] || '';
|
|
127
|
+
res.writeHead(502, {
|
|
128
|
+
'Functions-Request-Id': requestId,
|
|
129
|
+
'eo-pages-inner-scf-status': '502',
|
|
130
|
+
'eo-pages-inner-status-intercept': 'true'
|
|
131
|
+
});
|
|
99
132
|
res.end(JSON.stringify({
|
|
100
133
|
error: "Internal Server Error",
|
|
101
134
|
message: error.message
|
|
102
135
|
}));
|
|
103
|
-
} finally {
|
|
136
|
+
} finally {
|
|
104
137
|
const endTime = Date.now();
|
|
105
|
-
console.log(`
|
|
138
|
+
console.log(`Pages response status: ${response?.status || 'unknown'}`);
|
|
106
139
|
}
|
|
107
140
|
}
|
|
108
141
|
|
|
@@ -132,8 +165,6 @@ export const config = {
|
|
|
132
165
|
};
|
|
133
166
|
|
|
134
167
|
const port = 9000;
|
|
135
|
-
// const port = 9000;
|
|
136
|
-
|
|
137
168
|
|
|
138
169
|
// 实时流转换函数
|
|
139
170
|
function createReadableStreamFromRequest(req) {
|
|
@@ -188,20 +219,48 @@ const server = createServer(async (req, res) => {
|
|
|
188
219
|
|
|
189
220
|
const response = await handler(handlerReq, {});
|
|
190
221
|
|
|
191
|
-
|
|
222
|
+
// 不要在这里设置 functions-request-id,避免重复
|
|
223
|
+
// response.headers.set('functions-request-id', req.headers['x-scf-request-id'] || '');
|
|
224
|
+
// const requestEndTime = Date.now();
|
|
192
225
|
|
|
193
|
-
|
|
226
|
+
// 解析请求路径
|
|
194
227
|
const url = new URL(req.url, `http://${req.headers.host}`);
|
|
195
|
-
|
|
196
|
-
|
|
228
|
+
let pathname = url.pathname;
|
|
229
|
+
|
|
230
|
+
if (pathname !== '/' && pathname.endsWith('/')) {
|
|
231
|
+
pathname = pathname.slice(0, -1);
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
let fullPath = '';
|
|
235
|
+
if (req.headers.host === 'localhost:9000') {
|
|
236
|
+
fullPath = pathname;
|
|
237
|
+
} else {
|
|
238
|
+
const host = req.headers['eo-pages-host'];
|
|
239
|
+
const xForwardedProto = req.headers['x-forwarded-proto'];
|
|
240
|
+
|
|
241
|
+
fullPath = (xForwardedProto || 'https') + '://' + host + req.url;
|
|
242
|
+
|
|
243
|
+
if (fullPath.endsWith('?')) {
|
|
244
|
+
fullPath = fullPath.slice(0, -1);
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
console.log(`Pages request path: ${fullPath}`);
|
|
249
|
+
|
|
250
|
+
// console.log(`Request processing time: ${requestEndTime - requestStartTime}ms`);
|
|
197
251
|
await handleResponse(res, response, {
|
|
198
252
|
'functions-request-id': req.headers['x-scf-request-id'] || ''
|
|
199
253
|
});
|
|
200
254
|
return;
|
|
201
255
|
} catch (error) {
|
|
202
|
-
console.
|
|
203
|
-
|
|
256
|
+
console.log(`Pages response status: 502`);
|
|
257
|
+
// 用户函数内部错误 内部502 - 拦截
|
|
258
|
+
const requestId = req.headers['x-scf-request-id'] || '';
|
|
259
|
+
res.statusCode = 502;
|
|
204
260
|
res.setHeader('Content-Type', 'text/html; charset=utf-8');
|
|
261
|
+
res.setHeader('Functions-Request-Id', requestId);
|
|
262
|
+
res.setHeader('eo-pages-inner-scf-status', '502');
|
|
263
|
+
res.setHeader('eo-pages-inner-status-intercept', 'true');
|
|
205
264
|
res.end('<html><body><h1>Error</h1><p>'+error.message+'</p></body></html>');
|
|
206
265
|
}
|
|
207
266
|
});
|
|
@@ -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.2";
|
|
31
|
+
var version = "0.0.3-beta.2";
|
|
32
32
|
|
|
33
33
|
// src/run/handlers/tags-handler.cts
|
|
34
34
|
var import_request_context = require("./request-context.cjs");
|