@rsc-kit/core 0.31.3 → 0.33.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.
@@ -0,0 +1,53 @@
1
+ /**
2
+ * A server action that failed, told to the browser in development.
3
+ *
4
+ * An action is a fetch from a page already on screen, so its failure has no
5
+ * page of its own to replace: it left the renderer as the server's generic
6
+ * 500 and reached the browser as "Server action failed with 500". The cause
7
+ * - a missing $fillable in PHP, a nil map in Go - was in the terminal and
8
+ * the network tab, and the page showed nothing at all.
9
+ *
10
+ * So in development the failure is answered with what it was: the message,
11
+ * the renderer's stack, and the backend's own type and frames when a host
12
+ * call failed there. The browser opens Vite's error overlay with it. Nothing
13
+ * here is sent in production, where the visitor is not the developer.
14
+ */
15
+ /** The header that marks the answer as this, so a proxy's own 500 is not read as one. */
16
+ export const DEV_ACTION_ERROR = "X-RSC-Dev-Error";
17
+ /** Whether the request is a server action's call. */
18
+ export function isActionCall(request) {
19
+ return request.method === "POST" && request.headers.get("X-RSC-Action") !== null;
20
+ }
21
+ /**
22
+ * What a failure says about itself. The backend's part is read from the
23
+ * cause a failed host call carries (hostCalls' withBackendTrace).
24
+ */
25
+ export function devActionError(error) {
26
+ const failure = error instanceof Error ? error : new Error(String(error));
27
+ const marker = "\nCaused in the backend by ";
28
+ const own = (failure.stack ?? failure.message).split(marker)[0];
29
+ const cause = failure.cause;
30
+ const described = { message: failure.message, stack: own };
31
+ if (cause instanceof Error && cause.name.startsWith("Backend ")) {
32
+ described.backend = {
33
+ type: cause.name.slice("Backend ".length),
34
+ message: cause.message,
35
+ frames: (cause.stack ?? "")
36
+ .split("\n")
37
+ .filter((line) => line.startsWith(" at "))
38
+ .map((line) => line.slice(" at ".length)),
39
+ };
40
+ }
41
+ return described;
42
+ }
43
+ export function devActionErrorResponse(error) {
44
+ return new Response(JSON.stringify(devActionError(error)), {
45
+ status: 500,
46
+ headers: {
47
+ "Content-Type": "application/json",
48
+ "Cache-Control": "no-store",
49
+ [DEV_ACTION_ERROR]: "1",
50
+ },
51
+ });
52
+ }
53
+ //# sourceMappingURL=devActionError.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"devActionError.js","sourceRoot":"","sources":["../src/devActionError.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAEH,yFAAyF;AACzF,MAAM,CAAC,MAAM,gBAAgB,GAAG,iBAAiB,CAAC;AAUlD,qDAAqD;AACrD,MAAM,UAAU,YAAY,CAAC,OAAgB;IAC3C,OAAO,OAAO,CAAC,MAAM,KAAK,MAAM,IAAI,OAAO,CAAC,OAAO,CAAC,GAAG,CAAC,cAAc,CAAC,KAAK,IAAI,CAAC;AACnF,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,cAAc,CAAC,KAAc;IAC3C,MAAM,OAAO,GAAG,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,KAAK,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC;IAC1E,MAAM,MAAM,GAAG,6BAA6B,CAAC;IAC7C,MAAM,GAAG,GAAG,CAAC,OAAO,CAAC,KAAK,IAAI,OAAO,CAAC,OAAO,CAAC,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC,CAAE,CAAC;IACjE,MAAM,KAAK,GAAG,OAAO,CAAC,KAAK,CAAC;IAC5B,MAAM,SAAS,GAAmB,EAAE,OAAO,EAAE,OAAO,CAAC,OAAO,EAAE,KAAK,EAAE,GAAG,EAAE,CAAC;IAE3E,IAAI,KAAK,YAAY,KAAK,IAAI,KAAK,CAAC,IAAI,CAAC,UAAU,CAAC,UAAU,CAAC,EAAE,CAAC;QAChE,SAAS,CAAC,OAAO,GAAG;YAClB,IAAI,EAAE,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,UAAU,CAAC,MAAM,CAAC;YACzC,OAAO,EAAE,KAAK,CAAC,OAAO;YACtB,MAAM,EAAE,CAAC,KAAK,CAAC,KAAK,IAAI,EAAE,CAAC;iBACxB,KAAK,CAAC,IAAI,CAAC;iBACX,MAAM,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,UAAU,CAAC,SAAS,CAAC,CAAC;iBAC5C,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,KAAK,CAAC,SAAS,CAAC,MAAM,CAAC,CAAC;SAC/C,CAAC;IACJ,CAAC;IAED,OAAO,SAAS,CAAC;AACnB,CAAC;AAED,MAAM,UAAU,sBAAsB,CAAC,KAAc;IACnD,OAAO,IAAI,QAAQ,CAAC,IAAI,CAAC,SAAS,CAAC,cAAc,CAAC,KAAK,CAAC,CAAC,EAAE;QACzD,MAAM,EAAE,GAAG;QACX,OAAO,EAAE;YACP,cAAc,EAAE,kBAAkB;YAClC,eAAe,EAAE,UAAU;YAC3B,CAAC,gBAAgB,CAAC,EAAE,GAAG;SACxB;KACF,CAAC,CAAC;AACL,CAAC","sourcesContent":["/**\n * A server action that failed, told to the browser in development.\n *\n * An action is a fetch from a page already on screen, so its failure has no\n * page of its own to replace: it left the renderer as the server's generic\n * 500 and reached the browser as \"Server action failed with 500\". The cause\n * - a missing $fillable in PHP, a nil map in Go - was in the terminal and\n * the network tab, and the page showed nothing at all.\n *\n * So in development the failure is answered with what it was: the message,\n * the renderer's stack, and the backend's own type and frames when a host\n * call failed there. The browser opens Vite's error overlay with it. Nothing\n * here is sent in production, where the visitor is not the developer.\n */\n\n/** The header that marks the answer as this, so a proxy's own 500 is not read as one. */\nexport const DEV_ACTION_ERROR = \"X-RSC-Dev-Error\";\n\nexport interface DevActionError {\n message: string;\n /** The renderer's frames. */\n stack: string;\n /** Where the backend says it failed: Laravel's app.debug, Go's Debug. */\n backend?: { type: string; message: string; frames: string[] };\n}\n\n/** Whether the request is a server action's call. */\nexport function isActionCall(request: Request): boolean {\n return request.method === \"POST\" && request.headers.get(\"X-RSC-Action\") !== null;\n}\n\n/**\n * What a failure says about itself. The backend's part is read from the\n * cause a failed host call carries (hostCalls' withBackendTrace).\n */\nexport function devActionError(error: unknown): DevActionError {\n const failure = error instanceof Error ? error : new Error(String(error));\n const marker = \"\\nCaused in the backend by \";\n const own = (failure.stack ?? failure.message).split(marker)[0]!;\n const cause = failure.cause;\n const described: DevActionError = { message: failure.message, stack: own };\n\n if (cause instanceof Error && cause.name.startsWith(\"Backend \")) {\n described.backend = {\n type: cause.name.slice(\"Backend \".length),\n message: cause.message,\n frames: (cause.stack ?? \"\")\n .split(\"\\n\")\n .filter((line) => line.startsWith(\" at \"))\n .map((line) => line.slice(\" at \".length)),\n };\n }\n\n return described;\n}\n\nexport function devActionErrorResponse(error: unknown): Response {\n return new Response(JSON.stringify(devActionError(error)), {\n status: 500,\n headers: {\n \"Content-Type\": \"application/json\",\n \"Cache-Control\": \"no-store\",\n [DEV_ACTION_ERROR]: \"1\",\n },\n });\n}\n"]}
package/dist/host.d.ts CHANGED
@@ -154,6 +154,12 @@ export interface RscHostOptions {
154
154
  * another language answers without implementing the socket framing.
155
155
  */
156
156
  hostCalls?: (name: string, ...args: unknown[]) => Promise<unknown>;
157
+ /**
158
+ * Called when a request's host is a subdomain of the site that no route
159
+ * begins with, so it is served by path. The dev server passes one that
160
+ * says so; production passes none.
161
+ */
162
+ onUnroutedHost?: (host: string, segment: string) => void;
157
163
  /**
158
164
  * Props for a page, given whatever its url bound.
159
165
  *
package/dist/host.js CHANGED
@@ -16,7 +16,7 @@
16
16
  import { allowFor, matchApiRoute, matchIntercept, layoutChain, matchRoute, retentionKey, sharedDepth, } from "./routing.js";
17
17
  import { PARAM_PLACEHOLDER, pathKey, patternKey } from "./prerender.js";
18
18
  import { apiKey } from "./apiPrerender.js";
19
- import { hostPath, hostSegment, routableHost } from "./hostRouting.js";
19
+ import { bareHost, hostPath, hostSegment, routableHost } from "./hostRouting.js";
20
20
  import { withRevalidation } from "./revalidate.js";
21
21
  import { withoutPayloadKey } from "./js/payloadKey.js";
22
22
  import { cachingFor, createPageCache, finishCacheable, keptBodyOf, noteReadsSoFar, pageMemoryStore } from "./pageCache.js";
@@ -951,6 +951,11 @@ export function createRscHandler(options) {
951
951
  url = new URL(hostPath(host, url.pathname, siteHosts) + url.search, url.origin);
952
952
  hostOf.set(url, segment);
953
953
  }
954
+ else if (segment !== null && host && siteHosts.some((site) => bareHost(host).endsWith("." + site))) {
955
+ // A label under the site's own host, not a foreign one: a foreign
956
+ // host is a proxy's internal name as often as a mistake.
957
+ options.onUnroutedHost?.(host, segment);
958
+ }
954
959
  }
955
960
  const asPayload = pageForPayload(url.pathname);
956
961
  if (asPayload !== null) {