|
| 1 | +/** |
| 2 | + * Copyright (c) 2013-present, Facebook, Inc. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + */ |
| 7 | + |
| 8 | +import {Readable} from 'stream'; |
| 9 | +import { Transform } from 'stream'; |
| 10 | + |
| 11 | +import ReactPartialRenderer from './ReactPartialRenderer'; |
| 12 | + |
| 13 | +// This is a Readable Node.js stream which wraps the ReactDOMPartialRenderer. |
| 14 | +class ReactMarkupReadableStream extends Readable { |
| 15 | + constructor(element, makeStaticMarkup, cache, streamingStart, memLife) { |
| 16 | + // Calls the stream.Readable(options) constructor. Consider exposing built-in |
| 17 | + // features like highWaterMark in the future. |
| 18 | + super({}); |
| 19 | + this.cache = cache; |
| 20 | + this.streamingStart = streamingStart; |
| 21 | + this.memLife = memLife; |
| 22 | + this.partialRenderer = new ReactPartialRenderer(element, makeStaticMarkup); |
| 23 | + } |
| 24 | + |
| 25 | + async _read(size) { |
| 26 | + try { |
| 27 | + let readOutput = await this.partialRenderer.read(size, |
| 28 | + this.cache, |
| 29 | + true, |
| 30 | + this.streamingStart, |
| 31 | + this.memLife); |
| 32 | + this.push(readOutput); |
| 33 | + } catch (err) { |
| 34 | + this.emit('error', err); |
| 35 | + } |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +function createCacheStream(cache, streamingStart, memLife = 0) { |
| 40 | + const bufferedChunks = []; |
| 41 | + return new Transform({ |
| 42 | + // transform() is called with each chunk of data |
| 43 | + transform(data, enc, cb) { |
| 44 | + // We store the chunk of data (which is a Buffer) in memory |
| 45 | + bufferedChunks.push(data); |
| 46 | + // Then pass the data unchanged onwards to the next stream |
| 47 | + cb(null, data); |
| 48 | + }, |
| 49 | + |
| 50 | + // flush() is called when everything is done |
| 51 | + flush(cb) { |
| 52 | + // We concatenate all the buffered chunks of HTML to get the full HTML, then cache it at "key" |
| 53 | + let html = bufferedChunks.join(""); |
| 54 | + delete streamingStart.sliceStartCount; |
| 55 | + |
| 56 | + for (let component in streamingStart) { |
| 57 | + let tagStack = []; |
| 58 | + let tagStart; |
| 59 | + let tagEnd; |
| 60 | + |
| 61 | + do { |
| 62 | + if (!tagStart) { |
| 63 | + tagStart = streamingStart[component]; |
| 64 | + } else { |
| 65 | + tagStart = (html[tagEnd] === '<') ? tagEnd : html.indexOf('<', tagEnd); |
| 66 | + } |
| 67 | + tagEnd = html.indexOf('>', tagStart) + 1; |
| 68 | + // Skip stack logic for void/self-closing elements and HTML comments |
| 69 | + if (html[tagEnd - 2] !== '/' && html[tagStart + 1] !== '!') { |
| 70 | + // Push opening tags onto stack; pop closing tags off of stack |
| 71 | + if (html[tagStart + 1] !== '/') { |
| 72 | + tagStack.push(html.slice(tagStart, tagEnd)); |
| 73 | + } else { |
| 74 | + tagStack.pop(); |
| 75 | + } |
| 76 | + } |
| 77 | + } while (tagStack.length !== 0); |
| 78 | + // cache component by slicing 'html' |
| 79 | + if (memLife) { |
| 80 | + cache.set(component, html.slice(streamingStart[component], tagEnd), memLife, (err) => { |
| 81 | + if (err) { |
| 82 | + console.log(err); |
| 83 | + } |
| 84 | + }); |
| 85 | + } else { |
| 86 | + cache.set(component, html.slice(streamingStart[component], tagEnd)); |
| 87 | + } |
| 88 | + } |
| 89 | + cb(); |
| 90 | + }, |
| 91 | + }); |
| 92 | +} |
| 93 | +/** |
| 94 | + * Render a ReactElement to its initial HTML. This should only be used on the |
| 95 | + * server. |
| 96 | + * See https://reactjs.org/docs/react-dom-stream.html#rendertonodestream |
| 97 | + */ |
| 98 | +function originalRenderToNodeStream(element, cache, streamingStart, memLife=0) { |
| 99 | + return new ReactMarkupReadableStream(element, false, cache, streamingStart, memLife); |
| 100 | +} |
| 101 | + |
| 102 | +export function renderToNodeStream(element, cache, res) { |
| 103 | + |
| 104 | + const htmlStart = |
| 105 | + '<html><head><title>Page</title></head><body><div id="react-root">'; |
| 106 | + |
| 107 | + const htmlEnd = '</div></body></html>'; |
| 108 | + |
| 109 | + const streamingStart = { |
| 110 | + sliceStartCount: htmlStart.length, |
| 111 | + }; |
| 112 | + |
| 113 | + const cacheStream = createCacheStream(cache, streamingStart); |
| 114 | + cacheStream.pipe(res); |
| 115 | + cacheStream.write(htmlStart); |
| 116 | + |
| 117 | + const stream = originalRenderToNodeStream(element, cache, streamingStart); |
| 118 | + stream.pipe(cacheStream, { end: false }); |
| 119 | + stream.on("end", () => { |
| 120 | + cacheStream.end(htmlEnd); |
| 121 | + }); |
| 122 | + |
| 123 | +} |
| 124 | + |
| 125 | +/** |
| 126 | + * Similar to renderToNodeStream, except this doesn't create extra DOM attributes |
| 127 | + * such as data-react-id that React uses internally. |
| 128 | + * See https://reactjs.org/docs/react-dom-stream.html#rendertostaticnodestream |
| 129 | + */ |
| 130 | +function originalRenderToStaticNodeStream(element, cache, streamingStart, memLife=0) { |
| 131 | + return new ReactMarkupReadableStream(element, true, cache, streamingStart, memLife); |
| 132 | +} |
| 133 | + |
| 134 | +export function renderToStaticNodeStream(element, cache, res) { |
| 135 | + const htmlStart = |
| 136 | + '<html><head><title>Page</title></head><body><div id="react-root">'; |
| 137 | + |
| 138 | + const htmlEnd = '</div></body></html>'; |
| 139 | + |
| 140 | + const streamingStart = { |
| 141 | + sliceStartCount: htmlStart.length, |
| 142 | + }; |
| 143 | + |
| 144 | + const cacheStream = createCacheStream(cache, streamingStart); |
| 145 | + cacheStream.pipe(res); |
| 146 | + cacheStream.write(htmlStart); |
| 147 | + |
| 148 | + const stream = originalRenderToStaticNodeStream(element, cache, streamingStart); |
| 149 | + stream.pipe(cacheStream, { end: false }); |
| 150 | + stream.on("end", () => { |
| 151 | + cacheStream.end(htmlEnd); |
| 152 | + }); |
| 153 | +} |
0 commit comments