@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
@@ -2848,6 +2848,14 @@ class ValOps {
2848
2848
  * {@link ValOpsOptions.stagedAfterBuild}.
2849
2849
  */
2850
2850
  heldStaged = new Map();
2851
+ /**
2852
+ * The Source of each module NEW in the proposal -- staged after the build,
2853
+ * and not in it at all -- read from its staged text: what a save applies
2854
+ * its patches to and records (`getSources`, `prepare`). Not served: no
2855
+ * reader at this build has its schema, and the page's code has not got
2856
+ * the module. See {@link readStagedSources}.
2857
+ */
2858
+ stagedNewSources = {};
2851
2859
  /**
2852
2860
  * What the BUILD evaluated each module to, and its version: before a
2853
2861
  * snapshot, a staging or a publish put anything over it. What a reader's
@@ -2965,6 +2973,7 @@ class ValOps {
2965
2973
  ...staged.sources
2966
2974
  };
2967
2975
  this.heldStaged = (staged === null || staged === void 0 ? void 0 : staged.held) ?? new Map();
2976
+ this.stagedNewSources = (staged === null || staged === void 0 ? void 0 : staged.added) ?? {};
2968
2977
  /*
2969
2978
  * The bundle's own Source for every module either layer replaces: a
2970
2979
  * replaced `.jsonValues()` module's markers came as JSON, and an entry
@@ -3061,13 +3070,10 @@ class ValOps {
3061
3070
  core.Internal.log.error("could not read the staged modules' Source", err);
3062
3071
  }
3063
3072
  const sources = {};
3073
+ const added = {};
3064
3074
  const held = new Map();
3065
3075
  for (const path of paths) {
3066
3076
  const source = read[path];
3067
- if (bundled[path] === undefined) {
3068
- // Not in this build at all: nothing here serves it, or saves it.
3069
- continue;
3070
- }
3071
3077
  if (source === undefined) {
3072
3078
  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.`);
3073
3079
  continue;
@@ -3076,10 +3082,21 @@ class ValOps {
3076
3082
  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.`);
3077
3083
  continue;
3078
3084
  }
3085
+ if (bundled[path] === undefined) {
3086
+ // New in the proposal: this build has no such module. Its Source is
3087
+ // saved from its text all the same (a save applies its patches to
3088
+ // it, with no schema), and it is not served: see `stagedNewSources`.
3089
+ // It used to be skipped here, so a save recorded its text and no
3090
+ // Source, and the address went on serving what the module was
3091
+ // created with after the build (C11d).
3092
+ added[path] = source;
3093
+ continue;
3094
+ }
3079
3095
  sources[path] = source;
3080
3096
  }
3081
3097
  return {
3082
3098
  sources,
3099
+ added,
3083
3100
  held
3084
3101
  };
3085
3102
  }
@@ -3425,6 +3442,34 @@ class ValOps {
3425
3442
  return this.getSourcesSha();
3426
3443
  }
3427
3444
 
3445
+ /**
3446
+ * The build the chain is relative to, by its version's name
3447
+ * (`ProjectVersion.build`): the newest build's in fs and memory, this
3448
+ * instance's own in http -- the same rule as {@link chainBaseSha}, and for
3449
+ * the same reason. `/stat` answers it as `version.build`, and the fs and
3450
+ * memory long poll compare a Studio's echoed version with it, so every
3451
+ * instance answering one store names one build: a Studio parked on the
3452
+ * previous build's instance is told of the new build once, not on every
3453
+ * poll.
3454
+ */
3455
+ async chainBuild() {
3456
+ const newest = this.newestOps();
3457
+ if (newest !== this) {
3458
+ try {
3459
+ return await newest.ownBuild();
3460
+ } catch (err) {
3461
+ core.Internal.log.debug("could not read the newest build's version; answering with this one's", err);
3462
+ }
3463
+ }
3464
+ return this.ownBuild();
3465
+ }
3466
+
3467
+ /** This instance's own build, as `ProjectVersion.build` names it. */
3468
+ async ownBuild() {
3469
+ await this.initSources();
3470
+ return this.buildVersion ?? "";
3471
+ }
3472
+
3428
3473
  /**
3429
3474
  * The data layer whose head answers what each module IS for this store's
3430
3475
  * chain (`GET /manifest`, `GET /modules`): this one, unless the store keeps
@@ -3876,7 +3921,9 @@ class ValOps {
3876
3921
  };
3877
3922
  for (const patchData of analysis.patches) {
3878
3923
  const path = patchData.path;
3879
- const base = sources[path];
3924
+ // A module new in the proposal has no base in this build: its staged
3925
+ // text's Source is the one its patches apply to.
3926
+ const base = sources[path] ?? this.stagedNewSources[path];
3880
3927
  if (base === undefined) {
3881
3928
  if (!errors[path]) {
3882
3929
  errors[path] = [];
@@ -5059,6 +5106,19 @@ class ValOps {
5059
5106
  // other module its history.
5060
5107
  serialized = undefined;
5061
5108
  }
5109
+ const staged = stagedAfterBuild[path];
5110
+ if (!serialized && staged !== undefined && this.stagedNewSources[path] !== undefined) {
5111
+ // New in the proposal: no schema of this build's, and content records
5112
+ // the staging's. Without this the save recorded the module's text
5113
+ // and no Source (C11d).
5114
+ const source = sourcesAfter[path];
5115
+ moduleVersions[path] = {
5116
+ source: source === undefined ? null : source,
5117
+ schema: null,
5118
+ staged
5119
+ };
5120
+ continue;
5121
+ }
5062
5122
  if (!serialized) {
5063
5123
  continue;
5064
5124
  }
@@ -6199,7 +6259,7 @@ var packageJson = {
6199
6259
  ],
6200
6260
  exports: true
6201
6261
  },
6202
- version: "0.152.2",
6262
+ version: "0.153.1",
6203
6263
  scripts: {
6204
6264
  typecheck: "tsc --noEmit",
6205
6265
  test: "jest",
@@ -7803,6 +7863,133 @@ const ValServer = (valModules, options, callbacks, platform) => {
7803
7863
  };
7804
7864
  };
7805
7865
 
7866
+ /**
7867
+ * What `GET /manifest` answers, and what `/stat` carries when the Studio
7868
+ * says which manifest it holds: ONE function, so the two cannot disagree
7869
+ * about a delta. `held` is the ETag the client holds and `since` the `seq`
7870
+ * it was answered at; a delta is relative to exactly that one.
7871
+ */
7872
+ const manifestAnswer = async (held, since) => {
7873
+ // Through the head: "has the chain moved?" is its cheap `seq`
7874
+ // (`ValOps.chainSeq`), and the chain is read only when it moved --
7875
+ // in http mode one small read of the content service, not the
7876
+ // whole chain on every poll of every Studio. The NEWEST build's
7877
+ // head where the store keeps one chain for every build (fs,
7878
+ // memory): a previous build still answering would name its own,
7879
+ // older bases (S26, `ValOps.newestOps`).
7880
+ const ops = serverOps.newestOps();
7881
+ const synced = await ops.head.sync();
7882
+ if ("error" in synced) {
7883
+ return {
7884
+ type: "error",
7885
+ unauthorized: synced.error.unauthorized === true,
7886
+ message: synced.error.message
7887
+ };
7888
+ }
7889
+ const {
7890
+ project,
7891
+ versions: modules
7892
+ } = synced.ok;
7893
+ /*
7894
+ * A previous build still answering in http mode (S26): its versions
7895
+ * are right for its own bases, and the site serves another build's.
7896
+ * Said, so a Studio does not move to them; it asks again, and is
7897
+ * answered by the live build soon.
7898
+ */
7899
+ const superseded = synced.ok.fetched.superseded === true;
7900
+ /*
7901
+ * Where this build's code is, for a Studio opened on another build
7902
+ * (phase 9): it imports it and reloads in place. The answering
7903
+ * build's, not the newest's: in http mode a previous build says it
7904
+ * is superseded and its manifest is not adopted at all, and in fs
7905
+ * and memory mode there is no entry -- HMR brings the code.
7906
+ */
7907
+ const registry = serverOps.buildRegistry();
7908
+ const tag = await ops.manifestTag(project);
7909
+ if (held === tag) {
7910
+ return {
7911
+ type: "unchanged",
7912
+ tag,
7913
+ seq: project.seq
7914
+ };
7915
+ }
7916
+ ops.rememberManifest(tag, project, modules);
7917
+ const extra = {
7918
+ ...(superseded ? {
7919
+ superseded
7920
+ } : {}),
7921
+ ...(registry !== null ? {
7922
+ registry
7923
+ } : {})
7924
+ };
7925
+ /*
7926
+ * "Since seq N": the modules that moved since the manifest the
7927
+ * client holds -- the one its ETag names, answered at N -- when this
7928
+ * instance answered it. Otherwise whole, which is always correct.
7929
+ */
7930
+ if (held !== undefined && since !== undefined && Number.isSafeInteger(since)) {
7931
+ const moved = ops.manifestSince(held, since, project, modules);
7932
+ if (moved !== null) {
7933
+ return {
7934
+ type: "manifest",
7935
+ tag,
7936
+ json: {
7937
+ project,
7938
+ modules: moved,
7939
+ tag,
7940
+ since,
7941
+ ...extra
7942
+ }
7943
+ };
7944
+ }
7945
+ }
7946
+ return {
7947
+ type: "manifest",
7948
+ tag,
7949
+ json: {
7950
+ project,
7951
+ modules,
7952
+ tag,
7953
+ ...extra
7954
+ }
7955
+ };
7956
+ };
7957
+
7958
+ /**
7959
+ * {@link manifestAnswer} as `/stat` carries it (`StatManifest`), for a stat
7960
+ * answered at `seq`.
7961
+ *
7962
+ * Not read at all while `seq` is the one the held manifest was answered at:
7963
+ * a Studio reads a manifest only for a stat whose `seq` moved, and reading
7964
+ * it is a request to the content service in http mode -- on every poll of
7965
+ * every Studio, for an answer nobody reads. `unchanged` then hands the held
7966
+ * one back, so the next request names it again.
7967
+ */
7968
+ const statManifestOf = async (held, seq) => {
7969
+ if (held !== null && held.seq === seq) {
7970
+ return {
7971
+ unchanged: true,
7972
+ tag: held.tag,
7973
+ seq: held.seq
7974
+ };
7975
+ }
7976
+ const answer = await manifestAnswer(held === null || held === void 0 ? void 0 : held.tag, held === null || held === void 0 ? void 0 : held.seq);
7977
+ if (answer.type === "error") {
7978
+ return undefined;
7979
+ }
7980
+ if (answer.type === "unchanged") {
7981
+ return {
7982
+ unchanged: true,
7983
+ tag: answer.tag,
7984
+ seq: answer.seq
7985
+ };
7986
+ }
7987
+ return held !== null && answer.json.since !== undefined ? {
7988
+ ...answer.json,
7989
+ sinceTag: held.tag
7990
+ } : answer.json;
7991
+ };
7992
+
7806
7993
  /**
7807
7994
  * What the Studio's full check reads: base plus the patches the caller is
7808
7995
  * showing, as `/sources/~` resolves an explicit `patch_id` list -- including
@@ -8400,104 +8587,38 @@ const ValServer = (valModules, options, callbacks, platform) => {
8400
8587
  }
8401
8588
  };
8402
8589
  }
8403
- // Through the head: "has the chain moved?" is its cheap `seq`
8404
- // (`ValOps.chainSeq`), and the chain is read only when it moved --
8405
- // in http mode one small read of the content service, not the
8406
- // whole chain on every poll of every Studio. The NEWEST build's
8407
- // head where the store keeps one chain for every build (fs,
8408
- // memory): a previous build still answering would name its own,
8409
- // older bases (S26, `ValOps.newestOps`).
8410
- const ops = serverOps.newestOps();
8411
- const synced = await ops.head.sync();
8412
- if ("error" in synced) {
8413
- if (synced.error.unauthorized) {
8590
+ const since = req.query.since !== undefined ? Number(req.query.since) : undefined;
8591
+ const answer = await manifestAnswer(req.headers["if-none-match"], since);
8592
+ if (answer.type === "error") {
8593
+ if (answer.unauthorized) {
8414
8594
  return {
8415
8595
  status: 401,
8416
8596
  json: {
8417
- message: synced.error.message
8597
+ message: answer.message
8418
8598
  }
8419
8599
  };
8420
8600
  }
8421
8601
  return {
8422
8602
  status: 500,
8423
8603
  json: {
8424
- message: synced.error.message
8604
+ message: answer.message
8425
8605
  }
8426
8606
  };
8427
8607
  }
8428
- const {
8429
- project,
8430
- versions: modules
8431
- } = synced.ok;
8432
- /*
8433
- * A previous build still answering in http mode (S26): its versions
8434
- * are right for its own bases, and the site serves another build's.
8435
- * Said, so a Studio does not move to them; it asks again, and is
8436
- * answered by the live build soon.
8437
- */
8438
- const superseded = synced.ok.fetched.superseded === true;
8439
- /*
8440
- * Where this build's code is, for a Studio opened on another build
8441
- * (phase 9): it imports it and reloads in place. The answering
8442
- * build's, not the newest's: in http mode a previous build says it
8443
- * is superseded and its manifest is not adopted at all, and in fs
8444
- * and memory mode there is no entry -- HMR brings the code.
8445
- */
8446
- const registry = serverOps.buildRegistry();
8447
- const etag = await ops.manifestTag(project);
8448
8608
  const headers = {
8449
- ETag: etag,
8609
+ ETag: answer.tag,
8450
8610
  "Cache-Control": "no-cache"
8451
8611
  };
8452
- const held = req.headers["if-none-match"];
8453
- if (held === etag) {
8612
+ if (answer.type === "unchanged") {
8454
8613
  return {
8455
8614
  status: 304,
8456
8615
  headers
8457
8616
  };
8458
8617
  }
8459
- ops.rememberManifest(etag, project, modules);
8460
- /*
8461
- * "Since seq N": the modules that moved since the manifest the
8462
- * client holds -- the one its ETag names, answered at N -- when this
8463
- * instance answered it. Otherwise whole, which is always correct.
8464
- */
8465
- const since = req.query.since !== undefined ? Number(req.query.since) : NaN;
8466
- if (held !== undefined && Number.isSafeInteger(since)) {
8467
- const moved = ops.manifestSince(held, since, project, modules);
8468
- if (moved !== null) {
8469
- return {
8470
- status: 200,
8471
- headers,
8472
- json: {
8473
- project,
8474
- modules: moved,
8475
- tag: etag,
8476
- since,
8477
- ...(superseded ? {
8478
- superseded
8479
- } : {}),
8480
- ...(registry !== null ? {
8481
- registry
8482
- } : {})
8483
- }
8484
- };
8485
- }
8486
- }
8487
8618
  return {
8488
8619
  status: 200,
8489
8620
  headers,
8490
- json: {
8491
- project,
8492
- modules,
8493
- tag: etag,
8494
- ...(superseded ? {
8495
- superseded
8496
- } : {}),
8497
- ...(registry !== null ? {
8498
- registry
8499
- } : {})
8500
- }
8621
+ json: answer.json
8501
8622
  };
8502
8623
  }
8503
8624
  },
@@ -8526,8 +8647,18 @@ const ValServer = (valModules, options, callbacks, platform) => {
8526
8647
  // Before the stat: a new build counts itself here, once, so the
8527
8648
  // `seq` the stat reads is already past it.
8528
8649
  const versionBefore = await serverOps.projectVersion();
8650
+ /*
8651
+ * The manifest the Studio holds, or null for "none yet": asked, so
8652
+ * the answer carries the manifest too and another editor's keystroke
8653
+ * costs no `GET /manifest`. Not the stat's to read.
8654
+ */
8655
+ const {
8656
+ manifest: heldManifest,
8657
+ ...statBody
8658
+ } = req.body ?? {};
8659
+ const askedManifest = req.body !== null && "manifest" in req.body;
8529
8660
  const currentStat = await serverOps.getStat({
8530
- ...req.body,
8661
+ ...statBody,
8531
8662
  profileId
8532
8663
  });
8533
8664
  if (currentStat.type === "error" && currentStat.networkError) {
@@ -8607,14 +8738,26 @@ const ValServer = (valModules, options, callbacks, platform) => {
8607
8738
  seq: undefined
8608
8739
  };
8609
8740
  const version = {
8610
- build: versionBefore.build,
8741
+ // The build the chain is relative to: the newest build's in fs and
8742
+ // memory, so every instance on one store answers the same one and a
8743
+ // long poll can compare a Studio's echo with it (`chainBuild`).
8744
+ build: await serverOps.chainBuild(),
8611
8745
  seq: statSeq ?? ("headVersion" in currentStat ? currentStat.headVersion : undefined) ?? versionBefore.seq
8612
8746
  };
8747
+ /*
8748
+ * Read after the stat, so it is at least as new as `version`. An
8749
+ * error is left out rather than failing the stat: the Studio then
8750
+ * reads `GET /manifest`, which reports it.
8751
+ */
8752
+ const manifest = askedManifest ? await statManifestOf(heldManifest ?? null, version.seq) : undefined;
8613
8753
  return {
8614
8754
  status: 200,
8615
8755
  json: {
8616
8756
  ...stat,
8617
8757
  version,
8758
+ ...(manifest ? {
8759
+ manifest
8760
+ } : {}),
8618
8761
  profileId: profileId ?? null,
8619
8762
  mode,
8620
8763
  ...(publishRefusal ? {
@@ -9481,173 +9624,6 @@ const ValServer = (valModules, options, callbacks, platform) => {
9481
9624
  };
9482
9625
  }
9483
9626
  },
9484
- // #region sources
9485
- "/sources/~": {
9486
- PUT: async req => {
9487
- const query = req.query;
9488
- // Only the modules under this path, when one is given.
9489
- const only = req.path ? moduleFilePathOfRoute(req.path) : null;
9490
- const auth = getAuth(req.cookies);
9491
- if (auth.error) {
9492
- return {
9493
- status: 401,
9494
- json: {
9495
- message: auth.error
9496
- }
9497
- };
9498
- }
9499
- if (isHttpOps(serverOps) && !("id" in auth)) {
9500
- return {
9501
- status: 401,
9502
- json: {
9503
- message: "Unauthorized"
9504
- }
9505
- };
9506
- }
9507
- const sourcesSha = await serverOps.getSourcesSha();
9508
- const moduleErrors = await serverOps.getModuleErrors();
9509
- if ((moduleErrors === null || moduleErrors === void 0 ? void 0 : moduleErrors.length) > 0) {
9510
- core.Internal.log.error("Module errors", moduleErrors);
9511
- return {
9512
- status: 500,
9513
- json: {
9514
- message: "Val is not correctly setup. Check the val.modules file",
9515
- details: moduleErrors
9516
- }
9517
- };
9518
- }
9519
- const schemaSha = await serverOps.getSchemaSha();
9520
- /*
9521
- * No previews and no validation here any more (phase 6 of
9522
- * `docs/plans/one-content-model.md`): they cost every request the
9523
- * whole site, and nothing that reads this used them -- the Studio
9524
- * computes previews from its own schema instances and validates in
9525
- * its own worker and through `/validate`, one module at a time; the
9526
- * draft readers read the head. `validate_sources` and
9527
- * `validate_binary_files` are still accepted, and ignored.
9528
- */
9529
- // Default to true to keep the legacy contract for older clients.
9530
- // The studio client always passes false: it owns patch application,
9531
- // and treats /sources/~ as a pure un-patched read.
9532
- const applyPatches = query.apply_patches !== false;
9533
- if (query.exclude_patches === true) {
9534
- // The base alone: what a Studio of the release before fetched
9535
- // (`BaseAlignment`). Nothing of this release asks it; it goes with
9536
- // `PUT /sources/~` (see "Release-delayed deletions" in
9537
- // `docs/plans/one-content-model.md`).
9538
- const sources = await serverOps.getBaseSources();
9539
- const versions = await serverOps.moduleVersions([], false);
9540
- const modules = {};
9541
- for (const [path, source] of Object.entries(sources)) {
9542
- const moduleFilePath = path;
9543
- if (only !== null && !moduleFilePath.startsWith(only)) continue;
9544
- modules[moduleFilePath] = {
9545
- source,
9546
- ...(versions[moduleFilePath] !== undefined ? {
9547
- version: versions[moduleFilePath]
9548
- } : {})
9549
- };
9550
- }
9551
- return {
9552
- status: 200,
9553
- json: {
9554
- schemaSha,
9555
- sourcesSha,
9556
- modules,
9557
- project: await serverOps.projectVersion()
9558
- }
9559
- };
9560
- }
9561
- /*
9562
- * Through the head: the chain re-read only when it moved, and each
9563
- * patched module made once per version and kept -- not the whole
9564
- * chain fetched and applied on every request.
9565
- */
9566
- const tree = await serverOps.head.tree();
9567
- if ("error" in tree) {
9568
- if (tree.error.unauthorized) {
9569
- return {
9570
- status: 401,
9571
- json: {
9572
- message: "Unauthorized"
9573
- }
9574
- };
9575
- }
9576
- return {
9577
- status: 500,
9578
- json: {
9579
- message: tree.error.message,
9580
- details: {
9581
- message: tree.error.message
9582
- }
9583
- }
9584
- };
9585
- }
9586
- const {
9587
- chain
9588
- } = tree.ok;
9589
- const baseSources = applyPatches ? await serverOps.getBaseSources() : null;
9590
- const sources = applyPatches ? tree.ok.sources : await serverOps.getBaseSources();
9591
- const versions = applyPatches ? tree.ok.versions : await serverOps.moduleVersions(chain.overlay, false);
9592
- const modules = {};
9593
- for (const [moduleFilePathS, module] of Object.entries(sources)) {
9594
- var _chain$analysis$patch, _chain$analysis$patch2;
9595
- const moduleFilePath = moduleFilePathS;
9596
- if (only !== null && !moduleFilePath.startsWith(only)) continue;
9597
- const skippedPatches = [];
9598
- const patchErrors = {};
9599
- const appliedPatches = ((_chain$analysis$patch = chain.analysis.patchesByModule[moduleFilePath]) === null || _chain$analysis$patch === void 0 ? void 0 : _chain$analysis$patch.map(p => p.patchId)) || [];
9600
- if (applyPatches) {
9601
- for (const {
9602
- patchId,
9603
- skipped,
9604
- error
9605
- } of tree.ok.errors[moduleFilePath] || []) {
9606
- if (skipped) {
9607
- skippedPatches.push(patchId);
9608
- } else {
9609
- patchErrors[patchId] = {
9610
- message: error.message
9611
- };
9612
- }
9613
- }
9614
- }
9615
- const hasPatches = (((_chain$analysis$patch2 = chain.analysis.patchesByModule[moduleFilePath]) === null || _chain$analysis$patch2 === void 0 ? void 0 : _chain$analysis$patch2.length) ?? 0) > 0;
9616
- modules[moduleFilePath] = {
9617
- source: module,
9618
- // baseSource is only meaningful when the server applied patches:
9619
- // with apply_patches=false, `source` is already un-patched.
9620
- ...(baseSources !== null && hasPatches ? {
9621
- baseSource: baseSources[moduleFilePath]
9622
- } : {}),
9623
- ...(appliedPatches.length > 0 || skippedPatches.length > 0 || Object.keys(patchErrors).length > 0 ? {
9624
- patches: {
9625
- applied: appliedPatches,
9626
- ...(skippedPatches.length > 0 ? {
9627
- skipped: skippedPatches
9628
- } : {}),
9629
- ...(Object.keys(patchErrors).length > 0 ? {
9630
- errors: patchErrors
9631
- } : {})
9632
- }
9633
- } : {}),
9634
- ...(versions[moduleFilePath] !== undefined ? {
9635
- version: versions[moduleFilePath]
9636
- } : {})
9637
- };
9638
- }
9639
- const res = {
9640
- status: 200,
9641
- json: {
9642
- schemaSha,
9643
- sourcesSha,
9644
- modules,
9645
- project: chain.project
9646
- }
9647
- };
9648
- return res;
9649
- }
9650
- },
9651
9627
  // #region modules
9652
9628
  /*
9653
9629
  * One module by version (phase 6 of `docs/plans/one-content-model.md`):
@@ -12233,7 +12209,14 @@ class ValOpsMemory extends ValOps {
12233
12209
  // observed, either by `differs` below or by the recheck in parkUntilChange.
12234
12210
  const seenAt = this.changeCount;
12235
12211
  const before = await this.currentStat();
12236
- const differs = now => params === null || params.baseSha !== now.baseSha || params.schemaSha !== now.schemaSha || (params.patches ?? []).join(",") !== now.patches.join(",");
12212
+ /*
12213
+ * Another build is told by the version the Studio echoes: the build the
12214
+ * chain is relative to (`chainBuild`), which every instance on this store
12215
+ * answers with. A Studio that echoes no version (the release that
12216
+ * shipped the one content model) is told by `schemaSha`, as before.
12217
+ */
12218
+ const build = await this.chainBuild();
12219
+ 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(",");
12237
12220
 
12238
12221
  // Already behind: answer now. This is the case that matters for latency --
12239
12222
  // the client has just written a patch and is asking what happened.
@@ -12633,6 +12616,61 @@ class ValOpsMemory extends ValOps {
12633
12616
 
12634
12617
  /** Kept exported so a host can name the error shape it may get back. */
12635
12618
 
12619
+ /**
12620
+ * What `redirect_to` may be on Val's routes (`/enable`, `/disable`,
12621
+ * `/authorize`, `/draft/enable`, ...): an absolute URL, or a path on the site
12622
+ * that was asked, which is made absolute against the request's own origin.
12623
+ *
12624
+ * A path used to go through as it was, and two things downstream need an
12625
+ * absolute URL: `/authorize` reads the origin off it to build the callback,
12626
+ * and Next's `NextResponse.redirect` refuses anything else. Both threw, so
12627
+ * `/api/val/enable?redirect_to=/` answered 500 in a Next app.
12628
+ *
12629
+ * `//host/path` is a URL on another host, not a path on this one, and
12630
+ * anything that is neither is refused with what is accepted instead. An
12631
+ * empty value is not resolved at all: the router leaves it to the route, as
12632
+ * it always was (`/logout` reads it as absent).
12633
+ */
12634
+
12635
+ const REDIRECT_TO_ACCEPTS = "redirect_to must be an absolute URL (https://example.com/page) or a path on this site starting with / (/page).";
12636
+ function resolveRedirectTo(value, requestOrigin) {
12637
+ if (value.startsWith("/") && !value.startsWith("//")) {
12638
+ // A path stays on this origin. `/\host/x` also starts with one slash,
12639
+ // and a URL parser reads the backslash as a second one: another host,
12640
+ // not a path here.
12641
+ const resolved = parsed(value, requestOrigin);
12642
+ return resolved !== null && resolved.origin === new URL(requestOrigin).origin ? {
12643
+ ok: true,
12644
+ href: resolved.href
12645
+ } : {
12646
+ ok: false,
12647
+ message: REDIRECT_TO_ACCEPTS
12648
+ };
12649
+ }
12650
+ if (parsed(value) !== null) {
12651
+ return {
12652
+ ok: true,
12653
+ href: value
12654
+ };
12655
+ }
12656
+ return {
12657
+ ok: false,
12658
+ message: REDIRECT_TO_ACCEPTS
12659
+ };
12660
+ }
12661
+
12662
+ /**
12663
+ * `value` as a URL, or null. Not `URL.canParse`: this runs in the browser
12664
+ * too (demo mode), where it is missing before Safari 17 and Chrome 120.
12665
+ */
12666
+ function parsed(value, base) {
12667
+ try {
12668
+ return new URL(value, base);
12669
+ } catch {
12670
+ return null;
12671
+ }
12672
+ }
12673
+
12636
12674
  /**
12637
12675
  * Val's API as a web-standard handler: a `Request` in, a converted result out.
12638
12676
  *
@@ -12839,6 +12877,22 @@ function createValRequestHandler(route, valServerPromise, convert, serveStatic)
12839
12877
  }
12840
12878
  query = queryRes.data;
12841
12879
  }
12880
+ // `redirect_to` is a URL or a path on this site: see `resolveRedirectTo`.
12881
+ if ("redirect_to" in query && typeof query.redirect_to === "string" && query.redirect_to !== "") {
12882
+ const resolved = resolveRedirectTo(query.redirect_to, url.origin);
12883
+ if (!resolved.ok) {
12884
+ return {
12885
+ status: 400,
12886
+ json: {
12887
+ message: resolved.message
12888
+ }
12889
+ };
12890
+ }
12891
+ query = {
12892
+ ...query,
12893
+ redirect_to: resolved.href
12894
+ };
12895
+ }
12842
12896
 
12843
12897
  /*
12844
12898
  * A throw from an endpoint is Val's to report, not the framework's.