UNPKG

3.94 kBJavaScriptView Raw
1/** @license React v16.9.0
2 * react-dom-unstable-fizz.node.development.js
3 *
4 * Copyright (c) Facebook, Inc. and its affiliates.
5 *
6 * This source code is licensed under the MIT license found in the
7 * LICENSE file in the root directory of this source tree.
8 */
9
10'use strict';
11
12
13
14if (process.env.NODE_ENV !== "production") {
15 (function() {
16'use strict';
17
18function scheduleWork(callback) {
19 setImmediate(callback);
20}
21
22function flushBuffered(destination) {
23 // If we don't have any more data to send right now.
24 // Flush whatever is in the buffer to the wire.
25 if (typeof destination.flush === 'function') {
26 // By convention the Zlib streams provide a flush function for this purpose.
27 destination.flush();
28 }
29}
30
31function beginWriting(destination) {
32 destination.cork();
33}
34
35function writeChunk(destination, buffer) {
36 var nodeBuffer = buffer; // close enough
37 destination.write(nodeBuffer);
38}
39
40function completeWriting(destination) {
41 destination.uncork();
42}
43
44function close(destination) {
45 destination.end();
46}
47
48function convertStringToBuffer(content) {
49 return Buffer.from(content, 'utf8');
50}
51
52function formatChunk(type, props) {
53 var str = '<' + type + '>';
54 if (typeof props.children === 'string') {
55 str += props.children;
56 }
57 str += '</' + type + '>';
58 return convertStringToBuffer(str);
59}
60
61// The Symbol used to tag the ReactElement-like types. If there is no native Symbol
62// nor polyfill, then a plain number is used for performance.
63var hasSymbol = typeof Symbol === 'function' && Symbol.for;
64
65var REACT_ELEMENT_TYPE = hasSymbol ? Symbol.for('react.element') : 0xeac7;
66
67
68
69
70
71
72// TODO: We don't use AsyncMode or ConcurrentMode anymore. They were temporary
73// (unstable) APIs that have been removed. Can we remove the symbols?
74
75function createRequest(children, destination) {
76 return { destination: destination, children: children, completedChunks: [], flowing: false };
77}
78
79function performWork(request) {
80 var element = request.children;
81 request.children = null;
82 if (element && element.$$typeof !== REACT_ELEMENT_TYPE) {
83 return;
84 }
85 var type = element.type;
86 var props = element.props;
87 if (typeof type !== 'string') {
88 return;
89 }
90 request.completedChunks.push(formatChunk(type, props));
91 if (request.flowing) {
92 flushCompletedChunks(request);
93 }
94
95 flushBuffered(request.destination);
96}
97
98function flushCompletedChunks(request) {
99 var destination = request.destination;
100 var chunks = request.completedChunks;
101 request.completedChunks = [];
102
103 beginWriting(destination);
104 try {
105 for (var i = 0; i < chunks.length; i++) {
106 var chunk = chunks[i];
107 writeChunk(destination, chunk);
108 }
109 } finally {
110 completeWriting(destination);
111 }
112 close(destination);
113}
114
115function startWork(request) {
116 request.flowing = true;
117 scheduleWork(function () {
118 return performWork(request);
119 });
120}
121
122function startFlowing(request, desiredBytes) {
123 request.flowing = false;
124 flushCompletedChunks(request);
125}
126
127// This file intentionally does *not* have the Flow annotation.
128// Don't add it. See `./inline-typed.js` for an explanation.
129
130function createDrainHandler(destination, request) {
131 return function () {
132 return startFlowing(request, 0);
133 };
134}
135
136function pipeToNodeWritable(children, destination) {
137 var request = createRequest(children, destination);
138 destination.on('drain', createDrainHandler(destination, request));
139 startWork(request);
140}
141
142var ReactDOMFizzServerNode = {
143 pipeToNodeWritable: pipeToNodeWritable
144};
145
146var ReactDOMFizzServerNode$1 = Object.freeze({
147 default: ReactDOMFizzServerNode
148});
149
150var ReactDOMFizzServerNode$2 = ( ReactDOMFizzServerNode$1 && ReactDOMFizzServerNode ) || ReactDOMFizzServerNode$1;
151
152// TODO: decide on the top-level export form.
153// This is hacky but makes it work with both Rollup and Jest
154var unstableFizz_node = ReactDOMFizzServerNode$2.default || ReactDOMFizzServerNode$2;
155
156module.exports = unstableFizz_node;
157 })();
158}