@valbuild/tanstack 0.150.22 → 0.151.0

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 (32) hide show
  1. package/CHANGELOG.md +198 -0
  2. package/README.md +11 -11
  3. package/client/dist/valbuild-tanstack-client.cjs.dev.js +100 -293
  4. package/client/dist/valbuild-tanstack-client.cjs.prod.js +100 -293
  5. package/client/dist/valbuild-tanstack-client.esm.js +99 -292
  6. package/dist/ValOverlayContext-1e50a557.cjs.prod.js +47 -0
  7. package/dist/ValOverlayContext-6826b4d9.esm.js +40 -0
  8. package/dist/ValOverlayContext-b18c2f18.cjs.dev.js +47 -0
  9. package/dist/declarations/src/ValDraft.d.ts +6 -50
  10. package/dist/declarations/src/ValModulesClient.d.ts +1 -3
  11. package/dist/declarations/src/ValProvider.d.ts +1 -1
  12. package/dist/declarations/src/client/initValClient.d.ts +25 -3
  13. package/dist/declarations/src/server/initValContent.d.ts +55 -169
  14. package/dist/{routeFromVal-06590f50.esm.js → routeFromVal-124235a5.esm.js} +3 -153
  15. package/dist/{routeFromVal-3c3915e2.cjs.dev.js → routeFromVal-2e834397.cjs.prod.js} +1 -156
  16. package/dist/{routeFromVal-b31a9e00.cjs.prod.js → routeFromVal-3d4d7c30.cjs.dev.js} +1 -156
  17. package/dist/{typeof-66e2d869.esm.js → typeof-3e1a1d11.esm.js} +1 -1
  18. package/dist/{typeof-8998e126.cjs.prod.js → typeof-56656438.cjs.prod.js} +0 -2
  19. package/dist/{typeof-dd0246a1.cjs.dev.js → typeof-bd6ec82c.cjs.dev.js} +0 -2
  20. package/dist/valbuild-tanstack.cjs.dev.js +162 -282
  21. package/dist/valbuild-tanstack.cjs.prod.js +162 -282
  22. package/dist/valbuild-tanstack.esm.js +162 -281
  23. package/dist/{version-dafb6d0d.esm.js → version-2b3824bf.esm.js} +2 -19
  24. package/dist/{version-cae4af97.cjs.dev.js → version-8e19c405.cjs.dev.js} +1 -19
  25. package/dist/{version-51de2c0e.cjs.prod.js → version-d46bc4bb.cjs.prod.js} +1 -19
  26. package/package.json +8 -8
  27. package/server/dist/valbuild-tanstack-server.cjs.dev.js +278 -1140
  28. package/server/dist/valbuild-tanstack-server.cjs.prod.js +278 -1140
  29. package/server/dist/valbuild-tanstack-server.esm.js +279 -1141
  30. package/dist/ValOverlayContext-198b4bd1.cjs.dev.js +0 -301
  31. package/dist/ValOverlayContext-6411bbe3.esm.js +0 -293
  32. package/dist/ValOverlayContext-d9b22d1a.cjs.prod.js +0 -301
@@ -1,12 +1,13 @@
1
- import { V as VERSION, _ as _toConsumableArray, s as setValEnableCookieReader } from '../../dist/version-dafb6d0d.esm.js';
1
+ import { V as VERSION, s as setValEnableCookieReader } from '../../dist/version-2b3824bf.esm.js';
2
2
  import { _ as _asyncToGenerator, a as _regenerator } from '../../dist/asyncToGenerator-500f022f.esm.js';
3
3
  import { Internal } from '@valbuild/core';
4
- import { _ as _objectSpread2, a as _slicedToArray, b as _createForOfIteratorHelper, g as _defineProperty, c as _typeof } from '../../dist/typeof-66e2d869.esm.js';
5
- import { createValApiRouter, createValServer, layeredSnapshot, proposalSnapshotForReaders, proposalSnapshot } from '@valbuild/server';
4
+ import { _ as _objectSpread2, a as _slicedToArray } from '../../dist/typeof-3e1a1d11.esm.js';
5
+ import { createValApiRouter, createValServer, layeredSnapshot, proposalSnapshotForReaders } from '@valbuild/server';
6
6
  export { InMemoryPatchStore, createPrettierFormatter } from '@valbuild/server';
7
- import { stegaEncode, SET_AUTO_TAG_JSX_ENABLED } from '@valbuild/react/stega';
8
- import { memoizePerRequest, VAL_SESSION_COOKIE, JSON_ENTRIES_BATCH_MAX } from '@valbuild/shared/internal';
9
- import { f as fillJsonValuesRecord, j as jsonValuesRecordToFill, c as initValRouteFromVal, g as getJsonEntryStegaRoot, i as isJsonValuesRecordSchema, a as getValRouteUrlFromVal, b as bundledJsonEntryImport } from '../../dist/routeFromVal-06590f50.esm.js';
7
+ import { SET_AUTO_TAG_JSX_ENABLED } from '@valbuild/react/stega';
8
+ import { fetchValFrom, isJsonValuesRecordSchema, awaitResolved, resolveValEntry, snapshotSource, resolveVal } from '@valbuild/react/internal';
9
+ import { createValRuntime, proposalAsBuilt, draftForRequest, readerSchemaVersion, readDraftChanges } from '@valbuild/server/internal';
10
+ import { i as initValRouteFromVal, g as getValRouteUrlFromVal } from '../../dist/routeFromVal-124235a5.esm.js';
10
11
  import { initValMcp as initValMcp$1 } from '@valbuild/mcp';
11
12
  import '@valbuild/shared/client';
12
13
 
@@ -307,405 +308,97 @@ function initValServer(valModules, config, opts) {
307
308
  * normal client element and the distinction does not exist.
308
309
  */
309
310
 
310
- /**
311
- * The `modules` map every `fetchVal` in one request shares.
312
- *
313
- * Derived from the route's own response type rather than restated, so it cannot
314
- * drift from what `/sources/~` actually returns.
311
+ /*
312
+ * The readers below are `resolveVal` (`@valbuild/react/stega`) over this
313
+ * request's sources: the draft (`serverDraftSource`, in `@valbuild/server`),
314
+ * then the proposal's or content version's snapshot, then the bundle. Only
315
+ * the sources are TanStack's; the rule that picks between them is the same in
316
+ * `@valbuild/next` and in the hooks. See `docs/plans/one-content-model.md`,
317
+ * "One way to load".
315
318
  */
316
319
 
317
320
  /**
318
- * The slice of `ValServer` the draft-sources reader uses.
319
- *
320
- * Narrower than `ValServer` for the same reason `JsonEntryValServer` below is:
321
- * it says what the dependency IS, and it lets a test drive the reader with a
322
- * one-route fake instead of casting a partial object to the whole server type.
321
+ * What the readers read: this process's runtime for the app's `val.modules`
322
+ * (`createValRuntime`), the same one `initValServer`'s server is built on.
323
+ * Its head is the draft, read with the request's session.
323
324
  */
324
325
 
325
- /** What the route readers need: the tree, plus the single-entry route. */
326
-
327
- /**
328
- * Where the reader gets its per-request memo box.
329
- *
330
- * Injected rather than reached for, so that "this reader needs a request to
331
- * scope its cache to" is part of its contract and a test can supply a scope it
332
- * controls — there is no TanStack request in jest.
333
- */
326
+ /** What a misuse of a reader is told, beside what went wrong. */
327
+ var WHERE = "fetchVal can only be used server-side (in a loader, a server function, or a server route). Use useVal in components.";
334
328
 
335
329
  /**
336
- * A per-request memo box, keyed on the `Request` itself.
337
- *
338
- * That is what makes this safe: TanStack resolves one `Request` object per
339
- * in-flight request out of async local storage, so two visitors cannot collide,
340
- * and a `WeakMap` lets the entry go when the request does. This is the TanStack
341
- * half of the Next package's `cache()` — there is no RSC here, so React's
342
- * request memoisation does not exist (see the note at the top of this file).
343
- *
344
- * Outside a request there is nothing to scope to and `getRequest` throws;
345
- * `null` then means "compute every time", which is what this did before the
346
- * memo existed. See `memoizePerRequest`.
347
- *
348
- * Built PER `initValContent` rather than once for the module, because the box
349
- * is only as specific as the map it came from: two Val servers in one process
350
- * sharing one would answer each other's reads within a request, and they hold
351
- * different content.
330
+ * What a read in this request reads, in order: the draft (in draft mode),
331
+ * then the snapshot, then -- added by every read -- the bundle. Edit tags
332
+ * are decided here, once, from draft mode.
352
333
  */
353
- function createTanStackRequestScope() {
354
- var boxes = new WeakMap();
355
- return /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee() {
356
- var request, _yield$import, getRequest, existing, box;
357
- return _regenerator().w(function (_context) {
358
- while (1) switch (_context.p = _context.n) {
359
- case 0:
360
- _context.p = 0;
361
- _context.n = 1;
362
- return import('@tanstack/react-start/server');
363
- case 1:
364
- _yield$import = _context.v;
365
- getRequest = _yield$import.getRequest;
366
- request = getRequest();
367
- _context.n = 3;
368
- break;
369
- case 2:
370
- _context.p = 2;
371
- _context.v;
372
- return _context.a(2, null);
373
- case 3:
374
- existing = boxes.get(request);
375
- if (!existing) {
376
- _context.n = 4;
377
- break;
378
- }
379
- return _context.a(2, existing);
380
- case 4:
381
- box = {};
382
- boxes.set(request, box);
383
- return _context.a(2, box);
384
- }
385
- }, _callee, null, [[0, 2]]);
386
- }));
334
+ function requestEnv(enabled, draft, snapshot) {
335
+ if (enabled) {
336
+ SET_AUTO_TAG_JSX_ENABLED(true);
337
+ }
338
+ return {
339
+ sources: draft === null ? [snapshotSource(snapshot)] : [draft, snapshotSource(snapshot)],
340
+ encode: enabled,
341
+ schemaVersionOf: readerSchemaVersion
342
+ };
387
343
  }
388
344
 
389
- /**
390
- * ONE read of the whole module tree, for the session in `sessionCookie`.
391
- *
392
- * `path` stays `"/"` on purpose, and the measurement is why. `/sources/~`
393
- * evaluates, previews and validates EVERY module and only THEN filters the
394
- * response by `req.path` (the two TODOs in `ValServer.ts` say so), so narrowing
395
- * the path shrinks the answer and changes the work not at all: on a 60-module
396
- * project, `path: "/"` took 53.6ms and a single-module path 53.5ms. And this is
397
- * an in-process call rather than a round trip, so the object it builds is never
398
- * serialised or sent anywhere — the bytes it saves are not bytes anyone pays
399
- * for. Narrowing would also give each caller a different answer to cache, which
400
- * is what would cost the saving that IS real (2.7x on the same project).
401
- *
402
- * `draftRead.perf.test.ts` prints both numbers; re-run it before believing
403
- * anything different.
404
- *
405
- * Returns `null` when the session is not one the server accepts; the caller
406
- * then renders published content, which is what it did before.
407
- */
408
- function loadDraftSources(_x, _x2) {
409
- return _loadDraftSources.apply(this, arguments);
410
- }
411
- function _loadDraftSources() {
412
- _loadDraftSources = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee8(valServerPromise, sessionCookie) {
413
- var valServer, treeRes;
414
- return _regenerator().w(function (_context8) {
415
- while (1) switch (_context8.n) {
416
- case 0:
417
- _context8.n = 1;
418
- return valServerPromise;
419
- case 1:
420
- valServer = _context8.v;
421
- _context8.n = 2;
422
- return valServer["/sources/~"]["PUT"]({
423
- path: "/",
424
- query: {
425
- validate_sources: true,
426
- validate_binary_files: false,
427
- exclude_patches: false,
428
- // The server-side read uses the legacy "server applies patches"
429
- // path, same as the Next package's.
430
- apply_patches: undefined
431
- },
432
- cookies: _defineProperty({}, VAL_SESSION_COOKIE, sessionCookie)
433
- });
434
- case 2:
435
- treeRes = _context8.v;
436
- if (!(treeRes.status === 200)) {
437
- _context8.n = 3;
438
- break;
439
- }
440
- return _context8.a(2, treeRes.json.modules);
441
- case 3:
442
- if (!(treeRes.status === 401)) {
443
- _context8.n = 4;
444
- break;
445
- }
446
- Internal.log.debug("authentication error: ", treeRes.json.message);
447
- return _context8.a(2, null);
448
- case 4:
449
- throw Error(JSON.stringify(treeRes.json, null, 2));
450
- case 5:
451
- return _context8.a(2);
452
- }
453
- }, _callee8);
454
- }));
455
- return _loadDraftSources.apply(this, arguments);
456
- }
457
- var initFetchValStega = function initFetchValStega(config, valApiEndpoints, valServerPromise, isEnabled, getHeaders, getCookies, getDraftSourcesScope, snapshot) {
458
- return function (selector) {
459
- var exec = /*#__PURE__*/function () {
460
- var _ref2 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee2() {
461
- var enabled, headers, cookies, host, _cookies, sessionCookie, modules, _t2, _t3, _t4, _t5, _t6, _t7, _t8, _t9, _t0, _t1, _t10, _t11;
462
- return _regenerator().w(function (_context2) {
463
- while (1) switch (_context2.p = _context2.n) {
464
- case 0:
465
- enabled = false;
466
- _context2.p = 1;
467
- _context2.n = 2;
468
- return isEnabled();
469
- case 2:
470
- enabled = _context2.v;
471
- _context2.n = 4;
472
- break;
473
- case 3:
474
- _context2.p = 3;
475
- _t2 = _context2.v;
476
- Internal.log.error("could not check if Val is enabled! This might be due to an error reading draft mode. fetchVal can only be used server-side (in a loader, a server function, or a server route). Use useVal in components.", _t2);
477
- case 4:
478
- if (!enabled) {
479
- _context2.n = 16;
480
- break;
481
- }
482
- SET_AUTO_TAG_JSX_ENABLED(true);
483
- _context2.p = 5;
484
- _context2.n = 6;
485
- return getHeaders();
486
- case 6:
487
- headers = _context2.v;
488
- if (!(typeof headers.get !== "function")) {
489
- _context2.n = 7;
490
- break;
491
- }
492
- throw new Error("Invalid headers");
493
- case 7:
494
- _context2.n = 9;
495
- break;
496
- case 8:
497
- _context2.p = 8;
498
- _t3 = _context2.v;
499
- Internal.log.error("could not read headers! fetchVal can only be used server-side (in a loader, a server function, or a server route). Use useVal in components.", _t3);
500
- headers = null;
501
- case 9:
502
- _context2.p = 9;
503
- _context2.n = 10;
504
- return getCookies();
505
- case 10:
506
- cookies = _context2.v;
507
- _context2.n = 12;
508
- break;
509
- case 11:
510
- _context2.p = 11;
511
- _t4 = _context2.v;
512
- Internal.log.error("could not read cookies! fetchVal can only be used server-side (in a loader, a server function, or a server route). Use useVal in components.", _t4);
513
- cookies = null;
514
- case 12:
515
- host = headers && getHost(headers);
516
- if (!(host && cookies)) {
517
- _context2.n = 16;
518
- break;
519
- }
520
- sessionCookie = (_cookies = cookies) === null || _cookies === void 0 || (_cookies = _cookies.get(VAL_SESSION_COOKIE)) === null || _cookies === void 0 ? void 0 : _cookies.value;
521
- /*
522
- * Once per request, however many times the page reads.
523
- *
524
- * Every `fetchVal` in one request asks the same question — same
525
- * query, same session — so the second and third answer were identical
526
- * and cost the same as the first. `fetchValRouteUrl` made that worse
527
- * by calling `fetchVal` again on top of the caller's own.
528
- *
529
- * The key is the session, so a box that somehow outlived its request
530
- * misses rather than serving another author's draft. See
531
- * `memoizePerRequest`.
532
- */
533
- _t5 = memoizePerRequest;
534
- _context2.n = 13;
535
- return getDraftSourcesScope();
536
- case 13:
537
- _context2.n = 14;
538
- return _t5(_context2.v, sessionCookie !== null && sessionCookie !== void 0 ? sessionCookie : "", function () {
539
- return loadDraftSources(valServerPromise, sessionCookie);
540
- });
541
- case 14:
542
- modules = _context2.v;
543
- if (!modules) {
544
- _context2.n = 16;
545
- break;
546
- }
547
- _t6 = stegaEncode;
548
- _t7 = selector;
549
- _t8 = !enabled;
550
- _context2.n = 15;
551
- return withJsonValuesEntries(selector, function (path) {
552
- var _modules$path;
553
- return [(_modules$path = modules[path]) === null || _modules$path === void 0 ? void 0 : _modules$path.source, snapshot === null || snapshot === void 0 ? void 0 : snapshot[path]];
554
- });
555
- case 15:
556
- _t9 = _context2.v;
557
- return _context2.a(2, _t6(_t7, {
558
- disabled: _t8,
559
- getModule: _t9
560
- }));
561
- case 16:
562
- _t0 = stegaEncode;
563
- _t1 = selector;
564
- _t10 = !enabled;
565
- _context2.n = 17;
566
- return withJsonValuesEntries(selector, function (path) {
567
- return [snapshot === null || snapshot === void 0 ? void 0 : snapshot[path]];
568
- });
569
- case 17:
570
- _t11 = _context2.v;
571
- return _context2.a(2, _t0(_t1, {
572
- disabled: _t10,
573
- getModule: _t11
574
- }));
575
- }
576
- }, _callee2, null, [[9, 11], [5, 8], [1, 3]]);
577
- }));
578
- return function exec() {
579
- return _ref2.apply(this, arguments);
580
- };
581
- }();
582
- return exec()["catch"](/*#__PURE__*/function () {
583
- var _ref3 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee3(err) {
584
- var _t12, _t13, _t14;
585
- return _regenerator().w(function (_context3) {
586
- while (1) switch (_context3.n) {
587
- case 0:
588
- Internal.log.error("failed to fetch ", err);
589
- _t12 = stegaEncode;
590
- _t13 = selector;
591
- _context3.n = 1;
592
- return withJsonValuesEntries(selector, function (path) {
593
- return [snapshot === null || snapshot === void 0 ? void 0 : snapshot[path]];
594
- })["catch"](function () {
595
- return function (path) {
596
- return snapshot === null || snapshot === void 0 ? void 0 : snapshot[path];
597
- };
598
- });
599
- case 1:
600
- _t14 = _context3.v;
601
- return _context3.a(2, _t12(_t13, {
602
- getModule: _t14
603
- }));
604
- }
605
- }, _callee3);
606
- }));
607
- return function (_x3) {
608
- return _ref3.apply(this, arguments);
609
- };
610
- }());
611
- };
612
- };
345
+ /** What a reader needs from the app and the request. */
613
346
 
614
- /**
615
- * `getModule` for `stegaEncode`, with a whole `.jsonValues()` record's entries
616
- * filled in: its own source holds only lazy markers, so the entries no draft or
617
- * snapshot has are imported from the bundle, together. Every other module, and
618
- * a selector that is not such a record, reads the first of `sourcesOf` that has
619
- * it, as before. `fetchValKey` is the reader for one entry.
620
- */
621
- function withJsonValuesEntries(_x4, _x5) {
622
- return _withJsonValuesEntries.apply(this, arguments);
623
- }
624
- /** A proposal's saved Source, by module: see `ValProposal` in `@valbuild/server`. */
625
- function _withJsonValuesEntries() {
626
- _withJsonValuesEntries = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee0(
627
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
628
- selector, sourcesOf) {
629
- var first, plan, loaded, filled, _t28;
630
- return _regenerator().w(function (_context0) {
631
- while (1) switch (_context0.n) {
632
- case 0:
633
- first = function first(path) {
634
- return sourcesOf(path).find(function (source) {
635
- return source !== undefined;
347
+ /** `fetchVal`, with what it read: see `readsOfFetchVal`. */
348
+ function fetchValWithReads(deps) {
349
+ return /*#__PURE__*/function () {
350
+ var _ref = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee(selector, opts) {
351
+ var _yield$draftForReques, enabled, draft, env;
352
+ return _regenerator().w(function (_context) {
353
+ while (1) switch (_context.n) {
354
+ case 0:
355
+ _context.n = 1;
356
+ return draftForRequest({
357
+ runtime: deps.runtime,
358
+ isEnabled: deps.isEnabled,
359
+ getCookies: deps.getCookies,
360
+ modules: {
361
+ getHeaders: deps.getHeaders
362
+ },
363
+ where: WHERE
636
364
  });
637
- };
638
- plan = jsonValuesRecordToFill(selector, sourcesOf);
639
- if (!(plan === null)) {
640
- _context0.n = 1;
641
- break;
642
- }
643
- return _context0.a(2, first);
644
- case 1:
645
- _t28 = Object;
646
- _context0.n = 2;
647
- return Promise.all(plan.load.map(/*#__PURE__*/function () {
648
- var _ref8 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee9(key) {
649
- var thunk, _t25, _t26, _t27;
650
- return _regenerator().w(function (_context9) {
651
- while (1) switch (_context9.n) {
652
- case 0:
653
- thunk = bundledJsonEntryImport(plan.module, key);
654
- _t25 = key;
655
- if (!thunk) {
656
- _context9.n = 2;
657
- break;
658
- }
659
- _context9.n = 1;
660
- return thunk();
661
- case 1:
662
- _t26 = _context9.v["default"];
663
- _context9.n = 3;
664
- break;
665
- case 2:
666
- _t26 = undefined;
667
- case 3:
668
- _t27 = _t26;
669
- return _context9.a(2, [_t25, _t27]);
670
- }
671
- }, _callee9);
365
+ case 1:
366
+ _yield$draftForReques = _context.v;
367
+ enabled = _yield$draftForReques.enabled;
368
+ draft = _yield$draftForReques.draft;
369
+ env = requestEnv(enabled, draft, deps.snapshot);
370
+ return _context.a(2, awaitResolved(function (settled) {
371
+ return resolveVal(selector, env, _objectSpread2({
372
+ settled: settled
373
+ }, opts));
672
374
  }));
673
- return function (_x23) {
674
- return _ref8.apply(this, arguments);
675
- };
676
- }()));
677
- case 2:
678
- loaded = _t28.fromEntries.call(_t28, _context0.v);
679
- filled = fillJsonValuesRecord(plan.record, loaded);
680
- return _context0.a(2, function (path) {
681
- return path === plan.modulePath ? filled : first(path);
682
- });
683
- }
684
- }, _callee0);
685
- }));
686
- return _withJsonValuesEntries.apply(this, arguments);
375
+ }
376
+ }, _callee);
377
+ }));
378
+ return function (_x, _x2) {
379
+ return _ref.apply(this, arguments);
380
+ };
381
+ }();
687
382
  }
383
+ var initFetchValStega = function initFetchValStega(deps) {
384
+ return fetchValFrom(fetchValWithReads(deps));
385
+ };
386
+
387
+ /** A proposal's saved Source, by module: see `ValProposal` in `@valbuild/server`. */
388
+
688
389
  /**
689
390
  * What the readers render over the bundle: a proposal's saves over the
690
391
  * content version the site serves (`layeredSnapshot` in @valbuild/server),
691
392
  * either alone, or nothing.
692
393
  */
693
394
  function snapshotForReaders(http) {
694
- var source = layeredSnapshot(http === null || http === void 0 ? void 0 : http.proposal, http === null || http === void 0 ? void 0 : http.contentVersion);
395
+ var source = layeredSnapshot(
396
+ // Not a module staged after the build serving the address: its Source
397
+ // fits code these readers have not got. See `proposalAsBuilt`.
398
+ (http === null || http === void 0 ? void 0 : http.proposal) !== undefined ? proposalAsBuilt(http.proposal) : undefined, http === null || http === void 0 ? void 0 : http.contentVersion);
695
399
  return source !== undefined ? proposalSnapshotForReaders(source, http === null || http === void 0 ? void 0 : http.projectSource) : undefined;
696
400
  }
697
401
 
698
- /**
699
- * What the server's own draft starts from, where it is not the bundle's: the
700
- * layers `ValOpsHttp` puts over it (`siteSnapshotSources`, then
701
- * `snapshotSources`), in the same form, so the Studio that starts from it
702
- * holds the base the server reports. See `ValDraft.base`.
703
- */
704
- function draftBase(http) {
705
- var layered = layeredSnapshot(http === null || http === void 0 ? void 0 : http.proposal, http === null || http === void 0 ? void 0 : http.contentVersion);
706
- return layered !== undefined ? proposalSnapshot(layered) : undefined;
707
- }
708
-
709
402
  /**
710
403
  * Where an isolate says which content version it applied, for the platform
711
404
  * serving it to read back after a render (valbuild/home: the loader answers it
@@ -726,796 +419,237 @@ function markContentApplied(http) {
726
419
  Reflect.set(globalThis, CONTENT_APPLIED_GLOBAL, http.contentVersion.id);
727
420
  }
728
421
 
729
- /** What `fetchValDraft` reads: the tree, the entries, and the patches' ops. */
422
+ /** The runtime behind a reader: see {@link runtimeOfReaders}. */
423
+ var runtimesOf = new WeakMap();
424
+
425
+ /** What the hooks' server render reads the draft from: see `fetchValDraft`. */
426
+ var draftReadsOf = new WeakMap();
730
427
 
731
428
  /**
732
429
  * The draft a page in this request should start from, or `null` for none.
733
430
  *
734
431
  * `null` -- and no read at all -- unless draft mode is on: a visitor pays one
735
- * cookie lookup. Otherwise the SAME read every
736
- * `fetchVal` in the request shares (`loadDraftSources`, memoised per request),
737
- * cut down to the modules the draft changes. See `ValDraft`.
432
+ * cookie lookup. Otherwise the runtime's head, which every `fetchVal` reads,
433
+ * cut down to the modules the draft changes, with the version of each. See
434
+ * `ValDraft`. The read is `readDraftChanges` in
435
+ * `@valbuild/server`, which `@valbuild/next/rsc`'s `fetchValDraft` is too;
436
+ * this adds a proposal's snapshot, which Next has not.
738
437
  *
739
438
  * Exists because the hooks render on the server too, and had no draft there:
740
439
  * a draft page was rendered as the published site and only then replaced, in
741
440
  * the browser, by what the editor holds -- old text first on every load.
742
441
  */
743
- var initFetchValDraft = function initFetchValDraft(valServerPromise, isEnabled, getCookies, getDraftSourcesScope, snapshot, base) {
744
- var atProposal = arguments.length > 6 && arguments[6] !== undefined ? arguments[6] : false;
745
- return /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee4() {
746
- var proposal, onlySnapshot, _yield$getCookies$get, sessionCookie, modules, sources, _i, _Object$entries, _Object$entries$_i, path, module, source, _t15, _t16, _t17, _t18, _t19;
747
- return _regenerator().w(function (_context4) {
748
- while (1) switch (_context4.p = _context4.n) {
749
- case 0:
750
- proposal = atProposal ? {
751
- atProposal: true
752
- } : {}; // At a proposal's address the page needs the snapshot whatever draft mode
753
- // says, so a reviewer who is not editing sees the proposal too. So does a
754
- // content version, which is the live site's content.
755
- onlySnapshot = snapshot !== undefined ? _objectSpread2({
756
- sources: {},
757
- snapshot: snapshot,
758
- draftMode: false
759
- }, proposal) : null;
760
- _context4.p = 1;
761
- _context4.n = 2;
762
- return isEnabled();
763
- case 2:
764
- if (_context4.v) {
765
- _context4.n = 3;
766
- break;
767
- }
768
- return _context4.a(2, onlySnapshot);
769
- case 3:
770
- _context4.n = 4;
771
- return getCookies();
772
- case 4:
773
- _t16 = _yield$getCookies$get = _context4.v.get(VAL_SESSION_COOKIE);
774
- _t15 = _t16 === null;
775
- if (_t15) {
776
- _context4.n = 5;
777
- break;
778
- }
779
- _t15 = _yield$getCookies$get === void 0;
780
- case 5:
781
- if (!_t15) {
782
- _context4.n = 6;
783
- break;
784
- }
785
- _t17 = void 0;
786
- _context4.n = 7;
787
- break;
788
- case 6:
789
- _t17 = _yield$getCookies$get.value;
790
- case 7:
791
- sessionCookie = _t17;
792
- _t18 = memoizePerRequest;
793
- _context4.n = 8;
794
- return getDraftSourcesScope();
795
- case 8:
796
- _context4.n = 9;
797
- return _t18(_context4.v, sessionCookie !== null && sessionCookie !== void 0 ? sessionCookie : "", function () {
798
- return loadDraftSources(valServerPromise, sessionCookie);
799
- });
800
- case 9:
801
- modules = _context4.v;
802
- if (modules) {
803
- _context4.n = 10;
804
- break;
805
- }
806
- return _context4.a(2, onlySnapshot);
807
- case 10:
808
- sources = {};
809
- _i = 0, _Object$entries = Object.entries(modules);
810
- case 11:
811
- if (!(_i < _Object$entries.length)) {
812
- _context4.n = 15;
813
- break;
814
- }
815
- _Object$entries$_i = _slicedToArray(_Object$entries[_i], 2), path = _Object$entries$_i[0], module = _Object$entries$_i[1];
816
- if (!(module.patches === undefined)) {
817
- _context4.n = 12;
818
- break;
819
- }
820
- return _context4.a(3, 14);
821
- case 12:
822
- _context4.n = 13;
823
- return withDraftJsonEntries(valServerPromise, sessionCookie, path, module.source, module.patches.applied);
824
- case 13:
825
- source = _context4.v;
826
- if (source !== undefined) {
827
- sources[path] = source;
828
- }
829
- case 14:
830
- _i++;
831
- _context4.n = 11;
832
- break;
833
- case 15:
834
- return _context4.a(2, _objectSpread2(_objectSpread2(_objectSpread2({
835
- sources: sources
836
- }, snapshot !== undefined ? {
837
- snapshot: snapshot
838
- } : {}), base !== undefined ? {
839
- base: base
840
- } : {}), proposal));
841
- case 16:
842
- _context4.p = 16;
843
- _t19 = _context4.v;
844
- // Published content, as before this existed: a draft page that renders
845
- // the site is a degraded preview, one that fails to render is an outage.
846
- Internal.log.error("could not read the draft", _t19);
847
- return _context4.a(2, onlySnapshot);
848
- }
849
- }, _callee4, null, [[1, 16]]);
850
- }));
851
- };
852
-
853
- /**
854
- * A module's draft source, as the browser would have received it, with the
855
- * `.jsonValues()` entries the draft edits filled in -- or `undefined` when
856
- * they could not all be read, so the module is left out of the draft.
857
- *
858
- * Two things, both because `/sources/~` keeps each such entry as a MARKER
859
- * (its content lives in its own file):
860
- *
861
- * - Read in-process, a marker still carries its `import()` thunk, which no
862
- * serializer can send. So the source is put in its wire shape first, the
863
- * same one the Studio gets over HTTP: a marker without its thunk, which the
864
- * hooks read as "no draft for this entry" and resolve from the bundle.
865
- * - An entry edited in the draft is still a marker there, so the page would
866
- * render the published entry and swap it once the Studio loads it. Those
867
- * entries -- the ones an applied patch touches, and only those -- are read
868
- * through `/json` and put in place of their markers, which is the shape
869
- * the Studio's own store gives them once it has.
870
- *
871
- * Leaving the module out is the published render that existed before the
872
- * draft did: worse than this, and never wrong for long.
873
- */
874
- function withDraftJsonEntries(_x6, _x7, _x8, _x9, _x0) {
875
- return _withDraftJsonEntries.apply(this, arguments);
876
- }
877
- function _withDraftJsonEntries() {
878
- _withDraftJsonEntries = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee1(valServerPromise, sessionCookie, moduleFilePath, source, appliedPatchIds) {
879
- var wire, valServer, cookies, patchesRes, edited, _iterator, _step, _step$value, path, patch, _iterator3, _step3, op, touched, _i2, _touched, _key, keys, draft, i, res, _iterator2, _step2, _step2$value, key, content, _t29, _t30;
880
- return _regenerator().w(function (_context1) {
881
- while (1) switch (_context1.p = _context1.n) {
882
- case 0:
883
- if (!(source === undefined)) {
884
- _context1.n = 1;
885
- break;
886
- }
887
- return _context1.a(2, undefined);
888
- case 1:
889
- wire = JSON.parse(JSON.stringify(source));
890
- if (!(!isRecordOfJsonMarkers(source) || appliedPatchIds.length === 0)) {
891
- _context1.n = 2;
892
- break;
893
- }
894
- return _context1.a(2, wire);
895
- case 2:
896
- _context1.n = 3;
897
- return valServerPromise;
898
- case 3:
899
- valServer = _context1.v;
900
- cookies = _defineProperty({}, VAL_SESSION_COOKIE, sessionCookie);
901
- _context1.n = 4;
902
- return valServer["/patches"]["GET"]({
903
- query: {
904
- patch_id: appliedPatchIds,
905
- exclude_patch_ops: false
906
- },
907
- cookies: cookies
908
- });
909
- case 4:
910
- patchesRes = _context1.v;
911
- if (!(patchesRes.status !== 200)) {
912
- _context1.n = 5;
913
- break;
914
- }
915
- return _context1.a(2, undefined);
916
- case 5:
917
- edited = new Set();
918
- _iterator = _createForOfIteratorHelper(patchesRes.json.patches);
919
- _context1.p = 6;
920
- _iterator.s();
921
- case 7:
922
- if ((_step = _iterator.n()).done) {
923
- _context1.n = 19;
924
- break;
925
- }
926
- _step$value = _step.value, path = _step$value.path, patch = _step$value.patch;
927
- if (!(path !== moduleFilePath)) {
928
- _context1.n = 8;
929
- break;
930
- }
931
- return _context1.a(3, 18);
932
- case 8:
933
- _iterator3 = _createForOfIteratorHelper(patch !== null && patch !== void 0 ? patch : []);
934
- _context1.p = 9;
935
- _iterator3.s();
936
- case 10:
937
- if ((_step3 = _iterator3.n()).done) {
938
- _context1.n = 15;
939
- break;
940
- }
941
- op = _step3.value;
942
- touched = [op.path[0]];
943
- if (op.op === "move" || op.op === "copy") {
944
- touched.push(op.from[0]);
945
- }
946
- _i2 = 0, _touched = touched;
947
- case 11:
948
- if (!(_i2 < _touched.length)) {
949
- _context1.n = 14;
950
- break;
951
- }
952
- _key = _touched[_i2];
953
- if (!(_key === undefined)) {
954
- _context1.n = 12;
955
- break;
956
- }
957
- return _context1.a(2, undefined);
958
- case 12:
959
- if (Internal.isJson(source[_key])) {
960
- edited.add(_key);
961
- }
962
- case 13:
963
- _i2++;
964
- _context1.n = 11;
965
- break;
966
- case 14:
967
- _context1.n = 10;
968
- break;
969
- case 15:
970
- _context1.n = 17;
971
- break;
972
- case 16:
973
- _context1.p = 16;
974
- _t29 = _context1.v;
975
- _iterator3.e(_t29);
976
- case 17:
977
- _context1.p = 17;
978
- _iterator3.f();
979
- return _context1.f(17);
980
- case 18:
981
- _context1.n = 7;
982
- break;
983
- case 19:
984
- _context1.n = 21;
985
- break;
986
- case 20:
987
- _context1.p = 20;
988
- _t30 = _context1.v;
989
- _iterator.e(_t30);
990
- case 21:
991
- _context1.p = 21;
992
- _iterator.f();
993
- return _context1.f(21);
994
- case 22:
995
- keys = _toConsumableArray(edited);
996
- draft = wire;
997
- i = 0;
998
- case 23:
999
- if (!(i < keys.length)) {
1000
- _context1.n = 27;
1001
- break;
1002
- }
1003
- _context1.n = 24;
1004
- return valServer["/json"]["GET"]({
1005
- query: {
1006
- path: moduleFilePath,
1007
- key: undefined,
1008
- keys: keys.slice(i, i + JSON_ENTRIES_BATCH_MAX),
1009
- offset: undefined,
1010
- limit: undefined,
1011
- apply_patches: true
1012
- },
1013
- cookies: cookies
1014
- });
1015
- case 24:
1016
- res = _context1.v;
1017
- if (!(res.status !== 200 || !("entries" in res.json) || res.json.errors.length > 0 || res.json.missing.length > 0)) {
1018
- _context1.n = 25;
1019
- break;
1020
- }
1021
- return _context1.a(2, undefined);
1022
- case 25:
1023
- _iterator2 = _createForOfIteratorHelper(res.json.entries);
1024
- try {
1025
- for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
1026
- _step2$value = _step2.value, key = _step2$value.key, content = _step2$value.content;
1027
- draft[key] = content;
442
+ var initFetchValDraft = function initFetchValDraft(deps) {
443
+ var snapshot = deps.snapshot;
444
+ var withReads = /*#__PURE__*/function () {
445
+ var _ref2 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee2() {
446
+ var proposal, changes;
447
+ return _regenerator().w(function (_context2) {
448
+ while (1) switch (_context2.n) {
449
+ case 0:
450
+ proposal = deps.atProposal === true ? {
451
+ atProposal: true
452
+ } : {};
453
+ _context2.n = 1;
454
+ return readDraftChanges(deps);
455
+ case 1:
456
+ changes = _context2.v;
457
+ if (!(changes === null)) {
458
+ _context2.n = 2;
459
+ break;
1028
460
  }
1029
- } catch (err) {
1030
- _iterator2.e(err);
1031
- } finally {
1032
- _iterator2.f();
1033
- }
1034
- case 26:
1035
- i += JSON_ENTRIES_BATCH_MAX;
1036
- _context1.n = 23;
1037
- break;
1038
- case 27:
1039
- return _context1.a(2, draft);
1040
- }
1041
- }, _callee1, null, [[9, 16, 17, 18], [6, 20, 21, 22]]);
1042
- }));
1043
- return _withDraftJsonEntries.apply(this, arguments);
1044
- }
1045
- function isRecordOfJsonMarkers(source) {
1046
- return _typeof(source) === "object" && source !== null && !Array.isArray(source) && Object.values(source).some(function (value) {
1047
- return Internal.isJson(value);
1048
- });
1049
- }
1050
- function getHost(headers) {
1051
- var _headers$get;
1052
- var host = headers === null || headers === void 0 ? void 0 : headers.get("host");
1053
- var proto = "https";
1054
- if ((headers === null || headers === void 0 ? void 0 : headers.get("x-forwarded-proto")) === "http") {
1055
- proto = "http";
1056
- } else if (headers !== null && headers !== void 0 && (_headers$get = headers.get("referer")) !== null && _headers$get !== void 0 && _headers$get.startsWith("http://")) {
1057
- proto = "http";
1058
- } else if (host !== null && host !== void 0 && host.startsWith("localhost")) {
1059
- proto = "http";
1060
- }
1061
- if (host && proto) {
1062
- return "".concat(proto, "://").concat(host);
1063
- }
1064
- return null;
1065
- }
1066
-
1067
- // TODO: remove
1068
- // function getValAuthHeaders(cookies: {
1069
- // get(name: string): { name: string; value: string } | undefined;
1070
- // }): Record<string, string> {
1071
- // try {
1072
- // const session = cookies.get(Internal.VAL_SESSION_COOKIE);
1073
- // if (session) {
1074
- // return {
1075
- // Cookie: `${Internal.VAL_SESSION_COOKIE}=${encodeURIComponent(
1076
- // session.value
1077
- // )}`,
1078
- // };
1079
- // }
1080
- // return {};
1081
- // } catch (err) {
1082
- // console.error(
1083
- // "Val: could not read cookies! fetchVal can only be used server-side (in a loader, a server function, or a server route). Use useVal in components.",
1084
- // err
1085
- // );
1086
- // return {};
1087
- // }
1088
- // }
1089
-
1090
- var initFetchValRouteStega = function initFetchValRouteStega(config, valApiEndpoints, valServerPromise, isEnabled, getHeaders, getCookies, getDraftSourcesScope, snapshot) {
461
+ return _context2.a(2, {
462
+ value: snapshot !== undefined ? _objectSpread2({
463
+ sources: {},
464
+ snapshot: snapshot,
465
+ draftMode: false
466
+ }, proposal) : null,
467
+ reads: []
468
+ });
469
+ case 2:
470
+ return _context2.a(2, {
471
+ value: _objectSpread2(_objectSpread2({
472
+ sources: changes.sources,
473
+ versions: changes.versions
474
+ }, snapshot !== undefined ? {
475
+ snapshot: snapshot
476
+ } : {}), proposal),
477
+ reads: changes.reads
478
+ });
479
+ }
480
+ }, _callee2);
481
+ }));
482
+ return function withReads() {
483
+ return _ref2.apply(this, arguments);
484
+ };
485
+ }();
486
+ var fetchValDraft = /*#__PURE__*/function () {
487
+ var _ref3 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee3() {
488
+ return _regenerator().w(function (_context3) {
489
+ while (1) switch (_context3.n) {
490
+ case 0:
491
+ _context3.n = 1;
492
+ return withReads();
493
+ case 1:
494
+ return _context3.a(2, _context3.v.value);
495
+ }
496
+ }, _callee3);
497
+ }));
498
+ return function fetchValDraft() {
499
+ return _ref3.apply(this, arguments);
500
+ };
501
+ }();
502
+ draftReadsOf.set(fetchValDraft, withReads);
503
+ return fetchValDraft;
504
+ };
505
+ var initFetchValRouteStega = function initFetchValRouteStega(deps) {
506
+ var fetchVal = fetchValWithReads(deps);
507
+ var fetchValKey = initFetchValKeyStega(deps);
1091
508
  return /*#__PURE__*/function () {
1092
- var _ref5 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee5(selector, params) {
1093
- var resolvedParams, valModule, path, schema, source, url, enabled, draft, content, fetchVal, val, route, _t21;
1094
- return _regenerator().w(function (_context5) {
1095
- while (1) switch (_context5.p = _context5.n) {
509
+ var _ref4 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee4(selector, params) {
510
+ var resolvedParams, valModule, path, schema, url, content, val, _t;
511
+ return _regenerator().w(function (_context4) {
512
+ while (1) switch (_context4.n) {
1096
513
  case 0:
1097
- _context5.n = 1;
514
+ _context4.n = 1;
1098
515
  return Promise.resolve(params);
1099
516
  case 1:
1100
- resolvedParams = _context5.v;
517
+ resolvedParams = _context4.v;
1101
518
  // A view is a pointer: everything below reads a path, a schema and a source
1102
519
  // off this, and a pointer has none of them. See `resolveViewedModule`.
1103
520
  valModule = Internal.resolveViewedModule(selector);
1104
521
  path = valModule && Internal.getValPath(valModule);
1105
522
  schema = valModule && Internal.getSchema(valModule); // `.jsonValues()` router: map params → the entry key and load ONLY that
1106
- // entry's backing `*.val.json`, instead of eagerly resolving the whole
1107
- // record via `fetchVal`.
523
+ // entry, instead of the whole record.
1108
524
  if (!isJsonValuesRecordSchema(schema)) {
1109
- _context5.n = 11;
525
+ _context4.n = 4;
1110
526
  break;
1111
527
  }
1112
- source = valModule && Internal.getSource(valModule);
1113
- url = getValRouteUrlFromVal(resolvedParams, "fetchValRoute", path, schema, source);
528
+ url = getValRouteUrlFromVal(resolvedParams, "fetchValRoute", path, schema, valModule && Internal.getSource(valModule));
1114
529
  if (url) {
1115
- _context5.n = 2;
530
+ _context4.n = 2;
1116
531
  break;
1117
532
  }
1118
- return _context5.a(2, null);
533
+ return _context4.a(2, null);
1119
534
  case 2:
1120
- enabled = false;
1121
- _context5.p = 3;
1122
- _context5.n = 4;
1123
- return isEnabled();
535
+ _context4.n = 3;
536
+ return fetchValKey(valModule, url);
537
+ case 3:
538
+ content = _context4.v;
539
+ return _context4.a(2, content === undefined ? null : content);
1124
540
  case 4:
1125
- enabled = _context5.v;
1126
- _context5.n = 6;
1127
- break;
1128
- case 5:
1129
- _context5.p = 5;
1130
- _context5.v;
1131
- case 6:
1132
- draft = {
1133
- status: "unavailable"
1134
- };
1135
- if (!(enabled && path)) {
1136
- _context5.n = 8;
541
+ _t = valModule;
542
+ if (!_t) {
543
+ _context4.n = 6;
1137
544
  break;
1138
545
  }
1139
- SET_AUTO_TAG_JSX_ENABLED(true);
1140
- _context5.n = 7;
1141
- return loadDraftJsonEntry(valServerPromise, getCookies, path, url);
1142
- case 7:
1143
- draft = _context5.v;
1144
- case 8:
1145
- _context5.n = 9;
1146
- return resolveDraftOrCommittedEntry(draft.status === "unavailable" && path ? snapshotJsonEntry(snapshot, path, url) : draft, function () {
1147
- return loadJsonEntryContent(source, url);
1148
- });
1149
- case 9:
1150
- content = _context5.v;
1151
- if (!(content === undefined)) {
1152
- _context5.n = 10;
1153
- break;
1154
- }
1155
- return _context5.a(2, null);
1156
- case 10:
1157
- return _context5.a(2, stegaEncode(content, {
1158
- disabled: !enabled,
1159
- root: getJsonEntryStegaRoot(valModule, url)
1160
- }));
1161
- case 11:
1162
- fetchVal = initFetchValStega(config, valApiEndpoints, valServerPromise, isEnabled, getHeaders, getCookies, getDraftSourcesScope, snapshot);
1163
- _t21 = valModule;
1164
- if (!_t21) {
1165
- _context5.n = 13;
1166
- break;
1167
- }
1168
- _context5.n = 12;
546
+ _context4.n = 5;
1169
547
  return fetchVal(valModule);
1170
- case 12:
1171
- _t21 = _context5.v;
1172
- case 13:
1173
- val = _t21;
1174
- route = initValRouteFromVal(resolvedParams, "fetchValRoute", path, schema, val);
1175
- return _context5.a(2, route);
548
+ case 5:
549
+ _t = _context4.v.value;
550
+ case 6:
551
+ val = _t;
552
+ return _context4.a(2, initValRouteFromVal(resolvedParams, "fetchValRoute", path, schema, val));
1176
553
  }
1177
- }, _callee5, null, [[3, 5]]);
554
+ }, _callee4);
1178
555
  }));
1179
- return function (_x1, _x10) {
1180
- return _ref5.apply(this, arguments);
556
+ return function (_x3, _x4) {
557
+ return _ref4.apply(this, arguments);
1181
558
  };
1182
559
  }();
1183
560
  };
1184
561
 
1185
- /**
1186
- * Resolves a single `.jsonValues()` entry's content by key from a module's local
1187
- * source markers (one dynamic import). Returns `undefined` when the key is
1188
- * missing or its marker has no runtime thunk (transport marker / draft entry).
1189
- */
1190
- function loadJsonEntryContent(_x11, _x12) {
1191
- return _loadJsonEntryContent.apply(this, arguments);
1192
- }
1193
- /**
1194
- * Loads a single `.jsonValues()` entry's DRAFT content via the in-process
1195
- * `/json` endpoint (which replays pending patches). Returns `undefined` when the
1196
- * entry has no draft content to serve — the caller then falls back to the
1197
- * locally-bundled committed content.
1198
- */
1199
- /**
1200
- * What the draft state says about an entry.
1201
- *
1202
- * The three cases have to stay distinct: `absent` is an ANSWER — the entry is not
1203
- * there in the draft state, e.g. a pending patch removed it — while `unavailable`
1204
- * means we could not ask. Collapsing them into `undefined` is what made a
1205
- * draft-deleted entry keep rendering its committed content: the caller could not
1206
- * tell "it is gone" from "ask the committed source instead".
1207
- */
1208
- /**
1209
- * The slice of `ValServer` the single-entry readers actually use. Narrower than
1210
- * `ValServer` on purpose: it says what the dependency IS, and it lets a test
1211
- * drive these readers with a one-route fake instead of casting a partial object
1212
- * to the whole server type.
1213
- */
1214
- function _loadJsonEntryContent() {
1215
- _loadJsonEntryContent = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee10(source, key) {
1216
- var marker, thunk;
1217
- return _regenerator().w(function (_context10) {
1218
- while (1) switch (_context10.n) {
1219
- case 0:
1220
- if (!(!source || _typeof(source) !== "object")) {
1221
- _context10.n = 1;
1222
- break;
1223
- }
1224
- return _context10.a(2, undefined);
1225
- case 1:
1226
- marker = source[key];
1227
- if (Internal.isJson(marker)) {
1228
- _context10.n = 2;
1229
- break;
1230
- }
1231
- return _context10.a(2, undefined);
1232
- case 2:
1233
- thunk = Internal.getJsonImport(marker);
1234
- if (thunk) {
1235
- _context10.n = 3;
1236
- break;
1237
- }
1238
- return _context10.a(2, undefined);
1239
- case 3:
1240
- _context10.n = 4;
1241
- return thunk();
1242
- case 4:
1243
- return _context10.a(2, _context10.v["default"]);
1244
- }
1245
- }, _callee10);
1246
- }));
1247
- return _loadJsonEntryContent.apply(this, arguments);
1248
- }
1249
- /**
1250
- * Picks the content a draft-aware single-entry read should render.
1251
- *
1252
- * The rule the two callers share: the draft state WINS when it has an answer —
1253
- * including the answer "this entry is gone" — and the committed content is used
1254
- * only when there is no draft answer to be had (Val disabled, or we could not
1255
- * ask). Returning `undefined` means "render nothing"; both callers turn that into
1256
- * a null/undefined result.
1257
- */
1258
- /**
1259
- * What a proposal's snapshot says about one `.jsonValues()` entry: its saved
1260
- * content, `absent` when the proposal removed it, or `unavailable` -- not at
1261
- * a proposal, or an entry the proposal did not save, which is the build's.
1262
- *
1263
- * The snapshot carries saved entries as content in place of their markers
1264
- * (`proposalSnapshotForReaders` in @valbuild/server). The same rule as the
1265
- * browser's `draftJsonEntry`, so the server's render and the hydrated page
1266
- * agree.
1267
- */
1268
- function snapshotJsonEntry(snapshot, moduleFilePath, key) {
1269
- var source = snapshot === null || snapshot === void 0 ? void 0 : snapshot[moduleFilePath];
1270
- if (source === null) {
1271
- // A nullable record the proposal saved as null: it has no entries.
1272
- return {
1273
- status: "absent"
1274
- };
1275
- }
1276
- if (source === undefined || _typeof(source) !== "object" || Array.isArray(source)) {
1277
- return {
1278
- status: "unavailable"
1279
- };
1280
- }
1281
- // Own keys only: an entry may be named `toString`.
1282
- if (!Object.prototype.hasOwnProperty.call(source, key)) {
1283
- return {
1284
- status: "absent"
1285
- };
1286
- }
1287
- var entry = source[key];
1288
- if (Internal.isJson(entry)) {
1289
- return {
1290
- status: "unavailable"
1291
- };
1292
- }
1293
- return {
1294
- status: "content",
1295
- content: entry
1296
- };
1297
- }
1298
- function resolveDraftOrCommittedEntry(_x13, _x14) {
1299
- return _resolveDraftOrCommittedEntry.apply(this, arguments);
1300
- }
1301
- function _resolveDraftOrCommittedEntry() {
1302
- _resolveDraftOrCommittedEntry = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee11(draft, loadCommitted) {
1303
- return _regenerator().w(function (_context11) {
1304
- while (1) switch (_context11.n) {
1305
- case 0:
1306
- if (!(draft.status === "content")) {
1307
- _context11.n = 1;
1308
- break;
1309
- }
1310
- return _context11.a(2, draft.content);
1311
- case 1:
1312
- if (!(draft.status === "absent")) {
1313
- _context11.n = 2;
1314
- break;
1315
- }
1316
- return _context11.a(2, undefined);
1317
- case 2:
1318
- return _context11.a(2, loadCommitted());
1319
- }
1320
- }, _callee11);
1321
- }));
1322
- return _resolveDraftOrCommittedEntry.apply(this, arguments);
1323
- }
1324
- function loadDraftJsonEntry(_x15, _x16, _x17, _x18) {
1325
- return _loadDraftJsonEntry.apply(this, arguments);
1326
- }
1327
562
  /**
1328
563
  * Resolves ONE `.jsonValues()` entry by key, loading only that entry instead of
1329
564
  * the whole record — the runtime-scaling counterpart to the eager `fetchVal`.
1330
565
  *
1331
- * Production (Val disabled): resolves the entry's lazy import thunk from the
1332
- * locally-bundled module. One dynamic import, no server round-trip.
566
+ * Production (Val disabled): the entry's lazy import from the bundle (one
567
+ * dynamic import, no server round-trip), unless a proposal's snapshot has it.
1333
568
  *
1334
- * Enabled (draft mode): reads the entry through `/json`, which replays pending
1335
- * patches, so uncommitted Studio edits show up. Falls back to the local thunk if
1336
- * the draft read yields nothing.
569
+ * Draft mode: the entry from the runtime's head, with the pending patches the
570
+ * draft holds for it, so uncommitted Studio edits show up; an entry the draft
571
+ * deleted is not there. An entry of a record no patch touched is the bundle's
572
+ * (or the snapshot's).
1337
573
  */
1338
- function _loadDraftJsonEntry() {
1339
- _loadDraftJsonEntry = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee12(valServerPromise, getCookies, moduleFilePath, key) {
1340
- var _cookies4;
1341
- var cookies, valServer, res;
1342
- return _regenerator().w(function (_context12) {
1343
- while (1) switch (_context12.p = _context12.n) {
1344
- case 0:
1345
- _context12.p = 0;
1346
- _context12.n = 1;
1347
- return getCookies();
1348
- case 1:
1349
- cookies = _context12.v;
1350
- _context12.n = 3;
1351
- break;
1352
- case 2:
1353
- _context12.p = 2;
1354
- _context12.v;
1355
- return _context12.a(2, {
1356
- status: "unavailable"
1357
- });
1358
- case 3:
1359
- _context12.n = 4;
1360
- return valServerPromise;
1361
- case 4:
1362
- valServer = _context12.v;
1363
- _context12.n = 5;
1364
- return valServer["/json"]["GET"]({
1365
- query: {
1366
- path: moduleFilePath,
1367
- key: key,
1368
- keys: undefined,
1369
- // single-entry shape
1370
- offset: undefined,
1371
- limit: undefined,
1372
- apply_patches: true
1373
- },
1374
- cookies: _defineProperty({}, VAL_SESSION_COOKIE, (_cookies4 = cookies) === null || _cookies4 === void 0 || (_cookies4 = _cookies4.get(VAL_SESSION_COOKIE)) === null || _cookies4 === void 0 ? void 0 : _cookies4.value)
1375
- });
1376
- case 5:
1377
- res = _context12.v;
1378
- if (!(res.status === 200 && "content" in res.json)) {
1379
- _context12.n = 6;
1380
- break;
1381
- }
1382
- return _context12.a(2, {
1383
- status: "content",
1384
- content: res.json.content
1385
- });
1386
- case 6:
1387
- if (!(res.status === 401)) {
1388
- _context12.n = 7;
1389
- break;
1390
- }
1391
- Internal.log.debug("authentication error: ", res.json.message);
1392
- return _context12.a(2, {
1393
- status: "unavailable"
1394
- });
1395
- case 7:
1396
- if (!(res.status === 404)) {
1397
- _context12.n = 8;
1398
- break;
1399
- }
1400
- return _context12.a(2, {
1401
- status: "absent"
1402
- });
1403
- case 8:
1404
- Internal.log.error("could not load draft JSON entry: ", "message" in res.json ? res.json.message : "status ".concat(res.status));
1405
- return _context12.a(2, {
1406
- status: "unavailable"
1407
- });
1408
- }
1409
- }, _callee12, null, [[0, 2]]);
1410
- }));
1411
- return _loadDraftJsonEntry.apply(this, arguments);
1412
- }
1413
- var initFetchValKeyStega = function initFetchValKeyStega(valServerPromise, isEnabled, getCookies, snapshot) {
574
+ var initFetchValKeyStega = function initFetchValKeyStega(deps) {
1414
575
  return /*#__PURE__*/function () {
1415
- var _ref6 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee6(selector, key) {
1416
- var enabled, valModule, source, moduleFilePath, draft, content;
1417
- return _regenerator().w(function (_context6) {
1418
- while (1) switch (_context6.p = _context6.n) {
576
+ var _ref5 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee5(selector, key) {
577
+ var _yield$draftForReques2, enabled, draft, env;
578
+ return _regenerator().w(function (_context5) {
579
+ while (1) switch (_context5.n) {
1419
580
  case 0:
1420
- enabled = false;
1421
- _context6.p = 1;
1422
- _context6.n = 2;
1423
- return isEnabled();
1424
- case 2:
1425
- enabled = _context6.v;
1426
- _context6.n = 4;
1427
- break;
1428
- case 3:
1429
- _context6.p = 3;
1430
- _context6.v;
1431
- case 4:
1432
- // A view is a pointer: everything below reads a path and a source off this,
1433
- // and a pointer has neither. See `resolveViewedModule`.
1434
- valModule = Internal.resolveViewedModule(selector);
1435
- source = valModule && Internal.getSource(valModule);
1436
- moduleFilePath = valModule && Internal.getValPath(valModule);
1437
- draft = {
1438
- status: "unavailable"
1439
- };
1440
- if (!(enabled && moduleFilePath)) {
1441
- _context6.n = 6;
1442
- break;
1443
- }
1444
- SET_AUTO_TAG_JSX_ENABLED(true);
1445
- _context6.n = 5;
1446
- return loadDraftJsonEntry(valServerPromise, getCookies, moduleFilePath, key);
1447
- case 5:
1448
- draft = _context6.v;
1449
- case 6:
1450
- _context6.n = 7;
1451
- return resolveDraftOrCommittedEntry(draft.status === "unavailable" && moduleFilePath ? snapshotJsonEntry(snapshot, moduleFilePath, key) : draft, function () {
1452
- return loadJsonEntryContent(source, key);
581
+ _context5.n = 1;
582
+ return draftForRequest({
583
+ runtime: deps.runtime,
584
+ isEnabled: deps.isEnabled,
585
+ getCookies: deps.getCookies
1453
586
  });
1454
- case 7:
1455
- content = _context6.v;
1456
- if (!(content === undefined)) {
1457
- _context6.n = 8;
1458
- break;
1459
- }
1460
- return _context6.a(2, undefined);
1461
- case 8:
1462
- return _context6.a(2, stegaEncode(content, {
1463
- disabled: !enabled,
1464
- root: getJsonEntryStegaRoot(valModule, key)
1465
- }));
587
+ case 1:
588
+ _yield$draftForReques2 = _context5.v;
589
+ enabled = _yield$draftForReques2.enabled;
590
+ draft = _yield$draftForReques2.draft;
591
+ env = requestEnv(enabled, draft, deps.snapshot);
592
+ _context5.n = 2;
593
+ return awaitResolved(function (settled) {
594
+ return resolveValEntry(selector, key, env, {
595
+ settled: settled
596
+ });
597
+ });
598
+ case 2:
599
+ return _context5.a(2, _context5.v.value);
1466
600
  }
1467
- }, _callee6, null, [[1, 3]]);
601
+ }, _callee5);
1468
602
  }));
1469
- return function (_x19, _x20) {
1470
- return _ref6.apply(this, arguments);
603
+ return function (_x5, _x6) {
604
+ return _ref5.apply(this, arguments);
1471
605
  };
1472
606
  }();
1473
607
  };
1474
- var initFetchValRouteUrl = function initFetchValRouteUrl(config, valApiEndpoints, valServerPromise, isEnabled, getHeaders, getCookies, getDraftSourcesScope, snapshot) {
608
+ var initFetchValRouteUrl = function initFetchValRouteUrl(deps) {
609
+ var fetchVal = fetchValWithReads(deps);
1475
610
  return /*#__PURE__*/function () {
1476
- var _ref7 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee7(selector, params) {
1477
- var fetchVal, resolvedParams, valModule, path, schema, val, route, _t23, _t24;
1478
- return _regenerator().w(function (_context7) {
1479
- while (1) switch (_context7.n) {
611
+ var _ref6 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee6(selector, params) {
612
+ var resolvedParams, valModule, val, _t2, _t3;
613
+ return _regenerator().w(function (_context6) {
614
+ while (1) switch (_context6.n) {
1480
615
  case 0:
1481
- fetchVal = initFetchValStega(config, valApiEndpoints, valServerPromise, isEnabled, getHeaders, getCookies, getDraftSourcesScope, snapshot);
1482
616
  if (!(params === undefined)) {
1483
- _context7.n = 1;
617
+ _context6.n = 1;
1484
618
  break;
1485
619
  }
1486
- _t23 = undefined;
1487
- _context7.n = 3;
620
+ _t2 = undefined;
621
+ _context6.n = 3;
1488
622
  break;
1489
623
  case 1:
1490
- _context7.n = 2;
624
+ _context6.n = 2;
1491
625
  return Promise.resolve(params);
1492
626
  case 2:
1493
- _t23 = _context7.v;
627
+ _t2 = _context6.v;
1494
628
  case 3:
1495
- resolvedParams = _t23;
629
+ resolvedParams = _t2;
1496
630
  // A view is a pointer: everything below reads a path, a schema and a source
1497
631
  // off this, and a pointer has none of them. See `resolveViewedModule`.
1498
- valModule = Internal.resolveViewedModule(selector);
1499
- path = valModule && Internal.getValPath(valModule);
1500
- schema = valModule && Internal.getSchema(valModule);
1501
- _t24 = valModule;
1502
- if (!_t24) {
1503
- _context7.n = 5;
632
+ valModule = Internal.resolveViewedModule(selector); // The url is made of the params and the module's path: a `.jsonValues()`
633
+ // router's entries are not imported for it.
634
+ _t3 = valModule;
635
+ if (!_t3) {
636
+ _context6.n = 5;
1504
637
  break;
1505
638
  }
1506
- _context7.n = 4;
1507
- return fetchVal(valModule);
639
+ _context6.n = 4;
640
+ return fetchVal(valModule, {
641
+ entries: false
642
+ });
1508
643
  case 4:
1509
- _t24 = _context7.v;
644
+ _t3 = _context6.v.value;
1510
645
  case 5:
1511
- val = _t24;
1512
- route = getValRouteUrlFromVal(resolvedParams, "fetchValRouteUrl", path, schema, val);
1513
- return _context7.a(2, route);
646
+ val = _t3;
647
+ return _context6.a(2, getValRouteUrlFromVal(resolvedParams, "fetchValRouteUrl", valModule && Internal.getValPath(valModule), valModule && Internal.getSchema(valModule), val));
1514
648
  }
1515
- }, _callee7);
649
+ }, _callee6);
1516
650
  }));
1517
- return function (_x21, _x22) {
1518
- return _ref7.apply(this, arguments);
651
+ return function (_x7, _x8) {
652
+ return _ref6.apply(this, arguments);
1519
653
  };
1520
654
  }();
1521
655
  };
@@ -1533,25 +667,25 @@ function requestHeaders() {
1533
667
  }
1534
668
  /** The request's cookies, in the `{ name, value }` shape the readers want. */
1535
669
  function _requestHeaders() {
1536
- _requestHeaders = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee13() {
1537
- var _yield$import2, getRequestHeaders, headers;
1538
- return _regenerator().w(function (_context13) {
1539
- while (1) switch (_context13.n) {
670
+ _requestHeaders = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee7() {
671
+ var _yield$import, getRequestHeaders, headers;
672
+ return _regenerator().w(function (_context7) {
673
+ while (1) switch (_context7.n) {
1540
674
  case 0:
1541
- _context13.n = 1;
675
+ _context7.n = 1;
1542
676
  return import('@tanstack/react-start/server');
1543
677
  case 1:
1544
- _yield$import2 = _context13.v;
1545
- getRequestHeaders = _yield$import2.getRequestHeaders;
678
+ _yield$import = _context7.v;
679
+ getRequestHeaders = _yield$import.getRequestHeaders;
1546
680
  headers = getRequestHeaders();
1547
- return _context13.a(2, {
681
+ return _context7.a(2, {
1548
682
  get: function get(name) {
1549
- var _headers$get2;
1550
- return (_headers$get2 = headers.get(name)) !== null && _headers$get2 !== void 0 ? _headers$get2 : null;
683
+ var _headers$get;
684
+ return (_headers$get = headers.get(name)) !== null && _headers$get !== void 0 ? _headers$get : null;
1551
685
  }
1552
686
  });
1553
687
  }
1554
- }, _callee13);
688
+ }, _callee7);
1555
689
  }));
1556
690
  return _requestHeaders.apply(this, arguments);
1557
691
  }
@@ -1579,17 +713,17 @@ function requestCookies() {
1579
713
  * } = initValContent(config, valModules);
1580
714
  */
1581
715
  function _requestCookies() {
1582
- _requestCookies = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee14() {
1583
- var _yield$import3, getCookie;
1584
- return _regenerator().w(function (_context14) {
1585
- while (1) switch (_context14.n) {
716
+ _requestCookies = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee8() {
717
+ var _yield$import2, getCookie;
718
+ return _regenerator().w(function (_context8) {
719
+ while (1) switch (_context8.n) {
1586
720
  case 0:
1587
- _context14.n = 1;
721
+ _context8.n = 1;
1588
722
  return import('@tanstack/react-start/server');
1589
723
  case 1:
1590
- _yield$import3 = _context14.v;
1591
- getCookie = _yield$import3.getCookie;
1592
- return _context14.a(2, {
724
+ _yield$import2 = _context8.v;
725
+ getCookie = _yield$import2.getCookie;
726
+ return _context8.a(2, {
1593
727
  get: function get(name) {
1594
728
  var value = getCookie(name);
1595
729
  return value === undefined ? undefined : {
@@ -1599,7 +733,7 @@ function _requestCookies() {
1599
733
  }
1600
734
  });
1601
735
  }
1602
- }, _callee14);
736
+ }, _callee8);
1603
737
  }));
1604
738
  return _requestCookies.apply(this, arguments);
1605
739
  }
@@ -1613,15 +747,17 @@ function initValContent(config, valModules, opts) {
1613
747
  if (!tanstackVersion) {
1614
748
  throw new Error("Could not get @valbuild/tanstack package version");
1615
749
  }
1616
- var draftSourcesScope = createTanStackRequestScope();
1617
750
  var snapshot = snapshotForReaders(opts === null || opts === void 0 ? void 0 : opts.http);
1618
- var base = draftBase(opts === null || opts === void 0 ? void 0 : opts.http);
1619
751
  markContentApplied(opts === null || opts === void 0 ? void 0 : opts.http);
1620
752
  var draftMode = (_opts$draftMode = opts === null || opts === void 0 ? void 0 : opts.draftMode) !== null && _opts$draftMode !== void 0 ? _opts$draftMode : valDraftMode();
1621
753
  var isEnabled = function isEnabled() {
1622
754
  return draftMode.isEnabled();
1623
755
  };
1624
- var valServerPromise = createValServer(valModules, valApiEndpoints, _objectSpread2(_objectSpread2(_objectSpread2(_objectSpread2(_objectSpread2({
756
+
757
+ // One runtime per process for these modules: `initValServer`'s server is
758
+ // built on the same one when it is given the same options, so a save
759
+ // through the API moves what these read.
760
+ var runtime = createValRuntime(valModules, valApiEndpoints, _objectSpread2(_objectSpread2(_objectSpread2(_objectSpread2(_objectSpread2({
1625
761
  versions: {
1626
762
  // `next` is the field name in the wire contract; see initValServer.
1627
763
  next: tanstackVersion,
@@ -1634,23 +770,25 @@ function initValContent(config, valModules, opts) {
1634
770
  patchStore: opts.patchStore
1635
771
  } : {}), (opts === null || opts === void 0 ? void 0 : opts.unsafelyAllowUnauthenticated) !== undefined ? {
1636
772
  unsafelyAllowUnauthenticated: opts.unsafelyAllowUnauthenticated
1637
- } : {}), (opts === null || opts === void 0 ? void 0 : opts.http) !== undefined ? opts.http : {}), config, {
773
+ } : {}), (opts === null || opts === void 0 ? void 0 : opts.http) !== undefined ? opts.http : {}), config);
774
+ var deps = _objectSpread2({
775
+ runtime: runtime,
1638
776
  isEnabled: isEnabled,
1639
- onEnable: function onEnable() {
1640
- return draftMode.enable();
1641
- },
1642
- onDisable: function onDisable() {
1643
- return draftMode.disable();
1644
- }
1645
- });
777
+ getHeaders: requestHeaders,
778
+ getCookies: requestCookies
779
+ }, snapshot !== undefined ? {
780
+ snapshot: snapshot
781
+ } : {});
782
+ var fetchValStega = initFetchValStega(deps);
783
+ runtimesOf.set(fetchValStega, runtime);
1646
784
  return {
1647
- fetchValStega: initFetchValStega(config, valApiEndpoints,
1648
- // TODO: get from config
1649
- valServerPromise, isEnabled, requestHeaders, requestCookies, draftSourcesScope, snapshot),
1650
- fetchValKeyStega: initFetchValKeyStega(valServerPromise, isEnabled, requestCookies, snapshot),
1651
- fetchValRouteStega: initFetchValRouteStega(config, valApiEndpoints, valServerPromise, isEnabled, requestHeaders, requestCookies, draftSourcesScope, snapshot),
1652
- fetchValRouteUrl: initFetchValRouteUrl(config, valApiEndpoints, valServerPromise, isEnabled, requestHeaders, requestCookies, draftSourcesScope, snapshot),
1653
- fetchValDraft: initFetchValDraft(valServerPromise, isEnabled, requestCookies, draftSourcesScope, snapshot, base, (opts === null || opts === void 0 || (_opts$http = opts.http) === null || _opts$http === void 0 ? void 0 : _opts$http.proposal) !== undefined)
785
+ fetchValStega: fetchValStega,
786
+ fetchValKeyStega: initFetchValKeyStega(deps),
787
+ fetchValRouteStega: initFetchValRouteStega(deps),
788
+ fetchValRouteUrl: initFetchValRouteUrl(deps),
789
+ fetchValDraft: initFetchValDraft(_objectSpread2(_objectSpread2({}, deps), {}, {
790
+ atProposal: (opts === null || opts === void 0 || (_opts$http = opts.http) === null || _opts$http === void 0 ? void 0 : _opts$http.proposal) !== undefined
791
+ }))
1654
792
  };
1655
793
  }
1656
794