@reckona/mreact-router 0.0.74 → 0.0.75

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.
@@ -3,6 +3,7 @@ import type {
3
3
  AppRouterBuildTarget,
4
4
  AppRouterClientSourceMapMode,
5
5
  } from "./config.js";
6
+ import type { RequestHostPolicy } from "./serve.js";
6
7
 
7
8
  export type CliRequestLogMode = "requests";
8
9
  export type CliBuildTarget = AppRouterBuildTarget | "all";
@@ -10,8 +11,11 @@ export type CliBuildTarget = AppRouterBuildTarget | "all";
10
11
  export interface ParsedCliArguments {
11
12
  clientSourceMaps?: AppRouterClientSourceMapMode | undefined;
12
13
  command: string;
14
+ allowedHosts?: readonly string[] | undefined;
13
15
  from?: string | undefined;
14
16
  help?: boolean | undefined;
17
+ host?: string | undefined;
18
+ hostPolicy?: RequestHostPolicy | undefined;
15
19
  log?: CliRequestLogMode | undefined;
16
20
  out?: string | undefined;
17
21
  routeArg?: string | undefined;
@@ -43,6 +47,39 @@ export function parseCliArguments(argv: readonly string[]): ParsedCliArguments {
43
47
  continue;
44
48
  }
45
49
 
50
+ if (value === "--host") {
51
+ parsed.host = readOptionValue(argv, index, "host");
52
+ index += 1;
53
+ continue;
54
+ }
55
+
56
+ if (value.startsWith("--host=")) {
57
+ parsed.host = value.slice("--host=".length);
58
+ continue;
59
+ }
60
+
61
+ if (value === "--host-policy") {
62
+ parsed.hostPolicy = parseCliHostPolicy(readOptionValue(argv, index, "host-policy"));
63
+ index += 1;
64
+ continue;
65
+ }
66
+
67
+ if (value.startsWith("--host-policy=")) {
68
+ parsed.hostPolicy = parseCliHostPolicy(value.slice("--host-policy=".length));
69
+ continue;
70
+ }
71
+
72
+ if (value === "--allowed-hosts") {
73
+ parsed.allowedHosts = parseCliAllowedHosts(readOptionValue(argv, index, "allowed-hosts"));
74
+ index += 1;
75
+ continue;
76
+ }
77
+
78
+ if (value.startsWith("--allowed-hosts=")) {
79
+ parsed.allowedHosts = parseCliAllowedHosts(value.slice("--allowed-hosts=".length));
80
+ continue;
81
+ }
82
+
46
83
  if (value === "--from") {
47
84
  parsed.from = readOptionValue(argv, index, "from");
48
85
  index += 1;
@@ -155,8 +192,21 @@ export function formatCliHelp(command?: string | undefined): string {
155
192
  "Serve built mreact app router output with the Node adapter.",
156
193
  "",
157
194
  "Options:",
195
+ " --host <host> Bind address. Default: 127.0.0.1. Use 0.0.0.0 inside containers behind explicit port publishing or a reverse proxy.",
196
+ " --host-policy=strict|trusted-proxy",
197
+ " Control Host header trust for request origin reconstruction.",
198
+ " --allowed-hosts <host[,host...]>",
199
+ " Exact Host header allow-list for public deployments.",
158
200
  " --log=requests Print request summaries.",
159
201
  " -h, --help Show this help message.",
202
+ "",
203
+ "Environment:",
204
+ " HOST Bind address when --host is not set.",
205
+ " MREACT_ROUTER_HOST_POLICY",
206
+ " Host header trust policy when --host-policy is not set.",
207
+ " MREACT_ROUTER_ALLOWED_HOSTS",
208
+ " Comma-separated Host header allow-list when --allowed-hosts is not set.",
209
+ " PORT TCP port. Default: 3001.",
160
210
  ].join("\n");
161
211
  }
162
212
 
@@ -222,6 +272,42 @@ export function resolveCliRequestLogMode(
222
272
  return parseCliRequestLogMode(envValue);
223
273
  }
224
274
 
275
+ export function resolveCliHost(
276
+ flagValue: string | undefined,
277
+ env: { HOST?: string | undefined },
278
+ ): string {
279
+ if (flagValue !== undefined && flagValue !== "") {
280
+ return flagValue;
281
+ }
282
+
283
+ const envValue = env.HOST;
284
+ return envValue === undefined || envValue === "" ? "127.0.0.1" : envValue;
285
+ }
286
+
287
+ export function resolveCliHostPolicy(
288
+ flagValue: RequestHostPolicy | undefined,
289
+ env: { MREACT_ROUTER_HOST_POLICY?: string | undefined },
290
+ ): RequestHostPolicy | undefined {
291
+ if (flagValue !== undefined) {
292
+ return flagValue;
293
+ }
294
+
295
+ const envValue = env.MREACT_ROUTER_HOST_POLICY;
296
+ return envValue === undefined || envValue === "" ? undefined : parseCliHostPolicy(envValue);
297
+ }
298
+
299
+ export function resolveCliAllowedHosts(
300
+ flagValue: readonly string[] | undefined,
301
+ env: { MREACT_ROUTER_ALLOWED_HOSTS?: string | undefined },
302
+ ): readonly string[] | undefined {
303
+ if (flagValue !== undefined) {
304
+ return flagValue;
305
+ }
306
+
307
+ const envValue = env.MREACT_ROUTER_ALLOWED_HOSTS;
308
+ return envValue === undefined || envValue === "" ? undefined : parseCliAllowedHosts(envValue);
309
+ }
310
+
225
311
  export function createCliRequestLogger(): AppRouterLogger {
226
312
  return {
227
313
  error(event) {
@@ -245,6 +331,23 @@ function parseCliRequestLogMode(value: string): CliRequestLogMode {
245
331
  throw new Error(`Unsupported log mode ${JSON.stringify(value)}. Expected "requests".`);
246
332
  }
247
333
 
334
+ function parseCliHostPolicy(value: string): RequestHostPolicy {
335
+ if (value === "strict" || value === "trusted-proxy") {
336
+ return value;
337
+ }
338
+
339
+ throw new Error(
340
+ `Unsupported host policy ${JSON.stringify(value)}. Expected "strict" or "trusted-proxy".`,
341
+ );
342
+ }
343
+
344
+ function parseCliAllowedHosts(value: string): readonly string[] {
345
+ return value
346
+ .split(",")
347
+ .map((entry) => entry.trim())
348
+ .filter((entry) => entry !== "");
349
+ }
350
+
248
351
  function parseCliBuildTarget(value: string): CliBuildTarget {
249
352
  if (value === "node" || value === "cloudflare" || value === "aws-lambda" || value === "all") {
250
353
  return value;
package/src/cli.ts CHANGED
@@ -7,6 +7,9 @@ import {
7
7
  createCliRequestLogger,
8
8
  formatCliHelp,
9
9
  parseCliArguments,
10
+ resolveCliAllowedHosts,
11
+ resolveCliHost,
12
+ resolveCliHostPolicy,
10
13
  resolveCliRequestLogMode,
11
14
  } from "./cli-options.js";
12
15
  import { startDevServer } from "./dev-server.js";
@@ -77,6 +80,9 @@ if (parsed !== undefined) {
77
80
  console.log(`mreact app router ready at ${server.url}`);
78
81
  } else if (command === "start") {
79
82
  const server = await startServer({
83
+ allowedHosts: resolveCliAllowedHosts(parsed.allowedHosts, process.env),
84
+ hostPolicy: resolveCliHostPolicy(parsed.hostPolicy, process.env),
85
+ hostname: resolveCliHost(parsed.host, process.env),
80
86
  logger,
81
87
  outDir: resolve(routeArg ?? ".mreact"),
82
88
  port: Number(process.env.PORT ?? 3001),