react-server-dom-webpack.development.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885
  1. /**
  2. * @license React
  3. * react-server-dom-webpack.development.js
  4. *
  5. * Copyright (c) Facebook, Inc. and its affiliates.
  6. *
  7. * This source code is licensed under the MIT license found in the
  8. * LICENSE file in the root directory of this source tree.
  9. */
  10. 'use strict';
  11. if (process.env.NODE_ENV !== "production") {
  12. (function() {
  13. 'use strict';
  14. var React = require('react');
  15. function createStringDecoder() {
  16. return new TextDecoder();
  17. }
  18. var decoderOptions = {
  19. stream: true
  20. };
  21. function readPartialStringChunk(decoder, buffer) {
  22. return decoder.decode(buffer, decoderOptions);
  23. }
  24. function readFinalStringChunk(decoder, buffer) {
  25. return decoder.decode(buffer);
  26. }
  27. function parseModel(response, json) {
  28. return JSON.parse(json, response._fromJSON);
  29. }
  30. // eslint-disable-next-line no-unused-vars
  31. function resolveModuleReference(bundlerConfig, moduleData) {
  32. if (bundlerConfig) {
  33. var resolvedModuleData = bundlerConfig[moduleData.id][moduleData.name];
  34. if (moduleData.async) {
  35. return {
  36. id: resolvedModuleData.id,
  37. chunks: resolvedModuleData.chunks,
  38. name: resolvedModuleData.name,
  39. async: true
  40. };
  41. } else {
  42. return resolvedModuleData;
  43. }
  44. }
  45. return moduleData;
  46. } // The chunk cache contains all the chunks we've preloaded so far.
  47. // If they're still pending they're a thenable. This map also exists
  48. // in Webpack but unfortunately it's not exposed so we have to
  49. // replicate it in user space. null means that it has already loaded.
  50. var chunkCache = new Map();
  51. var asyncModuleCache = new Map();
  52. function ignoreReject() {// We rely on rejected promises to be handled by another listener.
  53. } // Start preloading the modules since we might need them soon.
  54. // This function doesn't suspend.
  55. function preloadModule(moduleData) {
  56. var chunks = moduleData.chunks;
  57. var promises = [];
  58. for (var i = 0; i < chunks.length; i++) {
  59. var chunkId = chunks[i];
  60. var entry = chunkCache.get(chunkId);
  61. if (entry === undefined) {
  62. var thenable = globalThis.__next_chunk_load__(chunkId);
  63. promises.push(thenable);
  64. var resolve = chunkCache.set.bind(chunkCache, chunkId, null);
  65. thenable.then(resolve, ignoreReject);
  66. chunkCache.set(chunkId, thenable);
  67. } else if (entry !== null) {
  68. promises.push(entry);
  69. }
  70. }
  71. if (moduleData.async) {
  72. var existingPromise = asyncModuleCache.get(moduleData.id);
  73. if (existingPromise) {
  74. if (existingPromise.status === 'fulfilled') {
  75. return null;
  76. }
  77. return existingPromise;
  78. } else {
  79. var modulePromise = Promise.all(promises).then(function () {
  80. return globalThis.__next_require__(moduleData.id);
  81. });
  82. modulePromise.then(function (value) {
  83. var fulfilledThenable = modulePromise;
  84. fulfilledThenable.status = 'fulfilled';
  85. fulfilledThenable.value = value;
  86. }, function (reason) {
  87. var rejectedThenable = modulePromise;
  88. rejectedThenable.status = 'rejected';
  89. rejectedThenable.reason = reason;
  90. });
  91. asyncModuleCache.set(moduleData.id, modulePromise);
  92. return modulePromise;
  93. }
  94. } else if (promises.length > 0) {
  95. return Promise.all(promises);
  96. } else {
  97. return null;
  98. }
  99. } // Actually require the module or suspend if it's not yet ready.
  100. // Increase priority if necessary.
  101. function requireModule(moduleData) {
  102. var moduleExports;
  103. if (moduleData.async) {
  104. // We assume that preloadModule has been called before, which
  105. // should have added something to the module cache.
  106. var promise = asyncModuleCache.get(moduleData.id);
  107. if (promise.status === 'fulfilled') {
  108. moduleExports = promise.value;
  109. } else {
  110. throw promise.reason;
  111. }
  112. } else {
  113. moduleExports = globalThis.__next_require__(moduleData.id);
  114. }
  115. if (moduleData.name === '*') {
  116. // This is a placeholder value that represents that the caller imported this
  117. // as a CommonJS module as is.
  118. return moduleExports;
  119. }
  120. if (moduleData.name === '') {
  121. // This is a placeholder value that represents that the caller accessed the
  122. // default property of this if it was an ESM interop module.
  123. return moduleExports.__esModule ? moduleExports.default : moduleExports;
  124. }
  125. return moduleExports[moduleData.name];
  126. }
  127. // ATTENTION
  128. // When adding new symbols to this file,
  129. // Please consider also adding to 'react-devtools-shared/src/backend/ReactSymbols'
  130. // The Symbol used to tag the ReactElement-like types.
  131. var REACT_ELEMENT_TYPE = Symbol.for('react.element');
  132. var REACT_LAZY_TYPE = Symbol.for('react.lazy');
  133. var REACT_SERVER_CONTEXT_DEFAULT_VALUE_NOT_LOADED = Symbol.for('react.default_value');
  134. var ReactSharedInternals = React.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED;
  135. var ContextRegistry = ReactSharedInternals.ContextRegistry;
  136. function getOrCreateServerContext(globalName) {
  137. if (!ContextRegistry[globalName]) {
  138. ContextRegistry[globalName] = React.createServerContext(globalName, // $FlowFixMe function signature doesn't reflect the symbol value
  139. REACT_SERVER_CONTEXT_DEFAULT_VALUE_NOT_LOADED);
  140. }
  141. return ContextRegistry[globalName];
  142. }
  143. var PENDING = 'pending';
  144. var BLOCKED = 'blocked';
  145. var RESOLVED_MODEL = 'resolved_model';
  146. var RESOLVED_MODULE = 'resolved_module';
  147. var INITIALIZED = 'fulfilled';
  148. var ERRORED = 'rejected';
  149. function Chunk(status, value, reason, response) {
  150. this.status = status;
  151. this.value = value;
  152. this.reason = reason;
  153. this._response = response;
  154. } // We subclass Promise.prototype so that we get other methods like .catch
  155. Chunk.prototype = Object.create(Promise.prototype); // TODO: This doesn't return a new Promise chain unlike the real .then
  156. Chunk.prototype.then = function (resolve, reject) {
  157. var chunk = this; // If we have resolved content, we try to initialize it first which
  158. // might put us back into one of the other states.
  159. switch (chunk.status) {
  160. case RESOLVED_MODEL:
  161. initializeModelChunk(chunk);
  162. break;
  163. case RESOLVED_MODULE:
  164. initializeModuleChunk(chunk);
  165. break;
  166. } // The status might have changed after initialization.
  167. switch (chunk.status) {
  168. case INITIALIZED:
  169. resolve(chunk.value);
  170. break;
  171. case PENDING:
  172. case BLOCKED:
  173. if (resolve) {
  174. if (chunk.value === null) {
  175. chunk.value = [];
  176. }
  177. chunk.value.push(resolve);
  178. }
  179. if (reject) {
  180. if (chunk.reason === null) {
  181. chunk.reason = [];
  182. }
  183. chunk.reason.push(reject);
  184. }
  185. break;
  186. default:
  187. reject(chunk.reason);
  188. break;
  189. }
  190. };
  191. function readChunk(chunk) {
  192. // If we have resolved content, we try to initialize it first which
  193. // might put us back into one of the other states.
  194. switch (chunk.status) {
  195. case RESOLVED_MODEL:
  196. initializeModelChunk(chunk);
  197. break;
  198. case RESOLVED_MODULE:
  199. initializeModuleChunk(chunk);
  200. break;
  201. } // The status might have changed after initialization.
  202. switch (chunk.status) {
  203. case INITIALIZED:
  204. return chunk.value;
  205. case PENDING:
  206. case BLOCKED:
  207. // eslint-disable-next-line no-throw-literal
  208. throw chunk;
  209. default:
  210. throw chunk.reason;
  211. }
  212. }
  213. function getRoot(response) {
  214. var chunk = getChunk(response, 0);
  215. return chunk;
  216. }
  217. function createPendingChunk(response) {
  218. // $FlowFixMe Flow doesn't support functions as constructors
  219. return new Chunk(PENDING, null, null, response);
  220. }
  221. function createBlockedChunk(response) {
  222. // $FlowFixMe Flow doesn't support functions as constructors
  223. return new Chunk(BLOCKED, null, null, response);
  224. }
  225. function createErrorChunk(response, error) {
  226. // $FlowFixMe Flow doesn't support functions as constructors
  227. return new Chunk(ERRORED, null, error, response);
  228. }
  229. function createInitializedChunk(response, value) {
  230. // $FlowFixMe Flow doesn't support functions as constructors
  231. return new Chunk(INITIALIZED, value, null, response);
  232. }
  233. function wakeChunk(listeners, value) {
  234. for (var i = 0; i < listeners.length; i++) {
  235. var listener = listeners[i];
  236. listener(value);
  237. }
  238. }
  239. function wakeChunkIfInitialized(chunk, resolveListeners, rejectListeners) {
  240. switch (chunk.status) {
  241. case INITIALIZED:
  242. wakeChunk(resolveListeners, chunk.value);
  243. break;
  244. case PENDING:
  245. case BLOCKED:
  246. chunk.value = resolveListeners;
  247. chunk.reason = rejectListeners;
  248. break;
  249. case ERRORED:
  250. if (rejectListeners) {
  251. wakeChunk(rejectListeners, chunk.reason);
  252. }
  253. break;
  254. }
  255. }
  256. function triggerErrorOnChunk(chunk, error) {
  257. if (chunk.status !== PENDING && chunk.status !== BLOCKED) {
  258. // We already resolved. We didn't expect to see this.
  259. return;
  260. }
  261. var listeners = chunk.reason;
  262. var erroredChunk = chunk;
  263. erroredChunk.status = ERRORED;
  264. erroredChunk.reason = error;
  265. if (listeners !== null) {
  266. wakeChunk(listeners, error);
  267. }
  268. }
  269. function createResolvedModelChunk(response, value) {
  270. // $FlowFixMe Flow doesn't support functions as constructors
  271. return new Chunk(RESOLVED_MODEL, value, null, response);
  272. }
  273. function createResolvedModuleChunk(response, value) {
  274. // $FlowFixMe Flow doesn't support functions as constructors
  275. return new Chunk(RESOLVED_MODULE, value, null, response);
  276. }
  277. function resolveModelChunk(chunk, value) {
  278. if (chunk.status !== PENDING) {
  279. // We already resolved. We didn't expect to see this.
  280. return;
  281. }
  282. var resolveListeners = chunk.value;
  283. var rejectListeners = chunk.reason;
  284. var resolvedChunk = chunk;
  285. resolvedChunk.status = RESOLVED_MODEL;
  286. resolvedChunk.value = value;
  287. if (resolveListeners !== null) {
  288. // This is unfortunate that we're reading this eagerly if
  289. // we already have listeners attached since they might no
  290. // longer be rendered or might not be the highest pri.
  291. initializeModelChunk(resolvedChunk); // The status might have changed after initialization.
  292. wakeChunkIfInitialized(chunk, resolveListeners, rejectListeners);
  293. }
  294. }
  295. function resolveModuleChunk(chunk, value) {
  296. if (chunk.status !== PENDING && chunk.status !== BLOCKED) {
  297. // We already resolved. We didn't expect to see this.
  298. return;
  299. }
  300. var resolveListeners = chunk.value;
  301. var rejectListeners = chunk.reason;
  302. var resolvedChunk = chunk;
  303. resolvedChunk.status = RESOLVED_MODULE;
  304. resolvedChunk.value = value;
  305. if (resolveListeners !== null) {
  306. initializeModuleChunk(resolvedChunk);
  307. wakeChunkIfInitialized(chunk, resolveListeners, rejectListeners);
  308. }
  309. }
  310. var initializingChunk = null;
  311. var initializingChunkBlockedModel = null;
  312. function initializeModelChunk(chunk) {
  313. var prevChunk = initializingChunk;
  314. var prevBlocked = initializingChunkBlockedModel;
  315. initializingChunk = chunk;
  316. initializingChunkBlockedModel = null;
  317. try {
  318. var _value = parseModel(chunk._response, chunk.value);
  319. if (initializingChunkBlockedModel !== null && initializingChunkBlockedModel.deps > 0) {
  320. initializingChunkBlockedModel.value = _value; // We discovered new dependencies on modules that are not yet resolved.
  321. // We have to go the BLOCKED state until they're resolved.
  322. var blockedChunk = chunk;
  323. blockedChunk.status = BLOCKED;
  324. blockedChunk.value = null;
  325. blockedChunk.reason = null;
  326. } else {
  327. var initializedChunk = chunk;
  328. initializedChunk.status = INITIALIZED;
  329. initializedChunk.value = _value;
  330. }
  331. } catch (error) {
  332. var erroredChunk = chunk;
  333. erroredChunk.status = ERRORED;
  334. erroredChunk.reason = error;
  335. } finally {
  336. initializingChunk = prevChunk;
  337. initializingChunkBlockedModel = prevBlocked;
  338. }
  339. }
  340. function initializeModuleChunk(chunk) {
  341. try {
  342. var _value2 = requireModule(chunk.value);
  343. var initializedChunk = chunk;
  344. initializedChunk.status = INITIALIZED;
  345. initializedChunk.value = _value2;
  346. } catch (error) {
  347. var erroredChunk = chunk;
  348. erroredChunk.status = ERRORED;
  349. erroredChunk.reason = error;
  350. }
  351. } // Report that any missing chunks in the model is now going to throw this
  352. // error upon read. Also notify any pending promises.
  353. function reportGlobalError(response, error) {
  354. response._chunks.forEach(function (chunk) {
  355. // If this chunk was already resolved or errored, it won't
  356. // trigger an error but if it wasn't then we need to
  357. // because we won't be getting any new data to resolve it.
  358. if (chunk.status === PENDING) {
  359. triggerErrorOnChunk(chunk, error);
  360. }
  361. });
  362. }
  363. function createElement(type, key, props) {
  364. var element = {
  365. // This tag allows us to uniquely identify this as a React Element
  366. $$typeof: REACT_ELEMENT_TYPE,
  367. // Built-in properties that belong on the element
  368. type: type,
  369. key: key,
  370. ref: null,
  371. props: props,
  372. // Record the component responsible for creating this element.
  373. _owner: null
  374. };
  375. {
  376. // We don't really need to add any of these but keeping them for good measure.
  377. // Unfortunately, _store is enumerable in jest matchers so for equality to
  378. // work, I need to keep it or make _store non-enumerable in the other file.
  379. element._store = {};
  380. Object.defineProperty(element._store, 'validated', {
  381. configurable: false,
  382. enumerable: false,
  383. writable: true,
  384. value: true // This element has already been validated on the server.
  385. });
  386. Object.defineProperty(element, '_self', {
  387. configurable: false,
  388. enumerable: false,
  389. writable: false,
  390. value: null
  391. });
  392. Object.defineProperty(element, '_source', {
  393. configurable: false,
  394. enumerable: false,
  395. writable: false,
  396. value: null
  397. });
  398. }
  399. return element;
  400. }
  401. function createLazyChunkWrapper(chunk) {
  402. var lazyType = {
  403. $$typeof: REACT_LAZY_TYPE,
  404. _payload: chunk,
  405. _init: readChunk
  406. };
  407. return lazyType;
  408. }
  409. function getChunk(response, id) {
  410. var chunks = response._chunks;
  411. var chunk = chunks.get(id);
  412. if (!chunk) {
  413. chunk = createPendingChunk(response);
  414. chunks.set(id, chunk);
  415. }
  416. return chunk;
  417. }
  418. function createModelResolver(chunk, parentObject, key) {
  419. var blocked;
  420. if (initializingChunkBlockedModel) {
  421. blocked = initializingChunkBlockedModel;
  422. blocked.deps++;
  423. } else {
  424. blocked = initializingChunkBlockedModel = {
  425. deps: 1,
  426. value: null
  427. };
  428. }
  429. return function (value) {
  430. parentObject[key] = value;
  431. blocked.deps--;
  432. if (blocked.deps === 0) {
  433. if (chunk.status !== BLOCKED) {
  434. return;
  435. }
  436. var resolveListeners = chunk.value;
  437. var initializedChunk = chunk;
  438. initializedChunk.status = INITIALIZED;
  439. initializedChunk.value = blocked.value;
  440. if (resolveListeners !== null) {
  441. wakeChunk(resolveListeners, blocked.value);
  442. }
  443. }
  444. };
  445. }
  446. function createModelReject(chunk) {
  447. return function (error) {
  448. return triggerErrorOnChunk(chunk, error);
  449. };
  450. }
  451. function parseModelString(response, parentObject, key, value) {
  452. switch (value[0]) {
  453. case '$':
  454. {
  455. if (value === '$') {
  456. return REACT_ELEMENT_TYPE;
  457. } else if (value[1] === '$' || value[1] === '@') {
  458. // This was an escaped string value.
  459. return value.substring(1);
  460. } else {
  461. var id = parseInt(value.substring(1), 16);
  462. var chunk = getChunk(response, id);
  463. switch (chunk.status) {
  464. case RESOLVED_MODEL:
  465. initializeModelChunk(chunk);
  466. break;
  467. case RESOLVED_MODULE:
  468. initializeModuleChunk(chunk);
  469. break;
  470. } // The status might have changed after initialization.
  471. switch (chunk.status) {
  472. case INITIALIZED:
  473. return chunk.value;
  474. case PENDING:
  475. case BLOCKED:
  476. var parentChunk = initializingChunk;
  477. chunk.then(createModelResolver(parentChunk, parentObject, key), createModelReject(parentChunk));
  478. return null;
  479. default:
  480. throw chunk.reason;
  481. }
  482. }
  483. }
  484. case '@':
  485. {
  486. var _id = parseInt(value.substring(1), 16);
  487. var _chunk = getChunk(response, _id); // We create a React.lazy wrapper around any lazy values.
  488. // When passed into React, we'll know how to suspend on this.
  489. return createLazyChunkWrapper(_chunk);
  490. }
  491. }
  492. return value;
  493. }
  494. function parseModelTuple(response, value) {
  495. var tuple = value;
  496. if (tuple[0] === REACT_ELEMENT_TYPE) {
  497. // TODO: Consider having React just directly accept these arrays as elements.
  498. // Or even change the ReactElement type to be an array.
  499. return createElement(tuple[1], tuple[2], tuple[3]);
  500. }
  501. return value;
  502. }
  503. function createResponse(bundlerConfig) {
  504. var chunks = new Map();
  505. var response = {
  506. _bundlerConfig: bundlerConfig,
  507. _chunks: chunks
  508. };
  509. return response;
  510. }
  511. function resolveModel(response, id, model) {
  512. var chunks = response._chunks;
  513. var chunk = chunks.get(id);
  514. if (!chunk) {
  515. chunks.set(id, createResolvedModelChunk(response, model));
  516. } else {
  517. resolveModelChunk(chunk, model);
  518. }
  519. }
  520. function resolveProvider(response, id, contextName) {
  521. var chunks = response._chunks;
  522. chunks.set(id, createInitializedChunk(response, getOrCreateServerContext(contextName).Provider));
  523. }
  524. function resolveModule(response, id, model) {
  525. var chunks = response._chunks;
  526. var chunk = chunks.get(id);
  527. var moduleMetaData = parseModel(response, model);
  528. var moduleReference = resolveModuleReference(response._bundlerConfig, moduleMetaData); // TODO: Add an option to encode modules that are lazy loaded.
  529. // For now we preload all modules as early as possible since it's likely
  530. // that we'll need them.
  531. var promise = preloadModule(moduleReference);
  532. if (promise) {
  533. var blockedChunk;
  534. if (!chunk) {
  535. // Technically, we should just treat promise as the chunk in this
  536. // case. Because it'll just behave as any other promise.
  537. blockedChunk = createBlockedChunk(response);
  538. chunks.set(id, blockedChunk);
  539. } else {
  540. // This can't actually happen because we don't have any forward
  541. // references to modules.
  542. blockedChunk = chunk;
  543. blockedChunk.status = BLOCKED;
  544. }
  545. promise.then(function () {
  546. return resolveModuleChunk(blockedChunk, moduleReference);
  547. }, function (error) {
  548. return triggerErrorOnChunk(blockedChunk, error);
  549. });
  550. } else {
  551. if (!chunk) {
  552. chunks.set(id, createResolvedModuleChunk(response, moduleReference));
  553. } else {
  554. // This can't actually happen because we don't have any forward
  555. // references to modules.
  556. resolveModuleChunk(chunk, moduleReference);
  557. }
  558. }
  559. }
  560. function resolveSymbol(response, id, name) {
  561. var chunks = response._chunks; // We assume that we'll always emit the symbol before anything references it
  562. // to save a few bytes.
  563. chunks.set(id, createInitializedChunk(response, Symbol.for(name)));
  564. }
  565. function resolveError(response, id, message, stack) {
  566. // eslint-disable-next-line react-internal/prod-error-codes
  567. var error = new Error(message);
  568. error.stack = stack;
  569. var chunks = response._chunks;
  570. var chunk = chunks.get(id);
  571. if (!chunk) {
  572. chunks.set(id, createErrorChunk(response, error));
  573. } else {
  574. triggerErrorOnChunk(chunk, error);
  575. }
  576. }
  577. function close(response) {
  578. // In case there are any remaining unresolved chunks, they won't
  579. // be resolved now. So we need to issue an error to those.
  580. // Ideally we should be able to early bail out if we kept a
  581. // ref count of pending chunks.
  582. reportGlobalError(response, new Error('Connection closed.'));
  583. }
  584. function processFullRow(response, row) {
  585. if (row === '') {
  586. return;
  587. }
  588. var tag = row[0]; // When tags that are not text are added, check them here before
  589. // parsing the row as text.
  590. // switch (tag) {
  591. // }
  592. var colon = row.indexOf(':', 1);
  593. var id = parseInt(row.substring(1, colon), 16);
  594. var text = row.substring(colon + 1);
  595. switch (tag) {
  596. case 'J':
  597. {
  598. resolveModel(response, id, text);
  599. return;
  600. }
  601. case 'M':
  602. {
  603. resolveModule(response, id, text);
  604. return;
  605. }
  606. case 'P':
  607. {
  608. resolveProvider(response, id, text);
  609. return;
  610. }
  611. case 'S':
  612. {
  613. resolveSymbol(response, id, JSON.parse(text));
  614. return;
  615. }
  616. case 'E':
  617. {
  618. var errorInfo = JSON.parse(text);
  619. resolveError(response, id, errorInfo.message, errorInfo.stack);
  620. return;
  621. }
  622. default:
  623. {
  624. throw new Error("Error parsing the data. It's probably an error code or network corruption.");
  625. }
  626. }
  627. }
  628. function processStringChunk(response, chunk, offset) {
  629. var linebreak = chunk.indexOf('\n', offset);
  630. while (linebreak > -1) {
  631. var fullrow = response._partialRow + chunk.substring(offset, linebreak);
  632. processFullRow(response, fullrow);
  633. response._partialRow = '';
  634. offset = linebreak + 1;
  635. linebreak = chunk.indexOf('\n', offset);
  636. }
  637. response._partialRow += chunk.substring(offset);
  638. }
  639. function processBinaryChunk(response, chunk) {
  640. var stringDecoder = response._stringDecoder;
  641. var linebreak = chunk.indexOf(10); // newline
  642. while (linebreak > -1) {
  643. var fullrow = response._partialRow + readFinalStringChunk(stringDecoder, chunk.subarray(0, linebreak));
  644. processFullRow(response, fullrow);
  645. response._partialRow = '';
  646. chunk = chunk.subarray(linebreak + 1);
  647. linebreak = chunk.indexOf(10); // newline
  648. }
  649. response._partialRow += readPartialStringChunk(stringDecoder, chunk);
  650. }
  651. function createFromJSONCallback(response) {
  652. return function (key, value) {
  653. if (typeof value === 'string') {
  654. // We can't use .bind here because we need the "this" value.
  655. return parseModelString(response, this, key, value);
  656. }
  657. if (typeof value === 'object' && value !== null) {
  658. return parseModelTuple(response, value);
  659. }
  660. return value;
  661. };
  662. }
  663. function createResponse$1(bundlerConfig) {
  664. // NOTE: CHECK THE COMPILER OUTPUT EACH TIME YOU CHANGE THIS.
  665. // It should be inlined to one object literal but minor changes can break it.
  666. var stringDecoder = createStringDecoder() ;
  667. var response = createResponse(bundlerConfig);
  668. response._partialRow = '';
  669. {
  670. response._stringDecoder = stringDecoder;
  671. } // Don't inline this call because it causes closure to outline the call above.
  672. response._fromJSON = createFromJSONCallback(response);
  673. return response;
  674. }
  675. function startReadingFromStream(response, stream) {
  676. var reader = stream.getReader();
  677. function progress(_ref) {
  678. var done = _ref.done,
  679. value = _ref.value;
  680. if (done) {
  681. close(response);
  682. return;
  683. }
  684. var buffer = value;
  685. processBinaryChunk(response, buffer);
  686. return reader.read().then(progress).catch(error);
  687. }
  688. function error(e) {
  689. reportGlobalError(response, e);
  690. }
  691. reader.read().then(progress).catch(error);
  692. }
  693. function createFromReadableStream(stream, options) {
  694. var response = createResponse$1(options && options.moduleMap ? options.moduleMap : null);
  695. startReadingFromStream(response, stream);
  696. return getRoot(response);
  697. }
  698. function createFromFetch(promiseForResponse, options) {
  699. var response = createResponse$1(options && options.moduleMap ? options.moduleMap : null);
  700. promiseForResponse.then(function (r) {
  701. startReadingFromStream(response, r.body);
  702. }, function (e) {
  703. reportGlobalError(response, e);
  704. });
  705. return getRoot(response);
  706. }
  707. function createFromXHR(request, options) {
  708. var response = createResponse$1(options && options.moduleMap ? options.moduleMap : null);
  709. var processedLength = 0;
  710. function progress(e) {
  711. var chunk = request.responseText;
  712. processStringChunk(response, chunk, processedLength);
  713. processedLength = chunk.length;
  714. }
  715. function load(e) {
  716. progress();
  717. close(response);
  718. }
  719. function error(e) {
  720. reportGlobalError(response, new TypeError('Network error'));
  721. }
  722. request.addEventListener('progress', progress);
  723. request.addEventListener('load', load);
  724. request.addEventListener('error', error);
  725. request.addEventListener('abort', error);
  726. request.addEventListener('timeout', error);
  727. return getRoot(response);
  728. }
  729. exports.createFromFetch = createFromFetch;
  730. exports.createFromReadableStream = createFromReadableStream;
  731. exports.createFromXHR = createFromXHR;
  732. })();
  733. }