@wwchen/stack 0.9.1 → 0.11.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/README.md +50 -23
- package/blocks/AGENTS.md +11 -31
- package/blocks/CLAUDE.md +10 -36
- package/blocks/env.example +1 -18
- package/blocks/gitignore +1 -8
- package/dist/cli/builtin.d.ts +7 -0
- package/dist/cli/builtin.js +42 -0
- package/dist/cli/check.d.ts +5 -0
- package/dist/cli/check.js +39 -0
- package/dist/cli/context.d.ts +4 -35
- package/dist/cli/context.js +1 -23
- package/dist/cli/discover.d.ts +15 -0
- package/dist/cli/discover.js +156 -0
- package/dist/cli/generate.d.ts +6 -0
- package/dist/cli/generate.js +94 -0
- package/dist/cli/host.d.ts +8 -0
- package/dist/cli/host.js +146 -0
- package/dist/cli/index.js +4 -71
- package/dist/cli/owned.d.ts +33 -0
- package/dist/cli/owned.js +151 -0
- package/dist/cli/sync.d.ts +19 -0
- package/dist/cli/sync.js +231 -0
- package/dist/configs/vitest.d.ts +3 -1
- package/dist/configs/vitest.js +8 -11
- 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/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/config.d.ts +2 -0
- package/dist/next/config.js +10 -6
- package/dist/next/health.d.ts +5 -7
- package/dist/next/health.js +16 -17
- package/dist/stack/contracts.d.ts +202 -0
- package/dist/stack/contracts.js +66 -0
- package/dist/vercel/index.d.ts +28 -0
- package/dist/vercel/index.js +90 -0
- package/files/.claude/rules/stack-ui.md +24 -0
- package/files/next.config.ts +6 -0
- package/files/src/app/api/health/route.ts +9 -0
- package/package.json +16 -29
- package/dist/cli/db.d.ts +0 -4
- package/dist/cli/db.js +0 -43
- package/dist/cli/dev.d.ts +0 -4
- package/dist/cli/dev.js +0 -70
- 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/neon.d.ts +0 -79
- package/dist/cli/neon.js +0 -384
- package/dist/cli/run.d.ts +0 -2
- package/dist/cli/run.js +0 -29
- package/dist/cli/sync-blocks.d.ts +0 -14
- package/dist/cli/sync-blocks.js +0 -93
- 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/db/explaining-pool.d.ts +0 -11
- package/dist/db/explaining-pool.js +0 -55
- package/dist/db/index.d.ts +0 -20
- package/dist/db/index.js +0 -31
- package/dist/db/migration-status.d.ts +0 -5
- package/dist/db/migration-status.js +0 -38
- package/dist/db/schema-changes.d.ts +0 -13
- package/dist/db/schema-changes.js +0 -99
- package/dist/db/ssl.d.ts +0 -4
- package/dist/db/ssl.js +0 -17
- package/dist/db/target.d.ts +0 -19
- package/dist/db/target.js +0 -65
- package/dist/db/testing/index.d.ts +0 -23
- package/dist/db/testing/index.js +0 -67
- package/dist/db/testing/isolate.d.ts +0 -1
- package/dist/db/testing/isolate.js +0 -3
- package/dist/db/testing/pglite.d.ts +0 -20
- package/dist/db/testing/pglite.js +0 -79
- package/dist/env/stack-env.d.ts +0 -37
- package/dist/env/stack-env.js +0 -54
- package/dist/env/testing.d.ts +0 -6
- package/dist/env/testing.js +0 -13
|
@@ -1,79 +0,0 @@
|
|
|
1
|
-
import { createRequire } from "node:module";
|
|
2
|
-
import { createServer } from "node:net";
|
|
3
|
-
import { join } from "node:path";
|
|
4
|
-
/**
|
|
5
|
-
* PGlite served over the Postgres wire protocol, so the app's `pg` pool talks to it like any other Postgres. Used by the test helper and a template's dev server.
|
|
6
|
-
*
|
|
7
|
-
* `1` because pglite-socket serialises every connection onto PGlite's single session: with more, one connection's open transaction blocks the others and there is no per-connection isolation. At the limit a second connection is refused outright, so a pool pointed at this server must use `max: PGLITE_MAX_CONNECTIONS` and nothing may open another pool against it.
|
|
8
|
-
*/
|
|
9
|
-
export const PGLITE_MAX_CONNECTIONS = 1;
|
|
10
|
-
async function freePort(host) {
|
|
11
|
-
return new Promise((resolve, reject) => {
|
|
12
|
-
const probe = createServer();
|
|
13
|
-
probe.once("error", reject);
|
|
14
|
-
probe.listen(0, host, () => {
|
|
15
|
-
const address = probe.address();
|
|
16
|
-
if (address === null || typeof address === "string") {
|
|
17
|
-
probe.close();
|
|
18
|
-
reject(new Error("could not find a free port"));
|
|
19
|
-
return;
|
|
20
|
-
}
|
|
21
|
-
probe.close(() => resolve(address.port));
|
|
22
|
-
});
|
|
23
|
-
});
|
|
24
|
-
}
|
|
25
|
-
/**
|
|
26
|
-
* PGlite and pglite-socket are optional peers, loaded only here, on the PGlite path: a Postgres-only setup (`TEST_DATABASE_URL`) need not install them. With `from` (an app's root), both come from that app's own dependencies instead of this package's resolution, as one pair: pglite-socket then loads the same PGlite build it is handed.
|
|
27
|
-
*/
|
|
28
|
-
async function loadPglite(from) {
|
|
29
|
-
try {
|
|
30
|
-
if (from !== undefined) {
|
|
31
|
-
const require = createRequire(join(from, "package.json"));
|
|
32
|
-
const { PGlite } = require("@electric-sql/pglite");
|
|
33
|
-
const { PGLiteSocketServer } = require("@electric-sql/pglite-socket");
|
|
34
|
-
return { PGlite, PGLiteSocketServer };
|
|
35
|
-
}
|
|
36
|
-
const [{ PGlite }, { PGLiteSocketServer }] = await Promise.all([
|
|
37
|
-
import("@electric-sql/pglite"),
|
|
38
|
-
import("@electric-sql/pglite-socket"),
|
|
39
|
-
]);
|
|
40
|
-
return { PGlite, PGLiteSocketServer };
|
|
41
|
-
}
|
|
42
|
-
catch (error) {
|
|
43
|
-
throw new Error(from !== undefined
|
|
44
|
-
? "PGlite could not be loaded from the app: add @electric-sql/pglite and " +
|
|
45
|
-
"@electric-sql/pglite-socket to its devDependencies and run `pnpm install`."
|
|
46
|
-
: "PGlite could not be loaded: install @electric-sql/pglite and @electric-sql/pglite-socket " +
|
|
47
|
-
"as devDependencies, or set TEST_DATABASE_URL to a Postgres server for the tests to use.", { cause: error });
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
/**
|
|
51
|
-
* In-memory when `dataDir` is absent. `port` 0 (or absent) picks a free one. `resolveFrom` loads PGlite from that directory's dependencies (an app's root) rather than this package's.
|
|
52
|
-
*/
|
|
53
|
-
export async function startPgliteServer(options = {}) {
|
|
54
|
-
const { PGlite, PGLiteSocketServer } = await loadPglite(options.resolveFrom);
|
|
55
|
-
const host = options.host ?? "127.0.0.1";
|
|
56
|
-
const port = options.port ? options.port : await freePort(host);
|
|
57
|
-
const db = await PGlite.create(options.dataDir);
|
|
58
|
-
const server = new PGLiteSocketServer({
|
|
59
|
-
db,
|
|
60
|
-
host,
|
|
61
|
-
port,
|
|
62
|
-
maxConnections: PGLITE_MAX_CONNECTIONS,
|
|
63
|
-
});
|
|
64
|
-
try {
|
|
65
|
-
await server.start();
|
|
66
|
-
}
|
|
67
|
-
catch (error) {
|
|
68
|
-
await db.close();
|
|
69
|
-
throw error;
|
|
70
|
-
}
|
|
71
|
-
return {
|
|
72
|
-
url: `postgres://postgres@${host}:${port}/postgres`,
|
|
73
|
-
port,
|
|
74
|
-
stop: async () => {
|
|
75
|
-
await server.stop();
|
|
76
|
-
await db.close();
|
|
77
|
-
},
|
|
78
|
-
};
|
|
79
|
-
}
|
package/dist/env/stack-env.d.ts
DELETED
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
import { z } from "zod";
|
|
2
|
-
import { type EnvSource } from "./define-env.ts";
|
|
3
|
-
/**
|
|
4
|
-
* The variables every stack app shares. Apps extend it: `defineEnv({ schema: { ...stackEnvSchema, MY_VAR: z.string() }, ... })`. Blank values are treated as absent by `defineEnv`, so no field needs its own preprocessing.
|
|
5
|
-
*/
|
|
6
|
-
export declare const stackEnvSchema: {
|
|
7
|
-
NODE_ENV: z.ZodDefault<z.ZodEnum<{
|
|
8
|
-
production: "production";
|
|
9
|
-
development: "development";
|
|
10
|
-
test: "test";
|
|
11
|
-
}>>;
|
|
12
|
-
APP_URL: z.ZodDefault<z.ZodString>;
|
|
13
|
-
DATABASE_URL: z.ZodString;
|
|
14
|
-
DATABASE_URL_UNPOOLED: z.ZodOptional<z.ZodString>;
|
|
15
|
-
PRODUCTION_DATABASE_HOST: z.ZodOptional<z.ZodString>;
|
|
16
|
-
BETTER_AUTH_SECRET: z.ZodDefault<z.ZodString>;
|
|
17
|
-
RESEND_API_KEY: z.ZodOptional<z.ZodString>;
|
|
18
|
-
EMAIL_FROM: z.ZodDefault<z.ZodString>;
|
|
19
|
-
MAGIC_LINK_SINK_PATH: z.ZodDefault<z.ZodString>;
|
|
20
|
-
CRON_SECRET: z.ZodOptional<z.ZodString>;
|
|
21
|
-
TOKEN_ENCRYPTION_KEY: z.ZodOptional<z.ZodString>;
|
|
22
|
-
HEALTH_TOKEN: z.ZodOptional<z.ZodString>;
|
|
23
|
-
GOOGLE_CLIENT_ID: z.ZodOptional<z.ZodString>;
|
|
24
|
-
GOOGLE_CLIENT_SECRET: z.ZodOptional<z.ZodString>;
|
|
25
|
-
TRUSTED_PROXY_HOPS: z.ZodDefault<z.ZodCoercedNumber<unknown>>;
|
|
26
|
-
LOG_LEVEL: z.ZodDefault<z.ZodString>;
|
|
27
|
-
};
|
|
28
|
-
/** Required (raw, non-blank) when NODE_ENV is production. Spread it to extend. */
|
|
29
|
-
export declare const stackProductionRequired: readonly ["APP_URL", "DATABASE_URL", "BETTER_AUTH_SECRET", "RESEND_API_KEY", "EMAIL_FROM"];
|
|
30
|
-
/** All-or-none groups. Spread it to extend. */
|
|
31
|
-
export declare const stackGroups: {
|
|
32
|
-
readonly google: readonly ["GOOGLE_CLIENT_ID", "GOOGLE_CLIENT_SECRET"];
|
|
33
|
-
};
|
|
34
|
-
export type StackEnv = z.infer<z.ZodObject<typeof stackEnvSchema>>;
|
|
35
|
-
export declare function stackEnv(source?: EnvSource): StackEnv;
|
|
36
|
-
/** Whether this is a production run. For the build-time config, which loads before any env schema. */
|
|
37
|
-
export declare function isProductionEnv(source?: EnvSource): boolean;
|
package/dist/env/stack-env.js
DELETED
|
@@ -1,54 +0,0 @@
|
|
|
1
|
-
import { z } from "zod";
|
|
2
|
-
import { defineEnv } from "./define-env.js";
|
|
3
|
-
/**
|
|
4
|
-
* The variables every stack app shares. Apps extend it: `defineEnv({ schema: { ...stackEnvSchema, MY_VAR: z.string() }, ... })`. Blank values are treated as absent by `defineEnv`, so no field needs its own preprocessing.
|
|
5
|
-
*/
|
|
6
|
-
export const stackEnvSchema = {
|
|
7
|
-
// Set by tooling, never by `.env`: next dev/build set it, Vitest sets `test`, Vercel's runtime sets `production`.
|
|
8
|
-
NODE_ENV: z.enum(["development", "test", "production"]).default("development"),
|
|
9
|
-
APP_URL: z.string().default("http://localhost:3000"),
|
|
10
|
-
DATABASE_URL: z.string().min(1),
|
|
11
|
-
DATABASE_URL_UNPOOLED: z.string().optional(),
|
|
12
|
-
// Production's database host, so local tooling can refuse it.
|
|
13
|
-
PRODUCTION_DATABASE_HOST: z.string().optional(),
|
|
14
|
-
// Signs session cookies and tokens. Better Auth warns on fewer than 32 characters.
|
|
15
|
-
BETTER_AUTH_SECRET: z.string().min(32).default("dev-only-better-auth-secret-change-me"),
|
|
16
|
-
// Sign-in mail. Production sends through Resend; elsewhere the link goes to the sink file.
|
|
17
|
-
RESEND_API_KEY: z.string().optional(),
|
|
18
|
-
EMAIL_FROM: z.string().default("App <no-reply@example.invalid>"),
|
|
19
|
-
MAGIC_LINK_SINK_PATH: z.string().default(".magic-links.log"),
|
|
20
|
-
CRON_SECRET: z.string().optional(),
|
|
21
|
-
// Encrypts connected-account tokens (`@wwchen/stack/connections`): 32 bytes, base64 (`openssl rand -base64 32`). Keep it forever: a new key makes every stored token unreadable.
|
|
22
|
-
TOKEN_ENCRYPTION_KEY: z
|
|
23
|
-
.string()
|
|
24
|
-
.refine((v) => Buffer.from(v, "base64").length === 32, {
|
|
25
|
-
message: "must be 32 bytes, base64-encoded (openssl rand -base64 32)",
|
|
26
|
-
})
|
|
27
|
-
.optional(),
|
|
28
|
-
HEALTH_TOKEN: z.string().optional(),
|
|
29
|
-
// Sign in with Google. Both or neither (group `google`).
|
|
30
|
-
GOOGLE_CLIENT_ID: z.string().optional(),
|
|
31
|
-
GOOGLE_CLIENT_SECRET: z.string().optional(),
|
|
32
|
-
// For `clientIp`: 0 on Vercel, which terminates the proxy chain itself.
|
|
33
|
-
TRUSTED_PROXY_HOPS: z.coerce.number().int().nonnegative().default(0),
|
|
34
|
-
LOG_LEVEL: z.string().default("info"),
|
|
35
|
-
};
|
|
36
|
-
/** Required (raw, non-blank) when NODE_ENV is production. Spread it to extend. */
|
|
37
|
-
export const stackProductionRequired = [
|
|
38
|
-
"APP_URL",
|
|
39
|
-
"DATABASE_URL",
|
|
40
|
-
"BETTER_AUTH_SECRET",
|
|
41
|
-
"RESEND_API_KEY",
|
|
42
|
-
"EMAIL_FROM",
|
|
43
|
-
];
|
|
44
|
-
/** All-or-none groups. Spread it to extend. */
|
|
45
|
-
export const stackGroups = {
|
|
46
|
-
google: ["GOOGLE_CLIENT_ID", "GOOGLE_CLIENT_SECRET"],
|
|
47
|
-
};
|
|
48
|
-
export function stackEnv(source = process.env) {
|
|
49
|
-
return defineEnv({ schema: stackEnvSchema, productionRequired: stackProductionRequired, groups: stackGroups }, source);
|
|
50
|
-
}
|
|
51
|
-
/** Whether this is a production run. For the build-time config, which loads before any env schema. */
|
|
52
|
-
export function isProductionEnv(source = process.env) {
|
|
53
|
-
return source.NODE_ENV === "production";
|
|
54
|
-
}
|
package/dist/env/testing.d.ts
DELETED
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
/** `TEST_DATABASE_URL`: a real Postgres server for test databases (a CI job's service); else PGlite. */
|
|
2
|
-
export declare function testDatabaseUrl(source?: Record<string, string | undefined>): string | undefined;
|
|
3
|
-
/**
|
|
4
|
-
* Points DATABASE_URL at a closed port before anything loads `.env`: an env loader never overrides a variable that is already set, so `.env`, which may name a real, remote database, cannot reach a test. A test that needs a database starts its own.
|
|
5
|
-
*/
|
|
6
|
-
export declare function isolateTestEnv(target?: Record<string, string | undefined>): void;
|
package/dist/env/testing.js
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
// The environment the test helpers (`@wwchen/stack/db/testing`) read and write, kept here so that src/env/ stays the one place in the package that touches `process.env`.
|
|
2
|
-
/** `TEST_DATABASE_URL`: a real Postgres server for test databases (a CI job's service); else PGlite. */
|
|
3
|
-
export function testDatabaseUrl(source = process.env) {
|
|
4
|
-
const url = source.TEST_DATABASE_URL?.trim();
|
|
5
|
-
return url ? url : undefined;
|
|
6
|
-
}
|
|
7
|
-
/**
|
|
8
|
-
* Points DATABASE_URL at a closed port before anything loads `.env`: an env loader never overrides a variable that is already set, so `.env`, which may name a real, remote database, cannot reach a test. A test that needs a database starts its own.
|
|
9
|
-
*/
|
|
10
|
-
export function isolateTestEnv(target = process.env) {
|
|
11
|
-
target.DATABASE_URL = "postgres://unused:unused@127.0.0.1:1/unused";
|
|
12
|
-
target.DATABASE_URL_UNPOOLED = "";
|
|
13
|
-
}
|