@valbuild/tanstack 0.154.1 → 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.
@@ -1,15 +1,14 @@
1
- import { V as VERSION, s as setValEnableCookieReader } from '../../dist/version-b7539dd8.esm.js';
1
+ import { V as VERSION, s as setValEnableCookieReader } from '../../dist/version-909919be.esm.js';
2
2
  import { _ as _asyncToGenerator, a as _regenerator } from '../../dist/asyncToGenerator-500f022f.esm.js';
3
3
  import { Internal } from '@valbuild/core';
4
4
  import { _ as _objectSpread2, a as _slicedToArray } from '../../dist/typeof-3e1a1d11.esm.js';
5
- import { createValApiRouter, createValServer, layeredSnapshot, proposalSnapshotForReaders } from '@valbuild/server';
6
- export { InMemoryPatchStore, createPrettierFormatter } from '@valbuild/server';
5
+ import { serverCanStartLazily, layeredSnapshot, proposalAsBuilt, servingBuildOf, readerSnapshot, createValRuntime, draftForRequest, readDraftChanges } from '@valbuild/server/internal';
6
+ export { InMemoryPatchStore, createPrettierFormatter } from '@valbuild/server/internal';
7
7
  import { SET_AUTO_TAG_JSX_ENABLED } from '@valbuild/react/stega';
8
- import { fetchValFrom, isJsonValuesRecordSchema, awaitResolved, resolveValEntry, snapshotSource, resolveVal } from '@valbuild/react/internal';
8
+ import { fetchValFrom, isJsonValuesRecordSchema, awaitResolved, resolveValEntry, snapshotSource, resolveVal, findValModule } from '@valbuild/react/internal';
9
9
  import { readerSchemaVersion } from '@valbuild/shared/internal';
10
- import { createValRuntime, proposalAsBuilt, servingBuildOf, draftForRequest, readDraftChanges } from '@valbuild/server/internal';
11
10
  import { i as initValRouteFromVal, g as getValRouteUrlFromVal } from '../../dist/routeFromVal-124235a5.esm.js';
12
- import { initValMcp as initValMcp$1 } from '@valbuild/mcp';
11
+ import { valMcpMetadataOf } from '@valbuild/mcp/metadata';
13
12
  import '@valbuild/shared/client';
14
13
 
15
14
  /**
@@ -175,6 +174,14 @@ function _hasValEnableCookieOnServer() {
175
174
  * a `Request` and hands back a `ValServerGenericResult` — so all this adds is
176
175
  * the conversion to a `Response`, which for Next needed `NextResponse` and here
177
176
  * does not need anything.
177
+ *
178
+ * `@valbuild/server` is imported by the first request to the API, not by this
179
+ * module. An app builds this handler in the `val.server.ts` its pages read
180
+ * content from, and TanStack Start's route tree imports the API route's module
181
+ * to match a URL at all, so a static import put the whole server -- the data
182
+ * layer, TypeScript, sucrase, and the Studio's assets, tens of megabytes -- in
183
+ * front of every visitor's first request to a cold isolate. See
184
+ * `servingPath.test.ts`.
178
185
  */
179
186
  var initValApiHandler = function initValApiHandler(valModules, config, draftMode, formatter, commitPrepared, sourceFiles, patchStore, unsafelyAllowUnauthenticated, apiKey, publishOverride, http, valContentUrl) {
180
187
  var route = "/api/val"; // TODO: get from config
@@ -186,7 +193,7 @@ var initValApiHandler = function initValApiHandler(valModules, config, draftMode
186
193
  if (!tanstackVersion) {
187
194
  throw new Error("Could not get @valbuild/tanstack package version");
188
195
  }
189
- return createValApiRouter(route, createValServer(valModules, route, _objectSpread2(_objectSpread2(_objectSpread2(_objectSpread2(_objectSpread2(_objectSpread2(_objectSpread2({
196
+ var options = _objectSpread2(_objectSpread2(_objectSpread2(_objectSpread2(_objectSpread2(_objectSpread2(_objectSpread2({
190
197
  versions: {
191
198
  /*
192
199
  * Reported as `next` because that is the field the wire contract has.
@@ -211,59 +218,119 @@ var initValApiHandler = function initValApiHandler(valModules, config, draftMode
211
218
  apiKey: apiKey
212
219
  } : {}), http !== undefined ? http : {}), valContentUrl !== undefined ? {
213
220
  valContentUrl: valContentUrl
214
- } : {}), config, {
215
- isEnabled: function isEnabled() {
216
- return draftMode.isEnabled();
217
- },
218
- onEnable: function onEnable() {
219
- return draftMode.enable();
220
- },
221
- onDisable: function onDisable() {
222
- return draftMode.disable();
223
- }
224
- }, formatter, commitPrepared, publishOverride), function (valRes) {
225
- var headers = new Headers();
226
- var valResHeaders = "headers" in valRes && valRes.headers || {};
227
- for (var key in valResHeaders) {
228
- var value = valResHeaders[key];
229
- if (typeof value === "string") {
230
- headers.set(key, value);
231
- }
232
- }
233
- if ("cookies" in valRes && valRes.cookies) {
234
- for (var _i = 0, _Object$entries = Object.entries(valRes.cookies); _i < _Object$entries.length; _i++) {
235
- var _Object$entries$_i = _slicedToArray(_Object$entries[_i], 2),
236
- cookieName = _Object$entries$_i[0],
237
- cookie = _Object$entries$_i[1];
238
- headers.append("Set-Cookie", serializeCookie(cookieName, cookie));
239
- }
240
- }
241
- if ("json" in valRes) {
242
- headers.set("Content-Type", "application/json");
243
- return new Response(JSON.stringify(valRes.json), {
244
- headers: headers,
245
- status: valRes.status
246
- });
247
- }
248
- if (valRes.status === 302) {
249
- headers.set("Location", valRes.redirectTo);
250
- /*
251
- * Built by hand rather than with `Response.redirect`.
252
- *
253
- * `Response.redirect` produces an immutable response whose headers
254
- * cannot be added to, and the redirect that enables Val is precisely
255
- * the one that has to set cookies on its way out.
256
- */
257
- return new Response(null, {
258
- status: valRes.status,
259
- headers: headers
260
- });
261
- }
262
- return new Response("body" in valRes ? valRes.body : null, {
263
- headers: headers,
264
- status: valRes.status
265
- });
266
- });
221
+ } : {});
222
+ var router = null;
223
+ var loadRouter = /*#__PURE__*/function () {
224
+ var _ref = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee() {
225
+ var _yield$import, createValApiRouter, createValServer;
226
+ return _regenerator().w(function (_context) {
227
+ while (1) switch (_context.n) {
228
+ case 0:
229
+ _context.n = 1;
230
+ return import('@valbuild/server');
231
+ case 1:
232
+ _yield$import = _context.v;
233
+ createValApiRouter = _yield$import.createValApiRouter;
234
+ createValServer = _yield$import.createValServer;
235
+ return _context.a(2, createValApiRouter(route, createValServer(valModules, route, options, 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 = _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
+ }));
288
+ }
289
+ }, _callee);
290
+ }));
291
+ return function loadRouter() {
292
+ return _ref.apply(this, arguments);
293
+ };
294
+ }();
295
+ /*
296
+ * Built now in `fs` and `memory` mode, at the first request in `http` mode.
297
+ *
298
+ * In the first two the newest server built in a process is the build its
299
+ * patches are relative to (`ValOps.chainBaseSha`): a previous build still
300
+ * answering a long poll after a reload names the newest one's base, which
301
+ * only works if the newest one exists. In `http` mode the content service
302
+ * says which build is live, so nothing depends on when this one is built --
303
+ * and a deployed site's cold start does not pay for it.
304
+ */
305
+ if (!serverCanStartLazily(options)) {
306
+ router = loadRouter();
307
+ // Reported by the first request, not as an unhandled rejection at start.
308
+ router["catch"](function () {});
309
+ }
310
+ return /*#__PURE__*/function () {
311
+ var _ref2 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee2(req) {
312
+ var _t;
313
+ return _regenerator().w(function (_context2) {
314
+ while (1) switch (_context2.n) {
315
+ case 0:
316
+ // Forgotten when it fails, so the next request tries again rather than
317
+ // failing on the same rejected promise for the life of the process.
318
+ router !== null && router !== void 0 ? router : router = loadRouter()["catch"](function (err) {
319
+ router = null;
320
+ throw err;
321
+ });
322
+ _context2.n = 1;
323
+ return router;
324
+ case 1:
325
+ _t = _context2.v;
326
+ return _context2.a(2, _t(req));
327
+ }
328
+ }, _callee2);
329
+ }));
330
+ return function (_x) {
331
+ return _ref2.apply(this, arguments);
332
+ };
333
+ }();
267
334
  };
268
335
 
269
336
  /**
@@ -321,7 +388,8 @@ function initValServer(valModules, config, opts) {
321
388
  /**
322
389
  * What the readers read: this process's runtime for the app's `val.modules`
323
390
  * (`createValRuntime`), the same one `initValServer`'s server is built on.
324
- * Its head is the draft, read with the request's session.
391
+ * Its head is the draft, read with the request's session. Asked for by the
392
+ * first draft read, never by a visitor's.
325
393
  */
326
394
 
327
395
  /** What a misuse of a reader is told, beside what went wrong. */
@@ -349,7 +417,7 @@ function requestEnv(enabled, draft, snapshot) {
349
417
  function fetchValWithReads(deps) {
350
418
  return /*#__PURE__*/function () {
351
419
  var _ref = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee(selector, opts) {
352
- var _yield$draftForReques, enabled, draft, env;
420
+ var _yield$draftForReques, enabled, draft, env, _t, _t2, _t3;
353
421
  return _regenerator().w(function (_context) {
354
422
  while (1) switch (_context.n) {
355
423
  case 0:
@@ -367,7 +435,13 @@ function fetchValWithReads(deps) {
367
435
  _yield$draftForReques = _context.v;
368
436
  enabled = _yield$draftForReques.enabled;
369
437
  draft = _yield$draftForReques.draft;
370
- env = requestEnv(enabled, draft, deps.snapshot);
438
+ _t = requestEnv;
439
+ _t2 = enabled;
440
+ _t3 = draft;
441
+ _context.n = 2;
442
+ return deps.snapshot;
443
+ case 2:
444
+ env = _t(_t2, _t3, _context.v);
371
445
  return _context.a(2, awaitResolved(function (settled) {
372
446
  return resolveVal(selector, env, _objectSpread2({
373
447
  settled: settled
@@ -391,14 +465,13 @@ var initFetchValStega = function initFetchValStega(deps) {
391
465
  * What the readers render over the bundle: a proposal's saves over the
392
466
  * content version the site serves (`layeredSnapshot` in @valbuild/server),
393
467
  * either alone, or nothing.
468
+ *
469
+ * A promise because a saved `.jsonValues()` entry takes the compiler to place
470
+ * (`readerSnapshot`), which is loaded only then: a snapshot without one is
471
+ * ready at once, and no snapshot at all is `undefined`.
394
472
  */
395
- function snapshotForReaders(http) {
396
- var source = layeredSnapshot(
397
- // Not a module staged after the build serving the address -- its own
398
- // file, or a file it imports: its Source fits code these readers have
399
- // not got. See `proposalAsBuilt`.
400
- (http === null || http === void 0 ? void 0 : http.proposal) !== undefined ? proposalAsBuilt(http.proposal, servingBuildOf(http)) : undefined, http === null || http === void 0 ? void 0 : http.contentVersion);
401
- return source !== undefined ? proposalSnapshotForReaders(source, http === null || http === void 0 ? void 0 : http.projectSource) : undefined;
473
+ function snapshotForReaders(_x3) {
474
+ return _snapshotForReaders.apply(this, arguments);
402
475
  }
403
476
 
404
477
  /**
@@ -408,6 +481,23 @@ function snapshotForReaders(http) {
408
481
  * how the platform tells that an app cannot be served a version its build does
409
482
  * not bundle -- an unknown option is otherwise ignored, silently.
410
483
  */
484
+ function _snapshotForReaders() {
485
+ _snapshotForReaders = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee7(http) {
486
+ var source;
487
+ return _regenerator().w(function (_context7) {
488
+ while (1) switch (_context7.n) {
489
+ case 0:
490
+ source = layeredSnapshot(
491
+ // Not a module staged after the build serving the address -- its own
492
+ // file, or a file it imports: its Source fits code these readers have
493
+ // not got. See `proposalAsBuilt`.
494
+ (http === null || http === void 0 ? void 0 : http.proposal) !== undefined ? proposalAsBuilt(http.proposal, servingBuildOf(http)) : undefined, http === null || http === void 0 ? void 0 : http.contentVersion);
495
+ return _context7.a(2, source !== undefined ? readerSnapshot(source, http === null || http === void 0 ? void 0 : http.projectSource) : undefined);
496
+ }
497
+ }, _callee7);
498
+ }));
499
+ return _snapshotForReaders.apply(this, arguments);
500
+ }
411
501
  var CONTENT_APPLIED_GLOBAL = "__VAL_CONTENT_APPLIED";
412
502
 
413
503
  /**
@@ -442,22 +532,25 @@ var draftReadsOf = new WeakMap();
442
532
  * the browser, by what the editor holds -- old text first on every load.
443
533
  */
444
534
  var initFetchValDraft = function initFetchValDraft(deps) {
445
- var snapshot = deps.snapshot;
446
535
  var withReads = /*#__PURE__*/function () {
447
536
  var _ref2 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee2() {
448
- var proposal, changes;
537
+ var snapshot, proposal, changes;
449
538
  return _regenerator().w(function (_context2) {
450
539
  while (1) switch (_context2.n) {
451
540
  case 0:
541
+ _context2.n = 1;
542
+ return deps.snapshot;
543
+ case 1:
544
+ snapshot = _context2.v;
452
545
  proposal = deps.atProposal === true ? {
453
546
  atProposal: true
454
547
  } : {};
455
- _context2.n = 1;
548
+ _context2.n = 2;
456
549
  return readDraftChanges(deps);
457
- case 1:
550
+ case 2:
458
551
  changes = _context2.v;
459
552
  if (!(changes === null)) {
460
- _context2.n = 2;
553
+ _context2.n = 3;
461
554
  break;
462
555
  }
463
556
  return _context2.a(2, {
@@ -468,7 +561,7 @@ var initFetchValDraft = function initFetchValDraft(deps) {
468
561
  }, proposal) : null,
469
562
  reads: []
470
563
  });
471
- case 2:
564
+ case 3:
472
565
  return _context2.a(2, {
473
566
  value: _objectSpread2(_objectSpread2({
474
567
  sources: changes.sources,
@@ -509,7 +602,7 @@ var initFetchValRouteStega = function initFetchValRouteStega(deps) {
509
602
  var fetchValKey = initFetchValKeyStega(deps);
510
603
  return /*#__PURE__*/function () {
511
604
  var _ref4 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee4(selector, params) {
512
- var resolvedParams, valModule, path, schema, url, content, val, _t;
605
+ var resolvedParams, valModule, path, schema, url, content, val, _t4;
513
606
  return _regenerator().w(function (_context4) {
514
607
  while (1) switch (_context4.n) {
515
608
  case 0:
@@ -540,22 +633,22 @@ var initFetchValRouteStega = function initFetchValRouteStega(deps) {
540
633
  content = _context4.v;
541
634
  return _context4.a(2, content === undefined ? null : content);
542
635
  case 4:
543
- _t = valModule;
544
- if (!_t) {
636
+ _t4 = valModule;
637
+ if (!_t4) {
545
638
  _context4.n = 6;
546
639
  break;
547
640
  }
548
641
  _context4.n = 5;
549
642
  return fetchVal(valModule);
550
643
  case 5:
551
- _t = _context4.v.value;
644
+ _t4 = _context4.v.value;
552
645
  case 6:
553
- val = _t;
646
+ val = _t4;
554
647
  return _context4.a(2, initValRouteFromVal(resolvedParams, "fetchValRoute", path, schema, val));
555
648
  }
556
649
  }, _callee4);
557
650
  }));
558
- return function (_x3, _x4) {
651
+ return function (_x4, _x5) {
559
652
  return _ref4.apply(this, arguments);
560
653
  };
561
654
  }();
@@ -576,7 +669,7 @@ var initFetchValRouteStega = function initFetchValRouteStega(deps) {
576
669
  var initFetchValKeyStega = function initFetchValKeyStega(deps) {
577
670
  return /*#__PURE__*/function () {
578
671
  var _ref5 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee5(selector, key) {
579
- var _yield$draftForReques2, enabled, draft, env;
672
+ var _yield$draftForReques2, enabled, draft, env, _t5, _t6, _t7;
580
673
  return _regenerator().w(function (_context5) {
581
674
  while (1) switch (_context5.n) {
582
675
  case 0:
@@ -590,19 +683,25 @@ var initFetchValKeyStega = function initFetchValKeyStega(deps) {
590
683
  _yield$draftForReques2 = _context5.v;
591
684
  enabled = _yield$draftForReques2.enabled;
592
685
  draft = _yield$draftForReques2.draft;
593
- env = requestEnv(enabled, draft, deps.snapshot);
686
+ _t5 = requestEnv;
687
+ _t6 = enabled;
688
+ _t7 = draft;
594
689
  _context5.n = 2;
690
+ return deps.snapshot;
691
+ case 2:
692
+ env = _t5(_t6, _t7, _context5.v);
693
+ _context5.n = 3;
595
694
  return awaitResolved(function (settled) {
596
695
  return resolveValEntry(selector, key, env, {
597
696
  settled: settled
598
697
  });
599
698
  });
600
- case 2:
699
+ case 3:
601
700
  return _context5.a(2, _context5.v.value);
602
701
  }
603
702
  }, _callee5);
604
703
  }));
605
- return function (_x5, _x6) {
704
+ return function (_x6, _x7) {
606
705
  return _ref5.apply(this, arguments);
607
706
  };
608
707
  }();
@@ -611,7 +710,7 @@ var initFetchValRouteUrl = function initFetchValRouteUrl(deps) {
611
710
  var fetchVal = fetchValWithReads(deps);
612
711
  return /*#__PURE__*/function () {
613
712
  var _ref6 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee6(selector, params) {
614
- var resolvedParams, valModule, val, _t2, _t3;
713
+ var resolvedParams, valModule, val, _t8, _t9;
615
714
  return _regenerator().w(function (_context6) {
616
715
  while (1) switch (_context6.n) {
617
716
  case 0:
@@ -619,22 +718,22 @@ var initFetchValRouteUrl = function initFetchValRouteUrl(deps) {
619
718
  _context6.n = 1;
620
719
  break;
621
720
  }
622
- _t2 = undefined;
721
+ _t8 = undefined;
623
722
  _context6.n = 3;
624
723
  break;
625
724
  case 1:
626
725
  _context6.n = 2;
627
726
  return Promise.resolve(params);
628
727
  case 2:
629
- _t2 = _context6.v;
728
+ _t8 = _context6.v;
630
729
  case 3:
631
- resolvedParams = _t2;
730
+ resolvedParams = _t8;
632
731
  // A view is a pointer: everything below reads a path, a schema and a source
633
732
  // off this, and a pointer has none of them. See `resolveViewedModule`.
634
733
  valModule = Internal.resolveViewedModule(selector); // The url is made of the params and the module's path: a `.jsonValues()`
635
734
  // router's entries are not imported for it.
636
- _t3 = valModule;
637
- if (!_t3) {
735
+ _t9 = valModule;
736
+ if (!_t9) {
638
737
  _context6.n = 5;
639
738
  break;
640
739
  }
@@ -643,14 +742,14 @@ var initFetchValRouteUrl = function initFetchValRouteUrl(deps) {
643
742
  entries: false
644
743
  });
645
744
  case 4:
646
- _t3 = _context6.v.value;
745
+ _t9 = _context6.v.value;
647
746
  case 5:
648
- val = _t3;
747
+ val = _t9;
649
748
  return _context6.a(2, getValRouteUrlFromVal(resolvedParams, "fetchValRouteUrl", valModule && Internal.getValPath(valModule), valModule && Internal.getSchema(valModule), val));
650
749
  }
651
750
  }, _callee6);
652
751
  }));
653
- return function (_x7, _x8) {
752
+ return function (_x8, _x9) {
654
753
  return _ref6.apply(this, arguments);
655
754
  };
656
755
  }();
@@ -669,25 +768,25 @@ function requestHeaders() {
669
768
  }
670
769
  /** The request's cookies, in the `{ name, value }` shape the readers want. */
671
770
  function _requestHeaders() {
672
- _requestHeaders = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee7() {
771
+ _requestHeaders = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee8() {
673
772
  var _yield$import, getRequestHeaders, headers;
674
- return _regenerator().w(function (_context7) {
675
- while (1) switch (_context7.n) {
773
+ return _regenerator().w(function (_context8) {
774
+ while (1) switch (_context8.n) {
676
775
  case 0:
677
- _context7.n = 1;
776
+ _context8.n = 1;
678
777
  return import('@tanstack/react-start/server');
679
778
  case 1:
680
- _yield$import = _context7.v;
779
+ _yield$import = _context8.v;
681
780
  getRequestHeaders = _yield$import.getRequestHeaders;
682
781
  headers = getRequestHeaders();
683
- return _context7.a(2, {
782
+ return _context8.a(2, {
684
783
  get: function get(name) {
685
784
  var _headers$get;
686
785
  return (_headers$get = headers.get(name)) !== null && _headers$get !== void 0 ? _headers$get : null;
687
786
  }
688
787
  });
689
788
  }
690
- }, _callee7);
789
+ }, _callee8);
691
790
  }));
692
791
  return _requestHeaders.apply(this, arguments);
693
792
  }
@@ -715,17 +814,17 @@ function requestCookies() {
715
814
  * } = initValContent(config, valModules);
716
815
  */
717
816
  function _requestCookies() {
718
- _requestCookies = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee8() {
817
+ _requestCookies = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee9() {
719
818
  var _yield$import2, getCookie;
720
- return _regenerator().w(function (_context8) {
721
- while (1) switch (_context8.n) {
819
+ return _regenerator().w(function (_context9) {
820
+ while (1) switch (_context9.n) {
722
821
  case 0:
723
- _context8.n = 1;
822
+ _context9.n = 1;
724
823
  return import('@tanstack/react-start/server');
725
824
  case 1:
726
- _yield$import2 = _context8.v;
825
+ _yield$import2 = _context9.v;
727
826
  getCookie = _yield$import2.getCookie;
728
- return _context8.a(2, {
827
+ return _context9.a(2, {
729
828
  get: function get(name) {
730
829
  var value = getCookie(name);
731
830
  return value === undefined ? undefined : {
@@ -735,7 +834,7 @@ function _requestCookies() {
735
834
  }
736
835
  });
737
836
  }
738
- }, _callee8);
837
+ }, _callee9);
739
838
  }));
740
839
  return _requestCookies.apply(this, arguments);
741
840
  }
@@ -750,6 +849,9 @@ function initValContent(config, valModules, opts) {
750
849
  throw new Error("Could not get @valbuild/tanstack package version");
751
850
  }
752
851
  var snapshot = snapshotForReaders(opts === null || opts === void 0 ? void 0 : opts.http);
852
+ // Read once per process and awaited by every read; a failure is each
853
+ // read's to report, not an unhandled rejection at startup.
854
+ snapshot["catch"](function () {});
753
855
  markContentApplied(opts === null || opts === void 0 ? void 0 : opts.http);
754
856
  var draftMode = (_opts$draftMode = opts === null || opts === void 0 ? void 0 : opts.draftMode) !== null && _opts$draftMode !== void 0 ? _opts$draftMode : valDraftMode();
755
857
  var isEnabled = function isEnabled() {
@@ -758,29 +860,36 @@ function initValContent(config, valModules, opts) {
758
860
 
759
861
  // One runtime per process for these modules: `initValServer`'s server is
760
862
  // built on the same one when it is given the same options, so a save
761
- // through the API moves what these read.
762
- var runtime = createValRuntime(valModules, valApiEndpoints, _objectSpread2(_objectSpread2(_objectSpread2(_objectSpread2(_objectSpread2({
763
- versions: {
764
- // `next` is the field name in the wire contract; see initValServer.
765
- next: tanstackVersion,
766
- core: coreVersion,
767
- framework: "tanstack"
768
- }
769
- }, config), (opts === null || opts === void 0 ? void 0 : opts.sourceFiles) !== undefined ? {
770
- sourceFiles: opts.sourceFiles
771
- } : {}), (opts === null || opts === void 0 ? void 0 : opts.patchStore) !== undefined ? {
772
- patchStore: opts.patchStore
773
- } : {}), (opts === null || opts === void 0 ? void 0 : opts.unsafelyAllowUnauthenticated) !== undefined ? {
774
- unsafelyAllowUnauthenticated: opts.unsafelyAllowUnauthenticated
775
- } : {}), (opts === null || opts === void 0 ? void 0 : opts.http) !== undefined ? opts.http : {}), config);
776
- var deps = _objectSpread2({
863
+ // through the API moves what these read. Built by the first draft read and
864
+ // not before: building it evaluates the data layer, which a visitor's page
865
+ // never needs.
866
+ var started = null;
867
+ var runtime = function runtime() {
868
+ return started !== null && started !== void 0 ? started : started = startRuntime();
869
+ };
870
+ var startRuntime = function startRuntime() {
871
+ return createValRuntime(valModules, valApiEndpoints, _objectSpread2(_objectSpread2(_objectSpread2(_objectSpread2(_objectSpread2({
872
+ versions: {
873
+ // `next` is the field name in the wire contract; see initValServer.
874
+ next: tanstackVersion,
875
+ core: coreVersion,
876
+ framework: "tanstack"
877
+ }
878
+ }, config), (opts === null || opts === void 0 ? void 0 : opts.sourceFiles) !== undefined ? {
879
+ sourceFiles: opts.sourceFiles
880
+ } : {}), (opts === null || opts === void 0 ? void 0 : opts.patchStore) !== undefined ? {
881
+ patchStore: opts.patchStore
882
+ } : {}), (opts === null || opts === void 0 ? void 0 : opts.unsafelyAllowUnauthenticated) !== undefined ? {
883
+ unsafelyAllowUnauthenticated: opts.unsafelyAllowUnauthenticated
884
+ } : {}), (opts === null || opts === void 0 ? void 0 : opts.http) !== undefined ? opts.http : {}), config);
885
+ };
886
+ var deps = {
777
887
  runtime: runtime,
778
888
  isEnabled: isEnabled,
779
889
  getHeaders: requestHeaders,
780
- getCookies: requestCookies
781
- }, snapshot !== undefined ? {
890
+ getCookies: requestCookies,
782
891
  snapshot: snapshot
783
- } : {});
892
+ };
784
893
  var fetchValStega = initFetchValStega(deps);
785
894
  runtimesOf.set(fetchValStega, runtime);
786
895
  return {
@@ -805,24 +914,123 @@ function initValContent(config, valModules, opts) {
805
914
  *
806
915
  * The version goes in the `next` field because that is the field name in the
807
916
  * wire contract; see `initValServer`.
917
+ *
918
+ * `@valbuild/mcp` is loaded by the first MCP request, not by this module: it
919
+ * brings Val's whole server (the tools run against its data layer), and an
920
+ * app's MCP route is imported by TanStack Start's route tree on every cold
921
+ * start, so a static import made every visitor's first request pay for it. The
922
+ * metadata document is the one part that must exist up front, and comes from
923
+ * `@valbuild/mcp/metadata`, which is light.
808
924
  */
809
925
  function initValMcp(valModules, config, opts) {
810
926
  var tanstackVersion = VERSION;
811
927
  if (!tanstackVersion) {
812
928
  throw new Error("Could not get @valbuild/tanstack package version");
813
929
  }
814
- return initValMcp$1(valModules, config, _objectSpread2(_objectSpread2({}, opts), {}, {
815
- versions: {
816
- next: tanstackVersion
930
+ var mcp = null;
931
+ var loadMcp = function loadMcp() {
932
+ // Forgotten when it fails, so the next request tries again.
933
+ mcp !== null && mcp !== void 0 ? mcp : mcp = import('@valbuild/mcp').then(function (_ref) {
934
+ var initValMcpCore = _ref.initValMcp;
935
+ return initValMcpCore(valModules, config, _objectSpread2(_objectSpread2({}, opts), {}, {
936
+ versions: {
937
+ next: tanstackVersion
938
+ }
939
+ }));
940
+ })["catch"](function (err) {
941
+ mcp = null;
942
+ throw err;
943
+ });
944
+ return mcp;
945
+ };
946
+ return {
947
+ valMcpMetadata: valMcpMetadataOf(opts === null || opts === void 0 ? void 0 : opts.oauth),
948
+ valMcpTools: function valMcpTools() {
949
+ return _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee() {
950
+ return _regenerator().w(function (_context) {
951
+ while (1) switch (_context.n) {
952
+ case 0:
953
+ _context.n = 1;
954
+ return loadMcp();
955
+ case 1:
956
+ return _context.a(2, _context.v.valMcpTools());
957
+ }
958
+ }, _callee);
959
+ }))();
960
+ },
961
+ valMcpAuthorize: function valMcpAuthorize(request) {
962
+ return _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee2() {
963
+ return _regenerator().w(function (_context2) {
964
+ while (1) switch (_context2.n) {
965
+ case 0:
966
+ _context2.n = 1;
967
+ return loadMcp();
968
+ case 1:
969
+ return _context2.a(2, _context2.v.valMcpAuthorize(request));
970
+ }
971
+ }, _callee2);
972
+ }))();
817
973
  }
974
+ };
975
+ }
976
+
977
+ /** What `unstable_fetchValPreviewSource` returns; serializable, for a loader. */
978
+
979
+ /**
980
+ * EXPERIMENTAL: the content of the module `sourcePath` is in, as the app reads
981
+ * it - for `Unstable_ValComponentPreview`, which renders the value at the path
982
+ * with its component.
983
+ *
984
+ * Call it from a server function: it reads with the app's own `fetchVal`, so
985
+ * an editor in draft mode gets the draft, with edit tags.
986
+ *
987
+ * The whole module rather than the value at the path, because which schema the
988
+ * path ends in can depend on the content along the way (a union's variant), and
989
+ * the browser has to walk it to find the component.
990
+ */
991
+ function unstable_fetchValPreviewSource(_x) {
992
+ return _unstable_fetchValPreviewSource.apply(this, arguments);
993
+ }
994
+ function _unstable_fetchValPreviewSource() {
995
+ _unstable_fetchValPreviewSource = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee(_ref) {
996
+ var valModules, fetchVal, sourcePath, valModule, _t, _t2, _t3;
997
+ return _regenerator().w(function (_context) {
998
+ while (1) switch (_context.n) {
999
+ case 0:
1000
+ valModules = _ref.valModules, fetchVal = _ref.fetchVal, sourcePath = _ref.sourcePath;
1001
+ _context.n = 1;
1002
+ return findValModule(valModules, sourcePath);
1003
+ case 1:
1004
+ valModule = _context.v;
1005
+ if (valModule) {
1006
+ _context.n = 2;
1007
+ break;
1008
+ }
1009
+ return _context.a(2, {
1010
+ status: "error",
1011
+ message: "No module for '".concat(sourcePath, "' in val.modules.")
1012
+ });
1013
+ case 2:
1014
+ _t = sourcePath;
1015
+ _t2 = JSON;
1016
+ _context.n = 3;
1017
+ return fetchVal(valModule);
1018
+ case 3:
1019
+ _t3 = _t2.stringify.call(_t2, _context.v);
1020
+ return _context.a(2, {
1021
+ status: "ok",
1022
+ sourcePath: _t,
1023
+ moduleJson: _t3
1024
+ });
1025
+ }
1026
+ }, _callee);
818
1027
  }));
1028
+ return _unstable_fetchValPreviewSource.apply(this, arguments);
819
1029
  }
820
1030
 
821
1031
  // Fills the slot the root entry holds, so `initVal().isValEnabled()` works
822
1032
  // without the root entry importing TanStack's server module. See
823
1033
  // valEnableCookieBridge.ts.
824
1034
  setValEnableCookieReader(hasValEnableCookieOnServer);
825
- // Re-exported so an app that mounts the MCP endpoint through this package does
826
- // not also have to depend on `@valbuild/mcp` for the types it hands back.
827
1035
 
828
- export { VAL_DRAFT_MODE_COOKIE, hasValEnableCookieOnServer, initValContent, initValMcp, initValServer, valDraftMode };
1036
+ export { VAL_DRAFT_MODE_COOKIE, hasValEnableCookieOnServer, initValContent, initValMcp, initValServer, unstable_fetchValPreviewSource, valDraftMode };