react-server-dom-webpack 18.3.0-next-5dd90c562-20230502 → 19.0.0-canary-2b036d3f1-20240327

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (42) hide show
  1. package/cjs/react-server-dom-webpack-client.browser.development.js +1768 -1188
  2. package/cjs/react-server-dom-webpack-client.browser.production.js +1739 -0
  3. package/cjs/react-server-dom-webpack-client.browser.production.min.js +40 -34
  4. package/cjs/react-server-dom-webpack-client.browser.production.min.js.map +1 -0
  5. package/cjs/react-server-dom-webpack-client.edge.development.js +1755 -221
  6. package/cjs/react-server-dom-webpack-client.edge.production.js +1986 -0
  7. package/cjs/react-server-dom-webpack-client.edge.production.min.js +45 -28
  8. package/cjs/react-server-dom-webpack-client.edge.production.min.js.map +1 -0
  9. package/cjs/react-server-dom-webpack-client.node.development.js +1743 -239
  10. package/cjs/react-server-dom-webpack-client.node.production.js +1942 -0
  11. package/cjs/react-server-dom-webpack-client.node.production.min.js +44 -28
  12. package/cjs/react-server-dom-webpack-client.node.production.min.js.map +1 -0
  13. package/cjs/react-server-dom-webpack-client.node.unbundled.development.js +1702 -194
  14. package/cjs/react-server-dom-webpack-client.node.unbundled.production.js +1895 -0
  15. package/cjs/react-server-dom-webpack-client.node.unbundled.production.min.js +43 -26
  16. package/cjs/react-server-dom-webpack-client.node.unbundled.production.min.js.map +1 -0
  17. package/cjs/react-server-dom-webpack-node-register.js +12 -18
  18. package/cjs/react-server-dom-webpack-node-register.js.map +1 -0
  19. package/cjs/react-server-dom-webpack-plugin.js +22 -19
  20. package/cjs/react-server-dom-webpack-plugin.js.map +1 -0
  21. package/cjs/react-server-dom-webpack-server.browser.development.js +1588 -808
  22. package/cjs/react-server-dom-webpack-server.browser.production.js +3257 -0
  23. package/cjs/react-server-dom-webpack-server.browser.production.min.js +80 -62
  24. package/cjs/react-server-dom-webpack-server.browser.production.min.js.map +1 -0
  25. package/cjs/react-server-dom-webpack-server.edge.development.js +1583 -811
  26. package/cjs/react-server-dom-webpack-server.edge.production.js +3261 -0
  27. package/cjs/react-server-dom-webpack-server.edge.production.min.js +82 -62
  28. package/cjs/react-server-dom-webpack-server.edge.production.min.js.map +1 -0
  29. package/cjs/react-server-dom-webpack-server.node.development.js +1575 -805
  30. package/cjs/react-server-dom-webpack-server.node.production.js +3464 -0
  31. package/cjs/react-server-dom-webpack-server.node.production.min.js +85 -67
  32. package/cjs/react-server-dom-webpack-server.node.production.min.js.map +1 -0
  33. package/cjs/react-server-dom-webpack-server.node.unbundled.development.js +1526 -761
  34. package/cjs/react-server-dom-webpack-server.node.unbundled.production.js +3391 -0
  35. package/cjs/react-server-dom-webpack-server.node.unbundled.production.min.js +82 -65
  36. package/cjs/react-server-dom-webpack-server.node.unbundled.production.min.js.map +1 -0
  37. package/esm/react-server-dom-webpack-node-loader.production.min.js +19 -12
  38. package/package.json +7 -13
  39. package/umd/react-server-dom-webpack-client.browser.development.js +1770 -1189
  40. package/umd/react-server-dom-webpack-client.browser.production.min.js +26 -20
  41. package/umd/react-server-dom-webpack-server.browser.development.js +1589 -808
  42. package/umd/react-server-dom-webpack-server.browser.production.min.js +55 -42
@@ -17,6 +17,9 @@ if (process.env.NODE_ENV !== "production") {
17
17
  var ReactDOM = require('react-dom');
18
18
  var React = require('react');
19
19
 
20
+ // -----------------------------------------------------------------------------
21
+ var enableBinaryFlight = false;
22
+
20
23
  function createStringDecoder() {
21
24
  return new TextDecoder();
22
25
  }
@@ -30,15 +33,66 @@ function readFinalStringChunk(decoder, buffer) {
30
33
  return decoder.decode(buffer);
31
34
  }
32
35
 
33
- function parseModel(response, json) {
34
- return JSON.parse(json, response._fromJSON);
36
+ var badgeFormat = '%c%s%c '; // Same badge styling as DevTools.
37
+
38
+ var badgeStyle = // We use a fixed background if light-dark is not supported, otherwise
39
+ // we use a transparent background.
40
+ 'background: #e6e6e6;' + 'background: light-dark(rgba(0,0,0,0.1), rgba(255,255,255,0.25));' + 'color: #000000;' + 'color: light-dark(#000000, #ffffff);' + 'border-radius: 2px';
41
+ var resetStyle = '';
42
+ var pad = ' ';
43
+ function printToConsole(methodName, args, badgeName) {
44
+ var offset = 0;
45
+
46
+ switch (methodName) {
47
+ case 'dir':
48
+ case 'dirxml':
49
+ case 'groupEnd':
50
+ case 'table':
51
+ {
52
+ // These methods cannot be colorized because they don't take a formatting string.
53
+ // eslint-disable-next-line react-internal/no-production-logging
54
+ console[methodName].apply(console, args);
55
+ return;
56
+ }
57
+
58
+ case 'assert':
59
+ {
60
+ // assert takes formatting options as the second argument.
61
+ offset = 1;
62
+ }
63
+ }
64
+
65
+ var newArgs = args.slice(0);
66
+
67
+ if (typeof newArgs[offset] === 'string') {
68
+ newArgs.splice(offset, 1, badgeFormat + newArgs[offset], badgeStyle, pad + badgeName + pad, resetStyle);
69
+ } else {
70
+ newArgs.splice(offset, 0, badgeFormat, badgeStyle, pad + badgeName + pad, resetStyle);
71
+ } // eslint-disable-next-line react-internal/no-production-logging
72
+
73
+
74
+ console[methodName].apply(console, newArgs);
75
+ return;
76
+ }
77
+
78
+ // This is the parsed shape of the wire format which is why it is
79
+ // condensed to only the essentialy information
80
+ var ID = 0;
81
+ var CHUNKS = 1;
82
+ var NAME = 2; // export const ASYNC = 3;
83
+ // This logic is correct because currently only include the 4th tuple member
84
+ // when the module is async. If that changes we will need to actually assert
85
+ // the value is true. We don't index into the 4th slot because flow does not
86
+ // like the potential out of bounds access
87
+
88
+ function isAsyncImport(metadata) {
89
+ return metadata.length === 4;
35
90
  }
36
91
 
37
- // eslint-disable-next-line no-unused-vars
38
92
  function resolveClientReference(bundlerConfig, metadata) {
39
93
  if (bundlerConfig) {
40
- var moduleExports = bundlerConfig[metadata.id];
41
- var resolvedModuleData = moduleExports[metadata.name];
94
+ var moduleExports = bundlerConfig[metadata[ID]];
95
+ var resolvedModuleData = moduleExports[metadata[NAME]];
42
96
  var name;
43
97
 
44
98
  if (resolvedModuleData) {
@@ -49,18 +103,19 @@ function resolveClientReference(bundlerConfig, metadata) {
49
103
  resolvedModuleData = moduleExports['*'];
50
104
 
51
105
  if (!resolvedModuleData) {
52
- throw new Error('Could not find the module "' + metadata.id + '" in the React SSR Manifest. ' + 'This is probably a bug in the React Server Components bundler.');
106
+ throw new Error('Could not find the module "' + metadata[ID] + '" in the React SSR Manifest. ' + 'This is probably a bug in the React Server Components bundler.');
53
107
  }
54
108
 
55
- name = metadata.name;
109
+ name = metadata[NAME];
56
110
  }
57
111
 
58
- return {
59
- id: resolvedModuleData.id,
60
- chunks: resolvedModuleData.chunks,
61
- name: name,
62
- async: !!metadata.async
63
- };
112
+ if (isAsyncImport(metadata)) {
113
+ return [resolvedModuleData.id, resolvedModuleData.chunks, name, 1
114
+ /* async */
115
+ ];
116
+ } else {
117
+ return [resolvedModuleData.id, resolvedModuleData.chunks, name];
118
+ }
64
119
  }
65
120
 
66
121
  return metadata;
@@ -70,7 +125,31 @@ function resolveClientReference(bundlerConfig, metadata) {
70
125
  // replicate it in user space. null means that it has already loaded.
71
126
 
72
127
  var chunkCache = new Map();
73
- var asyncModuleCache = new Map();
128
+
129
+ function requireAsyncModule(id) {
130
+ // We've already loaded all the chunks. We can require the module.
131
+ var promise = __webpack_require__(id);
132
+
133
+ if (typeof promise.then !== 'function') {
134
+ // This wasn't a promise after all.
135
+ return null;
136
+ } else if (promise.status === 'fulfilled') {
137
+ // This module was already resolved earlier.
138
+ return null;
139
+ } else {
140
+ // Instrument the Promise to stash the result.
141
+ promise.then(function (value) {
142
+ var fulfilledThenable = promise;
143
+ fulfilledThenable.status = 'fulfilled';
144
+ fulfilledThenable.value = value;
145
+ }, function (reason) {
146
+ var rejectedThenable = promise;
147
+ rejectedThenable.status = 'rejected';
148
+ rejectedThenable.reason = reason;
149
+ });
150
+ return promise;
151
+ }
152
+ }
74
153
 
75
154
  function ignoreReject() {// We rely on rejected promises to be handled by another listener.
76
155
  } // Start preloading the modules since we might need them soon.
@@ -78,16 +157,17 @@ function ignoreReject() {// We rely on rejected promises to be handled by anothe
78
157
 
79
158
 
80
159
  function preloadModule(metadata) {
81
- var chunks = metadata.chunks;
160
+ var chunks = metadata[CHUNKS];
82
161
  var promises = [];
162
+ var i = 0;
83
163
 
84
- for (var i = 0; i < chunks.length; i++) {
85
- var chunkId = chunks[i];
164
+ while (i < chunks.length) {
165
+ var chunkId = chunks[i++];
166
+ var chunkFilename = chunks[i++];
86
167
  var entry = chunkCache.get(chunkId);
87
168
 
88
169
  if (entry === undefined) {
89
- var thenable = __webpack_chunk_load__(chunkId);
90
-
170
+ var thenable = loadChunk(chunkId, chunkFilename);
91
171
  promises.push(thenable); // $FlowFixMe[method-unbinding]
92
172
 
93
173
  var resolve = chunkCache.set.bind(chunkCache, chunkId, null);
@@ -98,30 +178,13 @@ function preloadModule(metadata) {
98
178
  }
99
179
  }
100
180
 
101
- if (metadata.async) {
102
- var existingPromise = asyncModuleCache.get(metadata.id);
103
-
104
- if (existingPromise) {
105
- if (existingPromise.status === 'fulfilled') {
106
- return null;
107
- }
108
-
109
- return existingPromise;
181
+ if (isAsyncImport(metadata)) {
182
+ if (promises.length === 0) {
183
+ return requireAsyncModule(metadata[ID]);
110
184
  } else {
111
- var modulePromise = Promise.all(promises).then(function () {
112
- return __webpack_require__(metadata.id);
113
- });
114
- modulePromise.then(function (value) {
115
- var fulfilledThenable = modulePromise;
116
- fulfilledThenable.status = 'fulfilled';
117
- fulfilledThenable.value = value;
118
- }, function (reason) {
119
- var rejectedThenable = modulePromise;
120
- rejectedThenable.status = 'rejected';
121
- rejectedThenable.reason = reason;
185
+ return Promise.all(promises).then(function () {
186
+ return requireAsyncModule(metadata[ID]);
122
187
  });
123
- asyncModuleCache.set(metadata.id, modulePromise);
124
- return modulePromise;
125
188
  }
126
189
  } else if (promises.length > 0) {
127
190
  return Promise.all(promises);
@@ -132,101 +195,215 @@ function preloadModule(metadata) {
132
195
  // Increase priority if necessary.
133
196
 
134
197
  function requireModule(metadata) {
135
- var moduleExports;
198
+ var moduleExports = __webpack_require__(metadata[ID]);
136
199
 
137
- if (metadata.async) {
138
- // We assume that preloadModule has been called before, which
139
- // should have added something to the module cache.
140
- var promise = asyncModuleCache.get(metadata.id);
141
-
142
- if (promise.status === 'fulfilled') {
143
- moduleExports = promise.value;
200
+ if (isAsyncImport(metadata)) {
201
+ if (typeof moduleExports.then !== 'function') ; else if (moduleExports.status === 'fulfilled') {
202
+ // This Promise should've been instrumented by preloadModule.
203
+ moduleExports = moduleExports.value;
144
204
  } else {
145
- throw promise.reason;
205
+ throw moduleExports.reason;
146
206
  }
147
- } else {
148
- moduleExports = __webpack_require__(metadata.id);
149
207
  }
150
208
 
151
- if (metadata.name === '*') {
209
+ if (metadata[NAME] === '*') {
152
210
  // This is a placeholder value that represents that the caller imported this
153
211
  // as a CommonJS module as is.
154
212
  return moduleExports;
155
213
  }
156
214
 
157
- if (metadata.name === '') {
215
+ if (metadata[NAME] === '') {
158
216
  // This is a placeholder value that represents that the caller accessed the
159
217
  // default property of this if it was an ESM interop module.
160
218
  return moduleExports.__esModule ? moduleExports.default : moduleExports;
161
219
  }
162
220
 
163
- return moduleExports[metadata.name];
221
+ return moduleExports[metadata[NAME]];
222
+ }
223
+
224
+ var chunkMap = new Map();
225
+ /**
226
+ * We patch the chunk filename function in webpack to insert our own resolution
227
+ * of chunks that come from Flight and may not be known to the webpack runtime
228
+ */
229
+
230
+ var webpackGetChunkFilename = __webpack_require__.u;
231
+
232
+ __webpack_require__.u = function (chunkId) {
233
+ var flightChunk = chunkMap.get(chunkId);
234
+
235
+ if (flightChunk !== undefined) {
236
+ return flightChunk;
237
+ }
238
+
239
+ return webpackGetChunkFilename(chunkId);
240
+ };
241
+
242
+ function loadChunk(chunkId, filename) {
243
+ chunkMap.set(chunkId, filename);
244
+ return __webpack_chunk_load__(chunkId);
164
245
  }
165
246
 
166
247
  var ReactDOMSharedInternals = ReactDOM.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED;
167
248
 
168
249
  // This client file is in the shared folder because it applies to both SSR and browser contexts.
169
- var ReactDOMCurrentDispatcher = ReactDOMSharedInternals.Dispatcher;
250
+ var ReactDOMCurrentDispatcher = ReactDOMSharedInternals.ReactDOMCurrentDispatcher;
170
251
  function dispatchHint(code, model) {
171
252
  var dispatcher = ReactDOMCurrentDispatcher.current;
172
253
 
173
- if (dispatcher) {
174
- var href, options;
254
+ switch (code) {
255
+ case 'D':
256
+ {
257
+ var refined = refineModel(code, model);
258
+ var href = refined;
259
+ dispatcher.prefetchDNS(href);
260
+ return;
261
+ }
175
262
 
176
- if (typeof model === 'string') {
177
- href = model;
178
- } else {
179
- href = model[0];
180
- options = model[1];
181
- }
263
+ case 'C':
264
+ {
265
+ var _refined = refineModel(code, model);
182
266
 
183
- switch (code) {
184
- case 'D':
185
- {
186
- // $FlowFixMe[prop-missing] options are not refined to their types by code
187
- dispatcher.prefetchDNS(href, options);
188
- return;
267
+ if (typeof _refined === 'string') {
268
+ var _href = _refined;
269
+ dispatcher.preconnect(_href);
270
+ } else {
271
+ var _href2 = _refined[0];
272
+ var crossOrigin = _refined[1];
273
+ dispatcher.preconnect(_href2, crossOrigin);
189
274
  }
190
275
 
191
- case 'C':
192
- {
193
- // $FlowFixMe[prop-missing] options are not refined to their types by code
194
- dispatcher.preconnect(href, options);
195
- return;
276
+ return;
277
+ }
278
+
279
+ case 'L':
280
+ {
281
+ var _refined2 = refineModel(code, model);
282
+
283
+ var _href3 = _refined2[0];
284
+ var as = _refined2[1];
285
+
286
+ if (_refined2.length === 3) {
287
+ var options = _refined2[2];
288
+ dispatcher.preload(_href3, as, options);
289
+ } else {
290
+ dispatcher.preload(_href3, as);
196
291
  }
197
292
 
198
- case 'L':
199
- {
200
- // $FlowFixMe[prop-missing] options are not refined to their types by code
201
- // $FlowFixMe[incompatible-call] options are not refined to their types by code
202
- dispatcher.preload(href, options);
203
- return;
293
+ return;
294
+ }
295
+
296
+ case 'm':
297
+ {
298
+ var _refined3 = refineModel(code, model);
299
+
300
+ if (typeof _refined3 === 'string') {
301
+ var _href4 = _refined3;
302
+ dispatcher.preloadModule(_href4);
303
+ } else {
304
+ var _href5 = _refined3[0];
305
+ var _options = _refined3[1];
306
+ dispatcher.preloadModule(_href5, _options);
204
307
  }
205
308
 
206
- case 'I':
207
- {
208
- // $FlowFixMe[prop-missing] options are not refined to their types by code
209
- // $FlowFixMe[incompatible-call] options are not refined to their types by code
210
- dispatcher.preinit(href, options);
211
- return;
309
+ return;
310
+ }
311
+
312
+ case 'S':
313
+ {
314
+ var _refined4 = refineModel(code, model);
315
+
316
+ if (typeof _refined4 === 'string') {
317
+ var _href6 = _refined4;
318
+ dispatcher.preinitStyle(_href6);
319
+ } else {
320
+ var _href7 = _refined4[0];
321
+ var precedence = _refined4[1] === 0 ? undefined : _refined4[1];
322
+
323
+ var _options2 = _refined4.length === 3 ? _refined4[2] : undefined;
324
+
325
+ dispatcher.preinitStyle(_href7, precedence, _options2);
326
+ }
327
+
328
+ return;
329
+ }
330
+
331
+ case 'X':
332
+ {
333
+ var _refined5 = refineModel(code, model);
334
+
335
+ if (typeof _refined5 === 'string') {
336
+ var _href8 = _refined5;
337
+ dispatcher.preinitScript(_href8);
338
+ } else {
339
+ var _href9 = _refined5[0];
340
+ var _options3 = _refined5[1];
341
+ dispatcher.preinitScript(_href9, _options3);
212
342
  }
343
+
344
+ return;
345
+ }
346
+
347
+ case 'M':
348
+ {
349
+ var _refined6 = refineModel(code, model);
350
+
351
+ if (typeof _refined6 === 'string') {
352
+ var _href10 = _refined6;
353
+ dispatcher.preinitModuleScript(_href10);
354
+ } else {
355
+ var _href11 = _refined6[0];
356
+ var _options4 = _refined6[1];
357
+ dispatcher.preinitModuleScript(_href11, _options4);
358
+ }
359
+
360
+ return;
361
+ }
362
+ }
363
+ } // Flow is having trouble refining the HintModels so we help it a bit.
364
+ // This should be compiled out in the production build.
365
+
366
+ function refineModel(code, model) {
367
+ return model;
368
+ }
369
+
370
+ var ReactSharedInternals = React.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED;
371
+
372
+ function error(format) {
373
+ {
374
+ {
375
+ for (var _len2 = arguments.length, args = new Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) {
376
+ args[_key2 - 1] = arguments[_key2];
377
+ }
378
+
379
+ printWarning('error', format, args);
213
380
  }
214
381
  }
215
382
  }
216
383
 
217
- var knownServerReferences = new WeakMap();
218
- function createServerReference(id, callServer) {
219
- var proxy = function () {
220
- // $FlowFixMe[method-unbinding]
221
- var args = Array.prototype.slice.call(arguments);
222
- return callServer(id, args);
223
- };
384
+ function printWarning(level, format, args) {
385
+ // When changing this logic, you might want to also
386
+ // update consoleWithStackDev.www.js as well.
387
+ {
388
+ var ReactDebugCurrentFrame = ReactSharedInternals.ReactDebugCurrentFrame;
389
+ var stack = ReactDebugCurrentFrame.getStackAddendum();
224
390
 
225
- knownServerReferences.set(proxy, {
226
- id: id,
227
- bound: null
228
- });
229
- return proxy;
391
+ if (stack !== '') {
392
+ format += '%s';
393
+ args = args.concat([stack]);
394
+ } // eslint-disable-next-line react-internal/safe-string-coercion
395
+
396
+
397
+ var argsWithFormat = args.map(function (item) {
398
+ return String(item);
399
+ }); // Careful: RN currently depends on this prefix
400
+
401
+ argsWithFormat.unshift('Warning: ' + format); // We intentionally don't use spread (or .apply) directly because it
402
+ // breaks IE9: https://github.com/facebook/react/issues/13610
403
+ // eslint-disable-next-line react-internal/no-production-logging
404
+
405
+ Function.prototype.apply.call(console[level], console, argsWithFormat);
406
+ }
230
407
  }
231
408
 
232
409
  // ATTENTION
@@ -234,13 +411,12 @@ function createServerReference(id, callServer) {
234
411
  // Please consider also adding to 'react-devtools-shared/src/backend/ReactSymbols'
235
412
  // The Symbol used to tag the ReactElement-like types.
236
413
  var REACT_ELEMENT_TYPE = Symbol.for('react.element');
237
- var REACT_PROVIDER_TYPE = Symbol.for('react.provider');
414
+ var REACT_PROVIDER_TYPE = Symbol.for('react.provider'); // TODO: Delete with enableRenderableContext
238
415
  var REACT_FORWARD_REF_TYPE = Symbol.for('react.forward_ref');
239
416
  var REACT_SUSPENSE_TYPE = Symbol.for('react.suspense');
240
417
  var REACT_SUSPENSE_LIST_TYPE = Symbol.for('react.suspense_list');
241
418
  var REACT_MEMO_TYPE = Symbol.for('react.memo');
242
419
  var REACT_LAZY_TYPE = Symbol.for('react.lazy');
243
- var REACT_SERVER_CONTEXT_DEFAULT_VALUE_NOT_LOADED = Symbol.for('react.default_value');
244
420
  var MAYBE_ITERATOR_SYMBOL = Symbol.iterator;
245
421
  var FAUX_ITERATOR_SYMBOL = '@@iterator';
246
422
  function getIteratorFn(maybeIterable) {
@@ -257,1398 +433,1829 @@ function getIteratorFn(maybeIterable) {
257
433
  return null;
258
434
  }
259
435
 
260
- var ReactSharedInternals = React.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED;
261
-
262
- var ContextRegistry = ReactSharedInternals.ContextRegistry;
263
- function getOrCreateServerContext(globalName) {
264
- if (!ContextRegistry[globalName]) {
265
- ContextRegistry[globalName] = React.createServerContext(globalName, // $FlowFixMe[incompatible-call] function signature doesn't reflect the symbol value
266
- REACT_SERVER_CONTEXT_DEFAULT_VALUE_NOT_LOADED);
267
- }
436
+ var isArrayImpl = Array.isArray; // eslint-disable-next-line no-redeclare
268
437
 
269
- return ContextRegistry[globalName];
438
+ function isArray(a) {
439
+ return isArrayImpl(a);
270
440
  }
271
441
 
272
- var PENDING = 'pending';
273
- var BLOCKED = 'blocked';
274
- var RESOLVED_MODEL = 'resolved_model';
275
- var RESOLVED_MODULE = 'resolved_module';
276
- var INITIALIZED = 'fulfilled';
277
- var ERRORED = 'rejected'; // $FlowFixMe[missing-this-annot]
278
-
279
- function Chunk(status, value, reason, response) {
280
- this.status = status;
281
- this.value = value;
282
- this.reason = reason;
283
- this._response = response;
284
- } // We subclass Promise.prototype so that we get other methods like .catch
285
-
442
+ var getPrototypeOf = Object.getPrototypeOf;
286
443
 
287
- Chunk.prototype = Object.create(Promise.prototype); // TODO: This doesn't return a new Promise chain unlike the real .then
288
-
289
- Chunk.prototype.then = function (resolve, reject) {
290
- var chunk = this; // If we have resolved content, we try to initialize it first which
291
- // might put us back into one of the other states.
444
+ // in case they error.
292
445
 
293
- switch (chunk.status) {
294
- case RESOLVED_MODEL:
295
- initializeModelChunk(chunk);
296
- break;
446
+ var jsxPropsParents = new WeakMap();
447
+ var jsxChildrenParents = new WeakMap();
297
448
 
298
- case RESOLVED_MODULE:
299
- initializeModuleChunk(chunk);
300
- break;
301
- } // The status might have changed after initialization.
449
+ function isObjectPrototype(object) {
450
+ if (!object) {
451
+ return false;
452
+ }
302
453
 
454
+ var ObjectPrototype = Object.prototype;
303
455
 
304
- switch (chunk.status) {
305
- case INITIALIZED:
306
- resolve(chunk.value);
307
- break;
456
+ if (object === ObjectPrototype) {
457
+ return true;
458
+ } // It might be an object from a different Realm which is
459
+ // still just a plain simple object.
308
460
 
309
- case PENDING:
310
- case BLOCKED:
311
- if (resolve) {
312
- if (chunk.value === null) {
313
- chunk.value = [];
314
- }
315
461
 
316
- chunk.value.push(resolve);
317
- }
462
+ if (getPrototypeOf(object)) {
463
+ return false;
464
+ }
318
465
 
319
- if (reject) {
320
- if (chunk.reason === null) {
321
- chunk.reason = [];
322
- }
466
+ var names = Object.getOwnPropertyNames(object);
323
467
 
324
- chunk.reason.push(reject);
325
- }
468
+ for (var i = 0; i < names.length; i++) {
469
+ if (!(names[i] in ObjectPrototype)) {
470
+ return false;
471
+ }
472
+ }
326
473
 
327
- break;
474
+ return true;
475
+ }
328
476
 
329
- default:
330
- reject(chunk.reason);
331
- break;
477
+ function isSimpleObject(object) {
478
+ if (!isObjectPrototype(getPrototypeOf(object))) {
479
+ return false;
332
480
  }
333
- };
334
-
335
- function readChunk(chunk) {
336
- // If we have resolved content, we try to initialize it first which
337
- // might put us back into one of the other states.
338
- switch (chunk.status) {
339
- case RESOLVED_MODEL:
340
- initializeModelChunk(chunk);
341
- break;
342
481
 
343
- case RESOLVED_MODULE:
344
- initializeModuleChunk(chunk);
345
- break;
346
- } // The status might have changed after initialization.
482
+ var names = Object.getOwnPropertyNames(object);
347
483
 
484
+ for (var i = 0; i < names.length; i++) {
485
+ var descriptor = Object.getOwnPropertyDescriptor(object, names[i]);
348
486
 
349
- switch (chunk.status) {
350
- case INITIALIZED:
351
- return chunk.value;
487
+ if (!descriptor) {
488
+ return false;
489
+ }
352
490
 
353
- case PENDING:
354
- case BLOCKED:
355
- // eslint-disable-next-line no-throw-literal
356
- throw chunk;
491
+ if (!descriptor.enumerable) {
492
+ if ((names[i] === 'key' || names[i] === 'ref') && typeof descriptor.get === 'function') {
493
+ // React adds key and ref getters to props objects to issue warnings.
494
+ // Those getters will not be transferred to the client, but that's ok,
495
+ // so we'll special case them.
496
+ continue;
497
+ }
357
498
 
358
- default:
359
- throw chunk.reason;
499
+ return false;
500
+ }
360
501
  }
361
- }
362
502
 
363
- function getRoot(response) {
364
- var chunk = getChunk(response, 0);
365
- return chunk;
503
+ return true;
366
504
  }
367
-
368
- function createPendingChunk(response) {
369
- // $FlowFixMe[invalid-constructor] Flow doesn't support functions as constructors
370
- return new Chunk(PENDING, null, null, response);
505
+ function objectName(object) {
506
+ // $FlowFixMe[method-unbinding]
507
+ var name = Object.prototype.toString.call(object);
508
+ return name.replace(/^\[object (.*)\]$/, function (m, p0) {
509
+ return p0;
510
+ });
371
511
  }
372
512
 
373
- function createBlockedChunk(response) {
374
- // $FlowFixMe[invalid-constructor] Flow doesn't support functions as constructors
375
- return new Chunk(BLOCKED, null, null, response);
513
+ function describeKeyForErrorMessage(key) {
514
+ var encodedKey = JSON.stringify(key);
515
+ return '"' + key + '"' === encodedKey ? key : encodedKey;
376
516
  }
377
517
 
378
- function createErrorChunk(response, error) {
379
- // $FlowFixMe[invalid-constructor] Flow doesn't support functions as constructors
380
- return new Chunk(ERRORED, null, error, response);
381
- }
518
+ function describeValueForErrorMessage(value) {
519
+ switch (typeof value) {
520
+ case 'string':
521
+ {
522
+ return JSON.stringify(value.length <= 10 ? value : value.slice(0, 10) + '...');
523
+ }
382
524
 
383
- function wakeChunk(listeners, value) {
384
- for (var i = 0; i < listeners.length; i++) {
385
- var listener = listeners[i];
386
- listener(value);
387
- }
388
- }
525
+ case 'object':
526
+ {
527
+ if (isArray(value)) {
528
+ return '[...]';
529
+ }
389
530
 
390
- function wakeChunkIfInitialized(chunk, resolveListeners, rejectListeners) {
391
- switch (chunk.status) {
392
- case INITIALIZED:
393
- wakeChunk(resolveListeners, chunk.value);
394
- break;
531
+ if (value !== null && value.$$typeof === CLIENT_REFERENCE_TAG) {
532
+ return describeClientReference();
533
+ }
395
534
 
396
- case PENDING:
397
- case BLOCKED:
398
- chunk.value = resolveListeners;
399
- chunk.reason = rejectListeners;
400
- break;
535
+ var name = objectName(value);
401
536
 
402
- case ERRORED:
403
- if (rejectListeners) {
404
- wakeChunk(rejectListeners, chunk.reason);
537
+ if (name === 'Object') {
538
+ return '{...}';
539
+ }
540
+
541
+ return name;
405
542
  }
406
543
 
407
- break;
408
- }
409
- }
544
+ case 'function':
545
+ {
546
+ if (value.$$typeof === CLIENT_REFERENCE_TAG) {
547
+ return describeClientReference();
548
+ }
410
549
 
411
- function triggerErrorOnChunk(chunk, error) {
412
- if (chunk.status !== PENDING && chunk.status !== BLOCKED) {
413
- // We already resolved. We didn't expect to see this.
414
- return;
415
- }
550
+ var _name = value.displayName || value.name;
416
551
 
417
- var listeners = chunk.reason;
418
- var erroredChunk = chunk;
419
- erroredChunk.status = ERRORED;
420
- erroredChunk.reason = error;
552
+ return _name ? 'function ' + _name : 'function';
553
+ }
421
554
 
422
- if (listeners !== null) {
423
- wakeChunk(listeners, error);
555
+ default:
556
+ // eslint-disable-next-line react-internal/safe-string-coercion
557
+ return String(value);
424
558
  }
425
559
  }
426
560
 
427
- function createResolvedModelChunk(response, value) {
428
- // $FlowFixMe[invalid-constructor] Flow doesn't support functions as constructors
429
- return new Chunk(RESOLVED_MODEL, value, null, response);
430
- }
561
+ function describeElementType(type) {
562
+ if (typeof type === 'string') {
563
+ return type;
564
+ }
431
565
 
432
- function createResolvedModuleChunk(response, value) {
433
- // $FlowFixMe[invalid-constructor] Flow doesn't support functions as constructors
434
- return new Chunk(RESOLVED_MODULE, value, null, response);
435
- }
566
+ switch (type) {
567
+ case REACT_SUSPENSE_TYPE:
568
+ return 'Suspense';
436
569
 
437
- function resolveModelChunk(chunk, value) {
438
- if (chunk.status !== PENDING) {
439
- // We already resolved. We didn't expect to see this.
440
- return;
570
+ case REACT_SUSPENSE_LIST_TYPE:
571
+ return 'SuspenseList';
441
572
  }
442
573
 
443
- var resolveListeners = chunk.value;
444
- var rejectListeners = chunk.reason;
445
- var resolvedChunk = chunk;
446
- resolvedChunk.status = RESOLVED_MODEL;
447
- resolvedChunk.value = value;
574
+ if (typeof type === 'object') {
575
+ switch (type.$$typeof) {
576
+ case REACT_FORWARD_REF_TYPE:
577
+ return describeElementType(type.render);
448
578
 
449
- if (resolveListeners !== null) {
450
- // This is unfortunate that we're reading this eagerly if
451
- // we already have listeners attached since they might no
452
- // longer be rendered or might not be the highest pri.
453
- initializeModelChunk(resolvedChunk); // The status might have changed after initialization.
579
+ case REACT_MEMO_TYPE:
580
+ return describeElementType(type.type);
454
581
 
455
- wakeChunkIfInitialized(chunk, resolveListeners, rejectListeners);
582
+ case REACT_LAZY_TYPE:
583
+ {
584
+ var lazyComponent = type;
585
+ var payload = lazyComponent._payload;
586
+ var init = lazyComponent._init;
587
+
588
+ try {
589
+ // Lazy may contain any component type so we recursively resolve it.
590
+ return describeElementType(init(payload));
591
+ } catch (x) {}
592
+ }
593
+ }
456
594
  }
595
+
596
+ return '';
457
597
  }
458
598
 
459
- function resolveModuleChunk(chunk, value) {
460
- if (chunk.status !== PENDING && chunk.status !== BLOCKED) {
461
- // We already resolved. We didn't expect to see this.
462
- return;
463
- }
599
+ var CLIENT_REFERENCE_TAG = Symbol.for('react.client.reference');
464
600
 
465
- var resolveListeners = chunk.value;
466
- var rejectListeners = chunk.reason;
467
- var resolvedChunk = chunk;
468
- resolvedChunk.status = RESOLVED_MODULE;
469
- resolvedChunk.value = value;
601
+ function describeClientReference(ref) {
602
+ return 'client';
603
+ }
470
604
 
471
- if (resolveListeners !== null) {
472
- initializeModuleChunk(resolvedChunk);
473
- wakeChunkIfInitialized(chunk, resolveListeners, rejectListeners);
605
+ function describeObjectForErrorMessage(objectOrArray, expandedName) {
606
+ var objKind = objectName(objectOrArray);
607
+
608
+ if (objKind !== 'Object' && objKind !== 'Array') {
609
+ return objKind;
474
610
  }
475
- }
476
611
 
477
- var initializingChunk = null;
478
- var initializingChunkBlockedModel = null;
612
+ var str = '';
613
+ var start = -1;
614
+ var length = 0;
479
615
 
480
- function initializeModelChunk(chunk) {
481
- var prevChunk = initializingChunk;
482
- var prevBlocked = initializingChunkBlockedModel;
483
- initializingChunk = chunk;
484
- initializingChunkBlockedModel = null;
616
+ if (isArray(objectOrArray)) {
617
+ if (jsxChildrenParents.has(objectOrArray)) {
618
+ // Print JSX Children
619
+ var type = jsxChildrenParents.get(objectOrArray);
620
+ str = '<' + describeElementType(type) + '>';
621
+ var array = objectOrArray;
485
622
 
486
- try {
487
- var value = parseModel(chunk._response, chunk.value);
623
+ for (var i = 0; i < array.length; i++) {
624
+ var value = array[i];
625
+ var substr = void 0;
488
626
 
489
- if (initializingChunkBlockedModel !== null && initializingChunkBlockedModel.deps > 0) {
490
- initializingChunkBlockedModel.value = value; // We discovered new dependencies on modules that are not yet resolved.
491
- // We have to go the BLOCKED state until they're resolved.
627
+ if (typeof value === 'string') {
628
+ substr = value;
629
+ } else if (typeof value === 'object' && value !== null) {
630
+ substr = '{' + describeObjectForErrorMessage(value) + '}';
631
+ } else {
632
+ substr = '{' + describeValueForErrorMessage(value) + '}';
633
+ }
492
634
 
493
- var blockedChunk = chunk;
494
- blockedChunk.status = BLOCKED;
495
- blockedChunk.value = null;
496
- blockedChunk.reason = null;
635
+ if ('' + i === expandedName) {
636
+ start = str.length;
637
+ length = substr.length;
638
+ str += substr;
639
+ } else if (substr.length < 15 && str.length + substr.length < 40) {
640
+ str += substr;
641
+ } else {
642
+ str += '{...}';
643
+ }
644
+ }
645
+
646
+ str += '</' + describeElementType(type) + '>';
497
647
  } else {
498
- var initializedChunk = chunk;
499
- initializedChunk.status = INITIALIZED;
500
- initializedChunk.value = value;
501
- }
502
- } catch (error) {
503
- var erroredChunk = chunk;
504
- erroredChunk.status = ERRORED;
505
- erroredChunk.reason = error;
506
- } finally {
507
- initializingChunk = prevChunk;
508
- initializingChunkBlockedModel = prevBlocked;
509
- }
510
- }
648
+ // Print Array
649
+ str = '[';
650
+ var _array = objectOrArray;
511
651
 
512
- function initializeModuleChunk(chunk) {
513
- try {
514
- var value = requireModule(chunk.value);
515
- var initializedChunk = chunk;
516
- initializedChunk.status = INITIALIZED;
517
- initializedChunk.value = value;
518
- } catch (error) {
519
- var erroredChunk = chunk;
520
- erroredChunk.status = ERRORED;
521
- erroredChunk.reason = error;
522
- }
523
- } // Report that any missing chunks in the model is now going to throw this
524
- // error upon read. Also notify any pending promises.
652
+ for (var _i = 0; _i < _array.length; _i++) {
653
+ if (_i > 0) {
654
+ str += ', ';
655
+ }
525
656
 
657
+ var _value = _array[_i];
526
658
 
527
- function reportGlobalError(response, error) {
528
- response._chunks.forEach(function (chunk) {
529
- // If this chunk was already resolved or errored, it won't
530
- // trigger an error but if it wasn't then we need to
531
- // because we won't be getting any new data to resolve it.
532
- if (chunk.status === PENDING) {
533
- triggerErrorOnChunk(chunk, error);
534
- }
535
- });
659
+ var _substr = void 0;
660
+
661
+ if (typeof _value === 'object' && _value !== null) {
662
+ _substr = describeObjectForErrorMessage(_value);
663
+ } else {
664
+ _substr = describeValueForErrorMessage(_value);
665
+ }
666
+
667
+ if ('' + _i === expandedName) {
668
+ start = str.length;
669
+ length = _substr.length;
670
+ str += _substr;
671
+ } else if (_substr.length < 10 && str.length + _substr.length < 40) {
672
+ str += _substr;
673
+ } else {
674
+ str += '...';
675
+ }
676
+ }
677
+
678
+ str += ']';
679
+ }
680
+ } else {
681
+ if (objectOrArray.$$typeof === REACT_ELEMENT_TYPE) {
682
+ str = '<' + describeElementType(objectOrArray.type) + '/>';
683
+ } else if (objectOrArray.$$typeof === CLIENT_REFERENCE_TAG) {
684
+ return describeClientReference();
685
+ } else if (jsxPropsParents.has(objectOrArray)) {
686
+ // Print JSX
687
+ var _type = jsxPropsParents.get(objectOrArray);
688
+
689
+ str = '<' + (describeElementType(_type) || '...');
690
+ var object = objectOrArray;
691
+ var names = Object.keys(object);
692
+
693
+ for (var _i2 = 0; _i2 < names.length; _i2++) {
694
+ str += ' ';
695
+ var name = names[_i2];
696
+ str += describeKeyForErrorMessage(name) + '=';
697
+ var _value2 = object[name];
698
+
699
+ var _substr2 = void 0;
700
+
701
+ if (name === expandedName && typeof _value2 === 'object' && _value2 !== null) {
702
+ _substr2 = describeObjectForErrorMessage(_value2);
703
+ } else {
704
+ _substr2 = describeValueForErrorMessage(_value2);
705
+ }
706
+
707
+ if (typeof _value2 !== 'string') {
708
+ _substr2 = '{' + _substr2 + '}';
709
+ }
710
+
711
+ if (name === expandedName) {
712
+ start = str.length;
713
+ length = _substr2.length;
714
+ str += _substr2;
715
+ } else if (_substr2.length < 10 && str.length + _substr2.length < 40) {
716
+ str += _substr2;
717
+ } else {
718
+ str += '...';
719
+ }
720
+ }
721
+
722
+ str += '>';
723
+ } else {
724
+ // Print Object
725
+ str = '{';
726
+ var _object = objectOrArray;
727
+
728
+ var _names = Object.keys(_object);
729
+
730
+ for (var _i3 = 0; _i3 < _names.length; _i3++) {
731
+ if (_i3 > 0) {
732
+ str += ', ';
733
+ }
734
+
735
+ var _name2 = _names[_i3];
736
+ str += describeKeyForErrorMessage(_name2) + ': ';
737
+ var _value3 = _object[_name2];
738
+
739
+ var _substr3 = void 0;
740
+
741
+ if (typeof _value3 === 'object' && _value3 !== null) {
742
+ _substr3 = describeObjectForErrorMessage(_value3);
743
+ } else {
744
+ _substr3 = describeValueForErrorMessage(_value3);
745
+ }
746
+
747
+ if (_name2 === expandedName) {
748
+ start = str.length;
749
+ length = _substr3.length;
750
+ str += _substr3;
751
+ } else if (_substr3.length < 10 && str.length + _substr3.length < 40) {
752
+ str += _substr3;
753
+ } else {
754
+ str += '...';
755
+ }
756
+ }
757
+
758
+ str += '}';
759
+ }
760
+ }
761
+
762
+ if (expandedName === undefined) {
763
+ return str;
764
+ }
765
+
766
+ if (start > -1 && length > 0) {
767
+ var highlight = ' '.repeat(start) + '^'.repeat(length);
768
+ return '\n ' + str + '\n ' + highlight;
769
+ }
770
+
771
+ return '\n ' + str;
536
772
  }
537
773
 
538
- function createElement(type, key, props) {
539
- var element = {
540
- // This tag allows us to uniquely identify this as a React Element
541
- $$typeof: REACT_ELEMENT_TYPE,
542
- // Built-in properties that belong on the element
543
- type: type,
544
- key: key,
545
- ref: null,
546
- props: props,
547
- // Record the component responsible for creating this element.
548
- _owner: null
549
- };
774
+ function createTemporaryReferenceSet() {
775
+ return [];
776
+ }
777
+ function writeTemporaryReference(set, object) {
778
+ // We always create a new entry regardless if we've already written the same
779
+ // object. This ensures that we always generate a deterministic encoding of
780
+ // each slot in the reply for cacheability.
781
+ var newId = set.length;
782
+ set.push(object);
783
+ return newId;
784
+ }
785
+ function readTemporaryReference(set, id) {
786
+ if (id < 0 || id >= set.length) {
787
+ throw new Error("The RSC response contained a reference that doesn't exist in the temporary reference set. " + 'Always pass the matching set that was used to create the reply when parsing its response.');
788
+ }
550
789
 
551
- {
552
- // We don't really need to add any of these but keeping them for good measure.
553
- // Unfortunately, _store is enumerable in jest matchers so for equality to
554
- // work, I need to keep it or make _store non-enumerable in the other file.
555
- element._store = {};
556
- Object.defineProperty(element._store, 'validated', {
557
- configurable: false,
558
- enumerable: false,
559
- writable: true,
560
- value: true // This element has already been validated on the server.
790
+ return set[id];
791
+ }
561
792
 
562
- });
563
- Object.defineProperty(element, '_self', {
564
- configurable: false,
565
- enumerable: false,
566
- writable: false,
567
- value: null
568
- });
569
- Object.defineProperty(element, '_source', {
570
- configurable: false,
571
- enumerable: false,
572
- writable: false,
573
- value: null
574
- });
793
+ var ObjectPrototype = Object.prototype;
794
+ var knownServerReferences = new WeakMap(); // Serializable values
795
+ // Thenable<ReactServerValue>
796
+
797
+ function serializeByValueID(id) {
798
+ return '$' + id.toString(16);
799
+ }
800
+
801
+ function serializePromiseID(id) {
802
+ return '$@' + id.toString(16);
803
+ }
804
+
805
+ function serializeServerReferenceID(id) {
806
+ return '$F' + id.toString(16);
807
+ }
808
+
809
+ function serializeTemporaryReferenceID(id) {
810
+ return '$T' + id.toString(16);
811
+ }
812
+
813
+ function serializeFormDataReference(id) {
814
+ // Why K? F is "Function". D is "Date". What else?
815
+ return '$K' + id.toString(16);
816
+ }
817
+
818
+ function serializeNumber(number) {
819
+ if (Number.isFinite(number)) {
820
+ if (number === 0 && 1 / number === -Infinity) {
821
+ return '$-0';
822
+ } else {
823
+ return number;
824
+ }
825
+ } else {
826
+ if (number === Infinity) {
827
+ return '$Infinity';
828
+ } else if (number === -Infinity) {
829
+ return '$-Infinity';
830
+ } else {
831
+ return '$NaN';
832
+ }
575
833
  }
834
+ }
576
835
 
577
- return element;
836
+ function serializeUndefined() {
837
+ return '$undefined';
578
838
  }
579
839
 
580
- function createLazyChunkWrapper(chunk) {
581
- var lazyType = {
582
- $$typeof: REACT_LAZY_TYPE,
583
- _payload: chunk,
584
- _init: readChunk
585
- };
586
- return lazyType;
840
+ function serializeDateFromDateJSON(dateJSON) {
841
+ // JSON.stringify automatically calls Date.prototype.toJSON which calls toISOString.
842
+ // We need only tack on a $D prefix.
843
+ return '$D' + dateJSON;
844
+ }
845
+
846
+ function serializeBigInt(n) {
847
+ return '$n' + n.toString(10);
848
+ }
849
+
850
+ function serializeMapID(id) {
851
+ return '$Q' + id.toString(16);
852
+ }
853
+
854
+ function serializeSetID(id) {
855
+ return '$W' + id.toString(16);
856
+ }
857
+
858
+ function escapeStringValue(value) {
859
+ if (value[0] === '$') {
860
+ // We need to escape $ prefixed strings since we use those to encode
861
+ // references to IDs and as special symbol values.
862
+ return '$' + value;
863
+ } else {
864
+ return value;
865
+ }
587
866
  }
588
867
 
589
- function getChunk(response, id) {
590
- var chunks = response._chunks;
591
- var chunk = chunks.get(id);
868
+ function processReply(root, formFieldPrefix, temporaryReferences, resolve, reject) {
869
+ var nextPartId = 1;
870
+ var pendingParts = 0;
871
+ var formData = null;
872
+
873
+ function resolveToJSON(key, value) {
874
+ var parent = this; // Make sure that `parent[key]` wasn't JSONified before `value` was passed to us
875
+
876
+ {
877
+ // $FlowFixMe[incompatible-use]
878
+ var originalValue = parent[key];
879
+
880
+ if (typeof originalValue === 'object' && originalValue !== value && !(originalValue instanceof Date)) {
881
+ if (objectName(originalValue) !== 'Object') {
882
+ error('Only plain objects can be passed to Server Functions from the Client. ' + '%s objects are not supported.%s', objectName(originalValue), describeObjectForErrorMessage(parent, key));
883
+ } else {
884
+ error('Only plain objects can be passed to Server Functions from the Client. ' + 'Objects with toJSON methods are not supported. Convert it manually ' + 'to a simple value before passing it to props.%s', describeObjectForErrorMessage(parent, key));
885
+ }
886
+ }
887
+ }
888
+
889
+ if (value === null) {
890
+ return null;
891
+ }
892
+
893
+ if (typeof value === 'object') {
894
+ switch (value.$$typeof) {
895
+ case REACT_ELEMENT_TYPE:
896
+ {
897
+ if (temporaryReferences === undefined) {
898
+ throw new Error('React Element cannot be passed to Server Functions from the Client without a ' + 'temporary reference set. Pass a TemporaryReferenceSet to the options.' + (describeObjectForErrorMessage(parent, key) ));
899
+ }
900
+
901
+ return serializeTemporaryReferenceID(writeTemporaryReference(temporaryReferences, value));
902
+ }
903
+
904
+ case REACT_LAZY_TYPE:
905
+ {
906
+ // Resolve lazy as if it wasn't here. In the future this will be encoded as a Promise.
907
+ var lazy = value;
908
+ var payload = lazy._payload;
909
+ var init = lazy._init;
910
+
911
+ if (formData === null) {
912
+ // Upgrade to use FormData to allow us to stream this value.
913
+ formData = new FormData();
914
+ }
915
+
916
+ pendingParts++;
917
+
918
+ try {
919
+ var resolvedModel = init(payload); // We always outline this as a separate part even though we could inline it
920
+ // because it ensures a more deterministic encoding.
921
+
922
+ var lazyId = nextPartId++;
923
+ var partJSON = JSON.stringify(resolvedModel, resolveToJSON); // $FlowFixMe[incompatible-type] We know it's not null because we assigned it above.
924
+
925
+ var data = formData; // eslint-disable-next-line react-internal/safe-string-coercion
926
+
927
+ data.append(formFieldPrefix + lazyId, partJSON);
928
+ return serializeByValueID(lazyId);
929
+ } catch (x) {
930
+ if (typeof x === 'object' && x !== null && typeof x.then === 'function') {
931
+ // Suspended
932
+ pendingParts++;
933
+
934
+ var _lazyId = nextPartId++;
935
+
936
+ var thenable = x;
937
+
938
+ var retry = function () {
939
+ // While the first promise resolved, its value isn't necessarily what we'll
940
+ // resolve into because we might suspend again.
941
+ try {
942
+ var _partJSON = JSON.stringify(value, resolveToJSON); // $FlowFixMe[incompatible-type] We know it's not null because we assigned it above.
943
+
944
+
945
+ var _data = formData; // eslint-disable-next-line react-internal/safe-string-coercion
946
+
947
+ _data.append(formFieldPrefix + _lazyId, _partJSON);
948
+
949
+ pendingParts--;
950
+
951
+ if (pendingParts === 0) {
952
+ resolve(_data);
953
+ }
954
+ } catch (reason) {
955
+ reject(reason);
956
+ }
957
+ };
958
+
959
+ thenable.then(retry, retry);
960
+ return serializeByValueID(_lazyId);
961
+ } else {
962
+ // In the future we could consider serializing this as an error
963
+ // that throws on the server instead.
964
+ reject(x);
965
+ return null;
966
+ }
967
+ } finally {
968
+ pendingParts--;
969
+ }
970
+ }
971
+ } // $FlowFixMe[method-unbinding]
972
+
973
+
974
+ if (typeof value.then === 'function') {
975
+ // We assume that any object with a .then property is a "Thenable" type,
976
+ // or a Promise type. Either of which can be represented by a Promise.
977
+ if (formData === null) {
978
+ // Upgrade to use FormData to allow us to stream this value.
979
+ formData = new FormData();
980
+ }
981
+
982
+ pendingParts++;
983
+ var promiseId = nextPartId++;
984
+ var _thenable = value;
985
+
986
+ _thenable.then(function (partValue) {
987
+ try {
988
+ var _partJSON2 = JSON.stringify(partValue, resolveToJSON); // $FlowFixMe[incompatible-type] We know it's not null because we assigned it above.
989
+
990
+
991
+ var _data2 = formData; // eslint-disable-next-line react-internal/safe-string-coercion
992
+
993
+ _data2.append(formFieldPrefix + promiseId, _partJSON2);
994
+
995
+ pendingParts--;
996
+
997
+ if (pendingParts === 0) {
998
+ resolve(_data2);
999
+ }
1000
+ } catch (reason) {
1001
+ reject(reason);
1002
+ }
1003
+ }, function (reason) {
1004
+ // In the future we could consider serializing this as an error
1005
+ // that throws on the server instead.
1006
+ reject(reason);
1007
+ });
1008
+
1009
+ return serializePromiseID(promiseId);
1010
+ }
1011
+
1012
+ if (isArray(value)) {
1013
+ // $FlowFixMe[incompatible-return]
1014
+ return value;
1015
+ } // TODO: Should we the Object.prototype.toString.call() to test for cross-realm objects?
1016
+
1017
+
1018
+ if (value instanceof FormData) {
1019
+ if (formData === null) {
1020
+ // Upgrade to use FormData to allow us to use rich objects as its values.
1021
+ formData = new FormData();
1022
+ }
1023
+
1024
+ var _data3 = formData;
1025
+ var refId = nextPartId++; // Copy all the form fields with a prefix for this reference.
1026
+ // These must come first in the form order because we assume that all the
1027
+ // fields are available before this is referenced.
1028
+
1029
+ var prefix = formFieldPrefix + refId + '_'; // $FlowFixMe[prop-missing]: FormData has forEach.
1030
+
1031
+ value.forEach(function (originalValue, originalKey) {
1032
+ _data3.append(prefix + originalKey, originalValue);
1033
+ });
1034
+ return serializeFormDataReference(refId);
1035
+ }
1036
+
1037
+ if (value instanceof Map) {
1038
+ var _partJSON3 = JSON.stringify(Array.from(value), resolveToJSON);
1039
+
1040
+ if (formData === null) {
1041
+ formData = new FormData();
1042
+ }
1043
+
1044
+ var mapId = nextPartId++;
1045
+ formData.append(formFieldPrefix + mapId, _partJSON3);
1046
+ return serializeMapID(mapId);
1047
+ }
1048
+
1049
+ if (value instanceof Set) {
1050
+ var _partJSON4 = JSON.stringify(Array.from(value), resolveToJSON);
1051
+
1052
+ if (formData === null) {
1053
+ formData = new FormData();
1054
+ }
1055
+
1056
+ var setId = nextPartId++;
1057
+ formData.append(formFieldPrefix + setId, _partJSON4);
1058
+ return serializeSetID(setId);
1059
+ }
1060
+
1061
+ var iteratorFn = getIteratorFn(value);
1062
+
1063
+ if (iteratorFn) {
1064
+ return Array.from(value);
1065
+ } // Verify that this is a simple plain object.
1066
+
1067
+
1068
+ var proto = getPrototypeOf(value);
1069
+
1070
+ if (proto !== ObjectPrototype && (proto === null || getPrototypeOf(proto) !== null)) {
1071
+ if (temporaryReferences === undefined) {
1072
+ throw new Error('Only plain objects, and a few built-ins, can be passed to Server Actions. ' + 'Classes or null prototypes are not supported.');
1073
+ } // We can serialize class instances as temporary references.
1074
+
1075
+
1076
+ return serializeTemporaryReferenceID(writeTemporaryReference(temporaryReferences, value));
1077
+ }
1078
+
1079
+ {
1080
+ if (value.$$typeof === (REACT_PROVIDER_TYPE)) {
1081
+ error('React Context Providers cannot be passed to Server Functions from the Client.%s', describeObjectForErrorMessage(parent, key));
1082
+ } else if (objectName(value) !== 'Object') {
1083
+ error('Only plain objects can be passed to Server Functions from the Client. ' + '%s objects are not supported.%s', objectName(value), describeObjectForErrorMessage(parent, key));
1084
+ } else if (!isSimpleObject(value)) {
1085
+ error('Only plain objects can be passed to Server Functions from the Client. ' + 'Classes or other objects with methods are not supported.%s', describeObjectForErrorMessage(parent, key));
1086
+ } else if (Object.getOwnPropertySymbols) {
1087
+ var symbols = Object.getOwnPropertySymbols(value);
1088
+
1089
+ if (symbols.length > 0) {
1090
+ error('Only plain objects can be passed to Server Functions from the Client. ' + 'Objects with symbol properties like %s are not supported.%s', symbols[0].description, describeObjectForErrorMessage(parent, key));
1091
+ }
1092
+ }
1093
+ } // $FlowFixMe[incompatible-return]
1094
+
1095
+
1096
+ return value;
1097
+ }
1098
+
1099
+ if (typeof value === 'string') {
1100
+ // TODO: Maybe too clever. If we support URL there's no similar trick.
1101
+ if (value[value.length - 1] === 'Z') {
1102
+ // Possibly a Date, whose toJSON automatically calls toISOString
1103
+ // $FlowFixMe[incompatible-use]
1104
+ var _originalValue = parent[key];
1105
+
1106
+ if (_originalValue instanceof Date) {
1107
+ return serializeDateFromDateJSON(value);
1108
+ }
1109
+ }
1110
+
1111
+ return escapeStringValue(value);
1112
+ }
1113
+
1114
+ if (typeof value === 'boolean') {
1115
+ return value;
1116
+ }
1117
+
1118
+ if (typeof value === 'number') {
1119
+ return serializeNumber(value);
1120
+ }
1121
+
1122
+ if (typeof value === 'undefined') {
1123
+ return serializeUndefined();
1124
+ }
592
1125
 
593
- if (!chunk) {
594
- chunk = createPendingChunk(response);
595
- chunks.set(id, chunk);
596
- }
1126
+ if (typeof value === 'function') {
1127
+ var metaData = knownServerReferences.get(value);
597
1128
 
598
- return chunk;
599
- }
1129
+ if (metaData !== undefined) {
1130
+ var metaDataJSON = JSON.stringify(metaData, resolveToJSON);
600
1131
 
601
- function createModelResolver(chunk, parentObject, key) {
602
- var blocked;
1132
+ if (formData === null) {
1133
+ // Upgrade to use FormData to allow us to stream this value.
1134
+ formData = new FormData();
1135
+ } // The reference to this function came from the same client so we can pass it back.
603
1136
 
604
- if (initializingChunkBlockedModel) {
605
- blocked = initializingChunkBlockedModel;
606
- blocked.deps++;
607
- } else {
608
- blocked = initializingChunkBlockedModel = {
609
- deps: 1,
610
- value: null
611
- };
612
- }
613
1137
 
614
- return function (value) {
615
- parentObject[key] = value;
616
- blocked.deps--;
1138
+ var _refId = nextPartId++; // eslint-disable-next-line react-internal/safe-string-coercion
617
1139
 
618
- if (blocked.deps === 0) {
619
- if (chunk.status !== BLOCKED) {
620
- return;
621
- }
622
1140
 
623
- var resolveListeners = chunk.value;
624
- var initializedChunk = chunk;
625
- initializedChunk.status = INITIALIZED;
626
- initializedChunk.value = blocked.value;
1141
+ formData.set(formFieldPrefix + _refId, metaDataJSON);
1142
+ return serializeServerReferenceID(_refId);
1143
+ }
627
1144
 
628
- if (resolveListeners !== null) {
629
- wakeChunk(resolveListeners, blocked.value);
1145
+ if (temporaryReferences === undefined) {
1146
+ throw new Error('Client Functions cannot be passed directly to Server Functions. ' + 'Only Functions passed from the Server can be passed back again.');
630
1147
  }
631
- }
632
- };
633
- }
634
1148
 
635
- function createModelReject(chunk) {
636
- return function (error) {
637
- return triggerErrorOnChunk(chunk, error);
638
- };
639
- }
1149
+ return serializeTemporaryReferenceID(writeTemporaryReference(temporaryReferences, value));
1150
+ }
640
1151
 
641
- function createServerReferenceProxy(response, metaData) {
642
- var callServer = response._callServer;
1152
+ if (typeof value === 'symbol') {
1153
+ if (temporaryReferences === undefined) {
1154
+ throw new Error('Symbols cannot be passed to a Server Function without a ' + 'temporary reference set. Pass a TemporaryReferenceSet to the options.' + (describeObjectForErrorMessage(parent, key) ));
1155
+ }
643
1156
 
644
- var proxy = function () {
645
- // $FlowFixMe[method-unbinding]
646
- var args = Array.prototype.slice.call(arguments);
647
- var p = metaData.bound;
1157
+ return serializeTemporaryReferenceID(writeTemporaryReference(temporaryReferences, value));
1158
+ }
648
1159
 
649
- if (!p) {
650
- return callServer(metaData.id, args);
1160
+ if (typeof value === 'bigint') {
1161
+ return serializeBigInt(value);
651
1162
  }
652
1163
 
653
- if (p.status === INITIALIZED) {
654
- var bound = p.value;
655
- return callServer(metaData.id, bound.concat(args));
656
- } // Since this is a fake Promise whose .then doesn't chain, we have to wrap it.
657
- // TODO: Remove the wrapper once that's fixed.
1164
+ throw new Error("Type " + typeof value + " is not supported as an argument to a Server Function.");
1165
+ } // $FlowFixMe[incompatible-type] it's not going to be undefined because we'll encode it.
658
1166
 
659
1167
 
660
- return Promise.resolve(p).then(function (bound) {
661
- return callServer(metaData.id, bound.concat(args));
662
- });
663
- };
1168
+ var json = JSON.stringify(root, resolveToJSON);
664
1169
 
665
- knownServerReferences.set(proxy, metaData);
666
- return proxy;
667
- }
1170
+ if (formData === null) {
1171
+ // If it's a simple data structure, we just use plain JSON.
1172
+ resolve(json);
1173
+ } else {
1174
+ // Otherwise, we use FormData to let us stream in the result.
1175
+ formData.set(formFieldPrefix + '0', json);
668
1176
 
669
- function parseModelString(response, parentObject, key, value) {
670
- if (value[0] === '$') {
671
- if (value === '$') {
672
- // A very common symbol.
673
- return REACT_ELEMENT_TYPE;
1177
+ if (pendingParts === 0) {
1178
+ // $FlowFixMe[incompatible-call] this has already been refined.
1179
+ resolve(formData);
674
1180
  }
1181
+ }
1182
+ }
675
1183
 
676
- switch (value[1]) {
677
- case '$':
678
- {
679
- // This was an escaped string value.
680
- return value.slice(1);
681
- }
682
-
683
- case 'L':
684
- {
685
- // Lazy node
686
- var id = parseInt(value.slice(2), 16);
687
- var chunk = getChunk(response, id); // We create a React.lazy wrapper around any lazy values.
688
- // When passed into React, we'll know how to suspend on this.
689
-
690
- return createLazyChunkWrapper(chunk);
691
- }
692
-
693
- case '@':
694
- {
695
- // Promise
696
- var _id = parseInt(value.slice(2), 16);
1184
+ function registerServerReference(proxy, reference, encodeFormAction) {
697
1185
 
698
- var _chunk = getChunk(response, _id);
1186
+ knownServerReferences.set(proxy, reference);
1187
+ } // $FlowFixMe[method-unbinding]
699
1188
 
700
- return _chunk;
701
- }
1189
+ function createServerReference(id, callServer, encodeFormAction) {
1190
+ var proxy = function () {
1191
+ // $FlowFixMe[method-unbinding]
1192
+ var args = Array.prototype.slice.call(arguments);
1193
+ return callServer(id, args);
1194
+ };
702
1195
 
703
- case 'S':
704
- {
705
- // Symbol
706
- return Symbol.for(value.slice(2));
707
- }
1196
+ registerServerReference(proxy, {
1197
+ id: id,
1198
+ bound: null
1199
+ });
1200
+ return proxy;
1201
+ }
708
1202
 
709
- case 'P':
710
- {
711
- // Server Context Provider
712
- return getOrCreateServerContext(value.slice(2)).Provider;
713
- }
1203
+ var ROW_ID = 0;
1204
+ var ROW_TAG = 1;
1205
+ var ROW_LENGTH = 2;
1206
+ var ROW_CHUNK_BY_NEWLINE = 3;
1207
+ var ROW_CHUNK_BY_LENGTH = 4;
1208
+ var PENDING = 'pending';
1209
+ var BLOCKED = 'blocked';
1210
+ var CYCLIC = 'cyclic';
1211
+ var RESOLVED_MODEL = 'resolved_model';
1212
+ var RESOLVED_MODULE = 'resolved_module';
1213
+ var INITIALIZED = 'fulfilled';
1214
+ var ERRORED = 'rejected'; // $FlowFixMe[missing-this-annot]
714
1215
 
715
- case 'F':
716
- {
717
- // Server Reference
718
- var _id2 = parseInt(value.slice(2), 16);
1216
+ function Chunk(status, value, reason, response) {
1217
+ this.status = status;
1218
+ this.value = value;
1219
+ this.reason = reason;
1220
+ this._response = response;
719
1221
 
720
- var _chunk2 = getChunk(response, _id2);
1222
+ {
1223
+ this._debugInfo = null;
1224
+ }
1225
+ } // We subclass Promise.prototype so that we get other methods like .catch
721
1226
 
722
- switch (_chunk2.status) {
723
- case RESOLVED_MODEL:
724
- initializeModelChunk(_chunk2);
725
- break;
726
- } // The status might have changed after initialization.
727
1227
 
1228
+ Chunk.prototype = Object.create(Promise.prototype); // TODO: This doesn't return a new Promise chain unlike the real .then
728
1229
 
729
- switch (_chunk2.status) {
730
- case INITIALIZED:
731
- {
732
- var metadata = _chunk2.value;
733
- return createServerReferenceProxy(response, metadata);
734
- }
735
- // We always encode it first in the stream so it won't be pending.
1230
+ Chunk.prototype.then = function (resolve, reject) {
1231
+ var chunk = this; // If we have resolved content, we try to initialize it first which
1232
+ // might put us back into one of the other states.
736
1233
 
737
- default:
738
- throw _chunk2.reason;
739
- }
740
- }
1234
+ switch (chunk.status) {
1235
+ case RESOLVED_MODEL:
1236
+ initializeModelChunk(chunk);
1237
+ break;
741
1238
 
742
- case 'I':
743
- {
744
- // $Infinity
745
- return Infinity;
746
- }
1239
+ case RESOLVED_MODULE:
1240
+ initializeModuleChunk(chunk);
1241
+ break;
1242
+ } // The status might have changed after initialization.
747
1243
 
748
- case '-':
749
- {
750
- // $-0 or $-Infinity
751
- if (value === '$-0') {
752
- return -0;
753
- } else {
754
- return -Infinity;
755
- }
756
- }
757
1244
 
758
- case 'N':
759
- {
760
- // $NaN
761
- return NaN;
762
- }
1245
+ switch (chunk.status) {
1246
+ case INITIALIZED:
1247
+ resolve(chunk.value);
1248
+ break;
763
1249
 
764
- case 'u':
765
- {
766
- // matches "$undefined"
767
- // Special encoding for `undefined` which can't be serialized as JSON otherwise.
768
- return undefined;
1250
+ case PENDING:
1251
+ case BLOCKED:
1252
+ case CYCLIC:
1253
+ if (resolve) {
1254
+ if (chunk.value === null) {
1255
+ chunk.value = [];
769
1256
  }
770
1257
 
771
- case 'D':
772
- {
773
- // Date
774
- return new Date(Date.parse(value.slice(2)));
775
- }
1258
+ chunk.value.push(resolve);
1259
+ }
776
1260
 
777
- case 'n':
778
- {
779
- // BigInt
780
- return BigInt(value.slice(2));
1261
+ if (reject) {
1262
+ if (chunk.reason === null) {
1263
+ chunk.reason = [];
781
1264
  }
782
1265
 
783
- default:
784
- {
785
- // We assume that anything else is a reference ID.
786
- var _id3 = parseInt(value.slice(1), 16);
787
-
788
- var _chunk3 = getChunk(response, _id3);
1266
+ chunk.reason.push(reject);
1267
+ }
789
1268
 
790
- switch (_chunk3.status) {
791
- case RESOLVED_MODEL:
792
- initializeModelChunk(_chunk3);
793
- break;
1269
+ break;
794
1270
 
795
- case RESOLVED_MODULE:
796
- initializeModuleChunk(_chunk3);
797
- break;
798
- } // The status might have changed after initialization.
1271
+ default:
1272
+ reject(chunk.reason);
1273
+ break;
1274
+ }
1275
+ };
799
1276
 
1277
+ function readChunk(chunk) {
1278
+ // If we have resolved content, we try to initialize it first which
1279
+ // might put us back into one of the other states.
1280
+ switch (chunk.status) {
1281
+ case RESOLVED_MODEL:
1282
+ initializeModelChunk(chunk);
1283
+ break;
800
1284
 
801
- switch (_chunk3.status) {
802
- case INITIALIZED:
803
- return _chunk3.value;
1285
+ case RESOLVED_MODULE:
1286
+ initializeModuleChunk(chunk);
1287
+ break;
1288
+ } // The status might have changed after initialization.
804
1289
 
805
- case PENDING:
806
- case BLOCKED:
807
- var parentChunk = initializingChunk;
808
1290
 
809
- _chunk3.then(createModelResolver(parentChunk, parentObject, key), createModelReject(parentChunk));
1291
+ switch (chunk.status) {
1292
+ case INITIALIZED:
1293
+ return chunk.value;
810
1294
 
811
- return null;
1295
+ case PENDING:
1296
+ case BLOCKED:
1297
+ case CYCLIC:
1298
+ // eslint-disable-next-line no-throw-literal
1299
+ throw chunk;
812
1300
 
813
- default:
814
- throw _chunk3.reason;
815
- }
816
- }
817
- }
1301
+ default:
1302
+ throw chunk.reason;
818
1303
  }
819
-
820
- return value;
821
1304
  }
822
- function parseModelTuple(response, value) {
823
- var tuple = value;
824
1305
 
825
- if (tuple[0] === REACT_ELEMENT_TYPE) {
826
- // TODO: Consider having React just directly accept these arrays as elements.
827
- // Or even change the ReactElement type to be an array.
828
- return createElement(tuple[1], tuple[2], tuple[3]);
829
- }
1306
+ function getRoot(response) {
1307
+ var chunk = getChunk(response, 0);
1308
+ return chunk;
1309
+ }
830
1310
 
831
- return value;
1311
+ function createPendingChunk(response) {
1312
+ // $FlowFixMe[invalid-constructor] Flow doesn't support functions as constructors
1313
+ return new Chunk(PENDING, null, null, response);
832
1314
  }
833
1315
 
834
- function missingCall() {
835
- throw new Error('Trying to call a function from "use server" but the callServer option ' + 'was not implemented in your router runtime.');
1316
+ function createBlockedChunk(response) {
1317
+ // $FlowFixMe[invalid-constructor] Flow doesn't support functions as constructors
1318
+ return new Chunk(BLOCKED, null, null, response);
836
1319
  }
837
1320
 
838
- function createResponse$1(bundlerConfig, callServer) {
839
- var chunks = new Map();
840
- var response = {
841
- _bundlerConfig: bundlerConfig,
842
- _callServer: callServer !== undefined ? callServer : missingCall,
843
- _chunks: chunks
844
- };
845
- return response;
1321
+ function createErrorChunk(response, error) {
1322
+ // $FlowFixMe[invalid-constructor] Flow doesn't support functions as constructors
1323
+ return new Chunk(ERRORED, null, error, response);
846
1324
  }
847
- function resolveModel(response, id, model) {
848
- var chunks = response._chunks;
849
- var chunk = chunks.get(id);
850
1325
 
851
- if (!chunk) {
852
- chunks.set(id, createResolvedModelChunk(response, model));
853
- } else {
854
- resolveModelChunk(chunk, model);
1326
+ function wakeChunk(listeners, value) {
1327
+ for (var i = 0; i < listeners.length; i++) {
1328
+ var listener = listeners[i];
1329
+ listener(value);
855
1330
  }
856
1331
  }
857
- function resolveModule(response, id, model) {
858
- var chunks = response._chunks;
859
- var chunk = chunks.get(id);
860
- var clientReferenceMetadata = parseModel(response, model);
861
- var clientReference = resolveClientReference(response._bundlerConfig, clientReferenceMetadata); // TODO: Add an option to encode modules that are lazy loaded.
862
- // For now we preload all modules as early as possible since it's likely
863
- // that we'll need them.
864
1332
 
865
- var promise = preloadModule(clientReference);
1333
+ function wakeChunkIfInitialized(chunk, resolveListeners, rejectListeners) {
1334
+ switch (chunk.status) {
1335
+ case INITIALIZED:
1336
+ wakeChunk(resolveListeners, chunk.value);
1337
+ break;
866
1338
 
867
- if (promise) {
868
- var blockedChunk;
1339
+ case PENDING:
1340
+ case BLOCKED:
1341
+ case CYCLIC:
1342
+ chunk.value = resolveListeners;
1343
+ chunk.reason = rejectListeners;
1344
+ break;
869
1345
 
870
- if (!chunk) {
871
- // Technically, we should just treat promise as the chunk in this
872
- // case. Because it'll just behave as any other promise.
873
- blockedChunk = createBlockedChunk(response);
874
- chunks.set(id, blockedChunk);
875
- } else {
876
- // This can't actually happen because we don't have any forward
877
- // references to modules.
878
- blockedChunk = chunk;
879
- blockedChunk.status = BLOCKED;
880
- }
1346
+ case ERRORED:
1347
+ if (rejectListeners) {
1348
+ wakeChunk(rejectListeners, chunk.reason);
1349
+ }
881
1350
 
882
- promise.then(function () {
883
- return resolveModuleChunk(blockedChunk, clientReference);
884
- }, function (error) {
885
- return triggerErrorOnChunk(blockedChunk, error);
886
- });
887
- } else {
888
- if (!chunk) {
889
- chunks.set(id, createResolvedModuleChunk(response, clientReference));
890
- } else {
891
- // This can't actually happen because we don't have any forward
892
- // references to modules.
893
- resolveModuleChunk(chunk, clientReference);
894
- }
1351
+ break;
895
1352
  }
896
1353
  }
897
- function resolveErrorDev(response, id, digest, message, stack) {
898
1354
 
1355
+ function triggerErrorOnChunk(chunk, error) {
1356
+ if (chunk.status !== PENDING && chunk.status !== BLOCKED) {
1357
+ // We already resolved. We didn't expect to see this.
1358
+ return;
1359
+ }
899
1360
 
900
- var error = new Error(message || 'An error occurred in the Server Components render but no message was provided');
901
- error.stack = stack;
902
- error.digest = digest;
903
- var errorWithDigest = error;
904
- var chunks = response._chunks;
905
- var chunk = chunks.get(id);
1361
+ var listeners = chunk.reason;
1362
+ var erroredChunk = chunk;
1363
+ erroredChunk.status = ERRORED;
1364
+ erroredChunk.reason = error;
906
1365
 
907
- if (!chunk) {
908
- chunks.set(id, createErrorChunk(response, errorWithDigest));
909
- } else {
910
- triggerErrorOnChunk(chunk, errorWithDigest);
1366
+ if (listeners !== null) {
1367
+ wakeChunk(listeners, error);
911
1368
  }
912
1369
  }
913
- function resolveHint(response, code, model) {
914
- var hintModel = parseModel(response, model);
915
- dispatchHint(code, hintModel);
1370
+
1371
+ function createResolvedModelChunk(response, value) {
1372
+ // $FlowFixMe[invalid-constructor] Flow doesn't support functions as constructors
1373
+ return new Chunk(RESOLVED_MODEL, value, null, response);
916
1374
  }
917
- function close(response) {
918
- // In case there are any remaining unresolved chunks, they won't
919
- // be resolved now. So we need to issue an error to those.
920
- // Ideally we should be able to early bail out if we kept a
921
- // ref count of pending chunks.
922
- reportGlobalError(response, new Error('Connection closed.'));
1375
+
1376
+ function createResolvedModuleChunk(response, value) {
1377
+ // $FlowFixMe[invalid-constructor] Flow doesn't support functions as constructors
1378
+ return new Chunk(RESOLVED_MODULE, value, null, response);
1379
+ }
1380
+
1381
+ function createInitializedTextChunk(response, value) {
1382
+ // $FlowFixMe[invalid-constructor] Flow doesn't support functions as constructors
1383
+ return new Chunk(INITIALIZED, value, null, response);
923
1384
  }
924
1385
 
925
- function processFullRow(response, row) {
926
- if (row === '') {
1386
+ function resolveModelChunk(chunk, value) {
1387
+ if (chunk.status !== PENDING) {
1388
+ // We already resolved. We didn't expect to see this.
927
1389
  return;
928
1390
  }
929
1391
 
930
- var colon = row.indexOf(':', 0);
931
- var id = parseInt(row.slice(0, colon), 16);
932
- var tag = row[colon + 1]; // When tags that are not text are added, check them here before
933
- // parsing the row as text.
934
- // switch (tag) {
935
- // }
936
-
937
- switch (tag) {
938
- case 'I':
939
- {
940
- resolveModule(response, id, row.slice(colon + 2));
941
- return;
942
- }
1392
+ var resolveListeners = chunk.value;
1393
+ var rejectListeners = chunk.reason;
1394
+ var resolvedChunk = chunk;
1395
+ resolvedChunk.status = RESOLVED_MODEL;
1396
+ resolvedChunk.value = value;
943
1397
 
944
- case 'H':
945
- {
946
- var code = row[colon + 2];
947
- resolveHint(response, code, row.slice(colon + 3));
948
- return;
949
- }
1398
+ if (resolveListeners !== null) {
1399
+ // This is unfortunate that we're reading this eagerly if
1400
+ // we already have listeners attached since they might no
1401
+ // longer be rendered or might not be the highest pri.
1402
+ initializeModelChunk(resolvedChunk); // The status might have changed after initialization.
950
1403
 
951
- case 'E':
952
- {
953
- var errorInfo = JSON.parse(row.slice(colon + 2));
1404
+ wakeChunkIfInitialized(chunk, resolveListeners, rejectListeners);
1405
+ }
1406
+ }
954
1407
 
955
- {
956
- resolveErrorDev(response, id, errorInfo.digest, errorInfo.message, errorInfo.stack);
957
- }
1408
+ function resolveModuleChunk(chunk, value) {
1409
+ if (chunk.status !== PENDING && chunk.status !== BLOCKED) {
1410
+ // We already resolved. We didn't expect to see this.
1411
+ return;
1412
+ }
958
1413
 
959
- return;
960
- }
1414
+ var resolveListeners = chunk.value;
1415
+ var rejectListeners = chunk.reason;
1416
+ var resolvedChunk = chunk;
1417
+ resolvedChunk.status = RESOLVED_MODULE;
1418
+ resolvedChunk.value = value;
961
1419
 
962
- default:
963
- {
964
- // We assume anything else is JSON.
965
- resolveModel(response, id, row.slice(colon + 1));
966
- return;
967
- }
1420
+ if (resolveListeners !== null) {
1421
+ initializeModuleChunk(resolvedChunk);
1422
+ wakeChunkIfInitialized(chunk, resolveListeners, rejectListeners);
968
1423
  }
969
1424
  }
970
1425
 
971
- function processStringChunk(response, chunk, offset) {
972
- var linebreak = chunk.indexOf('\n', offset);
1426
+ var initializingChunk = null;
1427
+ var initializingChunkBlockedModel = null;
973
1428
 
974
- while (linebreak > -1) {
975
- var fullrow = response._partialRow + chunk.slice(offset, linebreak);
976
- processFullRow(response, fullrow);
977
- response._partialRow = '';
978
- offset = linebreak + 1;
979
- linebreak = chunk.indexOf('\n', offset);
980
- }
1429
+ function initializeModelChunk(chunk) {
1430
+ var prevChunk = initializingChunk;
1431
+ var prevBlocked = initializingChunkBlockedModel;
1432
+ initializingChunk = chunk;
1433
+ initializingChunkBlockedModel = null;
1434
+ var resolvedModel = chunk.value; // We go to the CYCLIC state until we've fully resolved this.
1435
+ // We do this before parsing in case we try to initialize the same chunk
1436
+ // while parsing the model. Such as in a cyclic reference.
981
1437
 
982
- response._partialRow += chunk.slice(offset);
983
- }
984
- function processBinaryChunk(response, chunk) {
1438
+ var cyclicChunk = chunk;
1439
+ cyclicChunk.status = CYCLIC;
1440
+ cyclicChunk.value = null;
1441
+ cyclicChunk.reason = null;
985
1442
 
986
- var stringDecoder = response._stringDecoder;
987
- var linebreak = chunk.indexOf(10); // newline
988
-
989
- while (linebreak > -1) {
990
- var fullrow = response._partialRow + readFinalStringChunk(stringDecoder, chunk.subarray(0, linebreak));
991
- processFullRow(response, fullrow);
992
- response._partialRow = '';
993
- chunk = chunk.subarray(linebreak + 1);
994
- linebreak = chunk.indexOf(10); // newline
995
- }
1443
+ try {
1444
+ var value = parseModel(chunk._response, resolvedModel);
996
1445
 
997
- response._partialRow += readPartialStringChunk(stringDecoder, chunk);
998
- }
1446
+ if (initializingChunkBlockedModel !== null && initializingChunkBlockedModel.deps > 0) {
1447
+ initializingChunkBlockedModel.value = value; // We discovered new dependencies on modules that are not yet resolved.
1448
+ // We have to go the BLOCKED state until they're resolved.
999
1449
 
1000
- function createFromJSONCallback(response) {
1001
- // $FlowFixMe[missing-this-annot]
1002
- return function (key, value) {
1003
- if (typeof value === 'string') {
1004
- // We can't use .bind here because we need the "this" value.
1005
- return parseModelString(response, this, key, value);
1006
- }
1450
+ var blockedChunk = chunk;
1451
+ blockedChunk.status = BLOCKED;
1452
+ blockedChunk.value = null;
1453
+ blockedChunk.reason = null;
1454
+ } else {
1455
+ var resolveListeners = cyclicChunk.value;
1456
+ var initializedChunk = chunk;
1457
+ initializedChunk.status = INITIALIZED;
1458
+ initializedChunk.value = value;
1007
1459
 
1008
- if (typeof value === 'object' && value !== null) {
1009
- return parseModelTuple(response, value);
1460
+ if (resolveListeners !== null) {
1461
+ wakeChunk(resolveListeners, value);
1462
+ }
1010
1463
  }
1011
-
1012
- return value;
1013
- };
1464
+ } catch (error) {
1465
+ var erroredChunk = chunk;
1466
+ erroredChunk.status = ERRORED;
1467
+ erroredChunk.reason = error;
1468
+ } finally {
1469
+ initializingChunk = prevChunk;
1470
+ initializingChunkBlockedModel = prevBlocked;
1471
+ }
1014
1472
  }
1015
1473
 
1016
- function createResponse(bundlerConfig, callServer) {
1017
- // NOTE: CHECK THE COMPILER OUTPUT EACH TIME YOU CHANGE THIS.
1018
- // It should be inlined to one object literal but minor changes can break it.
1019
- var stringDecoder = createStringDecoder() ;
1020
- var response = createResponse$1(bundlerConfig, callServer);
1021
- response._partialRow = '';
1022
-
1023
- {
1024
- response._stringDecoder = stringDecoder;
1025
- } // Don't inline this call because it causes closure to outline the call above.
1474
+ function initializeModuleChunk(chunk) {
1475
+ try {
1476
+ var value = requireModule(chunk.value);
1477
+ var initializedChunk = chunk;
1478
+ initializedChunk.status = INITIALIZED;
1479
+ initializedChunk.value = value;
1480
+ } catch (error) {
1481
+ var erroredChunk = chunk;
1482
+ erroredChunk.status = ERRORED;
1483
+ erroredChunk.reason = error;
1484
+ }
1485
+ } // Report that any missing chunks in the model is now going to throw this
1486
+ // error upon read. Also notify any pending promises.
1026
1487
 
1027
1488
 
1028
- response._fromJSON = createFromJSONCallback(response);
1029
- return response;
1489
+ function reportGlobalError(response, error) {
1490
+ response._chunks.forEach(function (chunk) {
1491
+ // If this chunk was already resolved or errored, it won't
1492
+ // trigger an error but if it wasn't then we need to
1493
+ // because we won't be getting any new data to resolve it.
1494
+ if (chunk.status === PENDING) {
1495
+ triggerErrorOnChunk(chunk, error);
1496
+ }
1497
+ });
1030
1498
  }
1031
1499
 
1032
- function error(format) {
1033
- {
1034
- {
1035
- for (var _len2 = arguments.length, args = new Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) {
1036
- args[_key2 - 1] = arguments[_key2];
1037
- }
1500
+ function createElement(type, key, props) {
1501
+ var element;
1038
1502
 
1039
- printWarning('error', format, args);
1040
- }
1503
+ {
1504
+ element = {
1505
+ // This tag allows us to uniquely identify this as a React Element
1506
+ $$typeof: REACT_ELEMENT_TYPE,
1507
+ type: type,
1508
+ key: key,
1509
+ ref: null,
1510
+ props: props,
1511
+ // Record the component responsible for creating this element.
1512
+ _owner: null
1513
+ };
1041
1514
  }
1042
- }
1043
1515
 
1044
- function printWarning(level, format, args) {
1045
- // When changing this logic, you might want to also
1046
- // update consoleWithStackDev.www.js as well.
1047
1516
  {
1048
- var ReactDebugCurrentFrame = ReactSharedInternals.ReactDebugCurrentFrame;
1049
- var stack = ReactDebugCurrentFrame.getStackAddendum();
1517
+ // We don't really need to add any of these but keeping them for good measure.
1518
+ // Unfortunately, _store is enumerable in jest matchers so for equality to
1519
+ // work, I need to keep it or make _store non-enumerable in the other file.
1520
+ element._store = {};
1521
+ Object.defineProperty(element._store, 'validated', {
1522
+ configurable: false,
1523
+ enumerable: false,
1524
+ writable: true,
1525
+ value: true // This element has already been validated on the server.
1050
1526
 
1051
- if (stack !== '') {
1052
- format += '%s';
1053
- args = args.concat([stack]);
1054
- } // eslint-disable-next-line react-internal/safe-string-coercion
1527
+ }); // debugInfo contains Server Component debug information.
1055
1528
 
1529
+ Object.defineProperty(element, '_debugInfo', {
1530
+ configurable: false,
1531
+ enumerable: false,
1532
+ writable: true,
1533
+ value: null
1534
+ });
1535
+ }
1056
1536
 
1057
- var argsWithFormat = args.map(function (item) {
1058
- return String(item);
1059
- }); // Careful: RN currently depends on this prefix
1537
+ return element;
1538
+ }
1060
1539
 
1061
- argsWithFormat.unshift('Warning: ' + format); // We intentionally don't use spread (or .apply) directly because it
1062
- // breaks IE9: https://github.com/facebook/react/issues/13610
1063
- // eslint-disable-next-line react-internal/no-production-logging
1540
+ function createLazyChunkWrapper(chunk) {
1541
+ var lazyType = {
1542
+ $$typeof: REACT_LAZY_TYPE,
1543
+ _payload: chunk,
1544
+ _init: readChunk
1545
+ };
1064
1546
 
1065
- Function.prototype.apply.call(console[level], console, argsWithFormat);
1547
+ {
1548
+ // Ensure we have a live array to track future debug info.
1549
+ var chunkDebugInfo = chunk._debugInfo || (chunk._debugInfo = []);
1550
+ lazyType._debugInfo = chunkDebugInfo;
1066
1551
  }
1067
- }
1068
-
1069
- var isArrayImpl = Array.isArray; // eslint-disable-next-line no-redeclare
1070
1552
 
1071
- function isArray(a) {
1072
- return isArrayImpl(a);
1553
+ return lazyType;
1073
1554
  }
1074
1555
 
1075
- // in case they error.
1076
-
1077
- var jsxPropsParents = new WeakMap();
1078
- var jsxChildrenParents = new WeakMap();
1556
+ function getChunk(response, id) {
1557
+ var chunks = response._chunks;
1558
+ var chunk = chunks.get(id);
1079
1559
 
1080
- function isObjectPrototype(object) {
1081
- if (!object) {
1082
- return false;
1560
+ if (!chunk) {
1561
+ chunk = createPendingChunk(response);
1562
+ chunks.set(id, chunk);
1083
1563
  }
1084
1564
 
1085
- var ObjectPrototype = Object.prototype;
1565
+ return chunk;
1566
+ }
1086
1567
 
1087
- if (object === ObjectPrototype) {
1088
- return true;
1089
- } // It might be an object from a different Realm which is
1090
- // still just a plain simple object.
1568
+ function createModelResolver(chunk, parentObject, key, cyclic) {
1569
+ var blocked;
1091
1570
 
1571
+ if (initializingChunkBlockedModel) {
1572
+ blocked = initializingChunkBlockedModel;
1092
1573
 
1093
- if (Object.getPrototypeOf(object)) {
1094
- return false;
1574
+ if (!cyclic) {
1575
+ blocked.deps++;
1576
+ }
1577
+ } else {
1578
+ blocked = initializingChunkBlockedModel = {
1579
+ deps: cyclic ? 0 : 1,
1580
+ value: null
1581
+ };
1095
1582
  }
1096
1583
 
1097
- var names = Object.getOwnPropertyNames(object);
1584
+ return function (value) {
1585
+ parentObject[key] = value;
1586
+ blocked.deps--;
1098
1587
 
1099
- for (var i = 0; i < names.length; i++) {
1100
- if (!(names[i] in ObjectPrototype)) {
1101
- return false;
1102
- }
1103
- }
1588
+ if (blocked.deps === 0) {
1589
+ if (chunk.status !== BLOCKED) {
1590
+ return;
1591
+ }
1104
1592
 
1105
- return true;
1593
+ var resolveListeners = chunk.value;
1594
+ var initializedChunk = chunk;
1595
+ initializedChunk.status = INITIALIZED;
1596
+ initializedChunk.value = blocked.value;
1597
+
1598
+ if (resolveListeners !== null) {
1599
+ wakeChunk(resolveListeners, blocked.value);
1600
+ }
1601
+ }
1602
+ };
1106
1603
  }
1107
1604
 
1108
- function isSimpleObject(object) {
1109
- if (!isObjectPrototype(Object.getPrototypeOf(object))) {
1110
- return false;
1111
- }
1605
+ function createModelReject(chunk) {
1606
+ return function (error) {
1607
+ return triggerErrorOnChunk(chunk, error);
1608
+ };
1609
+ }
1112
1610
 
1113
- var names = Object.getOwnPropertyNames(object);
1611
+ function createServerReferenceProxy(response, metaData) {
1612
+ var callServer = response._callServer;
1114
1613
 
1115
- for (var i = 0; i < names.length; i++) {
1116
- var descriptor = Object.getOwnPropertyDescriptor(object, names[i]);
1614
+ var proxy = function () {
1615
+ // $FlowFixMe[method-unbinding]
1616
+ var args = Array.prototype.slice.call(arguments);
1617
+ var p = metaData.bound;
1117
1618
 
1118
- if (!descriptor) {
1119
- return false;
1619
+ if (!p) {
1620
+ return callServer(metaData.id, args);
1120
1621
  }
1121
1622
 
1122
- if (!descriptor.enumerable) {
1123
- if ((names[i] === 'key' || names[i] === 'ref') && typeof descriptor.get === 'function') {
1124
- // React adds key and ref getters to props objects to issue warnings.
1125
- // Those getters will not be transferred to the client, but that's ok,
1126
- // so we'll special case them.
1127
- continue;
1128
- }
1623
+ if (p.status === INITIALIZED) {
1624
+ var bound = p.value;
1625
+ return callServer(metaData.id, bound.concat(args));
1626
+ } // Since this is a fake Promise whose .then doesn't chain, we have to wrap it.
1627
+ // TODO: Remove the wrapper once that's fixed.
1129
1628
 
1130
- return false;
1131
- }
1132
- }
1133
1629
 
1134
- return true;
1135
- }
1136
- function objectName(object) {
1137
- // $FlowFixMe[method-unbinding]
1138
- var name = Object.prototype.toString.call(object);
1139
- return name.replace(/^\[object (.*)\]$/, function (m, p0) {
1140
- return p0;
1141
- });
1142
- }
1630
+ return Promise.resolve(p).then(function (bound) {
1631
+ return callServer(metaData.id, bound.concat(args));
1632
+ });
1633
+ };
1143
1634
 
1144
- function describeKeyForErrorMessage(key) {
1145
- var encodedKey = JSON.stringify(key);
1146
- return '"' + key + '"' === encodedKey ? key : encodedKey;
1635
+ registerServerReference(proxy, metaData);
1636
+ return proxy;
1147
1637
  }
1148
1638
 
1149
- function describeValueForErrorMessage(value) {
1150
- switch (typeof value) {
1151
- case 'string':
1152
- {
1153
- return JSON.stringify(value.length <= 10 ? value : value.slice(0, 10) + '...');
1154
- }
1155
-
1156
- case 'object':
1157
- {
1158
- if (isArray(value)) {
1159
- return '[...]';
1160
- }
1639
+ function getOutlinedModel(response, id) {
1640
+ var chunk = getChunk(response, id);
1161
1641
 
1162
- var name = objectName(value);
1642
+ switch (chunk.status) {
1643
+ case RESOLVED_MODEL:
1644
+ initializeModelChunk(chunk);
1645
+ break;
1646
+ } // The status might have changed after initialization.
1163
1647
 
1164
- if (name === 'Object') {
1165
- return '{...}';
1166
- }
1167
1648
 
1168
- return name;
1649
+ switch (chunk.status) {
1650
+ case INITIALIZED:
1651
+ {
1652
+ return chunk.value;
1169
1653
  }
1170
-
1171
- case 'function':
1172
- return 'function';
1654
+ // We always encode it first in the stream so it won't be pending.
1173
1655
 
1174
1656
  default:
1175
- // eslint-disable-next-line react-internal/safe-string-coercion
1176
- return String(value);
1657
+ throw chunk.reason;
1177
1658
  }
1178
1659
  }
1179
1660
 
1180
- function describeElementType(type) {
1181
- if (typeof type === 'string') {
1182
- return type;
1183
- }
1184
-
1185
- switch (type) {
1186
- case REACT_SUSPENSE_TYPE:
1187
- return 'Suspense';
1188
-
1189
- case REACT_SUSPENSE_LIST_TYPE:
1190
- return 'SuspenseList';
1191
- }
1192
-
1193
- if (typeof type === 'object') {
1194
- switch (type.$$typeof) {
1195
- case REACT_FORWARD_REF_TYPE:
1196
- return describeElementType(type.render);
1197
-
1198
- case REACT_MEMO_TYPE:
1199
- return describeElementType(type.type);
1661
+ function parseModelString(response, parentObject, key, value) {
1662
+ if (value[0] === '$') {
1663
+ if (value === '$') {
1664
+ // A very common symbol.
1665
+ return REACT_ELEMENT_TYPE;
1666
+ }
1200
1667
 
1201
- case REACT_LAZY_TYPE:
1668
+ switch (value[1]) {
1669
+ case '$':
1202
1670
  {
1203
- var lazyComponent = type;
1204
- var payload = lazyComponent._payload;
1205
- var init = lazyComponent._init;
1671
+ // This was an escaped string value.
1672
+ return value.slice(1);
1673
+ }
1206
1674
 
1207
- try {
1208
- // Lazy may contain any component type so we recursively resolve it.
1209
- return describeElementType(init(payload));
1210
- } catch (x) {}
1675
+ case 'L':
1676
+ {
1677
+ // Lazy node
1678
+ var id = parseInt(value.slice(2), 16);
1679
+ var chunk = getChunk(response, id); // We create a React.lazy wrapper around any lazy values.
1680
+ // When passed into React, we'll know how to suspend on this.
1681
+
1682
+ return createLazyChunkWrapper(chunk);
1211
1683
  }
1212
- }
1213
- }
1214
1684
 
1215
- return '';
1216
- }
1685
+ case '@':
1686
+ {
1687
+ // Promise
1688
+ if (value.length === 2) {
1689
+ // Infinite promise that never resolves.
1690
+ return new Promise(function () {});
1691
+ }
1217
1692
 
1218
- function describeObjectForErrorMessage(objectOrArray, expandedName) {
1219
- var objKind = objectName(objectOrArray);
1693
+ var _id = parseInt(value.slice(2), 16);
1220
1694
 
1221
- if (objKind !== 'Object' && objKind !== 'Array') {
1222
- return objKind;
1223
- }
1695
+ var _chunk = getChunk(response, _id);
1224
1696
 
1225
- var str = '';
1226
- var start = -1;
1227
- var length = 0;
1697
+ return _chunk;
1698
+ }
1228
1699
 
1229
- if (isArray(objectOrArray)) {
1230
- if (jsxChildrenParents.has(objectOrArray)) {
1231
- // Print JSX Children
1232
- var type = jsxChildrenParents.get(objectOrArray);
1233
- str = '<' + describeElementType(type) + '>';
1234
- var array = objectOrArray;
1700
+ case 'S':
1701
+ {
1702
+ // Symbol
1703
+ return Symbol.for(value.slice(2));
1704
+ }
1235
1705
 
1236
- for (var i = 0; i < array.length; i++) {
1237
- var value = array[i];
1238
- var substr = void 0;
1706
+ case 'F':
1707
+ {
1708
+ // Server Reference
1709
+ var _id2 = parseInt(value.slice(2), 16);
1239
1710
 
1240
- if (typeof value === 'string') {
1241
- substr = value;
1242
- } else if (typeof value === 'object' && value !== null) {
1243
- // $FlowFixMe[incompatible-call] found when upgrading Flow
1244
- substr = '{' + describeObjectForErrorMessage(value) + '}';
1245
- } else {
1246
- substr = '{' + describeValueForErrorMessage(value) + '}';
1711
+ var metadata = getOutlinedModel(response, _id2);
1712
+ return createServerReferenceProxy(response, metadata);
1247
1713
  }
1248
1714
 
1249
- if ('' + i === expandedName) {
1250
- start = str.length;
1251
- length = substr.length;
1252
- str += substr;
1253
- } else if (substr.length < 15 && str.length + substr.length < 40) {
1254
- str += substr;
1255
- } else {
1256
- str += '{...}';
1715
+ case 'T':
1716
+ {
1717
+ // Temporary Reference
1718
+ var _id3 = parseInt(value.slice(2), 16);
1719
+
1720
+ var temporaryReferences = response._tempRefs;
1721
+
1722
+ if (temporaryReferences == null) {
1723
+ throw new Error('Missing a temporary reference set but the RSC response returned a temporary reference. ' + 'Pass a temporaryReference option with the set that was used with the reply.');
1724
+ }
1725
+
1726
+ return readTemporaryReference(temporaryReferences, _id3);
1257
1727
  }
1258
- }
1259
1728
 
1260
- str += '</' + describeElementType(type) + '>';
1261
- } else {
1262
- // Print Array
1263
- str = '[';
1264
- var _array = objectOrArray;
1729
+ case 'Q':
1730
+ {
1731
+ // Map
1732
+ var _id4 = parseInt(value.slice(2), 16);
1265
1733
 
1266
- for (var _i = 0; _i < _array.length; _i++) {
1267
- if (_i > 0) {
1268
- str += ', ';
1734
+ var data = getOutlinedModel(response, _id4);
1735
+ return new Map(data);
1269
1736
  }
1270
1737
 
1271
- var _value = _array[_i];
1738
+ case 'W':
1739
+ {
1740
+ // Set
1741
+ var _id5 = parseInt(value.slice(2), 16);
1272
1742
 
1273
- var _substr = void 0;
1743
+ var _data = getOutlinedModel(response, _id5);
1274
1744
 
1275
- if (typeof _value === 'object' && _value !== null) {
1276
- // $FlowFixMe[incompatible-call] found when upgrading Flow
1277
- _substr = describeObjectForErrorMessage(_value);
1278
- } else {
1279
- _substr = describeValueForErrorMessage(_value);
1745
+ return new Set(_data);
1280
1746
  }
1281
1747
 
1282
- if ('' + _i === expandedName) {
1283
- start = str.length;
1284
- length = _substr.length;
1285
- str += _substr;
1286
- } else if (_substr.length < 10 && str.length + _substr.length < 40) {
1287
- str += _substr;
1288
- } else {
1289
- str += '...';
1748
+ case 'I':
1749
+ {
1750
+ // $Infinity
1751
+ return Infinity;
1290
1752
  }
1291
- }
1292
-
1293
- str += ']';
1294
- }
1295
- } else {
1296
- if (objectOrArray.$$typeof === REACT_ELEMENT_TYPE) {
1297
- str = '<' + describeElementType(objectOrArray.type) + '/>';
1298
- } else if (jsxPropsParents.has(objectOrArray)) {
1299
- // Print JSX
1300
- var _type = jsxPropsParents.get(objectOrArray);
1301
1753
 
1302
- str = '<' + (describeElementType(_type) || '...');
1303
- var object = objectOrArray;
1304
- var names = Object.keys(object);
1754
+ case '-':
1755
+ {
1756
+ // $-0 or $-Infinity
1757
+ if (value === '$-0') {
1758
+ return -0;
1759
+ } else {
1760
+ return -Infinity;
1761
+ }
1762
+ }
1305
1763
 
1306
- for (var _i2 = 0; _i2 < names.length; _i2++) {
1307
- str += ' ';
1308
- var name = names[_i2];
1309
- str += describeKeyForErrorMessage(name) + '=';
1310
- var _value2 = object[name];
1764
+ case 'N':
1765
+ {
1766
+ // $NaN
1767
+ return NaN;
1768
+ }
1311
1769
 
1312
- var _substr2 = void 0;
1770
+ case 'u':
1771
+ {
1772
+ // matches "$undefined"
1773
+ // Special encoding for `undefined` which can't be serialized as JSON otherwise.
1774
+ return undefined;
1775
+ }
1313
1776
 
1314
- if (name === expandedName && typeof _value2 === 'object' && _value2 !== null) {
1315
- // $FlowFixMe[incompatible-call] found when upgrading Flow
1316
- _substr2 = describeObjectForErrorMessage(_value2);
1317
- } else {
1318
- _substr2 = describeValueForErrorMessage(_value2);
1777
+ case 'D':
1778
+ {
1779
+ // Date
1780
+ return new Date(Date.parse(value.slice(2)));
1319
1781
  }
1320
1782
 
1321
- if (typeof _value2 !== 'string') {
1322
- _substr2 = '{' + _substr2 + '}';
1783
+ case 'n':
1784
+ {
1785
+ // BigInt
1786
+ return BigInt(value.slice(2));
1323
1787
  }
1324
1788
 
1325
- if (name === expandedName) {
1326
- start = str.length;
1327
- length = _substr2.length;
1328
- str += _substr2;
1329
- } else if (_substr2.length < 10 && str.length + _substr2.length < 40) {
1330
- str += _substr2;
1331
- } else {
1332
- str += '...';
1789
+ case 'E':
1790
+ {
1791
+ {
1792
+ // In DEV mode we allow indirect eval to produce functions for logging.
1793
+ // This should not compile to eval() because then it has local scope access.
1794
+ try {
1795
+ // eslint-disable-next-line no-eval
1796
+ return (0, eval)(value.slice(2));
1797
+ } catch (x) {
1798
+ // We currently use this to express functions so we fail parsing it,
1799
+ // let's just return a blank function as a place holder.
1800
+ return function () {};
1801
+ }
1802
+ } // Fallthrough
1803
+
1333
1804
  }
1334
- }
1335
1805
 
1336
- str += '>';
1337
- } else {
1338
- // Print Object
1339
- str = '{';
1340
- var _object = objectOrArray;
1806
+ default:
1807
+ {
1808
+ // We assume that anything else is a reference ID.
1809
+ var _id6 = parseInt(value.slice(1), 16);
1341
1810
 
1342
- var _names = Object.keys(_object);
1811
+ var _chunk2 = getChunk(response, _id6);
1343
1812
 
1344
- for (var _i3 = 0; _i3 < _names.length; _i3++) {
1345
- if (_i3 > 0) {
1346
- str += ', ';
1347
- }
1813
+ switch (_chunk2.status) {
1814
+ case RESOLVED_MODEL:
1815
+ initializeModelChunk(_chunk2);
1816
+ break;
1348
1817
 
1349
- var _name = _names[_i3];
1350
- str += describeKeyForErrorMessage(_name) + ': ';
1351
- var _value3 = _object[_name];
1818
+ case RESOLVED_MODULE:
1819
+ initializeModuleChunk(_chunk2);
1820
+ break;
1821
+ } // The status might have changed after initialization.
1352
1822
 
1353
- var _substr3 = void 0;
1354
1823
 
1355
- if (typeof _value3 === 'object' && _value3 !== null) {
1356
- // $FlowFixMe[incompatible-call] found when upgrading Flow
1357
- _substr3 = describeObjectForErrorMessage(_value3);
1358
- } else {
1359
- _substr3 = describeValueForErrorMessage(_value3);
1360
- }
1824
+ switch (_chunk2.status) {
1825
+ case INITIALIZED:
1826
+ var chunkValue = _chunk2.value;
1827
+
1828
+ if (_chunk2._debugInfo) {
1829
+ // If we have a direct reference to an object that was rendered by a synchronous
1830
+ // server component, it might have some debug info about how it was rendered.
1831
+ // We forward this to the underlying object. This might be a React Element or
1832
+ // an Array fragment.
1833
+ // If this was a string / number return value we lose the debug info. We choose
1834
+ // that tradeoff to allow sync server components to return plain values and not
1835
+ // use them as React Nodes necessarily. We could otherwise wrap them in a Lazy.
1836
+ if (typeof chunkValue === 'object' && chunkValue !== null && (Array.isArray(chunkValue) || chunkValue.$$typeof === REACT_ELEMENT_TYPE) && !chunkValue._debugInfo) {
1837
+ // We should maybe use a unique symbol for arrays but this is a React owned array.
1838
+ // $FlowFixMe[prop-missing]: This should be added to elements.
1839
+ Object.defineProperty(chunkValue, '_debugInfo', {
1840
+ configurable: false,
1841
+ enumerable: false,
1842
+ writable: true,
1843
+ value: _chunk2._debugInfo
1844
+ });
1845
+ }
1846
+ }
1361
1847
 
1362
- if (_name === expandedName) {
1363
- start = str.length;
1364
- length = _substr3.length;
1365
- str += _substr3;
1366
- } else if (_substr3.length < 10 && str.length + _substr3.length < 40) {
1367
- str += _substr3;
1368
- } else {
1369
- str += '...';
1370
- }
1371
- }
1848
+ return chunkValue;
1372
1849
 
1373
- str += '}';
1850
+ case PENDING:
1851
+ case BLOCKED:
1852
+ case CYCLIC:
1853
+ var parentChunk = initializingChunk;
1854
+
1855
+ _chunk2.then(createModelResolver(parentChunk, parentObject, key, _chunk2.status === CYCLIC), createModelReject(parentChunk));
1856
+
1857
+ return null;
1858
+
1859
+ default:
1860
+ throw _chunk2.reason;
1861
+ }
1862
+ }
1374
1863
  }
1375
1864
  }
1376
1865
 
1377
- if (expandedName === undefined) {
1378
- return str;
1379
- }
1866
+ return value;
1867
+ }
1380
1868
 
1381
- if (start > -1 && length > 0) {
1382
- var highlight = ' '.repeat(start) + '^'.repeat(length);
1383
- return '\n ' + str + '\n ' + highlight;
1869
+ function parseModelTuple(response, value) {
1870
+ var tuple = value;
1871
+
1872
+ if (tuple[0] === REACT_ELEMENT_TYPE) {
1873
+ // TODO: Consider having React just directly accept these arrays as elements.
1874
+ // Or even change the ReactElement type to be an array.
1875
+ return createElement(tuple[1], tuple[2], tuple[3]);
1384
1876
  }
1385
1877
 
1386
- return '\n ' + str;
1878
+ return value;
1387
1879
  }
1388
1880
 
1389
- // Thenable<ReactServerValue>
1390
- // function serializeByValueID(id: number): string {
1391
- // return '$' + id.toString(16);
1392
- // }
1393
-
1394
- function serializePromiseID(id) {
1395
- return '$@' + id.toString(16);
1881
+ function missingCall() {
1882
+ throw new Error('Trying to call a function from "use server" but the callServer option ' + 'was not implemented in your router runtime.');
1396
1883
  }
1397
1884
 
1398
- function serializeServerReferenceID(id) {
1399
- return '$F' + id.toString(16);
1885
+ function createResponse(bundlerConfig, moduleLoading, callServer, encodeFormAction, nonce, temporaryReferences) {
1886
+ var chunks = new Map();
1887
+ var response = {
1888
+ _bundlerConfig: bundlerConfig,
1889
+ _moduleLoading: moduleLoading,
1890
+ _callServer: callServer !== undefined ? callServer : missingCall,
1891
+ _encodeFormAction: encodeFormAction,
1892
+ _nonce: nonce,
1893
+ _chunks: chunks,
1894
+ _stringDecoder: createStringDecoder(),
1895
+ _fromJSON: null,
1896
+ _rowState: 0,
1897
+ _rowID: 0,
1898
+ _rowTag: 0,
1899
+ _rowLength: 0,
1900
+ _buffer: [],
1901
+ _tempRefs: temporaryReferences
1902
+ }; // Don't inline this call because it causes closure to outline the call above.
1903
+
1904
+ response._fromJSON = createFromJSONCallback(response);
1905
+ return response;
1400
1906
  }
1401
1907
 
1402
- function serializeSymbolReference(name) {
1403
- return '$S' + name;
1908
+ function resolveModel(response, id, model) {
1909
+ var chunks = response._chunks;
1910
+ var chunk = chunks.get(id);
1911
+
1912
+ if (!chunk) {
1913
+ chunks.set(id, createResolvedModelChunk(response, model));
1914
+ } else {
1915
+ resolveModelChunk(chunk, model);
1916
+ }
1404
1917
  }
1405
1918
 
1406
- function serializeFormDataReference(id) {
1407
- // Why K? F is "Function". D is "Date". What else?
1408
- return '$K' + id.toString(16);
1919
+ function resolveText(response, id, text) {
1920
+ var chunks = response._chunks; // We assume that we always reference large strings after they've been
1921
+ // emitted.
1922
+
1923
+ chunks.set(id, createInitializedTextChunk(response, text));
1409
1924
  }
1410
1925
 
1411
- function serializeNumber(number) {
1412
- if (Number.isFinite(number)) {
1413
- if (number === 0 && 1 / number === -Infinity) {
1414
- return '$-0';
1926
+ function resolveModule(response, id, model) {
1927
+ var chunks = response._chunks;
1928
+ var chunk = chunks.get(id);
1929
+ var clientReferenceMetadata = parseModel(response, model);
1930
+ var clientReference = resolveClientReference(response._bundlerConfig, clientReferenceMetadata);
1931
+ // For now we preload all modules as early as possible since it's likely
1932
+ // that we'll need them.
1933
+
1934
+ var promise = preloadModule(clientReference);
1935
+
1936
+ if (promise) {
1937
+ var blockedChunk;
1938
+
1939
+ if (!chunk) {
1940
+ // Technically, we should just treat promise as the chunk in this
1941
+ // case. Because it'll just behave as any other promise.
1942
+ blockedChunk = createBlockedChunk(response);
1943
+ chunks.set(id, blockedChunk);
1415
1944
  } else {
1416
- return number;
1945
+ // This can't actually happen because we don't have any forward
1946
+ // references to modules.
1947
+ blockedChunk = chunk;
1948
+ blockedChunk.status = BLOCKED;
1417
1949
  }
1950
+
1951
+ promise.then(function () {
1952
+ return resolveModuleChunk(blockedChunk, clientReference);
1953
+ }, function (error) {
1954
+ return triggerErrorOnChunk(blockedChunk, error);
1955
+ });
1418
1956
  } else {
1419
- if (number === Infinity) {
1420
- return '$Infinity';
1421
- } else if (number === -Infinity) {
1422
- return '$-Infinity';
1957
+ if (!chunk) {
1958
+ chunks.set(id, createResolvedModuleChunk(response, clientReference));
1423
1959
  } else {
1424
- return '$NaN';
1960
+ // This can't actually happen because we don't have any forward
1961
+ // references to modules.
1962
+ resolveModuleChunk(chunk, clientReference);
1425
1963
  }
1426
1964
  }
1427
1965
  }
1428
1966
 
1429
- function serializeUndefined() {
1430
- return '$undefined';
1431
- }
1967
+ function resolveErrorDev(response, id, digest, message, stack) {
1432
1968
 
1433
- function serializeDateFromDateJSON(dateJSON) {
1434
- // JSON.stringify automatically calls Date.prototype.toJSON which calls toISOString.
1435
- // We need only tack on a $D prefix.
1436
- return '$D' + dateJSON;
1437
- }
1438
1969
 
1439
- function serializeBigInt(n) {
1440
- return '$n' + n.toString(10);
1441
- }
1970
+ var error = new Error(message || 'An error occurred in the Server Components render but no message was provided');
1971
+ error.stack = stack;
1972
+ error.digest = digest;
1973
+ var errorWithDigest = error;
1974
+ var chunks = response._chunks;
1975
+ var chunk = chunks.get(id);
1442
1976
 
1443
- function escapeStringValue(value) {
1444
- if (value[0] === '$') {
1445
- // We need to escape $ prefixed strings since we use those to encode
1446
- // references to IDs and as special symbol values.
1447
- return '$' + value;
1977
+ if (!chunk) {
1978
+ chunks.set(id, createErrorChunk(response, errorWithDigest));
1448
1979
  } else {
1449
- return value;
1980
+ triggerErrorOnChunk(chunk, errorWithDigest);
1450
1981
  }
1451
1982
  }
1452
1983
 
1453
- function processReply(root, formFieldPrefix, resolve, reject) {
1454
- var nextPartId = 1;
1455
- var pendingParts = 0;
1456
- var formData = null;
1984
+ function resolveHint(response, code, model) {
1985
+ var hintModel = parseModel(response, model);
1986
+ dispatchHint(code, hintModel);
1987
+ }
1457
1988
 
1458
- function resolveToJSON(key, value) {
1459
- var parent = this; // Make sure that `parent[key]` wasn't JSONified before `value` was passed to us
1989
+ function resolveDebugInfo(response, id, debugInfo) {
1460
1990
 
1461
- {
1462
- // $FlowFixMe[incompatible-use]
1463
- var originalValue = parent[key];
1991
+ var chunk = getChunk(response, id);
1992
+ var chunkDebugInfo = chunk._debugInfo || (chunk._debugInfo = []);
1993
+ chunkDebugInfo.push(debugInfo);
1994
+ }
1464
1995
 
1465
- if (typeof originalValue === 'object' && originalValue !== value && !(originalValue instanceof Date)) {
1466
- if (objectName(originalValue) !== 'Object') {
1467
- error('Only plain objects can be passed to Server Functions from the Client. ' + '%s objects are not supported.%s', objectName(originalValue), describeObjectForErrorMessage(parent, key));
1468
- } else {
1469
- error('Only plain objects can be passed to Server Functions from the Client. ' + 'Objects with toJSON methods are not supported. Convert it manually ' + 'to a simple value before passing it to props.%s', describeObjectForErrorMessage(parent, key));
1470
- }
1471
- }
1472
- }
1996
+ function resolveConsoleEntry(response, value) {
1473
1997
 
1474
- if (value === null) {
1475
- return null;
1476
- }
1998
+ var payload = parseModel(response, value);
1999
+ var methodName = payload[0]; // TODO: Restore the fake stack before logging.
2000
+ // const stackTrace = payload[1];
1477
2001
 
1478
- if (typeof value === 'object') {
1479
- // $FlowFixMe[method-unbinding]
1480
- if (typeof value.then === 'function') {
1481
- // We assume that any object with a .then property is a "Thenable" type,
1482
- // or a Promise type. Either of which can be represented by a Promise.
1483
- if (formData === null) {
1484
- // Upgrade to use FormData to allow us to stream this value.
1485
- formData = new FormData();
1486
- }
2002
+ var env = payload[2];
2003
+ var args = payload.slice(3);
2004
+ printToConsole(methodName, args, env);
2005
+ }
1487
2006
 
1488
- pendingParts++;
1489
- var promiseId = nextPartId++;
1490
- var thenable = value;
1491
- thenable.then(function (partValue) {
1492
- var partJSON = JSON.stringify(partValue, resolveToJSON); // $FlowFixMe[incompatible-type] We know it's not null because we assigned it above.
2007
+ function processFullRow(response, id, tag, buffer, chunk) {
1493
2008
 
1494
- var data = formData; // eslint-disable-next-line react-internal/safe-string-coercion
2009
+ var stringDecoder = response._stringDecoder;
2010
+ var row = '';
1495
2011
 
1496
- data.append(formFieldPrefix + promiseId, partJSON);
1497
- pendingParts--;
2012
+ for (var i = 0; i < buffer.length; i++) {
2013
+ row += readPartialStringChunk(stringDecoder, buffer[i]);
2014
+ }
1498
2015
 
1499
- if (pendingParts === 0) {
1500
- resolve(data);
1501
- }
1502
- }, function (reason) {
1503
- // In the future we could consider serializing this as an error
1504
- // that throws on the server instead.
1505
- reject(reason);
1506
- });
1507
- return serializePromiseID(promiseId);
1508
- } // TODO: Should we the Object.prototype.toString.call() to test for cross-realm objects?
2016
+ row += readFinalStringChunk(stringDecoder, chunk);
1509
2017
 
2018
+ switch (tag) {
2019
+ case 73
2020
+ /* "I" */
2021
+ :
2022
+ {
2023
+ resolveModule(response, id, row);
2024
+ return;
2025
+ }
1510
2026
 
1511
- if (value instanceof FormData) {
1512
- if (formData === null) {
1513
- // Upgrade to use FormData to allow us to use rich objects as its values.
1514
- formData = new FormData();
1515
- }
2027
+ case 72
2028
+ /* "H" */
2029
+ :
2030
+ {
2031
+ var code = row[0];
2032
+ resolveHint(response, code, row.slice(1));
2033
+ return;
2034
+ }
1516
2035
 
1517
- var data = formData;
1518
- var refId = nextPartId++; // Copy all the form fields with a prefix for this reference.
1519
- // These must come first in the form order because we assume that all the
1520
- // fields are available before this is referenced.
2036
+ case 69
2037
+ /* "E" */
2038
+ :
2039
+ {
2040
+ var errorInfo = JSON.parse(row);
1521
2041
 
1522
- var prefix = formFieldPrefix + refId + '_'; // $FlowFixMe[prop-missing]: FormData has forEach.
2042
+ {
2043
+ resolveErrorDev(response, id, errorInfo.digest, errorInfo.message, errorInfo.stack);
2044
+ }
1523
2045
 
1524
- value.forEach(function (originalValue, originalKey) {
1525
- data.append(prefix + originalKey, originalValue);
1526
- });
1527
- return serializeFormDataReference(refId);
2046
+ return;
2047
+ }
2048
+
2049
+ case 84
2050
+ /* "T" */
2051
+ :
2052
+ {
2053
+ resolveText(response, id, row);
2054
+ return;
1528
2055
  }
1529
2056
 
1530
- if (!isArray(value)) {
1531
- var iteratorFn = getIteratorFn(value);
2057
+ case 68
2058
+ /* "D" */
2059
+ :
2060
+ {
2061
+ {
2062
+ var debugInfo = JSON.parse(row);
2063
+ resolveDebugInfo(response, id, debugInfo);
2064
+ return;
2065
+ } // Fallthrough to share the error with Console entries.
2066
+
2067
+ }
1532
2068
 
1533
- if (iteratorFn) {
1534
- return Array.from(value);
2069
+ case 87
2070
+ /* "W" */
2071
+ :
2072
+ {
2073
+ {
2074
+ resolveConsoleEntry(response, row);
2075
+ return;
1535
2076
  }
1536
2077
  }
1537
2078
 
2079
+ case 80
2080
+ /* "P" */
2081
+ :
2082
+ // Fallthrough
2083
+
2084
+ default:
2085
+ /* """ "{" "[" "t" "f" "n" "0" - "9" */
1538
2086
  {
1539
- if (value !== null && !isArray(value)) {
1540
- // Verify that this is a simple plain object.
1541
- if (value.$$typeof === REACT_ELEMENT_TYPE) {
1542
- error('React Element cannot be passed to Server Functions from the Client.%s', describeObjectForErrorMessage(parent, key));
1543
- } else if (value.$$typeof === REACT_LAZY_TYPE) {
1544
- error('React Lazy cannot be passed to Server Functions from the Client.%s', describeObjectForErrorMessage(parent, key));
1545
- } else if (value.$$typeof === REACT_PROVIDER_TYPE) {
1546
- error('React Context Providers cannot be passed to Server Functions from the Client.%s', describeObjectForErrorMessage(parent, key));
1547
- } else if (objectName(value) !== 'Object') {
1548
- error('Only plain objects can be passed to Client Components from Server Components. ' + '%s objects are not supported.%s', objectName(value), describeObjectForErrorMessage(parent, key));
1549
- } else if (!isSimpleObject(value)) {
1550
- error('Only plain objects can be passed to Client Components from Server Components. ' + 'Classes or other objects with methods are not supported.%s', describeObjectForErrorMessage(parent, key));
1551
- } else if (Object.getOwnPropertySymbols) {
1552
- var symbols = Object.getOwnPropertySymbols(value);
1553
-
1554
- if (symbols.length > 0) {
1555
- error('Only plain objects can be passed to Client Components from Server Components. ' + 'Objects with symbol properties like %s are not supported.%s', symbols[0].description, describeObjectForErrorMessage(parent, key));
1556
- }
2087
+ // We assume anything else is JSON.
2088
+ resolveModel(response, id, row);
2089
+ return;
2090
+ }
2091
+ }
2092
+ }
2093
+
2094
+ function processBinaryChunk(response, chunk) {
2095
+ var i = 0;
2096
+ var rowState = response._rowState;
2097
+ var rowID = response._rowID;
2098
+ var rowTag = response._rowTag;
2099
+ var rowLength = response._rowLength;
2100
+ var buffer = response._buffer;
2101
+ var chunkLength = chunk.length;
2102
+
2103
+ while (i < chunkLength) {
2104
+ var lastIdx = -1;
2105
+
2106
+ switch (rowState) {
2107
+ case ROW_ID:
2108
+ {
2109
+ var byte = chunk[i++];
2110
+
2111
+ if (byte === 58
2112
+ /* ":" */
2113
+ ) {
2114
+ // Finished the rowID, next we'll parse the tag.
2115
+ rowState = ROW_TAG;
2116
+ } else {
2117
+ rowID = rowID << 4 | (byte > 96 ? byte - 87 : byte - 48);
1557
2118
  }
2119
+
2120
+ continue;
1558
2121
  }
1559
- } // $FlowFixMe[incompatible-return]
1560
2122
 
2123
+ case ROW_TAG:
2124
+ {
2125
+ var resolvedRowTag = chunk[i];
2126
+
2127
+ if (resolvedRowTag === 84
2128
+ /* "T" */
2129
+ || enableBinaryFlight
2130
+ /* "V" */
2131
+ ) {
2132
+ rowTag = resolvedRowTag;
2133
+ rowState = ROW_LENGTH;
2134
+ i++;
2135
+ } else if (resolvedRowTag > 64 && resolvedRowTag < 91
2136
+ /* "A"-"Z" */
2137
+ ) {
2138
+ rowTag = resolvedRowTag;
2139
+ rowState = ROW_CHUNK_BY_NEWLINE;
2140
+ i++;
2141
+ } else {
2142
+ rowTag = 0;
2143
+ rowState = ROW_CHUNK_BY_NEWLINE; // This was an unknown tag so it was probably part of the data.
2144
+ }
1561
2145
 
1562
- return value;
1563
- }
2146
+ continue;
2147
+ }
1564
2148
 
1565
- if (typeof value === 'string') {
1566
- // TODO: Maybe too clever. If we support URL there's no similar trick.
1567
- if (value[value.length - 1] === 'Z') {
1568
- // Possibly a Date, whose toJSON automatically calls toISOString
1569
- // $FlowFixMe[incompatible-use]
1570
- var _originalValue = parent[key]; // $FlowFixMe[method-unbinding]
2149
+ case ROW_LENGTH:
2150
+ {
2151
+ var _byte = chunk[i++];
2152
+
2153
+ if (_byte === 44
2154
+ /* "," */
2155
+ ) {
2156
+ // Finished the rowLength, next we'll buffer up to that length.
2157
+ rowState = ROW_CHUNK_BY_LENGTH;
2158
+ } else {
2159
+ rowLength = rowLength << 4 | (_byte > 96 ? _byte - 87 : _byte - 48);
2160
+ }
1571
2161
 
1572
- if (_originalValue instanceof Date) {
1573
- return serializeDateFromDateJSON(value);
2162
+ continue;
1574
2163
  }
1575
- }
1576
2164
 
1577
- return escapeStringValue(value);
1578
- }
2165
+ case ROW_CHUNK_BY_NEWLINE:
2166
+ {
2167
+ // We're looking for a newline
2168
+ lastIdx = chunk.indexOf(10
2169
+ /* "\n" */
2170
+ , i);
2171
+ break;
2172
+ }
1579
2173
 
1580
- if (typeof value === 'boolean') {
1581
- return value;
1582
- }
2174
+ case ROW_CHUNK_BY_LENGTH:
2175
+ {
2176
+ // We're looking for the remaining byte length
2177
+ lastIdx = i + rowLength;
1583
2178
 
1584
- if (typeof value === 'number') {
1585
- return serializeNumber(value);
1586
- }
2179
+ if (lastIdx > chunk.length) {
2180
+ lastIdx = -1;
2181
+ }
1587
2182
 
1588
- if (typeof value === 'undefined') {
1589
- return serializeUndefined();
2183
+ break;
2184
+ }
1590
2185
  }
1591
2186
 
1592
- if (typeof value === 'function') {
1593
- var metaData = knownServerReferences.get(value);
1594
-
1595
- if (metaData !== undefined) {
1596
- var metaDataJSON = JSON.stringify(metaData, resolveToJSON);
2187
+ var offset = chunk.byteOffset + i;
1597
2188
 
1598
- if (formData === null) {
1599
- // Upgrade to use FormData to allow us to stream this value.
1600
- formData = new FormData();
1601
- } // The reference to this function came from the same client so we can pass it back.
2189
+ if (lastIdx > -1) {
2190
+ // We found the last chunk of the row
2191
+ var length = lastIdx - i;
2192
+ var lastChunk = new Uint8Array(chunk.buffer, offset, length);
2193
+ processFullRow(response, rowID, rowTag, buffer, lastChunk); // Reset state machine for a new row
1602
2194
 
2195
+ i = lastIdx;
1603
2196
 
1604
- var _refId = nextPartId++; // eslint-disable-next-line react-internal/safe-string-coercion
2197
+ if (rowState === ROW_CHUNK_BY_NEWLINE) {
2198
+ // If we're trailing by a newline we need to skip it.
2199
+ i++;
2200
+ }
1605
2201
 
2202
+ rowState = ROW_ID;
2203
+ rowTag = 0;
2204
+ rowID = 0;
2205
+ rowLength = 0;
2206
+ buffer.length = 0;
2207
+ } else {
2208
+ // The rest of this row is in a future chunk. We stash the rest of the
2209
+ // current chunk until we can process the full row.
2210
+ var _length = chunk.byteLength - i;
1606
2211
 
1607
- formData.set(formFieldPrefix + _refId, metaDataJSON);
1608
- return serializeServerReferenceID(_refId);
1609
- }
2212
+ var remainingSlice = new Uint8Array(chunk.buffer, offset, _length);
2213
+ buffer.push(remainingSlice); // Update how many bytes we're still waiting for. If we're looking for
2214
+ // a newline, this doesn't hurt since we'll just ignore it.
1610
2215
 
1611
- throw new Error('Client Functions cannot be passed directly to Server Functions. ' + 'Only Functions passed from the Server can be passed back again.');
2216
+ rowLength -= remainingSlice.byteLength;
2217
+ break;
1612
2218
  }
2219
+ }
1613
2220
 
1614
- if (typeof value === 'symbol') {
1615
- // $FlowFixMe[incompatible-type] `description` might be undefined
1616
- var name = value.description;
2221
+ response._rowState = rowState;
2222
+ response._rowID = rowID;
2223
+ response._rowTag = rowTag;
2224
+ response._rowLength = rowLength;
2225
+ }
1617
2226
 
1618
- if (Symbol.for(name) !== value) {
1619
- throw new Error('Only global symbols received from Symbol.for(...) can be passed to Server Functions. ' + ("The symbol Symbol.for(" + // $FlowFixMe[incompatible-type] `description` might be undefined
1620
- value.description + ") cannot be found among global symbols."));
1621
- }
2227
+ function parseModel(response, json) {
2228
+ return JSON.parse(json, response._fromJSON);
2229
+ }
1622
2230
 
1623
- return serializeSymbolReference(name);
2231
+ function createFromJSONCallback(response) {
2232
+ // $FlowFixMe[missing-this-annot]
2233
+ return function (key, value) {
2234
+ if (typeof value === 'string') {
2235
+ // We can't use .bind here because we need the "this" value.
2236
+ return parseModelString(response, this, key, value);
1624
2237
  }
1625
2238
 
1626
- if (typeof value === 'bigint') {
1627
- return serializeBigInt(value);
2239
+ if (typeof value === 'object' && value !== null) {
2240
+ return parseModelTuple(response, value);
1628
2241
  }
1629
2242
 
1630
- throw new Error("Type " + typeof value + " is not supported as an argument to a Server Function.");
1631
- } // $FlowFixMe[incompatible-type] it's not going to be undefined because we'll encode it.
1632
-
1633
-
1634
- var json = JSON.stringify(root, resolveToJSON);
1635
-
1636
- if (formData === null) {
1637
- // If it's a simple data structure, we just use plain JSON.
1638
- resolve(json);
1639
- } else {
1640
- // Otherwise, we use FormData to let us stream in the result.
1641
- formData.set(formFieldPrefix + '0', json);
2243
+ return value;
2244
+ };
2245
+ }
1642
2246
 
1643
- if (pendingParts === 0) {
1644
- // $FlowFixMe[incompatible-call] this has already been refined.
1645
- resolve(formData);
1646
- }
1647
- }
2247
+ function close(response) {
2248
+ // In case there are any remaining unresolved chunks, they won't
2249
+ // be resolved now. So we need to issue an error to those.
2250
+ // Ideally we should be able to early bail out if we kept a
2251
+ // ref count of pending chunks.
2252
+ reportGlobalError(response, new Error('Connection closed.'));
1648
2253
  }
1649
2254
 
1650
2255
  function createResponseFromOptions(options) {
1651
- return createResponse(null, options && options.callServer ? options.callServer : undefined);
2256
+ return createResponse(null, null, options && options.callServer ? options.callServer : undefined, undefined, // encodeFormAction
2257
+ undefined, // nonce
2258
+ options && options.temporaryReferences ? options.temporaryReferences : undefined);
1652
2259
  }
1653
2260
 
1654
2261
  function startReadingFromStream(response, stream) {
@@ -1691,45 +2298,18 @@ function createFromFetch(promiseForResponse, options) {
1691
2298
  return getRoot(response);
1692
2299
  }
1693
2300
 
1694
- function createFromXHR(request, options) {
1695
- var response = createResponseFromOptions(options);
1696
- var processedLength = 0;
1697
-
1698
- function progress(e) {
1699
- var chunk = request.responseText;
1700
- processStringChunk(response, chunk, processedLength);
1701
- processedLength = chunk.length;
1702
- }
1703
-
1704
- function load(e) {
1705
- progress();
1706
- close(response);
1707
- }
1708
-
1709
- function error(e) {
1710
- reportGlobalError(response, new TypeError('Network error'));
1711
- }
1712
-
1713
- request.addEventListener('progress', progress);
1714
- request.addEventListener('load', load);
1715
- request.addEventListener('error', error);
1716
- request.addEventListener('abort', error);
1717
- request.addEventListener('timeout', error);
1718
- return getRoot(response);
1719
- }
1720
-
1721
- function encodeReply(value)
2301
+ function encodeReply(value, options)
1722
2302
  /* We don't use URLSearchParams yet but maybe */
1723
2303
  {
1724
2304
  return new Promise(function (resolve, reject) {
1725
- processReply(value, '', resolve, reject);
2305
+ processReply(value, '', options && options.temporaryReferences ? options.temporaryReferences : undefined, resolve, reject);
1726
2306
  });
1727
2307
  }
1728
2308
 
1729
2309
  exports.createFromFetch = createFromFetch;
1730
2310
  exports.createFromReadableStream = createFromReadableStream;
1731
- exports.createFromXHR = createFromXHR;
1732
2311
  exports.createServerReference = createServerReference;
2312
+ exports.createTemporaryReferenceSet = createTemporaryReferenceSet;
1733
2313
  exports.encodeReply = encodeReply;
1734
2314
  })();
1735
2315
  }