@modern-js/render 3.11.0 → 3.11.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.
- package/dist/cjs/client/callServer.js +134 -98
- package/dist/cjs/client/index.js +100 -82
- package/dist/cjs/rsc-html-stream/client.js +53 -49
- package/dist/cjs/rsc-html-stream/server.js +127 -135
- package/dist/cjs/server/rsc/csr.js +61 -49
- package/dist/cjs/server/rsc/index.js +66 -21
- package/dist/cjs/server/rsc/rsc.js +101 -90
- package/dist/cjs/server/ssr/index.js +59 -19
- package/dist/cjs/server/ssr/ssr.js +91 -100
- package/dist/esm/client/callServer.js +95 -75
- package/dist/esm/client/index.js +25 -41
- package/dist/esm/rsc-html-stream/client.js +17 -27
- package/dist/esm/rsc-html-stream/server.js +88 -112
- package/dist/esm/server/rsc/csr.js +26 -28
- package/dist/esm/server/rsc/index.js +2 -2
- package/dist/esm/server/rsc/rsc.js +51 -64
- package/dist/esm/server/ssr/index.js +1 -1
- package/dist/esm/server/ssr/ssr.js +54 -67
- package/dist/esm-node/client/callServer.mjs +96 -75
- package/dist/esm-node/client/index.mjs +26 -41
- package/dist/esm-node/rsc-html-stream/client.mjs +18 -27
- package/dist/esm-node/rsc-html-stream/server.mjs +89 -112
- package/dist/esm-node/server/rsc/csr.mjs +25 -26
- package/dist/esm-node/server/rsc/index.mjs +1 -0
- package/dist/esm-node/server/rsc/rsc.mjs +52 -64
- package/dist/esm-node/server/ssr/index.mjs +1 -0
- package/dist/esm-node/server/ssr/ssr.mjs +54 -66
- package/dist/types/client/index.d.ts +2 -2
- package/dist/types/server/rsc/index.d.ts +2 -2
- package/dist/types/server/ssr/index.d.ts +1 -1
- package/package.json +10 -11
|
@@ -1,83 +1,103 @@
|
|
|
1
|
-
import { _ as _class_private_field_get } from "@swc/helpers/_/_class_private_field_get";
|
|
2
|
-
import { _ as _class_private_field_init } from "@swc/helpers/_/_class_private_field_init";
|
|
3
|
-
import { _ as _class_private_field_set } from "@swc/helpers/_/_class_private_field_set";
|
|
4
1
|
import { createFromFetch, encodeReply } from "react-server-dom-rspack/client.browser";
|
|
5
|
-
|
|
2
|
+
function _check_private_redeclaration(obj, privateCollection) {
|
|
3
|
+
if (privateCollection.has(obj)) throw new TypeError("Cannot initialize the same private elements twice on an object");
|
|
4
|
+
}
|
|
5
|
+
function _class_apply_descriptor_get(receiver, descriptor) {
|
|
6
|
+
if (descriptor.get) return descriptor.get.call(receiver);
|
|
7
|
+
return descriptor.value;
|
|
8
|
+
}
|
|
9
|
+
function _class_apply_descriptor_set(receiver, descriptor, value) {
|
|
10
|
+
if (descriptor.set) descriptor.set.call(receiver, value);
|
|
11
|
+
else {
|
|
12
|
+
if (!descriptor.writable) throw new TypeError("attempted to set read only private field");
|
|
13
|
+
descriptor.value = value;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
function _class_extract_field_descriptor(receiver, privateMap, action) {
|
|
17
|
+
if (!privateMap.has(receiver)) throw new TypeError("attempted to " + action + " private field on non-instance");
|
|
18
|
+
return privateMap.get(receiver);
|
|
19
|
+
}
|
|
20
|
+
function _class_private_field_get(receiver, privateMap) {
|
|
21
|
+
var descriptor = _class_extract_field_descriptor(receiver, privateMap, "get");
|
|
22
|
+
return _class_apply_descriptor_get(receiver, descriptor);
|
|
23
|
+
}
|
|
24
|
+
function _class_private_field_init(obj, privateMap, value) {
|
|
25
|
+
_check_private_redeclaration(obj, privateMap);
|
|
26
|
+
privateMap.set(obj, value);
|
|
27
|
+
}
|
|
28
|
+
function _class_private_field_set(receiver, privateMap, value) {
|
|
29
|
+
var descriptor = _class_extract_field_descriptor(receiver, privateMap, "set");
|
|
30
|
+
_class_apply_descriptor_set(receiver, descriptor, value);
|
|
31
|
+
return value;
|
|
32
|
+
}
|
|
33
|
+
var _statusCode = /*#__PURE__*/ new WeakMap(), _url = /*#__PURE__*/ new WeakMap(), _details = /*#__PURE__*/ new WeakMap();
|
|
6
34
|
class CallServerError extends Error {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
}
|
|
35
|
+
get statusCode() {
|
|
36
|
+
return _class_private_field_get(this, _statusCode);
|
|
37
|
+
}
|
|
38
|
+
get url() {
|
|
39
|
+
return _class_private_field_get(this, _url);
|
|
40
|
+
}
|
|
41
|
+
get details() {
|
|
42
|
+
return {
|
|
43
|
+
..._class_private_field_get(this, _details)
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
toJSON() {
|
|
47
|
+
return {
|
|
48
|
+
name: this.name,
|
|
49
|
+
message: this.message,
|
|
50
|
+
statusCode: _class_private_field_get(this, _statusCode),
|
|
51
|
+
url: _class_private_field_get(this, _url),
|
|
52
|
+
details: _class_private_field_get(this, _details)
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
constructor(message, statusCode, url = '', details = {}){
|
|
56
|
+
const formattedMessage = `Call Server Action failed (${statusCode}): ${message}\nURL: ${url}`;
|
|
57
|
+
super(formattedMessage), _class_private_field_init(this, _statusCode, {
|
|
58
|
+
writable: true,
|
|
59
|
+
value: void 0
|
|
60
|
+
}), _class_private_field_init(this, _url, {
|
|
61
|
+
writable: true,
|
|
62
|
+
value: void 0
|
|
63
|
+
}), _class_private_field_init(this, _details, {
|
|
64
|
+
writable: true,
|
|
65
|
+
value: void 0
|
|
66
|
+
});
|
|
67
|
+
this.name = 'CallServerError';
|
|
68
|
+
_class_private_field_set(this, _statusCode, statusCode);
|
|
69
|
+
_class_private_field_set(this, _url, url);
|
|
70
|
+
_class_private_field_set(this, _details, details);
|
|
71
|
+
}
|
|
45
72
|
}
|
|
46
73
|
async function requestCallServer(id, args) {
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
74
|
+
const entryName = window.__MODERN_JS_ENTRY_NAME;
|
|
75
|
+
const url = 'main' === entryName || 'index' === entryName ? '/' : `/${entryName}`;
|
|
76
|
+
try {
|
|
77
|
+
const response = await fetch(url, {
|
|
78
|
+
method: 'POST',
|
|
79
|
+
headers: {
|
|
80
|
+
Accept: 'text/x-component',
|
|
81
|
+
'x-rsc-action': id
|
|
82
|
+
},
|
|
83
|
+
body: await encodeReply(args)
|
|
84
|
+
});
|
|
85
|
+
if (!response.ok) throw new CallServerError(response.statusText, response.status, url, {
|
|
86
|
+
id,
|
|
87
|
+
args
|
|
88
|
+
});
|
|
89
|
+
return response;
|
|
90
|
+
} catch (error) {
|
|
91
|
+
if (error instanceof CallServerError) throw error;
|
|
92
|
+
throw new CallServerError(error instanceof Error ? error.message : 'Unknown error', 1, url, {
|
|
93
|
+
id,
|
|
94
|
+
args
|
|
95
|
+
});
|
|
68
96
|
}
|
|
69
|
-
throw new CallServerError(error instanceof Error ? error.message : "Unknown error", 1, url, {
|
|
70
|
-
id,
|
|
71
|
-
args
|
|
72
|
-
});
|
|
73
|
-
}
|
|
74
97
|
}
|
|
75
98
|
async function callServer(id, args) {
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
99
|
+
const response = requestCallServer(id, args);
|
|
100
|
+
const res = createFromFetch(response);
|
|
101
|
+
return res;
|
|
79
102
|
}
|
|
80
|
-
export {
|
|
81
|
-
callServer,
|
|
82
|
-
requestCallServer
|
|
83
|
-
};
|
|
103
|
+
export { callServer, requestCallServer };
|
package/dist/esm/client/index.js
CHANGED
|
@@ -1,44 +1,28 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import
|
|
3
|
-
|
|
4
|
-
import { createFromReadableStream, createServerReference, setServerCallback } from "react-server-dom-rspack/client.browser";
|
|
5
|
-
import { rscStream } from "../rsc-html-stream/client";
|
|
6
|
-
import { callServer } from "./callServer";
|
|
7
|
-
import { createFromFetch } from "react-server-dom-rspack/client.browser";
|
|
8
|
-
const ResetRootContext = /* @__PURE__ */ createContext(void 0);
|
|
1
|
+
import { Fragment, jsx } from "react/jsx-runtime";
|
|
2
|
+
import react, { createContext, useState } from "react";
|
|
3
|
+
const ResetRootContext = /*#__PURE__*/ createContext(void 0);
|
|
9
4
|
function RscClientRoot({ rscPayload }) {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
5
|
+
const elements = react.use(rscPayload);
|
|
6
|
+
const [root, setRoot] = useState(elements);
|
|
7
|
+
return /*#__PURE__*/ jsx(ResetRootContext.Provider, {
|
|
8
|
+
value: {
|
|
9
|
+
setRoot
|
|
10
|
+
},
|
|
11
|
+
children: root
|
|
12
|
+
});
|
|
18
13
|
}
|
|
19
|
-
const ElementsContext =
|
|
20
|
-
const ServerElementsProvider = ({ elements, children })
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
})
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
const elements = React.use(ElementsContext);
|
|
30
|
-
return elements;
|
|
31
|
-
};
|
|
32
|
-
export {
|
|
33
|
-
ElementsContext,
|
|
34
|
-
RSCServerSlot,
|
|
35
|
-
ResetRootContext,
|
|
36
|
-
RscClientRoot,
|
|
37
|
-
ServerElementsProvider,
|
|
38
|
-
callServer,
|
|
39
|
-
createFromFetch,
|
|
40
|
-
createFromReadableStream,
|
|
41
|
-
createServerReference,
|
|
42
|
-
rscStream,
|
|
43
|
-
setServerCallback
|
|
14
|
+
const ElementsContext = /*#__PURE__*/ createContext(null);
|
|
15
|
+
const ServerElementsProvider = ({ elements, children })=>/*#__PURE__*/ jsx(Fragment, {
|
|
16
|
+
children: /*#__PURE__*/ jsx(ElementsContext.Provider, {
|
|
17
|
+
value: elements,
|
|
18
|
+
children: children
|
|
19
|
+
})
|
|
20
|
+
});
|
|
21
|
+
const RSCServerSlot = ()=>{
|
|
22
|
+
const elements = react.use(ElementsContext);
|
|
23
|
+
return elements;
|
|
44
24
|
};
|
|
25
|
+
export { createFromFetch, createFromReadableStream, createServerReference, setServerCallback } from "react-server-dom-rspack/client.browser";
|
|
26
|
+
export { rscStream } from "../rsc-html-stream/client.js";
|
|
27
|
+
export { callServer } from "./callServer.js";
|
|
28
|
+
export { ElementsContext, RSCServerSlot, ResetRootContext, RscClientRoot, ServerElementsProvider };
|
|
@@ -1,32 +1,22 @@
|
|
|
1
1
|
const encoder = new TextEncoder();
|
|
2
2
|
let streamController;
|
|
3
3
|
const rscStream = new ReadableStream({
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
4
|
+
start (controller) {
|
|
5
|
+
if ("u" < typeof window) return;
|
|
6
|
+
const handleChunk = (chunk)=>{
|
|
7
|
+
if ('string' == typeof chunk) controller.enqueue(encoder.encode(chunk));
|
|
8
|
+
else controller.enqueue(chunk);
|
|
9
|
+
};
|
|
10
|
+
window.__FLIGHT_DATA = window.__FLIGHT_DATA || [];
|
|
11
|
+
window.__FLIGHT_DATA.forEach(handleChunk);
|
|
12
|
+
window.__FLIGHT_DATA.push = (chunk)=>{
|
|
13
|
+
handleChunk(chunk);
|
|
14
|
+
};
|
|
15
|
+
streamController = controller;
|
|
7
16
|
}
|
|
8
|
-
const handleChunk = (chunk) => {
|
|
9
|
-
if (typeof chunk === "string") {
|
|
10
|
-
controller.enqueue(encoder.encode(chunk));
|
|
11
|
-
} else {
|
|
12
|
-
controller.enqueue(chunk);
|
|
13
|
-
}
|
|
14
|
-
};
|
|
15
|
-
window.__FLIGHT_DATA = window.__FLIGHT_DATA || [];
|
|
16
|
-
window.__FLIGHT_DATA.forEach(handleChunk);
|
|
17
|
-
window.__FLIGHT_DATA.push = (chunk) => {
|
|
18
|
-
handleChunk(chunk);
|
|
19
|
-
};
|
|
20
|
-
streamController = controller;
|
|
21
|
-
}
|
|
22
17
|
});
|
|
23
|
-
if (typeof document
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
streamController === null || streamController === void 0 ? void 0 : streamController.close();
|
|
29
|
-
}
|
|
30
|
-
export {
|
|
31
|
-
rscStream
|
|
32
|
-
};
|
|
18
|
+
if ("u" > typeof document && 'loading' === document.readyState) document.addEventListener('DOMContentLoaded', ()=>{
|
|
19
|
+
streamController?.close();
|
|
20
|
+
});
|
|
21
|
+
else streamController?.close();
|
|
22
|
+
export { rscStream };
|
|
@@ -1,131 +1,107 @@
|
|
|
1
1
|
const encoder = new TextEncoder();
|
|
2
2
|
const closingTagsPattern = /<\/body>\s*<\/html>\s*$/i;
|
|
3
3
|
function injectRSCPayload(rscStream, { injectClosingTags = true }) {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
}
|
|
16
|
-
controller.enqueue(encoder.encode(buf));
|
|
17
|
-
}
|
|
18
|
-
buffered.length = 0;
|
|
19
|
-
timeout = null;
|
|
20
|
-
}
|
|
21
|
-
return new TransformStream({
|
|
22
|
-
transform(chunk, controller) {
|
|
23
|
-
buffered.push(chunk);
|
|
24
|
-
if (timeout) {
|
|
25
|
-
return;
|
|
26
|
-
}
|
|
27
|
-
timeout = setTimeout(async () => {
|
|
28
|
-
flushBufferedChunks(controller);
|
|
29
|
-
if (!startedRSC) {
|
|
30
|
-
startedRSC = true;
|
|
31
|
-
writeRSCStream(rscStream, controller).catch((err) => controller.error(err)).then(() => resolveFlightDataPromise());
|
|
4
|
+
const decoder = new TextDecoder();
|
|
5
|
+
let resolveFlightDataPromise;
|
|
6
|
+
const flightDataPromise = new Promise((resolve)=>resolveFlightDataPromise = resolve);
|
|
7
|
+
let startedRSC = false;
|
|
8
|
+
const buffered = [];
|
|
9
|
+
let timeout = null;
|
|
10
|
+
function flushBufferedChunks(controller) {
|
|
11
|
+
for (const chunk of buffered){
|
|
12
|
+
let buf = decoder.decode(chunk);
|
|
13
|
+
if (closingTagsPattern.test(buf)) buf = buf.replace(closingTagsPattern, '');
|
|
14
|
+
controller.enqueue(encoder.encode(buf));
|
|
32
15
|
}
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
async flush(controller) {
|
|
36
|
-
await flightDataPromise;
|
|
37
|
-
if (timeout) {
|
|
38
|
-
clearTimeout(timeout);
|
|
39
|
-
flushBufferedChunks(controller);
|
|
40
|
-
}
|
|
41
|
-
if (injectClosingTags) {
|
|
42
|
-
controller.enqueue(encoder.encode("</body></html>"));
|
|
43
|
-
}
|
|
16
|
+
buffered.length = 0;
|
|
17
|
+
timeout = null;
|
|
44
18
|
}
|
|
45
|
-
|
|
19
|
+
return new TransformStream({
|
|
20
|
+
transform (chunk, controller) {
|
|
21
|
+
buffered.push(chunk);
|
|
22
|
+
if (timeout) return;
|
|
23
|
+
timeout = setTimeout(async ()=>{
|
|
24
|
+
flushBufferedChunks(controller);
|
|
25
|
+
if (!startedRSC) {
|
|
26
|
+
startedRSC = true;
|
|
27
|
+
writeRSCStream(rscStream, controller).catch((err)=>controller.error(err)).then(()=>resolveFlightDataPromise());
|
|
28
|
+
}
|
|
29
|
+
}, 0);
|
|
30
|
+
},
|
|
31
|
+
async flush (controller) {
|
|
32
|
+
await flightDataPromise;
|
|
33
|
+
if (timeout) {
|
|
34
|
+
clearTimeout(timeout);
|
|
35
|
+
flushBufferedChunks(controller);
|
|
36
|
+
}
|
|
37
|
+
if (injectClosingTags) controller.enqueue(encoder.encode('</body></html>'));
|
|
38
|
+
}
|
|
39
|
+
});
|
|
46
40
|
}
|
|
47
41
|
async function writeRSCStream(rscStream, controller) {
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
})), controller);
|
|
42
|
+
const decoder = new TextDecoder('utf-8', {
|
|
43
|
+
fatal: true
|
|
44
|
+
});
|
|
45
|
+
for await (const chunk of rscStream)try {
|
|
46
|
+
writeChunk(JSON.stringify(decoder.decode(chunk, {
|
|
47
|
+
stream: true
|
|
48
|
+
})), controller);
|
|
56
49
|
} catch (err) {
|
|
57
|
-
|
|
58
|
-
|
|
50
|
+
const base64 = JSON.stringify(btoa(String.fromCodePoint(...chunk)));
|
|
51
|
+
writeChunk(`Uint8Array.from(atob(${base64}), m => m.codePointAt(0))`, controller);
|
|
59
52
|
}
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
if (remaining.length) {
|
|
63
|
-
writeChunk(JSON.stringify(remaining), controller);
|
|
64
|
-
}
|
|
53
|
+
const remaining = decoder.decode();
|
|
54
|
+
if (remaining.length) writeChunk(JSON.stringify(remaining), controller);
|
|
65
55
|
}
|
|
66
56
|
function writeChunk(chunk, controller) {
|
|
67
|
-
|
|
57
|
+
controller.enqueue(encoder.encode(`<script>${escapeScript(`(self.__FLIGHT_DATA||=[]).push(${chunk})`)}</script>`));
|
|
68
58
|
}
|
|
69
59
|
function escapeScript(script) {
|
|
70
|
-
|
|
60
|
+
return script.replace(/<!--/g, '<\\!--').replace(/<\/(script)/gi, '</\\$1');
|
|
71
61
|
}
|
|
72
62
|
function injectCSS(cssFiles, { injectClosingTags = true } = {}) {
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
if (injectClosingTags) {
|
|
80
|
-
controller.enqueue(encoder.encode("</body></html>"));
|
|
63
|
+
if (0 === cssFiles.length) return new TransformStream({
|
|
64
|
+
transform (chunk, controller) {
|
|
65
|
+
controller.enqueue(chunk);
|
|
66
|
+
},
|
|
67
|
+
flush (controller) {
|
|
68
|
+
if (injectClosingTags) controller.enqueue(encoder.encode('</body></html>'));
|
|
81
69
|
}
|
|
82
|
-
}
|
|
83
70
|
});
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
buffered.length = 0;
|
|
102
|
-
timeout = null;
|
|
103
|
-
}
|
|
104
|
-
return new TransformStream({
|
|
105
|
-
transform(chunk, controller) {
|
|
106
|
-
buffered.push(chunk);
|
|
107
|
-
if (timeout) {
|
|
108
|
-
return;
|
|
109
|
-
}
|
|
110
|
-
timeout = setTimeout(() => {
|
|
111
|
-
flushBufferedChunks(controller);
|
|
112
|
-
}, 0);
|
|
113
|
-
},
|
|
114
|
-
async flush(controller) {
|
|
115
|
-
if (timeout) {
|
|
116
|
-
clearTimeout(timeout);
|
|
117
|
-
flushBufferedChunks(controller);
|
|
118
|
-
}
|
|
119
|
-
if (!cssInjected) {
|
|
120
|
-
controller.enqueue(encoder.encode(cssLinks));
|
|
121
|
-
}
|
|
122
|
-
if (injectClosingTags) {
|
|
123
|
-
controller.enqueue(encoder.encode("</body></html>"));
|
|
124
|
-
}
|
|
71
|
+
const decoder = new TextDecoder();
|
|
72
|
+
const headTrailer = '</head>';
|
|
73
|
+
const buffered = [];
|
|
74
|
+
let timeout = null;
|
|
75
|
+
let cssInjected = false;
|
|
76
|
+
const cssLinks = cssFiles.map((css)=>`<link href="${css}" rel="stylesheet" />`).join('');
|
|
77
|
+
function flushBufferedChunks(controller) {
|
|
78
|
+
for (const chunk of buffered){
|
|
79
|
+
let buf = decoder.decode(chunk);
|
|
80
|
+
if (!cssInjected && buf.includes(headTrailer)) {
|
|
81
|
+
buf = buf.replace(headTrailer, `${cssLinks}${headTrailer}`);
|
|
82
|
+
cssInjected = true;
|
|
83
|
+
}
|
|
84
|
+
controller.enqueue(encoder.encode(buf));
|
|
85
|
+
}
|
|
86
|
+
buffered.length = 0;
|
|
87
|
+
timeout = null;
|
|
125
88
|
}
|
|
126
|
-
|
|
89
|
+
return new TransformStream({
|
|
90
|
+
transform (chunk, controller) {
|
|
91
|
+
buffered.push(chunk);
|
|
92
|
+
if (timeout) return;
|
|
93
|
+
timeout = setTimeout(()=>{
|
|
94
|
+
flushBufferedChunks(controller);
|
|
95
|
+
}, 0);
|
|
96
|
+
},
|
|
97
|
+
async flush (controller) {
|
|
98
|
+
if (timeout) {
|
|
99
|
+
clearTimeout(timeout);
|
|
100
|
+
flushBufferedChunks(controller);
|
|
101
|
+
}
|
|
102
|
+
if (!cssInjected) controller.enqueue(encoder.encode(cssLinks));
|
|
103
|
+
if (injectClosingTags) controller.enqueue(encoder.encode('</body></html>'));
|
|
104
|
+
}
|
|
105
|
+
});
|
|
127
106
|
}
|
|
128
|
-
export {
|
|
129
|
-
injectCSS,
|
|
130
|
-
injectRSCPayload
|
|
131
|
-
};
|
|
107
|
+
export { injectCSS, injectRSCPayload };
|
|
@@ -1,30 +1,28 @@
|
|
|
1
|
-
import { injectCSS, injectRSCPayload } from "../../rsc-html-stream/server";
|
|
2
|
-
import { renderRsc } from "./rsc";
|
|
1
|
+
import { injectCSS, injectRSCPayload } from "../../rsc-html-stream/server.js";
|
|
2
|
+
import { renderRsc } from "./rsc.js";
|
|
3
3
|
async function renderCSRWithRSC(options) {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
4
|
+
const entryCssFiles = __rspack_rsc_manifest__?.entryCssFiles;
|
|
5
|
+
const cssFiles = entryCssFiles ? Object.values(entryCssFiles).flat() : [];
|
|
6
|
+
const rscPayloadStream = renderRsc({
|
|
7
|
+
element: options.rscRoot
|
|
8
|
+
});
|
|
9
|
+
const htmlStream = new ReadableStream({
|
|
10
|
+
start (controller) {
|
|
11
|
+
const encoder = new TextEncoder();
|
|
12
|
+
controller.enqueue(encoder.encode(options.html));
|
|
13
|
+
controller.close();
|
|
14
|
+
}
|
|
15
|
+
});
|
|
16
|
+
const stream = htmlStream.pipeThrough(injectCSS(cssFiles, {
|
|
17
|
+
injectClosingTags: false
|
|
18
|
+
})).pipeThrough(injectRSCPayload(rscPayloadStream, {
|
|
19
|
+
injectClosingTags: true
|
|
20
|
+
}));
|
|
21
|
+
return new Response(stream, {
|
|
22
|
+
status: 200,
|
|
23
|
+
headers: new Headers({
|
|
24
|
+
'content-type': 'text/html; charset=UTF-8'
|
|
25
|
+
})
|
|
26
|
+
});
|
|
27
27
|
}
|
|
28
|
-
export {
|
|
29
|
-
renderCSRWithRSC
|
|
30
|
-
};
|
|
28
|
+
export { renderCSRWithRSC };
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export * from "./rsc";
|
|
2
|
-
export * from "./csr";
|
|
1
|
+
export * from "./rsc.js";
|
|
2
|
+
export * from "./csr.js";
|