@wwchen/stack 0.2.0 → 0.2.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/blocks/AGENTS.md +2 -1
- package/configs/biome.json +9 -1
- package/dist/auth/better-auth/index.d.ts +1 -1
- package/dist/auth/better-auth/index.js +1 -1
- package/dist/auth/better-auth/tables.d.ts +11 -11
- package/dist/auth/better-auth/tables.js +3 -1
- package/dist/cli/context.js +2 -1
- package/dist/db/index.d.ts +1 -0
- package/dist/db/index.js +4 -1
- package/dist/db/ssl.d.ts +8 -0
- package/dist/db/ssl.js +21 -0
- package/dist/http/guards.d.ts +6 -0
- package/dist/http/guards.js +13 -0
- package/dist/next/api.js +4 -2
- package/dist/ui/icons.js +18 -3
- package/package.json +1 -1
package/blocks/AGENTS.md
CHANGED
|
@@ -11,7 +11,8 @@
|
|
|
11
11
|
| Typecheck | `pnpm typecheck` |
|
|
12
12
|
| Lint / format | `pnpm lint` · `pnpm lint:fix` |
|
|
13
13
|
| Tests | `pnpm test` · one file: `pnpm test tests/unit/<name>.test.ts` |
|
|
14
|
-
| Migrate
|
|
14
|
+
| Migrate | `pnpm db:migrate` (your dev branch) |
|
|
15
|
+
| Rebuild the `seed` branch (fake data; every dev and preview branch is cut from it) | `pnpm exec stack neon seed` |
|
|
15
16
|
| Add a user | `stack user:create <email> [--admin]` (on your dev branch; production: the runbook's "First admin") |
|
|
16
17
|
| Refresh the stack blocks in this file | `stack sync-blocks` (`--check` in CI) |
|
|
17
18
|
|
package/configs/biome.json
CHANGED
|
@@ -7,7 +7,15 @@
|
|
|
7
7
|
"useIgnoreFile": false
|
|
8
8
|
},
|
|
9
9
|
"files": {
|
|
10
|
-
"includes": [
|
|
10
|
+
"includes": [
|
|
11
|
+
"**",
|
|
12
|
+
"!**/*.svg",
|
|
13
|
+
"!**/.next",
|
|
14
|
+
"!**/.vercel",
|
|
15
|
+
"!**/drizzle",
|
|
16
|
+
"!**/dist",
|
|
17
|
+
"!**/coverage"
|
|
18
|
+
]
|
|
11
19
|
},
|
|
12
20
|
"formatter": {
|
|
13
21
|
"enabled": true,
|
|
@@ -313,4 +313,4 @@ declare function createInstance(opts: BetterAuthProviderOptions): import("better
|
|
|
313
313
|
};
|
|
314
314
|
}>;
|
|
315
315
|
export { type AuthTables, authTables } from "./tables.ts";
|
|
316
|
-
export { DuplicateUserError, LastAdminError, UserNotFoundError } from "./users.ts";
|
|
316
|
+
export { DuplicateUserError, LastAdminError, normaliseEmail, UserNotFoundError } from "./users.ts";
|
|
@@ -234,4 +234,4 @@ async function attempt(what, call) {
|
|
|
234
234
|
}
|
|
235
235
|
}
|
|
236
236
|
export { authTables } from "./tables.js";
|
|
237
|
-
export { DuplicateUserError, LastAdminError, UserNotFoundError } from "./users.js";
|
|
237
|
+
export { DuplicateUserError, LastAdminError, normaliseEmail, UserNotFoundError } from "./users.js";
|
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
export declare function authTables(opts?: {
|
|
2
|
-
userTable?:
|
|
1
|
+
export declare function authTables<TUser extends string = "app_user">(opts?: {
|
|
2
|
+
userTable?: TUser;
|
|
3
3
|
}): {
|
|
4
4
|
appUser: import("drizzle-orm/pg-core").PgTableWithColumns<{
|
|
5
|
-
name:
|
|
5
|
+
name: TUser;
|
|
6
6
|
schema: undefined;
|
|
7
7
|
columns: {
|
|
8
8
|
id: import("drizzle-orm/pg-core").PgColumn<{
|
|
9
9
|
name: "id";
|
|
10
|
-
tableName:
|
|
10
|
+
tableName: TUser;
|
|
11
11
|
dataType: "string";
|
|
12
12
|
columnType: "PgUUID";
|
|
13
13
|
data: string;
|
|
@@ -24,7 +24,7 @@ export declare function authTables(opts?: {
|
|
|
24
24
|
}, {}, {}>;
|
|
25
25
|
name: import("drizzle-orm/pg-core").PgColumn<{
|
|
26
26
|
name: "name";
|
|
27
|
-
tableName:
|
|
27
|
+
tableName: TUser;
|
|
28
28
|
dataType: "string";
|
|
29
29
|
columnType: "PgText";
|
|
30
30
|
data: string;
|
|
@@ -41,7 +41,7 @@ export declare function authTables(opts?: {
|
|
|
41
41
|
}, {}, {}>;
|
|
42
42
|
email: import("drizzle-orm/pg-core").PgColumn<{
|
|
43
43
|
name: "email";
|
|
44
|
-
tableName:
|
|
44
|
+
tableName: TUser;
|
|
45
45
|
dataType: "string";
|
|
46
46
|
columnType: "PgText";
|
|
47
47
|
data: string;
|
|
@@ -58,7 +58,7 @@ export declare function authTables(opts?: {
|
|
|
58
58
|
}, {}, {}>;
|
|
59
59
|
emailVerified: import("drizzle-orm/pg-core").PgColumn<{
|
|
60
60
|
name: "email_verified";
|
|
61
|
-
tableName:
|
|
61
|
+
tableName: TUser;
|
|
62
62
|
dataType: "boolean";
|
|
63
63
|
columnType: "PgBoolean";
|
|
64
64
|
data: boolean;
|
|
@@ -75,7 +75,7 @@ export declare function authTables(opts?: {
|
|
|
75
75
|
}, {}, {}>;
|
|
76
76
|
image: import("drizzle-orm/pg-core").PgColumn<{
|
|
77
77
|
name: "image";
|
|
78
|
-
tableName:
|
|
78
|
+
tableName: TUser;
|
|
79
79
|
dataType: "string";
|
|
80
80
|
columnType: "PgText";
|
|
81
81
|
data: string;
|
|
@@ -92,7 +92,7 @@ export declare function authTables(opts?: {
|
|
|
92
92
|
}, {}, {}>;
|
|
93
93
|
createdAt: import("drizzle-orm/pg-core").PgColumn<{
|
|
94
94
|
name: "created_at";
|
|
95
|
-
tableName:
|
|
95
|
+
tableName: TUser;
|
|
96
96
|
dataType: "date";
|
|
97
97
|
columnType: "PgTimestamp";
|
|
98
98
|
data: Date;
|
|
@@ -109,7 +109,7 @@ export declare function authTables(opts?: {
|
|
|
109
109
|
}, {}, {}>;
|
|
110
110
|
updatedAt: import("drizzle-orm/pg-core").PgColumn<{
|
|
111
111
|
name: "updated_at";
|
|
112
|
-
tableName:
|
|
112
|
+
tableName: TUser;
|
|
113
113
|
dataType: "date";
|
|
114
114
|
columnType: "PgTimestamp";
|
|
115
115
|
data: Date;
|
|
@@ -126,7 +126,7 @@ export declare function authTables(opts?: {
|
|
|
126
126
|
}, {}, {}>;
|
|
127
127
|
role: import("drizzle-orm/pg-core").PgColumn<{
|
|
128
128
|
name: "role";
|
|
129
|
-
tableName:
|
|
129
|
+
tableName: TUser;
|
|
130
130
|
dataType: "string";
|
|
131
131
|
columnType: "PgText";
|
|
132
132
|
data: string;
|
|
@@ -16,7 +16,9 @@ import { boolean, check, pgTable, text, timestamp, uuid } from "drizzle-orm/pg-c
|
|
|
16
16
|
const createdAt = () => timestamp("created_at", { withTimezone: true }).notNull().defaultNow();
|
|
17
17
|
const updatedAt = () => timestamp("updated_at", { withTimezone: true }).notNull().defaultNow();
|
|
18
18
|
export function authTables(opts = {}) {
|
|
19
|
-
|
|
19
|
+
// The name stays a literal type ("app_user", or cc's "user"): Drizzle keys a joined select's
|
|
20
|
+
// result by table name, so a plain `string` would merge every joined table's columns into one.
|
|
21
|
+
const userTable = (opts.userTable ?? "app_user");
|
|
20
22
|
const appUser = pgTable(userTable, {
|
|
21
23
|
id: uuid("id").primaryKey().defaultRandom(),
|
|
22
24
|
name: text("name").notNull(),
|
package/dist/cli/context.js
CHANGED
|
@@ -10,6 +10,7 @@ import { userInfo } from "node:os";
|
|
|
10
10
|
import { join } from "node:path";
|
|
11
11
|
import { pathToFileURL } from "node:url";
|
|
12
12
|
import pg from "pg";
|
|
13
|
+
import { explicitSslMode } from "../db/ssl.js";
|
|
13
14
|
import { loadEnvFile } from "../env/index.js";
|
|
14
15
|
export function resolveDeps(deps) {
|
|
15
16
|
return {
|
|
@@ -18,7 +19,7 @@ export function resolveDeps(deps) {
|
|
|
18
19
|
username: userInfo().username,
|
|
19
20
|
importModule: (specifier) => import(__rewriteRelativeImportExtension(specifier)),
|
|
20
21
|
sql: async (url, statement) => {
|
|
21
|
-
const client = new pg.Client({ connectionString: url });
|
|
22
|
+
const client = new pg.Client({ connectionString: explicitSslMode(url) });
|
|
22
23
|
await client.connect();
|
|
23
24
|
try {
|
|
24
25
|
return (await client.query(statement)).rows;
|
package/dist/db/index.d.ts
CHANGED
|
@@ -18,4 +18,5 @@ export declare function createDb<S extends Record<string, unknown>>(opts: {
|
|
|
18
18
|
};
|
|
19
19
|
export { ExplainingPool, PGLITE_HINT } from "./explaining-pool.ts";
|
|
20
20
|
export { migrationStatus } from "./migration-status.ts";
|
|
21
|
+
export { explicitSslMode } from "./ssl.ts";
|
|
21
22
|
export { assertDevDatabase, type DatabaseTargetOptions, endpointOf, isLoopbackUrl, } from "./target.ts";
|
package/dist/db/index.js
CHANGED
|
@@ -2,6 +2,7 @@ import { attachDatabasePool } from "@vercel/functions";
|
|
|
2
2
|
import { drizzle } from "drizzle-orm/node-postgres";
|
|
3
3
|
import { Pool } from "pg";
|
|
4
4
|
import { ExplainingPool } from "./explaining-pool.js";
|
|
5
|
+
import { explicitSslMode } from "./ssl.js";
|
|
5
6
|
/**
|
|
6
7
|
* The one place a connection pool is created. Pass Neon's **pooled** URL (`-pooler`, PgBouncer in
|
|
7
8
|
* transaction mode); migrations use the direct endpoint and do not come through here. Reads no
|
|
@@ -10,7 +11,8 @@ import { ExplainingPool } from "./explaining-pool.js";
|
|
|
10
11
|
export function createDb(opts) {
|
|
11
12
|
const pglite = opts.pglite === true;
|
|
12
13
|
const pool = new (pglite ? ExplainingPool : Pool)({
|
|
13
|
-
|
|
14
|
+
// Neon: `sslmode=require` spelled out as the `verify-full` pg already applies (`ssl.ts`).
|
|
15
|
+
connectionString: pglite ? opts.url : explicitSslMode(opts.url),
|
|
14
16
|
// Neon: a Vercel instance serves a handful of concurrent requests, and many instances share
|
|
15
17
|
// Neon's pooler, so each holds few connections. PGlite allows exactly one.
|
|
16
18
|
max: pglite ? 1 : (opts.max ?? 5),
|
|
@@ -33,4 +35,5 @@ export function createDb(opts) {
|
|
|
33
35
|
}
|
|
34
36
|
export { ExplainingPool, PGLITE_HINT } from "./explaining-pool.js";
|
|
35
37
|
export { migrationStatus } from "./migration-status.js";
|
|
38
|
+
export { explicitSslMode } from "./ssl.js";
|
|
36
39
|
export { assertDevDatabase, endpointOf, isLoopbackUrl, } from "./target.js";
|
package/dist/db/ssl.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Neon's connection strings say `sslmode=require`. pg 8 already treats `prefer`, `require` and
|
|
3
|
+
* `verify-ca` as `verify-full`, and says so on every new connection with a "SECURITY WARNING" that
|
|
4
|
+
* v9 will weaken them; on Vercel that lands in the runtime logs at error level on every cold start.
|
|
5
|
+
* Spelling out `verify-full` keeps exactly today's checking and silences the warning. Any other
|
|
6
|
+
* mode, or no mode, is left as it is; so is a string that is not a URL.
|
|
7
|
+
*/
|
|
8
|
+
export declare function explicitSslMode(connectionString: string): string;
|
package/dist/db/ssl.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Neon's connection strings say `sslmode=require`. pg 8 already treats `prefer`, `require` and
|
|
3
|
+
* `verify-ca` as `verify-full`, and says so on every new connection with a "SECURITY WARNING" that
|
|
4
|
+
* v9 will weaken them; on Vercel that lands in the runtime logs at error level on every cold start.
|
|
5
|
+
* Spelling out `verify-full` keeps exactly today's checking and silences the warning. Any other
|
|
6
|
+
* mode, or no mode, is left as it is; so is a string that is not a URL.
|
|
7
|
+
*/
|
|
8
|
+
export function explicitSslMode(connectionString) {
|
|
9
|
+
let url;
|
|
10
|
+
try {
|
|
11
|
+
url = new URL(connectionString);
|
|
12
|
+
}
|
|
13
|
+
catch {
|
|
14
|
+
return connectionString;
|
|
15
|
+
}
|
|
16
|
+
const mode = url.searchParams.get("sslmode");
|
|
17
|
+
if (mode !== "prefer" && mode !== "require" && mode !== "verify-ca")
|
|
18
|
+
return connectionString;
|
|
19
|
+
url.searchParams.set("sslmode", "verify-full");
|
|
20
|
+
return url.toString();
|
|
21
|
+
}
|
package/dist/http/guards.d.ts
CHANGED
|
@@ -2,6 +2,12 @@
|
|
|
2
2
|
export declare function isMutation(request: Request): boolean;
|
|
3
3
|
/** `application/json`, with or without parameters (`; charset=utf-8`). */
|
|
4
4
|
export declare function isJsonRequest(request: Request): boolean;
|
|
5
|
+
/**
|
|
6
|
+
* A request that carries no body: no `Content-Type`, and no `Content-Length` (or `0`) and no chunked
|
|
7
|
+
* transfer. `fetch(url, { method: "DELETE" })` sends exactly that. A browser form always sends a
|
|
8
|
+
* `Content-Type`, so a body-less request can never be a cross-site form post.
|
|
9
|
+
*/
|
|
10
|
+
export declare function hasNoBody(request: Request): boolean;
|
|
5
11
|
/**
|
|
6
12
|
* Whether a mutation came from this app's own pages. Browsers attach `Origin` to every cross-origin
|
|
7
13
|
* and every non-GET request, so it is compared with the host the request was addressed to (the
|
package/dist/http/guards.js
CHANGED
|
@@ -13,6 +13,19 @@ export function isJsonRequest(request) {
|
|
|
13
13
|
const type = request.headers.get("content-type")?.split(";")[0]?.trim().toLowerCase();
|
|
14
14
|
return type === "application/json";
|
|
15
15
|
}
|
|
16
|
+
/**
|
|
17
|
+
* A request that carries no body: no `Content-Type`, and no `Content-Length` (or `0`) and no chunked
|
|
18
|
+
* transfer. `fetch(url, { method: "DELETE" })` sends exactly that. A browser form always sends a
|
|
19
|
+
* `Content-Type`, so a body-less request can never be a cross-site form post.
|
|
20
|
+
*/
|
|
21
|
+
export function hasNoBody(request) {
|
|
22
|
+
if (request.headers.get("content-type") !== null)
|
|
23
|
+
return false;
|
|
24
|
+
if (request.headers.get("transfer-encoding") !== null)
|
|
25
|
+
return false;
|
|
26
|
+
const length = request.headers.get("content-length");
|
|
27
|
+
return length === null || length.trim() === "0";
|
|
28
|
+
}
|
|
16
29
|
function hostOf(origin) {
|
|
17
30
|
if (origin === null)
|
|
18
31
|
return null;
|
package/dist/next/api.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { z } from "zod";
|
|
2
|
-
import { isJsonRequest, isMutation, isSameOrigin } from "../http/guards.js";
|
|
2
|
+
import { hasNoBody, isJsonRequest, isMutation, isSameOrigin } from "../http/guards.js";
|
|
3
3
|
/**
|
|
4
4
|
* The shell every API route runs in: session, admin check, the request guards, the handler, and
|
|
5
5
|
* the envelope for whatever it throws. Written once so route files cannot drift on it.
|
|
@@ -99,7 +99,9 @@ export function apiHandler(opts, fn) {
|
|
|
99
99
|
return apiError(404, "not_found", "Not found.");
|
|
100
100
|
}
|
|
101
101
|
if (isMutation(req)) {
|
|
102
|
-
|
|
102
|
+
// JSON only, so a cross-site form post can't reach a handler; a request with no body at
|
|
103
|
+
// all (a plain `DELETE`) has nothing to be JSON, and a form always sends a Content-Type.
|
|
104
|
+
if (!isJsonRequest(req) && !hasNoBody(req)) {
|
|
103
105
|
return apiError(415, "unsupported_media_type", "Send application/json.");
|
|
104
106
|
}
|
|
105
107
|
if (!isSameOrigin(req))
|
package/dist/ui/icons.js
CHANGED
|
@@ -11,6 +11,20 @@ const ICON_PROP = /(?<![\w-])(iconStart|iconEnd|icon|name)=/g;
|
|
|
11
11
|
const GLYPH = /^[a-z][a-z0-9]*(?:-[a-z0-9]+)*$/;
|
|
12
12
|
/** A file that imports the design system, where `icon: "…"` in an object is an option's glyph. */
|
|
13
13
|
const IMPORTS_LDS = /from "(?:@wwchen\/stack\/ui|@lew-ds\/lds-react)"/;
|
|
14
|
+
/** An icon named as data: `icon: "home"` (or `iconStart`/`iconEnd`) in an object literal. */
|
|
15
|
+
const ICON_DATA = /\b(?:icon|iconStart|iconEnd):\s*"([^"]+)"/g;
|
|
16
|
+
/**
|
|
17
|
+
* Whether the file renders an LDS component, however it imports it: an app's own re-export
|
|
18
|
+
* (`@/components/ui/lds`) is as much LDS as `@wwchen/stack/ui`. A file that does is where a glyph
|
|
19
|
+
* listed as data (`{ href: "/", icon: "home" }`, handed on as `<Icon name={view.icon} />`) is drawn.
|
|
20
|
+
*/
|
|
21
|
+
function rendersLds(text) {
|
|
22
|
+
for (const tag of text.matchAll(/<([A-Z]\w*)/g)) {
|
|
23
|
+
if (LDS_COMPONENTS.has(tag[1]))
|
|
24
|
+
return true;
|
|
25
|
+
}
|
|
26
|
+
return false;
|
|
27
|
+
}
|
|
14
28
|
/** The text of a `{…}` attribute value starting at `open`, braces balanced. */
|
|
15
29
|
function braced(text, open) {
|
|
16
30
|
let depth = 0;
|
|
@@ -59,7 +73,8 @@ function enclosingTag(text, at) {
|
|
|
59
73
|
* Every glyph name a JSX source hands an LDS component: a literal (`iconStart="edit"`) or any
|
|
60
74
|
* string in an expression (`iconStart={open ? "chevron-up" : "chevron-down"}`), except an
|
|
61
75
|
* attribute value nested inside it (`type="button"` on an element passed as the icon). Also an
|
|
62
|
-
* `icon: "…"` in an object literal (a SegmentedControl option), in a file
|
|
76
|
+
* `icon: "…"` in an object literal (a SegmentedControl option, a nav's list of views), in a file
|
|
77
|
+
* that imports LDS or renders an LDS component.
|
|
63
78
|
*/
|
|
64
79
|
function glyphNamesIn(text) {
|
|
65
80
|
const found = [];
|
|
@@ -81,8 +96,8 @@ function glyphNamesIn(text) {
|
|
|
81
96
|
}
|
|
82
97
|
}
|
|
83
98
|
}
|
|
84
|
-
if (IMPORTS_LDS.test(text)) {
|
|
85
|
-
for (const option of text.matchAll(
|
|
99
|
+
if (IMPORTS_LDS.test(text) || rendersLds(text)) {
|
|
100
|
+
for (const option of text.matchAll(ICON_DATA))
|
|
86
101
|
found.push(option[1]);
|
|
87
102
|
}
|
|
88
103
|
return found;
|