sproutboat 0.52.1 → 0.53.1

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,34 @@ maintained going forward by the `release` skill.
10
10
 
11
11
  ## Unreleased
12
12
 
13
+ ## [0.53.1] - 2026-11-09
14
+
15
+ Built on `@sproutboat/runtime` 0.43.0, `@sproutboat/wire` 0.22.3 and `@sproutboat/toolchain` 0.24.2, unchanged from 0.53.0. No runtime or wire change, so there is no new compatibility date and no recompile.
16
+
17
+ ### Changed
18
+
19
+ - **`init` adds `sproutboat` to the project's devDependencies and the `dev`, `deploy` and `check` scripts,** the way Cloudflare's templates do for Wrangler, so a project pins the CLI version that compiles it and `npm run deploy` works after `npm install`. The range is a caret on the running CLI's version. A missing `package.json` is created (`private`, version `0.0.0`); an existing one keeps its fields and indentation, a script with the same name is never overwritten (init prints a warning naming it), and a `sproutboat` already declared in any dependency section is left as is. An unreadable `package.json` stops init before any file is written. The next step it prints follows the project's package manager (a lockfile, then the launching tool, then npm), for example `cd hello && pnpm install && pnpm run dev` (`pnpm run`, because `pnpm deploy` is pnpm's own command).
20
+
21
+ ## [0.53.0] - 2026-11-09
22
+
23
+ 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.
24
+
25
+ ### Added
26
+
27
+ - **`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`.
28
+ - **`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.
29
+
30
+ ### Changed
31
+
32
+ - **`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.
33
+ - **`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.
34
+ - **`.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.
35
+ - **`docs/standalone.md` no longer lists `<data>/secrets.json`**: a standalone binary has only ever read secrets from the environment.
36
+
37
+ ### Fixed
38
+
39
+ - **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.
40
+
13
41
  ## [0.52.1] - 2026-11-08
14
42
 
15
43
  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.
package/README.md CHANGED
@@ -25,7 +25,7 @@ for agents: [sproutboat.com/llms.txt](https://sproutboat.com/llms.txt)).
25
25
  ## Using
26
26
 
27
27
  The npm package installs a small Node launcher and the matching native
28
- platform package, so `npm install -g sproutboat` and `npm exec sproutboat` work
28
+ platform package, so `npm i -D sproutboat`, `npm i -g sproutboat` and `npx sproutboat` work
29
29
  without Bun on `darwin` and `linux` for `arm64` and `x64`. The self-hosted
30
30
  runtime exports remain in the root package for Bun-based platform integrations.
31
31
  Nothing is needed on the machine that *runs* a sprout: that gets a static
@@ -37,23 +37,35 @@ patches there, and publishes the cache entry atomically. Installation itself
37
37
  does not clone Porffor or invoke Git, Make, or a compiler. Warm-cache builds can
38
38
  run offline.
39
39
 
40
+ Install it in each project, as you would Wrangler. The CLI is the compiler, so
41
+ a project that pins its version builds the same way on every machine and in CI:
42
+
43
+ ```sh
44
+ npm i -D sproutboat # pnpm add -D sproutboat, bun add -d sproutboat, yarn add -D sproutboat
45
+ ```
46
+
47
+ `sproutboat init` does this for you: it writes a `package.json` with
48
+ `sproutboat` in `devDependencies` and `dev`, `deploy` and `check` scripts.
49
+
40
50
  ```sh
41
- npm exec sproutboat init hello
51
+ npx sproutboat init hello # or: pnpm dlx sproutboat init hello, bunx sproutboat init hello
42
52
  cd hello
43
- npm exec sproutboat dev # runs it right here, no control plane needed
53
+ npm install
54
+ npm run dev # runs it right here, no control plane needed
44
55
  ```
45
56
 
46
- Happy with it? Ship it:
57
+ Happy with it? Log in once, then ship it:
47
58
 
48
59
  ```sh
49
- bunx sproutboat login --api-url https://control.example.com # one browser approval
50
- bunx sproutboat deploy
60
+ npx sproutboat login --api-url https://control.example.com # one browser approval
61
+ npm run deploy # or: npx sproutboat deploy
51
62
  ```
52
63
 
53
- Or install it once and drop the `npm exec`:
64
+ For quick commands outside a project, install it globally. A global install is
65
+ not pinned to anything, so keep it for `init`, `login` and `whoami`:
54
66
 
55
67
  ```sh
56
- npm install -g sproutboat # then: sproutboat deploy, sproutboat tail, ...
68
+ npm i -g sproutboat # then: sproutboat init, sproutboat whoami, ...
57
69
  ```
58
70
 
59
71
  `login` is one-time. It writes a long-lived token to
@@ -67,7 +79,7 @@ The everyday loop:
67
79
 
68
80
  | Command | What it does |
69
81
  | --- | --- |
70
- | `init [name]` | Scaffold `sproutboat.jsonc` + `src/index.js` |
82
+ | `init [name]` | Scaffold `sproutboat.jsonc`, `src/index.js` and a `package.json` that pins `sproutboat` |
71
83
  | `dev [--port <n>]` | Run it here against a real broker, rebuilding on save |
72
84
  | `deploy` | Build, provision bindings, upload, wait until the URL serves |
73
85
  | `tail [filters]` | Live request events, console output and exceptions, like `wrangler tail` |
@@ -197,8 +209,12 @@ chunking, resume state, cleanup, and the remaining download limitation.
197
209
  `sproutboat dev` runs against a real local broker, so bindings behave like
198
210
  production without a deploy — except `secrets`, which live only in the control
199
211
  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.
212
+ (`API_KEY=whatever`, one per line, gitignored), or use the `.env` files or
213
+ `sproutboat dev --env-file <file>`, as in Wrangler (`.dev.vars` or `.env`, never
214
+ both), and `env.API_KEY` resolves under `dev` only. A declared secret with no
215
+ value is a warning. The rules, the dotenv syntax, and `sproutboat run` for
216
+ launching a standalone binary (which fails on a missing declared secret) are in
217
+ [docs/standalone.md](docs/standalone.md#launching-with-sproutboat-run).
202
218
 
203
219
  ## Requirements
204
220
 
package/SURFACE.md CHANGED
@@ -3,22 +3,23 @@
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.1
6
+ **Package:** `sproutboat` 0.53.1
7
7
 
8
8
  ## Commands
9
9
 
10
10
  | Command | Arguments | Summary |
11
11
  | --- | --- | --- |
12
- | `init` | `[name]` | Scaffold sproutboat.jsonc + src/index.js in ./<name>. |
12
+ | `init` | `[name]` | Scaffold sproutboat.jsonc, src/index.js and package.json (sproutboat devDependency, dev and deploy scripts) 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.1",
3
+ "version": "0.53.1",
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",
67
+ "@sproutboat/runtime": "^0.43.0",
68
68
  "@sproutboat/toolchain": "^0.24.2",
69
- "@sproutboat/wire": "^0.22.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.1",
82
- "@sproutboat/cli-darwin-x64": "0.52.1",
83
- "@sproutboat/cli-linux-arm64": "0.52.1",
84
- "@sproutboat/cli-linux-x64": "0.52.1"
81
+ "@sproutboat/cli-darwin-arm64": "0.53.1",
82
+ "@sproutboat/cli-darwin-x64": "0.53.1",
83
+ "@sproutboat/cli-linux-arm64": "0.53.1",
84
+ "@sproutboat/cli-linux-x64": "0.53.1"
85
85
  },
86
86
  "engines": {
87
87
  "node": ">=18"
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
+ }
@@ -0,0 +1,99 @@
1
+ import { existsSync } from "node:fs";
2
+ import { join } from "node:path";
3
+ import { jsonObject, parseJsonValue, type JsonObject } from "./json";
4
+
5
+ export type PackageManager = "npm" | "pnpm" | "yarn" | "bun";
6
+
7
+ // Same conventions as Wrangler's create-cloudflare templates: a private
8
+ // package at 0.0.0, `dev` and `deploy` scripts, and the CLI as a devDependency.
9
+ export const INIT_SCRIPTS = {
10
+ dev: "sproutboat dev",
11
+ deploy: "sproutboat deploy",
12
+ check: "sproutboat check",
13
+ } as const satisfies Record<string, string>;
14
+
15
+ const LOCKFILES: ReadonlyArray<readonly [string, PackageManager]> = [
16
+ ["bun.lock", "bun"],
17
+ ["bun.lockb", "bun"],
18
+ ["pnpm-lock.yaml", "pnpm"],
19
+ ["yarn.lock", "yarn"],
20
+ ["package-lock.json", "npm"],
21
+ ];
22
+
23
+ /** A lockfile in the project wins; otherwise the tool that launched the CLI; otherwise npm. */
24
+ export function detectPackageManager(directory: string, userAgent = process.env.npm_config_user_agent): PackageManager {
25
+ for (const [file, manager] of LOCKFILES) if (existsSync(join(directory, file))) return manager;
26
+ const agent = /^(npm|pnpm|yarn|bun)\//.exec(userAgent ?? "")?.[1];
27
+ return agent === "pnpm" || agent === "yarn" || agent === "bun" ? agent : "npm";
28
+ }
29
+
30
+ export function installCommand(manager: PackageManager): string {
31
+ return `${manager} install`;
32
+ }
33
+
34
+ export function runScriptCommand(manager: PackageManager, script: string): string {
35
+ // Only yarn can drop `run` for every script. `pnpm deploy` is pnpm's own
36
+ // workspace command, so the script has to be `pnpm run deploy`.
37
+ return manager === "yarn" ? `yarn ${script}` : `${manager} run ${script}`;
38
+ }
39
+
40
+ export type PackagePlan = {
41
+ /** Serialized package.json to write. */
42
+ text: string;
43
+ created: boolean;
44
+ addedDependency: boolean;
45
+ addedScripts: string[];
46
+ /** Existing scripts left alone because the name was taken. */
47
+ skippedScripts: string[];
48
+ };
49
+
50
+ /** Throws on a package.json that is not a JSON object, before anything is written. */
51
+ export function planPackageJson(name: string, existing: string | undefined, cliVersion: string): PackagePlan {
52
+ const range = `^${cliVersion}`;
53
+ let pkg: JsonObject;
54
+ let indent: string | number = 2;
55
+ let created = false;
56
+ if (existing === undefined) {
57
+ created = true;
58
+ pkg = { name, version: "0.0.0", private: true };
59
+ } else {
60
+ let parsed: JsonObject | undefined;
61
+ try {
62
+ parsed = jsonObject(parseJsonValue(existing));
63
+ } catch {
64
+ parsed = undefined;
65
+ }
66
+ if (!parsed) throw new Error("package.json is not a valid JSON object, so init did not touch it");
67
+ pkg = parsed;
68
+ indent = /^[ \t]+(?=")/m.exec(existing)?.[0] ?? 2;
69
+ }
70
+
71
+ const scripts: JsonObject = jsonObject(pkg.scripts) ?? {};
72
+ const addedScripts: string[] = [];
73
+ const skippedScripts: string[] = [];
74
+ for (const [script, command] of Object.entries(INIT_SCRIPTS)) {
75
+ if (!(script in scripts)) {
76
+ scripts[script] = command;
77
+ addedScripts.push(script);
78
+ } else if (scripts[script] !== command) skippedScripts.push(script);
79
+ }
80
+ if (addedScripts.length > 0) pkg.scripts = scripts;
81
+
82
+ const devDependencies: JsonObject = jsonObject(pkg.devDependencies) ?? {};
83
+ // Already declared anywhere: the project chose its own range.
84
+ const declared = [pkg.dependencies, pkg.devDependencies, pkg.peerDependencies, pkg.optionalDependencies].some(
85
+ (section) => jsonObject(section)?.sproutboat !== undefined,
86
+ );
87
+ const addedDependency = !declared;
88
+ if (addedDependency) {
89
+ devDependencies.sproutboat = range;
90
+ pkg.devDependencies = Object.fromEntries(Object.entries(devDependencies).sort(([a], [b]) => a.localeCompare(b)));
91
+ }
92
+ return {
93
+ text: `${JSON.stringify(pkg, null, indent)}\n`,
94
+ created,
95
+ addedDependency,
96
+ addedScripts,
97
+ skippedScripts,
98
+ };
99
+ }