@tschk/moonshine-framework 0.4.4 → 0.4.49

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.
package/README.md CHANGED
@@ -64,3 +64,11 @@ Metadata applies to native SSR, static output, and SPA document shells without
64
64
  mounting the SPA server component. API responses skip it. This adds no client head
65
65
  manager: subsequent client-side navigation must update document metadata explicitly.
66
66
  Legacy React rendering does not consume this export.
67
+
68
+ With an opt-in request scope, `getResponseHeaders()` from
69
+ `@tschk/moonshine-framework/request` returns the request's mutable response-header
70
+ sink. The pipeline merges it into completed responses, including redirects,
71
+ errors, not-found and HEAD responses. Explicit response headers win for ordinary
72
+ fields; `Set-Cookie` fields append separately. Without a supplied sink the helper
73
+ returns `undefined`. Custom scope wrappers must forward the optional third
74
+ `responseHeaders` argument to `runWithRequest`. The signal-only root is unchanged.
package/package.json CHANGED
@@ -1,13 +1,14 @@
1
1
  {
2
2
  "name": "@tschk/moonshine-framework",
3
- "version": "0.4.4",
3
+ "version": "0.4.49",
4
4
  "license": "ISC",
5
5
  "type": "module",
6
6
  "main": "./src/index.ts",
7
7
  "types": "./src/index.ts",
8
8
  "exports": {
9
9
  ".": "./src/index.ts",
10
- "./request": "./src/request.ts"
10
+ "./request": "./src/request.ts",
11
+ "./data-request": "./src/data-request.ts"
11
12
  },
12
13
  "files": [
13
14
  "src"
@@ -19,7 +20,7 @@
19
20
  "test": "bun test"
20
21
  },
21
22
  "dependencies": {
22
- "@tschk/moonshine": "^0.4.4"
23
+ "@tschk/moonshine": "^0.4.48"
23
24
  },
24
25
  "devDependencies": {
25
26
  "@types/bun": "1.3.14",
package/src/build.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import { normalizeRouteDataRequests } from "./data-request";
1
2
  import { copyFile, mkdir, realpath } from "node:fs/promises";
2
3
  import { dirname, isAbsolute, relative, resolve, sep } from "node:path";
3
4
  import type { DeploymentAdapter } from "./adapter";
@@ -111,6 +112,15 @@ export function validateRuntime(
111
112
  "Request context supports only matching Bun or Node deployment adapters/runtimes",
112
113
  );
113
114
  }
115
+ if (
116
+ normalizeRouteDataRequests(manifest.dataRequests) &&
117
+ (!["bun", "node"].includes(adapter.name) ||
118
+ manifest.routes.some((route) => route.runtime !== adapter.name))
119
+ ) {
120
+ throw new Error(
121
+ "Route data requests support only matching Bun or Node deployment adapters/runtimes",
122
+ );
123
+ }
114
124
  const errors: string[] = [];
115
125
  for (const route of manifest.routes) {
116
126
  if (!adapter.runtimes.includes(route.runtime)) {
package/src/config.ts CHANGED
@@ -1,9 +1,12 @@
1
+ import type { RouteDataRequests } from "./data-request";
1
2
  import type { RenderMode, RuntimeTarget } from "./routes";
2
3
 
3
4
  export type MoonshineConfig = {
4
5
  mode?: RenderMode;
5
6
  runtime?: RuntimeTarget;
6
7
  requestContext?: boolean;
8
+ /** Enable the opt-in JSON route-data request protocol. */
9
+ dataRequests?: RouteDataRequests;
7
10
  };
8
11
 
9
12
  export function defineConfig<T extends MoonshineConfig>(config: T): T {
@@ -0,0 +1,56 @@
1
+ /** Opt-in route-data wire contract; no server or renderer imports. */
2
+ export const ROUTE_DATA_HEADER = "x-moonshine-data";
3
+ export const ROUTE_DATA_VERSION = "1";
4
+
5
+ /** JSON response payload; applications validate the decoded data. */
6
+ export type RouteDataPayload = { data: unknown };
7
+
8
+ /** Expected public origin can be explicit when a TLS proxy fronts the server. */
9
+ export type RouteDataRequests = boolean | { origin: string };
10
+
11
+ /** Validate configured origins without trusting request forwarding headers. */
12
+ export function normalizeRouteDataOrigin(value: string): string {
13
+ if (typeof value !== "string" || value === "")
14
+ throw new TypeError("Invalid route-data origin");
15
+ let url: URL;
16
+ try {
17
+ url = new URL(value);
18
+ } catch {
19
+ throw new TypeError("Invalid route-data origin");
20
+ }
21
+ if (
22
+ (url.protocol !== "http:" && url.protocol !== "https:") ||
23
+ url.username !== "" ||
24
+ url.password !== "" ||
25
+ url.pathname !== "/" ||
26
+ value.includes("?") ||
27
+ value.includes("#")
28
+ ) {
29
+ throw new TypeError(
30
+ "Route-data origin must be an http(s) origin without credentials, path, query or hash",
31
+ );
32
+ }
33
+ return url.origin;
34
+ }
35
+
36
+ /** Normalize JS/JSON configuration before it enables a client-visible endpoint. */
37
+ export function normalizeRouteDataRequests(
38
+ value: unknown,
39
+ ): RouteDataRequests | undefined {
40
+ if (value === undefined || typeof value === "boolean") return value;
41
+ if (
42
+ value === null ||
43
+ typeof value !== "object" ||
44
+ Array.isArray(value) ||
45
+ !Object.hasOwn(value, "origin") ||
46
+ Object.keys(value).some((key) => key !== "origin")
47
+ ) {
48
+ throw new TypeError(
49
+ "dataRequests must be a boolean or an object containing only a string origin",
50
+ );
51
+ }
52
+ const origin = Reflect.get(value, "origin");
53
+ if (typeof origin !== "string")
54
+ throw new TypeError("dataRequests must contain a string origin");
55
+ return { origin: normalizeRouteDataOrigin(origin) };
56
+ }
package/src/manifest.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import type { RouteDataRequests } from "./data-request";
1
2
  import type { RenderMode, RouteDefinition, RuntimeTarget } from "./routes";
2
3
  import type { RendererName } from "./renderer";
3
4
 
@@ -26,6 +27,8 @@ export type MoonshineManifest = {
26
27
  frameworkVersion: string;
27
28
  renderer?: RendererName;
28
29
  requestContext?: boolean;
30
+ /** Enable the opt-in JSON route-data request protocol. */
31
+ dataRequests?: RouteDataRequests;
29
32
  routes: RouteArtifact[];
30
33
  assets: Array<{ path: string; file: string; integrity?: string }>;
31
34
  entries: { server?: string; client?: string };
package/src/renderer.ts CHANGED
@@ -28,4 +28,15 @@ export type Renderer = {
28
28
  };
29
29
 
30
30
  /** Runs the entire request pipeline in an optional server context. */
31
- export type RequestScope = <T>(request: Request, fn: () => T) => T;
31
+ export type RequestScope = <T>(
32
+ request: Request,
33
+ fn: () => T,
34
+ responseHeaders?: Headers,
35
+ ) => T;
36
+
37
+ /** Loader/action payload with document response metadata, without serialization. */
38
+ export type RouteData<T = unknown> = {
39
+ type: "DataWithResponseInit";
40
+ data: T;
41
+ init: ResponseInit;
42
+ };
package/src/request.ts CHANGED
@@ -2,11 +2,19 @@
2
2
  import { AsyncLocalStorage } from "node:async_hooks";
3
3
  import type { RequestScope } from "./renderer";
4
4
 
5
- const requests = new AsyncLocalStorage<Request>();
5
+ const requests = new AsyncLocalStorage<{
6
+ request: Request;
7
+ responseHeaders?: Headers;
8
+ }>();
6
9
 
7
- export const runWithRequest: RequestScope = (request, fn) =>
8
- requests.run(request, fn);
10
+ export const runWithRequest: RequestScope = (request, fn, responseHeaders) =>
11
+ requests.run({ request, responseHeaders }, fn);
9
12
 
10
13
  export function getRequest(): Request | undefined {
11
- return requests.getStore();
14
+ return requests.getStore()?.request;
15
+ }
16
+
17
+ /** Response fields accumulated by the opt-in server pipeline for this request. */
18
+ export function getResponseHeaders(): Headers | undefined {
19
+ return requests.getStore()?.responseHeaders;
12
20
  }