Dashboard sipadu mbip
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

react-dom-unstable-flight-client.development.js 8.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. /** @license React v16.12.0
  2. * react-dom-unstable-flight-client.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() :
  12. typeof define === 'function' && define.amd ? define(factory) :
  13. (global.ReactFlightDOMClient = factory());
  14. }(this, (function () { 'use strict';
  15. var supportsBinaryStreams = true;
  16. function createStringDecoder() {
  17. return new TextDecoder();
  18. }
  19. var decoderOptions = {
  20. stream: true
  21. };
  22. function readPartialStringChunk(decoder, buffer) {
  23. return decoder.decode(buffer, decoderOptions);
  24. }
  25. function readFinalStringChunk(decoder, buffer) {
  26. return decoder.decode(buffer);
  27. }
  28. var PENDING = 0;
  29. var RESOLVED = 1;
  30. var ERRORED = 2;
  31. function createResponse(source) {
  32. var modelRoot = {};
  33. var rootChunk = createPendingChunk();
  34. definePendingProperty(modelRoot, 'model', rootChunk);
  35. var chunks = new Map();
  36. chunks.set(0, rootChunk);
  37. var response = {
  38. source: source,
  39. partialRow: '',
  40. modelRoot: modelRoot,
  41. chunks: chunks,
  42. fromJSON: function (key, value) {
  43. return parseFromJSON(response, this, key, value);
  44. }
  45. };
  46. if (supportsBinaryStreams) {
  47. response.stringDecoder = createStringDecoder();
  48. }
  49. return response;
  50. }
  51. function createPendingChunk() {
  52. var resolve = null;
  53. var promise = new Promise(function (r) {
  54. return resolve = r;
  55. });
  56. return {
  57. status: PENDING,
  58. value: promise,
  59. resolve: resolve
  60. };
  61. }
  62. function createErrorChunk(error) {
  63. return {
  64. status: ERRORED,
  65. value: error,
  66. resolve: null
  67. };
  68. }
  69. function triggerErrorOnChunk(chunk, error) {
  70. if (chunk.status !== PENDING) {
  71. // We already resolved. We didn't expect to see this.
  72. return;
  73. }
  74. var resolve = chunk.resolve;
  75. var erroredChunk = chunk;
  76. erroredChunk.status = ERRORED;
  77. erroredChunk.value = error;
  78. erroredChunk.resolve = null;
  79. resolve();
  80. }
  81. function createResolvedChunk(value) {
  82. return {
  83. status: RESOLVED,
  84. value: value,
  85. resolve: null
  86. };
  87. }
  88. function resolveChunk(chunk, value) {
  89. if (chunk.status !== PENDING) {
  90. // We already resolved. We didn't expect to see this.
  91. return;
  92. }
  93. var resolve = chunk.resolve;
  94. var resolvedChunk = chunk;
  95. resolvedChunk.status = RESOLVED;
  96. resolvedChunk.value = value;
  97. resolvedChunk.resolve = null;
  98. resolve();
  99. } // Report that any missing chunks in the model is now going to throw this
  100. // error upon read. Also notify any pending promises.
  101. function reportGlobalError(response, error) {
  102. response.chunks.forEach(function (chunk) {
  103. // If this chunk was already resolved or errored, it won't
  104. // trigger an error but if it wasn't then we need to
  105. // because we won't be getting any new data to resolve it.
  106. triggerErrorOnChunk(chunk, error);
  107. });
  108. }
  109. function definePendingProperty(object, key, chunk) {
  110. Object.defineProperty(object, key, {
  111. configurable: false,
  112. enumerable: true,
  113. get: function () {
  114. if (chunk.status === RESOLVED) {
  115. return chunk.value;
  116. } else {
  117. throw chunk.value;
  118. }
  119. }
  120. });
  121. }
  122. function parseFromJSON(response, targetObj, key, value) {
  123. if (typeof value === 'string' && value[0] === '$') {
  124. if (value[1] === '$') {
  125. // This was an escaped string value.
  126. return value.substring(1);
  127. } else {
  128. var id = parseInt(value.substring(1), 16);
  129. var chunks = response.chunks;
  130. var chunk = chunks.get(id);
  131. if (!chunk) {
  132. chunk = createPendingChunk();
  133. chunks.set(id, chunk);
  134. } else if (chunk.status === RESOLVED) {
  135. return chunk.value;
  136. }
  137. definePendingProperty(targetObj, key, chunk);
  138. return undefined;
  139. }
  140. }
  141. return value;
  142. }
  143. function resolveJSONRow(response, id, json) {
  144. var model = JSON.parse(json, response.fromJSON);
  145. var chunks = response.chunks;
  146. var chunk = chunks.get(id);
  147. if (!chunk) {
  148. chunks.set(id, createResolvedChunk(model));
  149. } else {
  150. resolveChunk(chunk, model);
  151. }
  152. }
  153. function processFullRow(response, row) {
  154. if (row === '') {
  155. return;
  156. }
  157. var tag = row[0];
  158. switch (tag) {
  159. case 'J':
  160. {
  161. var colon = row.indexOf(':', 1);
  162. var id = parseInt(row.substring(1, colon), 16);
  163. var json = row.substring(colon + 1);
  164. resolveJSONRow(response, id, json);
  165. return;
  166. }
  167. case 'E':
  168. {
  169. var _colon = row.indexOf(':', 1);
  170. var _id = parseInt(row.substring(1, _colon), 16);
  171. var _json = row.substring(_colon + 1);
  172. var errorInfo = JSON.parse(_json);
  173. var error = new Error(errorInfo.message);
  174. error.stack = errorInfo.stack;
  175. var chunks = response.chunks;
  176. var chunk = chunks.get(_id);
  177. if (!chunk) {
  178. chunks.set(_id, createErrorChunk(error));
  179. } else {
  180. triggerErrorOnChunk(chunk, error);
  181. }
  182. return;
  183. }
  184. default:
  185. {
  186. // Assume this is the root model.
  187. resolveJSONRow(response, 0, row);
  188. return;
  189. }
  190. }
  191. }
  192. function processStringChunk(response, chunk, offset) {
  193. var linebreak = chunk.indexOf('\n', offset);
  194. while (linebreak > -1) {
  195. var fullrow = response.partialRow + chunk.substring(offset, linebreak);
  196. processFullRow(response, fullrow);
  197. response.partialRow = '';
  198. offset = linebreak + 1;
  199. linebreak = chunk.indexOf('\n', offset);
  200. }
  201. response.partialRow += chunk.substring(offset);
  202. }
  203. function processBinaryChunk(response, chunk) {
  204. if (!supportsBinaryStreams) {
  205. throw new Error("This environment don't support binary chunks.");
  206. }
  207. var stringDecoder = response.stringDecoder;
  208. var linebreak = chunk.indexOf(10); // newline
  209. while (linebreak > -1) {
  210. var fullrow = response.partialRow + readFinalStringChunk(stringDecoder, chunk.subarray(0, linebreak));
  211. processFullRow(response, fullrow);
  212. response.partialRow = '';
  213. chunk = chunk.subarray(linebreak + 1);
  214. linebreak = chunk.indexOf(10); // newline
  215. }
  216. response.partialRow += readPartialStringChunk(stringDecoder, chunk);
  217. }
  218. function complete(response) {
  219. // In case there are any remaining unresolved chunks, they won't
  220. // be resolved now. So we need to issue an error to those.
  221. // Ideally we should be able to early bail out if we kept a
  222. // ref count of pending chunks.
  223. reportGlobalError(response, new Error('Connection closed.'));
  224. }
  225. function getModelRoot(response) {
  226. return response.modelRoot;
  227. }
  228. // This file intentionally does *not* have the Flow annotation.
  229. // Don't add it. See `./inline-typed.js` for an explanation.
  230. function startReadingFromStream(response, stream) {
  231. var reader = stream.getReader();
  232. function progress(_ref) {
  233. var done = _ref.done,
  234. value = _ref.value;
  235. if (done) {
  236. complete(response);
  237. return;
  238. }
  239. var buffer = value;
  240. processBinaryChunk(response, buffer);
  241. return reader.read().then(progress, error);
  242. }
  243. function error(e) {
  244. reportGlobalError(response, e);
  245. }
  246. reader.read().then(progress, error);
  247. }
  248. function readFromReadableStream(stream) {
  249. var response = createResponse(stream);
  250. startReadingFromStream(response, stream);
  251. return getModelRoot(response);
  252. }
  253. function readFromFetch(promiseForResponse) {
  254. var response = createResponse(promiseForResponse);
  255. promiseForResponse.then(function (r) {
  256. startReadingFromStream(response, r.body);
  257. }, function (e) {
  258. reportGlobalError(response, e);
  259. });
  260. return getModelRoot(response);
  261. }
  262. function readFromXHR(request) {
  263. var response = createResponse(request);
  264. var processedLength = 0;
  265. function progress(e) {
  266. var chunk = request.responseText;
  267. processStringChunk(response, chunk, processedLength);
  268. processedLength = chunk.length;
  269. }
  270. function load(e) {
  271. progress(e);
  272. complete(response);
  273. }
  274. function error(e) {
  275. reportGlobalError(response, new TypeError('Network error'));
  276. }
  277. request.addEventListener('progress', progress);
  278. request.addEventListener('load', load);
  279. request.addEventListener('error', error);
  280. request.addEventListener('abort', error);
  281. request.addEventListener('timeout', error);
  282. return getModelRoot(response);
  283. }
  284. var ReactFlightDOMClient = {
  285. readFromXHR: readFromXHR,
  286. readFromFetch: readFromFetch,
  287. readFromReadableStream: readFromReadableStream
  288. };
  289. var ReactFlightDOMClient$1 = Object.freeze({
  290. default: ReactFlightDOMClient
  291. });
  292. var ReactFlightDOMClient$2 = ( ReactFlightDOMClient$1 && ReactFlightDOMClient ) || ReactFlightDOMClient$1;
  293. // TODO: decide on the top-level export form.
  294. // This is hacky but makes it work with both Rollup and Jest
  295. var unstableFlightClient = ReactFlightDOMClient$2.default || ReactFlightDOMClient$2;
  296. return unstableFlightClient;
  297. })));