@@ -2,6 +2,7 @@ import { describe, expect, test } from "vitest";
2
2
3
3
import { computePatchDiff } from "../../utils/test-patch.js" ;
4
4
import {
5
+ attachRequestMetaRule ,
5
6
buildIdRule ,
6
7
createCacheHandlerRule ,
7
8
createComposableCacheHandlersRule ,
@@ -106,6 +107,64 @@ class NextNodeServer extends _baseserver.default {
106
107
}
107
108
}
108
109
}
110
+ getPrerenderManifest() {
111
+ var _this_renderOpts, _this_serverOptions;
112
+ if (this._cachedPreviewManifest) {
113
+ return this._cachedPreviewManifest;
114
+ }
115
+ if (((_this_renderOpts = this.renderOpts) == null ? void 0 : _this_renderOpts.dev) || ((_this_serverOptions = this.serverOptions) == null ? void 0 : _this_serverOptions.dev) || process.env.NODE_ENV === 'development' || process.env.NEXT_PHASE === _constants.PHASE_PRODUCTION_BUILD) {
116
+ this._cachedPreviewManifest = {
117
+ version: 4,
118
+ routes: {},
119
+ dynamicRoutes: {},
120
+ notFoundRoutes: [],
121
+ preview: {
122
+ previewModeId: require('crypto').randomBytes(16).toString('hex'),
123
+ previewModeSigningKey: require('crypto').randomBytes(32).toString('hex'),
124
+ previewModeEncryptionKey: require('crypto').randomBytes(32).toString('hex')
125
+ }
126
+ };
127
+ return this._cachedPreviewManifest;
128
+ }
129
+ this._cachedPreviewManifest = (0, _loadmanifest.loadManifest)((0, _path.join)(this.distDir, _constants.PRERENDER_MANIFEST));
130
+ return this._cachedPreviewManifest;
131
+ }
132
+ getRoutesManifest() {
133
+ return (0, _tracer.getTracer)().trace(_constants2.NextNodeServerSpan.getRoutesManifest, ()=>{
134
+ const manifest = (0, _loadmanifest.loadManifest)((0, _path.join)(this.distDir, _constants.ROUTES_MANIFEST));
135
+ let rewrites = manifest.rewrites ?? {
136
+ beforeFiles: [],
137
+ afterFiles: [],
138
+ fallback: []
139
+ };
140
+ if (Array.isArray(rewrites)) {
141
+ rewrites = {
142
+ beforeFiles: [],
143
+ afterFiles: rewrites,
144
+ fallback: []
145
+ };
146
+ }
147
+ return {
148
+ ...manifest,
149
+ rewrites
150
+ };
151
+ });
152
+ }
153
+ attachRequestMeta(req, parsedUrl, isUpgradeReq) {
154
+ var _req_headers_xforwardedproto;
155
+ // Injected in base-server.ts
156
+ const protocol = ((_req_headers_xforwardedproto = req.headers['x-forwarded-proto']) == null ? void 0 : _req_headers_xforwardedproto.includes('https')) ? 'https' : 'http';
157
+ // When there are hostname and port we build an absolute URL
158
+ const initUrl = this.fetchHostname && this.port ? \`\${protocol}://\${this.fetchHostname}:\${this.port}\${req.url}\` : this.nextConfig.experimental.trustHostHeader ? \`https://\${req.headers.host || "localhost"}\${req.url}\` : req.url;
159
+ (0, _requestmeta.addRequestMeta)(req, 'initURL', initUrl);
160
+ (0, _requestmeta.addRequestMeta)(req, 'initQuery', {
161
+ ...parsedUrl.query
162
+ });
163
+ (0, _requestmeta.addRequestMeta)(req, 'initProtocol', protocol);
164
+ if (!isUpgradeReq) {
165
+ (0, _requestmeta.addRequestMeta)(req, 'clonableBody', (0, _bodystreams.getCloneableBody)(req.originalRequest));
166
+ }
167
+ }
109
168
// ...
110
169
}` ;
111
170
@@ -212,6 +271,48 @@ class NextNodeServer extends _baseserver.default {
212
271
test ( "disable node middleware" , ( ) => {
213
272
expect ( computePatchDiff ( "next-server.js" , nextServerCode , disableNodeMiddlewareRule ) )
214
273
. toMatchInlineSnapshot ( `
274
+ "Index: next-server.js
275
+ ===================================================================
276
+ --- next-server.js
277
+ +++ next-server.js
278
+ @@ -1,5 +1,4 @@
279
+ -
280
+ class NextNodeServer extends _baseserver.default {
281
+ constructor(options){
282
+ // Initialize super class
283
+ super(options);
284
+ @@ -79,23 +78,10 @@
285
+ pages: (0, _findpagesdir.findDir)(dir, "pages") ? true : false
286
+ };
287
+ }
288
+ async loadNodeMiddleware() {
289
+ - if (!process.env.NEXT_MINIMAL) {
290
+ - try {
291
+ - var _functionsConfig_functions;
292
+ - const functionsConfig = this.renderOpts.dev ? {} : require((0, _path.join)(this.distDir, 'server', _constants.FUNCTIONS_CONFIG_MANIFEST));
293
+ - if (this.renderOpts.dev || (functionsConfig == null ? void 0 : (_functionsConfig_functions = functionsConfig.functions) == null ? void 0 : _functionsConfig_functions['/_middleware'])) {
294
+ - // if used with top level await, this will be a promise
295
+ - return require((0, _path.join)(this.distDir, 'server', 'middleware.js'));
296
+ - }
297
+ - } catch (err) {
298
+ - if ((0, _iserror.default)(err) && err.code !== 'ENOENT' && err.code !== 'MODULE_NOT_FOUND') {
299
+ - throw err;
300
+ - }
301
+ - }
302
+ - }
303
+ - }
304
+ + // patched by open next
305
+ +}
306
+ getPrerenderManifest() {
307
+ var _this_renderOpts, _this_serverOptions;
308
+ if (this._cachedPreviewManifest) {
309
+ return this._cachedPreviewManifest;
310
+ "
311
+ ` ) ;
312
+ } ) ;
313
+
314
+ test ( "attachRequestMeta" , ( ) => {
315
+ expect ( computePatchDiff ( "next-server.js" , nextServerCode , attachRequestMetaRule ) ) . toMatchInlineSnapshot ( `
215
316
"Index: next-server.js
216
317
===================================================================
217
318
--- next-server.js
@@ -222,31 +323,17 @@ class NextNodeServer extends _baseserver.default {
222
323
constructor(options){
223
324
// Initialize super class
224
325
super(options);
225
- @@ -79,21 +78,8 @@
226
- pages: (0, _findpagesdir.findDir)(dir, "pages") ? true : false
227
- };
228
- }
229
- async loadNodeMiddleware() {
230
- - if (!process.env.NEXT_MINIMAL) {
231
- - try {
232
- - var _functionsConfig_functions;
233
- - const functionsConfig = this.renderOpts.dev ? {} : require((0, _path.join)(this.distDir, 'server', _constants.FUNCTIONS_CONFIG_MANIFEST));
234
- - if (this.renderOpts.dev || (functionsConfig == null ? void 0 : (_functionsConfig_functions = functionsConfig.functions) == null ? void 0 : _functionsConfig_functions['/_middleware'])) {
235
- - // if used with top level await, this will be a promise
236
- - return require((0, _path.join)(this.distDir, 'server', 'middleware.js'));
237
- - }
238
- - } catch (err) {
239
- - if ((0, _iserror.default)(err) && err.code !== 'ENOENT' && err.code !== 'MODULE_NOT_FOUND') {
240
- - throw err;
241
- - }
242
- - }
243
- - }
244
- - }
245
- + // patched by open next
246
- +}
247
- // ...
248
- }
249
- \\ No newline at end of file
326
+ @@ -143,9 +142,9 @@
327
+ // Injected in base-server.ts
328
+ const protocol = ((_req_headers_xforwardedproto = req.headers['x-forwarded-proto']) == null ? void 0 : _req_headers_xforwardedproto.includes('https')) ? 'https' : 'http';
329
+ // When there are hostname and port we build an absolute URL
330
+ const initUrl = this.fetchHostname && this.port ? \`\${protocol}://\${this.fetchHostname}:\${this.port}\${req.url}\` : this.nextConfig.experimental.trustHostHeader ? \`https://\${req.headers.host || "localhost"}\${req.url}\` : req.url;
331
+ - (0, _requestmeta.addRequestMeta)(req, 'initURL', initUrl);
332
+ + (0, _requestmeta.addRequestMeta)(req, 'initURL', req[Symbol.for("NextInternalRequestMeta")]?.initProtocol === "http:" && initUrl.startsWith("https://") ? \`http://\${initUrl.slice(8)}\`: initUrl);
333
+ (0, _requestmeta.addRequestMeta)(req, 'initQuery', {
334
+ ...parsedUrl.query
335
+ });
336
+ (0, _requestmeta.addRequestMeta)(req, 'initProtocol', protocol);
250
337
"
251
338
` ) ;
252
339
} ) ;
0 commit comments