@bleedingdev/modern-js-runtime 3.9.0-ultramodern.9 → 3.9.3-ultramodern.10
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/cjs/cli/code.js +2 -2
- package/dist/cjs/cli/index.js +0 -7
- package/dist/cjs/cli/ssr/loadable-bundler-plugin.js +2 -0
- package/dist/cjs/cli/template.server.js +1 -2
- package/dist/cjs/core/browser/hydrate.js +3 -9
- package/dist/cjs/core/browser/index.js +4 -5
- package/dist/cjs/core/context/index.js +8 -2
- package/dist/cjs/core/context/public.js +4 -4
- package/dist/cjs/core/context/runtime.js +3 -9
- package/dist/cjs/core/index.js +3 -3
- package/dist/cjs/core/plugin/index.js +2 -2
- package/dist/cjs/core/react/index.js +3 -3
- package/dist/cjs/core/react/wrapper.js +7 -8
- package/dist/cjs/core/server/requestHandler.js +253 -25
- package/dist/cjs/core/server/stream/beforeTemplate.js +3 -3
- package/dist/cjs/core/server/stream/beforeTemplate.worker.js +2 -2
- package/dist/cjs/core/server/stream/createReadableStream.js +51 -19
- package/dist/cjs/core/server/stream/createReadableStream.worker.js +54 -27
- package/dist/cjs/core/server/stream/deferredScript.js +12 -6
- package/dist/cjs/core/server/stream/deferredScriptOutputCoordinator.js +370 -0
- package/dist/cjs/core/server/stream/shared.js +4 -0
- package/dist/cjs/core/server/string/index.js +6 -4
- package/dist/cjs/core/server/string/loadable.js +4 -4
- package/dist/cjs/document/cli/index.js +14 -5
- package/dist/cjs/exports/head.js +2 -2
- package/dist/cjs/internal.js +2 -2
- package/dist/cjs/router/cli/code/nestedRoutes.js +1 -2
- package/dist/cjs/router/cli/code/templates.js +2 -2
- package/dist/cjs/router/cli/code/utils.js +5 -4
- package/dist/cjs/router/runtime/DeferredDataScripts.node.js +13 -10
- package/dist/cjs/router/runtime/PrefetchLink.js +4 -4
- package/dist/cjs/router/runtime/hooks.js +6 -7
- package/dist/cjs/router/runtime/plugin.js +22 -27
- package/dist/cjs/router/runtime/plugin.node.js +8 -9
- package/dist/esm/cli/code.mjs +2 -2
- package/dist/esm/cli/index.mjs +0 -7
- package/dist/esm/cli/ssr/loadable-bundler-plugin.mjs +2 -0
- package/dist/esm/cli/template.server.mjs +1 -2
- package/dist/esm/core/browser/hydrate.mjs +3 -9
- package/dist/esm/core/browser/index.mjs +1 -2
- package/dist/esm/core/context/index.mjs +1 -1
- package/dist/esm/core/context/public.mjs +1 -1
- package/dist/esm/core/context/runtime.mjs +3 -9
- package/dist/esm/core/index.mjs +1 -1
- package/dist/esm/core/plugin/index.mjs +1 -1
- package/dist/esm/core/react/index.mjs +1 -1
- package/dist/esm/core/react/wrapper.mjs +1 -2
- package/dist/esm/core/server/requestHandler.mjs +223 -19
- package/dist/esm/core/server/stream/beforeTemplate.mjs +3 -3
- package/dist/esm/core/server/stream/beforeTemplate.worker.mjs +2 -2
- package/dist/esm/core/server/stream/createReadableStream.mjs +48 -16
- package/dist/esm/core/server/stream/createReadableStream.worker.mjs +53 -26
- package/dist/esm/core/server/stream/deferredScript.mjs +12 -6
- package/dist/esm/core/server/stream/deferredScriptOutputCoordinator.mjs +332 -0
- package/dist/esm/core/server/stream/shared.mjs +2 -1
- package/dist/esm/core/server/string/index.mjs +3 -1
- package/dist/esm/core/server/string/loadable.mjs +4 -4
- package/dist/esm/document/cli/index.mjs +12 -6
- package/dist/esm/exports/head.mjs +1 -1
- package/dist/esm/internal.mjs +1 -1
- package/dist/esm/router/cli/code/nestedRoutes.mjs +1 -2
- package/dist/esm/router/cli/code/templates.mjs +2 -2
- package/dist/esm/router/cli/code/utils.mjs +6 -5
- package/dist/esm/router/runtime/DeferredDataScripts.node.mjs +13 -10
- package/dist/esm/router/runtime/PrefetchLink.mjs +1 -1
- package/dist/esm/router/runtime/hooks.mjs +6 -7
- package/dist/esm/router/runtime/plugin.mjs +14 -19
- package/dist/esm/router/runtime/plugin.node.mjs +3 -4
- package/dist/esm-node/cli/code.mjs +2 -2
- package/dist/esm-node/cli/index.mjs +1 -9
- package/dist/esm-node/cli/ssr/loadable-bundler-plugin.mjs +2 -0
- package/dist/esm-node/cli/template.server.mjs +1 -2
- package/dist/esm-node/core/browser/hydrate.mjs +3 -9
- package/dist/esm-node/core/browser/index.mjs +1 -2
- package/dist/esm-node/core/context/index.mjs +1 -1
- package/dist/esm-node/core/context/public.mjs +1 -1
- package/dist/esm-node/core/context/runtime.mjs +3 -9
- package/dist/esm-node/core/index.mjs +1 -1
- package/dist/esm-node/core/plugin/index.mjs +1 -1
- package/dist/esm-node/core/react/index.mjs +1 -1
- package/dist/esm-node/core/react/wrapper.mjs +1 -2
- package/dist/esm-node/core/server/requestHandler.mjs +223 -19
- package/dist/esm-node/core/server/stream/beforeTemplate.mjs +3 -3
- package/dist/esm-node/core/server/stream/beforeTemplate.worker.mjs +2 -2
- package/dist/esm-node/core/server/stream/createReadableStream.mjs +48 -16
- package/dist/esm-node/core/server/stream/createReadableStream.worker.mjs +53 -26
- package/dist/esm-node/core/server/stream/deferredScript.mjs +12 -6
- package/dist/esm-node/core/server/stream/deferredScriptOutputCoordinator.mjs +333 -0
- package/dist/esm-node/core/server/stream/shared.mjs +2 -1
- package/dist/esm-node/core/server/string/index.mjs +3 -1
- package/dist/esm-node/core/server/string/loadable.mjs +4 -4
- package/dist/esm-node/document/cli/index.mjs +12 -6
- package/dist/esm-node/exports/head.mjs +1 -1
- package/dist/esm-node/internal.mjs +1 -1
- package/dist/esm-node/router/cli/code/nestedRoutes.mjs +1 -2
- package/dist/esm-node/router/cli/code/templates.mjs +2 -2
- package/dist/esm-node/router/cli/code/utils.mjs +6 -5
- package/dist/esm-node/router/runtime/DeferredDataScripts.node.mjs +13 -10
- package/dist/esm-node/router/runtime/PrefetchLink.mjs +1 -1
- package/dist/esm-node/router/runtime/hooks.mjs +6 -7
- package/dist/esm-node/router/runtime/plugin.mjs +14 -19
- package/dist/esm-node/router/runtime/plugin.node.mjs +3 -4
- package/dist/types/common.d.ts +2 -8
- package/dist/types/core/context/index.d.ts +1 -1
- package/dist/types/core/context/public.d.ts +1 -1
- package/dist/types/core/index.d.ts +1 -1
- package/dist/types/core/react/wrapper.d.ts +1 -1
- package/dist/types/core/server/requestHandler.d.ts +30 -1
- package/dist/types/core/server/stream/deferredScript.d.ts +1 -1
- package/dist/types/core/server/stream/deferredScriptOutputCoordinator.d.ts +72 -0
- package/dist/types/core/server/stream/shared.d.ts +14 -0
- package/dist/types/document/cli/index.d.ts +1 -0
- package/dist/types/internal.d.ts +1 -1
- package/dist/types/router/cli/code/utils.d.ts +1 -1
- package/dist/types/router/runtime/hooks.d.ts +12 -16
- package/package.json +34 -29
- package/rslib.config.mts +3 -0
- package/rstest.config.mts +0 -4
- package/dist/cjs/core/server/requestResponse.js +0 -107
- package/dist/cjs/core/server/routerCleanup.js +0 -129
- package/dist/cjs/rsc/server.worker.js +0 -62
- package/dist/esm/core/server/requestResponse.mjs +0 -63
- package/dist/esm/core/server/routerCleanup.mjs +0 -82
- package/dist/esm/rsc/server.worker.mjs +0 -1
- package/dist/esm-node/core/server/requestResponse.mjs +0 -64
- package/dist/esm-node/core/server/routerCleanup.mjs +0 -83
- package/dist/esm-node/rsc/server.worker.mjs +0 -2
- package/dist/types/core/server/requestResponse.d.ts +0 -15
- package/dist/types/core/server/routerCleanup.d.ts +0 -31
- package/dist/types/rsc/server.worker.d.ts +0 -1
|
@@ -37,29 +37,217 @@ var __webpack_require__ = {};
|
|
|
37
37
|
var __webpack_exports__ = {};
|
|
38
38
|
__webpack_require__.r(__webpack_exports__);
|
|
39
39
|
__webpack_require__.d(__webpack_exports__, {
|
|
40
|
-
|
|
40
|
+
REQUEST_END_ERROR: ()=>REQUEST_END_ERROR,
|
|
41
|
+
RESPONSE_BODY_CANCEL_ERROR: ()=>RESPONSE_BODY_CANCEL_ERROR,
|
|
42
|
+
applyRouterResult: ()=>applyRouterResult,
|
|
43
|
+
createLoaderRedirectResponse: ()=>createLoaderRedirectResponse,
|
|
44
|
+
createRequestHandler: ()=>createRequestHandler,
|
|
45
|
+
createRequestLifecycle: ()=>createRequestLifecycle,
|
|
46
|
+
finalizeRenderResponse: ()=>finalizeRenderResponse,
|
|
47
|
+
finishWithRequestLifecycle: ()=>finishWithRequestLifecycle,
|
|
48
|
+
runWithRequestLifecycleOnError: ()=>runWithRequestLifecycleOnError
|
|
41
49
|
});
|
|
42
50
|
const jsx_runtime_namespaceObject = require("react/jsx-runtime");
|
|
51
|
+
const context_namespaceObject = require("@modern-js/runtime/context");
|
|
43
52
|
const node_namespaceObject = require("@modern-js/runtime-utils/node");
|
|
44
53
|
const request_namespaceObject = require("@modern-js/runtime-utils/universal/request");
|
|
45
54
|
const external_react_namespaceObject = require("react");
|
|
46
55
|
var external_react_default = /*#__PURE__*/ __webpack_require__.n(external_react_namespaceObject);
|
|
47
|
-
const
|
|
48
|
-
const
|
|
49
|
-
const serverPayload_index_js_namespaceObject = require("../context/serverPayload/index.js");
|
|
56
|
+
const redirect_js_namespaceObject = require("../../router/runtime/redirect.js");
|
|
57
|
+
const index_js_namespaceObject = require("../context/serverPayload/index.js");
|
|
50
58
|
const external_react_index_js_namespaceObject = require("../react/index.js");
|
|
51
59
|
const external_constants_js_namespaceObject = require("./constants.js");
|
|
52
|
-
const
|
|
53
|
-
const external_routerCleanup_js_namespaceObject = require("./routerCleanup.js");
|
|
60
|
+
const external_tracer_js_namespaceObject = require("./tracer.js");
|
|
54
61
|
const external_utils_js_namespaceObject = require("./utils.js");
|
|
62
|
+
const REQUEST_END_ERROR = 'An error occurs during request lifecycle completion';
|
|
63
|
+
const RESPONSE_BODY_CANCEL_ERROR = 'An error occurs while cancelling a discarded response body';
|
|
64
|
+
async function runWithRequestLifecycleOnError(lifecycle, callback) {
|
|
65
|
+
try {
|
|
66
|
+
return await callback();
|
|
67
|
+
} catch (error) {
|
|
68
|
+
await lifecycle.run({
|
|
69
|
+
status: 'error',
|
|
70
|
+
error
|
|
71
|
+
});
|
|
72
|
+
throw error;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
async function finishWithRequestLifecycle(lifecycle, callback) {
|
|
76
|
+
try {
|
|
77
|
+
return await callback();
|
|
78
|
+
} catch (error) {
|
|
79
|
+
if (!lifecycle.deferred) await lifecycle.run({
|
|
80
|
+
status: 'error',
|
|
81
|
+
error
|
|
82
|
+
});
|
|
83
|
+
throw error;
|
|
84
|
+
} finally{
|
|
85
|
+
if (!lifecycle.deferred) await lifecycle.run();
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
function createRequestLifecycle(onEnd, onError) {
|
|
89
|
+
let deferred = false;
|
|
90
|
+
let finished;
|
|
91
|
+
const run = (terminal = {
|
|
92
|
+
status: 'complete'
|
|
93
|
+
})=>{
|
|
94
|
+
finished ??= Promise.resolve().then(()=>onEnd(terminal)).catch((error)=>{
|
|
95
|
+
onError(error, REQUEST_END_ERROR);
|
|
96
|
+
});
|
|
97
|
+
return finished;
|
|
98
|
+
};
|
|
99
|
+
const deferUntilBodyDone = (response)=>{
|
|
100
|
+
const { body } = response;
|
|
101
|
+
if (!body) return response;
|
|
102
|
+
deferred = true;
|
|
103
|
+
if (body.locked) throw new TypeError('Cannot observe a locked response body before request completion');
|
|
104
|
+
let cancelled = false;
|
|
105
|
+
const reader = body.getReader();
|
|
106
|
+
const wrappedBody = new ReadableStream({
|
|
107
|
+
async pull (controller) {
|
|
108
|
+
let result;
|
|
109
|
+
try {
|
|
110
|
+
result = await reader.read();
|
|
111
|
+
} catch (error) {
|
|
112
|
+
if (!cancelled) {
|
|
113
|
+
await run({
|
|
114
|
+
status: 'error',
|
|
115
|
+
error
|
|
116
|
+
});
|
|
117
|
+
controller.error(error);
|
|
118
|
+
}
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
if (cancelled) return;
|
|
122
|
+
if (result.done) {
|
|
123
|
+
await run();
|
|
124
|
+
if (!cancelled) controller.close();
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
127
|
+
controller.enqueue(result.value);
|
|
128
|
+
},
|
|
129
|
+
async cancel (reason) {
|
|
130
|
+
cancelled = true;
|
|
131
|
+
try {
|
|
132
|
+
await reader.cancel(reason);
|
|
133
|
+
} catch {}
|
|
134
|
+
await run({
|
|
135
|
+
status: 'cancelled',
|
|
136
|
+
reason
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
});
|
|
140
|
+
return new Response(wrappedBody, {
|
|
141
|
+
status: response.status,
|
|
142
|
+
statusText: response.statusText,
|
|
143
|
+
headers: response.headers
|
|
144
|
+
});
|
|
145
|
+
};
|
|
146
|
+
const discardBody = async (response, terminal = {
|
|
147
|
+
status: 'discarded'
|
|
148
|
+
})=>{
|
|
149
|
+
const { body } = response;
|
|
150
|
+
if (!body) return void await run(terminal);
|
|
151
|
+
deferred = true;
|
|
152
|
+
if (body.locked) throw new TypeError('Cannot discard a locked response body before request completion');
|
|
153
|
+
try {
|
|
154
|
+
await body.cancel('Response body discarded during finalization');
|
|
155
|
+
} catch (error) {
|
|
156
|
+
await run('error' === terminal.status ? terminal : {
|
|
157
|
+
status: 'error',
|
|
158
|
+
error
|
|
159
|
+
});
|
|
160
|
+
if ('error' === terminal.status) onError(error, RESPONSE_BODY_CANCEL_ERROR);
|
|
161
|
+
throw error;
|
|
162
|
+
}
|
|
163
|
+
await run(terminal);
|
|
164
|
+
};
|
|
165
|
+
return {
|
|
166
|
+
get deferred () {
|
|
167
|
+
return deferred;
|
|
168
|
+
},
|
|
169
|
+
run,
|
|
170
|
+
deferUntilBodyDone,
|
|
171
|
+
discardBody
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
const isRedirectStatus = (status)=>301 === status || 302 === status || 303 === status || 307 === status || 308 === status;
|
|
175
|
+
const isNullBodyStatus = (status)=>204 === status || 205 === status || 304 === status;
|
|
176
|
+
const getRedirectLocation = (headers)=>{
|
|
177
|
+
const location = headers.get('Location');
|
|
178
|
+
return null !== location && '' !== location && URL.canParse(location, 'http://localhost') ? location : void 0;
|
|
179
|
+
};
|
|
180
|
+
const processRedirect = (headers, status, ctx)=>{
|
|
181
|
+
headers.delete('content-length');
|
|
182
|
+
headers.delete('transfer-encoding');
|
|
183
|
+
if (ctx.enableRsc && ctx.isRSCNavigation) return (0, redirect_js_namespaceObject.handleRSCRedirect)(headers, ctx.basename, status);
|
|
184
|
+
return new Response(null, {
|
|
185
|
+
status,
|
|
186
|
+
headers
|
|
187
|
+
});
|
|
188
|
+
};
|
|
189
|
+
const applyRouterResult = (context, routerResult, onError)=>{
|
|
190
|
+
const routerStatusCode = routerResult?.statusCode;
|
|
191
|
+
if (void 0 !== routerStatusCode && 0 !== routerStatusCode && !Number.isNaN(routerStatusCode) && 200 !== routerStatusCode) context.ssrContext?.response.status(routerStatusCode);
|
|
192
|
+
const errors = Object.values(routerResult?.errors || {});
|
|
193
|
+
if (errors.length > 0) onError(errors[0], external_tracer_js_namespaceObject.SSRErrors.LOADER_ERROR);
|
|
194
|
+
};
|
|
195
|
+
const createLoaderRedirectResponse = (beforeRenderResult, redirectCtx)=>{
|
|
196
|
+
if (void 0 === beforeRenderResult || !isRedirectStatus(beforeRenderResult.status)) return;
|
|
197
|
+
if (beforeRenderResult.headers.has('X-Modernjs-Redirect')) return beforeRenderResult;
|
|
198
|
+
const redirectUrl = getRedirectLocation(beforeRenderResult.headers);
|
|
199
|
+
if (void 0 === redirectUrl) return;
|
|
200
|
+
return processRedirect(new Headers(beforeRenderResult.headers), beforeRenderResult.status, redirectCtx);
|
|
201
|
+
};
|
|
202
|
+
const finalizeRenderResponse = async (response, responseProxy, redirectCtx, lifecycle)=>{
|
|
203
|
+
try {
|
|
204
|
+
const proxyHeaders = new Headers(responseProxy.headers);
|
|
205
|
+
if (-1 !== responseProxy.status && isRedirectStatus(responseProxy.status) && void 0 !== getRedirectLocation(proxyHeaders)) {
|
|
206
|
+
await lifecycle.discardBody(response);
|
|
207
|
+
return processRedirect(proxyHeaders, responseProxy.status, redirectCtx);
|
|
208
|
+
}
|
|
209
|
+
const headers = new Headers(response.headers);
|
|
210
|
+
Object.entries(responseProxy.headers).forEach(([key, value])=>{
|
|
211
|
+
headers.set(key, value);
|
|
212
|
+
});
|
|
213
|
+
if (-1 !== responseProxy.status) {
|
|
214
|
+
if (isNullBodyStatus(responseProxy.status)) {
|
|
215
|
+
await lifecycle.discardBody(response);
|
|
216
|
+
headers.delete('content-length');
|
|
217
|
+
headers.delete('transfer-encoding');
|
|
218
|
+
return new Response(null, {
|
|
219
|
+
status: responseProxy.status,
|
|
220
|
+
headers
|
|
221
|
+
});
|
|
222
|
+
}
|
|
223
|
+
if (response.body?.locked) lifecycle.deferUntilBodyDone(response);
|
|
224
|
+
return lifecycle.deferUntilBodyDone(new Response(response.body, {
|
|
225
|
+
status: responseProxy.status,
|
|
226
|
+
headers
|
|
227
|
+
}));
|
|
228
|
+
}
|
|
229
|
+
Object.entries(responseProxy.headers).forEach(([key, value])=>{
|
|
230
|
+
response.headers.set(key, value);
|
|
231
|
+
});
|
|
232
|
+
return lifecycle.deferUntilBodyDone(response);
|
|
233
|
+
} catch (error) {
|
|
234
|
+
if (!lifecycle.deferred) try {
|
|
235
|
+
await lifecycle.discardBody(response, {
|
|
236
|
+
status: 'error',
|
|
237
|
+
error
|
|
238
|
+
});
|
|
239
|
+
} catch {}
|
|
240
|
+
throw error;
|
|
241
|
+
}
|
|
242
|
+
};
|
|
55
243
|
async function handleRSCRequest(request, Root, context, options, handleRequest) {
|
|
56
|
-
const serverPayload = (0,
|
|
244
|
+
const serverPayload = (0, index_js_namespaceObject.getServerPayload)();
|
|
57
245
|
if (void 0 !== serverPayload) return await handleRequest(request, Root, {
|
|
58
246
|
...options,
|
|
59
247
|
runtimeContext: context,
|
|
60
248
|
rscRoot: serverPayload
|
|
61
249
|
});
|
|
62
|
-
const App = (0,
|
|
250
|
+
const App = (0, context_namespaceObject.getGlobalRSCRoot)();
|
|
63
251
|
if (App) return await handleRequest(request, external_react_namespaceObject.Fragment, {
|
|
64
252
|
...options,
|
|
65
253
|
runtimeContext: context,
|
|
@@ -142,19 +330,21 @@ const createRequestHandler = async (handleRequest, createRequestOptions)=>{
|
|
|
142
330
|
status: -1
|
|
143
331
|
};
|
|
144
332
|
const activeDeferreds = new Map();
|
|
333
|
+
const deferredScriptKeys = new Map();
|
|
145
334
|
return node_namespaceObject.storage.run({
|
|
146
335
|
headers: headersData,
|
|
147
336
|
request,
|
|
148
337
|
monitors: options.monitors,
|
|
149
338
|
responseProxy,
|
|
150
339
|
activeDeferreds,
|
|
340
|
+
deferredScriptKeys,
|
|
151
341
|
serverPayload: void 0
|
|
152
342
|
}, async ()=>{
|
|
153
343
|
const Root = (0, external_react_index_js_namespaceObject.createRoot)();
|
|
154
|
-
const internalRuntimeContext = (0,
|
|
344
|
+
const internalRuntimeContext = (0, context_namespaceObject.getGlobalInternalRuntimeContext)();
|
|
155
345
|
const hooks = internalRuntimeContext.hooks;
|
|
156
346
|
const { routeManifest } = options.resource;
|
|
157
|
-
const context = (0,
|
|
347
|
+
const context = (0, context_namespaceObject.getInitialContext)(false, routeManifest);
|
|
158
348
|
const runBeforeRender = async (context)=>{
|
|
159
349
|
const result = await hooks.onBeforeRender.call(context);
|
|
160
350
|
if ("u" > typeof Response && result instanceof Response) return result;
|
|
@@ -172,33 +362,71 @@ const createRequestHandler = async (handleRequest, createRequestOptions)=>{
|
|
|
172
362
|
isRSCNavigation: 'true' === request.headers.get('x-rsc-tree'),
|
|
173
363
|
basename: ssrContext.baseUrl || '/'
|
|
174
364
|
};
|
|
175
|
-
const
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
365
|
+
const lifecycle = createRequestLifecycle(async (terminal)=>{
|
|
366
|
+
await hooks.onRequestEnd?.call({
|
|
367
|
+
runtimeContext: context,
|
|
368
|
+
terminal
|
|
369
|
+
});
|
|
370
|
+
}, options.onError);
|
|
371
|
+
const beforeRenderResult = await runWithRequestLifecycleOnError(lifecycle, ()=>runBeforeRender(context));
|
|
372
|
+
let redirectResponse;
|
|
373
|
+
try {
|
|
374
|
+
const prepared = {
|
|
375
|
+
runtimeContext: context,
|
|
376
|
+
routerResult: context.routerContext
|
|
377
|
+
};
|
|
378
|
+
const result = await hooks.onRenderPrepared?.call(prepared) ?? prepared;
|
|
379
|
+
applyRouterResult(context, result.routerResult, options.onError);
|
|
380
|
+
if ("u" > typeof Response) redirectResponse = createLoaderRedirectResponse(beforeRenderResult, redirectCtx);
|
|
381
|
+
} catch (error) {
|
|
382
|
+
if (beforeRenderResult) try {
|
|
383
|
+
await lifecycle.discardBody(beforeRenderResult, {
|
|
384
|
+
status: 'error',
|
|
385
|
+
error
|
|
386
|
+
});
|
|
387
|
+
} catch {}
|
|
388
|
+
else await lifecycle.run({
|
|
389
|
+
status: 'error',
|
|
390
|
+
error
|
|
391
|
+
});
|
|
392
|
+
throw error;
|
|
393
|
+
}
|
|
394
|
+
if (redirectResponse) {
|
|
395
|
+
const response = redirectResponse;
|
|
396
|
+
if (beforeRenderResult?.body && response !== beforeRenderResult) await lifecycle.discardBody(beforeRenderResult);
|
|
397
|
+
return finishWithRequestLifecycle(lifecycle, ()=>lifecycle.deferUntilBodyDone(response));
|
|
186
398
|
}
|
|
187
|
-
await (
|
|
399
|
+
await runWithRequestLifecycleOnError(lifecycle, ()=>{
|
|
188
400
|
if (!createRequestOptions?.enableRsc) {
|
|
189
401
|
const { htmlTemplate } = options.resource;
|
|
190
402
|
options.resource.htmlTemplate = htmlTemplate.replace('</head>', `${external_constants_js_namespaceObject.CHUNK_CSS_PLACEHOLDER}</head>`);
|
|
191
403
|
}
|
|
192
404
|
});
|
|
193
|
-
const response = await (
|
|
194
|
-
return (
|
|
405
|
+
const response = await runWithRequestLifecycleOnError(lifecycle, ()=>renderRequest(request, Root, context, options, handleRequest, !!createRequestOptions?.enableRsc));
|
|
406
|
+
return finishWithRequestLifecycle(lifecycle, ()=>finalizeRenderResponse(response, responseProxy, redirectCtx, lifecycle));
|
|
195
407
|
});
|
|
196
408
|
};
|
|
197
409
|
return requestHandler;
|
|
198
410
|
};
|
|
411
|
+
exports.REQUEST_END_ERROR = __webpack_exports__.REQUEST_END_ERROR;
|
|
412
|
+
exports.RESPONSE_BODY_CANCEL_ERROR = __webpack_exports__.RESPONSE_BODY_CANCEL_ERROR;
|
|
413
|
+
exports.applyRouterResult = __webpack_exports__.applyRouterResult;
|
|
414
|
+
exports.createLoaderRedirectResponse = __webpack_exports__.createLoaderRedirectResponse;
|
|
199
415
|
exports.createRequestHandler = __webpack_exports__.createRequestHandler;
|
|
416
|
+
exports.createRequestLifecycle = __webpack_exports__.createRequestLifecycle;
|
|
417
|
+
exports.finalizeRenderResponse = __webpack_exports__.finalizeRenderResponse;
|
|
418
|
+
exports.finishWithRequestLifecycle = __webpack_exports__.finishWithRequestLifecycle;
|
|
419
|
+
exports.runWithRequestLifecycleOnError = __webpack_exports__.runWithRequestLifecycleOnError;
|
|
200
420
|
for(var __rspack_i in __webpack_exports__)if (-1 === [
|
|
201
|
-
"
|
|
421
|
+
"REQUEST_END_ERROR",
|
|
422
|
+
"RESPONSE_BODY_CANCEL_ERROR",
|
|
423
|
+
"applyRouterResult",
|
|
424
|
+
"createLoaderRedirectResponse",
|
|
425
|
+
"createRequestHandler",
|
|
426
|
+
"createRequestLifecycle",
|
|
427
|
+
"finalizeRenderResponse",
|
|
428
|
+
"finishWithRequestLifecycle",
|
|
429
|
+
"runWithRequestLifecycleOnError"
|
|
202
430
|
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
|
|
203
431
|
Object.defineProperty(exports, '__esModule', {
|
|
204
432
|
value: true
|
|
@@ -43,8 +43,8 @@ const readAsset = async (chunk)=>{
|
|
|
43
43
|
};
|
|
44
44
|
const checkIsInline = (chunk, enableInline)=>{
|
|
45
45
|
if ('production' !== process.env.NODE_ENV) return false;
|
|
46
|
-
if (enableInline instanceof RegExp) return enableInline.test(chunk);
|
|
47
|
-
return Boolean(enableInline);
|
|
46
|
+
else if (enableInline instanceof RegExp) return enableInline.test(chunk);
|
|
47
|
+
else return Boolean(enableInline);
|
|
48
48
|
};
|
|
49
49
|
async function buildShellBeforeTemplate(beforeAppTemplate, options) {
|
|
50
50
|
const { config, runtimeContext, styledComponentsStyleTags, entryName, lifecycle, helmetData } = options;
|
|
@@ -102,7 +102,7 @@ async function buildShellBeforeTemplate(beforeAppTemplate, options) {
|
|
|
102
102
|
const styles = await Promise.all(cssChunks.map(async (chunk)=>{
|
|
103
103
|
const link = `<link href="${chunk}" rel="stylesheet" />`;
|
|
104
104
|
if ((0, external_utils_js_namespaceObject.checkIsNode)() && checkIsInline(chunk, inlineStyles)) return readAsset(chunk).then((content)=>`<style>${content}</style>`).catch((_)=>link);
|
|
105
|
-
return link;
|
|
105
|
+
else return link;
|
|
106
106
|
}));
|
|
107
107
|
return `${styles.join('')}`;
|
|
108
108
|
}
|
|
@@ -36,8 +36,8 @@ const external_shared_js_namespaceObject = require("../shared.js");
|
|
|
36
36
|
const external_utils_js_namespaceObject = require("../utils.js");
|
|
37
37
|
const checkIsInline = (chunk, enableInline)=>{
|
|
38
38
|
if ('production' !== process.env.NODE_ENV) return false;
|
|
39
|
-
if (enableInline instanceof RegExp) return enableInline.test(chunk);
|
|
40
|
-
return Boolean(enableInline);
|
|
39
|
+
else if (enableInline instanceof RegExp) return enableInline.test(chunk);
|
|
40
|
+
else return Boolean(enableInline);
|
|
41
41
|
};
|
|
42
42
|
async function buildShellBeforeTemplate(beforeAppTemplate, options) {
|
|
43
43
|
const { config, runtimeContext, styledComponentsStyleTags, entryName, lifecycle, helmetData } = options;
|
|
@@ -30,24 +30,26 @@ __webpack_require__.r(__webpack_exports__);
|
|
|
30
30
|
__webpack_require__.d(__webpack_exports__, {
|
|
31
31
|
createReadableStreamFromElement: ()=>createReadableStreamFromElement
|
|
32
32
|
});
|
|
33
|
+
const context_namespaceObject = require("@modern-js/runtime/context");
|
|
33
34
|
const node_namespaceObject = require("@modern-js/runtime-utils/node");
|
|
34
35
|
const constants_namespaceObject = require("@modern-js/utils/universal/constants");
|
|
35
36
|
const external_stream_namespaceObject = require("stream");
|
|
37
|
+
const external_string_decoder_namespaceObject = require("string_decoder");
|
|
36
38
|
const external_common_js_namespaceObject = require("../../../common.js");
|
|
37
39
|
const external_constants_js_namespaceObject = require("../../constants.js");
|
|
38
|
-
const index_js_namespaceObject = require("../../context/index.js");
|
|
39
|
-
const monitors_index_js_namespaceObject = require("../../context/monitors/index.js");
|
|
40
|
+
const index_js_namespaceObject = require("../../context/monitors/index.js");
|
|
40
41
|
const wrapper_js_namespaceObject = require("../../react/wrapper.js");
|
|
41
42
|
const external_helmet_js_namespaceObject = require("../helmet.js");
|
|
42
43
|
const external_shared_js_namespaceObject = require("../shared.js");
|
|
43
44
|
const external_deferredScript_js_namespaceObject = require("./deferredScript.js");
|
|
45
|
+
const external_deferredScriptOutputCoordinator_js_namespaceObject = require("./deferredScriptOutputCoordinator.js");
|
|
44
46
|
const external_shared_js_namespaceObject_1 = require("./shared.js");
|
|
45
47
|
const external_template_js_namespaceObject = require("./template.js");
|
|
46
48
|
const createReadableStreamFromElement = async (request, rootElement, options)=>{
|
|
47
49
|
const { renderToPipeableStream } = await import("react-dom/server");
|
|
48
50
|
const { runtimeContext, htmlTemplate, config, ssrConfig, entryName, resource } = options;
|
|
49
|
-
const hooks = (0,
|
|
50
|
-
const isRsc = true === (0,
|
|
51
|
+
const hooks = (0, context_namespaceObject.getGlobalInternalRuntimeContext)().hooks;
|
|
52
|
+
const isRsc = true === (0, context_namespaceObject.getGlobalEnableRsc)();
|
|
51
53
|
const extenders = hooks.extendStreamSSR.call({
|
|
52
54
|
runtimeContext,
|
|
53
55
|
request,
|
|
@@ -55,6 +57,7 @@ const createReadableStreamFromElement = async (request, rootElement, options)=>{
|
|
|
55
57
|
config,
|
|
56
58
|
platform: 'node',
|
|
57
59
|
mode: 'stream',
|
|
60
|
+
monitors: (0, index_js_namespaceObject.getMonitors)(),
|
|
58
61
|
isRsc,
|
|
59
62
|
terminalMarker: external_common_js_namespaceObject.ESCAPED_SHELL_STREAM_END_MARK
|
|
60
63
|
}) || [];
|
|
@@ -153,29 +156,42 @@ const createReadableStreamFromElement = async (request, rootElement, options)=>{
|
|
|
153
156
|
});
|
|
154
157
|
if (failed) return;
|
|
155
158
|
const chunks = [];
|
|
159
|
+
let bufferedLength = 0;
|
|
156
160
|
const marker = Buffer.from(external_common_js_namespaceObject.ESCAPED_SHELL_STREAM_END_MARK);
|
|
161
|
+
let markerTail = Buffer.alloc(0);
|
|
162
|
+
const decoder = new external_string_decoder_namespaceObject.StringDecoder('utf8');
|
|
157
163
|
const pendingScripts = [];
|
|
164
|
+
let deferredResolversComplete = Promise.resolve();
|
|
158
165
|
let shellChunkStatus = external_shared_js_namespaceObject_1.ShellChunkStatus.START;
|
|
159
|
-
const emitShell = (
|
|
166
|
+
const emitShell = (buffered, markerIndex)=>{
|
|
160
167
|
const beforeMark = lifecycle.completedBody(buffered.subarray(0, markerIndex).toString('utf8'), 'shell');
|
|
161
168
|
const completedShellBefore = (0, external_helmet_js_namespaceObject.createReplaceHelemt)((0, external_helmet_js_namespaceObject.getHelmetData)(extenders))(shellBefore);
|
|
162
169
|
shellChunkStatus = external_shared_js_namespaceObject_1.ShellChunkStatus.FINISH;
|
|
163
170
|
chunks.length = 0;
|
|
164
|
-
|
|
171
|
+
bufferedLength = 0;
|
|
172
|
+
coordinator.writeReact(`${completedShellBefore}${beforeMark}${shellAfter}`);
|
|
165
173
|
const afterMark = buffered.subarray(markerIndex + marker.length);
|
|
166
|
-
if (afterMark.length > 0)
|
|
167
|
-
|
|
174
|
+
if (afterMark.length > 0) coordinator.writeReact(decoder.write(afterMark));
|
|
175
|
+
coordinator.markShellFinished();
|
|
176
|
+
for (const script of pendingScripts)coordinator.enqueueResolver(script);
|
|
168
177
|
pendingScripts.length = 0;
|
|
169
178
|
};
|
|
170
179
|
const body = new external_stream_namespaceObject.Transform({
|
|
171
180
|
transform (chunk, _encoding, callback) {
|
|
172
181
|
try {
|
|
173
|
-
|
|
182
|
+
const bytes = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk);
|
|
183
|
+
if (shellChunkStatus === external_shared_js_namespaceObject_1.ShellChunkStatus.FINISH) coordinator.writeReact(decoder.write(bytes));
|
|
174
184
|
else {
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
185
|
+
const window = Buffer.concat([
|
|
186
|
+
markerTail,
|
|
187
|
+
bytes
|
|
188
|
+
]);
|
|
189
|
+
const windowIndex = window.indexOf(marker);
|
|
190
|
+
const windowStart = bufferedLength - markerTail.length;
|
|
191
|
+
chunks.push(bytes);
|
|
192
|
+
bufferedLength += bytes.length;
|
|
193
|
+
if (-1 === windowIndex) markerTail = window.subarray(Math.max(0, window.length - marker.length + 1));
|
|
194
|
+
else emitShell(Buffer.concat(chunks, bufferedLength), windowStart + windowIndex);
|
|
179
195
|
}
|
|
180
196
|
callback();
|
|
181
197
|
} catch (error) {
|
|
@@ -186,14 +202,27 @@ const createReadableStreamFromElement = async (request, rootElement, options)=>{
|
|
|
186
202
|
try {
|
|
187
203
|
if (shellChunkStatus !== external_shared_js_namespaceObject_1.ShellChunkStatus.FINISH) {
|
|
188
204
|
const buffered = Buffer.concat(chunks);
|
|
189
|
-
emitShell(
|
|
205
|
+
emitShell(buffered, buffered.length);
|
|
190
206
|
}
|
|
191
|
-
|
|
207
|
+
const trailing = decoder.end();
|
|
208
|
+
if (trailing) coordinator.writeReact(trailing);
|
|
209
|
+
deferredResolversComplete.then(()=>{
|
|
210
|
+
try {
|
|
211
|
+
coordinator.finish();
|
|
212
|
+
callback();
|
|
213
|
+
} catch (error) {
|
|
214
|
+
callback(error instanceof Error ? error : new Error(String(error)));
|
|
215
|
+
}
|
|
216
|
+
}, (error)=>callback(error instanceof Error ? error : new Error(String(error))));
|
|
192
217
|
} catch (error) {
|
|
193
218
|
callback(error instanceof Error ? error : new Error(String(error)));
|
|
194
219
|
}
|
|
195
220
|
}
|
|
196
221
|
});
|
|
222
|
+
const coordinator = new external_deferredScriptOutputCoordinator_js_namespaceObject.DeferredScriptOutputCoordinator((content)=>{
|
|
223
|
+
body.push(content);
|
|
224
|
+
});
|
|
225
|
+
body.once('close', ()=>coordinator.abort());
|
|
197
226
|
own(body);
|
|
198
227
|
const passThrough = new external_stream_namespaceObject.PassThrough();
|
|
199
228
|
let processedStream = own(passThrough);
|
|
@@ -204,15 +233,17 @@ const createReadableStreamFromElement = async (request, rootElement, options)=>{
|
|
|
204
233
|
deliver(external_stream_namespaceObject.Readable.toWeb(body));
|
|
205
234
|
reactStream.pipe(passThrough);
|
|
206
235
|
try {
|
|
207
|
-
const
|
|
236
|
+
const storageContext = node_namespaceObject.storage.useContext?.();
|
|
237
|
+
const activeDeferreds = storageContext?.activeDeferreds;
|
|
238
|
+
const deferredScriptKeys = storageContext?.deferredScriptKeys;
|
|
208
239
|
const entries = activeDeferreds instanceof Map ? Array.from(activeDeferreds.entries()) : [];
|
|
209
|
-
(0, external_deferredScript_js_namespaceObject.enqueueFromEntries)(entries, config.nonce, (script)=>{
|
|
240
|
+
if (entries.length > 0) deferredResolversComplete = (0, external_deferredScript_js_namespaceObject.enqueueFromEntries)(entries, deferredScriptKeys, config.nonce, (script)=>{
|
|
210
241
|
if (failed || body.destroyed || body.writableEnded) return;
|
|
211
|
-
if (shellChunkStatus === external_shared_js_namespaceObject_1.ShellChunkStatus.FINISH)
|
|
242
|
+
if (shellChunkStatus === external_shared_js_namespaceObject_1.ShellChunkStatus.FINISH) coordinator.enqueueResolver(script);
|
|
212
243
|
else pendingScripts.push(script);
|
|
213
244
|
});
|
|
214
245
|
} catch (error) {
|
|
215
|
-
(0,
|
|
246
|
+
(0, index_js_namespaceObject.getMonitors)().error("cannot inject router data script", error);
|
|
216
247
|
}
|
|
217
248
|
};
|
|
218
249
|
const renderFallback = async (error)=>{
|
|
@@ -245,6 +276,7 @@ const createReadableStreamFromElement = async (request, rootElement, options)=>{
|
|
|
245
276
|
reactStream = renderToPipeableStream(processedRootElement, {
|
|
246
277
|
nonce: config.nonce,
|
|
247
278
|
identifierPrefix: constants_namespaceObject.SSR_HYDRATION_ID_PREFIX,
|
|
279
|
+
progressiveChunkSize: external_shared_js_namespaceObject_1.SHELL_PROGRESSIVE_CHUNK_SIZE,
|
|
248
280
|
[onReady] () {
|
|
249
281
|
startOutput().catch(fail);
|
|
250
282
|
},
|