ref0-git 0.27.0 → 0.29.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/app/apps/github/README.md +1 -5
- package/app/apps/github/package.json +0 -1
- package/app/apps/github/src/signin/auth.ts +17 -89
- package/app/apps/github/src/signin/credentials.ts +10 -14
- package/app/apps/github/src/signin/guarded-write.ts +19 -0
- package/app/apps/github/src/signin/owner-names.ts +11 -41
- package/app/apps/github/src/signin/unborn.ts +20 -30
- package/app/apps/github/tsconfig.json +0 -2
- package/app/pnpm-workspace.yaml +14 -34
- package/dist/cli/documents.mjs +39 -25
- package/dist/cli/keys.mjs +9 -3
- package/dist/cli/node.mjs +1 -2
- package/dist/cli/runtime.mjs +3 -4
- package/dist/cli/setup.mjs +216 -468
- package/dist/cli/tools-node.mjs +8 -7
- package/dist/cli/tools.mjs +3 -3
- package/dist/cli/vercel-stores.mjs +287 -0
- package/dist/init/app.mjs +4 -37
- package/dist/init/choices.mjs +62 -209
- package/dist/init/cloudflare.mjs +1 -2
- package/dist/init/detect.mjs +77 -167
- package/dist/init/files.mjs +2 -12
- package/dist/init/frameworks.mjs +50 -174
- package/dist/init/git-module.mjs +5 -1
- package/dist/init/metadata.mjs +4 -22
- package/dist/init/next.mjs +13 -66
- package/dist/init/output.mjs +3 -6
- package/dist/init/plan.mjs +1 -29
- package/dist/init/routes.mjs +1 -78
- package/dist/init/stores.mjs +6 -35
- package/dist/init/template.mjs +14 -49
- package/dist/init/templates.mjs +30 -115
- package/dist/init/vercel-regions.mjs +87 -0
- package/dist/init/vercel.mjs +313 -212
- package/dist/init/versions.mjs +3 -3
- package/package.json +5 -11
- package/app/apps/github/migrations/postgres/0001_auth.sql +0 -53
- package/app/apps/github/src/camo/lookup.ts +0 -23
- package/app/apps/github/src/camo/node.ts +0 -50
- package/app/apps/github/src/signin/person-lock.ts +0 -104
- package/dist/cli/sqlite.mjs +0 -28
- package/dist/init/astro.mjs +0 -49
- package/dist/init/create.mjs +0 -152
- package/dist/init/mounts.mjs +0 -104
- package/dist/init/node-app.mjs +0 -196
- package/dist/init/node.mjs +0 -446
- package/dist/init/nuxt.mjs +0 -240
- package/dist/init/react-router.mjs +0 -59
- package/dist/init/sveltekit.mjs +0 -141
|
@@ -14,7 +14,7 @@ npm run dev
|
|
|
14
14
|
|
|
15
15
|
Open http://localhost:5173. The first request seeds a demo: Pip and Mo, who sign in with `.dev.vars`' `DEMO_PASSWORD`, the org `acme`, which Pip owns and Mo belongs to, and its repos `acme/web` in TypeScript, `acme/handbook` in Markdown and `acme/cli` in Go, each with a description and a license, and with history, pull requests, reviews and issues. It runs in workerd, with its objects, its bucket and its D1 on disk in `.wrangler/state`, and needs no Cloudflare account. Delete `.wrangler/state` to start again from an empty host.
|
|
16
16
|
|
|
17
|
-
To sign in, sign in at `/login` as `pip` or `mo` with `DEMO_PASSWORD`, or sign up at `/signup` with a login of your own and a password of at least 15 characters. People, orgs, their passwords and their sessions are Better Auth's, in D1: `src/signin/auth.ts` sets it up, and `migrations/sql/` holds its tables
|
|
17
|
+
To sign in, sign in at `/login` as `pip` or `mo` with `DEMO_PASSWORD`, or sign up at `/signup` with a login of your own and a password of at least 15 characters. People, orgs, their passwords and their sessions are Better Auth's, in D1: `src/signin/auth.ts` sets it up, and `migrations/sql/` holds its tables. The app applies them to D1 itself, before its first sign-in, and records each in wrangler's `d1_migrations` table, as `wrangler d1 migrations apply` would.
|
|
18
18
|
|
|
19
19
|
## Deploy it to Cloudflare
|
|
20
20
|
|
|
@@ -68,7 +68,3 @@ cd apps/github && pnpm run generate # writes apps/github/generated/git-rest.ts
|
|
|
68
68
|
```
|
|
69
69
|
|
|
70
70
|
Then copy that file over the project's `generated/git-rest.ts`. A new project `init` writes from that checkout takes it too, with the packages it matches.
|
|
71
|
-
|
|
72
|
-
## Other platforms
|
|
73
|
-
|
|
74
|
-
[`docs/`](../../docs/README.md) says which adapter fits each platform, and how Git runs on it.
|
|
@@ -1,14 +1,7 @@
|
|
|
1
1
|
import type { D1Database } from '@cloudflare/workers-types';
|
|
2
2
|
import type { Tokens } from '@ref0/git';
|
|
3
3
|
import { passkey } from '@better-auth/passkey';
|
|
4
|
-
import {
|
|
5
|
-
type Auth,
|
|
6
|
-
type BetterAuthOptions,
|
|
7
|
-
type DBAdapter,
|
|
8
|
-
type DBTransactionAdapter,
|
|
9
|
-
betterAuth,
|
|
10
|
-
getCurrentAdapter,
|
|
11
|
-
} from 'better-auth';
|
|
4
|
+
import { type Auth, type BetterAuthOptions, type DBAdapter, betterAuth } from 'better-auth';
|
|
12
5
|
import { APIError, createAuthMiddleware, getSessionFromCtx } from 'better-auth/api';
|
|
13
6
|
import { organization } from 'better-auth/plugins/organization';
|
|
14
7
|
import { username } from 'better-auth/plugins/username';
|
|
@@ -16,66 +9,26 @@ import * as Effect from 'effect/Effect';
|
|
|
16
9
|
import * as Exit from 'effect/Exit';
|
|
17
10
|
import * as Option from 'effect/Option';
|
|
18
11
|
import * as Schema from 'effect/Schema';
|
|
19
|
-
import type { Dialect } from 'kysely';
|
|
20
12
|
|
|
21
13
|
import type { Session, SessionCookie } from '../context/context.ts';
|
|
22
14
|
import { loginProblem, ownerProblem } from '../names/owners.ts';
|
|
23
15
|
import { deleteUnlessLast } from './credentials.ts';
|
|
24
16
|
import { closedCode, lastCredentialCode, reservedCode } from './errors.ts';
|
|
25
|
-
import {
|
|
26
|
-
claimInTransaction,
|
|
27
|
-
claimLogin,
|
|
28
|
-
holdLogin,
|
|
29
|
-
isHeld,
|
|
30
|
-
isTaken,
|
|
31
|
-
ownerNames,
|
|
32
|
-
} from './owner-names.ts';
|
|
33
17
|
import { migrate } from './migrate.ts';
|
|
18
|
+
import { claimLogin, holdLogin, isHeld, isTaken, ownerNames } from './owner-names.ts';
|
|
34
19
|
import { revokeRetrying } from './revoke.ts';
|
|
35
20
|
import { open, seal } from './seal.ts';
|
|
36
21
|
import { avatarTable, avatars } from './avatars.ts';
|
|
37
22
|
import { repoGrantTable } from './repo-grants.ts';
|
|
38
23
|
import { type Unborn, forgetUnborn } from './unborn.ts';
|
|
39
24
|
|
|
40
|
-
/**
|
|
41
|
-
|
|
42
|
-
readonly dialect: Dialect;
|
|
43
|
-
readonly type: 'sqlite' | 'postgres';
|
|
44
|
-
/** Whether Better Auth wraps its writes in transactions: off unless the dialect has them. */
|
|
45
|
-
readonly transaction?: boolean;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
/** A value `node:sqlite` binds to a statement's parameter. */
|
|
49
|
-
export type NodeSqliteValue = string | number | bigint | null | Uint8Array;
|
|
50
|
-
|
|
51
|
-
/**
|
|
52
|
-
* `node:sqlite`'s `DatabaseSync`, by the parts the app calls. The pages' types hold no Node's, so
|
|
53
|
-
* it's named by its shape, and Better Auth tells it from D1's binding with `instanceof` as it runs.
|
|
54
|
-
*/
|
|
55
|
-
export interface NodeSqliteDatabase {
|
|
56
|
-
readonly prepare: (sql: string) => {
|
|
57
|
-
readonly run: (...values: NodeSqliteValue[]) => { readonly changes: number | bigint };
|
|
58
|
-
};
|
|
59
|
-
readonly close: () => void;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
/**
|
|
63
|
-
* Where people and sessions are kept, as the platform's file passes it: on Cloudflare, the
|
|
64
|
-
* `AUTH_DB` binding, which Better Auth reads through its own D1 dialect; on Node with SQLite,
|
|
65
|
-
* `node:sqlite`'s file, which it reads through its own too; and elsewhere a dialect.
|
|
66
|
-
*/
|
|
67
|
-
export type AuthDatabase = D1Database | NodeSqliteDatabase | DialectDatabase;
|
|
68
|
-
|
|
69
|
-
/** Whether `database` is D1's binding, which alone of the three has `batch`, as Better Auth tells it. */
|
|
70
|
-
const isD1 = (database: AuthDatabase): database is D1Database => 'batch' in database;
|
|
71
|
-
|
|
72
|
-
/** Each instance's database, as `starterAuth` made it over, for `readyAuth` to ready. */
|
|
73
|
-
const databases = new WeakMap<Auth, AuthDatabase>();
|
|
25
|
+
/** Each instance's `AUTH_DB`, as `starterAuth` made it over, for `readyAuth` to migrate. */
|
|
26
|
+
const databases = new WeakMap<Auth, D1Database>();
|
|
74
27
|
|
|
75
28
|
/** What the app's people need: their database, the session secret, where it's served, and Git's tokens. */
|
|
76
29
|
export interface StarterAuthOptions {
|
|
77
|
-
/** Better Auth's tables, which `migrations/sql/` makes
|
|
78
|
-
readonly database:
|
|
30
|
+
/** The `AUTH_DB` binding: Better Auth's tables, which `migrations/sql/` makes, read through its D1 dialect. */
|
|
31
|
+
readonly database: D1Database;
|
|
79
32
|
/** `SESSION_SECRET`, which signs Better Auth's cookie. */
|
|
80
33
|
readonly secret: string;
|
|
81
34
|
/** `APP_ORIGIN`: Better Auth's base URL, and the one origin its POSTs are taken from. */
|
|
@@ -93,9 +46,7 @@ export interface StarterAuthOptions {
|
|
|
93
46
|
readonly ownsRepos: (login: string) => Effect.Effect<boolean>;
|
|
94
47
|
/**
|
|
95
48
|
* The header Better Auth keys its rate limits on, which only the platform sets: Cloudflare's
|
|
96
|
-
* `cf-connecting-ip`, which on workerd `servedClientIp` sets in its place
|
|
97
|
-
* `clientIpHeader`, which the server sets from the caller's address after dropping any copy the
|
|
98
|
-
* caller sent.
|
|
49
|
+
* `cf-connecting-ip`, which on workerd `servedClientIp` sets in its place.
|
|
99
50
|
*/
|
|
100
51
|
readonly ipHeader: string;
|
|
101
52
|
}
|
|
@@ -299,13 +250,6 @@ const claimOf = (
|
|
|
299
250
|
return { login, claim };
|
|
300
251
|
};
|
|
301
252
|
|
|
302
|
-
/** Whether a hook writes inside a real transaction: `current` is its own, and can roll back. */
|
|
303
|
-
const inTransaction = (base: DBAdapter, current: DBTransactionAdapter) => {
|
|
304
|
-
const transaction = base.options?.adapterConfig.transaction;
|
|
305
|
-
|
|
306
|
-
return transaction !== undefined && transaction !== false && current !== base;
|
|
307
|
-
};
|
|
308
|
-
|
|
309
253
|
/** A person's row, as far as a session reads it: Better Auth's username is their login. */
|
|
310
254
|
const Named = Schema.Struct({ username: Schema.String });
|
|
311
255
|
|
|
@@ -462,7 +406,7 @@ export const starterAuth = (options: StarterAuthOptions): Auth => {
|
|
|
462
406
|
avatarTable(),
|
|
463
407
|
repoGrantTable(),
|
|
464
408
|
// A credential ID names one passkey only, since the plugin finds a sign-in's passkey by it.
|
|
465
|
-
// Its schema only indexes the column, so this makes it unique, in
|
|
409
|
+
// Its schema only indexes the column, so this makes it unique, in D1's migration.
|
|
466
410
|
{
|
|
467
411
|
id: 'unique-credential-id',
|
|
468
412
|
schema: {
|
|
@@ -486,8 +430,8 @@ export const starterAuth = (options: StarterAuthOptions): Auth => {
|
|
|
486
430
|
// On, whatever `NODE_ENV` says: Better Auth turns both off under `test`.
|
|
487
431
|
disableOriginCheck: false,
|
|
488
432
|
disableCSRFCheck: false,
|
|
489
|
-
//
|
|
490
|
-
database: { validateSchema:
|
|
433
|
+
// `readyAuth` applies the app's migrations in place of Better Auth's check.
|
|
434
|
+
database: { validateSchema: false },
|
|
491
435
|
},
|
|
492
436
|
hooks: {
|
|
493
437
|
before: createAuthMiddleware(async (ctx) => {
|
|
@@ -529,31 +473,15 @@ export const starterAuth = (options: StarterAuthOptions): Auth => {
|
|
|
529
473
|
databaseHooks: {
|
|
530
474
|
user: {
|
|
531
475
|
create: {
|
|
532
|
-
//
|
|
533
|
-
//
|
|
534
|
-
// (Better Auth's `db/with-hooks.mjs:34`), when a retake could take the claim. On D1,
|
|
535
|
-
// which has none, the claim is taken here and `create.after` holds it once the person,
|
|
536
|
-
// their passkey and their session exist.
|
|
476
|
+
// D1 has no transactions, so the claim is taken here and `create.after` holds it once
|
|
477
|
+
// the person, their passkey and their session exist.
|
|
537
478
|
before: async (user, context) => {
|
|
538
479
|
// The username plugin lowercases a login before this hook, but a create past it may
|
|
539
480
|
// not: a claim is the lowercase login's, as `owner_names` and every read of it are.
|
|
540
481
|
const login = namedOf(user).pipe(Option.map((named) => named.username.toLowerCase()));
|
|
541
482
|
if (Option.isNone(login)) return;
|
|
542
483
|
|
|
543
|
-
const
|
|
544
|
-
const current = await getCurrentAdapter(base);
|
|
545
|
-
|
|
546
|
-
if (inTransaction(base, current)) {
|
|
547
|
-
const claim = await claimInTransaction(current, login.value);
|
|
548
|
-
|
|
549
|
-
if (claim === undefined || !(await holdLogin(current, login.value, claim))) {
|
|
550
|
-
throw taken('USERNAME_IS_ALREADY_TAKEN');
|
|
551
|
-
}
|
|
552
|
-
|
|
553
|
-
return;
|
|
554
|
-
}
|
|
555
|
-
|
|
556
|
-
const claim = await claimLogin(base, login.value);
|
|
484
|
+
const claim = await claimLogin(await adapterOf(), login.value);
|
|
557
485
|
if (claim === undefined) throw taken('USERNAME_IS_ALREADY_TAKEN');
|
|
558
486
|
|
|
559
487
|
if (context === null || context === undefined) {
|
|
@@ -677,8 +605,8 @@ export const readyAdapter = async (auth: StarterAuth): Promise<DBAdapter> => {
|
|
|
677
605
|
|
|
678
606
|
/**
|
|
679
607
|
* Better Auth for a request: the isolate's, kept once a request's check of it has settled, or else
|
|
680
|
-
* one `make` makes for this request, which awaits its own check:
|
|
681
|
-
*
|
|
608
|
+
* one `make` makes for this request, which awaits its own check: the app's migrations, applied as
|
|
609
|
+
* `migrate` applies them. A Worker settles a
|
|
682
610
|
* promise only in the request that made it, so no request awaits another's check: one that ended
|
|
683
611
|
* first, by a disconnect or an early answer, would leave it unsettled, and every later sign-in
|
|
684
612
|
* with it (#480).
|
|
@@ -696,7 +624,7 @@ export const readyAuth = (make: () => StarterAuth): (() => Promise<StarterAuth>)
|
|
|
696
624
|
|
|
697
625
|
const made = make();
|
|
698
626
|
const database = databases.get(made);
|
|
699
|
-
if (database !== undefined
|
|
627
|
+
if (database !== undefined) await migrate(database);
|
|
700
628
|
|
|
701
629
|
await readyAdapter(made);
|
|
702
630
|
ready ??= made;
|
|
@@ -726,7 +654,7 @@ const refuseStale = async (ctx: HookContext) => {
|
|
|
726
654
|
* refused. With no session, a bad body, or a passkey that isn't theirs, it deletes nothing, and
|
|
727
655
|
* the plugin's own route answers as it does: its check of the owner runs inside the route.
|
|
728
656
|
*/
|
|
729
|
-
const deleteOrRefuse = async (database:
|
|
657
|
+
const deleteOrRefuse = async (database: D1Database, ctx: HookContext) => {
|
|
730
658
|
const found = await getSessionFromCtx(ctx);
|
|
731
659
|
const body = Option.getOrUndefined(deleteBodyOf(ctx.body));
|
|
732
660
|
if (found === null || body === undefined) return false;
|
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import type { D1Database } from '@cloudflare/workers-types';
|
|
2
2
|
|
|
3
|
-
import
|
|
4
|
-
import { withPersonLocked } from './person-lock.ts';
|
|
3
|
+
import { guardedWrite } from './guarded-write.ts';
|
|
5
4
|
|
|
6
5
|
/** A passkey of a person's, by its row's ID. */
|
|
7
6
|
export interface PasskeyOf {
|
|
@@ -12,15 +11,12 @@ export interface PasskeyOf {
|
|
|
12
11
|
/**
|
|
13
12
|
* Deletes a person's passkey unless it's the last thing that signs them in, and answers whether
|
|
14
13
|
* it did: another passkey or a password must stay, since the hidden email reaches no one. It's
|
|
15
|
-
* one statement
|
|
16
|
-
*
|
|
14
|
+
* one guarded statement, so two deletes of a person's last two passkeys at once can't both find
|
|
15
|
+
* the other still there.
|
|
17
16
|
*/
|
|
18
|
-
export const deleteUnlessLast = (database:
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
return deleted > 0;
|
|
26
|
-
});
|
|
17
|
+
export const deleteUnlessLast = async (database: D1Database, passkey: PasskeyOf) =>
|
|
18
|
+
(await guardedWrite(database)(
|
|
19
|
+
`delete from "passkey" where "id" = ?1 and "userId" = ?2 and (exists (select 1 from "passkey" where "userId" = ?2 and "id" <> ?1) or exists (select 1 from "account" where "userId" = ?2 and "providerId" = 'credential'))`,
|
|
20
|
+
passkey.id,
|
|
21
|
+
passkey.userId,
|
|
22
|
+
)) > 0;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { D1Database } from '@cloudflare/workers-types';
|
|
2
|
+
|
|
3
|
+
/** Runs one write, its `?1`, `?2`… bound to `values` in order, and answers how many rows it changed. */
|
|
4
|
+
export type Write = (sql: string, ...values: readonly string[]) => Promise<number>;
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* A person's guarded writes on D1, each one statement that checks what the person holds as it
|
|
8
|
+
* writes, such as deleting their row only while no session is theirs. D1 runs one statement at a
|
|
9
|
+
* time, so each sees every write before it, and needs no lock.
|
|
10
|
+
*/
|
|
11
|
+
export const guardedWrite =
|
|
12
|
+
(database: D1Database): Write =>
|
|
13
|
+
async (sql, ...values) =>
|
|
14
|
+
(
|
|
15
|
+
await database
|
|
16
|
+
.prepare(sql)
|
|
17
|
+
.bind(...values)
|
|
18
|
+
.run()
|
|
19
|
+
).meta.changes;
|
|
@@ -4,8 +4,8 @@ import * as Schema from 'effect/Schema';
|
|
|
4
4
|
/**
|
|
5
5
|
* One namespace for people's logins and orgs' slugs (Q631): Better Auth checks a username only
|
|
6
6
|
* against `user` and a slug only against `organization`, so each create claims its login first in
|
|
7
|
-
* `owner_names`, whose login is the primary key. The unique insert decides a race,
|
|
8
|
-
* transactions.
|
|
7
|
+
* `owner_names`, whose login is the primary key. The unique insert decides a race, though D1 has
|
|
8
|
+
* no transactions.
|
|
9
9
|
*/
|
|
10
10
|
|
|
11
11
|
/** Better Auth's model for the claims, and the table it keeps them in. */
|
|
@@ -73,23 +73,12 @@ export const isTaken = async (
|
|
|
73
73
|
};
|
|
74
74
|
|
|
75
75
|
/**
|
|
76
|
-
*
|
|
77
|
-
*
|
|
78
|
-
* or a unique index's. D1 gives no code, only SQLite's message, which names a primary key's
|
|
79
|
-
* refusal as a unique one too.
|
|
76
|
+
* D1's refusal of a row whose key it holds. D1 gives no code, only SQLite's message, which names a
|
|
77
|
+
* primary key's refusal as a unique one too.
|
|
80
78
|
*/
|
|
81
|
-
const DuplicateKey = Schema.
|
|
82
|
-
Schema.
|
|
83
|
-
|
|
84
|
-
}),
|
|
85
|
-
Schema.Struct({
|
|
86
|
-
code: Schema.Literal('ERR_SQLITE_ERROR'),
|
|
87
|
-
errcode: Schema.Literals([1555, 2067]),
|
|
88
|
-
}),
|
|
89
|
-
Schema.Struct({
|
|
90
|
-
message: Schema.TemplateLiteral(['D1_ERROR: UNIQUE constraint failed: ', Schema.String]),
|
|
91
|
-
}),
|
|
92
|
-
]);
|
|
79
|
+
const DuplicateKey = Schema.Struct({
|
|
80
|
+
message: Schema.TemplateLiteral(['D1_ERROR: UNIQUE constraint failed: ', Schema.String]),
|
|
81
|
+
});
|
|
93
82
|
|
|
94
83
|
const isDuplicateKey = Schema.is(DuplicateKey);
|
|
95
84
|
|
|
@@ -128,40 +117,21 @@ const retaken = async (adapter: Claims, login: string, claim: string, now: numbe
|
|
|
128
117
|
/**
|
|
129
118
|
* Claims `login` for a create about to happen, answering the claim's token when it's this create's:
|
|
130
119
|
* a fresh insert, or a retake of a claim no create held within `staleAfter`. A retake is one
|
|
131
|
-
* conditional update of the claim row,
|
|
132
|
-
*
|
|
133
|
-
*
|
|
120
|
+
* conditional update of the claim row, which D1 runs alone, so a second retaker finds the claim's
|
|
121
|
+
* new `claimedAt` and changes nothing. A held claim, a live one, or one whose login a person or
|
|
122
|
+
* org holds is refused.
|
|
134
123
|
*/
|
|
135
124
|
export const claimLogin = async (adapter: Claims, login: string, now = Date.now()) => {
|
|
136
125
|
const claim = crypto.randomUUID();
|
|
137
126
|
if (await inserted(adapter, login, claim, now)) return claim;
|
|
138
127
|
|
|
139
128
|
// A person or org made with the claim, whose request stopped before it held the claim, keeps
|
|
140
|
-
// its login here: the row was
|
|
141
|
-
// this read misses a row committed while the retake runs, so a create that both stops before
|
|
142
|
-
// its `held` and commits within these few milliseconds stays a known gap.
|
|
129
|
+
// its login here: the row was written long before the claim went stale.
|
|
143
130
|
if (await isHeld(adapter, login)) return undefined;
|
|
144
131
|
|
|
145
132
|
return (await retaken(adapter, login, claim, now)) ? claim : undefined;
|
|
146
133
|
};
|
|
147
134
|
|
|
148
|
-
/**
|
|
149
|
-
* Claims `login` inside a create's own transaction, on its connection, answering the claim's
|
|
150
|
-
* token, or `undefined` once the transaction can only roll back. A claim taken beside the
|
|
151
|
-
* transaction would need a second connection from the pool the transaction holds one of, so a
|
|
152
|
-
* full pool would wait on itself for good. It retakes first and inserts last: an insert that
|
|
153
|
-
* loses aborts the transaction, which is the create's answer then, since the login is taken. A
|
|
154
|
-
* concurrent create's insert waits on this one's row until it commits or rolls back.
|
|
155
|
-
*/
|
|
156
|
-
export const claimInTransaction = async (adapter: Claims, login: string, now = Date.now()) => {
|
|
157
|
-
if (await isHeld(adapter, login)) return undefined;
|
|
158
|
-
|
|
159
|
-
const claim = crypto.randomUUID();
|
|
160
|
-
if (await retaken(adapter, login, claim, now)) return claim;
|
|
161
|
-
|
|
162
|
-
return (await inserted(adapter, login, claim, now)) ? claim : undefined;
|
|
163
|
-
};
|
|
164
|
-
|
|
165
135
|
/**
|
|
166
136
|
* Marks `login`'s claim held for the create whose token `claim` is, answering whether it still was
|
|
167
137
|
* that create's: a retake replaced the token, and then the create must fail. Its person or org is
|
|
@@ -1,13 +1,12 @@
|
|
|
1
|
+
import type { D1Database } from '@cloudflare/workers-types';
|
|
1
2
|
import type { Tokens } from '@ref0/git';
|
|
2
3
|
import * as Effect from 'effect/Effect';
|
|
3
|
-
import { sql } from 'kysely';
|
|
4
4
|
|
|
5
|
-
import
|
|
6
|
-
import { withPersonLocked } from './person-lock.ts';
|
|
5
|
+
import { guardedWrite } from './guarded-write.ts';
|
|
7
6
|
|
|
8
7
|
/** What forgetting a failed sign-up's person reads and writes: their rows, and Git's records. */
|
|
9
8
|
export interface UnbornCalls {
|
|
10
|
-
readonly database:
|
|
9
|
+
readonly database: D1Database;
|
|
11
10
|
readonly tokens: Pick<Tokens.Tokens['Service'], 'list'>;
|
|
12
11
|
/** Whether Git holds a repo under `<login>/`, or couldn't say. */
|
|
13
12
|
readonly ownsRepos: (login: string) => Effect.Effect<boolean>;
|
|
@@ -67,27 +66,23 @@ const gitHolds = (calls: UnbornCalls, login: string) =>
|
|
|
67
66
|
});
|
|
68
67
|
|
|
69
68
|
/**
|
|
70
|
-
* Whether an org names the person
|
|
71
|
-
* name them by ID or email, not login, so deleting them would cascade the membership away,
|
|
72
|
-
* next person to sign up with the login would find no trace of it.
|
|
69
|
+
* Whether an org names the person `?1`: a `member` row, or a pending `invitation` to their email.
|
|
70
|
+
* Both name them by ID or email, not login, so deleting them would cascade the membership away,
|
|
71
|
+
* and the next person to sign up with the login would find no trace of it.
|
|
73
72
|
*/
|
|
74
|
-
const inOrg = (id
|
|
75
|
-
sql`(exists (select 1 from "member" where "userId" = ${id}) or exists (select 1 from "invitation" where "status" = 'pending' and "email" = (select "email" from "user" where "id" = ${id})))`;
|
|
73
|
+
const inOrg = `(exists (select 1 from "member" where "userId" = ?1) or exists (select 1 from "invitation" where "status" = 'pending' and "email" = (select "email" from "user" where "id" = ?1)))`;
|
|
76
74
|
|
|
77
75
|
/**
|
|
78
|
-
* Answers 1 when an org names the person
|
|
79
|
-
*
|
|
76
|
+
* Answers 1 when an org names the person `?1`, and changes nothing: a write's count is the one
|
|
77
|
+
* answer a guarded write gives.
|
|
80
78
|
*/
|
|
81
|
-
const orgHolds =
|
|
82
|
-
sql`update "user" set "id" = "id" where "id" = ${id} and ${inOrg(id)}`;
|
|
79
|
+
const orgHolds = `update "user" set "id" = "id" where "id" = ?1 and ${inOrg}`;
|
|
83
80
|
|
|
84
81
|
/**
|
|
85
|
-
* The person's row, deleted only while no session row is theirs and no org names them: a
|
|
86
|
-
* or an org's owner that raced the cleanup keeps them.
|
|
87
|
-
* Postgres it sees a session or a member committed while it waited.
|
|
82
|
+
* The person `?1`'s row, deleted only while no session row is theirs and no org names them: a
|
|
83
|
+
* sign-in or an org's owner that raced the cleanup keeps them.
|
|
88
84
|
*/
|
|
89
|
-
const unbornDelete =
|
|
90
|
-
sql`delete from "user" where "id" = ${id} and not exists (select 1 from "session" where "userId" = ${id}) and not ${inOrg(id)}`;
|
|
85
|
+
const unbornDelete = `delete from "user" where "id" = ?1 and not exists (select 1 from "session" where "userId" = ?1) and not ${inOrg}`;
|
|
91
86
|
|
|
92
87
|
/**
|
|
93
88
|
* Deletes a person a failed passkey sign-up made, so their login is free to try again, unless
|
|
@@ -105,19 +100,14 @@ export const forgetUnborn = async (calls: UnbornCalls, person: Unborn): Promise<
|
|
|
105
100
|
const holds = await Effect.runPromise(gitHolds(calls, person.login));
|
|
106
101
|
if (holds !== undefined) return kept(holds);
|
|
107
102
|
|
|
108
|
-
const
|
|
109
|
-
|
|
103
|
+
const write = guardedWrite(calls.database);
|
|
104
|
+
if ((await write(orgHolds, person.id)) > 0) return kept('org');
|
|
110
105
|
|
|
111
|
-
|
|
106
|
+
if ((await write(unbornDelete, person.id)) === 0) return kept('session');
|
|
112
107
|
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
// Their login's claim goes with them, only once the delete answered their row (Q655).
|
|
117
|
-
await write(sql`delete from "owner_names" where "id" = ${person.login}`);
|
|
108
|
+
// Their passkeys and accounts go by D1's cascade. Their login's claim goes with them, only once
|
|
109
|
+
// the delete answered their row (Q655).
|
|
110
|
+
await write('delete from "owner_names" where "id" = ?1', person.login);
|
|
118
111
|
|
|
119
|
-
|
|
120
|
-
});
|
|
121
|
-
|
|
122
|
-
return outcome === 'forgotten' ? { kind: 'forgotten' } : kept(outcome);
|
|
112
|
+
return { kind: 'forgotten' };
|
|
123
113
|
};
|
package/app/pnpm-workspace.yaml
CHANGED
|
@@ -79,11 +79,7 @@ catalog:
|
|
|
79
79
|
'@tanstack/react-start': 1.169.7
|
|
80
80
|
# hast's types, for the app's Markdown tests, which read renders as hast. MIT.
|
|
81
81
|
'@types/hast': 3.0.5
|
|
82
|
-
#
|
|
83
|
-
# tests open local files with. MIT.
|
|
84
|
-
'@libsql/client': 0.18.0
|
|
85
|
-
'@libsql/kysely-libsql': 0.4.1
|
|
86
|
-
# better-sqlite3's types, for @ref0/infra/sqlite's tests over it. MIT.
|
|
82
|
+
# better-sqlite3's types, for the app's D1 stand-in over it. MIT.
|
|
87
83
|
'@types/better-sqlite3': 9.6.0
|
|
88
84
|
'@types/node': 24.19.4
|
|
89
85
|
# pg's types, for @ref0/infra/postgres's `listen` and its tests. MIT.
|
|
@@ -92,12 +88,9 @@ catalog:
|
|
|
92
88
|
# semver's types, for ref0-git's property over npm's version ranges, beside semver. MIT.
|
|
93
89
|
'@types/semver': 7.8.0
|
|
94
90
|
'@types/react-dom': 19.3.0
|
|
91
|
+
'@vitejs/plugin-react': 6.3.0
|
|
95
92
|
# Vercel Blob's SDK, which @ref0/infra/vercel's bucket wraps: V2 measured 2.9.0. Apache-2.0.
|
|
96
93
|
'@vercel/blob': 2.9.0
|
|
97
|
-
'@vitejs/plugin-react': 6.3.0
|
|
98
|
-
# Vercel Queues' client, which @ref0/infra/vercel's wake-up sends its messages with, at the release
|
|
99
|
-
# spike V1 ran. Apache-2.0.
|
|
100
|
-
'@vercel/queue': 0.11.0
|
|
101
94
|
# A JSON Schema validator, for ref0-git's test of the vercel.json init writes against Vercel's
|
|
102
95
|
# published schema. MIT.
|
|
103
96
|
ajv: 8.20.0
|
|
@@ -107,10 +100,6 @@ catalog:
|
|
|
107
100
|
# Exact, as ref0 pins it: npm ignores pnpm's lockfile, so a range would let a consumer pull a
|
|
108
101
|
# newer Effect than the one tested.
|
|
109
102
|
effect: 4.1.7
|
|
110
|
-
# Postgres 17's binaries, which @ref0/infra/postgres's tests start a server from, with no Docker.
|
|
111
|
-
# The package is MIT, and the binaries in its platform packages are under the PostgreSQL License.
|
|
112
|
-
# The tests never import it: on `beforeExit` it calls `process.exit(0)`.
|
|
113
|
-
embedded-postgres: 17.12.0-beta.19
|
|
114
103
|
# The app's people, sign-up and sessions, over D1 through its own Kysely dialect: from 1.7.19,
|
|
115
104
|
# published 10 Nov 2026 (Q639), its username plugin checks a login on a sign-up's transaction, not
|
|
116
105
|
# on a second connection beside it. Its username plugin is the login. No install scripts. MIT.
|
|
@@ -118,13 +107,19 @@ catalog:
|
|
|
118
107
|
# Better Auth's passkeys, at its own release, over @simplewebauthn/server and /browser. No install
|
|
119
108
|
# scripts. MIT.
|
|
120
109
|
'@better-auth/passkey': 1.7.28
|
|
121
|
-
# The
|
|
122
|
-
#
|
|
123
|
-
kysely: 0.29.9
|
|
124
|
-
# A SQLite driver @ref0/infra/sqlite's conformance runs on, beside node:sqlite. MIT.
|
|
110
|
+
# The SQLite the app's sign-in tests run D1's stand-in over, `signin/testing/sqlite-d1.ts`,
|
|
111
|
+
# where workerd's own D1 is too slow to start for each. MIT.
|
|
125
112
|
better-sqlite3: 13.2.0
|
|
126
|
-
#
|
|
127
|
-
|
|
113
|
+
# Postgres 17's binaries, which @ref0/infra/postgres's tests start a server from, with no Docker.
|
|
114
|
+
# The package is MIT, and the binaries in its platform packages are under the PostgreSQL License.
|
|
115
|
+
# The tests never import it: on `beforeExit` it calls `process.exit(0)`.
|
|
116
|
+
embedded-postgres: 17.12.0-beta.19
|
|
117
|
+
# node-postgres: the pool @ref0/infra/postgres's tests run on, and the client its `listen` opens,
|
|
118
|
+
# an optional peer loaded only when `listen` is given. MIT.
|
|
119
|
+
pg: 8.23.1
|
|
120
|
+
# The matcher Next.js 16.4.6 compiles next.config's rewrites with, which it bundles at this
|
|
121
|
+
# release: ref0-git's tests read init's rewrite with it, as Next does. MIT.
|
|
122
|
+
path-to-regexp: 6.3.0
|
|
128
123
|
# A DOM for the app's mounted tests, at ref0's version. MIT.
|
|
129
124
|
happy-dom: 21.0.4
|
|
130
125
|
# IndexedDB in Node, which the app's saved-read tests run code/saved-reads.ts's store on, since
|
|
@@ -139,21 +134,6 @@ catalog:
|
|
|
139
134
|
# The app's fonts, from its shadcn preset: Inter for text and Geist for headings, bundled. OFL-1.1.
|
|
140
135
|
'@fontsource-variable/geist': 5.3.0
|
|
141
136
|
'@fontsource-variable/inter': 5.3.0
|
|
142
|
-
nitro: 3.0.261103-rc
|
|
143
|
-
# A pack's zlib members deflated one at a time, through its low-level API alone, in @ref0/git's
|
|
144
|
-
# `protocol/bytes/deflate.ts`, and an entry's CRC-32 in `crc32.ts`; zlib-ng inflates them, in
|
|
145
|
-
# `protocol/bytes/zlib-ng/`. pako 3 exports no low-level API. Pinned at 2.1.0, which
|
|
146
|
-
# `deflate.ts` names: it resets the state it keeps between objects.
|
|
147
|
-
pako: 2.1.0
|
|
148
|
-
# node-postgres: the pool @ref0/infra/postgres's tests run on, and the client its `listen` opens,
|
|
149
|
-
# an optional peer loaded only when `listen` is given. MIT.
|
|
150
|
-
pg: 8.23.1
|
|
151
|
-
# The matcher Next.js 16.4.6 compiles next.config's rewrites with, which it bundles at this
|
|
152
|
-
# release: ref0-git's tests read init's rewrite with it, as Next does. MIT.
|
|
153
|
-
path-to-regexp: 6.3.0
|
|
154
|
-
# The h3 Nuxt 4.6 serves with: ref0-git's tests run init's Nuxt routes and middleware through its
|
|
155
|
-
# toWebRequest, as Nitro's Node server does. MIT.
|
|
156
|
-
h3: 1.15.11
|
|
157
137
|
class-variance-authority: 0.7.1
|
|
158
138
|
# shadcn's `cn`: Tailwind classes merged, at ref0's version. MIT.
|
|
159
139
|
cn: 0.5.2
|