forked from facebook/react
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathReactDOMLegacyServerNode.js
118 lines (103 loc) · 2.65 KB
/
ReactDOMLegacyServerNode.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/
import type {ReactNodeList} from 'shared/ReactTypes';
import type {Request} from 'react-server/src/ReactFizzServer';
import {
createRequest,
startWork,
startFlowing,
abort,
} from 'react-server/src/ReactFizzServer';
import {
createResponseState,
createRootFormatContext,
} from './ReactDOMServerLegacyFormatConfig';
import {
version,
renderToString,
renderToStaticMarkup,
} from './ReactDOMLegacyServerBrowser';
import {Readable} from 'stream';
type ServerOptions = {
identifierPrefix?: string,
};
class ReactMarkupReadableStream extends Readable {
request: Request;
startedFlowing: boolean;
constructor() {
// Calls the stream.Readable(options) constructor. Consider exposing built-in
// features like highWaterMark in the future.
super({});
this.request = (null: any);
this.startedFlowing = false;
}
_destroy(err, callback) {
abort(this.request);
// $FlowFixMe: The type definition for the callback should allow undefined and null.
callback(err);
}
_read(size) {
if (this.startedFlowing) {
startFlowing(this.request, this);
}
}
}
function onError() {
// Non-fatal errors are ignored.
}
function renderToNodeStreamImpl(
children: ReactNodeList,
options: void | ServerOptions,
generateStaticMarkup: boolean,
): Readable {
function onAllReady() {
// We wait until everything has loaded before starting to write.
// That way we only end up with fully resolved HTML even if we suspend.
destination.startedFlowing = true;
startFlowing(request, destination);
}
const destination = new ReactMarkupReadableStream();
const request = createRequest(
children,
createResponseState(false, options ? options.identifierPrefix : undefined),
createRootFormatContext(),
Infinity,
onError,
onAllReady,
undefined,
undefined,
);
destination.request = request;
startWork(request);
return destination;
}
function renderToNodeStream(
children: ReactNodeList,
options?: ServerOptions,
): Readable {
if (__DEV__) {
console.error(
'renderToNodeStream is deprecated. Use renderToPipeableStream instead.',
);
}
return renderToNodeStreamImpl(children, options, false);
}
function renderToStaticNodeStream(
children: ReactNodeList,
options?: ServerOptions,
): Readable {
return renderToNodeStreamImpl(children, options, true);
}
export {
renderToString,
renderToStaticMarkup,
renderToNodeStream,
renderToStaticNodeStream,
version,
};