react-server-dom-webpack 18.3.0-next-fa4314841-20230502 → 19.0.0-canary-05797cceb-20240328

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
@@ -0,0 +1,1986 @@
1
+ /**
2
+ * @license React
3
+ * react-server-dom-webpack-client.edge.production.min.js
4
+ *
5
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
6
+ *
7
+ * This source code is licensed under the MIT license found in the
8
+ * LICENSE file in the root directory of this source tree.
9
+ */
10
+
11
+ 'use strict';
12
+
13
+ var ReactDOM = require('react-dom');
14
+
15
+ // -----------------------------------------------------------------------------
16
+ const enableBinaryFlight = false;
17
+
18
+ function createStringDecoder() {
19
+ return new TextDecoder();
20
+ }
21
+ const decoderOptions = {
22
+ stream: true
23
+ };
24
+ function readPartialStringChunk(decoder, buffer) {
25
+ return decoder.decode(buffer, decoderOptions);
26
+ }
27
+ function readFinalStringChunk(decoder, buffer) {
28
+ return decoder.decode(buffer);
29
+ }
30
+
31
+ // This is the parsed shape of the wire format which is why it is
32
+ // condensed to only the essentialy information
33
+ const ID = 0;
34
+ const CHUNKS = 1;
35
+ const NAME = 2; // export const ASYNC = 3;
36
+ // This logic is correct because currently only include the 4th tuple member
37
+ // when the module is async. If that changes we will need to actually assert
38
+ // the value is true. We don't index into the 4th slot because flow does not
39
+ // like the potential out of bounds access
40
+
41
+ function isAsyncImport(metadata) {
42
+ return metadata.length === 4;
43
+ }
44
+
45
+ // The reason this function needs to defined here in this file instead of just
46
+ // being exported directly from the WebpackDestination... file is because the
47
+ // ClientReferenceMetadata is opaque and we can't unwrap it there.
48
+ // This should get inlined and we could also just implement an unwrapping function
49
+ // though that risks it getting used in places it shouldn't be. This is unfortunate
50
+ // but currently it seems to be the best option we have.
51
+
52
+ function prepareDestinationForModule(moduleLoading, nonce, metadata) {
53
+ prepareDestinationWithChunks(moduleLoading, metadata[CHUNKS], nonce);
54
+ }
55
+ function resolveClientReference(bundlerConfig, metadata) {
56
+ if (bundlerConfig) {
57
+ const moduleExports = bundlerConfig[metadata[ID]];
58
+ let resolvedModuleData = moduleExports[metadata[NAME]];
59
+ let name;
60
+
61
+ if (resolvedModuleData) {
62
+ // The potentially aliased name.
63
+ name = resolvedModuleData.name;
64
+ } else {
65
+ // If we don't have this specific name, we might have the full module.
66
+ resolvedModuleData = moduleExports['*'];
67
+
68
+ if (!resolvedModuleData) {
69
+ 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.');
70
+ }
71
+
72
+ name = metadata[NAME];
73
+ }
74
+
75
+ if (isAsyncImport(metadata)) {
76
+ return [resolvedModuleData.id, resolvedModuleData.chunks, name, 1
77
+ /* async */
78
+ ];
79
+ } else {
80
+ return [resolvedModuleData.id, resolvedModuleData.chunks, name];
81
+ }
82
+ }
83
+
84
+ return metadata;
85
+ }
86
+ // If they're still pending they're a thenable. This map also exists
87
+ // in Webpack but unfortunately it's not exposed so we have to
88
+ // replicate it in user space. null means that it has already loaded.
89
+
90
+ const chunkCache = new Map();
91
+
92
+ function requireAsyncModule(id) {
93
+ // We've already loaded all the chunks. We can require the module.
94
+ const promise = __webpack_require__(id);
95
+
96
+ if (typeof promise.then !== 'function') {
97
+ // This wasn't a promise after all.
98
+ return null;
99
+ } else if (promise.status === 'fulfilled') {
100
+ // This module was already resolved earlier.
101
+ return null;
102
+ } else {
103
+ // Instrument the Promise to stash the result.
104
+ promise.then(value => {
105
+ const fulfilledThenable = promise;
106
+ fulfilledThenable.status = 'fulfilled';
107
+ fulfilledThenable.value = value;
108
+ }, reason => {
109
+ const rejectedThenable = promise;
110
+ rejectedThenable.status = 'rejected';
111
+ rejectedThenable.reason = reason;
112
+ });
113
+ return promise;
114
+ }
115
+ }
116
+
117
+ function ignoreReject() {// We rely on rejected promises to be handled by another listener.
118
+ } // Start preloading the modules since we might need them soon.
119
+ // This function doesn't suspend.
120
+
121
+
122
+ function preloadModule(metadata) {
123
+ const chunks = metadata[CHUNKS];
124
+ const promises = [];
125
+ let i = 0;
126
+
127
+ while (i < chunks.length) {
128
+ const chunkId = chunks[i++];
129
+ chunks[i++];
130
+ const entry = chunkCache.get(chunkId);
131
+
132
+ if (entry === undefined) {
133
+ const thenable = loadChunk(chunkId);
134
+ promises.push(thenable); // $FlowFixMe[method-unbinding]
135
+
136
+ const resolve = chunkCache.set.bind(chunkCache, chunkId, null);
137
+ thenable.then(resolve, ignoreReject);
138
+ chunkCache.set(chunkId, thenable);
139
+ } else if (entry !== null) {
140
+ promises.push(entry);
141
+ }
142
+ }
143
+
144
+ if (isAsyncImport(metadata)) {
145
+ if (promises.length === 0) {
146
+ return requireAsyncModule(metadata[ID]);
147
+ } else {
148
+ return Promise.all(promises).then(() => {
149
+ return requireAsyncModule(metadata[ID]);
150
+ });
151
+ }
152
+ } else if (promises.length > 0) {
153
+ return Promise.all(promises);
154
+ } else {
155
+ return null;
156
+ }
157
+ } // Actually require the module or suspend if it's not yet ready.
158
+ // Increase priority if necessary.
159
+
160
+ function requireModule(metadata) {
161
+ let moduleExports = __webpack_require__(metadata[ID]);
162
+
163
+ if (isAsyncImport(metadata)) {
164
+ if (typeof moduleExports.then !== 'function') ; else if (moduleExports.status === 'fulfilled') {
165
+ // This Promise should've been instrumented by preloadModule.
166
+ moduleExports = moduleExports.value;
167
+ } else {
168
+ throw moduleExports.reason;
169
+ }
170
+ }
171
+
172
+ if (metadata[NAME] === '*') {
173
+ // This is a placeholder value that represents that the caller imported this
174
+ // as a CommonJS module as is.
175
+ return moduleExports;
176
+ }
177
+
178
+ if (metadata[NAME] === '') {
179
+ // This is a placeholder value that represents that the caller accessed the
180
+ // default property of this if it was an ESM interop module.
181
+ return moduleExports.__esModule ? moduleExports.default : moduleExports;
182
+ }
183
+
184
+ return moduleExports[metadata[NAME]];
185
+ }
186
+
187
+ function loadChunk(chunkId, filename) {
188
+ return __webpack_chunk_load__(chunkId);
189
+ }
190
+
191
+ function prepareDestinationWithChunks(moduleLoading, // Chunks are double-indexed [..., idx, filenamex, idy, filenamey, ...]
192
+ chunks, nonce) {
193
+ if (moduleLoading !== null) {
194
+ for (let i = 1; i < chunks.length; i += 2) {
195
+ preinitScriptForSSR(moduleLoading.prefix + chunks[i], nonce, moduleLoading.crossOrigin);
196
+ }
197
+ }
198
+ }
199
+
200
+ const ReactDOMSharedInternals = ReactDOM.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED;
201
+
202
+ function getCrossOriginString(input) {
203
+ if (typeof input === 'string') {
204
+ return input === 'use-credentials' ? input : '';
205
+ }
206
+
207
+ return undefined;
208
+ }
209
+
210
+ // This client file is in the shared folder because it applies to both SSR and browser contexts.
211
+ const ReactDOMCurrentDispatcher = ReactDOMSharedInternals.ReactDOMCurrentDispatcher;
212
+ function dispatchHint(code, model) {
213
+ const dispatcher = ReactDOMCurrentDispatcher.current;
214
+
215
+ switch (code) {
216
+ case 'D':
217
+ {
218
+ const refined = refineModel(code, model);
219
+ const href = refined;
220
+ dispatcher.prefetchDNS(href);
221
+ return;
222
+ }
223
+
224
+ case 'C':
225
+ {
226
+ const refined = refineModel(code, model);
227
+
228
+ if (typeof refined === 'string') {
229
+ const href = refined;
230
+ dispatcher.preconnect(href);
231
+ } else {
232
+ const href = refined[0];
233
+ const crossOrigin = refined[1];
234
+ dispatcher.preconnect(href, crossOrigin);
235
+ }
236
+
237
+ return;
238
+ }
239
+
240
+ case 'L':
241
+ {
242
+ const refined = refineModel(code, model);
243
+ const href = refined[0];
244
+ const as = refined[1];
245
+
246
+ if (refined.length === 3) {
247
+ const options = refined[2];
248
+ dispatcher.preload(href, as, options);
249
+ } else {
250
+ dispatcher.preload(href, as);
251
+ }
252
+
253
+ return;
254
+ }
255
+
256
+ case 'm':
257
+ {
258
+ const refined = refineModel(code, model);
259
+
260
+ if (typeof refined === 'string') {
261
+ const href = refined;
262
+ dispatcher.preloadModule(href);
263
+ } else {
264
+ const href = refined[0];
265
+ const options = refined[1];
266
+ dispatcher.preloadModule(href, options);
267
+ }
268
+
269
+ return;
270
+ }
271
+
272
+ case 'S':
273
+ {
274
+ const refined = refineModel(code, model);
275
+
276
+ if (typeof refined === 'string') {
277
+ const href = refined;
278
+ dispatcher.preinitStyle(href);
279
+ } else {
280
+ const href = refined[0];
281
+ const precedence = refined[1] === 0 ? undefined : refined[1];
282
+ const options = refined.length === 3 ? refined[2] : undefined;
283
+ dispatcher.preinitStyle(href, precedence, options);
284
+ }
285
+
286
+ return;
287
+ }
288
+
289
+ case 'X':
290
+ {
291
+ const refined = refineModel(code, model);
292
+
293
+ if (typeof refined === 'string') {
294
+ const href = refined;
295
+ dispatcher.preinitScript(href);
296
+ } else {
297
+ const href = refined[0];
298
+ const options = refined[1];
299
+ dispatcher.preinitScript(href, options);
300
+ }
301
+
302
+ return;
303
+ }
304
+
305
+ case 'M':
306
+ {
307
+ const refined = refineModel(code, model);
308
+
309
+ if (typeof refined === 'string') {
310
+ const href = refined;
311
+ dispatcher.preinitModuleScript(href);
312
+ } else {
313
+ const href = refined[0];
314
+ const options = refined[1];
315
+ dispatcher.preinitModuleScript(href, options);
316
+ }
317
+
318
+ return;
319
+ }
320
+ }
321
+ } // Flow is having trouble refining the HintModels so we help it a bit.
322
+ // This should be compiled out in the production build.
323
+
324
+ function refineModel(code, model) {
325
+ return model;
326
+ }
327
+ function preinitScriptForSSR(href, nonce, crossOrigin) {
328
+ const dispatcher = ReactDOMCurrentDispatcher.current;
329
+
330
+ if (dispatcher) {
331
+ dispatcher.preinitScript(href, {
332
+ crossOrigin: getCrossOriginString(crossOrigin),
333
+ nonce
334
+ });
335
+ }
336
+ }
337
+
338
+ // ATTENTION
339
+ // When adding new symbols to this file,
340
+ // Please consider also adding to 'react-devtools-shared/src/backend/ReactSymbols'
341
+ // The Symbol used to tag the ReactElement-like types.
342
+ const REACT_ELEMENT_TYPE = Symbol.for('react.element');
343
+ const REACT_LAZY_TYPE = Symbol.for('react.lazy');
344
+ const MAYBE_ITERATOR_SYMBOL = Symbol.iterator;
345
+ const FAUX_ITERATOR_SYMBOL = '@@iterator';
346
+ function getIteratorFn(maybeIterable) {
347
+ if (maybeIterable === null || typeof maybeIterable !== 'object') {
348
+ return null;
349
+ }
350
+
351
+ const maybeIterator = MAYBE_ITERATOR_SYMBOL && maybeIterable[MAYBE_ITERATOR_SYMBOL] || maybeIterable[FAUX_ITERATOR_SYMBOL];
352
+
353
+ if (typeof maybeIterator === 'function') {
354
+ return maybeIterator;
355
+ }
356
+
357
+ return null;
358
+ }
359
+
360
+ const isArrayImpl = Array.isArray; // eslint-disable-next-line no-redeclare
361
+
362
+ function isArray(a) {
363
+ return isArrayImpl(a);
364
+ }
365
+
366
+ const getPrototypeOf = Object.getPrototypeOf;
367
+
368
+ function createTemporaryReferenceSet() {
369
+ return [];
370
+ }
371
+ function writeTemporaryReference(set, object) {
372
+ // We always create a new entry regardless if we've already written the same
373
+ // object. This ensures that we always generate a deterministic encoding of
374
+ // each slot in the reply for cacheability.
375
+ const newId = set.length;
376
+ set.push(object);
377
+ return newId;
378
+ }
379
+ function readTemporaryReference(set, id) {
380
+ if (id < 0 || id >= set.length) {
381
+ 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.');
382
+ }
383
+
384
+ return set[id];
385
+ }
386
+
387
+ const ObjectPrototype = Object.prototype;
388
+ const knownServerReferences = new WeakMap(); // Serializable values
389
+ // Thenable<ReactServerValue>
390
+
391
+ function serializeByValueID(id) {
392
+ return '$' + id.toString(16);
393
+ }
394
+
395
+ function serializePromiseID(id) {
396
+ return '$@' + id.toString(16);
397
+ }
398
+
399
+ function serializeServerReferenceID(id) {
400
+ return '$F' + id.toString(16);
401
+ }
402
+
403
+ function serializeTemporaryReferenceID(id) {
404
+ return '$T' + id.toString(16);
405
+ }
406
+
407
+ function serializeFormDataReference(id) {
408
+ // Why K? F is "Function". D is "Date". What else?
409
+ return '$K' + id.toString(16);
410
+ }
411
+
412
+ function serializeNumber(number) {
413
+ if (Number.isFinite(number)) {
414
+ if (number === 0 && 1 / number === -Infinity) {
415
+ return '$-0';
416
+ } else {
417
+ return number;
418
+ }
419
+ } else {
420
+ if (number === Infinity) {
421
+ return '$Infinity';
422
+ } else if (number === -Infinity) {
423
+ return '$-Infinity';
424
+ } else {
425
+ return '$NaN';
426
+ }
427
+ }
428
+ }
429
+
430
+ function serializeUndefined() {
431
+ return '$undefined';
432
+ }
433
+
434
+ function serializeDateFromDateJSON(dateJSON) {
435
+ // JSON.stringify automatically calls Date.prototype.toJSON which calls toISOString.
436
+ // We need only tack on a $D prefix.
437
+ return '$D' + dateJSON;
438
+ }
439
+
440
+ function serializeBigInt(n) {
441
+ return '$n' + n.toString(10);
442
+ }
443
+
444
+ function serializeMapID(id) {
445
+ return '$Q' + id.toString(16);
446
+ }
447
+
448
+ function serializeSetID(id) {
449
+ return '$W' + id.toString(16);
450
+ }
451
+
452
+ function escapeStringValue(value) {
453
+ if (value[0] === '$') {
454
+ // We need to escape $ prefixed strings since we use those to encode
455
+ // references to IDs and as special symbol values.
456
+ return '$' + value;
457
+ } else {
458
+ return value;
459
+ }
460
+ }
461
+
462
+ function processReply(root, formFieldPrefix, temporaryReferences, resolve, reject) {
463
+ let nextPartId = 1;
464
+ let pendingParts = 0;
465
+ let formData = null;
466
+
467
+ function resolveToJSON(key, value) {
468
+ const parent = this; // Make sure that `parent[key]` wasn't JSONified before `value` was passed to us
469
+
470
+ if (value === null) {
471
+ return null;
472
+ }
473
+
474
+ if (typeof value === 'object') {
475
+ switch (value.$$typeof) {
476
+ case REACT_ELEMENT_TYPE:
477
+ {
478
+ if (temporaryReferences === undefined) {
479
+ 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.' + (''));
480
+ }
481
+
482
+ return serializeTemporaryReferenceID(writeTemporaryReference(temporaryReferences, value));
483
+ }
484
+
485
+ case REACT_LAZY_TYPE:
486
+ {
487
+ // Resolve lazy as if it wasn't here. In the future this will be encoded as a Promise.
488
+ const lazy = value;
489
+ const payload = lazy._payload;
490
+ const init = lazy._init;
491
+
492
+ if (formData === null) {
493
+ // Upgrade to use FormData to allow us to stream this value.
494
+ formData = new FormData();
495
+ }
496
+
497
+ pendingParts++;
498
+
499
+ try {
500
+ const resolvedModel = init(payload); // We always outline this as a separate part even though we could inline it
501
+ // because it ensures a more deterministic encoding.
502
+
503
+ const lazyId = nextPartId++;
504
+ const partJSON = JSON.stringify(resolvedModel, resolveToJSON); // $FlowFixMe[incompatible-type] We know it's not null because we assigned it above.
505
+
506
+ const data = formData; // eslint-disable-next-line react-internal/safe-string-coercion
507
+
508
+ data.append(formFieldPrefix + lazyId, partJSON);
509
+ return serializeByValueID(lazyId);
510
+ } catch (x) {
511
+ if (typeof x === 'object' && x !== null && typeof x.then === 'function') {
512
+ // Suspended
513
+ pendingParts++;
514
+ const lazyId = nextPartId++;
515
+ const thenable = x;
516
+
517
+ const retry = function () {
518
+ // While the first promise resolved, its value isn't necessarily what we'll
519
+ // resolve into because we might suspend again.
520
+ try {
521
+ const partJSON = JSON.stringify(value, resolveToJSON); // $FlowFixMe[incompatible-type] We know it's not null because we assigned it above.
522
+
523
+ const data = formData; // eslint-disable-next-line react-internal/safe-string-coercion
524
+
525
+ data.append(formFieldPrefix + lazyId, partJSON);
526
+ pendingParts--;
527
+
528
+ if (pendingParts === 0) {
529
+ resolve(data);
530
+ }
531
+ } catch (reason) {
532
+ reject(reason);
533
+ }
534
+ };
535
+
536
+ thenable.then(retry, retry);
537
+ return serializeByValueID(lazyId);
538
+ } else {
539
+ // In the future we could consider serializing this as an error
540
+ // that throws on the server instead.
541
+ reject(x);
542
+ return null;
543
+ }
544
+ } finally {
545
+ pendingParts--;
546
+ }
547
+ }
548
+ } // $FlowFixMe[method-unbinding]
549
+
550
+
551
+ if (typeof value.then === 'function') {
552
+ // We assume that any object with a .then property is a "Thenable" type,
553
+ // or a Promise type. Either of which can be represented by a Promise.
554
+ if (formData === null) {
555
+ // Upgrade to use FormData to allow us to stream this value.
556
+ formData = new FormData();
557
+ }
558
+
559
+ pendingParts++;
560
+ const promiseId = nextPartId++;
561
+ const thenable = value;
562
+ thenable.then(partValue => {
563
+ try {
564
+ const partJSON = JSON.stringify(partValue, resolveToJSON); // $FlowFixMe[incompatible-type] We know it's not null because we assigned it above.
565
+
566
+ const data = formData; // eslint-disable-next-line react-internal/safe-string-coercion
567
+
568
+ data.append(formFieldPrefix + promiseId, partJSON);
569
+ pendingParts--;
570
+
571
+ if (pendingParts === 0) {
572
+ resolve(data);
573
+ }
574
+ } catch (reason) {
575
+ reject(reason);
576
+ }
577
+ }, reason => {
578
+ // In the future we could consider serializing this as an error
579
+ // that throws on the server instead.
580
+ reject(reason);
581
+ });
582
+ return serializePromiseID(promiseId);
583
+ }
584
+
585
+ if (isArray(value)) {
586
+ // $FlowFixMe[incompatible-return]
587
+ return value;
588
+ } // TODO: Should we the Object.prototype.toString.call() to test for cross-realm objects?
589
+
590
+
591
+ if (value instanceof FormData) {
592
+ if (formData === null) {
593
+ // Upgrade to use FormData to allow us to use rich objects as its values.
594
+ formData = new FormData();
595
+ }
596
+
597
+ const data = formData;
598
+ const refId = nextPartId++; // Copy all the form fields with a prefix for this reference.
599
+ // These must come first in the form order because we assume that all the
600
+ // fields are available before this is referenced.
601
+
602
+ const prefix = formFieldPrefix + refId + '_'; // $FlowFixMe[prop-missing]: FormData has forEach.
603
+
604
+ value.forEach((originalValue, originalKey) => {
605
+ data.append(prefix + originalKey, originalValue);
606
+ });
607
+ return serializeFormDataReference(refId);
608
+ }
609
+
610
+ if (value instanceof Map) {
611
+ const partJSON = JSON.stringify(Array.from(value), resolveToJSON);
612
+
613
+ if (formData === null) {
614
+ formData = new FormData();
615
+ }
616
+
617
+ const mapId = nextPartId++;
618
+ formData.append(formFieldPrefix + mapId, partJSON);
619
+ return serializeMapID(mapId);
620
+ }
621
+
622
+ if (value instanceof Set) {
623
+ const partJSON = JSON.stringify(Array.from(value), resolveToJSON);
624
+
625
+ if (formData === null) {
626
+ formData = new FormData();
627
+ }
628
+
629
+ const setId = nextPartId++;
630
+ formData.append(formFieldPrefix + setId, partJSON);
631
+ return serializeSetID(setId);
632
+ }
633
+
634
+ const iteratorFn = getIteratorFn(value);
635
+
636
+ if (iteratorFn) {
637
+ return Array.from(value);
638
+ } // Verify that this is a simple plain object.
639
+
640
+
641
+ const proto = getPrototypeOf(value);
642
+
643
+ if (proto !== ObjectPrototype && (proto === null || getPrototypeOf(proto) !== null)) {
644
+ if (temporaryReferences === undefined) {
645
+ throw new Error('Only plain objects, and a few built-ins, can be passed to Server Actions. ' + 'Classes or null prototypes are not supported.');
646
+ } // We can serialize class instances as temporary references.
647
+
648
+
649
+ return serializeTemporaryReferenceID(writeTemporaryReference(temporaryReferences, value));
650
+ }
651
+
652
+
653
+ return value;
654
+ }
655
+
656
+ if (typeof value === 'string') {
657
+ // TODO: Maybe too clever. If we support URL there's no similar trick.
658
+ if (value[value.length - 1] === 'Z') {
659
+ // Possibly a Date, whose toJSON automatically calls toISOString
660
+ // $FlowFixMe[incompatible-use]
661
+ const originalValue = parent[key];
662
+
663
+ if (originalValue instanceof Date) {
664
+ return serializeDateFromDateJSON(value);
665
+ }
666
+ }
667
+
668
+ return escapeStringValue(value);
669
+ }
670
+
671
+ if (typeof value === 'boolean') {
672
+ return value;
673
+ }
674
+
675
+ if (typeof value === 'number') {
676
+ return serializeNumber(value);
677
+ }
678
+
679
+ if (typeof value === 'undefined') {
680
+ return serializeUndefined();
681
+ }
682
+
683
+ if (typeof value === 'function') {
684
+ const metaData = knownServerReferences.get(value);
685
+
686
+ if (metaData !== undefined) {
687
+ const metaDataJSON = JSON.stringify(metaData, resolveToJSON);
688
+
689
+ if (formData === null) {
690
+ // Upgrade to use FormData to allow us to stream this value.
691
+ formData = new FormData();
692
+ } // The reference to this function came from the same client so we can pass it back.
693
+
694
+
695
+ const refId = nextPartId++; // eslint-disable-next-line react-internal/safe-string-coercion
696
+
697
+ formData.set(formFieldPrefix + refId, metaDataJSON);
698
+ return serializeServerReferenceID(refId);
699
+ }
700
+
701
+ if (temporaryReferences === undefined) {
702
+ throw new Error('Client Functions cannot be passed directly to Server Functions. ' + 'Only Functions passed from the Server can be passed back again.');
703
+ }
704
+
705
+ return serializeTemporaryReferenceID(writeTemporaryReference(temporaryReferences, value));
706
+ }
707
+
708
+ if (typeof value === 'symbol') {
709
+ if (temporaryReferences === undefined) {
710
+ throw new Error('Symbols cannot be passed to a Server Function without a ' + 'temporary reference set. Pass a TemporaryReferenceSet to the options.' + (''));
711
+ }
712
+
713
+ return serializeTemporaryReferenceID(writeTemporaryReference(temporaryReferences, value));
714
+ }
715
+
716
+ if (typeof value === 'bigint') {
717
+ return serializeBigInt(value);
718
+ }
719
+
720
+ throw new Error("Type " + typeof value + " is not supported as an argument to a Server Function.");
721
+ } // $FlowFixMe[incompatible-type] it's not going to be undefined because we'll encode it.
722
+
723
+
724
+ const json = JSON.stringify(root, resolveToJSON);
725
+
726
+ if (formData === null) {
727
+ // If it's a simple data structure, we just use plain JSON.
728
+ resolve(json);
729
+ } else {
730
+ // Otherwise, we use FormData to let us stream in the result.
731
+ formData.set(formFieldPrefix + '0', json);
732
+
733
+ if (pendingParts === 0) {
734
+ // $FlowFixMe[incompatible-call] this has already been refined.
735
+ resolve(formData);
736
+ }
737
+ }
738
+ }
739
+ const boundCache = new WeakMap();
740
+
741
+ function encodeFormData(reference) {
742
+ let resolve, reject; // We need to have a handle on the thenable so that we can synchronously set
743
+ // its status from processReply, when it can complete synchronously.
744
+
745
+ const thenable = new Promise((res, rej) => {
746
+ resolve = res;
747
+ reject = rej;
748
+ });
749
+ processReply(reference, '', undefined, // TODO: This means React Elements can't be used as state in progressive enhancement.
750
+ body => {
751
+ if (typeof body === 'string') {
752
+ const data = new FormData();
753
+ data.append('0', body);
754
+ body = data;
755
+ }
756
+
757
+ const fulfilled = thenable;
758
+ fulfilled.status = 'fulfilled';
759
+ fulfilled.value = body;
760
+ resolve(body);
761
+ }, e => {
762
+ const rejected = thenable;
763
+ rejected.status = 'rejected';
764
+ rejected.reason = e;
765
+ reject(e);
766
+ });
767
+ return thenable;
768
+ }
769
+
770
+ function defaultEncodeFormAction(identifierPrefix) {
771
+ const reference = knownServerReferences.get(this);
772
+
773
+ if (!reference) {
774
+ throw new Error('Tried to encode a Server Action from a different instance than the encoder is from. ' + 'This is a bug in React.');
775
+ }
776
+
777
+ let data = null;
778
+ let name;
779
+ const boundPromise = reference.bound;
780
+
781
+ if (boundPromise !== null) {
782
+ let thenable = boundCache.get(reference);
783
+
784
+ if (!thenable) {
785
+ thenable = encodeFormData(reference);
786
+ boundCache.set(reference, thenable);
787
+ }
788
+
789
+ if (thenable.status === 'rejected') {
790
+ throw thenable.reason;
791
+ } else if (thenable.status !== 'fulfilled') {
792
+ throw thenable;
793
+ }
794
+
795
+ const encodedFormData = thenable.value; // This is hacky but we need the identifier prefix to be added to
796
+ // all fields but the suspense cache would break since we might get
797
+ // a new identifier each time. So we just append it at the end instead.
798
+
799
+ const prefixedData = new FormData(); // $FlowFixMe[prop-missing]
800
+
801
+ encodedFormData.forEach((value, key) => {
802
+ prefixedData.append('$ACTION_' + identifierPrefix + ':' + key, value);
803
+ });
804
+ data = prefixedData; // We encode the name of the prefix containing the data.
805
+
806
+ name = '$ACTION_REF_' + identifierPrefix;
807
+ } else {
808
+ // This is the simple case so we can just encode the ID.
809
+ name = '$ACTION_ID_' + reference.id;
810
+ }
811
+
812
+ return {
813
+ name: name,
814
+ method: 'POST',
815
+ encType: 'multipart/form-data',
816
+ data: data
817
+ };
818
+ }
819
+
820
+ function customEncodeFormAction(proxy, identifierPrefix, encodeFormAction) {
821
+ const reference = knownServerReferences.get(proxy);
822
+
823
+ if (!reference) {
824
+ throw new Error('Tried to encode a Server Action from a different instance than the encoder is from. ' + 'This is a bug in React.');
825
+ }
826
+
827
+ let boundPromise = reference.bound;
828
+
829
+ if (boundPromise === null) {
830
+ boundPromise = Promise.resolve([]);
831
+ }
832
+
833
+ return encodeFormAction(reference.id, boundPromise);
834
+ }
835
+
836
+ function isSignatureEqual(referenceId, numberOfBoundArgs) {
837
+ const reference = knownServerReferences.get(this);
838
+
839
+ if (!reference) {
840
+ throw new Error('Tried to encode a Server Action from a different instance than the encoder is from. ' + 'This is a bug in React.');
841
+ }
842
+
843
+ if (reference.id !== referenceId) {
844
+ // These are different functions.
845
+ return false;
846
+ } // Now check if the number of bound arguments is the same.
847
+
848
+
849
+ const boundPromise = reference.bound;
850
+
851
+ if (boundPromise === null) {
852
+ // No bound arguments.
853
+ return numberOfBoundArgs === 0;
854
+ } // Unwrap the bound arguments array by suspending, if necessary. As with
855
+ // encodeFormData, this means isSignatureEqual can only be called while React
856
+ // is rendering.
857
+
858
+
859
+ switch (boundPromise.status) {
860
+ case 'fulfilled':
861
+ {
862
+ const boundArgs = boundPromise.value;
863
+ return boundArgs.length === numberOfBoundArgs;
864
+ }
865
+
866
+ case 'pending':
867
+ {
868
+ throw boundPromise;
869
+ }
870
+
871
+ case 'rejected':
872
+ {
873
+ throw boundPromise.reason;
874
+ }
875
+
876
+ default:
877
+ {
878
+ if (typeof boundPromise.status === 'string') ; else {
879
+ const pendingThenable = boundPromise;
880
+ pendingThenable.status = 'pending';
881
+ pendingThenable.then(boundArgs => {
882
+ const fulfilledThenable = boundPromise;
883
+ fulfilledThenable.status = 'fulfilled';
884
+ fulfilledThenable.value = boundArgs;
885
+ }, error => {
886
+ const rejectedThenable = boundPromise;
887
+ rejectedThenable.status = 'rejected';
888
+ rejectedThenable.reason = error;
889
+ });
890
+ }
891
+
892
+ throw boundPromise;
893
+ }
894
+ }
895
+ }
896
+
897
+ function registerServerReference(proxy, reference, encodeFormAction) {
898
+ // Expose encoder for use by SSR, as well as a special bind that can be used to
899
+ // keep server capabilities.
900
+ {
901
+ // Only expose this in builds that would actually use it. Not needed on the client.
902
+ const $$FORM_ACTION = encodeFormAction === undefined ? defaultEncodeFormAction : function (identifierPrefix) {
903
+ return customEncodeFormAction(this, identifierPrefix, encodeFormAction);
904
+ };
905
+ Object.defineProperties(proxy, {
906
+ $$FORM_ACTION: {
907
+ value: $$FORM_ACTION
908
+ },
909
+ $$IS_SIGNATURE_EQUAL: {
910
+ value: isSignatureEqual
911
+ },
912
+ bind: {
913
+ value: bind
914
+ }
915
+ });
916
+ }
917
+
918
+ knownServerReferences.set(proxy, reference);
919
+ } // $FlowFixMe[method-unbinding]
920
+
921
+ const FunctionBind = Function.prototype.bind; // $FlowFixMe[method-unbinding]
922
+
923
+ const ArraySlice = Array.prototype.slice;
924
+
925
+ function bind() {
926
+ // $FlowFixMe[unsupported-syntax]
927
+ const newFn = FunctionBind.apply(this, arguments);
928
+ const reference = knownServerReferences.get(this);
929
+
930
+ if (reference) {
931
+
932
+ const args = ArraySlice.call(arguments, 1);
933
+ let boundPromise = null;
934
+
935
+ if (reference.bound !== null) {
936
+ boundPromise = Promise.resolve(reference.bound).then(boundArgs => boundArgs.concat(args));
937
+ } else {
938
+ boundPromise = Promise.resolve(args);
939
+ } // Expose encoder for use by SSR, as well as a special bind that can be used to
940
+ // keep server capabilities.
941
+
942
+
943
+ {
944
+ // Only expose this in builds that would actually use it. Not needed on the client.
945
+ Object.defineProperties(newFn, {
946
+ $$FORM_ACTION: {
947
+ value: this.$$FORM_ACTION
948
+ },
949
+ $$IS_SIGNATURE_EQUAL: {
950
+ value: isSignatureEqual
951
+ },
952
+ bind: {
953
+ value: bind
954
+ }
955
+ });
956
+ }
957
+
958
+ knownServerReferences.set(newFn, {
959
+ id: reference.id,
960
+ bound: boundPromise
961
+ });
962
+ }
963
+
964
+ return newFn;
965
+ }
966
+
967
+ function createServerReference$1(id, callServer, encodeFormAction) {
968
+ const proxy = function () {
969
+ // $FlowFixMe[method-unbinding]
970
+ const args = Array.prototype.slice.call(arguments);
971
+ return callServer(id, args);
972
+ };
973
+
974
+ registerServerReference(proxy, {
975
+ id,
976
+ bound: null
977
+ }, encodeFormAction);
978
+ return proxy;
979
+ }
980
+
981
+ const ROW_ID = 0;
982
+ const ROW_TAG = 1;
983
+ const ROW_LENGTH = 2;
984
+ const ROW_CHUNK_BY_NEWLINE = 3;
985
+ const ROW_CHUNK_BY_LENGTH = 4;
986
+ const PENDING = 'pending';
987
+ const BLOCKED = 'blocked';
988
+ const CYCLIC = 'cyclic';
989
+ const RESOLVED_MODEL = 'resolved_model';
990
+ const RESOLVED_MODULE = 'resolved_module';
991
+ const INITIALIZED = 'fulfilled';
992
+ const ERRORED = 'rejected'; // $FlowFixMe[missing-this-annot]
993
+
994
+ function Chunk(status, value, reason, response) {
995
+ this.status = status;
996
+ this.value = value;
997
+ this.reason = reason;
998
+ this._response = response;
999
+ } // We subclass Promise.prototype so that we get other methods like .catch
1000
+
1001
+
1002
+ Chunk.prototype = Object.create(Promise.prototype); // TODO: This doesn't return a new Promise chain unlike the real .then
1003
+
1004
+ Chunk.prototype.then = function (resolve, reject) {
1005
+ const chunk = this; // If we have resolved content, we try to initialize it first which
1006
+ // might put us back into one of the other states.
1007
+
1008
+ switch (chunk.status) {
1009
+ case RESOLVED_MODEL:
1010
+ initializeModelChunk(chunk);
1011
+ break;
1012
+
1013
+ case RESOLVED_MODULE:
1014
+ initializeModuleChunk(chunk);
1015
+ break;
1016
+ } // The status might have changed after initialization.
1017
+
1018
+
1019
+ switch (chunk.status) {
1020
+ case INITIALIZED:
1021
+ resolve(chunk.value);
1022
+ break;
1023
+
1024
+ case PENDING:
1025
+ case BLOCKED:
1026
+ case CYCLIC:
1027
+ if (resolve) {
1028
+ if (chunk.value === null) {
1029
+ chunk.value = [];
1030
+ }
1031
+
1032
+ chunk.value.push(resolve);
1033
+ }
1034
+
1035
+ if (reject) {
1036
+ if (chunk.reason === null) {
1037
+ chunk.reason = [];
1038
+ }
1039
+
1040
+ chunk.reason.push(reject);
1041
+ }
1042
+
1043
+ break;
1044
+
1045
+ default:
1046
+ reject(chunk.reason);
1047
+ break;
1048
+ }
1049
+ };
1050
+
1051
+ function readChunk(chunk) {
1052
+ // If we have resolved content, we try to initialize it first which
1053
+ // might put us back into one of the other states.
1054
+ switch (chunk.status) {
1055
+ case RESOLVED_MODEL:
1056
+ initializeModelChunk(chunk);
1057
+ break;
1058
+
1059
+ case RESOLVED_MODULE:
1060
+ initializeModuleChunk(chunk);
1061
+ break;
1062
+ } // The status might have changed after initialization.
1063
+
1064
+
1065
+ switch (chunk.status) {
1066
+ case INITIALIZED:
1067
+ return chunk.value;
1068
+
1069
+ case PENDING:
1070
+ case BLOCKED:
1071
+ case CYCLIC:
1072
+ // eslint-disable-next-line no-throw-literal
1073
+ throw chunk;
1074
+
1075
+ default:
1076
+ throw chunk.reason;
1077
+ }
1078
+ }
1079
+
1080
+ function getRoot(response) {
1081
+ const chunk = getChunk(response, 0);
1082
+ return chunk;
1083
+ }
1084
+
1085
+ function createPendingChunk(response) {
1086
+ // $FlowFixMe[invalid-constructor] Flow doesn't support functions as constructors
1087
+ return new Chunk(PENDING, null, null, response);
1088
+ }
1089
+
1090
+ function createBlockedChunk(response) {
1091
+ // $FlowFixMe[invalid-constructor] Flow doesn't support functions as constructors
1092
+ return new Chunk(BLOCKED, null, null, response);
1093
+ }
1094
+
1095
+ function createErrorChunk(response, error) {
1096
+ // $FlowFixMe[invalid-constructor] Flow doesn't support functions as constructors
1097
+ return new Chunk(ERRORED, null, error, response);
1098
+ }
1099
+
1100
+ function wakeChunk(listeners, value) {
1101
+ for (let i = 0; i < listeners.length; i++) {
1102
+ const listener = listeners[i];
1103
+ listener(value);
1104
+ }
1105
+ }
1106
+
1107
+ function wakeChunkIfInitialized(chunk, resolveListeners, rejectListeners) {
1108
+ switch (chunk.status) {
1109
+ case INITIALIZED:
1110
+ wakeChunk(resolveListeners, chunk.value);
1111
+ break;
1112
+
1113
+ case PENDING:
1114
+ case BLOCKED:
1115
+ case CYCLIC:
1116
+ chunk.value = resolveListeners;
1117
+ chunk.reason = rejectListeners;
1118
+ break;
1119
+
1120
+ case ERRORED:
1121
+ if (rejectListeners) {
1122
+ wakeChunk(rejectListeners, chunk.reason);
1123
+ }
1124
+
1125
+ break;
1126
+ }
1127
+ }
1128
+
1129
+ function triggerErrorOnChunk(chunk, error) {
1130
+ if (chunk.status !== PENDING && chunk.status !== BLOCKED) {
1131
+ // We already resolved. We didn't expect to see this.
1132
+ return;
1133
+ }
1134
+
1135
+ const listeners = chunk.reason;
1136
+ const erroredChunk = chunk;
1137
+ erroredChunk.status = ERRORED;
1138
+ erroredChunk.reason = error;
1139
+
1140
+ if (listeners !== null) {
1141
+ wakeChunk(listeners, error);
1142
+ }
1143
+ }
1144
+
1145
+ function createResolvedModelChunk(response, value) {
1146
+ // $FlowFixMe[invalid-constructor] Flow doesn't support functions as constructors
1147
+ return new Chunk(RESOLVED_MODEL, value, null, response);
1148
+ }
1149
+
1150
+ function createResolvedModuleChunk(response, value) {
1151
+ // $FlowFixMe[invalid-constructor] Flow doesn't support functions as constructors
1152
+ return new Chunk(RESOLVED_MODULE, value, null, response);
1153
+ }
1154
+
1155
+ function createInitializedTextChunk(response, value) {
1156
+ // $FlowFixMe[invalid-constructor] Flow doesn't support functions as constructors
1157
+ return new Chunk(INITIALIZED, value, null, response);
1158
+ }
1159
+
1160
+ function resolveModelChunk(chunk, value) {
1161
+ if (chunk.status !== PENDING) {
1162
+ // We already resolved. We didn't expect to see this.
1163
+ return;
1164
+ }
1165
+
1166
+ const resolveListeners = chunk.value;
1167
+ const rejectListeners = chunk.reason;
1168
+ const resolvedChunk = chunk;
1169
+ resolvedChunk.status = RESOLVED_MODEL;
1170
+ resolvedChunk.value = value;
1171
+
1172
+ if (resolveListeners !== null) {
1173
+ // This is unfortunate that we're reading this eagerly if
1174
+ // we already have listeners attached since they might no
1175
+ // longer be rendered or might not be the highest pri.
1176
+ initializeModelChunk(resolvedChunk); // The status might have changed after initialization.
1177
+
1178
+ wakeChunkIfInitialized(chunk, resolveListeners, rejectListeners);
1179
+ }
1180
+ }
1181
+
1182
+ function resolveModuleChunk(chunk, value) {
1183
+ if (chunk.status !== PENDING && chunk.status !== BLOCKED) {
1184
+ // We already resolved. We didn't expect to see this.
1185
+ return;
1186
+ }
1187
+
1188
+ const resolveListeners = chunk.value;
1189
+ const rejectListeners = chunk.reason;
1190
+ const resolvedChunk = chunk;
1191
+ resolvedChunk.status = RESOLVED_MODULE;
1192
+ resolvedChunk.value = value;
1193
+
1194
+ if (resolveListeners !== null) {
1195
+ initializeModuleChunk(resolvedChunk);
1196
+ wakeChunkIfInitialized(chunk, resolveListeners, rejectListeners);
1197
+ }
1198
+ }
1199
+
1200
+ let initializingChunk = null;
1201
+ let initializingChunkBlockedModel = null;
1202
+
1203
+ function initializeModelChunk(chunk) {
1204
+ const prevChunk = initializingChunk;
1205
+ const prevBlocked = initializingChunkBlockedModel;
1206
+ initializingChunk = chunk;
1207
+ initializingChunkBlockedModel = null;
1208
+ const resolvedModel = chunk.value; // We go to the CYCLIC state until we've fully resolved this.
1209
+ // We do this before parsing in case we try to initialize the same chunk
1210
+ // while parsing the model. Such as in a cyclic reference.
1211
+
1212
+ const cyclicChunk = chunk;
1213
+ cyclicChunk.status = CYCLIC;
1214
+ cyclicChunk.value = null;
1215
+ cyclicChunk.reason = null;
1216
+
1217
+ try {
1218
+ const value = parseModel(chunk._response, resolvedModel);
1219
+
1220
+ if (initializingChunkBlockedModel !== null && initializingChunkBlockedModel.deps > 0) {
1221
+ initializingChunkBlockedModel.value = value; // We discovered new dependencies on modules that are not yet resolved.
1222
+ // We have to go the BLOCKED state until they're resolved.
1223
+
1224
+ const blockedChunk = chunk;
1225
+ blockedChunk.status = BLOCKED;
1226
+ blockedChunk.value = null;
1227
+ blockedChunk.reason = null;
1228
+ } else {
1229
+ const resolveListeners = cyclicChunk.value;
1230
+ const initializedChunk = chunk;
1231
+ initializedChunk.status = INITIALIZED;
1232
+ initializedChunk.value = value;
1233
+
1234
+ if (resolveListeners !== null) {
1235
+ wakeChunk(resolveListeners, value);
1236
+ }
1237
+ }
1238
+ } catch (error) {
1239
+ const erroredChunk = chunk;
1240
+ erroredChunk.status = ERRORED;
1241
+ erroredChunk.reason = error;
1242
+ } finally {
1243
+ initializingChunk = prevChunk;
1244
+ initializingChunkBlockedModel = prevBlocked;
1245
+ }
1246
+ }
1247
+
1248
+ function initializeModuleChunk(chunk) {
1249
+ try {
1250
+ const value = requireModule(chunk.value);
1251
+ const initializedChunk = chunk;
1252
+ initializedChunk.status = INITIALIZED;
1253
+ initializedChunk.value = value;
1254
+ } catch (error) {
1255
+ const erroredChunk = chunk;
1256
+ erroredChunk.status = ERRORED;
1257
+ erroredChunk.reason = error;
1258
+ }
1259
+ } // Report that any missing chunks in the model is now going to throw this
1260
+ // error upon read. Also notify any pending promises.
1261
+
1262
+
1263
+ function reportGlobalError(response, error) {
1264
+ response._chunks.forEach(chunk => {
1265
+ // If this chunk was already resolved or errored, it won't
1266
+ // trigger an error but if it wasn't then we need to
1267
+ // because we won't be getting any new data to resolve it.
1268
+ if (chunk.status === PENDING) {
1269
+ triggerErrorOnChunk(chunk, error);
1270
+ }
1271
+ });
1272
+ }
1273
+
1274
+ function createElement(type, key, props) {
1275
+ let element;
1276
+
1277
+ {
1278
+ element = {
1279
+ // This tag allows us to uniquely identify this as a React Element
1280
+ $$typeof: REACT_ELEMENT_TYPE,
1281
+ type,
1282
+ key,
1283
+ ref: null,
1284
+ props,
1285
+ // Record the component responsible for creating this element.
1286
+ _owner: null
1287
+ };
1288
+ }
1289
+
1290
+ return element;
1291
+ }
1292
+
1293
+ function createLazyChunkWrapper(chunk) {
1294
+ const lazyType = {
1295
+ $$typeof: REACT_LAZY_TYPE,
1296
+ _payload: chunk,
1297
+ _init: readChunk
1298
+ };
1299
+
1300
+ return lazyType;
1301
+ }
1302
+
1303
+ function getChunk(response, id) {
1304
+ const chunks = response._chunks;
1305
+ let chunk = chunks.get(id);
1306
+
1307
+ if (!chunk) {
1308
+ chunk = createPendingChunk(response);
1309
+ chunks.set(id, chunk);
1310
+ }
1311
+
1312
+ return chunk;
1313
+ }
1314
+
1315
+ function createModelResolver(chunk, parentObject, key, cyclic) {
1316
+ let blocked;
1317
+
1318
+ if (initializingChunkBlockedModel) {
1319
+ blocked = initializingChunkBlockedModel;
1320
+
1321
+ if (!cyclic) {
1322
+ blocked.deps++;
1323
+ }
1324
+ } else {
1325
+ blocked = initializingChunkBlockedModel = {
1326
+ deps: cyclic ? 0 : 1,
1327
+ value: null
1328
+ };
1329
+ }
1330
+
1331
+ return value => {
1332
+ parentObject[key] = value;
1333
+ blocked.deps--;
1334
+
1335
+ if (blocked.deps === 0) {
1336
+ if (chunk.status !== BLOCKED) {
1337
+ return;
1338
+ }
1339
+
1340
+ const resolveListeners = chunk.value;
1341
+ const initializedChunk = chunk;
1342
+ initializedChunk.status = INITIALIZED;
1343
+ initializedChunk.value = blocked.value;
1344
+
1345
+ if (resolveListeners !== null) {
1346
+ wakeChunk(resolveListeners, blocked.value);
1347
+ }
1348
+ }
1349
+ };
1350
+ }
1351
+
1352
+ function createModelReject(chunk) {
1353
+ return error => triggerErrorOnChunk(chunk, error);
1354
+ }
1355
+
1356
+ function createServerReferenceProxy(response, metaData) {
1357
+ const callServer = response._callServer;
1358
+
1359
+ const proxy = function () {
1360
+ // $FlowFixMe[method-unbinding]
1361
+ const args = Array.prototype.slice.call(arguments);
1362
+ const p = metaData.bound;
1363
+
1364
+ if (!p) {
1365
+ return callServer(metaData.id, args);
1366
+ }
1367
+
1368
+ if (p.status === INITIALIZED) {
1369
+ const bound = p.value;
1370
+ return callServer(metaData.id, bound.concat(args));
1371
+ } // Since this is a fake Promise whose .then doesn't chain, we have to wrap it.
1372
+ // TODO: Remove the wrapper once that's fixed.
1373
+
1374
+
1375
+ return Promise.resolve(p).then(function (bound) {
1376
+ return callServer(metaData.id, bound.concat(args));
1377
+ });
1378
+ };
1379
+
1380
+ registerServerReference(proxy, metaData, response._encodeFormAction);
1381
+ return proxy;
1382
+ }
1383
+
1384
+ function getOutlinedModel(response, id) {
1385
+ const chunk = getChunk(response, id);
1386
+
1387
+ switch (chunk.status) {
1388
+ case RESOLVED_MODEL:
1389
+ initializeModelChunk(chunk);
1390
+ break;
1391
+ } // The status might have changed after initialization.
1392
+
1393
+
1394
+ switch (chunk.status) {
1395
+ case INITIALIZED:
1396
+ {
1397
+ return chunk.value;
1398
+ }
1399
+ // We always encode it first in the stream so it won't be pending.
1400
+
1401
+ default:
1402
+ throw chunk.reason;
1403
+ }
1404
+ }
1405
+
1406
+ function parseModelString(response, parentObject, key, value) {
1407
+ if (value[0] === '$') {
1408
+ if (value === '$') {
1409
+ // A very common symbol.
1410
+ return REACT_ELEMENT_TYPE;
1411
+ }
1412
+
1413
+ switch (value[1]) {
1414
+ case '$':
1415
+ {
1416
+ // This was an escaped string value.
1417
+ return value.slice(1);
1418
+ }
1419
+
1420
+ case 'L':
1421
+ {
1422
+ // Lazy node
1423
+ const id = parseInt(value.slice(2), 16);
1424
+ const chunk = getChunk(response, id); // We create a React.lazy wrapper around any lazy values.
1425
+ // When passed into React, we'll know how to suspend on this.
1426
+
1427
+ return createLazyChunkWrapper(chunk);
1428
+ }
1429
+
1430
+ case '@':
1431
+ {
1432
+ // Promise
1433
+ if (value.length === 2) {
1434
+ // Infinite promise that never resolves.
1435
+ return new Promise(() => {});
1436
+ }
1437
+
1438
+ const id = parseInt(value.slice(2), 16);
1439
+ const chunk = getChunk(response, id);
1440
+ return chunk;
1441
+ }
1442
+
1443
+ case 'S':
1444
+ {
1445
+ // Symbol
1446
+ return Symbol.for(value.slice(2));
1447
+ }
1448
+
1449
+ case 'F':
1450
+ {
1451
+ // Server Reference
1452
+ const id = parseInt(value.slice(2), 16);
1453
+ const metadata = getOutlinedModel(response, id);
1454
+ return createServerReferenceProxy(response, metadata);
1455
+ }
1456
+
1457
+ case 'T':
1458
+ {
1459
+ // Temporary Reference
1460
+ const id = parseInt(value.slice(2), 16);
1461
+ const temporaryReferences = response._tempRefs;
1462
+
1463
+ if (temporaryReferences == null) {
1464
+ 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.');
1465
+ }
1466
+
1467
+ return readTemporaryReference(temporaryReferences, id);
1468
+ }
1469
+
1470
+ case 'Q':
1471
+ {
1472
+ // Map
1473
+ const id = parseInt(value.slice(2), 16);
1474
+ const data = getOutlinedModel(response, id);
1475
+ return new Map(data);
1476
+ }
1477
+
1478
+ case 'W':
1479
+ {
1480
+ // Set
1481
+ const id = parseInt(value.slice(2), 16);
1482
+ const data = getOutlinedModel(response, id);
1483
+ return new Set(data);
1484
+ }
1485
+
1486
+ case 'I':
1487
+ {
1488
+ // $Infinity
1489
+ return Infinity;
1490
+ }
1491
+
1492
+ case '-':
1493
+ {
1494
+ // $-0 or $-Infinity
1495
+ if (value === '$-0') {
1496
+ return -0;
1497
+ } else {
1498
+ return -Infinity;
1499
+ }
1500
+ }
1501
+
1502
+ case 'N':
1503
+ {
1504
+ // $NaN
1505
+ return NaN;
1506
+ }
1507
+
1508
+ case 'u':
1509
+ {
1510
+ // matches "$undefined"
1511
+ // Special encoding for `undefined` which can't be serialized as JSON otherwise.
1512
+ return undefined;
1513
+ }
1514
+
1515
+ case 'D':
1516
+ {
1517
+ // Date
1518
+ return new Date(Date.parse(value.slice(2)));
1519
+ }
1520
+
1521
+ case 'n':
1522
+ {
1523
+ // BigInt
1524
+ return BigInt(value.slice(2));
1525
+ }
1526
+
1527
+ case 'E':
1528
+
1529
+ default:
1530
+ {
1531
+ // We assume that anything else is a reference ID.
1532
+ const id = parseInt(value.slice(1), 16);
1533
+ const chunk = getChunk(response, id);
1534
+
1535
+ switch (chunk.status) {
1536
+ case RESOLVED_MODEL:
1537
+ initializeModelChunk(chunk);
1538
+ break;
1539
+
1540
+ case RESOLVED_MODULE:
1541
+ initializeModuleChunk(chunk);
1542
+ break;
1543
+ } // The status might have changed after initialization.
1544
+
1545
+
1546
+ switch (chunk.status) {
1547
+ case INITIALIZED:
1548
+ const chunkValue = chunk.value;
1549
+
1550
+ return chunkValue;
1551
+
1552
+ case PENDING:
1553
+ case BLOCKED:
1554
+ case CYCLIC:
1555
+ const parentChunk = initializingChunk;
1556
+ chunk.then(createModelResolver(parentChunk, parentObject, key, chunk.status === CYCLIC), createModelReject(parentChunk));
1557
+ return null;
1558
+
1559
+ default:
1560
+ throw chunk.reason;
1561
+ }
1562
+ }
1563
+ }
1564
+ }
1565
+
1566
+ return value;
1567
+ }
1568
+
1569
+ function parseModelTuple(response, value) {
1570
+ const tuple = value;
1571
+
1572
+ if (tuple[0] === REACT_ELEMENT_TYPE) {
1573
+ // TODO: Consider having React just directly accept these arrays as elements.
1574
+ // Or even change the ReactElement type to be an array.
1575
+ return createElement(tuple[1], tuple[2], tuple[3]);
1576
+ }
1577
+
1578
+ return value;
1579
+ }
1580
+
1581
+ function missingCall() {
1582
+ throw new Error('Trying to call a function from "use server" but the callServer option ' + 'was not implemented in your router runtime.');
1583
+ }
1584
+
1585
+ function createResponse(bundlerConfig, moduleLoading, callServer, encodeFormAction, nonce, temporaryReferences) {
1586
+ const chunks = new Map();
1587
+ const response = {
1588
+ _bundlerConfig: bundlerConfig,
1589
+ _moduleLoading: moduleLoading,
1590
+ _callServer: callServer !== undefined ? callServer : missingCall,
1591
+ _encodeFormAction: encodeFormAction,
1592
+ _nonce: nonce,
1593
+ _chunks: chunks,
1594
+ _stringDecoder: createStringDecoder(),
1595
+ _fromJSON: null,
1596
+ _rowState: 0,
1597
+ _rowID: 0,
1598
+ _rowTag: 0,
1599
+ _rowLength: 0,
1600
+ _buffer: [],
1601
+ _tempRefs: temporaryReferences
1602
+ }; // Don't inline this call because it causes closure to outline the call above.
1603
+
1604
+ response._fromJSON = createFromJSONCallback(response);
1605
+ return response;
1606
+ }
1607
+
1608
+ function resolveModel(response, id, model) {
1609
+ const chunks = response._chunks;
1610
+ const chunk = chunks.get(id);
1611
+
1612
+ if (!chunk) {
1613
+ chunks.set(id, createResolvedModelChunk(response, model));
1614
+ } else {
1615
+ resolveModelChunk(chunk, model);
1616
+ }
1617
+ }
1618
+
1619
+ function resolveText(response, id, text) {
1620
+ const chunks = response._chunks; // We assume that we always reference large strings after they've been
1621
+ // emitted.
1622
+
1623
+ chunks.set(id, createInitializedTextChunk(response, text));
1624
+ }
1625
+
1626
+ function resolveModule(response, id, model) {
1627
+ const chunks = response._chunks;
1628
+ const chunk = chunks.get(id);
1629
+ const clientReferenceMetadata = parseModel(response, model);
1630
+ const clientReference = resolveClientReference(response._bundlerConfig, clientReferenceMetadata);
1631
+ prepareDestinationForModule(response._moduleLoading, response._nonce, clientReferenceMetadata); // TODO: Add an option to encode modules that are lazy loaded.
1632
+ // For now we preload all modules as early as possible since it's likely
1633
+ // that we'll need them.
1634
+
1635
+ const promise = preloadModule(clientReference);
1636
+
1637
+ if (promise) {
1638
+ let blockedChunk;
1639
+
1640
+ if (!chunk) {
1641
+ // Technically, we should just treat promise as the chunk in this
1642
+ // case. Because it'll just behave as any other promise.
1643
+ blockedChunk = createBlockedChunk(response);
1644
+ chunks.set(id, blockedChunk);
1645
+ } else {
1646
+ // This can't actually happen because we don't have any forward
1647
+ // references to modules.
1648
+ blockedChunk = chunk;
1649
+ blockedChunk.status = BLOCKED;
1650
+ }
1651
+
1652
+ promise.then(() => resolveModuleChunk(blockedChunk, clientReference), error => triggerErrorOnChunk(blockedChunk, error));
1653
+ } else {
1654
+ if (!chunk) {
1655
+ chunks.set(id, createResolvedModuleChunk(response, clientReference));
1656
+ } else {
1657
+ // This can't actually happen because we don't have any forward
1658
+ // references to modules.
1659
+ resolveModuleChunk(chunk, clientReference);
1660
+ }
1661
+ }
1662
+ }
1663
+
1664
+ function resolveErrorProd(response, id, digest) {
1665
+
1666
+ const error = new Error('An error occurred in the Server Components render. The specific message is omitted in production' + ' builds to avoid leaking sensitive details. A digest property is included on this error instance which' + ' may provide additional details about the nature of the error.');
1667
+ error.stack = 'Error: ' + error.message;
1668
+ error.digest = digest;
1669
+ const errorWithDigest = error;
1670
+ const chunks = response._chunks;
1671
+ const chunk = chunks.get(id);
1672
+
1673
+ if (!chunk) {
1674
+ chunks.set(id, createErrorChunk(response, errorWithDigest));
1675
+ } else {
1676
+ triggerErrorOnChunk(chunk, errorWithDigest);
1677
+ }
1678
+ }
1679
+
1680
+ function resolveHint(response, code, model) {
1681
+ const hintModel = parseModel(response, model);
1682
+ dispatchHint(code, hintModel);
1683
+ }
1684
+
1685
+ function processFullRow(response, id, tag, buffer, chunk) {
1686
+
1687
+ const stringDecoder = response._stringDecoder;
1688
+ let row = '';
1689
+
1690
+ for (let i = 0; i < buffer.length; i++) {
1691
+ row += readPartialStringChunk(stringDecoder, buffer[i]);
1692
+ }
1693
+
1694
+ row += readFinalStringChunk(stringDecoder, chunk);
1695
+
1696
+ switch (tag) {
1697
+ case 73
1698
+ /* "I" */
1699
+ :
1700
+ {
1701
+ resolveModule(response, id, row);
1702
+ return;
1703
+ }
1704
+
1705
+ case 72
1706
+ /* "H" */
1707
+ :
1708
+ {
1709
+ const code = row[0];
1710
+ resolveHint(response, code, row.slice(1));
1711
+ return;
1712
+ }
1713
+
1714
+ case 69
1715
+ /* "E" */
1716
+ :
1717
+ {
1718
+ const errorInfo = JSON.parse(row);
1719
+
1720
+ {
1721
+ resolveErrorProd(response, id, errorInfo.digest);
1722
+ }
1723
+
1724
+ return;
1725
+ }
1726
+
1727
+ case 84
1728
+ /* "T" */
1729
+ :
1730
+ {
1731
+ resolveText(response, id, row);
1732
+ return;
1733
+ }
1734
+
1735
+ case 68
1736
+ /* "D" */
1737
+ :
1738
+
1739
+ case 87
1740
+ /* "W" */
1741
+ :
1742
+ {
1743
+
1744
+ throw new Error('Failed to read a RSC payload created by a development version of React ' + 'on the server while using a production version on the client. Always use ' + 'matching versions on the server and the client.');
1745
+ }
1746
+
1747
+ case 80
1748
+ /* "P" */
1749
+ :
1750
+ // Fallthrough
1751
+
1752
+ default:
1753
+ /* """ "{" "[" "t" "f" "n" "0" - "9" */
1754
+ {
1755
+ // We assume anything else is JSON.
1756
+ resolveModel(response, id, row);
1757
+ return;
1758
+ }
1759
+ }
1760
+ }
1761
+
1762
+ function processBinaryChunk(response, chunk) {
1763
+ let i = 0;
1764
+ let rowState = response._rowState;
1765
+ let rowID = response._rowID;
1766
+ let rowTag = response._rowTag;
1767
+ let rowLength = response._rowLength;
1768
+ const buffer = response._buffer;
1769
+ const chunkLength = chunk.length;
1770
+
1771
+ while (i < chunkLength) {
1772
+ let lastIdx = -1;
1773
+
1774
+ switch (rowState) {
1775
+ case ROW_ID:
1776
+ {
1777
+ const byte = chunk[i++];
1778
+
1779
+ if (byte === 58
1780
+ /* ":" */
1781
+ ) {
1782
+ // Finished the rowID, next we'll parse the tag.
1783
+ rowState = ROW_TAG;
1784
+ } else {
1785
+ rowID = rowID << 4 | (byte > 96 ? byte - 87 : byte - 48);
1786
+ }
1787
+
1788
+ continue;
1789
+ }
1790
+
1791
+ case ROW_TAG:
1792
+ {
1793
+ const resolvedRowTag = chunk[i];
1794
+
1795
+ if (resolvedRowTag === 84
1796
+ /* "T" */
1797
+ || enableBinaryFlight
1798
+ /* "V" */
1799
+ ) {
1800
+ rowTag = resolvedRowTag;
1801
+ rowState = ROW_LENGTH;
1802
+ i++;
1803
+ } else if (resolvedRowTag > 64 && resolvedRowTag < 91
1804
+ /* "A"-"Z" */
1805
+ ) {
1806
+ rowTag = resolvedRowTag;
1807
+ rowState = ROW_CHUNK_BY_NEWLINE;
1808
+ i++;
1809
+ } else {
1810
+ rowTag = 0;
1811
+ rowState = ROW_CHUNK_BY_NEWLINE; // This was an unknown tag so it was probably part of the data.
1812
+ }
1813
+
1814
+ continue;
1815
+ }
1816
+
1817
+ case ROW_LENGTH:
1818
+ {
1819
+ const byte = chunk[i++];
1820
+
1821
+ if (byte === 44
1822
+ /* "," */
1823
+ ) {
1824
+ // Finished the rowLength, next we'll buffer up to that length.
1825
+ rowState = ROW_CHUNK_BY_LENGTH;
1826
+ } else {
1827
+ rowLength = rowLength << 4 | (byte > 96 ? byte - 87 : byte - 48);
1828
+ }
1829
+
1830
+ continue;
1831
+ }
1832
+
1833
+ case ROW_CHUNK_BY_NEWLINE:
1834
+ {
1835
+ // We're looking for a newline
1836
+ lastIdx = chunk.indexOf(10
1837
+ /* "\n" */
1838
+ , i);
1839
+ break;
1840
+ }
1841
+
1842
+ case ROW_CHUNK_BY_LENGTH:
1843
+ {
1844
+ // We're looking for the remaining byte length
1845
+ lastIdx = i + rowLength;
1846
+
1847
+ if (lastIdx > chunk.length) {
1848
+ lastIdx = -1;
1849
+ }
1850
+
1851
+ break;
1852
+ }
1853
+ }
1854
+
1855
+ const offset = chunk.byteOffset + i;
1856
+
1857
+ if (lastIdx > -1) {
1858
+ // We found the last chunk of the row
1859
+ const length = lastIdx - i;
1860
+ const lastChunk = new Uint8Array(chunk.buffer, offset, length);
1861
+ processFullRow(response, rowID, rowTag, buffer, lastChunk); // Reset state machine for a new row
1862
+
1863
+ i = lastIdx;
1864
+
1865
+ if (rowState === ROW_CHUNK_BY_NEWLINE) {
1866
+ // If we're trailing by a newline we need to skip it.
1867
+ i++;
1868
+ }
1869
+
1870
+ rowState = ROW_ID;
1871
+ rowTag = 0;
1872
+ rowID = 0;
1873
+ rowLength = 0;
1874
+ buffer.length = 0;
1875
+ } else {
1876
+ // The rest of this row is in a future chunk. We stash the rest of the
1877
+ // current chunk until we can process the full row.
1878
+ const length = chunk.byteLength - i;
1879
+ const remainingSlice = new Uint8Array(chunk.buffer, offset, length);
1880
+ buffer.push(remainingSlice); // Update how many bytes we're still waiting for. If we're looking for
1881
+ // a newline, this doesn't hurt since we'll just ignore it.
1882
+
1883
+ rowLength -= remainingSlice.byteLength;
1884
+ break;
1885
+ }
1886
+ }
1887
+
1888
+ response._rowState = rowState;
1889
+ response._rowID = rowID;
1890
+ response._rowTag = rowTag;
1891
+ response._rowLength = rowLength;
1892
+ }
1893
+
1894
+ function parseModel(response, json) {
1895
+ return JSON.parse(json, response._fromJSON);
1896
+ }
1897
+
1898
+ function createFromJSONCallback(response) {
1899
+ // $FlowFixMe[missing-this-annot]
1900
+ return function (key, value) {
1901
+ if (typeof value === 'string') {
1902
+ // We can't use .bind here because we need the "this" value.
1903
+ return parseModelString(response, this, key, value);
1904
+ }
1905
+
1906
+ if (typeof value === 'object' && value !== null) {
1907
+ return parseModelTuple(response, value);
1908
+ }
1909
+
1910
+ return value;
1911
+ };
1912
+ }
1913
+
1914
+ function close(response) {
1915
+ // In case there are any remaining unresolved chunks, they won't
1916
+ // be resolved now. So we need to issue an error to those.
1917
+ // Ideally we should be able to early bail out if we kept a
1918
+ // ref count of pending chunks.
1919
+ reportGlobalError(response, new Error('Connection closed.'));
1920
+ }
1921
+
1922
+ function noServerCall() {
1923
+ throw new Error('Server Functions cannot be called during initial render. ' + 'This would create a fetch waterfall. Try to use a Server Component ' + 'to pass data to Client Components instead.');
1924
+ }
1925
+
1926
+ function createServerReference(id, callServer) {
1927
+ return createServerReference$1(id, noServerCall);
1928
+ }
1929
+
1930
+ function createResponseFromOptions(options) {
1931
+ return createResponse(options.ssrManifest.moduleMap, options.ssrManifest.moduleLoading, noServerCall, options.encodeFormAction, typeof options.nonce === 'string' ? options.nonce : undefined, options && options.temporaryReferences ? options.temporaryReferences : undefined);
1932
+ }
1933
+
1934
+ function startReadingFromStream(response, stream) {
1935
+ const reader = stream.getReader();
1936
+
1937
+ function progress(_ref) {
1938
+ let done = _ref.done,
1939
+ value = _ref.value;
1940
+
1941
+ if (done) {
1942
+ close(response);
1943
+ return;
1944
+ }
1945
+
1946
+ const buffer = value;
1947
+ processBinaryChunk(response, buffer);
1948
+ return reader.read().then(progress).catch(error);
1949
+ }
1950
+
1951
+ function error(e) {
1952
+ reportGlobalError(response, e);
1953
+ }
1954
+
1955
+ reader.read().then(progress).catch(error);
1956
+ }
1957
+
1958
+ function createFromReadableStream(stream, options) {
1959
+ const response = createResponseFromOptions(options);
1960
+ startReadingFromStream(response, stream);
1961
+ return getRoot(response);
1962
+ }
1963
+
1964
+ function createFromFetch(promiseForResponse, options) {
1965
+ const response = createResponseFromOptions(options);
1966
+ promiseForResponse.then(function (r) {
1967
+ startReadingFromStream(response, r.body);
1968
+ }, function (e) {
1969
+ reportGlobalError(response, e);
1970
+ });
1971
+ return getRoot(response);
1972
+ }
1973
+
1974
+ function encodeReply(value, options)
1975
+ /* We don't use URLSearchParams yet but maybe */
1976
+ {
1977
+ return new Promise((resolve, reject) => {
1978
+ processReply(value, '', options && options.temporaryReferences ? options.temporaryReferences : undefined, resolve, reject);
1979
+ });
1980
+ }
1981
+
1982
+ exports.createFromFetch = createFromFetch;
1983
+ exports.createFromReadableStream = createFromReadableStream;
1984
+ exports.createServerReference = createServerReference;
1985
+ exports.createTemporaryReferenceSet = createTemporaryReferenceSet;
1986
+ exports.encodeReply = encodeReply;