@akanjs/devkit 3.3.0-rc.70 → 3.3.0-rc.72

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.
@@ -1,6 +1,7 @@
1
1
  import { existsSync, realpathSync } from "node:fs";
2
2
  import path from "node:path";
3
3
  import { Logger, toError } from "akanjs/common";
4
+ import { EnvProfile } from "akanjs/server/lifecycle/envProfile";
4
5
  import { ServerEnvEvaluationError } from "./serverEnvEvaluationError";
5
6
 
6
7
  export interface ServerEnvProfileProps {
@@ -17,6 +18,8 @@ export interface ServerEnvSource {
17
18
  export interface LibEnvSource extends ServerEnvSource {
18
19
  profileFile: boolean;
19
20
  stripped: string[];
21
+ /** The defaults `contents` exports when the lib has no profile file: the lib's own with each secret left out. */
22
+ value?: unknown;
20
23
  }
21
24
 
22
25
  type EnvLoad = (envFile: string) => Promise<ServerEnvSource>;
@@ -52,7 +55,7 @@ type EvaluatedMessage = { load: string } | { env: string; lossy: string[]; reads
52
55
  //* A server env profile is bundled in place of the environment's own file, for a server that runs where its bundle
53
56
  //* can be read: the computers a desktop app is installed on. So it may hold no secret.
54
57
  export class ServerEnvProfile {
55
- static readonly namePattern = /^[a-z][a-zA-Z0-9-]*$/;
58
+ static readonly namePattern = EnvProfile.namePattern;
56
59
  static readonly envFilePattern = /[\\/]env[\\/]env\.server\.[A-Za-z0-9-]+\.ts$/;
57
60
  static readonly envModulePattern = /[\\/]env[\\/]env\.server(?:\.[A-Za-z0-9-]+)?\.ts$/;
58
61
  static readonly evaluationTimeoutMs = 60_000;
@@ -407,12 +410,13 @@ export class ServerEnvProfile {
407
410
  throw new Error(
408
411
  `${file} holds ${evaluated.lossy.join(", ")}, which a "${profile}" build cannot carry: it bundles a lib env's defaults as JSON, which keeps no function, Map, Set, Date or class instance. Give the lib ${path.basename(profileFile)}.`,
409
412
  );
410
- const { value, stripped } = ServerEnvProfile.strip(evaluated.value);
413
+ const { value = {}, stripped } = ServerEnvProfile.strip(evaluated.value);
411
414
  return {
412
415
  loader: "js",
413
- contents: `export const env = ${JSON.stringify(value ?? {})};\n`,
416
+ contents: `export const env = ${JSON.stringify(value)};\n`,
414
417
  profileFile: false,
415
418
  stripped,
419
+ value,
416
420
  };
417
421
  }
418
422
 
@@ -32,11 +32,7 @@ interface Built {
32
32
  text: string;
33
33
  }
34
34
 
35
- const build = async (
36
- root: string,
37
- secrets?: "read" | "inline",
38
- { mainExtra = "", overSwitch = false } = {},
39
- ): Promise<Built> => {
35
+ const build = async (root: string, secrets?: "read", { mainExtra = "", overSwitch = false } = {}): Promise<Built> => {
40
36
  const { appDir, envDir } = await writeSecretEnvFixture(root, { mainExtra });
41
37
  if (overSwitch)
42
38
  await writeText(path.join(envDir, "env.server.ts"), await Bun.file(path.join(envDir, "env.server.main.ts")).text());
@@ -110,29 +106,30 @@ describe("createServerEnvPlugin for an image", () => {
110
106
  });
111
107
  });
112
108
 
113
- test("the developer's own variable still outranks the one the build reads", async () => {
109
+ test("the developer's own variable still outranks the one the build reads, and stands in for it at boot", async () => {
114
110
  const built = await build(await makeTempRoot(), "read");
115
111
  const { list, values } = await declare(built);
116
- const booted = boot(built.file, {
117
- AKAN_ENV_NAMES: list,
118
- ...values,
119
- DATABASE_URL: "postgres://own.example.test/app",
120
- });
112
+ expect(list).toContain(",POSTGRES_URL|DATABASE_URL");
113
+ const own = { DATABASE_URL: "postgres://own.example.test/app" };
114
+ const booted = boot(built.file, { AKAN_ENV_NAMES: list, ...values, ...own });
121
115
  expect(booted.env).toMatchObject({ database: { postgres: { url: "postgres://own.example.test/app" } } });
116
+ const { POSTGRES_URL: _url, ...set } = values;
117
+ const instead = boot(built.file, { AKAN_ENV_NAMES: list, ...set, ...own });
118
+ expect({ exitCode: instead.exitCode, warnings: instead.warnings }).toEqual({ exitCode: 0, warnings: [] });
119
+ expect(instead.env).toMatchObject({ database: { postgres: { url: "postgres://own.example.test/app" } } });
122
120
  });
123
121
 
124
- test("started without its variables, it names each one in a single warning and runs without them", async () => {
122
+ test("started without its variables, it exits non-zero, naming each one and no value", async () => {
125
123
  const built = await build(await makeTempRoot(), "read");
126
124
  const { list, values } = await declare(built);
127
125
  const { MAILER__AUTH__PASS: _pass, DISCORD__TOKENS: _tokens, ...set } = values;
128
126
  const booted = boot(built.file, { AKAN_ENV_NAMES: list, ...set });
129
- expect(booted.exitCode).toBe(0);
130
- expect(booted.warnings).toEqual([
131
- "2 secret env values this server declares are not set: MAILER__AUTH__PASS, DISCORD__TOKENS. The framework does not stop for them yet, though whatever reads them may, and akanjs 3.5.0 will refuse to boot. Set them in the deployment's environment; `akan secret-env <app> --env main` lists every name.",
132
- ]);
133
- expect(booted.env).toMatchObject({ mailer: { address: "noreply@example.test", auth: { user: "mailer" } } });
134
- expect((booted.env?.mailer as { auth: object } | undefined)?.auth).not.toHaveProperty("pass");
135
- expect(booted.env?.discord).not.toHaveProperty("tokens");
127
+ expect(booted.exitCode).not.toBe(0);
128
+ expect(booted.env).toBeNull();
129
+ expect(booted.warnings.join("\n")).toContain(
130
+ "2 secret env values this server reads from its process env are not set, so it does not start: MAILER__AUTH__PASS, DISCORD__TOKENS. Its build left the values out of the image. Set them in the deployment's environment; `akan secret-env <app> --env main` lists every name and writes the values from the env file.",
131
+ );
132
+ expect(holdsSentinel(booted.warnings.join("\n"))).toBe(false);
136
133
  });
137
134
 
138
135
  test("after a key is renamed in the env file, a deployment still setting the old variable is told the new name", async () => {
@@ -146,32 +143,11 @@ describe("createServerEnvPlugin for an image", () => {
146
143
  });
147
144
  const { list } = await declare(renamed);
148
145
  const booted = boot(renamed.file, { AKAN_ENV_NAMES: list, ...old.values });
149
- expect(booted.warnings).toEqual([
150
- "1 secret env value this server declares is not set: LLM__ACCESS_KEY. The framework does not stop for it yet, though whatever reads it may, and akanjs 3.5.0 will refuse to boot. Set it in the deployment's environment; `akan secret-env <app> --env main` lists every name.",
151
- ]);
152
- expect(booted.env?.llm).toEqual({});
153
- });
154
-
155
- test("an --inline-secrets build keeps the values as what an unset variable falls back to, and a set one wins", async () => {
156
- const built = await build(await makeTempRoot(), "inline");
157
- expect(built.text).toContain(secretEnvSentinels.jwt);
158
- const { list } = await declare(built);
159
- const fallback = boot(built.file, { AKAN_ENV_NAMES: list, AKAN_ENV_FALLBACK: "image" });
160
- expect(fallback.env).toEqual((await evaluated(built)) as { [key: string]: unknown });
161
- expect(fallback.warnings).toHaveLength(1);
162
- expect(fallback.warnings[0]).toStartWith(
163
- "10 secret env values this server declares are not set, so it uses the values built into this image (akan build --inline-secrets): JWT_SECRET, ",
146
+ expect(booted.exitCode).not.toBe(0);
147
+ expect(booted.warnings.join("\n")).toContain(
148
+ "1 secret env value this server reads from its process env is not set, so it does not start: LLM__ACCESS_KEY.",
164
149
  );
165
- const overridden = boot(built.file, {
166
- AKAN_ENV_NAMES: list,
167
- AKAN_ENV_FALLBACK: "image",
168
- JWT_SECRET: "set-by-the-deployment",
169
- DISCORD__TOKENS: '["set-by-the-deployment"]',
170
- });
171
- expect(overridden.env).toMatchObject({
172
- security: { jwtSecret: "set-by-the-deployment", sso: { google: { clientSecret: secretEnvSentinels.google } } },
173
- discord: { tokens: ["set-by-the-deployment"], webhook: secretEnvSentinels.webhook },
174
- });
150
+ expect(holdsSentinel(booted.warnings.join("\n"))).toBe(false);
175
151
  });
176
152
 
177
153
  test("the env file a pipeline copies over the switch is read the same way", async () => {