|
| 1 | +/** |
| 2 | + * Copyright (c) Facebook, Inc. and its affiliates. |
| 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 | + * @flow |
| 8 | + */ |
| 9 | + |
| 10 | +import type {ReactNodeList} from 'shared/ReactTypes'; |
| 11 | + |
| 12 | +import type {Request} from 'react-server/src/ReactFizzServer'; |
| 13 | + |
| 14 | +import { |
| 15 | + createRequest, |
| 16 | + startWork, |
| 17 | + startFlowing, |
| 18 | + abort, |
| 19 | +} from 'react-server/src/ReactFizzServer'; |
| 20 | + |
| 21 | +import { |
| 22 | + createResponseState, |
| 23 | + createRootFormatContext, |
| 24 | +} from './ReactDOMServerLegacyFormatConfig'; |
| 25 | + |
| 26 | +import { |
| 27 | + version, |
| 28 | + renderToString, |
| 29 | + renderToStaticMarkup, |
| 30 | +} from './ReactDOMLegacyServerBrowser'; |
| 31 | + |
| 32 | +import {Readable} from 'stream'; |
| 33 | + |
| 34 | +type ServerOptions = { |
| 35 | + identifierPrefix?: string, |
| 36 | +}; |
| 37 | + |
| 38 | +class ReactMarkupReadableStream extends Readable { |
| 39 | + request: Request; |
| 40 | + startedFlowing: boolean; |
| 41 | + constructor() { |
| 42 | + // Calls the stream.Readable(options) constructor. Consider exposing built-in |
| 43 | + // features like highWaterMark in the future. |
| 44 | + super({}); |
| 45 | + this.request = (null: any); |
| 46 | + this.startedFlowing = false; |
| 47 | + } |
| 48 | + |
| 49 | + _destroy(err, callback) { |
| 50 | + abort(this.request); |
| 51 | + // $FlowFixMe: The type definition for the callback should allow undefined and null. |
| 52 | + callback(err); |
| 53 | + } |
| 54 | + |
| 55 | + _read(size) { |
| 56 | + if (this.startedFlowing) { |
| 57 | + startFlowing(this.request); |
| 58 | + } |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +function onError() { |
| 63 | + // Non-fatal errors are ignored. |
| 64 | +} |
| 65 | + |
| 66 | +function renderToNodeStreamImpl( |
| 67 | + children: ReactNodeList, |
| 68 | + options: void | ServerOptions, |
| 69 | + generateStaticMarkup: boolean, |
| 70 | +): Readable { |
| 71 | + function onCompleteAll() { |
| 72 | + // We wait until everything has loaded before starting to write. |
| 73 | + // That way we only end up with fully resolved HTML even if we suspend. |
| 74 | + destination.startedFlowing = true; |
| 75 | + startFlowing(request); |
| 76 | + } |
| 77 | + const destination = new ReactMarkupReadableStream(); |
| 78 | + const request = createRequest( |
| 79 | + children, |
| 80 | + destination, |
| 81 | + createResponseState(false, options ? options.identifierPrefix : undefined), |
| 82 | + createRootFormatContext(undefined), |
| 83 | + Infinity, |
| 84 | + onError, |
| 85 | + onCompleteAll, |
| 86 | + undefined, |
| 87 | + ); |
| 88 | + destination.request = request; |
| 89 | + startWork(request); |
| 90 | + return destination; |
| 91 | +} |
| 92 | + |
| 93 | +function renderToNodeStream( |
| 94 | + children: ReactNodeList, |
| 95 | + options?: ServerOptions, |
| 96 | +): Readable { |
| 97 | + return renderToNodeStreamImpl(children, options, false); |
| 98 | +} |
| 99 | + |
| 100 | +function renderToStaticNodeStream( |
| 101 | + children: ReactNodeList, |
| 102 | + options?: ServerOptions, |
| 103 | +): Readable { |
| 104 | + return renderToNodeStreamImpl(children, options, true); |
| 105 | +} |
| 106 | + |
| 107 | +export { |
| 108 | + renderToString, |
| 109 | + renderToStaticMarkup, |
| 110 | + renderToNodeStream, |
| 111 | + renderToStaticNodeStream, |
| 112 | + version, |
| 113 | +}; |
0 commit comments