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