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