@edgeone/opennextjs-pages 0.0.1
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/advanced-api-routes.js +147 -0
- package/dist/build/cache.js +36 -0
- package/dist/build/content/next-shims/telemetry-storage.cjs +55 -0
- package/dist/build/content/prerendered.js +292 -0
- package/dist/build/content/server.js +236 -0
- package/dist/build/content/static.js +119 -0
- package/dist/build/functions/server.js +133 -0
- package/dist/build/plugin-context.js +367 -0
- package/dist/build/routes.js +127 -0
- package/dist/build/templates/handler-monorepo.tmpl.js +210 -0
- package/dist/build/templates/handler.tmpl.js +206 -0
- package/dist/esm-chunks/chunk-5J3FID2N.js +5551 -0
- package/dist/esm-chunks/chunk-6BT4RYQJ.js +43 -0
- package/dist/esm-chunks/chunk-FKDTZJRV.js +832 -0
- package/dist/esm-chunks/chunk-TLQCAGE2.js +1921 -0
- package/dist/index.js +50 -0
- package/dist/run/config.js +37 -0
- package/dist/run/constants.js +19 -0
- package/dist/run/handlers/cache.cjs +369 -0
- package/dist/run/handlers/request-context.cjs +148 -0
- package/dist/run/handlers/server.js +3213 -0
- package/dist/run/handlers/tags-handler.cjs +92 -0
- package/dist/run/handlers/tracer.cjs +916 -0
- package/dist/run/handlers/use-cache-handler.js +1538 -0
- package/dist/run/handlers/wait-until.cjs +39 -0
- package/dist/run/headers.js +81 -0
- package/dist/run/next.cjs +100 -0
- package/dist/run/revalidate.js +34 -0
- package/dist/run/storage/regional-blob-store.cjs +64 -0
- package/dist/run/storage/request-scoped-in-memory-cache.cjs +1496 -0
- package/dist/run/storage/storage.cjs +37 -0
- package/dist/shared/blob-types.cjs +37 -0
- package/dist/shared/blobkey.js +25 -0
- package/dist/shared/cache-types.cjs +33 -0
- package/package.json +55 -0
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
|
|
2
|
+
import { createServer } from 'http';
|
|
3
|
+
import {
|
|
4
|
+
createRequestContext,
|
|
5
|
+
runWithRequestContext,
|
|
6
|
+
} from './.edgeone/dist/run/handlers/request-context.cjs';
|
|
7
|
+
import serverHandler from './.edgeone/dist/run/handlers/server.js';
|
|
8
|
+
import { getTracer } from './.edgeone/dist/run/handlers/tracer.cjs';
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
async function handleResponse(res, response, passHeaders = {}) {
|
|
12
|
+
const startTime = Date.now();
|
|
13
|
+
|
|
14
|
+
if (!response) {
|
|
15
|
+
res.writeHead(404);
|
|
16
|
+
res.end(JSON.stringify({
|
|
17
|
+
error: "Not Found",
|
|
18
|
+
message: "The requested path does not exist"
|
|
19
|
+
}));
|
|
20
|
+
const endTime = Date.now();
|
|
21
|
+
console.log(`HandleResponse: 404 Not Found - ${endTime - startTime}ms`);
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
try {
|
|
26
|
+
if (response instanceof Response) {
|
|
27
|
+
const headers = Object.fromEntries(response.headers);
|
|
28
|
+
Object.assign(headers, passHeaders);
|
|
29
|
+
if (response.headers.get('eop-client-geo')) {
|
|
30
|
+
// 删除 eop-client-geo 头部
|
|
31
|
+
response.headers.delete('eop-client-geo');
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// 检查是否是流式响应
|
|
35
|
+
const isStream = response.body && (
|
|
36
|
+
response.headers.get('content-type')?.includes('text/event-stream') ||
|
|
37
|
+
response.headers.get('transfer-encoding')?.includes('chunked') ||
|
|
38
|
+
response.body instanceof ReadableStream ||
|
|
39
|
+
typeof response.body.pipe === 'function' ||
|
|
40
|
+
response.headers.get('x-content-type-stream') === 'true'
|
|
41
|
+
);
|
|
42
|
+
|
|
43
|
+
if (isStream) {
|
|
44
|
+
// 设置流式响应所需的头部
|
|
45
|
+
const streamHeaders = {
|
|
46
|
+
...headers
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
if (response.headers.get('content-type')?.includes('text/event-stream')) {
|
|
50
|
+
streamHeaders['Content-Type'] = 'text/event-stream';
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
res.writeHead(response.status, streamHeaders);
|
|
54
|
+
|
|
55
|
+
if (typeof response.body.pipe === 'function') {
|
|
56
|
+
response.body.pipe(res);
|
|
57
|
+
} else {
|
|
58
|
+
const reader = response.body.getReader();
|
|
59
|
+
try {
|
|
60
|
+
while (true) {
|
|
61
|
+
const { done, value } = await reader.read();
|
|
62
|
+
if (done) break;
|
|
63
|
+
|
|
64
|
+
if (value instanceof Uint8Array || Buffer.isBuffer(value)) {
|
|
65
|
+
res.write(value);
|
|
66
|
+
} else {
|
|
67
|
+
const chunk = new TextDecoder().decode(value);
|
|
68
|
+
res.write(chunk);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
} finally {
|
|
72
|
+
reader.releaseLock();
|
|
73
|
+
res.end();
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
} else {
|
|
77
|
+
// 普通响应
|
|
78
|
+
res.writeHead(response.status, headers);
|
|
79
|
+
const body = await response.text();
|
|
80
|
+
res.end(body);
|
|
81
|
+
}
|
|
82
|
+
} else {
|
|
83
|
+
// 非 Response 对象,直接返回 JSON
|
|
84
|
+
res.writeHead(200, {
|
|
85
|
+
'Content-Type': 'application/json'
|
|
86
|
+
});
|
|
87
|
+
res.end(JSON.stringify(response));
|
|
88
|
+
}
|
|
89
|
+
} catch (error) {
|
|
90
|
+
console.error('HandleResponse error', error);
|
|
91
|
+
// 错误处理
|
|
92
|
+
res.writeHead(500);
|
|
93
|
+
res.end(JSON.stringify({
|
|
94
|
+
error: "Internal Server Error",
|
|
95
|
+
message: error.message
|
|
96
|
+
}));
|
|
97
|
+
} finally {
|
|
98
|
+
const endTime = Date.now();
|
|
99
|
+
console.log(`HandleResponse: ${response?.status || 'unknown'} - ${endTime - startTime}ms`);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
process.env.USE_REGIONAL_BLOBS = 'true';
|
|
105
|
+
export default async function handler(req, context) {
|
|
106
|
+
const requestContext = createRequestContext(req, context);
|
|
107
|
+
const tracer = getTracer();
|
|
108
|
+
|
|
109
|
+
const handlerResponse = await runWithRequestContext(requestContext, () => {
|
|
110
|
+
return tracer.withActiveSpan('Next.js Server Handler', async (span) => {
|
|
111
|
+
const response = await serverHandler(req, context, span, requestContext);
|
|
112
|
+
return response;
|
|
113
|
+
});
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
if (requestContext.serverTiming) {
|
|
117
|
+
handlerResponse.headers.set('server-timing', requestContext.serverTiming);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
return handlerResponse;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
export const config = {
|
|
124
|
+
path: '/*',
|
|
125
|
+
preferStatic: true,
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
const port = 9000;
|
|
129
|
+
// const port = 9000;
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
// 实时流转换函数
|
|
133
|
+
function createReadableStreamFromRequest(req) {
|
|
134
|
+
return new ReadableStream({
|
|
135
|
+
start(controller) {
|
|
136
|
+
req.on('data', chunk => {
|
|
137
|
+
// 将Buffer转换为Uint8Array
|
|
138
|
+
const uint8Array = new Uint8Array(chunk);
|
|
139
|
+
controller.enqueue(uint8Array);
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
req.on('end', () => {
|
|
143
|
+
controller.close();
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
req.on('error', error => {
|
|
147
|
+
controller.error(error);
|
|
148
|
+
});
|
|
149
|
+
},
|
|
150
|
+
|
|
151
|
+
cancel() {
|
|
152
|
+
// 清理资源
|
|
153
|
+
req.destroy();
|
|
154
|
+
}
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
const server = createServer(async (req, res) => {
|
|
159
|
+
try {
|
|
160
|
+
const requestStartTime = Date.now();
|
|
161
|
+
|
|
162
|
+
// 构造 handler 需要的 req 对象(可根据需要扩展)
|
|
163
|
+
const handlerReq = {
|
|
164
|
+
...req,
|
|
165
|
+
headers: {
|
|
166
|
+
...req.headers,
|
|
167
|
+
'accept-encoding': 'identity',
|
|
168
|
+
},
|
|
169
|
+
method: req.method,
|
|
170
|
+
url: req.url,
|
|
171
|
+
};
|
|
172
|
+
|
|
173
|
+
// 读取 body(如果有)
|
|
174
|
+
// let body = [];
|
|
175
|
+
// req.on('data', chunk => body.push(chunk));
|
|
176
|
+
// await new Promise(resolve => req.on('end', resolve));
|
|
177
|
+
// if (body.length > 0) {
|
|
178
|
+
// handlerReq.body = Buffer.concat(body);
|
|
179
|
+
// }
|
|
180
|
+
|
|
181
|
+
handlerReq.body = createReadableStreamFromRequest(req);
|
|
182
|
+
|
|
183
|
+
const response = await handler(handlerReq, {});
|
|
184
|
+
|
|
185
|
+
response.headers.set('functions-request-id', req.headers['x-scf-request-id'] || '');
|
|
186
|
+
|
|
187
|
+
const requestEndTime = Date.now();
|
|
188
|
+
const url = new URL(req.url, `http://${req.headers.host}`);
|
|
189
|
+
console.log(`Request path: ${url.pathname}`);
|
|
190
|
+
console.log(`Request processing time: ${requestEndTime - requestStartTime}ms`);
|
|
191
|
+
await handleResponse(res, response, {
|
|
192
|
+
'functions-request-id': req.headers['x-scf-request-id'] || ''
|
|
193
|
+
});
|
|
194
|
+
return;
|
|
195
|
+
} catch (error) {
|
|
196
|
+
console.error('SSR Error:', error);
|
|
197
|
+
res.statusCode = 500;
|
|
198
|
+
res.setHeader('Content-Type', 'text/html; charset=utf-8');
|
|
199
|
+
res.end('<html><body><h1>Error</h1><p>'+error.message+'</p></body></html>');
|
|
200
|
+
}
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
server.listen(port, () => {
|
|
204
|
+
console.log('Server is running on http://localhost:9000');
|
|
205
|
+
});
|
|
206
|
+
|