ref0-git 0.14.0 → 0.25.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/generated/git-rest.ts +170 -3
- package/app/apps/github/generated/routeTree.gen.ts +42 -21
- package/app/apps/github/src/clients/host-calls.ts +59 -34
- package/app/apps/github/src/components/states/states.tsx +1 -19
- package/app/apps/github/src/components/view-tabs.tsx +6 -2
- package/app/apps/github/src/context/context.ts +7 -4
- package/app/apps/github/src/context/host.ts +72 -36
- package/app/apps/github/src/issues/issue-list.tsx +11 -3
- package/app/apps/github/src/issues/issues.ts +11 -8
- package/app/apps/github/src/lists/across-list.tsx +358 -0
- package/app/apps/github/src/lists/across.ts +239 -0
- package/app/apps/github/src/names/owners.ts +3 -1
- package/app/apps/github/src/palette/targets.ts +3 -2
- package/app/apps/github/src/policy.ts +23 -30
- package/app/apps/github/src/private-repos.ts +24 -24
- package/app/apps/github/src/pulls/pulls.ts +2 -1
- package/app/apps/github/src/routes/-nav.tsx +40 -12
- package/app/apps/github/src/routes/issues.tsx +15 -0
- package/app/apps/github/src/routes/orgs/$owner/-org-list.ts +31 -0
- package/app/apps/github/src/routes/orgs/$owner/issues.tsx +10 -3
- package/app/apps/github/src/routes/orgs/$owner/pulls.tsx +10 -3
- package/app/apps/github/src/routes/pulls.tsx +15 -0
- package/app/apps/github/src/seed/seed.ts +104 -52
- package/app/apps/github/src/server.ts +17 -7
- package/app/apps/github/src/signin/approval.ts +24 -16
- package/app/apps/github/src/signin/attempts.ts +1 -1
- package/app/apps/github/src/signin/auth.ts +27 -19
- package/app/apps/github/src/signin/device-prompt.ts +1 -2
- package/app/apps/github/src/signin/revoke.ts +9 -17
- package/app/apps/github/src/signin/unborn.ts +33 -20
- package/app/pnpm-workspace.yaml +2 -2
- package/dist/cli/setup.mjs +3 -3
- package/dist/cli/sign-in.mjs +2 -3
- package/dist/init/app.mjs +5 -4
- package/dist/init/cloudflare.mjs +14 -8
- package/dist/init/create.mjs +1 -1
- package/dist/init/git-module.mjs +13 -5
- package/dist/init/metadata.mjs +1 -1
- package/dist/init/node.mjs +1 -1
- package/dist/init/stores.mjs +1 -1
- package/dist/init/template.mjs +3 -0
- package/dist/init/templates.mjs +34 -21
- package/dist/init/versions.mjs +1 -1
- package/package.json +5 -5
- package/app/apps/github/src/routes/orgs/$owner/releases.tsx +0 -8
|
@@ -1,8 +1,15 @@
|
|
|
1
1
|
import { createFileRoute } from '@tanstack/react-router';
|
|
2
2
|
|
|
3
|
-
import {
|
|
3
|
+
import { OrgTab } from '../../../lists/across-list.tsx';
|
|
4
|
+
import { orgListed } from './-org-list.ts';
|
|
4
5
|
|
|
5
|
-
/** An
|
|
6
|
+
/** An org's open issues across its repos the viewer may read. */
|
|
6
7
|
export const Route = createFileRoute('/orgs/$owner/issues')({
|
|
7
|
-
|
|
8
|
+
loader: ({ params, context }) => orgListed(params.owner, context, '/issues'),
|
|
9
|
+
component: function OrgIssues() {
|
|
10
|
+
const { org } = Route.useLoaderData();
|
|
11
|
+
const { viewer } = Route.useRouteContext();
|
|
12
|
+
|
|
13
|
+
return <OrgTab signedIn={viewer !== null} org={org} kind="issue" />;
|
|
14
|
+
},
|
|
8
15
|
});
|
|
@@ -1,8 +1,15 @@
|
|
|
1
1
|
import { createFileRoute } from '@tanstack/react-router';
|
|
2
2
|
|
|
3
|
-
import {
|
|
3
|
+
import { OrgTab } from '../../../lists/across-list.tsx';
|
|
4
|
+
import { orgListed } from './-org-list.ts';
|
|
4
5
|
|
|
5
|
-
/** An
|
|
6
|
+
/** An org's open pull requests across its repos the viewer may read. */
|
|
6
7
|
export const Route = createFileRoute('/orgs/$owner/pulls')({
|
|
7
|
-
|
|
8
|
+
loader: ({ params, context }) => orgListed(params.owner, context, '/pulls'),
|
|
9
|
+
component: function OrgPulls() {
|
|
10
|
+
const { org } = Route.useLoaderData();
|
|
11
|
+
const { viewer } = Route.useRouteContext();
|
|
12
|
+
|
|
13
|
+
return <OrgTab signedIn={viewer !== null} org={org} kind="pull" />;
|
|
14
|
+
},
|
|
8
15
|
});
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { createFileRoute } from '@tanstack/react-router';
|
|
2
|
+
|
|
3
|
+
import { pullFilters, readYourSearch, yourFilterOf } from '../lists/across.ts';
|
|
4
|
+
import { YourPulls } from '../lists/across-list.tsx';
|
|
5
|
+
|
|
6
|
+
/** Your pull requests, under the filter its URL names, the viewer's across every repo they may read. */
|
|
7
|
+
export const Route = createFileRoute('/pulls')({
|
|
8
|
+
validateSearch: readYourSearch,
|
|
9
|
+
component: function Pulls() {
|
|
10
|
+
const { viewer } = Route.useRouteContext();
|
|
11
|
+
const filter = yourFilterOf(pullFilters, Route.useSearch().filter);
|
|
12
|
+
|
|
13
|
+
return <YourPulls signedIn={viewer !== null} filter={filter} />;
|
|
14
|
+
},
|
|
15
|
+
});
|
|
@@ -215,6 +215,11 @@ const decodeCreated = Schema.decodeUnknownSync(Schema.fromJsonString(CreatedItem
|
|
|
215
215
|
|
|
216
216
|
const decodeDescribed = Schema.decodeUnknownSync(Schema.fromJsonString(Described));
|
|
217
217
|
|
|
218
|
+
/** A repo's labels, by their names. */
|
|
219
|
+
const decodeNamed = Schema.decodeUnknownSync(
|
|
220
|
+
Schema.fromJsonString(Schema.Array(Schema.Struct({ name: Schema.String }))),
|
|
221
|
+
);
|
|
222
|
+
|
|
218
223
|
/** A repo's milestones, by their titles. */
|
|
219
224
|
const decodeTitled = Schema.decodeUnknownSync(
|
|
220
225
|
Schema.fromJsonString(
|
|
@@ -233,17 +238,21 @@ interface Cast {
|
|
|
233
238
|
readonly call: (person: Person) => Call;
|
|
234
239
|
}
|
|
235
240
|
|
|
241
|
+
/** Ensures repo `id` exists with `description`; returns `true` when this run just created it. */
|
|
236
242
|
const ensureRepo = async (cast: Cast, id: RepoName, path: string, description: string) => {
|
|
237
243
|
const found = await cast.adminCall('GET', path);
|
|
244
|
+
const created = found.status === 404;
|
|
238
245
|
|
|
239
|
-
if (
|
|
246
|
+
if (created) {
|
|
240
247
|
value(await answer(cast.admin.repos.create({ name: id, public: true })), `make ${id}`);
|
|
241
248
|
} else if (decodeDescribed(found.text).description === (description || null)) {
|
|
242
|
-
return;
|
|
249
|
+
return false;
|
|
243
250
|
}
|
|
244
251
|
|
|
245
252
|
// A repo's settings are its admin's to change, which a person's `write` token isn't.
|
|
246
253
|
await cast.adminCall('PATCH', path, { description });
|
|
254
|
+
|
|
255
|
+
return created;
|
|
247
256
|
};
|
|
248
257
|
|
|
249
258
|
/** Each commit in turn on `parent`, as `git commit` makes them; their IDs, oldest first. */
|
|
@@ -278,7 +287,13 @@ const commitAll = async (
|
|
|
278
287
|
* makes the same objects, then each branch and the tag that isn't there yet, at once. Checks the
|
|
279
288
|
* expected refs first so a resumed seed skips rebuilding commits and tags once every ref exists.
|
|
280
289
|
*/
|
|
281
|
-
const pushHistory = async (
|
|
290
|
+
const pushHistory = async (
|
|
291
|
+
git: RestClientCalls,
|
|
292
|
+
id: RepoName,
|
|
293
|
+
repo: SeedRepo,
|
|
294
|
+
since: number,
|
|
295
|
+
fresh: boolean,
|
|
296
|
+
) => {
|
|
282
297
|
const { release } = repo;
|
|
283
298
|
const targets: readonly {
|
|
284
299
|
readonly ref: RefName;
|
|
@@ -314,12 +329,19 @@ const pushHistory = async (git: RestClientCalls, id: RepoName, repo: SeedRepo, s
|
|
|
314
329
|
|
|
315
330
|
const missingRefs = new Set<RefName>();
|
|
316
331
|
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
332
|
+
if (fresh) {
|
|
333
|
+
for (const { ref } of targets) missingRefs.add(ref);
|
|
334
|
+
} else {
|
|
335
|
+
const checks = await Promise.all(
|
|
336
|
+
targets.map(async ({ ref }) => ({ ref, found: await answer(git.refs.get(id, { ref })) })),
|
|
337
|
+
);
|
|
338
|
+
|
|
339
|
+
for (const { ref, found } of checks) {
|
|
340
|
+
if (found.kind === 'success') continue;
|
|
320
341
|
|
|
321
|
-
|
|
322
|
-
|
|
342
|
+
if (!missing(found.error)) value(found, `read ${ref}`);
|
|
343
|
+
missingRefs.add(ref);
|
|
344
|
+
}
|
|
323
345
|
}
|
|
324
346
|
|
|
325
347
|
if (missingRefs.size === 0) return;
|
|
@@ -341,21 +363,39 @@ const pushHistory = async (git: RestClientCalls, id: RepoName, repo: SeedRepo, s
|
|
|
341
363
|
};
|
|
342
364
|
|
|
343
365
|
/** Each label that isn't there yet, made by Pip. */
|
|
344
|
-
const makeLabels = async (call: Call, path: string, repo: SeedRepo) => {
|
|
366
|
+
const makeLabels = async (call: Call, path: string, repo: SeedRepo, fresh: boolean) => {
|
|
367
|
+
if (fresh) {
|
|
368
|
+
for (const label of repo.labels) await call('POST', `${path}/labels`, { ...label });
|
|
369
|
+
|
|
370
|
+
return;
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
const existing = new Set(
|
|
374
|
+
decodeNamed((await call('GET', `${path}/labels?per_page=100`)).text).map((label) =>
|
|
375
|
+
label.name.toLowerCase(),
|
|
376
|
+
),
|
|
377
|
+
);
|
|
378
|
+
|
|
345
379
|
for (const label of repo.labels) {
|
|
380
|
+
if (existing.has(label.name.toLowerCase())) continue;
|
|
381
|
+
|
|
382
|
+
// Labels past page 1 (up to 1,000 per repo) aren't in the batch; confirm by name before POST.
|
|
346
383
|
const found = await call('GET', `${path}/labels/${encodeURIComponent(label.name)}`);
|
|
347
384
|
if (found.status === 404) await call('POST', `${path}/labels`, { ...label });
|
|
348
385
|
}
|
|
349
386
|
};
|
|
350
387
|
|
|
351
388
|
/** Each of the repo's milestones' numbers, by its title: made where it isn't there yet. */
|
|
352
|
-
const makeMilestones = async (call: Call, path: string, repo: SeedRepo) => {
|
|
353
|
-
const
|
|
354
|
-
(await call('GET', `${path}/milestones?state=all&per_page=100`)).text,
|
|
355
|
-
);
|
|
389
|
+
const makeMilestones = async (call: Call, path: string, repo: SeedRepo, fresh: boolean) => {
|
|
390
|
+
const titles = repo.milestones ?? [];
|
|
356
391
|
const numbers = new Map<string, number>();
|
|
392
|
+
if (titles.length === 0) return numbers;
|
|
357
393
|
|
|
358
|
-
|
|
394
|
+
const listed = fresh
|
|
395
|
+
? []
|
|
396
|
+
: decodeTitled((await call('GET', `${path}/milestones?state=all&per_page=100`)).text);
|
|
397
|
+
|
|
398
|
+
for (const title of titles) {
|
|
359
399
|
const number =
|
|
360
400
|
listed.find((milestone) => milestone.title === title)?.number ??
|
|
361
401
|
decodeNumbered((await call('POST', `${path}/milestones`, { title })).text).number;
|
|
@@ -414,9 +454,11 @@ const openPull = async (
|
|
|
414
454
|
);
|
|
415
455
|
|
|
416
456
|
const reviewer = cast.call(pull.review.by);
|
|
417
|
-
const reviews = await reviewer('GET', `${path}/pulls/${item.number}/reviews`);
|
|
418
457
|
|
|
419
|
-
if (
|
|
458
|
+
if (
|
|
459
|
+
found === undefined ||
|
|
460
|
+
!wrote((await reviewer('GET', `${path}/pulls/${item.number}/reviews`)).text, pull.review.by)
|
|
461
|
+
) {
|
|
420
462
|
const { event, body } = pull.review;
|
|
421
463
|
await reviewer('POST', `${path}/pulls/${item.number}/reviews`, { event, body });
|
|
422
464
|
}
|
|
@@ -435,31 +477,33 @@ const openIssue = async (
|
|
|
435
477
|
found: ExistingIssue | undefined,
|
|
436
478
|
) => {
|
|
437
479
|
const author = cast.call(issue.by);
|
|
480
|
+
const milestone = milestones.get(issue.milestone ?? '');
|
|
481
|
+
const base = { title: issue.title, body: issue.body, labels: issue.labels };
|
|
438
482
|
const item =
|
|
439
483
|
found ??
|
|
440
484
|
decodeCreated(
|
|
441
485
|
(
|
|
442
|
-
await author(
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
486
|
+
await author(
|
|
487
|
+
'POST',
|
|
488
|
+
`${path}/issues`,
|
|
489
|
+
milestone === undefined ? base : { ...base, milestone },
|
|
490
|
+
)
|
|
447
491
|
).text,
|
|
448
492
|
);
|
|
449
|
-
await placeOn(
|
|
450
|
-
author,
|
|
451
|
-
path,
|
|
452
|
-
item.number,
|
|
453
|
-
item.milestone?.number ?? null,
|
|
454
|
-
milestones.get(issue.milestone ?? ''),
|
|
455
|
-
);
|
|
456
493
|
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
494
|
+
await placeOn(author, path, item.number, item.milestone?.number ?? null, milestone);
|
|
495
|
+
|
|
496
|
+
if (issue.comments.length > 0) {
|
|
497
|
+
const existingComments =
|
|
498
|
+
found === undefined
|
|
499
|
+
? undefined
|
|
500
|
+
: (await author('GET', `${path}/issues/${item.number}/comments?per_page=100`)).text;
|
|
460
501
|
|
|
461
|
-
|
|
462
|
-
|
|
502
|
+
for (const comment of issue.comments) {
|
|
503
|
+
if (existingComments === undefined || !wrote(existingComments, comment.by, comment.body)) {
|
|
504
|
+
const commenter = cast.call(comment.by);
|
|
505
|
+
await commenter('POST', `${path}/issues/${item.number}/comments`, { body: comment.body });
|
|
506
|
+
}
|
|
463
507
|
}
|
|
464
508
|
}
|
|
465
509
|
|
|
@@ -471,12 +515,14 @@ const openIssue = async (
|
|
|
471
515
|
}
|
|
472
516
|
};
|
|
473
517
|
|
|
474
|
-
const publishRelease = async (call: Call, path: string, repo: SeedRepo) => {
|
|
518
|
+
const publishRelease = async (call: Call, path: string, repo: SeedRepo, fresh: boolean) => {
|
|
475
519
|
const { release } = repo;
|
|
476
520
|
if (release === undefined) return;
|
|
477
521
|
|
|
478
|
-
|
|
479
|
-
|
|
522
|
+
if (!fresh) {
|
|
523
|
+
const found = await call('GET', `${path}/releases/tags/${encodeURIComponent(release.tag)}`);
|
|
524
|
+
if (found.status !== 404) return;
|
|
525
|
+
}
|
|
480
526
|
|
|
481
527
|
await call('POST', `${path}/releases`, {
|
|
482
528
|
tag_name: release.tag,
|
|
@@ -490,19 +536,22 @@ const seedRepo = async (cast: Cast, repo: SeedRepo, since: number) => {
|
|
|
490
536
|
const path = `/repos/${id}`;
|
|
491
537
|
const asPip = cast.call(pip);
|
|
492
538
|
|
|
493
|
-
await ensureRepo(cast, id, path, repo.description);
|
|
494
|
-
await pushHistory(cast.git(pip), id, repo, since);
|
|
495
|
-
await makeLabels(asPip, path, repo);
|
|
496
|
-
const milestones = await makeMilestones(asPip, path, repo);
|
|
539
|
+
const fresh = await ensureRepo(cast, id, path, repo.description);
|
|
540
|
+
await pushHistory(cast.git(pip), id, repo, since, fresh);
|
|
541
|
+
await makeLabels(asPip, path, repo, fresh);
|
|
542
|
+
const milestones = await makeMilestones(asPip, path, repo, fresh);
|
|
497
543
|
|
|
498
544
|
if (repo.pulls.length > 0) {
|
|
499
|
-
const listedPulls = decodePulls(
|
|
500
|
-
(await asPip('GET', `${path}/pulls?state=all&per_page=100`)).text,
|
|
501
|
-
);
|
|
502
545
|
const pullsByHead = new Map<string, ExistingPull>();
|
|
503
546
|
|
|
504
|
-
|
|
505
|
-
|
|
547
|
+
if (!fresh) {
|
|
548
|
+
const listedPulls = decodePulls(
|
|
549
|
+
(await asPip('GET', `${path}/pulls?state=all&per_page=100`)).text,
|
|
550
|
+
);
|
|
551
|
+
|
|
552
|
+
for (const item of listedPulls) {
|
|
553
|
+
if (!pullsByHead.has(item.head.label)) pullsByHead.set(item.head.label, item);
|
|
554
|
+
}
|
|
506
555
|
}
|
|
507
556
|
|
|
508
557
|
for (const pull of repo.pulls) {
|
|
@@ -511,14 +560,17 @@ const seedRepo = async (cast: Cast, repo: SeedRepo, since: number) => {
|
|
|
511
560
|
}
|
|
512
561
|
|
|
513
562
|
if (repo.issues.length > 0) {
|
|
514
|
-
const listedIssues = decodeIssues(
|
|
515
|
-
(await asPip('GET', `${path}/issues?state=all&per_page=100`)).text,
|
|
516
|
-
);
|
|
517
563
|
const issuesByTitle = new Map<string, ExistingIssue>();
|
|
518
564
|
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
565
|
+
if (!fresh) {
|
|
566
|
+
const listedIssues = decodeIssues(
|
|
567
|
+
(await asPip('GET', `${path}/issues?state=all&per_page=100`)).text,
|
|
568
|
+
);
|
|
569
|
+
|
|
570
|
+
for (const item of listedIssues) {
|
|
571
|
+
if (item.pull_request === undefined && !issuesByTitle.has(item.title)) {
|
|
572
|
+
issuesByTitle.set(item.title, item);
|
|
573
|
+
}
|
|
522
574
|
}
|
|
523
575
|
}
|
|
524
576
|
|
|
@@ -527,7 +579,7 @@ const seedRepo = async (cast: Cast, repo: SeedRepo, since: number) => {
|
|
|
527
579
|
}
|
|
528
580
|
}
|
|
529
581
|
|
|
530
|
-
await publishRelease(asPip, path, repo);
|
|
582
|
+
await publishRelease(asPip, path, repo, fresh);
|
|
531
583
|
};
|
|
532
584
|
|
|
533
585
|
const members: ReadonlySet<string> = new Set(org.members.map((person) => person.login));
|
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
import { Git } from '@ref0/git';
|
|
1
|
+
import { Git, Repos } from '@ref0/git';
|
|
2
2
|
import { Records } from '@ref0/infra';
|
|
3
3
|
import handler, { createServerEntry } from '@tanstack/react-start/server-entry';
|
|
4
4
|
import * as Effect from 'effect/Effect';
|
|
5
5
|
|
|
6
6
|
import { workersFetch } from './camo/fetch.ts';
|
|
7
7
|
import { imageProxy } from './camo/proxy.ts';
|
|
8
|
-
import type { AppContext } from './context/context.ts';
|
|
9
|
-
import { auth, directory, env, pictures, policy,
|
|
10
|
-
import {
|
|
8
|
+
import type { AppContext, HostGit } from './context/context.ts';
|
|
9
|
+
import { auth, directory, env, host, pictures, policy, runtime, sessionKey } from './git.ts';
|
|
10
|
+
import { routeOn, tokensOn } from './context/host.ts';
|
|
11
11
|
import { ingressOf } from './ingress.ts';
|
|
12
12
|
import { hostRules } from './policy.ts';
|
|
13
13
|
import { shownRules } from './settings/rules.ts';
|
|
@@ -30,8 +30,11 @@ const gate = deviceAttemptGate({
|
|
|
30
30
|
/** The front door, then Git's routes, the drop-in's among them, for outside and the app alike. */
|
|
31
31
|
const { owned, api } = ingressOf({ gate, git: routeOn(runtime) });
|
|
32
32
|
|
|
33
|
-
/** Git's own calls, as host calls read them. */
|
|
34
|
-
const git = {
|
|
33
|
+
/** Git's own calls, as host calls read them, each an Effect, which the server function runs. */
|
|
34
|
+
const git = {
|
|
35
|
+
repos: { list: (input) => host(Repos.list(input)) },
|
|
36
|
+
tokens: tokensOn(host),
|
|
37
|
+
} satisfies HostGit;
|
|
35
38
|
|
|
36
39
|
/**
|
|
37
40
|
* The image proxy (Q860): a text's remote images load through `/_camo/`, and the pages' CSP loads
|
|
@@ -108,7 +111,14 @@ export default createServerEntry({
|
|
|
108
111
|
session: async (each) => sessionFrom(await auth(), sessionKey)(each),
|
|
109
112
|
endSession: async (each) => sessionEnder(await auth())(each),
|
|
110
113
|
sessionCookie: async () => sessionCookieOf(await auth())(),
|
|
111
|
-
|
|
114
|
+
// The server functions' own edge: who a credential names, as their Promise reads it.
|
|
115
|
+
identify: (each) =>
|
|
116
|
+
runtime.runPromise(
|
|
117
|
+
Effect.match(Git.identify(each), {
|
|
118
|
+
onFailure: (error) => ({ kind: 'error', error }) as const,
|
|
119
|
+
onSuccess: (value) => ({ kind: 'success', value }) as const,
|
|
120
|
+
}),
|
|
121
|
+
),
|
|
112
122
|
authorize: policy.authorize,
|
|
113
123
|
authorizeGitHub: policy.authorizeGitHub,
|
|
114
124
|
rules,
|
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import * as Effect from 'effect/Effect';
|
|
2
|
+
import * as Option from 'effect/Option';
|
|
2
3
|
import type {
|
|
3
4
|
ApprovalPageInput,
|
|
4
5
|
Caller,
|
|
5
6
|
ConfirmApprovalPage,
|
|
6
7
|
DeviceApproval,
|
|
7
8
|
DeviceCodePage,
|
|
8
|
-
|
|
9
|
+
GitError,
|
|
9
10
|
} from '@ref0/git';
|
|
10
11
|
|
|
11
12
|
import { ownerProblem } from '../names/owners.ts';
|
|
@@ -18,8 +19,11 @@ export interface StarterApprovalOptions {
|
|
|
18
19
|
readonly auth: () => Promise<StarterAuth>;
|
|
19
20
|
/** The key prompts and sessions' Git tokens are sealed under, from `SESSION_SECRET`. */
|
|
20
21
|
readonly sealKey: Promise<CryptoKey>;
|
|
21
|
-
/**
|
|
22
|
-
|
|
22
|
+
/**
|
|
23
|
+
* Git's `identify`, in the page's own fiber: whether the session's Git token still names its
|
|
24
|
+
* person.
|
|
25
|
+
*/
|
|
26
|
+
readonly identify: (request: Request) => Effect.Effect<Caller, GitError>;
|
|
23
27
|
/** `APP_ORIGIN`: the one origin a decision is posted from. */
|
|
24
28
|
readonly origin: string;
|
|
25
29
|
/** How long a token the person approves lasts. */
|
|
@@ -92,35 +96,40 @@ const fromApp = (request: Request, options: StarterApprovalOptions) =>
|
|
|
92
96
|
request.method === 'POST' && request.headers.get('origin') === new URL(options.origin).origin;
|
|
93
97
|
|
|
94
98
|
/** The session's login, when the browser has one and its Git token still names that person. */
|
|
95
|
-
const signedIn =
|
|
96
|
-
|
|
99
|
+
const signedIn = Effect.fnUntraced(function* (
|
|
100
|
+
input: ConfirmApprovalPage,
|
|
101
|
+
options: StarterApprovalOptions,
|
|
102
|
+
) {
|
|
103
|
+
const session = yield* Effect.promise(() => sessionOf(input.request, options));
|
|
97
104
|
if (session === undefined) return undefined;
|
|
98
105
|
|
|
99
106
|
const headers = { authorization: `Bearer ${session.token}` };
|
|
100
|
-
const held =
|
|
101
|
-
const actor = held
|
|
107
|
+
const held = yield* Effect.option(options.identify(new Request(options.origin, { headers })));
|
|
108
|
+
const actor = Option.isSome(held) ? held.value.actor : undefined;
|
|
102
109
|
|
|
103
110
|
return actor?.anonymous !== true && actor?.id === session.login ? session.login : undefined;
|
|
104
|
-
};
|
|
111
|
+
});
|
|
105
112
|
|
|
106
113
|
/**
|
|
107
114
|
* A code posted from the Code field, by a live session: Git's prompt, sealed for that person. A
|
|
108
115
|
* link or another site's form that names a code gets the empty Code field, so no one is shown a
|
|
109
116
|
* code they didn't type.
|
|
110
117
|
*/
|
|
111
|
-
const shown =
|
|
118
|
+
const shown = Effect.fnUntraced(function* (
|
|
112
119
|
input: ConfirmApprovalPage,
|
|
113
120
|
prompt: DeviceApproval,
|
|
114
121
|
options: StarterApprovalOptions,
|
|
115
|
-
)
|
|
116
|
-
const login = fromApp(input.request, options) ?
|
|
122
|
+
) {
|
|
123
|
+
const login = fromApp(input.request, options) ? yield* signedIn(input, options) : undefined;
|
|
117
124
|
if (login === undefined) return seeOther(codePage);
|
|
118
125
|
|
|
119
126
|
const grant = { login, days: options.tokenDays };
|
|
120
|
-
const sealed =
|
|
127
|
+
const sealed = yield* Effect.promise(async () =>
|
|
128
|
+
sealPrompt(await options.sealKey, prompt, grant, Date.now()),
|
|
129
|
+
);
|
|
121
130
|
|
|
122
131
|
return seeOther(codePageWith({ prompt: sealed }));
|
|
123
|
-
};
|
|
132
|
+
});
|
|
124
133
|
|
|
125
134
|
/**
|
|
126
135
|
* A decision posted from the Card: from the app's own page, by a live session, with the sealed
|
|
@@ -140,7 +149,7 @@ const decided = Effect.fnUntraced(function* (
|
|
|
140
149
|
);
|
|
141
150
|
if (!fromApp(input.request, options)) return signInFirst;
|
|
142
151
|
|
|
143
|
-
const login = yield*
|
|
152
|
+
const login = yield* signedIn(input, options);
|
|
144
153
|
if (login === undefined) return signInFirst;
|
|
145
154
|
|
|
146
155
|
const opened = yield* Effect.promise(async () =>
|
|
@@ -185,8 +194,7 @@ export const starterApprovalPage = (options: StarterApprovalOptions) =>
|
|
|
185
194
|
input.request.method === 'POST'
|
|
186
195
|
? new URLSearchParams(yield* Effect.promise(() => input.request.text()))
|
|
187
196
|
: undefined;
|
|
188
|
-
if (form?.has('decision') !== true)
|
|
189
|
-
return yield* Effect.promise(() => shown(input, prompt, options));
|
|
197
|
+
if (form?.has('decision') !== true) return yield* shown(input, prompt, options);
|
|
190
198
|
|
|
191
199
|
return yield* decided(input, prompt, form, options);
|
|
192
200
|
});
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { D1Database } from '@cloudflare/workers-types';
|
|
2
|
-
import type {
|
|
2
|
+
import type { Tokens } from '@ref0/git';
|
|
3
3
|
import { passkey } from '@better-auth/passkey';
|
|
4
4
|
import {
|
|
5
5
|
type Auth,
|
|
@@ -12,6 +12,8 @@ import {
|
|
|
12
12
|
import { APIError, createAuthMiddleware, getSessionFromCtx } from 'better-auth/api';
|
|
13
13
|
import { organization } from 'better-auth/plugins/organization';
|
|
14
14
|
import { username } from 'better-auth/plugins/username';
|
|
15
|
+
import * as Effect from 'effect/Effect';
|
|
16
|
+
import * as Exit from 'effect/Exit';
|
|
15
17
|
import * as Option from 'effect/Option';
|
|
16
18
|
import * as Schema from 'effect/Schema';
|
|
17
19
|
import type { Dialect } from 'kysely';
|
|
@@ -75,10 +77,13 @@ export interface StarterAuthOptions {
|
|
|
75
77
|
readonly signUp: boolean;
|
|
76
78
|
/** The key a session's Git token is sealed under in D1. */
|
|
77
79
|
readonly key: Promise<CryptoKey>;
|
|
78
|
-
/**
|
|
79
|
-
|
|
80
|
+
/**
|
|
81
|
+
* Git's tokens, which each session mints its own from, and revokes at its end, each an Effect
|
|
82
|
+
* Better Auth's hook runs.
|
|
83
|
+
*/
|
|
84
|
+
readonly tokens: Tokens.Tokens['Service'];
|
|
80
85
|
/** Whether Git holds a repo under `<login>/`, or couldn't say: such a person is never forgotten. */
|
|
81
|
-
readonly ownsRepos: (login: string) =>
|
|
86
|
+
readonly ownsRepos: (login: string) => Effect.Effect<boolean>;
|
|
82
87
|
/**
|
|
83
88
|
* The header Better Auth keys its rate limits on, which only the platform sets: Cloudflare's
|
|
84
89
|
* `cf-connecting-ip`, or on Node `clientIpHeader`, which the server sets from the caller's
|
|
@@ -581,16 +586,18 @@ export const starterAuth = (options: StarterAuthOptions): Auth => {
|
|
|
581
586
|
const login = loginOf(namedOf(user));
|
|
582
587
|
if (context === undefined || login === undefined) return;
|
|
583
588
|
|
|
584
|
-
const made = await
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
589
|
+
const made = await Effect.runPromiseExit(
|
|
590
|
+
options.tokens.create(
|
|
591
|
+
{ prefix: '' },
|
|
592
|
+
{
|
|
593
|
+
scope: 'write',
|
|
594
|
+
subject: { id: login },
|
|
595
|
+
expiresAt: session.expiresAt,
|
|
596
|
+
key: sessionKeyOf(session.id),
|
|
597
|
+
},
|
|
598
|
+
),
|
|
592
599
|
);
|
|
593
|
-
if (made
|
|
600
|
+
if (Exit.isFailure(made)) return;
|
|
594
601
|
|
|
595
602
|
const gitToken = await seal(await options.key, purpose, made.value.secret);
|
|
596
603
|
const held = await context.adapter.update({
|
|
@@ -604,9 +611,10 @@ export const starterAuth = (options: StarterAuthOptions): Auth => {
|
|
|
604
611
|
// Only a row with no token takes it; `returning` answers nothing for one it skipped.
|
|
605
612
|
if (held !== null && held !== undefined) return;
|
|
606
613
|
|
|
607
|
-
const revoked = await
|
|
608
|
-
|
|
609
|
-
|
|
614
|
+
const revoked = await Effect.runPromiseExit(
|
|
615
|
+
revokeRetrying(options.tokens.revoke(made.value.id)),
|
|
616
|
+
);
|
|
617
|
+
if (Exit.isFailure(revoked)) console.error(`Git didn't revoke token ${made.value.id}`);
|
|
610
618
|
},
|
|
611
619
|
},
|
|
612
620
|
delete: {
|
|
@@ -615,10 +623,10 @@ export const starterAuth = (options: StarterAuthOptions): Auth => {
|
|
|
615
623
|
// on it, and one still to come is refused until the session would end.
|
|
616
624
|
const key = sessionKeyOf(session.id);
|
|
617
625
|
const until = session.expiresAt;
|
|
618
|
-
const revoked = await
|
|
619
|
-
options.tokens.revokeKey({ prefix: '' }, key, { until }),
|
|
626
|
+
const revoked = await Effect.runPromiseExit(
|
|
627
|
+
revokeRetrying(options.tokens.revokeKey({ prefix: '' }, key, { until })),
|
|
620
628
|
);
|
|
621
|
-
if (revoked
|
|
629
|
+
if (Exit.isFailure(revoked)) console.error(`Git didn't revoke session token ${key}`);
|
|
622
630
|
},
|
|
623
631
|
},
|
|
624
632
|
},
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import type { DeviceApproval, GrantScope } from '@ref0/git';
|
|
2
|
-
import { ghClientId, ghWriteScope } from '@ref0/git
|
|
3
|
-
import { grantScopes } from '@ref0/git';
|
|
2
|
+
import { ghClientId, ghWriteScope, grantScopes } from '@ref0/git';
|
|
4
3
|
import * as Option from 'effect/Option';
|
|
5
4
|
import * as Schema from 'effect/Schema';
|
|
6
5
|
|
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
import type { GitError
|
|
1
|
+
import type { GitError } from '@ref0/git';
|
|
2
|
+
import * as Effect from 'effect/Effect';
|
|
3
|
+
import * as Schedule from 'effect/Schedule';
|
|
2
4
|
|
|
3
5
|
/**
|
|
4
6
|
* The most times a session's token revoke is sent again after other writes to the same tokens won
|
|
@@ -14,23 +16,13 @@ export const revokePauseFirst = 250;
|
|
|
14
16
|
const tooManyWrites = (error: GitError) =>
|
|
15
17
|
error.code === 'UNAVAILABLE' && error.context?.reason === 'too-many-writes';
|
|
16
18
|
|
|
17
|
-
const paused = (ms: number) => new Promise<void>((resolve) => setTimeout(resolve, ms));
|
|
18
|
-
|
|
19
19
|
/**
|
|
20
20
|
* `revoke`, sent again while Git answers that other writes won every try, `revokeTriesMost` times
|
|
21
21
|
* at most. Any other failure answers at once, as does the last try's.
|
|
22
22
|
*/
|
|
23
|
-
export const revokeRetrying =
|
|
24
|
-
revoke
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
if (revoked.kind === 'success' || !tooManyWrites(revoked.error)) return revoked;
|
|
30
|
-
|
|
31
|
-
await paused(revokePauseFirst * 2 ** tried);
|
|
32
|
-
revoked = await revoke();
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
return revoked;
|
|
36
|
-
};
|
|
23
|
+
export const revokeRetrying = (revoke: Effect.Effect<void, GitError>) =>
|
|
24
|
+
Effect.retry(revoke, {
|
|
25
|
+
while: tooManyWrites,
|
|
26
|
+
times: revokeTriesMost,
|
|
27
|
+
schedule: Schedule.exponential(revokePauseFirst),
|
|
28
|
+
});
|