Dashboard sipadu mbip
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

react-dom-unstable-flight-server.browser.development.js 9.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. /** @license React v16.12.0
  2. * react-dom-unstable-flight-server.browser.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. 'use strict';
  10. (function (global, factory) {
  11. typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('react-dom/server')) :
  12. typeof define === 'function' && define.amd ? define(['react-dom/server'], factory) :
  13. (global.ReactFlightDOMServer = factory(global.ReactDOMServer));
  14. }(this, (function (ReactDOMServer) { 'use strict';
  15. function scheduleWork(callback) {
  16. callback();
  17. }
  18. function writeChunk(destination, buffer) {
  19. destination.enqueue(buffer);
  20. return destination.desiredSize > 0;
  21. }
  22. function close(destination) {
  23. destination.close();
  24. }
  25. var textEncoder = new TextEncoder();
  26. function convertStringToBuffer(content) {
  27. return textEncoder.encode(content);
  28. }
  29. function renderHostChildrenToString(children) {
  30. // TODO: This file is used to actually implement a server renderer
  31. // so we can't actually reference the renderer here. Instead, we
  32. // should replace this method with a reference to Fizz which
  33. // then uses this file to implement the server renderer.
  34. return ReactDOMServer.renderToStaticMarkup(children);
  35. }
  36. // The Symbol used to tag the ReactElement-like types. If there is no native Symbol
  37. // nor polyfill, then a plain number is used for performance.
  38. var hasSymbol = typeof Symbol === 'function' && Symbol.for;
  39. var REACT_ELEMENT_TYPE = hasSymbol ? Symbol.for('react.element') : 0xeac7;
  40. // TODO: We don't use AsyncMode or ConcurrentMode anymore. They were temporary
  41. // (unstable) APIs that have been removed. Can we remove the symbols?
  42. /*
  43. FLIGHT PROTOCOL GRAMMAR
  44. Response
  45. - JSONData RowSequence
  46. - JSONData
  47. RowSequence
  48. - Row RowSequence
  49. - Row
  50. Row
  51. - "J" RowID JSONData
  52. - "H" RowID HTMLData
  53. - "B" RowID BlobData
  54. - "U" RowID URLData
  55. - "E" RowID ErrorData
  56. RowID
  57. - HexDigits ":"
  58. HexDigits
  59. - HexDigit HexDigits
  60. - HexDigit
  61. HexDigit
  62. - 0-F
  63. URLData
  64. - (UTF8 encoded URL) "\n"
  65. ErrorData
  66. - (UTF8 encoded JSON: {message: "...", stack: "..."}) "\n"
  67. JSONData
  68. - (UTF8 encoded JSON) "\n"
  69. - String values that begin with $ are escaped with a "$" prefix.
  70. - References to other rows are encoding as JSONReference strings.
  71. JSONReference
  72. - "$" HexDigits
  73. HTMLData
  74. - ByteSize (UTF8 encoded HTML)
  75. BlobData
  76. - ByteSize (Binary Data)
  77. ByteSize
  78. - (unsigned 32-bit integer)
  79. */
  80. // TODO: Implement HTMLData, BlobData and URLData.
  81. var stringify = JSON.stringify;
  82. function createRequest(model, destination) {
  83. var pingedSegments = [];
  84. var request = {
  85. destination: destination,
  86. nextChunkId: 0,
  87. pendingChunks: 0,
  88. pingedSegments: pingedSegments,
  89. completedJSONChunks: [],
  90. completedErrorChunks: [],
  91. flowing: false,
  92. toJSON: function (key, value) {
  93. return resolveModelToJSON(request, value);
  94. }
  95. };
  96. request.pendingChunks++;
  97. var rootSegment = createSegment(request, model);
  98. pingedSegments.push(rootSegment);
  99. return request;
  100. }
  101. function attemptResolveModelComponent(element) {
  102. var type = element.type;
  103. var props = element.props;
  104. if (typeof type === 'function') {
  105. // This is a nested view model.
  106. return type(props);
  107. } else if (typeof type === 'string') {
  108. // This is a host element. E.g. HTML.
  109. return renderHostChildrenToString(element);
  110. } else {
  111. throw new Error('Unsupported type.');
  112. }
  113. }
  114. function pingSegment(request, segment) {
  115. var pingedSegments = request.pingedSegments;
  116. pingedSegments.push(segment);
  117. if (pingedSegments.length === 1) {
  118. scheduleWork(function () {
  119. return performWork(request);
  120. });
  121. }
  122. }
  123. function createSegment(request, model) {
  124. var id = request.nextChunkId++;
  125. var segment = {
  126. id: id,
  127. model: model,
  128. ping: function () {
  129. return pingSegment(request, segment);
  130. }
  131. };
  132. return segment;
  133. }
  134. function serializeIDRef(id) {
  135. return '$' + id.toString(16);
  136. }
  137. function serializeRowHeader(tag, id) {
  138. return tag + id.toString(16) + ':';
  139. }
  140. function escapeStringValue(value) {
  141. if (value[0] === '$') {
  142. // We need to escape $ prefixed strings since we use that to encode
  143. // references to IDs.
  144. return '$' + value;
  145. } else {
  146. return value;
  147. }
  148. }
  149. function resolveModelToJSON(request, value) {
  150. if (typeof value === 'string') {
  151. return escapeStringValue(value);
  152. }
  153. while (typeof value === 'object' && value !== null && value.$$typeof === REACT_ELEMENT_TYPE) {
  154. var element = value;
  155. try {
  156. value = attemptResolveModelComponent(element);
  157. } catch (x) {
  158. if (typeof x === 'object' && x !== null && typeof x.then === 'function') {
  159. // Something suspended, we'll need to create a new segment and resolve it later.
  160. request.pendingChunks++;
  161. var newSegment = createSegment(request, element);
  162. var ping = newSegment.ping;
  163. x.then(ping, ping);
  164. return serializeIDRef(newSegment.id);
  165. } else {
  166. request.pendingChunks++;
  167. var errorId = request.nextChunkId++;
  168. emitErrorChunk(request, errorId, x);
  169. return serializeIDRef(errorId);
  170. }
  171. }
  172. }
  173. return value;
  174. }
  175. function emitErrorChunk(request, id, error) {
  176. // TODO: We should not leak error messages to the client in prod.
  177. // Give this an error code instead and log on the server.
  178. // We can serialize the error in DEV as a convenience.
  179. var message;
  180. var stack = '';
  181. try {
  182. if (error instanceof Error) {
  183. message = '' + error.message;
  184. stack = '' + error.stack;
  185. } else {
  186. message = 'Error: ' + error;
  187. }
  188. } catch (x) {
  189. message = 'An error occurred but serializing the error message failed.';
  190. }
  191. var errorInfo = {
  192. message: message,
  193. stack: stack
  194. };
  195. var row = serializeRowHeader('E', id) + stringify(errorInfo) + '\n';
  196. request.completedErrorChunks.push(convertStringToBuffer(row));
  197. }
  198. function retrySegment(request, segment) {
  199. var value = segment.model;
  200. try {
  201. while (typeof value === 'object' && value !== null && value.$$typeof === REACT_ELEMENT_TYPE) {
  202. // If this is a nested model, there's no need to create another chunk,
  203. // we can reuse the existing one and try again.
  204. var element = value;
  205. segment.model = element;
  206. value = attemptResolveModelComponent(element);
  207. }
  208. var json = stringify(value, request.toJSON);
  209. var row;
  210. var id = segment.id;
  211. if (id === 0) {
  212. row = json + '\n';
  213. } else {
  214. row = serializeRowHeader('J', id) + json + '\n';
  215. }
  216. request.completedJSONChunks.push(convertStringToBuffer(row));
  217. } catch (x) {
  218. if (typeof x === 'object' && x !== null && typeof x.then === 'function') {
  219. // Something suspended again, let's pick it back up later.
  220. var ping = segment.ping;
  221. x.then(ping, ping);
  222. return;
  223. } else {
  224. // This errored, we need to serialize this error to the
  225. emitErrorChunk(request, segment.id, x);
  226. }
  227. }
  228. }
  229. function performWork(request) {
  230. var pingedSegments = request.pingedSegments;
  231. request.pingedSegments = [];
  232. for (var i = 0; i < pingedSegments.length; i++) {
  233. var segment = pingedSegments[i];
  234. retrySegment(request, segment);
  235. }
  236. if (request.flowing) {
  237. flushCompletedChunks(request);
  238. }
  239. }
  240. var reentrant = false;
  241. function flushCompletedChunks(request) {
  242. if (reentrant) {
  243. return;
  244. }
  245. reentrant = true;
  246. var destination = request.destination;
  247. try {
  248. var jsonChunks = request.completedJSONChunks;
  249. var i = 0;
  250. for (; i < jsonChunks.length; i++) {
  251. request.pendingChunks--;
  252. var chunk = jsonChunks[i];
  253. if (!writeChunk(destination, chunk)) {
  254. request.flowing = false;
  255. i++;
  256. break;
  257. }
  258. }
  259. jsonChunks.splice(0, i);
  260. var errorChunks = request.completedErrorChunks;
  261. i = 0;
  262. for (; i < errorChunks.length; i++) {
  263. request.pendingChunks--;
  264. var _chunk = errorChunks[i];
  265. if (!writeChunk(destination, _chunk)) {
  266. request.flowing = false;
  267. i++;
  268. break;
  269. }
  270. }
  271. errorChunks.splice(0, i);
  272. } finally {
  273. reentrant = false;
  274. }
  275. if (request.pendingChunks === 0) {
  276. // We're done.
  277. close(destination);
  278. }
  279. }
  280. function startWork(request) {
  281. request.flowing = true;
  282. scheduleWork(function () {
  283. return performWork(request);
  284. });
  285. }
  286. function startFlowing(request) {
  287. request.flowing = true;
  288. flushCompletedChunks(request);
  289. }
  290. // This file intentionally does *not* have the Flow annotation.
  291. // Don't add it. See `./inline-typed.js` for an explanation.
  292. function renderToReadableStream(model) {
  293. var request;
  294. return new ReadableStream({
  295. start: function (controller) {
  296. request = createRequest(model, controller);
  297. startWork(request);
  298. },
  299. pull: function (controller) {
  300. startFlowing(request);
  301. },
  302. cancel: function (reason) {}
  303. });
  304. }
  305. var ReactFlightDOMServerBrowser = {
  306. renderToReadableStream: renderToReadableStream
  307. };
  308. var ReactFlightDOMServerBrowser$1 = Object.freeze({
  309. default: ReactFlightDOMServerBrowser
  310. });
  311. var ReactFlightDOMServerBrowser$2 = ( ReactFlightDOMServerBrowser$1 && ReactFlightDOMServerBrowser ) || ReactFlightDOMServerBrowser$1;
  312. // TODO: decide on the top-level export form.
  313. // This is hacky but makes it work with both Rollup and Jest
  314. var unstableFlightServer_browser = ReactFlightDOMServerBrowser$2.default || ReactFlightDOMServerBrowser$2;
  315. return unstableFlightServer_browser;
  316. })));