@wwchen/stack 0.9.0 → 0.10.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/README.md +23 -13
- package/blocks/AGENTS.md +4 -12
- package/blocks/CLAUDE.md +8 -15
- package/blocks/env.example +0 -11
- package/blocks/gitignore +1 -2
- package/dist/cli/builtin.d.ts +6 -0
- package/dist/cli/builtin.js +47 -0
- package/dist/cli/context.d.ts +6 -20
- package/dist/cli/context.js +2 -2
- package/dist/cli/host.d.ts +8 -0
- package/dist/cli/host.js +128 -0
- package/dist/cli/index.js +4 -71
- package/dist/cli/neon.d.ts +5 -5
- package/dist/cli/neon.js +20 -10
- package/dist/cli/run.js +1 -1
- package/dist/cli/sync-blocks.d.ts +9 -8
- package/dist/cli/sync-blocks.js +157 -47
- package/dist/configs/vitest.js +1 -6
- package/dist/connections/env.d.ts +8 -0
- package/dist/connections/env.js +15 -0
- package/dist/connections/index.d.ts +1 -1
- package/dist/connections/index.js +1 -1
- package/dist/connections/table.d.ts +5 -3
- package/dist/connections/table.js +3 -3
- package/dist/db/branches.d.ts +11 -0
- package/dist/db/branches.js +19 -0
- package/dist/db/doctor.d.ts +9 -0
- package/dist/db/doctor.js +120 -0
- package/dist/db/env.d.ts +14 -0
- package/dist/db/env.js +15 -0
- package/dist/db/health.d.ts +10 -0
- package/dist/db/health.js +26 -0
- package/dist/db/index.d.ts +1 -0
- package/dist/db/index.js +1 -0
- package/dist/db/target.js +2 -1
- package/dist/env/core-env.d.ts +23 -0
- package/dist/env/core-env.js +24 -0
- package/dist/env/define-env.d.ts +22 -10
- package/dist/env/define-env.js +72 -17
- package/dist/env/index.d.ts +2 -2
- package/dist/env/index.js +1 -1
- package/dist/http/client-ip.d.ts +6 -0
- package/dist/http/client-ip.js +21 -0
- package/dist/http/index.d.ts +2 -0
- package/dist/http/index.js +2 -0
- package/dist/http/safe-next.d.ts +11 -0
- package/dist/http/safe-next.js +30 -0
- package/dist/index.d.ts +2 -1
- package/dist/index.js +1 -1
- package/dist/next/api.d.ts +9 -8
- package/dist/next/api.js +10 -8
- package/dist/next/health.d.ts +5 -7
- package/dist/next/health.js +16 -17
- package/dist/stack/contracts.d.ts +152 -0
- package/dist/stack/contracts.js +42 -0
- package/dist/vercel/index.d.ts +28 -0
- package/dist/vercel/index.js +90 -0
- package/package.json +22 -4
- package/dist/cli/doctor.d.ts +0 -40
- package/dist/cli/doctor.js +0 -166
- package/dist/cli/migrate-suite.d.ts +0 -2
- package/dist/cli/migrate-suite.js +0 -491
- package/dist/cli/user-create.d.ts +0 -6
- package/dist/cli/user-create.js +0 -77
- package/dist/cli/vercel-build.d.ts +0 -4
- package/dist/cli/vercel-build.js +0 -61
- package/dist/env/stack-env.d.ts +0 -37
- package/dist/env/stack-env.js +0 -54
package/dist/next/api.js
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
import { z } from "zod";
|
|
2
2
|
import { hasNoBody, isJsonRequest, isMutation, isSameOrigin } from "../http/guards.js";
|
|
3
3
|
/**
|
|
4
|
-
* The shell every API route runs in:
|
|
4
|
+
* The shell every API route runs in: the gate, the request guards, the handler, and the envelope for whatever it throws. Written once so route files cannot drift on it.
|
|
5
|
+
*
|
|
6
|
+
* The gate decides who the caller is: its 401 is answered `unauthenticated`, its 404 `not_found` (never a 403, so a route the caller may not use does not announce itself), and its principal is handed to the handler. A gate that throws is a bug like any other.
|
|
5
7
|
*
|
|
6
8
|
* Every failure answers `{ error: { code, message } }`. An `ApiError` is answered as it says; that is how `readJson` reports the request body (400 `invalid_json`, 422 `invalid_input`). Anything else is a bug, even a SyntaxError or a ZodError from the route's own code, and becomes a 500 with a fixed message: what was thrown is logged for the operator and never sent to the caller.
|
|
7
9
|
*
|
|
8
|
-
* `CRON_SECRET` and `HEALTH_TOKEN` are never read here. This wrapper
|
|
10
|
+
* `CRON_SECRET` and `HEALTH_TOKEN` are never read here. This wrapper admits only what its gate admits, so neither secret opens an API route.
|
|
9
11
|
*/
|
|
10
12
|
/** An error a route throws on purpose: its message is written for the caller and is sent as is. */
|
|
11
13
|
export class ApiError extends Error {
|
|
@@ -84,11 +86,11 @@ export async function readJson(req, schema) {
|
|
|
84
86
|
export function apiHandler(opts, fn) {
|
|
85
87
|
return async (req, ctx) => {
|
|
86
88
|
try {
|
|
87
|
-
const
|
|
88
|
-
if (!
|
|
89
|
-
return
|
|
90
|
-
|
|
91
|
-
|
|
89
|
+
const admitted = await opts.gate(req.headers);
|
|
90
|
+
if (!admitted.ok) {
|
|
91
|
+
return admitted.status === 401
|
|
92
|
+
? apiError(401, "unauthenticated", "Sign in to continue.")
|
|
93
|
+
: apiError(404, "not_found", "Not found.");
|
|
92
94
|
}
|
|
93
95
|
if (isMutation(req)) {
|
|
94
96
|
// JSON only, so a cross-site form post can't reach a handler; a request with no body at all (a plain `DELETE`) has nothing to be JSON, and a form always sends a Content-Type.
|
|
@@ -98,7 +100,7 @@ export function apiHandler(opts, fn) {
|
|
|
98
100
|
if (!isSameOrigin(req))
|
|
99
101
|
return apiError(403, "cross_origin", "Cross-origin request refused.");
|
|
100
102
|
}
|
|
101
|
-
return await fn(req, ctx,
|
|
103
|
+
return await fn(req, ctx, admitted.principal);
|
|
102
104
|
}
|
|
103
105
|
catch (error) {
|
|
104
106
|
if (error instanceof ApiError) {
|
package/dist/next/health.d.ts
CHANGED
|
@@ -1,14 +1,12 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { HealthCheck } from "../stack/contracts.ts";
|
|
2
2
|
/**
|
|
3
|
-
* Liveness, deliberately not readiness. The status is ALWAYS 200 while the process can answer: a
|
|
3
|
+
* Liveness, deliberately not readiness. The status is ALWAYS 200 while the process can answer: a failing dependency must not make an uptime check report a healthy web process as down.
|
|
4
4
|
*
|
|
5
|
-
* Without the token the answer is `{ ok: true }` and
|
|
5
|
+
* Without the token the answer is `{ ok: true }` and no check runs, so an anonymous caller cannot make the app touch what the checks probe. With `Authorization: Bearer $HEALTH_TOKEN` each check runs in turn and its fields join the answer in order; a check that throws reports `{ [name]: "unknown" }`. `HEALTH_TOKEN` is its own secret, never `CRON_SECRET`.
|
|
6
6
|
*
|
|
7
|
-
* `export const GET = healthRoute({
|
|
7
|
+
* `export const GET = healthRoute({ token: env.HEALTH_TOKEN, checks: [databaseCheck(db)] })`, with `dynamic = "force-dynamic"`.
|
|
8
8
|
*/
|
|
9
9
|
export declare function healthRoute(opts: {
|
|
10
|
-
db?: NodePgDatabase<any>;
|
|
11
|
-
/** Defaults to `drizzle/` under the working directory. */
|
|
12
|
-
migrationsFolder?: string;
|
|
13
10
|
token?: string;
|
|
11
|
+
checks?: HealthCheck[];
|
|
14
12
|
}): (req: Request) => Promise<Response>;
|
package/dist/next/health.js
CHANGED
|
@@ -1,31 +1,30 @@
|
|
|
1
|
-
import path from "node:path";
|
|
2
|
-
import { sql } from "drizzle-orm";
|
|
3
|
-
import { migrationStatus } from "../db/migration-status.js";
|
|
4
1
|
import { bearerMatches } from "../http/guards.js";
|
|
5
2
|
/**
|
|
6
|
-
* Liveness, deliberately not readiness. The status is ALWAYS 200 while the process can answer: a
|
|
3
|
+
* Liveness, deliberately not readiness. The status is ALWAYS 200 while the process can answer: a failing dependency must not make an uptime check report a healthy web process as down.
|
|
7
4
|
*
|
|
8
|
-
* Without the token the answer is `{ ok: true }` and
|
|
5
|
+
* Without the token the answer is `{ ok: true }` and no check runs, so an anonymous caller cannot make the app touch what the checks probe. With `Authorization: Bearer $HEALTH_TOKEN` each check runs in turn and its fields join the answer in order; a check that throws reports `{ [name]: "unknown" }`. `HEALTH_TOKEN` is its own secret, never `CRON_SECRET`.
|
|
9
6
|
*
|
|
10
|
-
* `export const GET = healthRoute({
|
|
7
|
+
* `export const GET = healthRoute({ token: env.HEALTH_TOKEN, checks: [databaseCheck(db)] })`, with `dynamic = "force-dynamic"`.
|
|
11
8
|
*/
|
|
12
9
|
export function healthRoute(opts) {
|
|
13
|
-
const
|
|
10
|
+
const checks = opts.checks ?? [];
|
|
14
11
|
// No-store: a cached liveness answer is not a liveness answer.
|
|
15
12
|
const headers = { "cache-control": "no-store" };
|
|
16
13
|
return async (req) => {
|
|
17
14
|
if (!bearerMatches(req, opts.token))
|
|
18
15
|
return Response.json({ ok: true }, { headers });
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
16
|
+
let body = { ok: true };
|
|
17
|
+
for (const probe of checks) {
|
|
18
|
+
let fields;
|
|
19
|
+
try {
|
|
20
|
+
fields = await probe.check();
|
|
21
|
+
}
|
|
22
|
+
catch (error) {
|
|
23
|
+
console.error(`[health] ${probe.name} check failed`, error);
|
|
24
|
+
fields = { [probe.name]: "unknown" };
|
|
25
|
+
}
|
|
26
|
+
body = { ...body, ...fields };
|
|
24
27
|
}
|
|
25
|
-
|
|
26
|
-
return Response.json({ ok: true, db: "down", migrations: "unknown" }, { headers });
|
|
27
|
-
}
|
|
28
|
-
const migrations = await migrationStatus(db, migrationsFolder).catch(() => "unknown");
|
|
29
|
-
return Response.json({ ok: true, db: "up", migrations }, { headers });
|
|
28
|
+
return Response.json(body, { headers });
|
|
30
29
|
};
|
|
31
30
|
}
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
import type { z } from "zod";
|
|
2
|
+
/** The file that composes the app's packages. */
|
|
3
|
+
export declare const STACK_CONFIG_FILE = "stack.config.ts";
|
|
4
|
+
/** The built-in package's name, which its blocks' markers carry. */
|
|
5
|
+
export declare const BUILTIN_PACKAGE = "stack";
|
|
6
|
+
/** The files `stack sync-blocks` writes packages' marked blocks into. */
|
|
7
|
+
export declare const BLOCK_FILES: {
|
|
8
|
+
readonly agents: "AGENTS.md";
|
|
9
|
+
readonly claude: "CLAUDE.md";
|
|
10
|
+
readonly gitignore: ".gitignore";
|
|
11
|
+
readonly envExample: ".env.example";
|
|
12
|
+
};
|
|
13
|
+
export type BlockFile = (typeof BLOCK_FILES)[keyof typeof BLOCK_FILES];
|
|
14
|
+
/** A package's marked block: the file it goes in, and its text. */
|
|
15
|
+
export type Block = {
|
|
16
|
+
file: BlockFile;
|
|
17
|
+
source: URL;
|
|
18
|
+
};
|
|
19
|
+
/**
|
|
20
|
+
* A package's blocks for `files`, each read from `dir` (the package's `blocks/` directory) under its file's name without the leading dot (`gitignore`, `env.example`), because npm leaves a `.gitignore` out of a published package.
|
|
21
|
+
*/
|
|
22
|
+
export declare const packageBlocks: (dir: URL, files: readonly BlockFile[]) => Block[];
|
|
23
|
+
/** How much a `stack doctor` finding matters: an error fails the command. */
|
|
24
|
+
export declare const SEVERITY: {
|
|
25
|
+
readonly error: "error";
|
|
26
|
+
readonly warning: "warning";
|
|
27
|
+
readonly info: "info";
|
|
28
|
+
};
|
|
29
|
+
export type Severity = (typeof SEVERITY)[keyof typeof SEVERITY];
|
|
30
|
+
/** What `NODE_ENV` holds: tooling sets it (next dev and build, Vitest, Vercel's runtime). */
|
|
31
|
+
export declare const NODE_ENVS: {
|
|
32
|
+
readonly development: "development";
|
|
33
|
+
readonly test: "test";
|
|
34
|
+
readonly production: "production";
|
|
35
|
+
};
|
|
36
|
+
export type NodeEnv = (typeof NODE_ENVS)[keyof typeof NODE_ENVS];
|
|
37
|
+
/** Vercel's environments, which a variable targets and `VERCEL_ENV` names. */
|
|
38
|
+
export declare const VERCEL_TARGET: {
|
|
39
|
+
readonly production: "production";
|
|
40
|
+
readonly preview: "preview";
|
|
41
|
+
readonly development: "development";
|
|
42
|
+
};
|
|
43
|
+
export type VercelTarget = (typeof VERCEL_TARGET)[keyof typeof VERCEL_TARGET];
|
|
44
|
+
export type Env = Record<string, string | undefined>;
|
|
45
|
+
export type ExecResult = {
|
|
46
|
+
stdout: string;
|
|
47
|
+
code: number;
|
|
48
|
+
};
|
|
49
|
+
export type ExecOptions = {
|
|
50
|
+
/** The child's environment; defaults to the CLI's own. */
|
|
51
|
+
env?: Env;
|
|
52
|
+
/** Hand the child this terminal (a dev server, a build) instead of capturing its stdout. */
|
|
53
|
+
inherit?: boolean;
|
|
54
|
+
/**
|
|
55
|
+
* Written to the child's stdin, then closed: how a secret reaches another CLI without appearing in its arguments. Pass a value already held in full, never a stream still arriving: a CLI that reads stdin at start-up takes whatever is there, which may be nothing yet.
|
|
56
|
+
*/
|
|
57
|
+
input?: string;
|
|
58
|
+
};
|
|
59
|
+
/** What a command receives from the `stack` CLI host. */
|
|
60
|
+
export type CommandContext = {
|
|
61
|
+
cwd: string;
|
|
62
|
+
env: Env;
|
|
63
|
+
exec(command: string, args: string[], options?: ExecOptions): Promise<ExecResult>;
|
|
64
|
+
log(message: string): void;
|
|
65
|
+
error(message: string): void;
|
|
66
|
+
/** Every composed package, the built-in one first. */
|
|
67
|
+
packages: readonly StackPackage[];
|
|
68
|
+
};
|
|
69
|
+
/** A `stack` command; it resolves to the exit code. */
|
|
70
|
+
export type Command = (ctx: CommandContext, args: string[]) => Promise<number>;
|
|
71
|
+
/** A step run before `next build`; a throw fails the build. */
|
|
72
|
+
export type BuildStep = {
|
|
73
|
+
name: string;
|
|
74
|
+
run(ctx: CommandContext): Promise<void>;
|
|
75
|
+
};
|
|
76
|
+
export type Finding = {
|
|
77
|
+
severity: Severity;
|
|
78
|
+
message: string;
|
|
79
|
+
};
|
|
80
|
+
/** One Vercel environment variable, metadata only: there is deliberately no `value`. */
|
|
81
|
+
export type EnvVarMeta = {
|
|
82
|
+
id: string;
|
|
83
|
+
key: string;
|
|
84
|
+
type: string;
|
|
85
|
+
target: string[];
|
|
86
|
+
gitBranch?: string;
|
|
87
|
+
configurationId?: string;
|
|
88
|
+
comment?: string;
|
|
89
|
+
};
|
|
90
|
+
/** The linked Vercel project's metadata, never a secret value. */
|
|
91
|
+
export type DoctorFacts = {
|
|
92
|
+
project: string;
|
|
93
|
+
/** The project's framework preset; null is Vercel's "Other". */
|
|
94
|
+
framework: string | null;
|
|
95
|
+
/** `vercel.json`'s `framework`, which overrides the preset; undefined when it sets none. */
|
|
96
|
+
vercelJsonFramework?: string | null;
|
|
97
|
+
envs: EnvVarMeta[];
|
|
98
|
+
/** The API had more pages of variables than were read. */
|
|
99
|
+
truncated?: boolean;
|
|
100
|
+
};
|
|
101
|
+
export type DoctorCheck = (facts: DoctorFacts) => Promise<Finding[]>;
|
|
102
|
+
/** What a package's `/stack` entry gives the app's `stack.config.ts`. */
|
|
103
|
+
export type StackPackage = {
|
|
104
|
+
/** A composed package's npm name (`@scope/name`; the built-in package is `stack`): its blocks' markers, and how the host tells an installed package from a composed one. */
|
|
105
|
+
name: string;
|
|
106
|
+
/** Each command's loader, called only when that command runs. */
|
|
107
|
+
commands?: Record<string, () => Promise<Command>>;
|
|
108
|
+
buildSteps?: BuildStep[];
|
|
109
|
+
doctorChecks?: DoctorCheck[];
|
|
110
|
+
/** The marked blocks `sync-blocks` writes. */
|
|
111
|
+
blocks?: Block[];
|
|
112
|
+
};
|
|
113
|
+
export type StackConfig = {
|
|
114
|
+
packages: StackPackage[];
|
|
115
|
+
};
|
|
116
|
+
/** A function has `call`, so a package's factory listed without its call (`infra` for `infra()`) is a type error in `defineStack`. */
|
|
117
|
+
type NotAFactory = {
|
|
118
|
+
call?: never;
|
|
119
|
+
};
|
|
120
|
+
/** The app's composition, the default export of its `stack.config.ts`. Throws on two packages with one name; the `stack` CLI refuses an entry that is not a package. */
|
|
121
|
+
export declare function defineStack(config: {
|
|
122
|
+
packages: (StackPackage & NotAFactory)[];
|
|
123
|
+
}): StackConfig;
|
|
124
|
+
export type GateResult<P> = {
|
|
125
|
+
ok: true;
|
|
126
|
+
principal: P;
|
|
127
|
+
} | {
|
|
128
|
+
ok: false;
|
|
129
|
+
status: 401 | 404;
|
|
130
|
+
};
|
|
131
|
+
/** Decides who a request is; `apiHandler` answers 401 and 404 with its own envelopes. */
|
|
132
|
+
export type Gate<P> = (headers: Headers) => Promise<GateResult<P>>;
|
|
133
|
+
/** A health probe; its fields join the health response. */
|
|
134
|
+
export type HealthCheck = {
|
|
135
|
+
name: string;
|
|
136
|
+
check(): Promise<Record<string, string>>;
|
|
137
|
+
};
|
|
138
|
+
/** A fragment's variable names: a name checked against them (`"DATABASE_URL" satisfies VariableOf<typeof dbEnv>`) fails the typecheck when the fragment drops it. */
|
|
139
|
+
export type VariableOf<F extends EnvFragment> = keyof F["schema"] & string;
|
|
140
|
+
/** A package's share of the app's env schema, which the app merges in `defineEnv({ fragments })`. */
|
|
141
|
+
export type EnvFragment<S extends z.ZodRawShape = z.ZodRawShape> = {
|
|
142
|
+
/** The fragment's export name (`coreEnv`), by which `defineEnv` names it in a mistake. */
|
|
143
|
+
name: string;
|
|
144
|
+
schema: S;
|
|
145
|
+
/** Required (non-blank, no default) when NODE_ENV is production. */
|
|
146
|
+
productionRequired?: readonly (keyof S & string)[];
|
|
147
|
+
/** Named all-or-none groups: either every member is set or none is. */
|
|
148
|
+
groups?: Readonly<Record<string, readonly (keyof S & string)[]>>;
|
|
149
|
+
/** Values an unset variable takes during a build or with validation skipped, where nothing connects to what it names. */
|
|
150
|
+
buildPlaceholders?: Partial<Record<keyof S & string, string>>;
|
|
151
|
+
};
|
|
152
|
+
export {};
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/** The file that composes the app's packages. */
|
|
2
|
+
export const STACK_CONFIG_FILE = "stack.config.ts";
|
|
3
|
+
/** The built-in package's name, which its blocks' markers carry. */
|
|
4
|
+
export const BUILTIN_PACKAGE = "stack";
|
|
5
|
+
/** The files `stack sync-blocks` writes packages' marked blocks into. */
|
|
6
|
+
export const BLOCK_FILES = {
|
|
7
|
+
agents: "AGENTS.md",
|
|
8
|
+
claude: "CLAUDE.md",
|
|
9
|
+
gitignore: ".gitignore",
|
|
10
|
+
envExample: ".env.example",
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* A package's blocks for `files`, each read from `dir` (the package's `blocks/` directory) under its file's name without the leading dot (`gitignore`, `env.example`), because npm leaves a `.gitignore` out of a published package.
|
|
14
|
+
*/
|
|
15
|
+
export const packageBlocks = (dir, files) => files.map((file) => ({ file, source: new URL(file.replace(/^\./, ""), dir) }));
|
|
16
|
+
/** How much a `stack doctor` finding matters: an error fails the command. */
|
|
17
|
+
export const SEVERITY = { error: "error", warning: "warning", info: "info" };
|
|
18
|
+
/** What `NODE_ENV` holds: tooling sets it (next dev and build, Vitest, Vercel's runtime). */
|
|
19
|
+
export const NODE_ENVS = {
|
|
20
|
+
development: "development",
|
|
21
|
+
test: "test",
|
|
22
|
+
production: "production",
|
|
23
|
+
};
|
|
24
|
+
/** Vercel's environments, which a variable targets and `VERCEL_ENV` names. */
|
|
25
|
+
export const VERCEL_TARGET = {
|
|
26
|
+
production: "production",
|
|
27
|
+
preview: "preview",
|
|
28
|
+
development: "development",
|
|
29
|
+
};
|
|
30
|
+
/** The app's composition, the default export of its `stack.config.ts`. Throws on two packages with one name; the `stack` CLI refuses an entry that is not a package. */
|
|
31
|
+
export function defineStack(config) {
|
|
32
|
+
const seen = new Set();
|
|
33
|
+
for (const entry of config.packages) {
|
|
34
|
+
const name = entry?.name;
|
|
35
|
+
if (typeof name !== "string")
|
|
36
|
+
continue;
|
|
37
|
+
if (seen.has(name))
|
|
38
|
+
throw new Error(`${STACK_CONFIG_FILE}: two packages are named ${name}`);
|
|
39
|
+
seen.add(name);
|
|
40
|
+
}
|
|
41
|
+
return config;
|
|
42
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { type CommandContext, type DoctorCheck, type EnvVarMeta } from "../stack/contracts.ts";
|
|
2
|
+
export type { EnvVarMeta };
|
|
3
|
+
/** What the reader needs of a command's context: its directory and its `exec`. */
|
|
4
|
+
export type VercelContext = Pick<CommandContext, "cwd" | "exec">;
|
|
5
|
+
/** The linked project: its id, and the `?teamId=` query its team's API calls carry ("" for a personal account). */
|
|
6
|
+
export type LinkedProject = {
|
|
7
|
+
id: string;
|
|
8
|
+
query: string;
|
|
9
|
+
};
|
|
10
|
+
/** A non-empty string, or undefined: how a field of an API answer or a link file is read. */
|
|
11
|
+
export declare const nonEmptyString: (value: unknown) => string | undefined;
|
|
12
|
+
/** A JSON file's object; undefined when there is no such file or it holds no object (`null`, a number). */
|
|
13
|
+
export declare function readJsonFile(path: string): Record<string, unknown> | undefined;
|
|
14
|
+
/** The project `vercel link` wrote in `.vercel/project.json`. */
|
|
15
|
+
export declare function linkedProject(ctx: Pick<VercelContext, "cwd">): LinkedProject;
|
|
16
|
+
/** Runs `vercel api <path>`; a failure names the endpoint and exit code, never the response. */
|
|
17
|
+
export declare function vercelApi(ctx: VercelContext, path: string): Promise<Record<string, unknown>>;
|
|
18
|
+
/** The metadata of one API variable; its value (and anything else) is not copied. */
|
|
19
|
+
export declare function envVarMeta(raw: unknown): EnvVarMeta;
|
|
20
|
+
/** The first page of the project's variables, metadata only; `truncated` when the API has more pages. */
|
|
21
|
+
export declare function listEnvs(ctx: VercelContext, project: LinkedProject): Promise<{
|
|
22
|
+
envs: EnvVarMeta[];
|
|
23
|
+
truncated: boolean;
|
|
24
|
+
}>;
|
|
25
|
+
/**
|
|
26
|
+
* `stack doctor`'s check that each of `keys`, a package's secrets, holds Preview's own value, never production's (runbook §3): a warning for each that is one variable for Production and Preview, in the order given.
|
|
27
|
+
*/
|
|
28
|
+
export declare function previewOwnSecretsCheck(keys: readonly string[]): DoctorCheck;
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
// The linked Vercel project's metadata, read through the Vercel CLI (`vercel api`): the project `vercel link` wrote, its variables' metadata and the API's answers. Never asks the API to decrypt: a variable's value is dropped the moment the response is parsed, and never printed. For the `stack` commands that audit or change the project's settings; never part of the app's runtime, and it loads nothing but Node's own modules.
|
|
2
|
+
import { readFileSync } from "node:fs";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
import { SEVERITY, VERCEL_TARGET, } from "../stack/contracts.js";
|
|
5
|
+
/** A non-empty string, or undefined: how a field of an API answer or a link file is read. */
|
|
6
|
+
export const nonEmptyString = (value) => typeof value === "string" && value !== "" ? value : undefined;
|
|
7
|
+
/** A JSON file's object; undefined when there is no such file or it holds no object (`null`, a number). */
|
|
8
|
+
export function readJsonFile(path) {
|
|
9
|
+
let text;
|
|
10
|
+
try {
|
|
11
|
+
text = readFileSync(path, "utf8");
|
|
12
|
+
}
|
|
13
|
+
catch (error) {
|
|
14
|
+
if (error.code === "ENOENT")
|
|
15
|
+
return undefined;
|
|
16
|
+
throw error;
|
|
17
|
+
}
|
|
18
|
+
let parsed;
|
|
19
|
+
try {
|
|
20
|
+
parsed = JSON.parse(text);
|
|
21
|
+
}
|
|
22
|
+
catch {
|
|
23
|
+
throw new Error(`${path} is not valid JSON.`);
|
|
24
|
+
}
|
|
25
|
+
return typeof parsed === "object" && parsed !== null
|
|
26
|
+
? parsed
|
|
27
|
+
: undefined;
|
|
28
|
+
}
|
|
29
|
+
/** The project `vercel link` wrote in `.vercel/project.json`. */
|
|
30
|
+
export function linkedProject(ctx) {
|
|
31
|
+
const link = readJsonFile(join(ctx.cwd, ".vercel", "project.json"));
|
|
32
|
+
if (!link) {
|
|
33
|
+
throw new Error("this directory is not linked to a Vercel project: run `vercel link`.");
|
|
34
|
+
}
|
|
35
|
+
const id = nonEmptyString(link.projectId);
|
|
36
|
+
if (!id)
|
|
37
|
+
throw new Error(".vercel/project.json has no projectId: run `vercel link`.");
|
|
38
|
+
const orgId = nonEmptyString(link.orgId);
|
|
39
|
+
return { id, query: orgId?.startsWith("team_") ? `?teamId=${encodeURIComponent(orgId)}` : "" };
|
|
40
|
+
}
|
|
41
|
+
/** Runs `vercel api <path>`; a failure names the endpoint and exit code, never the response. */
|
|
42
|
+
export async function vercelApi(ctx, path) {
|
|
43
|
+
const { stdout, code } = await ctx.exec("vercel", ["api", path, "--raw"]);
|
|
44
|
+
if (code !== 0) {
|
|
45
|
+
throw new Error(`vercel api ${path.split("?")[0]} failed (exit ${code}). Is the Vercel CLI signed in (\`vercel login\`)?`);
|
|
46
|
+
}
|
|
47
|
+
try {
|
|
48
|
+
return JSON.parse(stdout);
|
|
49
|
+
}
|
|
50
|
+
catch {
|
|
51
|
+
throw new Error(`vercel api ${path.split("?")[0]} did not answer with JSON.`);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
/** The metadata of one API variable; its value (and anything else) is not copied. */
|
|
55
|
+
export function envVarMeta(raw) {
|
|
56
|
+
const r = (raw ?? {});
|
|
57
|
+
const target = Array.isArray(r.target) ? r.target : r.target === undefined ? [] : [r.target];
|
|
58
|
+
return {
|
|
59
|
+
id: String(r.id ?? ""),
|
|
60
|
+
key: String(r.key ?? ""),
|
|
61
|
+
type: String(r.type ?? ""),
|
|
62
|
+
target: target.filter((t) => typeof t === "string"),
|
|
63
|
+
gitBranch: nonEmptyString(r.gitBranch),
|
|
64
|
+
configurationId: nonEmptyString(r.configurationId),
|
|
65
|
+
comment: nonEmptyString(r.comment),
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
/** The first page of the project's variables, metadata only; `truncated` when the API has more pages. */
|
|
69
|
+
export async function listEnvs(ctx, project) {
|
|
70
|
+
const listed = await vercelApi(ctx, `/v10/projects/${encodeURIComponent(project.id)}/env${project.query}`);
|
|
71
|
+
const next = listed.pagination?.next;
|
|
72
|
+
return {
|
|
73
|
+
envs: (Array.isArray(listed.envs) ? listed.envs : []).map(envVarMeta),
|
|
74
|
+
truncated: next !== undefined && next !== null,
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* `stack doctor`'s check that each of `keys`, a package's secrets, holds Preview's own value, never production's (runbook §3): a warning for each that is one variable for Production and Preview, in the order given.
|
|
79
|
+
*/
|
|
80
|
+
export function previewOwnSecretsCheck(keys) {
|
|
81
|
+
const previewOwnSecrets = async ({ envs }) => keys
|
|
82
|
+
.filter((key) => envs.some((v) => v.key === key &&
|
|
83
|
+
v.target.includes(VERCEL_TARGET.production) &&
|
|
84
|
+
v.target.includes(VERCEL_TARGET.preview)))
|
|
85
|
+
.map((key) => ({
|
|
86
|
+
severity: SEVERITY.warning,
|
|
87
|
+
message: `${key} is one variable for Production and Preview: give Preview its own value, so a preview can never use production's.`,
|
|
88
|
+
}));
|
|
89
|
+
return previewOwnSecrets;
|
|
90
|
+
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wwchen/stack",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "Shared Next.js, Neon
|
|
3
|
+
"version": "0.10.1",
|
|
4
|
+
"description": "Shared Next.js, Neon and lew-ds building blocks, and the stack CLI that composes an app's packages.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
7
7
|
"type": "git",
|
|
@@ -36,6 +36,14 @@
|
|
|
36
36
|
"types": "./dist/db/index.d.ts",
|
|
37
37
|
"default": "./dist/db/index.js"
|
|
38
38
|
},
|
|
39
|
+
"./db/env": {
|
|
40
|
+
"types": "./dist/db/env.d.ts",
|
|
41
|
+
"default": "./dist/db/env.js"
|
|
42
|
+
},
|
|
43
|
+
"./db/branches": {
|
|
44
|
+
"types": "./dist/db/branches.d.ts",
|
|
45
|
+
"default": "./dist/db/branches.js"
|
|
46
|
+
},
|
|
39
47
|
"./db/testing": {
|
|
40
48
|
"types": "./dist/db/testing/index.d.ts",
|
|
41
49
|
"default": "./dist/db/testing/index.js"
|
|
@@ -44,10 +52,22 @@
|
|
|
44
52
|
"types": "./dist/db/testing/isolate.d.ts",
|
|
45
53
|
"default": "./dist/db/testing/isolate.js"
|
|
46
54
|
},
|
|
55
|
+
"./http": {
|
|
56
|
+
"types": "./dist/http/index.d.ts",
|
|
57
|
+
"default": "./dist/http/index.js"
|
|
58
|
+
},
|
|
59
|
+
"./vercel": {
|
|
60
|
+
"types": "./dist/vercel/index.d.ts",
|
|
61
|
+
"default": "./dist/vercel/index.js"
|
|
62
|
+
},
|
|
47
63
|
"./connections": {
|
|
48
64
|
"types": "./dist/connections/index.d.ts",
|
|
49
65
|
"default": "./dist/connections/index.js"
|
|
50
66
|
},
|
|
67
|
+
"./connections/env": {
|
|
68
|
+
"types": "./dist/connections/env.d.ts",
|
|
69
|
+
"default": "./dist/connections/env.js"
|
|
70
|
+
},
|
|
51
71
|
"./jobs": {
|
|
52
72
|
"types": "./dist/jobs/index.d.ts",
|
|
53
73
|
"default": "./dist/jobs/index.js"
|
|
@@ -84,8 +104,6 @@
|
|
|
84
104
|
},
|
|
85
105
|
"dependencies": {
|
|
86
106
|
"@vercel/functions": "3.12.0",
|
|
87
|
-
"@wwchen/stack-auth": "0.9.0",
|
|
88
|
-
"@wwchen/stack-notify": "0.9.0",
|
|
89
107
|
"pg": "8.23.1",
|
|
90
108
|
"zod": "4.6.7"
|
|
91
109
|
},
|
package/dist/cli/doctor.d.ts
DELETED
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
import { type Cli } from "./context.ts";
|
|
2
|
-
export type Severity = "error" | "warning" | "info";
|
|
3
|
-
export type Finding = {
|
|
4
|
-
severity: Severity;
|
|
5
|
-
message: string;
|
|
6
|
-
};
|
|
7
|
-
export type Target = "production" | "preview" | "development";
|
|
8
|
-
/** One Vercel environment variable, metadata only: there is deliberately no `value`. */
|
|
9
|
-
export type EnvVarMeta = {
|
|
10
|
-
id: string;
|
|
11
|
-
key: string;
|
|
12
|
-
type: string;
|
|
13
|
-
target: string[];
|
|
14
|
-
gitBranch?: string;
|
|
15
|
-
configurationId?: string;
|
|
16
|
-
comment?: string;
|
|
17
|
-
};
|
|
18
|
-
export type Hosting = {
|
|
19
|
-
project: string;
|
|
20
|
-
/** The project's framework preset; null is Vercel's "Other". */
|
|
21
|
-
framework: string | null;
|
|
22
|
-
/** `vercel.json`'s `framework`, which overrides the preset; undefined when it sets none. */
|
|
23
|
-
vercelJsonFramework?: string | null;
|
|
24
|
-
envs: EnvVarMeta[];
|
|
25
|
-
/** The API had more pages of variables than were read. */
|
|
26
|
-
truncated?: boolean;
|
|
27
|
-
};
|
|
28
|
-
/** What the Neon Vercel integration writes on every variable it creates. */
|
|
29
|
-
export declare const NEON_INTEGRATION_COMMENT = "The Neon Postgres integration sets this environment variable automatically";
|
|
30
|
-
/** Secrets a preview must hold its own value of, never production's (runbook §3). */
|
|
31
|
-
export declare const PREVIEW_OWN_SECRETS: readonly ["BETTER_AUTH_SECRET", "TOKEN_ENCRYPTION_KEY", "CRON_SECRET", "RESEND_API_KEY", "GOOGLE_CLIENT_SECRET"];
|
|
32
|
-
/** The metadata of one API variable; its value (and anything else) is not copied. */
|
|
33
|
-
export declare function envVarMeta(raw: unknown): EnvVarMeta;
|
|
34
|
-
/** Every check, on metadata alone. Sorted: errors, then warnings, then notes. */
|
|
35
|
-
export declare function audit(h: Hosting): Finding[];
|
|
36
|
-
/**
|
|
37
|
-
* The linked project (`.vercel/project.json`, written by `vercel link`), its preset and its variables' metadata from the Vercel API, and the app's `vercel.json`.
|
|
38
|
-
*/
|
|
39
|
-
export declare function readHosting(cli: Cli): Promise<Hosting>;
|
|
40
|
-
export declare function doctor(cli: Cli): Promise<number>;
|