UNPKG

3.78 kBJavaScriptView Raw
1/** @license React v16.8.6
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
67function createRequest(children, destination) {
68 return { destination: destination, children: children, completedChunks: [], flowing: false };
69}
70
71function performWork(request) {
72 var element = request.children;
73 request.children = null;
74 if (element && element.$$typeof !== REACT_ELEMENT_TYPE) {
75 return;
76 }
77 var type = element.type;
78 var props = element.props;
79 if (typeof type !== 'string') {
80 return;
81 }
82 request.completedChunks.push(formatChunk(type, props));
83 if (request.flowing) {
84 flushCompletedChunks(request);
85 }
86
87 flushBuffered(request.destination);
88}
89
90function flushCompletedChunks(request) {
91 var destination = request.destination;
92 var chunks = request.completedChunks;
93 request.completedChunks = [];
94
95 beginWriting(destination);
96 try {
97 for (var i = 0; i < chunks.length; i++) {
98 var chunk = chunks[i];
99 writeChunk(destination, chunk);
100 }
101 } finally {
102 completeWriting(destination);
103 }
104 close(destination);
105}
106
107function startWork(request) {
108 request.flowing = true;
109 scheduleWork(function () {
110 return performWork(request);
111 });
112}
113
114function startFlowing(request, desiredBytes) {
115 request.flowing = false;
116 flushCompletedChunks(request);
117}
118
119// This file intentionally does *not* have the Flow annotation.
120// Don't add it. See `./inline-typed.js` for an explanation.
121
122function createDrainHandler(destination, request) {
123 return function () {
124 return startFlowing(request, 0);
125 };
126}
127
128function pipeToNodeWritable(children, destination) {
129 var request = createRequest(children, destination);
130 destination.on('drain', createDrainHandler(destination, request));
131 startWork(request);
132}
133
134var ReactDOMFizzServerNode = {
135 pipeToNodeWritable: pipeToNodeWritable
136};
137
138var ReactDOMFizzServerNode$1 = Object.freeze({
139 default: ReactDOMFizzServerNode
140});
141
142var ReactDOMFizzServerNode$2 = ( ReactDOMFizzServerNode$1 && ReactDOMFizzServerNode ) || ReactDOMFizzServerNode$1;
143
144// TODO: decide on the top-level export form.
145// This is hacky but makes it work with both Rollup and Jest
146var unstableFizz_node = ReactDOMFizzServerNode$2.default || ReactDOMFizzServerNode$2;
147
148module.exports = unstableFizz_node;
149 })();
150}