@valbuild/tanstack 0.154.0 → 0.154.2

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.
@@ -2,17 +2,16 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- var version = require('../../dist/version-961b447c.cjs.prod.js');
5
+ var version = require('../../dist/version-fdcad756.cjs.prod.js');
6
6
  var asyncToGenerator = require('../../dist/asyncToGenerator-8e5c36c8.cjs.prod.js');
7
7
  var core = require('@valbuild/core');
8
8
  var _typeof = require('../../dist/typeof-56656438.cjs.prod.js');
9
- var server = require('@valbuild/server');
9
+ var internal = require('@valbuild/server/internal');
10
10
  var stega = require('@valbuild/react/stega');
11
11
  var internal$1 = require('@valbuild/react/internal');
12
12
  var internal$2 = require('@valbuild/shared/internal');
13
- var internal = require('@valbuild/server/internal');
14
13
  var routeFromVal = require('../../dist/routeFromVal-2e834397.cjs.prod.js');
15
- var mcp = require('@valbuild/mcp');
14
+ var metadata = require('@valbuild/mcp/metadata');
16
15
  require('@valbuild/shared/client');
17
16
 
18
17
  function _interopNamespace(e) {
@@ -196,6 +195,14 @@ function _hasValEnableCookieOnServer() {
196
195
  * a `Request` and hands back a `ValServerGenericResult` — so all this adds is
197
196
  * the conversion to a `Response`, which for Next needed `NextResponse` and here
198
197
  * does not need anything.
198
+ *
199
+ * `@valbuild/server` is imported by the first request to the API, not by this
200
+ * module. An app builds this handler in the `val.server.ts` its pages read
201
+ * content from, and TanStack Start's route tree imports the API route's module
202
+ * to match a URL at all, so a static import put the whole server -- the data
203
+ * layer, TypeScript, sucrase, and the Studio's assets, tens of megabytes -- in
204
+ * front of every visitor's first request to a cold isolate. See
205
+ * `servingPath.test.ts`.
199
206
  */
200
207
  var initValApiHandler = function initValApiHandler(valModules, config, draftMode, formatter, commitPrepared, sourceFiles, patchStore, unsafelyAllowUnauthenticated, apiKey, publishOverride, http, valContentUrl) {
201
208
  var route = "/api/val"; // TODO: get from config
@@ -207,7 +214,7 @@ var initValApiHandler = function initValApiHandler(valModules, config, draftMode
207
214
  if (!tanstackVersion) {
208
215
  throw new Error("Could not get @valbuild/tanstack package version");
209
216
  }
210
- return server.createValApiRouter(route, server.createValServer(valModules, route, _typeof._objectSpread2(_typeof._objectSpread2(_typeof._objectSpread2(_typeof._objectSpread2(_typeof._objectSpread2(_typeof._objectSpread2(_typeof._objectSpread2({
217
+ var options = _typeof._objectSpread2(_typeof._objectSpread2(_typeof._objectSpread2(_typeof._objectSpread2(_typeof._objectSpread2(_typeof._objectSpread2(_typeof._objectSpread2({
211
218
  versions: {
212
219
  /*
213
220
  * Reported as `next` because that is the field the wire contract has.
@@ -232,59 +239,119 @@ var initValApiHandler = function initValApiHandler(valModules, config, draftMode
232
239
  apiKey: apiKey
233
240
  } : {}), http !== undefined ? http : {}), valContentUrl !== undefined ? {
234
241
  valContentUrl: valContentUrl
235
- } : {}), config, {
236
- isEnabled: function isEnabled() {
237
- return draftMode.isEnabled();
238
- },
239
- onEnable: function onEnable() {
240
- return draftMode.enable();
241
- },
242
- onDisable: function onDisable() {
243
- return draftMode.disable();
244
- }
245
- }, formatter, commitPrepared, publishOverride), function (valRes) {
246
- var headers = new Headers();
247
- var valResHeaders = "headers" in valRes && valRes.headers || {};
248
- for (var key in valResHeaders) {
249
- var value = valResHeaders[key];
250
- if (typeof value === "string") {
251
- headers.set(key, value);
252
- }
253
- }
254
- if ("cookies" in valRes && valRes.cookies) {
255
- for (var _i = 0, _Object$entries = Object.entries(valRes.cookies); _i < _Object$entries.length; _i++) {
256
- var _Object$entries$_i = _typeof._slicedToArray(_Object$entries[_i], 2),
257
- cookieName = _Object$entries$_i[0],
258
- cookie = _Object$entries$_i[1];
259
- headers.append("Set-Cookie", serializeCookie(cookieName, cookie));
260
- }
261
- }
262
- if ("json" in valRes) {
263
- headers.set("Content-Type", "application/json");
264
- return new Response(JSON.stringify(valRes.json), {
265
- headers: headers,
266
- status: valRes.status
267
- });
268
- }
269
- if (valRes.status === 302) {
270
- headers.set("Location", valRes.redirectTo);
271
- /*
272
- * Built by hand rather than with `Response.redirect`.
273
- *
274
- * `Response.redirect` produces an immutable response whose headers
275
- * cannot be added to, and the redirect that enables Val is precisely
276
- * the one that has to set cookies on its way out.
277
- */
278
- return new Response(null, {
279
- status: valRes.status,
280
- headers: headers
281
- });
282
- }
283
- return new Response("body" in valRes ? valRes.body : null, {
284
- headers: headers,
285
- status: valRes.status
286
- });
287
- });
242
+ } : {});
243
+ var router = null;
244
+ var loadRouter = /*#__PURE__*/function () {
245
+ var _ref = asyncToGenerator._asyncToGenerator(/*#__PURE__*/asyncToGenerator._regenerator().m(function _callee() {
246
+ var _yield$import, createValApiRouter, createValServer;
247
+ return asyncToGenerator._regenerator().w(function (_context) {
248
+ while (1) switch (_context.n) {
249
+ case 0:
250
+ _context.n = 1;
251
+ return Promise.resolve().then(function () { return /*#__PURE__*/_interopNamespace(require('@valbuild/server')); });
252
+ case 1:
253
+ _yield$import = _context.v;
254
+ createValApiRouter = _yield$import.createValApiRouter;
255
+ createValServer = _yield$import.createValServer;
256
+ return _context.a(2, createValApiRouter(route, createValServer(valModules, route, options, config, {
257
+ isEnabled: function isEnabled() {
258
+ return draftMode.isEnabled();
259
+ },
260
+ onEnable: function onEnable() {
261
+ return draftMode.enable();
262
+ },
263
+ onDisable: function onDisable() {
264
+ return draftMode.disable();
265
+ }
266
+ }, formatter, commitPrepared, publishOverride), function (valRes) {
267
+ var headers = new Headers();
268
+ var valResHeaders = "headers" in valRes && valRes.headers || {};
269
+ for (var key in valResHeaders) {
270
+ var value = valResHeaders[key];
271
+ if (typeof value === "string") {
272
+ headers.set(key, value);
273
+ }
274
+ }
275
+ if ("cookies" in valRes && valRes.cookies) {
276
+ for (var _i = 0, _Object$entries = Object.entries(valRes.cookies); _i < _Object$entries.length; _i++) {
277
+ var _Object$entries$_i = _typeof._slicedToArray(_Object$entries[_i], 2),
278
+ cookieName = _Object$entries$_i[0],
279
+ cookie = _Object$entries$_i[1];
280
+ headers.append("Set-Cookie", serializeCookie(cookieName, cookie));
281
+ }
282
+ }
283
+ if ("json" in valRes) {
284
+ headers.set("Content-Type", "application/json");
285
+ return new Response(JSON.stringify(valRes.json), {
286
+ headers: headers,
287
+ status: valRes.status
288
+ });
289
+ }
290
+ if (valRes.status === 302) {
291
+ headers.set("Location", valRes.redirectTo);
292
+ /*
293
+ * Built by hand rather than with `Response.redirect`.
294
+ *
295
+ * `Response.redirect` produces an immutable response whose headers
296
+ * cannot be added to, and the redirect that enables Val is precisely
297
+ * the one that has to set cookies on its way out.
298
+ */
299
+ return new Response(null, {
300
+ status: valRes.status,
301
+ headers: headers
302
+ });
303
+ }
304
+ return new Response("body" in valRes ? valRes.body : null, {
305
+ headers: headers,
306
+ status: valRes.status
307
+ });
308
+ }));
309
+ }
310
+ }, _callee);
311
+ }));
312
+ return function loadRouter() {
313
+ return _ref.apply(this, arguments);
314
+ };
315
+ }();
316
+ /*
317
+ * Built now in `fs` and `memory` mode, at the first request in `http` mode.
318
+ *
319
+ * In the first two the newest server built in a process is the build its
320
+ * patches are relative to (`ValOps.chainBaseSha`): a previous build still
321
+ * answering a long poll after a reload names the newest one's base, which
322
+ * only works if the newest one exists. In `http` mode the content service
323
+ * says which build is live, so nothing depends on when this one is built --
324
+ * and a deployed site's cold start does not pay for it.
325
+ */
326
+ if (!internal.serverCanStartLazily(options)) {
327
+ router = loadRouter();
328
+ // Reported by the first request, not as an unhandled rejection at start.
329
+ router["catch"](function () {});
330
+ }
331
+ return /*#__PURE__*/function () {
332
+ var _ref2 = asyncToGenerator._asyncToGenerator(/*#__PURE__*/asyncToGenerator._regenerator().m(function _callee2(req) {
333
+ var _t;
334
+ return asyncToGenerator._regenerator().w(function (_context2) {
335
+ while (1) switch (_context2.n) {
336
+ case 0:
337
+ // Forgotten when it fails, so the next request tries again rather than
338
+ // failing on the same rejected promise for the life of the process.
339
+ router !== null && router !== void 0 ? router : router = loadRouter()["catch"](function (err) {
340
+ router = null;
341
+ throw err;
342
+ });
343
+ _context2.n = 1;
344
+ return router;
345
+ case 1:
346
+ _t = _context2.v;
347
+ return _context2.a(2, _t(req));
348
+ }
349
+ }, _callee2);
350
+ }));
351
+ return function (_x) {
352
+ return _ref2.apply(this, arguments);
353
+ };
354
+ }();
288
355
  };
289
356
 
290
357
  /**
@@ -342,7 +409,8 @@ function initValServer(valModules, config, opts) {
342
409
  /**
343
410
  * What the readers read: this process's runtime for the app's `val.modules`
344
411
  * (`createValRuntime`), the same one `initValServer`'s server is built on.
345
- * Its head is the draft, read with the request's session.
412
+ * Its head is the draft, read with the request's session. Asked for by the
413
+ * first draft read, never by a visitor's.
346
414
  */
347
415
 
348
416
  /** What a misuse of a reader is told, beside what went wrong. */
@@ -370,7 +438,7 @@ function requestEnv(enabled, draft, snapshot) {
370
438
  function fetchValWithReads(deps) {
371
439
  return /*#__PURE__*/function () {
372
440
  var _ref = asyncToGenerator._asyncToGenerator(/*#__PURE__*/asyncToGenerator._regenerator().m(function _callee(selector, opts) {
373
- var _yield$draftForReques, enabled, draft, env;
441
+ var _yield$draftForReques, enabled, draft, env, _t, _t2, _t3;
374
442
  return asyncToGenerator._regenerator().w(function (_context) {
375
443
  while (1) switch (_context.n) {
376
444
  case 0:
@@ -388,7 +456,13 @@ function fetchValWithReads(deps) {
388
456
  _yield$draftForReques = _context.v;
389
457
  enabled = _yield$draftForReques.enabled;
390
458
  draft = _yield$draftForReques.draft;
391
- env = requestEnv(enabled, draft, deps.snapshot);
459
+ _t = requestEnv;
460
+ _t2 = enabled;
461
+ _t3 = draft;
462
+ _context.n = 2;
463
+ return deps.snapshot;
464
+ case 2:
465
+ env = _t(_t2, _t3, _context.v);
392
466
  return _context.a(2, internal$1.awaitResolved(function (settled) {
393
467
  return internal$1.resolveVal(selector, env, _typeof._objectSpread2({
394
468
  settled: settled
@@ -412,14 +486,13 @@ var initFetchValStega = function initFetchValStega(deps) {
412
486
  * What the readers render over the bundle: a proposal's saves over the
413
487
  * content version the site serves (`layeredSnapshot` in @valbuild/server),
414
488
  * either alone, or nothing.
489
+ *
490
+ * A promise because a saved `.jsonValues()` entry takes the compiler to place
491
+ * (`readerSnapshot`), which is loaded only then: a snapshot without one is
492
+ * ready at once, and no snapshot at all is `undefined`.
415
493
  */
416
- function snapshotForReaders(http) {
417
- var source = server.layeredSnapshot(
418
- // Not a module staged after the build serving the address -- its own
419
- // file, or a file it imports: its Source fits code these readers have
420
- // not got. See `proposalAsBuilt`.
421
- (http === null || http === void 0 ? void 0 : http.proposal) !== undefined ? internal.proposalAsBuilt(http.proposal, internal.servingBuildOf(http)) : undefined, http === null || http === void 0 ? void 0 : http.contentVersion);
422
- return source !== undefined ? server.proposalSnapshotForReaders(source, http === null || http === void 0 ? void 0 : http.projectSource) : undefined;
494
+ function snapshotForReaders(_x3) {
495
+ return _snapshotForReaders.apply(this, arguments);
423
496
  }
424
497
 
425
498
  /**
@@ -429,6 +502,23 @@ function snapshotForReaders(http) {
429
502
  * how the platform tells that an app cannot be served a version its build does
430
503
  * not bundle -- an unknown option is otherwise ignored, silently.
431
504
  */
505
+ function _snapshotForReaders() {
506
+ _snapshotForReaders = asyncToGenerator._asyncToGenerator(/*#__PURE__*/asyncToGenerator._regenerator().m(function _callee7(http) {
507
+ var source;
508
+ return asyncToGenerator._regenerator().w(function (_context7) {
509
+ while (1) switch (_context7.n) {
510
+ case 0:
511
+ source = internal.layeredSnapshot(
512
+ // Not a module staged after the build serving the address -- its own
513
+ // file, or a file it imports: its Source fits code these readers have
514
+ // not got. See `proposalAsBuilt`.
515
+ (http === null || http === void 0 ? void 0 : http.proposal) !== undefined ? internal.proposalAsBuilt(http.proposal, internal.servingBuildOf(http)) : undefined, http === null || http === void 0 ? void 0 : http.contentVersion);
516
+ return _context7.a(2, source !== undefined ? internal.readerSnapshot(source, http === null || http === void 0 ? void 0 : http.projectSource) : undefined);
517
+ }
518
+ }, _callee7);
519
+ }));
520
+ return _snapshotForReaders.apply(this, arguments);
521
+ }
432
522
  var CONTENT_APPLIED_GLOBAL = "__VAL_CONTENT_APPLIED";
433
523
 
434
524
  /**
@@ -463,22 +553,25 @@ var draftReadsOf = new WeakMap();
463
553
  * the browser, by what the editor holds -- old text first on every load.
464
554
  */
465
555
  var initFetchValDraft = function initFetchValDraft(deps) {
466
- var snapshot = deps.snapshot;
467
556
  var withReads = /*#__PURE__*/function () {
468
557
  var _ref2 = asyncToGenerator._asyncToGenerator(/*#__PURE__*/asyncToGenerator._regenerator().m(function _callee2() {
469
- var proposal, changes;
558
+ var snapshot, proposal, changes;
470
559
  return asyncToGenerator._regenerator().w(function (_context2) {
471
560
  while (1) switch (_context2.n) {
472
561
  case 0:
562
+ _context2.n = 1;
563
+ return deps.snapshot;
564
+ case 1:
565
+ snapshot = _context2.v;
473
566
  proposal = deps.atProposal === true ? {
474
567
  atProposal: true
475
568
  } : {};
476
- _context2.n = 1;
569
+ _context2.n = 2;
477
570
  return internal.readDraftChanges(deps);
478
- case 1:
571
+ case 2:
479
572
  changes = _context2.v;
480
573
  if (!(changes === null)) {
481
- _context2.n = 2;
574
+ _context2.n = 3;
482
575
  break;
483
576
  }
484
577
  return _context2.a(2, {
@@ -489,7 +582,7 @@ var initFetchValDraft = function initFetchValDraft(deps) {
489
582
  }, proposal) : null,
490
583
  reads: []
491
584
  });
492
- case 2:
585
+ case 3:
493
586
  return _context2.a(2, {
494
587
  value: _typeof._objectSpread2(_typeof._objectSpread2({
495
588
  sources: changes.sources,
@@ -530,7 +623,7 @@ var initFetchValRouteStega = function initFetchValRouteStega(deps) {
530
623
  var fetchValKey = initFetchValKeyStega(deps);
531
624
  return /*#__PURE__*/function () {
532
625
  var _ref4 = asyncToGenerator._asyncToGenerator(/*#__PURE__*/asyncToGenerator._regenerator().m(function _callee4(selector, params) {
533
- var resolvedParams, valModule, path, schema, url, content, val, _t;
626
+ var resolvedParams, valModule, path, schema, url, content, val, _t4;
534
627
  return asyncToGenerator._regenerator().w(function (_context4) {
535
628
  while (1) switch (_context4.n) {
536
629
  case 0:
@@ -561,22 +654,22 @@ var initFetchValRouteStega = function initFetchValRouteStega(deps) {
561
654
  content = _context4.v;
562
655
  return _context4.a(2, content === undefined ? null : content);
563
656
  case 4:
564
- _t = valModule;
565
- if (!_t) {
657
+ _t4 = valModule;
658
+ if (!_t4) {
566
659
  _context4.n = 6;
567
660
  break;
568
661
  }
569
662
  _context4.n = 5;
570
663
  return fetchVal(valModule);
571
664
  case 5:
572
- _t = _context4.v.value;
665
+ _t4 = _context4.v.value;
573
666
  case 6:
574
- val = _t;
667
+ val = _t4;
575
668
  return _context4.a(2, routeFromVal.initValRouteFromVal(resolvedParams, "fetchValRoute", path, schema, val));
576
669
  }
577
670
  }, _callee4);
578
671
  }));
579
- return function (_x3, _x4) {
672
+ return function (_x4, _x5) {
580
673
  return _ref4.apply(this, arguments);
581
674
  };
582
675
  }();
@@ -597,7 +690,7 @@ var initFetchValRouteStega = function initFetchValRouteStega(deps) {
597
690
  var initFetchValKeyStega = function initFetchValKeyStega(deps) {
598
691
  return /*#__PURE__*/function () {
599
692
  var _ref5 = asyncToGenerator._asyncToGenerator(/*#__PURE__*/asyncToGenerator._regenerator().m(function _callee5(selector, key) {
600
- var _yield$draftForReques2, enabled, draft, env;
693
+ var _yield$draftForReques2, enabled, draft, env, _t5, _t6, _t7;
601
694
  return asyncToGenerator._regenerator().w(function (_context5) {
602
695
  while (1) switch (_context5.n) {
603
696
  case 0:
@@ -611,19 +704,25 @@ var initFetchValKeyStega = function initFetchValKeyStega(deps) {
611
704
  _yield$draftForReques2 = _context5.v;
612
705
  enabled = _yield$draftForReques2.enabled;
613
706
  draft = _yield$draftForReques2.draft;
614
- env = requestEnv(enabled, draft, deps.snapshot);
707
+ _t5 = requestEnv;
708
+ _t6 = enabled;
709
+ _t7 = draft;
615
710
  _context5.n = 2;
711
+ return deps.snapshot;
712
+ case 2:
713
+ env = _t5(_t6, _t7, _context5.v);
714
+ _context5.n = 3;
616
715
  return internal$1.awaitResolved(function (settled) {
617
716
  return internal$1.resolveValEntry(selector, key, env, {
618
717
  settled: settled
619
718
  });
620
719
  });
621
- case 2:
720
+ case 3:
622
721
  return _context5.a(2, _context5.v.value);
623
722
  }
624
723
  }, _callee5);
625
724
  }));
626
- return function (_x5, _x6) {
725
+ return function (_x6, _x7) {
627
726
  return _ref5.apply(this, arguments);
628
727
  };
629
728
  }();
@@ -632,7 +731,7 @@ var initFetchValRouteUrl = function initFetchValRouteUrl(deps) {
632
731
  var fetchVal = fetchValWithReads(deps);
633
732
  return /*#__PURE__*/function () {
634
733
  var _ref6 = asyncToGenerator._asyncToGenerator(/*#__PURE__*/asyncToGenerator._regenerator().m(function _callee6(selector, params) {
635
- var resolvedParams, valModule, val, _t2, _t3;
734
+ var resolvedParams, valModule, val, _t8, _t9;
636
735
  return asyncToGenerator._regenerator().w(function (_context6) {
637
736
  while (1) switch (_context6.n) {
638
737
  case 0:
@@ -640,22 +739,22 @@ var initFetchValRouteUrl = function initFetchValRouteUrl(deps) {
640
739
  _context6.n = 1;
641
740
  break;
642
741
  }
643
- _t2 = undefined;
742
+ _t8 = undefined;
644
743
  _context6.n = 3;
645
744
  break;
646
745
  case 1:
647
746
  _context6.n = 2;
648
747
  return Promise.resolve(params);
649
748
  case 2:
650
- _t2 = _context6.v;
749
+ _t8 = _context6.v;
651
750
  case 3:
652
- resolvedParams = _t2;
751
+ resolvedParams = _t8;
653
752
  // A view is a pointer: everything below reads a path, a schema and a source
654
753
  // off this, and a pointer has none of them. See `resolveViewedModule`.
655
754
  valModule = core.Internal.resolveViewedModule(selector); // The url is made of the params and the module's path: a `.jsonValues()`
656
755
  // router's entries are not imported for it.
657
- _t3 = valModule;
658
- if (!_t3) {
756
+ _t9 = valModule;
757
+ if (!_t9) {
659
758
  _context6.n = 5;
660
759
  break;
661
760
  }
@@ -664,14 +763,14 @@ var initFetchValRouteUrl = function initFetchValRouteUrl(deps) {
664
763
  entries: false
665
764
  });
666
765
  case 4:
667
- _t3 = _context6.v.value;
766
+ _t9 = _context6.v.value;
668
767
  case 5:
669
- val = _t3;
768
+ val = _t9;
670
769
  return _context6.a(2, routeFromVal.getValRouteUrlFromVal(resolvedParams, "fetchValRouteUrl", valModule && core.Internal.getValPath(valModule), valModule && core.Internal.getSchema(valModule), val));
671
770
  }
672
771
  }, _callee6);
673
772
  }));
674
- return function (_x7, _x8) {
773
+ return function (_x8, _x9) {
675
774
  return _ref6.apply(this, arguments);
676
775
  };
677
776
  }();
@@ -690,25 +789,25 @@ function requestHeaders() {
690
789
  }
691
790
  /** The request's cookies, in the `{ name, value }` shape the readers want. */
692
791
  function _requestHeaders() {
693
- _requestHeaders = asyncToGenerator._asyncToGenerator(/*#__PURE__*/asyncToGenerator._regenerator().m(function _callee7() {
792
+ _requestHeaders = asyncToGenerator._asyncToGenerator(/*#__PURE__*/asyncToGenerator._regenerator().m(function _callee8() {
694
793
  var _yield$import, getRequestHeaders, headers;
695
- return asyncToGenerator._regenerator().w(function (_context7) {
696
- while (1) switch (_context7.n) {
794
+ return asyncToGenerator._regenerator().w(function (_context8) {
795
+ while (1) switch (_context8.n) {
697
796
  case 0:
698
- _context7.n = 1;
797
+ _context8.n = 1;
699
798
  return Promise.resolve().then(function () { return /*#__PURE__*/_interopNamespace(require('@tanstack/react-start/server')); });
700
799
  case 1:
701
- _yield$import = _context7.v;
800
+ _yield$import = _context8.v;
702
801
  getRequestHeaders = _yield$import.getRequestHeaders;
703
802
  headers = getRequestHeaders();
704
- return _context7.a(2, {
803
+ return _context8.a(2, {
705
804
  get: function get(name) {
706
805
  var _headers$get;
707
806
  return (_headers$get = headers.get(name)) !== null && _headers$get !== void 0 ? _headers$get : null;
708
807
  }
709
808
  });
710
809
  }
711
- }, _callee7);
810
+ }, _callee8);
712
811
  }));
713
812
  return _requestHeaders.apply(this, arguments);
714
813
  }
@@ -736,17 +835,17 @@ function requestCookies() {
736
835
  * } = initValContent(config, valModules);
737
836
  */
738
837
  function _requestCookies() {
739
- _requestCookies = asyncToGenerator._asyncToGenerator(/*#__PURE__*/asyncToGenerator._regenerator().m(function _callee8() {
838
+ _requestCookies = asyncToGenerator._asyncToGenerator(/*#__PURE__*/asyncToGenerator._regenerator().m(function _callee9() {
740
839
  var _yield$import2, getCookie;
741
- return asyncToGenerator._regenerator().w(function (_context8) {
742
- while (1) switch (_context8.n) {
840
+ return asyncToGenerator._regenerator().w(function (_context9) {
841
+ while (1) switch (_context9.n) {
743
842
  case 0:
744
- _context8.n = 1;
843
+ _context9.n = 1;
745
844
  return Promise.resolve().then(function () { return /*#__PURE__*/_interopNamespace(require('@tanstack/react-start/server')); });
746
845
  case 1:
747
- _yield$import2 = _context8.v;
846
+ _yield$import2 = _context9.v;
748
847
  getCookie = _yield$import2.getCookie;
749
- return _context8.a(2, {
848
+ return _context9.a(2, {
750
849
  get: function get(name) {
751
850
  var value = getCookie(name);
752
851
  return value === undefined ? undefined : {
@@ -756,7 +855,7 @@ function _requestCookies() {
756
855
  }
757
856
  });
758
857
  }
759
- }, _callee8);
858
+ }, _callee9);
760
859
  }));
761
860
  return _requestCookies.apply(this, arguments);
762
861
  }
@@ -771,6 +870,9 @@ function initValContent(config, valModules, opts) {
771
870
  throw new Error("Could not get @valbuild/tanstack package version");
772
871
  }
773
872
  var snapshot = snapshotForReaders(opts === null || opts === void 0 ? void 0 : opts.http);
873
+ // Read once per process and awaited by every read; a failure is each
874
+ // read's to report, not an unhandled rejection at startup.
875
+ snapshot["catch"](function () {});
774
876
  markContentApplied(opts === null || opts === void 0 ? void 0 : opts.http);
775
877
  var draftMode = (_opts$draftMode = opts === null || opts === void 0 ? void 0 : opts.draftMode) !== null && _opts$draftMode !== void 0 ? _opts$draftMode : valDraftMode();
776
878
  var isEnabled = function isEnabled() {
@@ -779,29 +881,36 @@ function initValContent(config, valModules, opts) {
779
881
 
780
882
  // One runtime per process for these modules: `initValServer`'s server is
781
883
  // built on the same one when it is given the same options, so a save
782
- // through the API moves what these read.
783
- var runtime = internal.createValRuntime(valModules, valApiEndpoints, _typeof._objectSpread2(_typeof._objectSpread2(_typeof._objectSpread2(_typeof._objectSpread2(_typeof._objectSpread2({
784
- versions: {
785
- // `next` is the field name in the wire contract; see initValServer.
786
- next: tanstackVersion,
787
- core: coreVersion,
788
- framework: "tanstack"
789
- }
790
- }, config), (opts === null || opts === void 0 ? void 0 : opts.sourceFiles) !== undefined ? {
791
- sourceFiles: opts.sourceFiles
792
- } : {}), (opts === null || opts === void 0 ? void 0 : opts.patchStore) !== undefined ? {
793
- patchStore: opts.patchStore
794
- } : {}), (opts === null || opts === void 0 ? void 0 : opts.unsafelyAllowUnauthenticated) !== undefined ? {
795
- unsafelyAllowUnauthenticated: opts.unsafelyAllowUnauthenticated
796
- } : {}), (opts === null || opts === void 0 ? void 0 : opts.http) !== undefined ? opts.http : {}), config);
797
- var deps = _typeof._objectSpread2({
884
+ // through the API moves what these read. Built by the first draft read and
885
+ // not before: building it evaluates the data layer, which a visitor's page
886
+ // never needs.
887
+ var started = null;
888
+ var runtime = function runtime() {
889
+ return started !== null && started !== void 0 ? started : started = startRuntime();
890
+ };
891
+ var startRuntime = function startRuntime() {
892
+ return internal.createValRuntime(valModules, valApiEndpoints, _typeof._objectSpread2(_typeof._objectSpread2(_typeof._objectSpread2(_typeof._objectSpread2(_typeof._objectSpread2({
893
+ versions: {
894
+ // `next` is the field name in the wire contract; see initValServer.
895
+ next: tanstackVersion,
896
+ core: coreVersion,
897
+ framework: "tanstack"
898
+ }
899
+ }, config), (opts === null || opts === void 0 ? void 0 : opts.sourceFiles) !== undefined ? {
900
+ sourceFiles: opts.sourceFiles
901
+ } : {}), (opts === null || opts === void 0 ? void 0 : opts.patchStore) !== undefined ? {
902
+ patchStore: opts.patchStore
903
+ } : {}), (opts === null || opts === void 0 ? void 0 : opts.unsafelyAllowUnauthenticated) !== undefined ? {
904
+ unsafelyAllowUnauthenticated: opts.unsafelyAllowUnauthenticated
905
+ } : {}), (opts === null || opts === void 0 ? void 0 : opts.http) !== undefined ? opts.http : {}), config);
906
+ };
907
+ var deps = {
798
908
  runtime: runtime,
799
909
  isEnabled: isEnabled,
800
910
  getHeaders: requestHeaders,
801
- getCookies: requestCookies
802
- }, snapshot !== undefined ? {
911
+ getCookies: requestCookies,
803
912
  snapshot: snapshot
804
- } : {});
913
+ };
805
914
  var fetchValStega = initFetchValStega(deps);
806
915
  runtimesOf.set(fetchValStega, runtime);
807
916
  return {
@@ -826,37 +935,137 @@ function initValContent(config, valModules, opts) {
826
935
  *
827
936
  * The version goes in the `next` field because that is the field name in the
828
937
  * wire contract; see `initValServer`.
938
+ *
939
+ * `@valbuild/mcp` is loaded by the first MCP request, not by this module: it
940
+ * brings Val's whole server (the tools run against its data layer), and an
941
+ * app's MCP route is imported by TanStack Start's route tree on every cold
942
+ * start, so a static import made every visitor's first request pay for it. The
943
+ * metadata document is the one part that must exist up front, and comes from
944
+ * `@valbuild/mcp/metadata`, which is light.
829
945
  */
830
946
  function initValMcp(valModules, config, opts) {
831
947
  var tanstackVersion = version.VERSION;
832
948
  if (!tanstackVersion) {
833
949
  throw new Error("Could not get @valbuild/tanstack package version");
834
950
  }
835
- return mcp.initValMcp(valModules, config, _typeof._objectSpread2(_typeof._objectSpread2({}, opts), {}, {
836
- versions: {
837
- next: tanstackVersion
951
+ var mcp = null;
952
+ var loadMcp = function loadMcp() {
953
+ // Forgotten when it fails, so the next request tries again.
954
+ mcp !== null && mcp !== void 0 ? mcp : mcp = Promise.resolve().then(function () { return /*#__PURE__*/_interopNamespace(require('@valbuild/mcp')); }).then(function (_ref) {
955
+ var initValMcpCore = _ref.initValMcp;
956
+ return initValMcpCore(valModules, config, _typeof._objectSpread2(_typeof._objectSpread2({}, opts), {}, {
957
+ versions: {
958
+ next: tanstackVersion
959
+ }
960
+ }));
961
+ })["catch"](function (err) {
962
+ mcp = null;
963
+ throw err;
964
+ });
965
+ return mcp;
966
+ };
967
+ return {
968
+ valMcpMetadata: metadata.valMcpMetadataOf(opts === null || opts === void 0 ? void 0 : opts.oauth),
969
+ valMcpTools: function valMcpTools() {
970
+ return asyncToGenerator._asyncToGenerator(/*#__PURE__*/asyncToGenerator._regenerator().m(function _callee() {
971
+ return asyncToGenerator._regenerator().w(function (_context) {
972
+ while (1) switch (_context.n) {
973
+ case 0:
974
+ _context.n = 1;
975
+ return loadMcp();
976
+ case 1:
977
+ return _context.a(2, _context.v.valMcpTools());
978
+ }
979
+ }, _callee);
980
+ }))();
981
+ },
982
+ valMcpAuthorize: function valMcpAuthorize(request) {
983
+ return asyncToGenerator._asyncToGenerator(/*#__PURE__*/asyncToGenerator._regenerator().m(function _callee2() {
984
+ return asyncToGenerator._regenerator().w(function (_context2) {
985
+ while (1) switch (_context2.n) {
986
+ case 0:
987
+ _context2.n = 1;
988
+ return loadMcp();
989
+ case 1:
990
+ return _context2.a(2, _context2.v.valMcpAuthorize(request));
991
+ }
992
+ }, _callee2);
993
+ }))();
838
994
  }
995
+ };
996
+ }
997
+
998
+ /** What `unstable_fetchValPreviewSource` returns; serializable, for a loader. */
999
+
1000
+ /**
1001
+ * EXPERIMENTAL: the content of the module `sourcePath` is in, as the app reads
1002
+ * it - for `Unstable_ValComponentPreview`, which renders the value at the path
1003
+ * with its component.
1004
+ *
1005
+ * Call it from a server function: it reads with the app's own `fetchVal`, so
1006
+ * an editor in draft mode gets the draft, with edit tags.
1007
+ *
1008
+ * The whole module rather than the value at the path, because which schema the
1009
+ * path ends in can depend on the content along the way (a union's variant), and
1010
+ * the browser has to walk it to find the component.
1011
+ */
1012
+ function unstable_fetchValPreviewSource(_x) {
1013
+ return _unstable_fetchValPreviewSource.apply(this, arguments);
1014
+ }
1015
+ function _unstable_fetchValPreviewSource() {
1016
+ _unstable_fetchValPreviewSource = asyncToGenerator._asyncToGenerator(/*#__PURE__*/asyncToGenerator._regenerator().m(function _callee(_ref) {
1017
+ var valModules, fetchVal, sourcePath, valModule, _t, _t2, _t3;
1018
+ return asyncToGenerator._regenerator().w(function (_context) {
1019
+ while (1) switch (_context.n) {
1020
+ case 0:
1021
+ valModules = _ref.valModules, fetchVal = _ref.fetchVal, sourcePath = _ref.sourcePath;
1022
+ _context.n = 1;
1023
+ return internal$1.findValModule(valModules, sourcePath);
1024
+ case 1:
1025
+ valModule = _context.v;
1026
+ if (valModule) {
1027
+ _context.n = 2;
1028
+ break;
1029
+ }
1030
+ return _context.a(2, {
1031
+ status: "error",
1032
+ message: "No module for '".concat(sourcePath, "' in val.modules.")
1033
+ });
1034
+ case 2:
1035
+ _t = sourcePath;
1036
+ _t2 = JSON;
1037
+ _context.n = 3;
1038
+ return fetchVal(valModule);
1039
+ case 3:
1040
+ _t3 = _t2.stringify.call(_t2, _context.v);
1041
+ return _context.a(2, {
1042
+ status: "ok",
1043
+ sourcePath: _t,
1044
+ moduleJson: _t3
1045
+ });
1046
+ }
1047
+ }, _callee);
839
1048
  }));
1049
+ return _unstable_fetchValPreviewSource.apply(this, arguments);
840
1050
  }
841
1051
 
842
1052
  // Fills the slot the root entry holds, so `initVal().isValEnabled()` works
843
1053
  // without the root entry importing TanStack's server module. See
844
1054
  // valEnableCookieBridge.ts.
845
1055
  version.setValEnableCookieReader(hasValEnableCookieOnServer);
846
- // Re-exported so an app that mounts the MCP endpoint through this package does
847
- // not also have to depend on `@valbuild/mcp` for the types it hands back.
848
1056
 
849
1057
  Object.defineProperty(exports, 'InMemoryPatchStore', {
850
1058
  enumerable: true,
851
- get: function () { return server.InMemoryPatchStore; }
1059
+ get: function () { return internal.InMemoryPatchStore; }
852
1060
  });
853
1061
  Object.defineProperty(exports, 'createPrettierFormatter', {
854
1062
  enumerable: true,
855
- get: function () { return server.createPrettierFormatter; }
1063
+ get: function () { return internal.createPrettierFormatter; }
856
1064
  });
857
1065
  exports.VAL_DRAFT_MODE_COOKIE = VAL_DRAFT_MODE_COOKIE;
858
1066
  exports.hasValEnableCookieOnServer = hasValEnableCookieOnServer;
859
1067
  exports.initValContent = initValContent;
860
1068
  exports.initValMcp = initValMcp;
861
1069
  exports.initValServer = initValServer;
1070
+ exports.unstable_fetchValPreviewSource = unstable_fetchValPreviewSource;
862
1071
  exports.valDraftMode = valDraftMode;