@alxia/create 0.6.0 → 0.7.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.
package/docs/guide.md CHANGED
@@ -335,7 +335,7 @@ instead:
335
335
  ### `src/env.ts`
336
336
 
337
337
  ```ts
338
- import { cloudflareRanges, trustProxy } from "@alxia/core";
338
+ import { parseTrustedProxies } from "@alxia/core";
339
339
  import { defineEnv } from "@alxia/env";
340
340
  import { z } from "zod";
341
341
 
@@ -343,16 +343,6 @@ import { z } from "zod";
343
343
  // production included, is a deployment.
344
344
  const local = ["development", "test"].includes(Bun.env.NODE_ENV ?? "");
345
345
 
346
- // A range or an address trustProxy accepts: it throws on any other.
347
- const isRange = (range: string) => {
348
- try {
349
- trustProxy({ trusted: [range], untrusted: "refuse" });
350
- return true;
351
- } catch {
352
- return false;
353
- }
354
- };
355
-
356
346
  export const env = defineEnv(
357
347
  {
358
348
  PORT: z.coerce.number().default(3000),
@@ -360,17 +350,19 @@ export const env = defineEnv(
360
350
  API_DOCS: z.stringbool().default(Bun.env.NODE_ENV === "development"),
361
351
  TRUSTED_PROXIES: z
362
352
  .string()
363
- .transform((list) =>
364
- list
365
- .split(",")
366
- .map((range) => range.trim())
367
- .filter(Boolean)
368
- .flatMap((range) =>
369
- range === "cloudflare" ? cloudflareRanges : [range],
370
- ),
371
- )
372
- .pipe(z.array(z.string().refine(isRange, "not a CIDR range or address")))
373
- .transform((ranges) => (ranges.length > 0 ? ranges : undefined))
353
+ .transform((list, ctx) => {
354
+ try {
355
+ const ranges = parseTrustedProxies(list);
356
+ return ranges.length > 0 ? ranges : undefined;
357
+ } catch (error) {
358
+ ctx.issues.push({
359
+ code: "custom",
360
+ message: (error as Error).message,
361
+ input: list,
362
+ });
363
+ return z.NEVER;
364
+ }
365
+ })
374
366
  .optional(),
375
367
  },
376
368
  { secret: ["API_KEY"] },
@@ -1081,6 +1073,8 @@ the base only when it is set:
1081
1073
  TRUSTED_PROXIES=10.0.0.0/8,172.16.0.0/12 bun start
1082
1074
  ```
1083
1075
 
1076
+ Entries are validated by `@alxia/core`'s `parseTrustedProxies`: the keyword `cloudflare` is case-insensitive, an empty entry (`a,,b`), an entry with a port and a range that trusts every address (`0.0.0.0/0`) stop the app, and unset or blank declares no proxy.
1077
+
1084
1078
  Behind Cloudflare and a reverse proxy, the keyword `cloudflare` stands for
1085
1079
  Cloudflare's published ranges (`cloudflareRanges` of `@alxia/core`), beside
1086
1080
  your own proxy's network
package/docs/roadmap.md CHANGED
@@ -7,8 +7,7 @@ number on it. Every release, with each change it made, is in
7
7
 
8
8
  ## Now
9
9
 
10
- - **`TRUSTED_PROXIES=cloudflare`.** The `api` and `graphql` templates expand
11
- the keyword `cloudflare` to Cloudflare's ranges, beside CIDR entries.
10
+ - **`TRUSTED_PROXIES=cloudflare`.** The `api` and `graphql` templates read the variable with `@alxia/core`'s `parseTrustedProxies`: the keyword `cloudflare` (any case) expands to Cloudflare's ranges, beside CIDR entries, and an empty entry stops the app.
12
11
 
13
12
  ## Next
14
13
 
@@ -730,11 +730,16 @@ later. A secret's value is never printed: `API_KEY` shows as `***`.
730
730
  PORT=3000 bun dev
731
731
  ```
732
732
 
733
- A malformed `TRUSTED_PROXIES` stops the app the same way, with
734
- `TRUSTED_PROXIES.1: not a CIDR range or address`, the number being the
735
- position of the bad entry, from 0: it is comma-separated CIDR
736
- ranges or addresses (`10.0.0.0/8,172.16.0.0/12`), and unset or empty it
737
- declares no proxy.
733
+ A malformed `TRUSTED_PROXIES` stops the app the same way, through
734
+ `@alxia/core`'s `parseTrustedProxies`, naming the entry. It is
735
+ comma-separated CIDR ranges or addresses (`10.0.0.0/8,172.16.0.0/12`) or the
736
+ keyword `cloudflare` (any case); unset or blank it declares no proxy:
737
+
738
+ - `TRUSTED_PROXIES: parseTrustedProxies: "not-a-range" is not an IP address, a CIDR range or the keyword cloudflare`
739
+ - `TRUSTED_PROXIES: parseTrustedProxies: entry 2 is empty`, for `a,,b` or a trailing comma (the entries count from 1)
740
+ - `TRUSTED_PROXIES: parseTrustedProxies: "10.0.0.1:8080" has a port: name the proxy's address or range alone`
741
+ - `TRUSTED_PROXIES: parseTrustedProxies: "10.0.0.0 /8" contains a space`
742
+ - `TRUSTED_PROXIES: parseTrustedProxies: "0.0.0.0/0" would trust every address`, also `::/0` and `::ffff:0:0/96`
738
743
 
739
744
  A variable the schema gives a default (`PORT`, the `api` project's
740
745
  `API_DOCS`) is fine unset. The `api` project's `API_KEY` defaults to
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alxia/create",
3
- "version": "0.6.0",
3
+ "version": "0.7.0",
4
4
  "description": "Start an alxia app: `bun create @alxia` writes a minimal API, a spec-first one, a GraphQL one, or React Router's official template served by alxia",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -41,11 +41,11 @@
41
41
  ]
42
42
  },
43
43
  "devDependencies": {
44
- "@alxia/core": "^0.20.0",
44
+ "@alxia/core": "^0.21.0",
45
45
  "@alxia/env": "^0.5.0",
46
- "@alxia/graphql": "^0.15.0",
47
- "@alxia/openapi": "^0.6.15",
48
- "@alxia/react-router": "^0.58.0",
46
+ "@alxia/graphql": "^0.15.1",
47
+ "@alxia/openapi": "^0.6.16",
48
+ "@alxia/react-router": "^0.58.1",
49
49
  "@graphql-codegen/cli": "7.4.3",
50
50
  "@graphql-codegen/typescript": "6.1.0",
51
51
  "@graphql-codegen/typescript-resolvers": "6.1.0",
@@ -19,7 +19,7 @@ const load = async (trustedProxies: string) => {
19
19
 
20
20
  test("a malformed TRUSTED_PROXIES stops the app, naming the variable", async () => {
21
21
  await expect(load("10.0.0.0/8, not-a-range")).rejects.toThrow(
22
- "TRUSTED_PROXIES.1: not a CIDR range or address",
22
+ 'TRUSTED_PROXIES: parseTrustedProxies: "not-a-range" is not an IP address, a CIDR range or the keyword cloudflare',
23
23
  );
24
24
  });
25
25
 
@@ -35,8 +35,13 @@ test("the keyword cloudflare expands to Cloudflare's ranges, beside other entrie
35
35
  expect(env.TRUSTED_PROXIES).toEqual([...cloudflareRanges, "172.16.0.0/12"]);
36
36
  });
37
37
 
38
- test("the keyword is lowercase: Cloudflare stops the app, naming the variable", async () => {
39
- await expect(load("Cloudflare")).rejects.toThrow(
40
- "TRUSTED_PROXIES.0: not a CIDR range or address",
38
+ test("the keyword is case-insensitive, and an empty entry stops the app", async () => {
39
+ const { cloudflareRanges } = await import("@alxia/core");
40
+ expect((await load("Cloudflare")).env.TRUSTED_PROXIES).toEqual([
41
+ ...cloudflareRanges,
42
+ ]);
43
+ await expect(load("10.0.0.0/8,,172.16.0.0/12")).rejects.toThrow(
44
+ "TRUSTED_PROXIES: parseTrustedProxies: entry 2 is empty",
41
45
  );
46
+ await expect(load("0.0.0.0/0")).rejects.toThrow("would trust every address");
42
47
  });
@@ -1,4 +1,4 @@
1
- import { cloudflareRanges, trustProxy } from "@alxia/core";
1
+ import { parseTrustedProxies } from "@alxia/core";
2
2
  import { defineEnv } from "@alxia/env";
3
3
  import { z } from "zod";
4
4
 
@@ -6,16 +6,6 @@ import { z } from "zod";
6
6
  // production included, is a deployment.
7
7
  const local = ["development", "test"].includes(Bun.env.NODE_ENV ?? "");
8
8
 
9
- // A range or an address trustProxy accepts: it throws on any other.
10
- const isRange = (range: string) => {
11
- try {
12
- trustProxy({ trusted: [range], untrusted: "refuse" });
13
- return true;
14
- } catch {
15
- return false;
16
- }
17
- };
18
-
19
9
  // Checked once, when this module is first imported: a missing or malformed
20
10
  // variable stops the process, with every issue, before it listens. Secrets
21
11
  // print as `***`. API_KEY defaults to `dev-key` in development and test
@@ -30,17 +20,19 @@ export const env = defineEnv(
30
20
  API_DOCS: z.stringbool().default(Bun.env.NODE_ENV === "development"),
31
21
  TRUSTED_PROXIES: z
32
22
  .string()
33
- .transform((list) =>
34
- list
35
- .split(",")
36
- .map((range) => range.trim())
37
- .filter(Boolean)
38
- .flatMap((range) =>
39
- range === "cloudflare" ? cloudflareRanges : [range],
40
- ),
41
- )
42
- .pipe(z.array(z.string().refine(isRange, "not a CIDR range or address")))
43
- .transform((ranges) => (ranges.length > 0 ? ranges : undefined))
23
+ .transform((list, ctx) => {
24
+ try {
25
+ const ranges = parseTrustedProxies(list);
26
+ return ranges.length > 0 ? ranges : undefined;
27
+ } catch (error) {
28
+ ctx.issues.push({
29
+ code: "custom",
30
+ message: (error as Error).message,
31
+ input: list,
32
+ });
33
+ return z.NEVER;
34
+ }
35
+ })
44
36
  .optional(),
45
37
  },
46
38
  { secret: ["API_KEY"] },
@@ -19,7 +19,7 @@ const load = async (trustedProxies: string) => {
19
19
 
20
20
  test("a malformed TRUSTED_PROXIES stops the app, naming the variable", async () => {
21
21
  await expect(load("10.0.0.0/8, not-a-range")).rejects.toThrow(
22
- "TRUSTED_PROXIES.1: not a CIDR range or address",
22
+ 'TRUSTED_PROXIES: parseTrustedProxies: "not-a-range" is not an IP address, a CIDR range or the keyword cloudflare',
23
23
  );
24
24
  });
25
25
 
@@ -35,8 +35,13 @@ test("the keyword cloudflare expands to Cloudflare's ranges, beside other entrie
35
35
  expect(env.TRUSTED_PROXIES).toEqual([...cloudflareRanges, "172.16.0.0/12"]);
36
36
  });
37
37
 
38
- test("the keyword is lowercase: Cloudflare stops the app, naming the variable", async () => {
39
- await expect(load("Cloudflare")).rejects.toThrow(
40
- "TRUSTED_PROXIES.0: not a CIDR range or address",
38
+ test("the keyword is case-insensitive, and an empty entry stops the app", async () => {
39
+ const { cloudflareRanges } = await import("@alxia/core");
40
+ expect((await load("Cloudflare")).env.TRUSTED_PROXIES).toEqual([
41
+ ...cloudflareRanges,
42
+ ]);
43
+ await expect(load("10.0.0.0/8,,172.16.0.0/12")).rejects.toThrow(
44
+ "TRUSTED_PROXIES: parseTrustedProxies: entry 2 is empty",
41
45
  );
46
+ await expect(load("0.0.0.0/0")).rejects.toThrow("would trust every address");
42
47
  });
@@ -1,17 +1,7 @@
1
- import { cloudflareRanges, trustProxy } from "@alxia/core";
1
+ import { parseTrustedProxies } from "@alxia/core";
2
2
  import { defineEnv } from "@alxia/env";
3
3
  import { z } from "zod";
4
4
 
5
- // A range or an address trustProxy accepts: it throws on any other.
6
- const isRange = (range: string) => {
7
- try {
8
- trustProxy({ trusted: [range], untrusted: "refuse" });
9
- return true;
10
- } catch {
11
- return false;
12
- }
13
- };
14
-
15
5
  // Checked once, when this module is first imported: a malformed variable
16
6
  // stops the process, with every issue, before it listens. TRUSTED_PROXIES is
17
7
  // unset by default: the app then reads no forwarding header. Set it
@@ -20,16 +10,18 @@ export const env = defineEnv({
20
10
  PORT: z.coerce.number().default(3000),
21
11
  TRUSTED_PROXIES: z
22
12
  .string()
23
- .transform((list) =>
24
- list
25
- .split(",")
26
- .map((range) => range.trim())
27
- .filter(Boolean)
28
- .flatMap((range) =>
29
- range === "cloudflare" ? cloudflareRanges : [range],
30
- ),
31
- )
32
- .pipe(z.array(z.string().refine(isRange, "not a CIDR range or address")))
33
- .transform((ranges) => (ranges.length > 0 ? ranges : undefined))
13
+ .transform((list, ctx) => {
14
+ try {
15
+ const ranges = parseTrustedProxies(list);
16
+ return ranges.length > 0 ? ranges : undefined;
17
+ } catch (error) {
18
+ ctx.issues.push({
19
+ code: "custom",
20
+ message: (error as Error).message,
21
+ input: list,
22
+ });
23
+ return z.NEVER;
24
+ }
25
+ })
34
26
  .optional(),
35
27
  });