sproutboat 0.52.0 → 0.53.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/CHANGELOG.md CHANGED
@@ -10,6 +10,36 @@ maintained going forward by the `release` skill.
10
10
 
11
11
  ## Unreleased
12
12
 
13
+ ## [0.53.0] - 2026-11-09
14
+
15
+ Built on `@sproutboat/runtime` 0.43.0, `@sproutboat/wire` 0.22.3 and `@sproutboat/toolchain` 0.24.2. The runtime change is the opt-in `SB_LOG=json` log below; without it nothing an app can observe changes, so there is no new compatibility date. Every app is recompiled on its next build, since the runtime prelude changed.
16
+
17
+ ### Added
18
+
19
+ - **`sproutboat tail --file <log|->` follows a standalone binary's request and error records (#268).** Run the binary with `SB_LOG=json` (runtime 0.43.0 writes them) and `tail --file` prints the last 10 matching records, then new ones as they are written, in pretty lines or one JSON record per line (`--format json`). `--status`, `--method` and `--search` filter; `--lines` sets the snapshot size and `--no-follow` prints it and exits; `--file -` reads a pipe such as `journalctl -f -o cat`. An error follows its request's filters, and errors from background work always show. A missing file is waited for, a rotated or truncated file is reread from its start, a line over 64 KiB is skipped and counted, control characters are replaced before printing, and only known fields are written, so output stays bounded and parseable. It needs no login or network and ends with exit 0 on Ctrl-C. The existing control plane `tail` (live stream, `--format json`, filters, `--recent`, `--sprout`) is unchanged. See "Logs" in `docs/standalone.md`.
20
+ - **`sproutboat run` launches a standalone binary with its settings supplied at launch (#275).** `sproutboat run [project-dir] --env-file prod.env --port 80 --data-dir /var/lib/app` starts `dist/<name>` (or `--binary`) with an environment built from three layers, highest first: `--port` and `--data-dir`, the `--env-file` files (repeat the flag; a later file overrides an earlier one), then the inherited environment. Like `wrangler deploy`, it fails before the binary starts when a declared secret (the config's `secrets` plus `--require`) is unset or empty; `--check` validates and prints a summary without starting it. The summary names each secret and the layer it came from, never a value, nothing is written to disk, and the binary and its artifact metadata are untouched, so the identical binary moves between development and production. SIGINT, SIGTERM and SIGHUP are passed to the binary and its exit status is returned. `examples/vars-secrets` walks through it.
21
+
22
+ ### Changed
23
+
24
+ - **`init` scaffolds the Workers-style handler.** New projects get compatibility date `2026-09-28` and `fetch(request, env, ctx)`, so `env` and `ctx` are available without a date bump. Existing `sproutboat.jsonc` files are never rewritten, and projects on older dates keep their behavior.
25
+ - **`sproutboat dev` takes local secrets the way Wrangler does (#275).** `.dev.vars` or the `.env` files, never both (a warning names the ignored ones); `--env <name>` selects `.dev.vars.<name>` and the `.env.<name>` files; the `.env` files layer `.env.<name>.local`, `.env.local`, `.env.<name>`, `.env`; `CLOUDFLARE_LOAD_DEV_VARS_FROM_DOT_ENV=false` turns them off; `--env-file <file>` (repeatable, later overrides earlier) replaces the default set. The process environment is not passed to the app, except for declared secret names when `CLOUDFLARE_INCLUDE_PROCESS_ENV=true` and no `.dev.vars` is in use, and files win over it. A declared secret with no value is a warning and `dev` still starts. Files use dotenv syntax; a line that is not an assignment is skipped with a `file:line` warning that never repeats the line.
26
+ - **`.dev.vars` is now parsed with dotenv syntax.** It used to be a simpler format. The differences that can matter: an unquoted value ends at a `#` (it used to keep it), quotes may be single, double or backtick, double quotes expand `\n`, and the last of a repeated name wins. A line that is not an assignment is skipped with a warning instead of silently.
27
+ - **`docs/standalone.md` no longer lists `<data>/secrets.json`**: a standalone binary has only ever read secrets from the environment.
28
+
29
+ ### Fixed
30
+
31
+ - **The native conformance harnesses no longer hang on a stalled response (#276).** The broker and standalone kitchen-sink harnesses and the small-example smoke runner give every HTTP and raw-socket request a deadline (`SB_HARNESS_REQUEST_TIMEOUT_MS`, default 30 s) that covers the whole body, and bound readiness (`SB_HARNESS_READY_TIMEOUT_MS`) and compilation (`SB_HARNESS_BUILD_TIMEOUT_MS`) separately, so a slow build cannot look like a hung handler. A timeout, or a child that exits during a request, reports the step, backend, runtime and toolchain versions and the child's recent stderr with secret values removed. Only the run's own children are stopped and its temp dir removed, on failure and on SIGINT or SIGTERM. Fault-injection tests cover a server that never completes a body and a child that exits mid-request. This is repository tooling; the shipped CLI does not change.
32
+
33
+ ## [0.52.1] - 2026-11-08
34
+
35
+ Built on `@sproutboat/toolchain` 0.24.2, `@sproutboat/runtime` 0.42.2 and `@sproutboat/wire` 0.22.2. The compiler output changes (four Porffor alpha 22 miscompile fixes in the toolchain), so every app is recompiled on its next build. Nothing else an app can observe changes, so there is no new compatibility date.
36
+
37
+ ### Fixed
38
+
39
+ - **Four Porffor alpha 22 miscompiles are fixed** (toolchain 0.24.2), found with postgres.js and drizzle-orm. A method call on a receiver of unknown type evaluated its arguments in C's unspecified order, so `buf.toString("utf8", i, i += n)` returned `""`. A class declaration that is assigned to, such as TypeScript's `(SQL || (SQL = {}))` namespace merge, never stored the class in its binding and died with "Cannot get property of null". The right operand of `||`, `&&` and `??` retyped a local at compile time as if it always ran, so the same pattern inside a function still failed. `new Date(s)` returned NaN for `+HH` and `+HHmm` offsets such as Postgres' `2026-11-08 14:48:19.097+00`; they now parse, together with `+HH:mm`.
40
+ - **A warm standalone build no longer recompiles every C unit.** The alpha 22 build cleared the units directory, object cache included, before each render, so every build paid the full C compile (about 13 s for hello on an M1). It now clears only the previous render and keeps `obj/`; a unit whose text and flags are unchanged is not compiled again.
41
+ - **`build --standalone --target host` uses the compile cache.** An unchanged host build returns in about 0.2 s (was about 39 s for hello). The key includes the host compiler's name and `--version` banner. Dev builds still bypass it.
42
+
13
43
  ## [0.52.0] - 2026-11-06
14
44
 
15
45
  Built on `@sproutboat/toolchain` 0.24.1, `@sproutboat/runtime` 0.42.2 and `@sproutboat/wire` 0.22.2. The compiler changes, so every app is recompiled on its next build. Nothing an app can observe changes apart from the two fixes below, so there is no new compatibility date.
package/README.md CHANGED
@@ -197,8 +197,12 @@ chunking, resume state, cleanup, and the remaining download limitation.
197
197
  `sproutboat dev` runs against a real local broker, so bindings behave like
198
198
  production without a deploy — except `secrets`, which live only in the control
199
199
  plane. Put values in a `.dev.vars` file next to `sproutboat.jsonc`
200
- (`API_KEY=whatever`, one per line, gitignored) and `env.API_KEY` resolves under
201
- `dev` only.
200
+ (`API_KEY=whatever`, one per line, gitignored), or use the `.env` files or
201
+ `sproutboat dev --env-file <file>`, as in Wrangler (`.dev.vars` or `.env`, never
202
+ both), and `env.API_KEY` resolves under `dev` only. A declared secret with no
203
+ value is a warning. The rules, the dotenv syntax, and `sproutboat run` for
204
+ launching a standalone binary (which fails on a missing declared secret) are in
205
+ [docs/standalone.md](docs/standalone.md#launching-with-sproutboat-run).
202
206
 
203
207
  ## Requirements
204
208
 
package/SURFACE.md CHANGED
@@ -3,7 +3,7 @@
3
3
  > Generated by `src/surface.test.ts` from `src/surface.ts` + the pinned
4
4
  > toolchain constants. Do not edit by hand — run `UPDATE_SURFACE=1 bun test`.
5
5
 
6
- **Package:** `sproutboat` 0.52.0
6
+ **Package:** `sproutboat` 0.53.0
7
7
 
8
8
  ## Commands
9
9
 
@@ -12,13 +12,14 @@
12
12
  | `init` | `[name]` | Scaffold sproutboat.jsonc + src/index.js in ./<name>. |
13
13
  | `check` | `[project-dir]` | Validate the config and entry point without building. |
14
14
  | `types` | `[project-dir]` | Write sproutboat-env.d.ts from the config, so an editor knows what `env` holds. `dev` and `deploy` refresh it when the config is newer. |
15
- | `dev` | `[project-dir] [--port <n>] [--no-watch]` | Run the project on this machine against a real broker, rebuilding on save. |
15
+ | `dev` | `[project-dir] [--port <n>] [--no-watch] [--env <name>] [--env-file <file>]...` | Run the project on this machine against a real broker, rebuilding on save. Secrets come from .dev.vars or the .env files (never both), as in Wrangler; a declared secret with no value is a warning. |
16
+ | `run` | `[project-dir] [--binary <file>] [--env-file <file>]... [--port <n>] [--data-dir <dir>] [--require <NAME,...>] [--check]` | Launch a standalone binary with its port, data directory and declared secrets supplied at launch, never baked in. Precedence: --port/--data-dir, then the --env-file files (later overrides earlier), then the environment. A missing declared secret fails the launch, like wrangler deploy; --check validates without starting. |
16
17
  | `build` | `[project-dir] [--target host|linux-x64|linux-arm64] [--standalone] [--deploy-files] [--with-restore] [--emit-only]` | Cross-compile the native-fetch sprout (Porffor + Zig). `--target host` builds for this machine instead, to run locally, not deployable. `--standalone` emits one executable carrying its own bindings, with SQLite compiled in (~2 MB) and no broker process; add `--target linux-arm64` for an aarch64 box (the default is `linux-x64`) and `--deploy-files` to write a Dockerfile, a systemd socket and service, a fly.toml and a push script beside it. `--with-restore` adds an offline `APP ltx-restore --generation DIR --output NEW_DB [--txid N]` startup command to a standalone executable; it runs before app initialization. `--emit-only` stops after writing `.sproutboat/porffor-units/<shape>/sprout.generated.js`, the module Porffor would compile, and prints its path: every check still runs, nothing is compiled. |
17
18
  | `toolchain` | `doctor [--json]` | Report the selected compiler inputs, cache locations, overrides, and host prerequisites without downloading or compiling anything. |
18
19
  | `deploy` | `[project-dir] [--dry-run] [--artifact <dir>] [--no-wait] [--no-provision]` | Build (unless --artifact), auto-provision id-less storage bindings and pin their ids into sproutboat.jsonc, print the report, upload, wait until the URL serves. The control plane skips an upload that matches the live artifact byte-for-byte. --dry-run stops before upload; --no-wait skips the health check; --no-provision leaves id-less bindings as ephemeral deploy-scoped stores. |
19
20
  | `versions` | `<list | view <version-id>> [project-dir]` | List the project's deployed versions, or show one version's artifact and bindings. |
20
21
  | `rollback` | `<version-id> [project-dir]` | Re-activate a previous version. |
21
- | `tail` | `[project-dir] [--format pretty|json] [--status ok|error|<code>|5xx] [--method GET,POST] [--sampling-rate <0..1>] [--search <text>] [--outcome <name>] [--recent] [--sprout]` | Stream live request events, console output and exceptions (like wrangler tail); --recent prints the last 100 request records, --sprout the running sprout + broker stdout/stderr. |
22
+ | `tail` | `[project-dir] [--format pretty|json] [--status ok|error|<code>|5xx] [--method GET,POST] [--sampling-rate <0..1>] [--search <text>] [--outcome <name>] [--recent] [--sprout] [--file <log|-> [--lines <n>] [--no-follow]]` | Stream live request events, console output and exceptions (like wrangler tail); --recent prints the last 100 request records, --sprout the running sprout + broker stdout/stderr. --file follows the JSON records a standalone binary writes with SB_LOG=json (no login needed). |
22
23
  | `kv` | `<list | create <name> | info <name> | rename <name> <new> | delete <name>> | <key | bulk | export> ...` | KV namespaces. `create` prints the id to bind from sproutboat.jsonc; key and bulk operations manage contents, and export writes a restorable JSON dump. |
23
24
  | `d1` | `<list | create <name> | info <name> | rename <name> <new> | delete <name>>` | D1 databases. `create` prints the id to bind from sproutboat.jsonc. |
24
25
  | `r2` | `<list | create <name> | info <name> | rename <name> <new> | delete <name>>` | R2 buckets. `create` prints the id to bind from sproutboat.jsonc. |
@@ -32,7 +33,7 @@
32
33
  | `complete` | `[bash | zsh | fish]` | Print a shell completion script for the top-level commands (defaults to $SHELL). |
33
34
 
34
35
  ```
35
- usage: sproutboat <init [name] | check [project-dir] | types [project-dir] | dev [project-dir] [--port <n>] [--no-watch] | build [project-dir] [--target host|linux-x64|linux-arm64] [--standalone] [--deploy-files] [--with-restore] [--emit-only] | toolchain doctor [--json] | deploy [project-dir] [--dry-run] [--artifact <dir>] [--no-wait] [--no-provision] | versions <list | view <version-id>> [project-dir] | rollback <version-id> [project-dir] | tail [project-dir] [--format pretty|json] [--status ok|error|<code>|5xx] [--method GET,POST] [--sampling-rate <0..1>] [--search <text>] [--outcome <name>] [--recent] [--sprout] | kv <list | create <name> | info <name> | rename <name> <new> | delete <name>> | <key | bulk | export> ... | d1 <list | create <name> | info <name> | rename <name> <new> | delete <name>> | r2 <list | create <name> | info <name> | rename <name> <new> | delete <name>> | queues <list | create <name> | info <name> | rename <name> <new> | delete <name>> | domains <list | add <host> | verify <host> | delete <host>> [project-dir] | secrets <list | put <NAME> [--value <value>] | delete <NAME>> [project-dir] | delete [project-dir] [--name <project>] --yes | login [--api-url <url>] [--token <token>] | logout [--api-url <url>] | whoami | complete [bash | zsh | fish]>
36
+ usage: sproutboat <init [name] | check [project-dir] | types [project-dir] | dev [project-dir] [--port <n>] [--no-watch] [--env <name>] [--env-file <file>]... | run [project-dir] [--binary <file>] [--env-file <file>]... [--port <n>] [--data-dir <dir>] [--require <NAME,...>] [--check] | build [project-dir] [--target host|linux-x64|linux-arm64] [--standalone] [--deploy-files] [--with-restore] [--emit-only] | toolchain doctor [--json] | deploy [project-dir] [--dry-run] [--artifact <dir>] [--no-wait] [--no-provision] | versions <list | view <version-id>> [project-dir] | rollback <version-id> [project-dir] | tail [project-dir] [--format pretty|json] [--status ok|error|<code>|5xx] [--method GET,POST] [--sampling-rate <0..1>] [--search <text>] [--outcome <name>] [--recent] [--sprout] [--file <log|-> [--lines <n>] [--no-follow]] | kv <list | create <name> | info <name> | rename <name> <new> | delete <name>> | <key | bulk | export> ... | d1 <list | create <name> | info <name> | rename <name> <new> | delete <name>> | r2 <list | create <name> | info <name> | rename <name> <new> | delete <name>> | queues <list | create <name> | info <name> | rename <name> <new> | delete <name>> | domains <list | add <host> | verify <host> | delete <host>> [project-dir] | secrets <list | put <NAME> [--value <value>] | delete <NAME>> [project-dir] | delete [project-dir] [--name <project>] --yes | login [--api-url <url>] [--token <token>] | logout [--api-url <url>] | whoami | complete [bash | zsh | fish]>
36
37
  ```
37
38
 
38
39
  ## Environment variables
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sproutboat",
3
- "version": "0.52.0",
3
+ "version": "0.53.0",
4
4
  "description": "Wrangler-shaped CLI for Sproutboat. Deploys workers to any control plane via --api-url / SPROUTBOAT_API_URL.",
5
5
  "keywords": [
6
6
  "cli",
@@ -64,9 +64,9 @@
64
64
  "@sproutboat/artifact": "^0.3.0",
65
65
  "@sproutboat/assets": "^0.3.0",
66
66
  "@sproutboat/config": "^0.6.0",
67
- "@sproutboat/runtime": "^0.42.2",
68
- "@sproutboat/toolchain": "^0.24.1",
69
- "@sproutboat/wire": "^0.22.2"
67
+ "@sproutboat/runtime": "^0.43.0",
68
+ "@sproutboat/toolchain": "^0.24.2",
69
+ "@sproutboat/wire": "^0.22.3"
70
70
  },
71
71
  "devDependencies": {
72
72
  "@oxlint/plugins": "1.85.0",
@@ -78,10 +78,10 @@
78
78
  "typescript": "7.0.2"
79
79
  },
80
80
  "optionalDependencies": {
81
- "@sproutboat/cli-darwin-arm64": "0.52.0",
82
- "@sproutboat/cli-darwin-x64": "0.52.0",
83
- "@sproutboat/cli-linux-arm64": "0.52.0",
84
- "@sproutboat/cli-linux-x64": "0.52.0"
81
+ "@sproutboat/cli-darwin-arm64": "0.53.0",
82
+ "@sproutboat/cli-darwin-x64": "0.53.0",
83
+ "@sproutboat/cli-linux-arm64": "0.53.0",
84
+ "@sproutboat/cli-linux-x64": "0.53.0"
85
85
  },
86
86
  "engines": {
87
87
  "node": ">=18"
package/src/build.ts CHANGED
@@ -18,7 +18,7 @@ import {
18
18
  RUNTIME,
19
19
  type ArtifactManifest,
20
20
  } from "./manifest";
21
- import { ensureZig, esbuildVersion, porfforVersion, toolchainStamp } from "./toolchain";
21
+ import { ensureZig, esbuildVersion, hostCompilerIdentity, porfforVersion, toolchainStamp } from "./toolchain";
22
22
  import { PORFFOR_ARCHIVE_SHA256, PORFFOR_COMMIT_FULL } from "./porffor-toolchain";
23
23
  import { A21_STANDALONE_FINGERPRINT } from "@sproutboat/toolchain/a21";
24
24
  import { version as toolchainPackageVersion } from "@sproutboat/toolchain/package.json" with { type: "json" };
@@ -346,6 +346,8 @@ export async function buildArtifact(input: BuildInput): Promise<BuildOutput> {
346
346
  : null,
347
347
  native: embedded ? [sqliteStamp(), bearsslStamp()] : subtle ? [bearsslStamp()] : [],
348
348
  integration: await integrationIdentity(),
349
+ // a host binary comes from this machine's C compiler, so a different compiler is a different binary
350
+ hostCompiler: host ? await hostCompilerIdentity() : null,
349
351
  nativeRestore: input.withRestore ? NATIVE_RESTORE_IDENTITY : null,
350
352
  }),
351
353
  ).slice("sha256:".length);
@@ -399,7 +401,6 @@ export async function buildArtifact(input: BuildInput): Promise<BuildOutput> {
399
401
  const compileStartedAt = performance.now();
400
402
  if (
401
403
  !input.reuseSproutPath &&
402
- !host &&
403
404
  input.optimize !== "dev" &&
404
405
  !input.outputDirectory &&
405
406
  !versionMetadata &&
package/src/compile.ts CHANGED
@@ -164,6 +164,23 @@ export async function loadPrelude(transport: Transport = "broker"): Promise<stri
164
164
  return core.replace(TRANSPORT_MARKER, chosen);
165
165
  }
166
166
 
167
+ /**
168
+ * Drop the previous render from a units directory, keeping `obj/`.
169
+ *
170
+ * The renderer rewrites every unit, and a unit whose name changed must not linger (the build compiles every
171
+ * `.c` it finds). `obj/` holds the objects `buildStandaloneA21` stamps by the unit text, `porf.h` and the
172
+ * flags, so it has to survive a rebuild: wiping it recompiled every unit on every build, which made a warm
173
+ * host build as slow as a cold one (about 13 s of C compile plus the link).
174
+ */
175
+ export async function clearRenderedUnits(unitsDir: string): Promise<void> {
176
+ const entries = await readdir(unitsDir).catch((): string[] => []);
177
+ await Promise.all(
178
+ entries
179
+ .filter((name) => name !== "obj")
180
+ .map((name) => rm(resolve(unitsDir, name), { recursive: true, force: true })),
181
+ );
182
+ }
183
+
167
184
  let porfforBuildsPruned = false;
168
185
  /**
169
186
  * Porffor writes one build dir per entry path under ~/.cache/porffor/build and
@@ -448,7 +465,7 @@ async function compileSproutA21(
448
465
  const entry = resolve(dir, "sprout.a22.js");
449
466
  await writeFile(entry, composed.entry);
450
467
  const unitsDir = resolve(dir, "units-a22");
451
- await rm(unitsDir, { recursive: true, force: true }).catch(() => {});
468
+ await clearRenderedUnits(unitsDir);
452
469
  // the compiler's own build cache (keyed by entry path) is not used: `c` only renders
453
470
  const render = Bun.spawn(
454
471
  [
package/src/dev.ts CHANGED
@@ -18,6 +18,7 @@ import { dirname, resolve } from "node:path";
18
18
  import { buildArtifact } from "./build";
19
19
  import { createBroker, listen, listenTransfers, type Bindings, type Broker } from "./broker";
20
20
  import { jsonObject, parseJsonValue } from "./json";
21
+ import { resolveDevVars } from "./env-file";
21
22
  import { amber, dim, leaf, ok } from "./style";
22
23
  import type { SproutboatConfig } from "./config";
23
24
 
@@ -32,6 +33,10 @@ export type DevInput = {
32
33
  source: string;
33
34
  port: number;
34
35
  watch: boolean;
36
+ /** #275: `--env-file` paths (absolute), in order. Default: `.dev.vars`, else the `.env` files. */
37
+ envFiles?: string[];
38
+ /** #275: `--env <name>`, which selects `.dev.vars.<name>` and the `.env.<name>` files. */
39
+ envName?: string;
35
40
  /** Re-read the complete project after a change; throws with a readable message. */
36
41
  rebuild: () => Promise<Pick<DevInput, "config" | "sourcePath" | "source">>;
37
42
  /** Set internally for an asset-only refresh, where recompiling is unnecessary. */
@@ -54,30 +59,25 @@ export function isAssetOnlyRefresh(
54
59
  }
55
60
 
56
61
  /**
57
- * Secrets for local dev, `KEY=value` per line, from `.dev.vars` beside the
58
- * config — the same file Wrangler uses. Deployed secrets live in the control
59
- * plane and are never on a developer's disk, so this is the only way a bound
60
- * secret can resolve here.
62
+ * Secrets for local dev (#275), by Wrangler's rules (env-file.ts): `.dev.vars`
63
+ * or the `.env` files, never both; the process environment only with
64
+ * CLOUDFLARE_INCLUDE_PROCESS_ENV=true. A declared secret with no value, or a
65
+ * skipped line, is a warning and `dev` still starts. Read on every (re)start, so
66
+ * an edited file is picked up.
61
67
  */
62
- async function readDevVars(projectDir: string): Promise<Record<string, string>> {
63
- const path = resolve(projectDir, ".dev.vars");
64
- if (!existsSync(path)) return {};
65
- const text = await readFile(path, "utf8");
66
- return Object.fromEntries(
67
- text.split("\n").flatMap((line): Array<[string, string]> => {
68
- const trimmed = line.trim();
69
- if (trimmed === "" || trimmed.startsWith("#")) return [];
70
- const eq = trimmed.indexOf("=");
71
- if (eq <= 0) return [];
72
- const value = trimmed.slice(eq + 1).trim();
73
- // Accept quoted values, since a secret can legitimately contain spaces.
74
- const unquoted =
75
- (value.startsWith('"') && value.endsWith('"')) || (value.startsWith("'") && value.endsWith("'"))
76
- ? value.slice(1, -1)
77
- : value;
78
- return [[trimmed.slice(0, eq).trim(), unquoted]];
79
- }),
80
- );
68
+ export async function devSecrets(
69
+ input: Pick<DevInput, "projectDir" | "envFiles" | "envName" | "config">,
70
+ inherited: Record<string, string | undefined> = process.env,
71
+ ): Promise<Record<string, string>> {
72
+ const resolved = await resolveDevVars({
73
+ projectDir: input.projectDir,
74
+ envName: input.envName,
75
+ envFiles: input.envFiles,
76
+ declared: input.config.secrets ?? [],
77
+ inherited,
78
+ });
79
+ for (const warning of resolved.warnings) console.warn(amber(`! ${warning}`));
80
+ return resolved.values;
81
81
  }
82
82
 
83
83
  /** `bindings.json` is written by the build whenever the project declares any. */
@@ -149,7 +149,7 @@ async function start(input: DevInput, port: number): Promise<Running> {
149
149
  resourceDir: resolve(stateDir, "resources"),
150
150
  token: "sproutboat-dev",
151
151
  bindings: await readBindings(artifactDir),
152
- secrets: await readDevVars(input.projectDir),
152
+ secrets: await devSecrets(input),
153
153
  sproutUrl: `http://127.0.0.1:${port}/`,
154
154
  assetsDir: existsSync(assetsDir) ? assetsDir : undefined,
155
155
  dispatchEnabled: () => dispatchEnabled,
@@ -0,0 +1,248 @@
1
+ /**
2
+ * Local secret input (#275), modelled on Wrangler so the habits carry over.
3
+ *
4
+ * `sproutboat dev` takes local secrets from files, never from the process
5
+ * environment unless asked:
6
+ *
7
+ * - `.dev.vars` beside the config, or the `.env` files, never both. When a
8
+ * `.dev.vars` exists the `.env*` files are not loaded (a warning names them).
9
+ * With `--env <name>`, `.dev.vars.<name>` replaces `.dev.vars` when it exists.
10
+ * - The `.env` files layer, most specific first: `.env.<name>.local`,
11
+ * `.env.local`, `.env.<name>`, `.env`. `CLOUDFLARE_LOAD_DEV_VARS_FROM_DOT_ENV=false`
12
+ * turns this off. Repeated `--env-file` flags replace the default set; a later
13
+ * file overrides an earlier one.
14
+ * - The process environment reaches the app only when
15
+ * `CLOUDFLARE_INCLUDE_PROCESS_ENV=true` and no `.dev.vars` is in use, and then
16
+ * only for declared secret names. Files win over it.
17
+ * - A declared secret with no value is a warning, and `dev` still starts.
18
+ *
19
+ * `sproutboat run` (the production launch of a standalone binary) is stricter:
20
+ * a missing declared secret fails, like `wrangler deploy`. Its layers, highest
21
+ * first: `--port` and `--data-dir`, the `--env-file` files (later overrides
22
+ * earlier), the inherited environment.
23
+ *
24
+ * File syntax is dotenv's: `NAME=value`, an optional `export `, `#` comments
25
+ * (a whole line, or after an unquoted value), single, double and backtick quotes,
26
+ * `\n` and `\r` expanded inside double quotes only, quoted values may span lines,
27
+ * the last of a repeated name wins. A line that is not an assignment is skipped
28
+ * with a `file:line` warning. Warnings never repeat the text of a line, so a value
29
+ * cannot leak through one.
30
+ */
31
+ import { existsSync } from "node:fs";
32
+ import { readFile } from "node:fs/promises";
33
+ import { resolve } from "node:path";
34
+
35
+ export type EnvEntry = { key: string; value: string; line: number };
36
+ export type EnvSource = "option" | "env file" | "environment";
37
+ export type ParsedEnv = { entries: EnvEntry[]; warnings: string[] };
38
+
39
+ /** A file that could not be read at all (a missing explicit file). The message is safe to print. */
40
+ export class EnvFileError extends Error {}
41
+
42
+ /** Declared secrets that are unset or empty, for a launch that must not start without them. Names only. */
43
+ export class MissingSecretsError extends Error {
44
+ constructor(
45
+ readonly names: string[],
46
+ where: string,
47
+ ) {
48
+ super(
49
+ `missing declared secret${names.length === 1 ? "" : "s"}: ${names.join(", ")}. Set ${names.length === 1 ? "it" : "them"} in ${where} or in the environment.`,
50
+ );
51
+ }
52
+ }
53
+
54
+ const ASSIGNMENT = /^\s*(?:export\s+)?([\w.-]+)(?:\s*=\s*|:\s+)(.*)$/;
55
+
56
+ /** Parse dotenv text. Never throws: a bad line becomes a warning. */
57
+ export function parseDotenv(text: string, label: string): ParsedEnv {
58
+ const source = (text.startsWith("") ? text.slice(1) : text).replace(/\r\n?/g, "\n");
59
+ const lines = source.split("\n");
60
+ const byKey = new Map<string, EnvEntry>();
61
+ const warnings: string[] = [];
62
+ for (let index = 0; index < lines.length; index += 1) {
63
+ const line = lines[index];
64
+ const trimmed = line.trim();
65
+ if (trimmed === "" || trimmed.startsWith("#")) continue;
66
+ const match = ASSIGNMENT.exec(line);
67
+ if (!match) {
68
+ warnings.push(`${label}:${index + 1}: skipped, not a NAME=value assignment`);
69
+ continue;
70
+ }
71
+ const key = match[1];
72
+ const rest = match[2].trimStart();
73
+ const quote = rest[0];
74
+ let value: string;
75
+ if (quote === "'" || quote === '"' || quote === "`") {
76
+ let out = "";
77
+ let row = index;
78
+ let column = 0;
79
+ let current = rest.slice(1);
80
+ let closed = false;
81
+ for (;;) {
82
+ while (column < current.length) {
83
+ const char = current[column];
84
+ // As in dotenv, an escaped closing quote does not end the value (and the backslash stays).
85
+ if (char === "\\" && current[column + 1] === quote) {
86
+ out += char + quote;
87
+ column += 2;
88
+ continue;
89
+ }
90
+ if (char === quote) {
91
+ closed = true;
92
+ break;
93
+ }
94
+ out += char;
95
+ column += 1;
96
+ }
97
+ if (closed || row + 1 >= lines.length) break;
98
+ row += 1;
99
+ current = lines[row];
100
+ column = 0;
101
+ out += "\n";
102
+ }
103
+ if (!closed) {
104
+ warnings.push(`${label}:${index + 1}: skipped, unterminated ${quote} quote`);
105
+ break;
106
+ }
107
+ index = row;
108
+ value = quote === '"' ? out.replace(/\\n/g, "\n").replace(/\\r/g, "\r") : out;
109
+ } else {
110
+ const hash = rest.indexOf("#");
111
+ value = (hash >= 0 ? rest.slice(0, hash) : rest).trim();
112
+ }
113
+ byKey.set(key, { key, value, line: index + 1 });
114
+ }
115
+ return { entries: [...byKey.values()], warnings };
116
+ }
117
+
118
+ /** Read and parse an env file. Throws EnvFileError when it cannot be read. */
119
+ export async function readEnvFile(path: string): Promise<ParsedEnv> {
120
+ let text: string;
121
+ try {
122
+ text = await readFile(path, "utf8");
123
+ } catch (error) {
124
+ const code = error instanceof Error && "code" in error ? error.code : undefined;
125
+ if (code === "ENOENT") throw new EnvFileError(`env file not found: ${path}`);
126
+ throw new EnvFileError(`could not read ${path}: ${code ?? "unknown error"}`);
127
+ }
128
+ return parseDotenv(text, path);
129
+ }
130
+
131
+ /** Later entries override earlier ones, by name. */
132
+ export function mergeEntries(layers: EnvEntry[][]): EnvEntry[] {
133
+ const merged = new Map<string, EnvEntry>();
134
+ for (const layer of layers) for (const entry of layer) merged.set(entry.key, entry);
135
+ return [...merged.values()];
136
+ }
137
+
138
+ export type EnvInput = {
139
+ inherited: Record<string, string | undefined>;
140
+ file?: EnvEntry[];
141
+ /** Explicit launch options as environment variables: `--port` is PORT, `--data-dir` is SB_DATA_DIR. */
142
+ options?: Record<string, string>;
143
+ };
144
+ export type ResolvedEnvironment = { env: Record<string, string>; sources: Map<string, EnvSource> };
145
+
146
+ /** The environment to launch with, and which layer each name came from. Values are never logged. */
147
+ export function resolveEnvironment(input: EnvInput): ResolvedEnvironment {
148
+ const env: Record<string, string> = {};
149
+ const sources = new Map<string, EnvSource>();
150
+ for (const [key, value] of Object.entries(input.inherited)) {
151
+ if (value === undefined) continue;
152
+ env[key] = value;
153
+ sources.set(key, "environment");
154
+ }
155
+ for (const entry of input.file ?? []) {
156
+ env[entry.key] = entry.value;
157
+ sources.set(entry.key, "env file");
158
+ }
159
+ for (const [key, value] of Object.entries(input.options ?? {})) {
160
+ env[key] = value;
161
+ sources.set(key, "option");
162
+ }
163
+ return { env, sources };
164
+ }
165
+
166
+ /** Declared secrets that are unset or empty in `env`, in declaration order. */
167
+ export function missingSecrets(declared: readonly string[], env: Record<string, string>): string[] {
168
+ return declared.filter((name) => (env[name] ?? "") === "");
169
+ }
170
+
171
+ export type DevVars = {
172
+ /** What the app sees: every name from the files, plus declared names the process environment supplies when allowed. */
173
+ values: Record<string, string>;
174
+ /** Files that were read, in order of increasing precedence. */
175
+ used: string[];
176
+ /** Everything worth telling the developer: ignored files, skipped lines, missing declared secrets. No values. */
177
+ warnings: string[];
178
+ };
179
+
180
+ /** Local secrets for `sproutboat dev`, by the rules in the module comment. */
181
+ export async function resolveDevVars(input: {
182
+ projectDir: string;
183
+ /** `--env <name>`: selects `.dev.vars.<name>` and the `.env.<name>` files. */
184
+ envName?: string;
185
+ /** `--env-file` paths, absolute, in the order given. */
186
+ envFiles?: string[];
187
+ declared: readonly string[];
188
+ inherited: Record<string, string | undefined>;
189
+ }): Promise<DevVars> {
190
+ const warnings: string[] = [];
191
+ const at = (name: string) => resolve(input.projectDir, name);
192
+ const name = input.envName;
193
+ const explicit = input.envFiles ?? [];
194
+ const devVarsPath = [name ? at(`.dev.vars.${name}`) : undefined, at(".dev.vars")].find(
195
+ (path) => path !== undefined && existsSync(path),
196
+ );
197
+ const loadDotEnv = input.inherited.CLOUDFLARE_LOAD_DEV_VARS_FROM_DOT_ENV !== "false";
198
+ const dotEnvCandidates =
199
+ explicit.length > 0
200
+ ? explicit
201
+ : [
202
+ at(".env"),
203
+ ...(name ? [at(`.env.${name}`)] : []),
204
+ at(".env.local"),
205
+ ...(name ? [at(`.env.${name}.local`)] : []),
206
+ ];
207
+
208
+ let layers: EnvEntry[][] = [];
209
+ const used: string[] = [];
210
+ if (devVarsPath !== undefined) {
211
+ const present = dotEnvCandidates.filter((path) => existsSync(path));
212
+ if (present.length > 0)
213
+ warnings.push(`${devVarsPath} exists, so the .env files are not loaded (ignored: ${present.join(", ")})`);
214
+ const parsed = await readEnvFile(devVarsPath);
215
+ warnings.push(...parsed.warnings);
216
+ layers = [parsed.entries];
217
+ used.push(devVarsPath);
218
+ } else if (!loadDotEnv) {
219
+ if (explicit.length > 0)
220
+ warnings.push("CLOUDFLARE_LOAD_DEV_VARS_FROM_DOT_ENV=false, so the --env-file files are not loaded");
221
+ } else {
222
+ for (const path of dotEnvCandidates) {
223
+ // The default files are optional; a file named with --env-file is not.
224
+ if (explicit.length === 0 && !existsSync(path)) continue;
225
+ const parsed = await readEnvFile(path);
226
+ warnings.push(...parsed.warnings);
227
+ layers.push(parsed.entries);
228
+ used.push(path);
229
+ }
230
+ }
231
+
232
+ const values: Record<string, string> = {};
233
+ for (const entry of mergeEntries(layers)) values[entry.key] = entry.value;
234
+ // The process environment never reaches the app by default, and not when .dev.vars is in use.
235
+ if (devVarsPath === undefined && input.inherited.CLOUDFLARE_INCLUDE_PROCESS_ENV === "true") {
236
+ for (const declaredName of input.declared) {
237
+ const fromProcess = input.inherited[declaredName];
238
+ if (values[declaredName] === undefined && fromProcess !== undefined) values[declaredName] = fromProcess;
239
+ }
240
+ }
241
+ const missing = input.declared.filter((declaredName) => (values[declaredName] ?? "") === "");
242
+ if (missing.length > 0) {
243
+ warnings.push(
244
+ `declared secret${missing.length === 1 ? "" : "s"} with no value: ${missing.join(", ")}. A handler that reads ${missing.length === 1 ? "it" : "one"} will throw. Set ${missing.length === 1 ? "it" : "them"} in .dev.vars or a .env file.`,
245
+ );
246
+ }
247
+ return { values, used, warnings };
248
+ }