1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | 'use strict';
|
11 |
|
12 |
|
13 |
|
14 | if (process.env.NODE_ENV !== "production") {
|
15 | (function() {
|
16 | 'use strict';
|
17 |
|
18 | var ReactDOMServer = require('react-dom/server');
|
19 |
|
20 | function scheduleWork(callback) {
|
21 | callback();
|
22 | }
|
23 |
|
24 |
|
25 | function writeChunk(destination, buffer) {
|
26 | destination.enqueue(buffer);
|
27 | return destination.desiredSize > 0;
|
28 | }
|
29 |
|
30 | function close(destination) {
|
31 | destination.close();
|
32 | }
|
33 | var textEncoder = new TextEncoder();
|
34 | function convertStringToBuffer(content) {
|
35 | return textEncoder.encode(content);
|
36 | }
|
37 |
|
38 | function renderHostChildrenToString(children) {
|
39 |
|
40 |
|
41 |
|
42 |
|
43 | return ReactDOMServer.renderToStaticMarkup(children);
|
44 | }
|
45 |
|
46 |
|
47 |
|
48 | var hasSymbol = typeof Symbol === 'function' && Symbol.for;
|
49 | var REACT_ELEMENT_TYPE = hasSymbol ? Symbol.for('react.element') : 0xeac7;
|
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 | var stringify = JSON.stringify;
|
113 | function createRequest(model, destination) {
|
114 | var pingedSegments = [];
|
115 | var request = {
|
116 | destination: destination,
|
117 | nextChunkId: 0,
|
118 | pendingChunks: 0,
|
119 | pingedSegments: pingedSegments,
|
120 | completedJSONChunks: [],
|
121 | completedErrorChunks: [],
|
122 | flowing: false,
|
123 | toJSON: function (key, value) {
|
124 | return resolveModelToJSON(request, value);
|
125 | }
|
126 | };
|
127 | request.pendingChunks++;
|
128 | var rootSegment = createSegment(request, model);
|
129 | pingedSegments.push(rootSegment);
|
130 | return request;
|
131 | }
|
132 |
|
133 | function attemptResolveModelComponent(element) {
|
134 | var type = element.type;
|
135 | var props = element.props;
|
136 |
|
137 | if (typeof type === 'function') {
|
138 |
|
139 | return type(props);
|
140 | } else if (typeof type === 'string') {
|
141 |
|
142 | return renderHostChildrenToString(element);
|
143 | } else {
|
144 | throw new Error('Unsupported type.');
|
145 | }
|
146 | }
|
147 |
|
148 | function pingSegment(request, segment) {
|
149 | var pingedSegments = request.pingedSegments;
|
150 | pingedSegments.push(segment);
|
151 |
|
152 | if (pingedSegments.length === 1) {
|
153 | scheduleWork(function () {
|
154 | return performWork(request);
|
155 | });
|
156 | }
|
157 | }
|
158 |
|
159 | function createSegment(request, model) {
|
160 | var id = request.nextChunkId++;
|
161 | var segment = {
|
162 | id: id,
|
163 | model: model,
|
164 | ping: function () {
|
165 | return pingSegment(request, segment);
|
166 | }
|
167 | };
|
168 | return segment;
|
169 | }
|
170 |
|
171 | function serializeIDRef(id) {
|
172 | return '$' + id.toString(16);
|
173 | }
|
174 |
|
175 | function serializeRowHeader(tag, id) {
|
176 | return tag + id.toString(16) + ':';
|
177 | }
|
178 |
|
179 | function escapeStringValue(value) {
|
180 | if (value[0] === '$') {
|
181 |
|
182 |
|
183 | return '$' + value;
|
184 | } else {
|
185 | return value;
|
186 | }
|
187 | }
|
188 |
|
189 | function resolveModelToJSON(request, value) {
|
190 | if (typeof value === 'string') {
|
191 | return escapeStringValue(value);
|
192 | }
|
193 |
|
194 | while (typeof value === 'object' && value !== null && value.$$typeof === REACT_ELEMENT_TYPE) {
|
195 | var element = value;
|
196 |
|
197 | try {
|
198 | value = attemptResolveModelComponent(element);
|
199 | } catch (x) {
|
200 | if (typeof x === 'object' && x !== null && typeof x.then === 'function') {
|
201 |
|
202 | request.pendingChunks++;
|
203 | var newSegment = createSegment(request, element);
|
204 | var ping = newSegment.ping;
|
205 | x.then(ping, ping);
|
206 | return serializeIDRef(newSegment.id);
|
207 | } else {
|
208 | request.pendingChunks++;
|
209 | var errorId = request.nextChunkId++;
|
210 | emitErrorChunk(request, errorId, x);
|
211 | return serializeIDRef(errorId);
|
212 | }
|
213 | }
|
214 | }
|
215 |
|
216 | return value;
|
217 | }
|
218 |
|
219 | function emitErrorChunk(request, id, error) {
|
220 |
|
221 |
|
222 |
|
223 | var message;
|
224 | var stack = '';
|
225 |
|
226 | try {
|
227 | if (error instanceof Error) {
|
228 | message = '' + error.message;
|
229 | stack = '' + error.stack;
|
230 | } else {
|
231 | message = 'Error: ' + error;
|
232 | }
|
233 | } catch (x) {
|
234 | message = 'An error occurred but serializing the error message failed.';
|
235 | }
|
236 |
|
237 | var errorInfo = {
|
238 | message: message,
|
239 | stack: stack
|
240 | };
|
241 | var row = serializeRowHeader('E', id) + stringify(errorInfo) + '\n';
|
242 | request.completedErrorChunks.push(convertStringToBuffer(row));
|
243 | }
|
244 |
|
245 | function retrySegment(request, segment) {
|
246 | var value = segment.model;
|
247 |
|
248 | try {
|
249 | while (typeof value === 'object' && value !== null && value.$$typeof === REACT_ELEMENT_TYPE) {
|
250 |
|
251 |
|
252 | var element = value;
|
253 | segment.model = element;
|
254 | value = attemptResolveModelComponent(element);
|
255 | }
|
256 |
|
257 | var json = stringify(value, request.toJSON);
|
258 | var row;
|
259 | var id = segment.id;
|
260 |
|
261 | if (id === 0) {
|
262 | row = json + '\n';
|
263 | } else {
|
264 | row = serializeRowHeader('J', id) + json + '\n';
|
265 | }
|
266 |
|
267 | request.completedJSONChunks.push(convertStringToBuffer(row));
|
268 | } catch (x) {
|
269 | if (typeof x === 'object' && x !== null && typeof x.then === 'function') {
|
270 |
|
271 | var ping = segment.ping;
|
272 | x.then(ping, ping);
|
273 | return;
|
274 | } else {
|
275 |
|
276 | emitErrorChunk(request, segment.id, x);
|
277 | }
|
278 | }
|
279 | }
|
280 |
|
281 | function performWork(request) {
|
282 | var pingedSegments = request.pingedSegments;
|
283 | request.pingedSegments = [];
|
284 |
|
285 | for (var i = 0; i < pingedSegments.length; i++) {
|
286 | var segment = pingedSegments[i];
|
287 | retrySegment(request, segment);
|
288 | }
|
289 |
|
290 | if (request.flowing) {
|
291 | flushCompletedChunks(request);
|
292 | }
|
293 | }
|
294 |
|
295 | var reentrant = false;
|
296 |
|
297 | function flushCompletedChunks(request) {
|
298 | if (reentrant) {
|
299 | return;
|
300 | }
|
301 |
|
302 | reentrant = true;
|
303 | var destination = request.destination;
|
304 | try {
|
305 | var jsonChunks = request.completedJSONChunks;
|
306 | var i = 0;
|
307 |
|
308 | for (; i < jsonChunks.length; i++) {
|
309 | request.pendingChunks--;
|
310 | var chunk = jsonChunks[i];
|
311 |
|
312 | if (!writeChunk(destination, chunk)) {
|
313 | request.flowing = false;
|
314 | i++;
|
315 | break;
|
316 | }
|
317 | }
|
318 |
|
319 | jsonChunks.splice(0, i);
|
320 | var errorChunks = request.completedErrorChunks;
|
321 | i = 0;
|
322 |
|
323 | for (; i < errorChunks.length; i++) {
|
324 | request.pendingChunks--;
|
325 | var _chunk = errorChunks[i];
|
326 |
|
327 | if (!writeChunk(destination, _chunk)) {
|
328 | request.flowing = false;
|
329 | i++;
|
330 | break;
|
331 | }
|
332 | }
|
333 |
|
334 | errorChunks.splice(0, i);
|
335 | } finally {
|
336 | reentrant = false;
|
337 |
|
338 | }
|
339 |
|
340 | if (request.pendingChunks === 0) {
|
341 |
|
342 | close(destination);
|
343 | }
|
344 | }
|
345 |
|
346 | function startWork(request) {
|
347 | request.flowing = true;
|
348 | scheduleWork(function () {
|
349 | return performWork(request);
|
350 | });
|
351 | }
|
352 | function startFlowing(request) {
|
353 | request.flowing = true;
|
354 | flushCompletedChunks(request);
|
355 | }
|
356 |
|
357 |
|
358 |
|
359 |
|
360 | function renderToReadableStream(model) {
|
361 | var request;
|
362 | return new ReadableStream({
|
363 | start: function (controller) {
|
364 | request = createRequest(model, controller);
|
365 | startWork(request);
|
366 | },
|
367 | pull: function (controller) {
|
368 | startFlowing(request);
|
369 | },
|
370 | cancel: function (reason) {}
|
371 | });
|
372 | }
|
373 |
|
374 | var ReactFlightDOMServerBrowser = {
|
375 | renderToReadableStream: renderToReadableStream
|
376 | };
|
377 |
|
378 | var ReactFlightDOMServerBrowser$1 = Object.freeze({
|
379 | default: ReactFlightDOMServerBrowser
|
380 | });
|
381 |
|
382 | var ReactFlightDOMServerBrowser$2 = ( ReactFlightDOMServerBrowser$1 && ReactFlightDOMServerBrowser ) || ReactFlightDOMServerBrowser$1;
|
383 |
|
384 |
|
385 |
|
386 |
|
387 |
|
388 | var unstableFlightServer_browser = ReactFlightDOMServerBrowser$2.default || ReactFlightDOMServerBrowser$2;
|
389 |
|
390 | module.exports = unstableFlightServer_browser;
|
391 | })();
|
392 | }
|