@valbuild/server 0.152.2 → 0.153.1

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 (38) hide show
  1. package/CHANGELOG.md +44 -0
  2. package/browser/dist/valbuild-server-browser.cjs.dev.js +2 -2
  3. package/browser/dist/valbuild-server-browser.cjs.prod.js +2 -2
  4. package/browser/dist/valbuild-server-browser.esm.js +2 -2
  5. package/dist/{ValRouter-709cceaa.cjs.prod.js → ValRouter-40bf46a2.cjs.prod.js} +86 -11
  6. package/dist/{ValRouter-ff659cff.cjs.dev.js → ValRouter-6c633acf.cjs.dev.js} +86 -11
  7. package/dist/{ValRouter-7e314a55.esm.js → ValRouter-db465719.esm.js} +86 -12
  8. package/dist/{ValRouterCore-a5f17ad7.cjs.dev.js → ValRouterCore-2d86ff95.cjs.prod.js} +305 -251
  9. package/dist/{ValRouterCore-4e858bec.cjs.prod.js → ValRouterCore-a4d99115.cjs.dev.js} +305 -251
  10. package/dist/{ValRouterCore-c729eed3.esm.js → ValRouterCore-b3773130.esm.js} +305 -251
  11. package/dist/declarations/src/ValOps.d.ts +42 -4
  12. package/dist/declarations/src/ValOpsFS.d.ts +6 -3
  13. package/dist/declarations/src/ValOpsHttp.d.ts +5 -2
  14. package/dist/declarations/src/ValOpsMemory.d.ts +3 -2
  15. package/dist/declarations/src/ValRouter.d.ts +10 -0
  16. package/dist/declarations/src/ValServer.d.ts +2 -0
  17. package/dist/declarations/src/draftReads.d.ts +1 -8
  18. package/dist/declarations/src/internal/index.d.ts +2 -2
  19. package/dist/declarations/src/proposal.d.ts +19 -3
  20. package/dist/{draftReads-05415ff6.cjs.dev.js → draftReads-0dcddf72.cjs.prod.js} +0 -17
  21. package/dist/{draftReads-5f99edd3.esm.js → draftReads-1f29aedd.esm.js} +2 -18
  22. package/dist/{draftReads-ea3946e0.cjs.prod.js → draftReads-df44e629.cjs.dev.js} +0 -17
  23. package/dist/{inTabFetch-fb9ee475.cjs.dev.js → inTabFetch-6382d21a.cjs.dev.js} +1 -1
  24. package/dist/{inTabFetch-b0a0e42f.esm.js → inTabFetch-80b6b9e8.esm.js} +1 -1
  25. package/dist/{inTabFetch-9b93a5d8.cjs.prod.js → inTabFetch-95c17bcc.cjs.prod.js} +1 -1
  26. package/dist/{publishKit-9c0c6880.cjs.dev.js → publishKit-500645fd.cjs.dev.js} +2 -2
  27. package/dist/{publishKit-ba7ff0e3.cjs.prod.js → publishKit-b4c47ed0.cjs.prod.js} +2 -2
  28. package/dist/{publishKit-5fe7f248.esm.js → publishKit-c03f3e49.esm.js} +3 -3
  29. package/dist/valbuild-server.cjs.dev.js +2 -2
  30. package/dist/valbuild-server.cjs.prod.js +2 -2
  31. package/dist/valbuild-server.esm.js +4 -4
  32. package/internal/browser/dist/valbuild-server-internal-browser.cjs.dev.js +3 -3
  33. package/internal/browser/dist/valbuild-server-internal-browser.cjs.prod.js +3 -3
  34. package/internal/browser/dist/valbuild-server-internal-browser.esm.js +3 -3
  35. package/internal/dist/valbuild-server-internal.cjs.dev.js +4 -4
  36. package/internal/dist/valbuild-server-internal.cjs.prod.js +4 -4
  37. package/internal/dist/valbuild-server-internal.esm.js +3 -3
  38. package/package.json +4 -4
@@ -2842,6 +2842,14 @@ class ValOps {
2842
2842
  * {@link ValOpsOptions.stagedAfterBuild}.
2843
2843
  */
2844
2844
  heldStaged = new Map();
2845
+ /**
2846
+ * The Source of each module NEW in the proposal -- staged after the build,
2847
+ * and not in it at all -- read from its staged text: what a save applies
2848
+ * its patches to and records (`getSources`, `prepare`). Not served: no
2849
+ * reader at this build has its schema, and the page's code has not got
2850
+ * the module. See {@link readStagedSources}.
2851
+ */
2852
+ stagedNewSources = {};
2845
2853
  /**
2846
2854
  * What the BUILD evaluated each module to, and its version: before a
2847
2855
  * snapshot, a staging or a publish put anything over it. What a reader's
@@ -2959,6 +2967,7 @@ class ValOps {
2959
2967
  ...staged.sources
2960
2968
  };
2961
2969
  this.heldStaged = (staged === null || staged === void 0 ? void 0 : staged.held) ?? new Map();
2970
+ this.stagedNewSources = (staged === null || staged === void 0 ? void 0 : staged.added) ?? {};
2962
2971
  /*
2963
2972
  * The bundle's own Source for every module either layer replaces: a
2964
2973
  * replaced `.jsonValues()` module's markers came as JSON, and an entry
@@ -3055,13 +3064,10 @@ class ValOps {
3055
3064
  Internal.log.error("could not read the staged modules' Source", err);
3056
3065
  }
3057
3066
  const sources = {};
3067
+ const added = {};
3058
3068
  const held = new Map();
3059
3069
  for (const path of paths) {
3060
3070
  const source = read[path];
3061
- if (bundled[path] === undefined) {
3062
- // Not in this build at all: nothing here serves it, or saves it.
3063
- continue;
3064
- }
3065
3071
  if (source === undefined) {
3066
3072
  held.set(path, `its staged code is not in the build serving this proposal, and its content is not written out in its .val.ts (it is made by code), so it can only be saved once the proposal is built. Build the proposal, then save.`);
3067
3073
  continue;
@@ -3070,10 +3076,21 @@ class ValOps {
3070
3076
  held.set(path, `its staged code is not in the build serving this proposal, and its .jsonValues() entries are saved through a schema only a build has. Build the proposal, then save.`);
3071
3077
  continue;
3072
3078
  }
3079
+ if (bundled[path] === undefined) {
3080
+ // New in the proposal: this build has no such module. Its Source is
3081
+ // saved from its text all the same (a save applies its patches to
3082
+ // it, with no schema), and it is not served: see `stagedNewSources`.
3083
+ // It used to be skipped here, so a save recorded its text and no
3084
+ // Source, and the address went on serving what the module was
3085
+ // created with after the build (C11d).
3086
+ added[path] = source;
3087
+ continue;
3088
+ }
3073
3089
  sources[path] = source;
3074
3090
  }
3075
3091
  return {
3076
3092
  sources,
3093
+ added,
3077
3094
  held
3078
3095
  };
3079
3096
  }
@@ -3419,6 +3436,34 @@ class ValOps {
3419
3436
  return this.getSourcesSha();
3420
3437
  }
3421
3438
 
3439
+ /**
3440
+ * The build the chain is relative to, by its version's name
3441
+ * (`ProjectVersion.build`): the newest build's in fs and memory, this
3442
+ * instance's own in http -- the same rule as {@link chainBaseSha}, and for
3443
+ * the same reason. `/stat` answers it as `version.build`, and the fs and
3444
+ * memory long poll compare a Studio's echoed version with it, so every
3445
+ * instance answering one store names one build: a Studio parked on the
3446
+ * previous build's instance is told of the new build once, not on every
3447
+ * poll.
3448
+ */
3449
+ async chainBuild() {
3450
+ const newest = this.newestOps();
3451
+ if (newest !== this) {
3452
+ try {
3453
+ return await newest.ownBuild();
3454
+ } catch (err) {
3455
+ Internal.log.debug("could not read the newest build's version; answering with this one's", err);
3456
+ }
3457
+ }
3458
+ return this.ownBuild();
3459
+ }
3460
+
3461
+ /** This instance's own build, as `ProjectVersion.build` names it. */
3462
+ async ownBuild() {
3463
+ await this.initSources();
3464
+ return this.buildVersion ?? "";
3465
+ }
3466
+
3422
3467
  /**
3423
3468
  * The data layer whose head answers what each module IS for this store's
3424
3469
  * chain (`GET /manifest`, `GET /modules`): this one, unless the store keeps
@@ -3870,7 +3915,9 @@ class ValOps {
3870
3915
  };
3871
3916
  for (const patchData of analysis.patches) {
3872
3917
  const path = patchData.path;
3873
- const base = sources[path];
3918
+ // A module new in the proposal has no base in this build: its staged
3919
+ // text's Source is the one its patches apply to.
3920
+ const base = sources[path] ?? this.stagedNewSources[path];
3874
3921
  if (base === undefined) {
3875
3922
  if (!errors[path]) {
3876
3923
  errors[path] = [];
@@ -5053,6 +5100,19 @@ class ValOps {
5053
5100
  // other module its history.
5054
5101
  serialized = undefined;
5055
5102
  }
5103
+ const staged = stagedAfterBuild[path];
5104
+ if (!serialized && staged !== undefined && this.stagedNewSources[path] !== undefined) {
5105
+ // New in the proposal: no schema of this build's, and content records
5106
+ // the staging's. Without this the save recorded the module's text
5107
+ // and no Source (C11d).
5108
+ const source = sourcesAfter[path];
5109
+ moduleVersions[path] = {
5110
+ source: source === undefined ? null : source,
5111
+ schema: null,
5112
+ staged
5113
+ };
5114
+ continue;
5115
+ }
5056
5116
  if (!serialized) {
5057
5117
  continue;
5058
5118
  }
@@ -6193,7 +6253,7 @@ var packageJson = {
6193
6253
  ],
6194
6254
  exports: true
6195
6255
  },
6196
- version: "0.152.2",
6256
+ version: "0.153.1",
6197
6257
  scripts: {
6198
6258
  typecheck: "tsc --noEmit",
6199
6259
  test: "jest",
@@ -7797,6 +7857,133 @@ const ValServer = (valModules, options, callbacks, platform) => {
7797
7857
  };
7798
7858
  };
7799
7859
 
7860
+ /**
7861
+ * What `GET /manifest` answers, and what `/stat` carries when the Studio
7862
+ * says which manifest it holds: ONE function, so the two cannot disagree
7863
+ * about a delta. `held` is the ETag the client holds and `since` the `seq`
7864
+ * it was answered at; a delta is relative to exactly that one.
7865
+ */
7866
+ const manifestAnswer = async (held, since) => {
7867
+ // Through the head: "has the chain moved?" is its cheap `seq`
7868
+ // (`ValOps.chainSeq`), and the chain is read only when it moved --
7869
+ // in http mode one small read of the content service, not the
7870
+ // whole chain on every poll of every Studio. The NEWEST build's
7871
+ // head where the store keeps one chain for every build (fs,
7872
+ // memory): a previous build still answering would name its own,
7873
+ // older bases (S26, `ValOps.newestOps`).
7874
+ const ops = serverOps.newestOps();
7875
+ const synced = await ops.head.sync();
7876
+ if ("error" in synced) {
7877
+ return {
7878
+ type: "error",
7879
+ unauthorized: synced.error.unauthorized === true,
7880
+ message: synced.error.message
7881
+ };
7882
+ }
7883
+ const {
7884
+ project,
7885
+ versions: modules
7886
+ } = synced.ok;
7887
+ /*
7888
+ * A previous build still answering in http mode (S26): its versions
7889
+ * are right for its own bases, and the site serves another build's.
7890
+ * Said, so a Studio does not move to them; it asks again, and is
7891
+ * answered by the live build soon.
7892
+ */
7893
+ const superseded = synced.ok.fetched.superseded === true;
7894
+ /*
7895
+ * Where this build's code is, for a Studio opened on another build
7896
+ * (phase 9): it imports it and reloads in place. The answering
7897
+ * build's, not the newest's: in http mode a previous build says it
7898
+ * is superseded and its manifest is not adopted at all, and in fs
7899
+ * and memory mode there is no entry -- HMR brings the code.
7900
+ */
7901
+ const registry = serverOps.buildRegistry();
7902
+ const tag = await ops.manifestTag(project);
7903
+ if (held === tag) {
7904
+ return {
7905
+ type: "unchanged",
7906
+ tag,
7907
+ seq: project.seq
7908
+ };
7909
+ }
7910
+ ops.rememberManifest(tag, project, modules);
7911
+ const extra = {
7912
+ ...(superseded ? {
7913
+ superseded
7914
+ } : {}),
7915
+ ...(registry !== null ? {
7916
+ registry
7917
+ } : {})
7918
+ };
7919
+ /*
7920
+ * "Since seq N": the modules that moved since the manifest the
7921
+ * client holds -- the one its ETag names, answered at N -- when this
7922
+ * instance answered it. Otherwise whole, which is always correct.
7923
+ */
7924
+ if (held !== undefined && since !== undefined && Number.isSafeInteger(since)) {
7925
+ const moved = ops.manifestSince(held, since, project, modules);
7926
+ if (moved !== null) {
7927
+ return {
7928
+ type: "manifest",
7929
+ tag,
7930
+ json: {
7931
+ project,
7932
+ modules: moved,
7933
+ tag,
7934
+ since,
7935
+ ...extra
7936
+ }
7937
+ };
7938
+ }
7939
+ }
7940
+ return {
7941
+ type: "manifest",
7942
+ tag,
7943
+ json: {
7944
+ project,
7945
+ modules,
7946
+ tag,
7947
+ ...extra
7948
+ }
7949
+ };
7950
+ };
7951
+
7952
+ /**
7953
+ * {@link manifestAnswer} as `/stat` carries it (`StatManifest`), for a stat
7954
+ * answered at `seq`.
7955
+ *
7956
+ * Not read at all while `seq` is the one the held manifest was answered at:
7957
+ * a Studio reads a manifest only for a stat whose `seq` moved, and reading
7958
+ * it is a request to the content service in http mode -- on every poll of
7959
+ * every Studio, for an answer nobody reads. `unchanged` then hands the held
7960
+ * one back, so the next request names it again.
7961
+ */
7962
+ const statManifestOf = async (held, seq) => {
7963
+ if (held !== null && held.seq === seq) {
7964
+ return {
7965
+ unchanged: true,
7966
+ tag: held.tag,
7967
+ seq: held.seq
7968
+ };
7969
+ }
7970
+ const answer = await manifestAnswer(held === null || held === void 0 ? void 0 : held.tag, held === null || held === void 0 ? void 0 : held.seq);
7971
+ if (answer.type === "error") {
7972
+ return undefined;
7973
+ }
7974
+ if (answer.type === "unchanged") {
7975
+ return {
7976
+ unchanged: true,
7977
+ tag: answer.tag,
7978
+ seq: answer.seq
7979
+ };
7980
+ }
7981
+ return held !== null && answer.json.since !== undefined ? {
7982
+ ...answer.json,
7983
+ sinceTag: held.tag
7984
+ } : answer.json;
7985
+ };
7986
+
7800
7987
  /**
7801
7988
  * What the Studio's full check reads: base plus the patches the caller is
7802
7989
  * showing, as `/sources/~` resolves an explicit `patch_id` list -- including
@@ -8394,104 +8581,38 @@ const ValServer = (valModules, options, callbacks, platform) => {
8394
8581
  }
8395
8582
  };
8396
8583
  }
8397
- // Through the head: "has the chain moved?" is its cheap `seq`
8398
- // (`ValOps.chainSeq`), and the chain is read only when it moved --
8399
- // in http mode one small read of the content service, not the
8400
- // whole chain on every poll of every Studio. The NEWEST build's
8401
- // head where the store keeps one chain for every build (fs,
8402
- // memory): a previous build still answering would name its own,
8403
- // older bases (S26, `ValOps.newestOps`).
8404
- const ops = serverOps.newestOps();
8405
- const synced = await ops.head.sync();
8406
- if ("error" in synced) {
8407
- if (synced.error.unauthorized) {
8584
+ const since = req.query.since !== undefined ? Number(req.query.since) : undefined;
8585
+ const answer = await manifestAnswer(req.headers["if-none-match"], since);
8586
+ if (answer.type === "error") {
8587
+ if (answer.unauthorized) {
8408
8588
  return {
8409
8589
  status: 401,
8410
8590
  json: {
8411
- message: synced.error.message
8591
+ message: answer.message
8412
8592
  }
8413
8593
  };
8414
8594
  }
8415
8595
  return {
8416
8596
  status: 500,
8417
8597
  json: {
8418
- message: synced.error.message
8598
+ message: answer.message
8419
8599
  }
8420
8600
  };
8421
8601
  }
8422
- const {
8423
- project,
8424
- versions: modules
8425
- } = synced.ok;
8426
- /*
8427
- * A previous build still answering in http mode (S26): its versions
8428
- * are right for its own bases, and the site serves another build's.
8429
- * Said, so a Studio does not move to them; it asks again, and is
8430
- * answered by the live build soon.
8431
- */
8432
- const superseded = synced.ok.fetched.superseded === true;
8433
- /*
8434
- * Where this build's code is, for a Studio opened on another build
8435
- * (phase 9): it imports it and reloads in place. The answering
8436
- * build's, not the newest's: in http mode a previous build says it
8437
- * is superseded and its manifest is not adopted at all, and in fs
8438
- * and memory mode there is no entry -- HMR brings the code.
8439
- */
8440
- const registry = serverOps.buildRegistry();
8441
- const etag = await ops.manifestTag(project);
8442
8602
  const headers = {
8443
- ETag: etag,
8603
+ ETag: answer.tag,
8444
8604
  "Cache-Control": "no-cache"
8445
8605
  };
8446
- const held = req.headers["if-none-match"];
8447
- if (held === etag) {
8606
+ if (answer.type === "unchanged") {
8448
8607
  return {
8449
8608
  status: 304,
8450
8609
  headers
8451
8610
  };
8452
8611
  }
8453
- ops.rememberManifest(etag, project, modules);
8454
- /*
8455
- * "Since seq N": the modules that moved since the manifest the
8456
- * client holds -- the one its ETag names, answered at N -- when this
8457
- * instance answered it. Otherwise whole, which is always correct.
8458
- */
8459
- const since = req.query.since !== undefined ? Number(req.query.since) : NaN;
8460
- if (held !== undefined && Number.isSafeInteger(since)) {
8461
- const moved = ops.manifestSince(held, since, project, modules);
8462
- if (moved !== null) {
8463
- return {
8464
- status: 200,
8465
- headers,
8466
- json: {
8467
- project,
8468
- modules: moved,
8469
- tag: etag,
8470
- since,
8471
- ...(superseded ? {
8472
- superseded
8473
- } : {}),
8474
- ...(registry !== null ? {
8475
- registry
8476
- } : {})
8477
- }
8478
- };
8479
- }
8480
- }
8481
8612
  return {
8482
8613
  status: 200,
8483
8614
  headers,
8484
- json: {
8485
- project,
8486
- modules,
8487
- tag: etag,
8488
- ...(superseded ? {
8489
- superseded
8490
- } : {}),
8491
- ...(registry !== null ? {
8492
- registry
8493
- } : {})
8494
- }
8615
+ json: answer.json
8495
8616
  };
8496
8617
  }
8497
8618
  },
@@ -8520,8 +8641,18 @@ const ValServer = (valModules, options, callbacks, platform) => {
8520
8641
  // Before the stat: a new build counts itself here, once, so the
8521
8642
  // `seq` the stat reads is already past it.
8522
8643
  const versionBefore = await serverOps.projectVersion();
8644
+ /*
8645
+ * The manifest the Studio holds, or null for "none yet": asked, so
8646
+ * the answer carries the manifest too and another editor's keystroke
8647
+ * costs no `GET /manifest`. Not the stat's to read.
8648
+ */
8649
+ const {
8650
+ manifest: heldManifest,
8651
+ ...statBody
8652
+ } = req.body ?? {};
8653
+ const askedManifest = req.body !== null && "manifest" in req.body;
8523
8654
  const currentStat = await serverOps.getStat({
8524
- ...req.body,
8655
+ ...statBody,
8525
8656
  profileId
8526
8657
  });
8527
8658
  if (currentStat.type === "error" && currentStat.networkError) {
@@ -8601,14 +8732,26 @@ const ValServer = (valModules, options, callbacks, platform) => {
8601
8732
  seq: undefined
8602
8733
  };
8603
8734
  const version = {
8604
- build: versionBefore.build,
8735
+ // The build the chain is relative to: the newest build's in fs and
8736
+ // memory, so every instance on one store answers the same one and a
8737
+ // long poll can compare a Studio's echo with it (`chainBuild`).
8738
+ build: await serverOps.chainBuild(),
8605
8739
  seq: statSeq ?? ("headVersion" in currentStat ? currentStat.headVersion : undefined) ?? versionBefore.seq
8606
8740
  };
8741
+ /*
8742
+ * Read after the stat, so it is at least as new as `version`. An
8743
+ * error is left out rather than failing the stat: the Studio then
8744
+ * reads `GET /manifest`, which reports it.
8745
+ */
8746
+ const manifest = askedManifest ? await statManifestOf(heldManifest ?? null, version.seq) : undefined;
8607
8747
  return {
8608
8748
  status: 200,
8609
8749
  json: {
8610
8750
  ...stat,
8611
8751
  version,
8752
+ ...(manifest ? {
8753
+ manifest
8754
+ } : {}),
8612
8755
  profileId: profileId ?? null,
8613
8756
  mode,
8614
8757
  ...(publishRefusal ? {
@@ -9475,173 +9618,6 @@ const ValServer = (valModules, options, callbacks, platform) => {
9475
9618
  };
9476
9619
  }
9477
9620
  },
9478
- // #region sources
9479
- "/sources/~": {
9480
- PUT: async req => {
9481
- const query = req.query;
9482
- // Only the modules under this path, when one is given.
9483
- const only = req.path ? moduleFilePathOfRoute(req.path) : null;
9484
- const auth = getAuth(req.cookies);
9485
- if (auth.error) {
9486
- return {
9487
- status: 401,
9488
- json: {
9489
- message: auth.error
9490
- }
9491
- };
9492
- }
9493
- if (isHttpOps(serverOps) && !("id" in auth)) {
9494
- return {
9495
- status: 401,
9496
- json: {
9497
- message: "Unauthorized"
9498
- }
9499
- };
9500
- }
9501
- const sourcesSha = await serverOps.getSourcesSha();
9502
- const moduleErrors = await serverOps.getModuleErrors();
9503
- if ((moduleErrors === null || moduleErrors === void 0 ? void 0 : moduleErrors.length) > 0) {
9504
- Internal.log.error("Module errors", moduleErrors);
9505
- return {
9506
- status: 500,
9507
- json: {
9508
- message: "Val is not correctly setup. Check the val.modules file",
9509
- details: moduleErrors
9510
- }
9511
- };
9512
- }
9513
- const schemaSha = await serverOps.getSchemaSha();
9514
- /*
9515
- * No previews and no validation here any more (phase 6 of
9516
- * `docs/plans/one-content-model.md`): they cost every request the
9517
- * whole site, and nothing that reads this used them -- the Studio
9518
- * computes previews from its own schema instances and validates in
9519
- * its own worker and through `/validate`, one module at a time; the
9520
- * draft readers read the head. `validate_sources` and
9521
- * `validate_binary_files` are still accepted, and ignored.
9522
- */
9523
- // Default to true to keep the legacy contract for older clients.
9524
- // The studio client always passes false: it owns patch application,
9525
- // and treats /sources/~ as a pure un-patched read.
9526
- const applyPatches = query.apply_patches !== false;
9527
- if (query.exclude_patches === true) {
9528
- // The base alone: what a Studio of the release before fetched
9529
- // (`BaseAlignment`). Nothing of this release asks it; it goes with
9530
- // `PUT /sources/~` (see "Release-delayed deletions" in
9531
- // `docs/plans/one-content-model.md`).
9532
- const sources = await serverOps.getBaseSources();
9533
- const versions = await serverOps.moduleVersions([], false);
9534
- const modules = {};
9535
- for (const [path, source] of Object.entries(sources)) {
9536
- const moduleFilePath = path;
9537
- if (only !== null && !moduleFilePath.startsWith(only)) continue;
9538
- modules[moduleFilePath] = {
9539
- source,
9540
- ...(versions[moduleFilePath] !== undefined ? {
9541
- version: versions[moduleFilePath]
9542
- } : {})
9543
- };
9544
- }
9545
- return {
9546
- status: 200,
9547
- json: {
9548
- schemaSha,
9549
- sourcesSha,
9550
- modules,
9551
- project: await serverOps.projectVersion()
9552
- }
9553
- };
9554
- }
9555
- /*
9556
- * Through the head: the chain re-read only when it moved, and each
9557
- * patched module made once per version and kept -- not the whole
9558
- * chain fetched and applied on every request.
9559
- */
9560
- const tree = await serverOps.head.tree();
9561
- if ("error" in tree) {
9562
- if (tree.error.unauthorized) {
9563
- return {
9564
- status: 401,
9565
- json: {
9566
- message: "Unauthorized"
9567
- }
9568
- };
9569
- }
9570
- return {
9571
- status: 500,
9572
- json: {
9573
- message: tree.error.message,
9574
- details: {
9575
- message: tree.error.message
9576
- }
9577
- }
9578
- };
9579
- }
9580
- const {
9581
- chain
9582
- } = tree.ok;
9583
- const baseSources = applyPatches ? await serverOps.getBaseSources() : null;
9584
- const sources = applyPatches ? tree.ok.sources : await serverOps.getBaseSources();
9585
- const versions = applyPatches ? tree.ok.versions : await serverOps.moduleVersions(chain.overlay, false);
9586
- const modules = {};
9587
- for (const [moduleFilePathS, module] of Object.entries(sources)) {
9588
- var _chain$analysis$patch, _chain$analysis$patch2;
9589
- const moduleFilePath = moduleFilePathS;
9590
- if (only !== null && !moduleFilePath.startsWith(only)) continue;
9591
- const skippedPatches = [];
9592
- const patchErrors = {};
9593
- const appliedPatches = ((_chain$analysis$patch = chain.analysis.patchesByModule[moduleFilePath]) === null || _chain$analysis$patch === void 0 ? void 0 : _chain$analysis$patch.map(p => p.patchId)) || [];
9594
- if (applyPatches) {
9595
- for (const {
9596
- patchId,
9597
- skipped,
9598
- error
9599
- } of tree.ok.errors[moduleFilePath] || []) {
9600
- if (skipped) {
9601
- skippedPatches.push(patchId);
9602
- } else {
9603
- patchErrors[patchId] = {
9604
- message: error.message
9605
- };
9606
- }
9607
- }
9608
- }
9609
- const hasPatches = (((_chain$analysis$patch2 = chain.analysis.patchesByModule[moduleFilePath]) === null || _chain$analysis$patch2 === void 0 ? void 0 : _chain$analysis$patch2.length) ?? 0) > 0;
9610
- modules[moduleFilePath] = {
9611
- source: module,
9612
- // baseSource is only meaningful when the server applied patches:
9613
- // with apply_patches=false, `source` is already un-patched.
9614
- ...(baseSources !== null && hasPatches ? {
9615
- baseSource: baseSources[moduleFilePath]
9616
- } : {}),
9617
- ...(appliedPatches.length > 0 || skippedPatches.length > 0 || Object.keys(patchErrors).length > 0 ? {
9618
- patches: {
9619
- applied: appliedPatches,
9620
- ...(skippedPatches.length > 0 ? {
9621
- skipped: skippedPatches
9622
- } : {}),
9623
- ...(Object.keys(patchErrors).length > 0 ? {
9624
- errors: patchErrors
9625
- } : {})
9626
- }
9627
- } : {}),
9628
- ...(versions[moduleFilePath] !== undefined ? {
9629
- version: versions[moduleFilePath]
9630
- } : {})
9631
- };
9632
- }
9633
- const res = {
9634
- status: 200,
9635
- json: {
9636
- schemaSha,
9637
- sourcesSha,
9638
- modules,
9639
- project: chain.project
9640
- }
9641
- };
9642
- return res;
9643
- }
9644
- },
9645
9621
  // #region modules
9646
9622
  /*
9647
9623
  * One module by version (phase 6 of `docs/plans/one-content-model.md`):
@@ -12227,7 +12203,14 @@ class ValOpsMemory extends ValOps {
12227
12203
  // observed, either by `differs` below or by the recheck in parkUntilChange.
12228
12204
  const seenAt = this.changeCount;
12229
12205
  const before = await this.currentStat();
12230
- const differs = now => params === null || params.baseSha !== now.baseSha || params.schemaSha !== now.schemaSha || (params.patches ?? []).join(",") !== now.patches.join(",");
12206
+ /*
12207
+ * Another build is told by the version the Studio echoes: the build the
12208
+ * chain is relative to (`chainBuild`), which every instance on this store
12209
+ * answers with. A Studio that echoes no version (the release that
12210
+ * shipped the one content model) is told by `schemaSha`, as before.
12211
+ */
12212
+ const build = await this.chainBuild();
12213
+ const differs = now => params === null || params.baseSha !== now.baseSha || (params.version !== undefined ? params.version.build !== build : params.schemaSha !== now.schemaSha) || (params.patches ?? []).join(",") !== now.patches.join(",");
12231
12214
 
12232
12215
  // Already behind: answer now. This is the case that matters for latency --
12233
12216
  // the client has just written a patch and is asking what happened.
@@ -12627,6 +12610,61 @@ class ValOpsMemory extends ValOps {
12627
12610
 
12628
12611
  /** Kept exported so a host can name the error shape it may get back. */
12629
12612
 
12613
+ /**
12614
+ * What `redirect_to` may be on Val's routes (`/enable`, `/disable`,
12615
+ * `/authorize`, `/draft/enable`, ...): an absolute URL, or a path on the site
12616
+ * that was asked, which is made absolute against the request's own origin.
12617
+ *
12618
+ * A path used to go through as it was, and two things downstream need an
12619
+ * absolute URL: `/authorize` reads the origin off it to build the callback,
12620
+ * and Next's `NextResponse.redirect` refuses anything else. Both threw, so
12621
+ * `/api/val/enable?redirect_to=/` answered 500 in a Next app.
12622
+ *
12623
+ * `//host/path` is a URL on another host, not a path on this one, and
12624
+ * anything that is neither is refused with what is accepted instead. An
12625
+ * empty value is not resolved at all: the router leaves it to the route, as
12626
+ * it always was (`/logout` reads it as absent).
12627
+ */
12628
+
12629
+ const REDIRECT_TO_ACCEPTS = "redirect_to must be an absolute URL (https://example.com/page) or a path on this site starting with / (/page).";
12630
+ function resolveRedirectTo(value, requestOrigin) {
12631
+ if (value.startsWith("/") && !value.startsWith("//")) {
12632
+ // A path stays on this origin. `/\host/x` also starts with one slash,
12633
+ // and a URL parser reads the backslash as a second one: another host,
12634
+ // not a path here.
12635
+ const resolved = parsed(value, requestOrigin);
12636
+ return resolved !== null && resolved.origin === new URL(requestOrigin).origin ? {
12637
+ ok: true,
12638
+ href: resolved.href
12639
+ } : {
12640
+ ok: false,
12641
+ message: REDIRECT_TO_ACCEPTS
12642
+ };
12643
+ }
12644
+ if (parsed(value) !== null) {
12645
+ return {
12646
+ ok: true,
12647
+ href: value
12648
+ };
12649
+ }
12650
+ return {
12651
+ ok: false,
12652
+ message: REDIRECT_TO_ACCEPTS
12653
+ };
12654
+ }
12655
+
12656
+ /**
12657
+ * `value` as a URL, or null. Not `URL.canParse`: this runs in the browser
12658
+ * too (demo mode), where it is missing before Safari 17 and Chrome 120.
12659
+ */
12660
+ function parsed(value, base) {
12661
+ try {
12662
+ return new URL(value, base);
12663
+ } catch {
12664
+ return null;
12665
+ }
12666
+ }
12667
+
12630
12668
  /**
12631
12669
  * Val's API as a web-standard handler: a `Request` in, a converted result out.
12632
12670
  *
@@ -12833,6 +12871,22 @@ function createValRequestHandler(route, valServerPromise, convert, serveStatic)
12833
12871
  }
12834
12872
  query = queryRes.data;
12835
12873
  }
12874
+ // `redirect_to` is a URL or a path on this site: see `resolveRedirectTo`.
12875
+ if ("redirect_to" in query && typeof query.redirect_to === "string" && query.redirect_to !== "") {
12876
+ const resolved = resolveRedirectTo(query.redirect_to, url.origin);
12877
+ if (!resolved.ok) {
12878
+ return {
12879
+ status: 400,
12880
+ json: {
12881
+ message: resolved.message
12882
+ }
12883
+ };
12884
+ }
12885
+ query = {
12886
+ ...query,
12887
+ redirect_to: resolved.href
12888
+ };
12889
+ }
12836
12890
 
12837
12891
  /*
12838
12892
  * A throw from an endpoint is Val's to report, not the framework's.