-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
36 lines (32 loc) · 864 Bytes
/
index.js
1
2
3
4
5
6
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
const {
renderToNodeStream,
renderToStaticNodeStream,
} = require("react-dom/server");
const { Writable, pipeline } = require("stream");
const createRenderToStringAsync = (renderToNodeStreamFn) => (
node,
{ scheduleFn = setImmediate } = {}
) =>
new Promise((resolve, reject) => {
let body = "";
const writableStream = new Writable({
write: (chunk, encoding, callback) => {
body += chunk;
scheduleFn(callback);
},
});
pipeline(renderToNodeStreamFn(node), writableStream, (ex) => {
if (ex) {
return reject(ex);
}
resolve(body);
});
});
const renderToStringAsync = createRenderToStringAsync(renderToNodeStream);
const renderToStaticMarkupAsync = createRenderToStringAsync(
renderToStaticNodeStream
);
module.exports = {
renderToStringAsync,
renderToStaticMarkupAsync,
};