@reckona/mreact-router 0.0.74 → 0.0.76

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,10 +11,14 @@ 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;
21
+ port?: number | undefined;
17
22
  routeArg?: string | undefined;
18
23
  target?: CliBuildTarget | undefined;
19
24
  }
@@ -43,6 +48,50 @@ export function parseCliArguments(argv: readonly string[]): ParsedCliArguments {
43
48
  continue;
44
49
  }
45
50
 
51
+ if (value === "--port") {
52
+ parsed.port = parseCliPort(readOptionValue(argv, index, "port"));
53
+ index += 1;
54
+ continue;
55
+ }
56
+
57
+ if (value.startsWith("--port=")) {
58
+ parsed.port = parseCliPort(value.slice("--port=".length));
59
+ continue;
60
+ }
61
+
62
+ if (value === "--host") {
63
+ parsed.host = readOptionValue(argv, index, "host");
64
+ index += 1;
65
+ continue;
66
+ }
67
+
68
+ if (value.startsWith("--host=")) {
69
+ parsed.host = value.slice("--host=".length);
70
+ continue;
71
+ }
72
+
73
+ if (value === "--host-policy") {
74
+ parsed.hostPolicy = parseCliHostPolicy(readOptionValue(argv, index, "host-policy"));
75
+ index += 1;
76
+ continue;
77
+ }
78
+
79
+ if (value.startsWith("--host-policy=")) {
80
+ parsed.hostPolicy = parseCliHostPolicy(value.slice("--host-policy=".length));
81
+ continue;
82
+ }
83
+
84
+ if (value === "--allowed-hosts") {
85
+ parsed.allowedHosts = parseCliAllowedHosts(readOptionValue(argv, index, "allowed-hosts"));
86
+ index += 1;
87
+ continue;
88
+ }
89
+
90
+ if (value.startsWith("--allowed-hosts=")) {
91
+ parsed.allowedHosts = parseCliAllowedHosts(value.slice("--allowed-hosts=".length));
92
+ continue;
93
+ }
94
+
46
95
  if (value === "--from") {
47
96
  parsed.from = readOptionValue(argv, index, "from");
48
97
  index += 1;
@@ -134,17 +183,22 @@ export function formatCliHelp(command?: string | undefined): string {
134
183
 
135
184
  if (command === "package") {
136
185
  return [
137
- "Usage: mreact-router package aws-lambda [options]",
186
+ "Usage: mreact-router package <target> [options]",
187
+ "",
188
+ "Package generated build output into a deployable artifact directory.",
138
189
  "",
139
- "Package generated build output into a minimal AWS Lambda asset directory.",
190
+ "Targets:",
191
+ " aws-lambda Minimal AWS Lambda asset directory.",
192
+ " cloudflare-pages Cloudflare Pages advanced mode output with _worker.js.",
140
193
  "",
141
194
  "Options:",
142
- " --from <dir> Build output directory. Default: .mreact",
143
- " --out <dir> Lambda asset output directory. Default: .lambda",
144
- " -h, --help Show this help message.",
195
+ " --from <dir> Build output directory. Default: .mreact",
196
+ " --out <dir> Output directory. Defaults to .lambda for aws-lambda and .mreact/pages for cloudflare-pages.",
197
+ " -h, --help Show this help message.",
145
198
  "",
146
- "Example:",
199
+ "Examples:",
147
200
  " mreact-router package aws-lambda --from .mreact --out .lambda",
201
+ " mreact-router package cloudflare-pages --from .mreact --out .mreact/pages",
148
202
  ].join("\n");
149
203
  }
150
204
 
@@ -155,8 +209,21 @@ export function formatCliHelp(command?: string | undefined): string {
155
209
  "Serve built mreact app router output with the Node adapter.",
156
210
  "",
157
211
  "Options:",
212
+ " --host <host> Bind address. Default: 127.0.0.1. Use 0.0.0.0 inside containers behind explicit port publishing or a reverse proxy.",
213
+ " --host-policy=strict|trusted-proxy",
214
+ " Control Host header trust for request origin reconstruction.",
215
+ " --allowed-hosts <host[,host...]>",
216
+ " Exact Host header allow-list for public deployments.",
158
217
  " --log=requests Print request summaries.",
159
218
  " -h, --help Show this help message.",
219
+ "",
220
+ "Environment:",
221
+ " HOST Bind address when --host is not set.",
222
+ " MREACT_ROUTER_HOST_POLICY",
223
+ " Host header trust policy when --host-policy is not set.",
224
+ " MREACT_ROUTER_ALLOWED_HOSTS",
225
+ " Comma-separated Host header allow-list when --allowed-hosts is not set.",
226
+ " PORT TCP port. Default: 3001.",
160
227
  ].join("\n");
161
228
  }
162
229
 
@@ -167,8 +234,12 @@ export function formatCliHelp(command?: string | undefined): string {
167
234
  "Start the mreact app router development server.",
168
235
  "",
169
236
  "Options:",
237
+ " --port <port> TCP port. Overrides PORT and vite.config.ts server.port.",
170
238
  " --log=requests Print request summaries.",
171
239
  " -h, --help Show this help message.",
240
+ "",
241
+ "Environment:",
242
+ " PORT TCP port when --port is not set.",
172
243
  ].join("\n");
173
244
  }
174
245
 
@@ -182,6 +253,8 @@ export function formatCliHelp(command?: string | undefined): string {
182
253
  " start [outDir] Serve built Node output.",
183
254
  " package aws-lambda --from .mreact --out .lambda",
184
255
  " Package a minimal AWS Lambda asset directory.",
256
+ " package cloudflare-pages --from .mreact --out .mreact/pages",
257
+ " Package Cloudflare Pages advanced mode output.",
185
258
  " help [command] Show help.",
186
259
  "",
187
260
  "Options:",
@@ -222,6 +295,55 @@ export function resolveCliRequestLogMode(
222
295
  return parseCliRequestLogMode(envValue);
223
296
  }
224
297
 
298
+ export function resolveCliHost(
299
+ flagValue: string | undefined,
300
+ env: { HOST?: string | undefined },
301
+ ): string {
302
+ if (flagValue !== undefined && flagValue !== "") {
303
+ return flagValue;
304
+ }
305
+
306
+ const envValue = env.HOST;
307
+ return envValue === undefined || envValue === "" ? "127.0.0.1" : envValue;
308
+ }
309
+
310
+ export function resolveCliDevPort(
311
+ flagValue: number | undefined,
312
+ env: { PORT?: string | undefined },
313
+ viteConfigPort: number | undefined,
314
+ ): number | undefined {
315
+ if (flagValue !== undefined) {
316
+ return flagValue;
317
+ }
318
+
319
+ const envValue = env.PORT;
320
+ return envValue === undefined || envValue === "" ? viteConfigPort : parseCliPort(envValue);
321
+ }
322
+
323
+ export function resolveCliHostPolicy(
324
+ flagValue: RequestHostPolicy | undefined,
325
+ env: { MREACT_ROUTER_HOST_POLICY?: string | undefined },
326
+ ): RequestHostPolicy | undefined {
327
+ if (flagValue !== undefined) {
328
+ return flagValue;
329
+ }
330
+
331
+ const envValue = env.MREACT_ROUTER_HOST_POLICY;
332
+ return envValue === undefined || envValue === "" ? undefined : parseCliHostPolicy(envValue);
333
+ }
334
+
335
+ export function resolveCliAllowedHosts(
336
+ flagValue: readonly string[] | undefined,
337
+ env: { MREACT_ROUTER_ALLOWED_HOSTS?: string | undefined },
338
+ ): readonly string[] | undefined {
339
+ if (flagValue !== undefined) {
340
+ return flagValue;
341
+ }
342
+
343
+ const envValue = env.MREACT_ROUTER_ALLOWED_HOSTS;
344
+ return envValue === undefined || envValue === "" ? undefined : parseCliAllowedHosts(envValue);
345
+ }
346
+
225
347
  export function createCliRequestLogger(): AppRouterLogger {
226
348
  return {
227
349
  error(event) {
@@ -245,6 +367,33 @@ function parseCliRequestLogMode(value: string): CliRequestLogMode {
245
367
  throw new Error(`Unsupported log mode ${JSON.stringify(value)}. Expected "requests".`);
246
368
  }
247
369
 
370
+ function parseCliPort(value: string): number {
371
+ const port = Number(value);
372
+
373
+ if (Number.isInteger(port) && port >= 0 && port <= 65535) {
374
+ return port;
375
+ }
376
+
377
+ throw new Error(`Unsupported port ${JSON.stringify(value)}. Expected an integer from 0 to 65535.`);
378
+ }
379
+
380
+ function parseCliHostPolicy(value: string): RequestHostPolicy {
381
+ if (value === "strict" || value === "trusted-proxy") {
382
+ return value;
383
+ }
384
+
385
+ throw new Error(
386
+ `Unsupported host policy ${JSON.stringify(value)}. Expected "strict" or "trusted-proxy".`,
387
+ );
388
+ }
389
+
390
+ function parseCliAllowedHosts(value: string): readonly string[] {
391
+ return value
392
+ .split(",")
393
+ .map((entry) => entry.trim())
394
+ .filter((entry) => entry !== "");
395
+ }
396
+
248
397
  function parseCliBuildTarget(value: string): CliBuildTarget {
249
398
  if (value === "node" || value === "cloudflare" || value === "aws-lambda" || value === "all") {
250
399
  return value;
package/src/cli.ts CHANGED
@@ -1,12 +1,16 @@
1
1
  #!/usr/bin/env node
2
2
 
3
3
  import { resolve } from "node:path";
4
- import { buildApp, packageAwsLambdaArtifact } from "./build.js";
4
+ import { buildApp, packageAwsLambdaArtifact, packageCloudflarePagesArtifact } from "./build.js";
5
5
  import {
6
6
  buildTargetsFromCliTarget,
7
7
  createCliRequestLogger,
8
8
  formatCliHelp,
9
9
  parseCliArguments,
10
+ resolveCliAllowedHosts,
11
+ resolveCliDevPort,
12
+ resolveCliHost,
13
+ resolveCliHostPolicy,
10
14
  resolveCliRequestLogMode,
11
15
  } from "./cli-options.js";
12
16
  import { startDevServer } from "./dev-server.js";
@@ -51,18 +55,27 @@ if (parsed !== undefined) {
51
55
  });
52
56
  console.log(`Built ${result.routes.length} routes.`);
53
57
  } else if (command === "package") {
54
- if (routeArg !== "aws-lambda") {
58
+ if (routeArg === "aws-lambda") {
59
+ const manifest = await packageAwsLambdaArtifact({
60
+ fromDir: resolve(parsed.from ?? ".mreact"),
61
+ outDir: resolve(parsed.out ?? ".lambda"),
62
+ });
63
+ console.log(
64
+ `Packaged AWS Lambda artifact with ${manifest.files.length} files (${manifest.totalBytes} bytes).`,
65
+ );
66
+ } else if (routeArg === "cloudflare-pages") {
67
+ const manifest = await packageCloudflarePagesArtifact({
68
+ fromDir: resolve(parsed.from ?? ".mreact"),
69
+ outDir: resolve(parsed.out ?? ".mreact/pages"),
70
+ });
71
+ console.log(
72
+ `Packaged Cloudflare Pages artifact with ${manifest.files.length} files (${manifest.totalBytes} bytes).`,
73
+ );
74
+ } else {
55
75
  throw new Error(
56
- `Unsupported package target ${JSON.stringify(routeArg)}. Expected "aws-lambda".`,
76
+ `Unsupported package target ${JSON.stringify(routeArg)}. Expected "aws-lambda" or "cloudflare-pages".`,
57
77
  );
58
78
  }
59
- const manifest = await packageAwsLambdaArtifact({
60
- fromDir: resolve(parsed.from ?? ".mreact"),
61
- outDir: resolve(parsed.out ?? ".lambda"),
62
- });
63
- console.log(
64
- `Packaged AWS Lambda artifact with ${manifest.files.length} files (${manifest.totalBytes} bytes).`,
65
- );
66
79
  } else if (command === "dev") {
67
80
  const loaded =
68
81
  routeArg === undefined
@@ -71,12 +84,15 @@ if (parsed !== undefined) {
71
84
  const server = await startDevServer({
72
85
  ...loaded.project,
73
86
  logger,
74
- port: process.env.PORT === undefined ? loaded.serverPort : Number(process.env.PORT),
87
+ port: resolveCliDevPort(parsed.port, process.env, loaded.serverPort),
75
88
  viteConfig: loaded.viteConfig,
76
89
  });
77
90
  console.log(`mreact app router ready at ${server.url}`);
78
91
  } else if (command === "start") {
79
92
  const server = await startServer({
93
+ allowedHosts: resolveCliAllowedHosts(parsed.allowedHosts, process.env),
94
+ hostPolicy: resolveCliHostPolicy(parsed.hostPolicy, process.env),
95
+ hostname: resolveCliHost(parsed.host, process.env),
80
96
  logger,
81
97
  outDir: resolve(routeArg ?? ".mreact"),
82
98
  port: Number(process.env.PORT ?? 3001),
package/src/index.ts CHANGED
@@ -1,4 +1,4 @@
1
- export { buildApp, packageAwsLambdaArtifact } from "./build.js";
1
+ export { buildApp, packageAwsLambdaArtifact, packageCloudflarePagesArtifact } from "./build.js";
2
2
  export { assetHref, assetPreloadLinks } from "./assets.js";
3
3
  export { cacheControl, createMemoryRouteCache, revalidatePath } from "./cache.js";
4
4
  export { deleteCookie, parseCookieHeader, serializeCookie, setCookie } from "./cookies.js";
@@ -74,7 +74,9 @@ export type {
74
74
  BuildAppOptions,
75
75
  BuildAppResult,
76
76
  BuiltImportPolicyArtifact,
77
+ CloudflarePagesArtifactManifest,
77
78
  PackageAwsLambdaArtifactOptions,
79
+ PackageCloudflarePagesArtifactOptions,
78
80
  } from "./build.js";
79
81
  export type {
80
82
  InferLoaderData,