@valbuild/server 0.151.0 → 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 (27) hide show
  1. package/CHANGELOG.md +22 -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-2827d620.esm.js → ValRouter-05fcb0da.esm.js} +218 -44
  6. package/dist/{ValRouter-ca467c83.cjs.dev.js → ValRouter-afcba447.cjs.dev.js} +217 -43
  7. package/dist/{ValRouter-d43e0a33.cjs.prod.js → ValRouter-b4e5b412.cjs.prod.js} +217 -43
  8. package/dist/{ValRouterCore-eb1f53f5.cjs.prod.js → ValRouterCore-a0ea46bf.cjs.prod.js} +47 -4
  9. package/dist/{ValRouterCore-6c29fb75.esm.js → ValRouterCore-b5d70504.esm.js} +47 -4
  10. package/dist/{ValRouterCore-0921fe9c.cjs.dev.js → ValRouterCore-d16c3327.cjs.dev.js} +47 -4
  11. package/dist/declarations/src/ValOps.d.ts +20 -2
  12. package/dist/{inTabFetch-35cf5698.cjs.prod.js → inTabFetch-095bb2be.cjs.prod.js} +1 -1
  13. package/dist/{inTabFetch-49db354b.cjs.dev.js → inTabFetch-2cbbfaa4.cjs.dev.js} +1 -1
  14. package/dist/{inTabFetch-0cd45721.esm.js → inTabFetch-91c3e85e.esm.js} +1 -1
  15. package/dist/{publishKit-3afe011d.cjs.dev.js → publishKit-4e2b0014.cjs.dev.js} +2 -2
  16. package/dist/{publishKit-24e1406b.cjs.prod.js → publishKit-6e52ffa6.cjs.prod.js} +2 -2
  17. package/dist/{publishKit-d2e3e44d.esm.js → publishKit-af14cd7a.esm.js} +3 -3
  18. package/dist/valbuild-server.cjs.dev.js +2 -2
  19. package/dist/valbuild-server.cjs.prod.js +2 -2
  20. package/dist/valbuild-server.esm.js +4 -4
  21. package/internal/browser/dist/valbuild-server-internal-browser.cjs.dev.js +2 -2
  22. package/internal/browser/dist/valbuild-server-internal-browser.cjs.prod.js +2 -2
  23. package/internal/browser/dist/valbuild-server-internal-browser.esm.js +2 -2
  24. package/internal/dist/valbuild-server-internal.cjs.dev.js +2 -2
  25. package/internal/dist/valbuild-server-internal.cjs.prod.js +2 -2
  26. package/internal/dist/valbuild-server-internal.esm.js +2 -2
  27. package/package.json +4 -4
@@ -2,7 +2,7 @@
2
2
 
3
3
  var core = require('@valbuild/core');
4
4
  var server = require('@valbuild/ui/server');
5
- var ValRouterCore = require('./ValRouterCore-0921fe9c.cjs.dev.js');
5
+ var ValRouterCore = require('./ValRouterCore-d16c3327.cjs.dev.js');
6
6
  var path = require('path');
7
7
  var ts = require('typescript');
8
8
  var z = require('zod');
@@ -2205,7 +2205,7 @@ function createFsSketchpadStore(rootDir, now = () => new Date()) {
2205
2205
  * as a bundler does.
2206
2206
  */
2207
2207
  function loadTypeScriptKit() {
2208
- return Promise.resolve().then(function () { return require('./publishKit-3afe011d.cjs.dev.js'); });
2208
+ return Promise.resolve().then(function () { return require('./publishKit-4e2b0014.cjs.dev.js'); });
2209
2209
  }
2210
2210
 
2211
2211
  /** Serializes a schema, or gives up quietly — serialization errors are reported elsewhere. */
@@ -4914,7 +4914,7 @@ async function patchModuleText(text, patch$1) {
4914
4914
  const {
4915
4915
  tsOps,
4916
4916
  parseValFile
4917
- } = await Promise.resolve().then(function () { return require('./publishKit-3afe011d.cjs.dev.js'); });
4917
+ } = await Promise.resolve().then(function () { return require('./publishKit-4e2b0014.cjs.dev.js'); });
4918
4918
  const applied = patch.applyPatch(parseValFile(text), tsOps, patch$1);
4919
4919
  if (fp.result.isErr(applied)) {
4920
4920
  const reason = messagesOf(applied.error).join(" ");
@@ -5012,7 +5012,7 @@ async function jsonEntryFilesOf(moduleFilePath, text) {
5012
5012
  parseValFile,
5013
5013
  analyzeValModule,
5014
5014
  analyzeJsonValuesEntries
5015
- } = await Promise.resolve().then(function () { return require('./publishKit-3afe011d.cjs.dev.js'); });
5015
+ } = await Promise.resolve().then(function () { return require('./publishKit-4e2b0014.cjs.dev.js'); });
5016
5016
  try {
5017
5017
  const analysis = analyzeValModule(parseValFile(text));
5018
5018
  if (fp.result.isErr(analysis)) return files;
@@ -5066,7 +5066,7 @@ async function stagedModuleSources(files) {
5066
5066
  parseValFile,
5067
5067
  analyzeValModule,
5068
5068
  evaluateModuleSource
5069
- } = await Promise.resolve().then(function () { return require('./publishKit-3afe011d.cjs.dev.js'); });
5069
+ } = await Promise.resolve().then(function () { return require('./publishKit-4e2b0014.cjs.dev.js'); });
5070
5070
  const sources = {};
5071
5071
  for (const {
5072
5072
  path,
@@ -5303,6 +5303,126 @@ function createHttpSketchpadStore(options) {
5303
5303
  };
5304
5304
  }
5305
5305
 
5306
+ /**
5307
+ * What to tell a person when the content service did not do what was asked.
5308
+ *
5309
+ * Written because the old sentences guessed, and guessed wrong. The patches
5310
+ * request answered "It is most likely due to a network issue. Check your
5311
+ * network connection" for ANY non-2xx answer -- including a 500 from a content
5312
+ * service whose database was missing a column, while the editor's network was
5313
+ * fine. A status code is an answer, and an answer means the network worked.
5314
+ *
5315
+ * So there are two shapes and they say different things:
5316
+ *
5317
+ * - **The service answered** with an error (`status`). It is named, the
5318
+ * sentence says it answered with an error or refused, and the status line
5319
+ * and the service's own words go in `details` for whoever has to report it.
5320
+ * - **The service could not be reached** (`unreachable`: `fetch` threw). That
5321
+ * IS a connection failure -- but between this site's server and the
5322
+ * service, not the editor's: the editor reached this server, or nothing
5323
+ * would have asked. So it never says "your connection" either.
5324
+ *
5325
+ * `retryable` is what decides whether a caller offers Try again: a 5xx, a
5326
+ * timeout or a service that could not be reached may well work the next time;
5327
+ * a 4xx is the same refusal again, and a button that leads to the same error
5328
+ * is worse than none.
5329
+ */
5330
+
5331
+ /** Long enough for a database error, short enough for a dialog. */
5332
+ const MAX_BODY_LENGTH = 500;
5333
+
5334
+ /**
5335
+ * The content service, by a name a person would recognise.
5336
+ *
5337
+ * `content.val.build` is Val Build. Anything else -- a self-hosted engine, a
5338
+ * staging host, the e2e mock -- is named by its host, so the person can tell
5339
+ * WHICH service failed rather than being told "the server", which in a page
5340
+ * that is itself a server says nothing.
5341
+ */
5342
+ function contentServiceName(contentUrl) {
5343
+ let host;
5344
+ try {
5345
+ host = new URL(contentUrl).host;
5346
+ } catch {
5347
+ return "the content service";
5348
+ }
5349
+ if (host === "val.build" || host.endsWith(".val.build")) {
5350
+ return "Val Build";
5351
+ }
5352
+ return `the content service at ${host}`;
5353
+ }
5354
+
5355
+ /**
5356
+ * The service answered `status`.
5357
+ *
5358
+ * `what` completes "could not ...": "load your changes", "save your change".
5359
+ * `body` is the raw response text; a JSON body is read the way the rest of
5360
+ * `ValOpsHttp` reads one, so `home`'s `{ message: "Internal Server Error",
5361
+ * details: "<the real cause>" }` keeps the real cause.
5362
+ */
5363
+ function contentServiceAnswered(service, what, response) {
5364
+ const retryable = response.status >= 500 || response.status === 408 || response.status === 429;
5365
+ const Service = capitalize(service);
5366
+ const message = retryable ? `${Service} answered with an error, so Val could not ${what}.` : `${Service} refused to ${what}.`;
5367
+ const statusLine = `HTTP ${response.status}${response.statusText ? ` ${response.statusText}` : ""} from ${service}`;
5368
+ const said = bodyText(response.body);
5369
+ return {
5370
+ message,
5371
+ details: said === null ? statusLine : `${statusLine}: ${said}`,
5372
+ retryable
5373
+ };
5374
+ }
5375
+
5376
+ /** `fetch` threw: the service was never heard from. */
5377
+ function contentServiceUnreachable(service, what, error) {
5378
+ return {
5379
+ message: `${capitalize(service)} could not be reached from this site's server, so Val could not ${what}.`,
5380
+ details: errorText(error),
5381
+ retryable: true
5382
+ };
5383
+ }
5384
+ function bodyText(body) {
5385
+ const trimmed = body.trim();
5386
+ if (trimmed === "") return null;
5387
+ let text = trimmed;
5388
+ try {
5389
+ const json = JSON.parse(trimmed);
5390
+ text = internal.getErrorMessageFromUnknownJson(json, trimmed);
5391
+ } catch {
5392
+ // Not JSON: the text is what the service said.
5393
+ }
5394
+ return text.length <= MAX_BODY_LENGTH ? text : `${text.slice(0, MAX_BODY_LENGTH)}…`;
5395
+ }
5396
+
5397
+ /**
5398
+ * The reason a `fetch` threw, with its cause.
5399
+ *
5400
+ * Node's `fetch` throws `TypeError: fetch failed` and hides what failed in
5401
+ * `cause` (`ECONNREFUSED`, a DNS miss, a certificate), which is the half
5402
+ * support needs.
5403
+ */
5404
+ function errorText(error) {
5405
+ if (!(error instanceof Error)) {
5406
+ return String(error);
5407
+ }
5408
+ const cause = error.cause;
5409
+ if (cause instanceof Error && cause.message !== error.message) {
5410
+ return `${error.message}: ${cause.message}`;
5411
+ }
5412
+ return error.message;
5413
+ }
5414
+ function capitalize(text) {
5415
+ return text.length === 0 ? text : text[0].toUpperCase() + text.slice(1);
5416
+ }
5417
+
5418
+ /**
5419
+ * One string, for the error shapes that carry only a `message`: the sentence
5420
+ * first, the evidence after it in brackets.
5421
+ */
5422
+ function contentServiceFailureText(failure) {
5423
+ return `${failure.message} (${failure.details})`;
5424
+ }
5425
+
5306
5426
  const textEncoder$2 = new TextEncoder();
5307
5427
 
5308
5428
  /** The body of the content API's 403 for a write the project's plan refuses. */
@@ -7460,7 +7580,7 @@ class ValOpsHttp extends ValRouterCore.ValOps {
7460
7580
  return {
7461
7581
  status: "error",
7462
7582
  error: {
7463
- message: "Could not get nonce. Error: " + (e instanceof Error ? e.message : e.toString())
7583
+ message: contentServiceFailureText(contentServiceUnreachable(contentServiceName(this.contentUrl), "connect for live updates", e))
7464
7584
  }
7465
7585
  };
7466
7586
  });
@@ -7726,7 +7846,12 @@ class ValOpsHttp extends ValRouterCore.ValOps {
7726
7846
  }
7727
7847
  };
7728
7848
  }
7729
- core.Internal.log.error("Could not get patches. HTTP error: " + patchesRes.status + " " + patchesRes.statusText);
7849
+ const failure = contentServiceAnswered(contentServiceName(this.contentUrl), "load your changes", {
7850
+ status: patchesRes.status,
7851
+ statusText: patchesRes.statusText,
7852
+ body: await patchesRes.text().catch(() => "")
7853
+ });
7854
+ core.Internal.log.error(`Could not get patches. ${failure.message} ${failure.details}`);
7730
7855
  if (patchesRes.status === 401) {
7731
7856
  return {
7732
7857
  patches,
@@ -7735,19 +7860,29 @@ class ValOpsHttp extends ValRouterCore.ValOps {
7735
7860
  }
7736
7861
  };
7737
7862
  }
7863
+ /*
7864
+ * The service ANSWERED, so this is not a network problem -- it used to
7865
+ * say "most likely due to a network issue. Check your network
7866
+ * connection" about a 500, to an editor whose network was fine. What it
7867
+ * said goes in `details`. The Studio asks again on the next `/stat`,
7868
+ * which fails fast and is retried while the service is failing.
7869
+ */
7738
7870
  return {
7739
7871
  patches,
7740
7872
  error: {
7741
- message: "Could not get your changes. It is most likely due to a network issue. Check your network connection and please try again."
7873
+ message: failure.message,
7874
+ details: failure.details
7742
7875
  }
7743
7876
  };
7744
7877
  } catch (err) {
7745
- core.Internal.log.error("Could not get patches (connection error):", err instanceof Error ? err.message : JSON.stringify(err));
7878
+ const failure = contentServiceUnreachable(contentServiceName(this.contentUrl), "load your changes", err);
7879
+ core.Internal.log.error(`Could not get patches. ${failure.message} ${failure.details}`);
7746
7880
  return {
7747
7881
  patches: [],
7748
7882
  networkError: true,
7749
7883
  error: {
7750
- message: `Error: ${err instanceof Error ? err.message : JSON.stringify(err)}`
7884
+ message: failure.message,
7885
+ details: failure.details
7751
7886
  }
7752
7887
  };
7753
7888
  }
@@ -7781,7 +7916,6 @@ class ValOpsHttp extends ValRouterCore.ValOps {
7781
7916
  coreVersion: core.Internal.VERSION.core
7782
7917
  })
7783
7918
  }).then(async res => {
7784
- var _res$headers$get3;
7785
7919
  if (res.ok) {
7786
7920
  const parsed = SavePatchResponse.safeParse(await res.json());
7787
7921
  if (parsed.success) {
@@ -7803,8 +7937,27 @@ class ValOpsHttp extends ValRouterCore.ValOps {
7803
7937
  message: "Conflict: " + (await res.text())
7804
7938
  });
7805
7939
  }
7806
- if ((_res$headers$get3 = res.headers.get("Content-Type")) !== null && _res$headers$get3 !== void 0 && _res$headers$get3.includes("application/json")) {
7807
- const json = await res.json();
7940
+ const body = await res.text();
7941
+ const failure = contentServiceAnswered(contentServiceName(this.contentUrl), "save your change", {
7942
+ status: res.status,
7943
+ statusText: res.statusText,
7944
+ body
7945
+ });
7946
+ if (failure.retryable) {
7947
+ /*
7948
+ * The SERVICE failed -- a 500 from a database missing a column was
7949
+ * the one that found this. It used to be an `other`, which the
7950
+ * Studio is told is a bad patch and drops: the field went back to
7951
+ * its old value and the status bar said "All changes saved".
7952
+ */
7953
+ return fp.result.err({
7954
+ errorType: "service-error",
7955
+ message: failure.message,
7956
+ details: failure.details
7957
+ });
7958
+ }
7959
+ const json = parseJsonOrNull(body);
7960
+ if (json !== null) {
7808
7961
  const message = internal.getErrorMessageFromUnknownJson(json, "Unknown error");
7809
7962
  /*
7810
7963
  * Refused for the plan: the patch is fine, and the reason comes as
@@ -7829,12 +7982,14 @@ class ValOpsHttp extends ValRouterCore.ValOps {
7829
7982
  }
7830
7983
  return fp.result.err({
7831
7984
  errorType: "other",
7832
- message: "Could not save patch. HTTP error: " + res.status + " " + res.statusText
7985
+ message: `${failure.message} ${failure.details}`
7833
7986
  });
7834
7987
  }).catch(e => {
7988
+ const failure = contentServiceUnreachable(contentServiceName(this.contentUrl), "save your change", e);
7835
7989
  return fp.result.err({
7836
- errorType: "other",
7837
- message: `Could save source file patch (connection error?): ${e instanceof Error ? e.message : e.toString()}`
7990
+ errorType: "service-error",
7991
+ message: failure.message,
7992
+ details: failure.details
7838
7993
  });
7839
7994
  });
7840
7995
  }
@@ -7875,13 +8030,17 @@ class ValOpsHttp extends ValRouterCore.ValOps {
7875
8030
  }
7876
8031
  return {
7877
8032
  error: {
7878
- message: "Could not save patch file. HTTP error: " + res.status + " " + res.statusText
8033
+ message: contentServiceFailureText(contentServiceAnswered(contentServiceName(this.contentUrl), "save the file", {
8034
+ status: res.status,
8035
+ statusText: res.statusText,
8036
+ body: await res.text().catch(() => "")
8037
+ }))
7879
8038
  }
7880
8039
  };
7881
8040
  }).catch(e => {
7882
8041
  return {
7883
8042
  error: {
7884
- message: `Could save source binary file in patch (connection error?): ${e.toString()}`
8043
+ message: contentServiceFailureText(contentServiceUnreachable(contentServiceName(this.contentUrl), "save the file", e))
7885
8044
  }
7886
8045
  };
7887
8046
  });
@@ -7919,13 +8078,17 @@ class ValOpsHttp extends ValRouterCore.ValOps {
7919
8078
  }
7920
8079
  return {
7921
8080
  error: {
7922
- message: "Could not get files. HTTP error: " + res.status + " " + res.statusText
8081
+ message: contentServiceFailureText(contentServiceAnswered(contentServiceName(this.contentUrl), "load the files", {
8082
+ status: res.status,
8083
+ statusText: res.statusText,
8084
+ body: await res.text().catch(() => "")
8085
+ }))
7923
8086
  }
7924
8087
  };
7925
8088
  }).catch(e => {
7926
8089
  return {
7927
8090
  error: {
7928
- message: `Could not get file (connection error?): ${e instanceof Error ? e.message : e.toString()}`
8091
+ message: contentServiceFailureText(contentServiceUnreachable(contentServiceName(this.contentUrl), "load the files", e))
7929
8092
  }
7930
8093
  };
7931
8094
  });
@@ -8071,14 +8234,18 @@ class ValOpsHttp extends ValRouterCore.ValOps {
8071
8234
  }
8072
8235
  return {
8073
8236
  errors: [{
8074
- message: "Could not get metadata. HTTP error: " + metadataRes.status + " " + metadataRes.statusText,
8237
+ message: contentServiceFailureText(contentServiceAnswered(contentServiceName(this.contentUrl), "read the file's details", {
8238
+ status: metadataRes.status,
8239
+ statusText: metadataRes.statusText,
8240
+ body: await metadataRes.text().catch(() => "")
8241
+ })),
8075
8242
  filePath
8076
8243
  }]
8077
8244
  };
8078
8245
  } catch (err) {
8079
8246
  return {
8080
8247
  errors: [{
8081
- message: "Could not get metadata (connection error?): " + (err instanceof Error ? err.message : (err === null || err === void 0 ? void 0 : err.toString()) || "unknown error")
8248
+ message: contentServiceFailureText(contentServiceUnreachable(contentServiceName(this.contentUrl), "read the file's details", err))
8082
8249
  }]
8083
8250
  };
8084
8251
  }
@@ -8143,13 +8310,17 @@ class ValOpsHttp extends ValRouterCore.ValOps {
8143
8310
  }
8144
8311
  return {
8145
8312
  error: {
8146
- message: "Could not delete patches. HTTP error: " + res.status + " " + res.statusText
8313
+ message: contentServiceFailureText(contentServiceAnswered(contentServiceName(this.contentUrl), "discard the changes", {
8314
+ status: res.status,
8315
+ statusText: res.statusText,
8316
+ body: await res.text().catch(() => "")
8317
+ }))
8147
8318
  }
8148
8319
  };
8149
8320
  }).catch(e => {
8150
8321
  return {
8151
8322
  error: {
8152
- message: `Could not delete patches (connection error?): ${e instanceof Error ? e.message : e.toString()}`
8323
+ message: contentServiceFailureText(contentServiceUnreachable(contentServiceName(this.contentUrl), "discard the changes", e))
8153
8324
  }
8154
8325
  };
8155
8326
  });
@@ -8162,7 +8333,7 @@ class ValOpsHttp extends ValRouterCore.ValOps {
8162
8333
  */
8163
8334
  intoProposal) {
8164
8335
  try {
8165
- var _this$proposal4, _res$headers$get4;
8336
+ var _this$proposal4;
8166
8337
  /*
8167
8338
  * A save in a proposal is a commit on the proposal's branch, at its own
8168
8339
  * route: the site's `/commit` refuses a proposal's patches, which is
@@ -8256,34 +8427,37 @@ class ValOpsHttp extends ValRouterCore.ValOps {
8256
8427
  }
8257
8428
  };
8258
8429
  }
8259
- if ((_res$headers$get4 = res.headers.get("Content-Type")) !== null && _res$headers$get4 !== void 0 && _res$headers$get4.includes("application/json")) {
8260
- const json = z.z.object({
8261
- isNotFastForward: z.z.boolean().optional()
8262
- }).safeParse(await res.json());
8263
- if (json.success && json.data.isNotFastForward) {
8264
- return {
8265
- isNotFastForward: true,
8266
- error: {
8267
- message: "Could not commit. Not a fast-forward commit"
8268
- }
8269
- };
8270
- }
8271
- const message = internal.getErrorMessageFromUnknownJson(json, "Unknown error");
8430
+ const body = await res.text().catch(() => "");
8431
+ const payload = parseJsonOrNull(body);
8432
+ const notFastForward = z.z.object({
8433
+ isNotFastForward: z.z.boolean().optional()
8434
+ }).safeParse(payload);
8435
+ if (notFastForward.success && notFastForward.data.isNotFastForward) {
8272
8436
  return {
8437
+ isNotFastForward: true,
8273
8438
  error: {
8274
- message
8439
+ message: "Could not commit. Not a fast-forward commit"
8275
8440
  }
8276
8441
  };
8277
8442
  }
8443
+ /*
8444
+ * The service's own words, read from the PAYLOAD. This used to hand
8445
+ * `getErrorMessageFromUnknownJson` the zod `safeParse` result, which
8446
+ * has no `message`, so every commit failure read "Unknown error".
8447
+ */
8278
8448
  return {
8279
8449
  error: {
8280
- message: "Could not commit. HTTP error: " + res.status + " " + res.statusText
8450
+ message: contentServiceFailureText(contentServiceAnswered(contentServiceName(this.contentUrl), "publish", {
8451
+ status: res.status,
8452
+ statusText: res.statusText,
8453
+ body
8454
+ }))
8281
8455
  }
8282
8456
  };
8283
8457
  } catch (err) {
8284
8458
  return {
8285
8459
  error: {
8286
- message: `Could not commit (connection error?): ${err instanceof Error ? err.message : (err === null || err === void 0 ? void 0 : err.toString()) || "unknown error"}`
8460
+ message: contentServiceFailureText(contentServiceUnreachable(contentServiceName(this.contentUrl), "publish", err))
8287
8461
  }
8288
8462
  };
8289
8463
  }
@@ -8322,9 +8496,9 @@ class ValOpsHttp extends ValRouterCore.ValOps {
8322
8496
  });
8323
8497
  }
8324
8498
  if (!res.ok) {
8325
- var _res$headers$get5;
8499
+ var _res$headers$get3;
8326
8500
  let message = `${res.status} ${res.statusText}`;
8327
- if ((_res$headers$get5 = res.headers.get("Content-Type")) !== null && _res$headers$get5 !== void 0 && _res$headers$get5.includes("application/json")) {
8501
+ if ((_res$headers$get3 = res.headers.get("Content-Type")) !== null && _res$headers$get3 !== void 0 && _res$headers$get3.includes("application/json")) {
8328
8502
  message = internal.getErrorMessageFromUnknownJson(await res.json(), message);
8329
8503
  }
8330
8504
  // A 5xx from the service reading a record it says it has is a real