ref0-git 0.3.0 → 0.4.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.
Files changed (210) hide show
  1. package/app/apps/github/.dev.vars.example +2 -1
  2. package/app/apps/github/generated/git-rest.ts +191 -25
  3. package/app/apps/github/generated/routeTree.gen.ts +21 -0
  4. package/app/apps/github/package.json +1 -1
  5. package/app/apps/github/src/branches/branch-list.tsx +8 -4
  6. package/app/apps/github/src/branches/branches.ts +10 -5
  7. package/app/apps/github/src/camo/addresses.ts +120 -0
  8. package/app/apps/github/src/camo/client.ts +125 -0
  9. package/app/apps/github/src/camo/fetch.ts +201 -0
  10. package/app/apps/github/src/camo/lookup.ts +23 -0
  11. package/app/apps/github/src/camo/node.ts +50 -0
  12. package/app/apps/github/src/camo/proxy.ts +116 -0
  13. package/app/apps/github/src/camo/sign-images.ts +307 -0
  14. package/app/apps/github/src/camo/signing.ts +80 -0
  15. package/app/apps/github/src/clients/allowed.ts +111 -18
  16. package/app/apps/github/src/clients/clients.ts +76 -61
  17. package/app/apps/github/src/clients/transport.ts +13 -0
  18. package/app/apps/github/src/code/code-tree.tsx +3 -1
  19. package/app/apps/github/src/code/file-view.tsx +15 -6
  20. package/app/apps/github/src/code/files.ts +26 -19
  21. package/app/apps/github/src/code/go-to-file.tsx +2 -1
  22. package/app/apps/github/src/code/load.ts +41 -23
  23. package/app/apps/github/src/code/markdown-files.ts +13 -2
  24. package/app/apps/github/src/code/ref-pages.ts +14 -5
  25. package/app/apps/github/src/code/ref-switcher.tsx +4 -3
  26. package/app/apps/github/src/code/refs.ts +42 -8
  27. package/app/apps/github/src/code/repo-home.tsx +6 -2
  28. package/app/apps/github/src/code/repo.ts +12 -6
  29. package/app/apps/github/src/code/saved.ts +5 -3
  30. package/app/apps/github/src/code/trees.ts +11 -9
  31. package/app/apps/github/src/components/markdown/markdown.css +6 -0
  32. package/app/apps/github/src/components/markdown/markdown.tsx +95 -10
  33. package/app/apps/github/src/components/owner-card.tsx +3 -1
  34. package/app/apps/github/src/components/repos/your-repos.tsx +3 -1
  35. package/app/apps/github/src/components/shell/owner-repos.tsx +7 -12
  36. package/app/apps/github/src/components/shell/repo-switcher.tsx +10 -16
  37. package/app/apps/github/src/components/signin/device-card.tsx +9 -2
  38. package/app/apps/github/src/components/user-avatar.tsx +2 -1
  39. package/app/apps/github/src/context/context.ts +13 -0
  40. package/app/apps/github/src/diffs/blobs.ts +37 -14
  41. package/app/apps/github/src/diffs/diff-job.ts +53 -0
  42. package/app/apps/github/src/diffs/diff-worker.ts +6 -0
  43. package/app/apps/github/src/diffs/diffing.ts +136 -0
  44. package/app/apps/github/src/diffs/files.ts +4 -3
  45. package/app/apps/github/src/diffs/hunks.tsx +101 -53
  46. package/app/apps/github/src/diffs/patch.ts +20 -11
  47. package/app/apps/github/src/history/commit-diff.ts +10 -5
  48. package/app/apps/github/src/history/commit-list.tsx +3 -1
  49. package/app/apps/github/src/history/commit-page.tsx +4 -2
  50. package/app/apps/github/src/history/history.ts +12 -10
  51. package/app/apps/github/src/hooks/allowed.ts +9 -9
  52. package/app/apps/github/src/hooks/avatars.ts +3 -3
  53. package/app/apps/github/src/hooks/open-counts.ts +5 -4
  54. package/app/apps/github/src/hooks/profiles.ts +26 -12
  55. package/app/apps/github/src/hooks/repos.ts +78 -25
  56. package/app/apps/github/src/inbox/count.ts +30 -29
  57. package/app/apps/github/src/inbox/inbox-item.tsx +3 -1
  58. package/app/apps/github/src/inbox/inbox-page.tsx +5 -2
  59. package/app/apps/github/src/inbox/marks.ts +26 -15
  60. package/app/apps/github/src/inbox/release-tags.ts +7 -6
  61. package/app/apps/github/src/inbox/subjects.ts +5 -4
  62. package/app/apps/github/src/inbox/subscription-controls.tsx +14 -5
  63. package/app/apps/github/src/inbox/subscriptions.ts +95 -35
  64. package/app/apps/github/src/inbox/threads.ts +6 -6
  65. package/app/apps/github/src/issues/changes.ts +40 -16
  66. package/app/apps/github/src/issues/counts.ts +8 -5
  67. package/app/apps/github/src/issues/issue-list.tsx +20 -6
  68. package/app/apps/github/src/issues/issue-page.tsx +30 -14
  69. package/app/apps/github/src/issues/issues.ts +195 -46
  70. package/app/apps/github/src/issues/label-dialog.tsx +8 -11
  71. package/app/apps/github/src/issues/labels-page.tsx +35 -3
  72. package/app/apps/github/src/issues/milestone-dialog.tsx +197 -0
  73. package/app/apps/github/src/issues/milestones-page.tsx +269 -0
  74. package/app/apps/github/src/issues/new-issue.tsx +3 -1
  75. package/app/apps/github/src/issues/sidebar.tsx +8 -6
  76. package/app/apps/github/src/markdown/README.md +22 -17
  77. package/app/apps/github/src/markdown/context.ts +83 -10
  78. package/app/apps/github/src/markdown/images.ts +3 -2
  79. package/app/apps/github/src/markdown/lookup.ts +9 -8
  80. package/app/apps/github/src/markdown/page-budget.ts +50 -0
  81. package/app/apps/github/src/markdown/patches.ts +74 -32
  82. package/app/apps/github/src/markdown/query.ts +7 -2
  83. package/app/apps/github/src/markdown/remote.ts +106 -0
  84. package/app/apps/github/src/markdown/render.ts +101 -24
  85. package/app/apps/github/src/markdown/texts.ts +43 -0
  86. package/app/apps/github/src/markdown/words-module.d.ts +5 -0
  87. package/app/apps/github/src/markdown/words-plugin.ts +135 -0
  88. package/app/apps/github/src/names/repos.ts +1 -1
  89. package/app/apps/github/src/palette/palette.tsx +7 -4
  90. package/app/apps/github/src/palette/rows.tsx +54 -33
  91. package/app/apps/github/src/palette/server.ts +5 -5
  92. package/app/apps/github/src/palette/targets.ts +1 -0
  93. package/app/apps/github/src/policy.ts +131 -61
  94. package/app/apps/github/src/private-repos.ts +2 -2
  95. package/app/apps/github/src/pulls/compare-page.tsx +6 -3
  96. package/app/apps/github/src/pulls/files-tab.tsx +480 -214
  97. package/app/apps/github/src/pulls/held-summary.ts +178 -0
  98. package/app/apps/github/src/pulls/live.ts +6 -4
  99. package/app/apps/github/src/pulls/merge-box.tsx +17 -10
  100. package/app/apps/github/src/pulls/pull-list.tsx +6 -3
  101. package/app/apps/github/src/pulls/pull-page.tsx +19 -10
  102. package/app/apps/github/src/pulls/pull.ts +68 -40
  103. package/app/apps/github/src/pulls/pulls.ts +39 -22
  104. package/app/apps/github/src/pulls/review-drafts.ts +131 -0
  105. package/app/apps/github/src/pulls/review.ts +374 -99
  106. package/app/apps/github/src/pulls/threads.tsx +152 -0
  107. package/app/apps/github/src/queue/queue-page.tsx +15 -8
  108. package/app/apps/github/src/queue/queue.ts +35 -20
  109. package/app/apps/github/src/releases/latest-release.tsx +3 -1
  110. package/app/apps/github/src/releases/overview.tsx +4 -2
  111. package/app/apps/github/src/releases/release-form.tsx +8 -5
  112. package/app/apps/github/src/releases/release-list.tsx +8 -5
  113. package/app/apps/github/src/releases/release-view.tsx +1 -0
  114. package/app/apps/github/src/releases/releases.ts +33 -23
  115. package/app/apps/github/src/releases/tag-list.tsx +3 -1
  116. package/app/apps/github/src/router.tsx +5 -2
  117. package/app/apps/github/src/routes/$owner/$repo/_code/blob/$.tsx +7 -1
  118. package/app/apps/github/src/routes/$owner/$repo/_code/tree/$.tsx +6 -1
  119. package/app/apps/github/src/routes/$owner/$repo/_releases/releases/index.tsx +2 -1
  120. package/app/apps/github/src/routes/$owner/$repo/_releases/tags.tsx +1 -1
  121. package/app/apps/github/src/routes/$owner/$repo/branches.tsx +5 -1
  122. package/app/apps/github/src/routes/$owner/$repo/commit/$sha.tsx +1 -1
  123. package/app/apps/github/src/routes/$owner/$repo/commits/$.tsx +6 -1
  124. package/app/apps/github/src/routes/$owner/$repo/index.tsx +1 -1
  125. package/app/apps/github/src/routes/$owner/$repo/issues/$number.tsx +4 -2
  126. package/app/apps/github/src/routes/$owner/$repo/issues/index.tsx +2 -2
  127. package/app/apps/github/src/routes/$owner/$repo/issues/new.tsx +5 -2
  128. package/app/apps/github/src/routes/$owner/$repo/labels.tsx +2 -1
  129. package/app/apps/github/src/routes/$owner/$repo/milestones.tsx +36 -0
  130. package/app/apps/github/src/routes/$owner/$repo/pull/$number/route.tsx +4 -2
  131. package/app/apps/github/src/routes/$owner/$repo/pulls/index.tsx +2 -2
  132. package/app/apps/github/src/routes/$owner/$repo/queue.tsx +2 -1
  133. package/app/apps/github/src/routes/$owner/$repo/releases/edit/$.tsx +5 -2
  134. package/app/apps/github/src/routes/$owner/$repo/releases/new.tsx +2 -1
  135. package/app/apps/github/src/routes/$owner/$repo/releases/tag/$.tsx +4 -2
  136. package/app/apps/github/src/routes/$owner/$repo/route.tsx +9 -5
  137. package/app/apps/github/src/routes/$owner/$repo/settings/hooks/index.tsx +3 -1
  138. package/app/apps/github/src/routes/$owner/$repo/settings/index.tsx +3 -1
  139. package/app/apps/github/src/routes/-nav.tsx +10 -2
  140. package/app/apps/github/src/routes/-root-testing.ts +29 -6
  141. package/app/apps/github/src/routes/__root.tsx +9 -4
  142. package/app/apps/github/src/routes/index.tsx +3 -1
  143. package/app/apps/github/src/routes/orgs/$owner/route.tsx +4 -1
  144. package/app/apps/github/src/routes/orgs/$owner/settings.tsx +4 -1
  145. package/app/apps/github/src/seed/acme.ts +1 -1
  146. package/app/apps/github/src/seed/seed.ts +199 -110
  147. package/app/apps/github/src/server.ts +24 -15
  148. package/app/apps/github/src/settings/account-avatar.tsx +4 -1
  149. package/app/apps/github/src/settings/general-settings.tsx +3 -1
  150. package/app/apps/github/src/settings/general.ts +4 -4
  151. package/app/apps/github/src/settings/hook-form.tsx +3 -1
  152. package/app/apps/github/src/settings/hook-list.tsx +5 -2
  153. package/app/apps/github/src/settings/hooks.ts +7 -7
  154. package/app/apps/github/src/settings/org-settings.tsx +3 -1
  155. package/app/apps/github/src/settings/orgs.ts +21 -11
  156. package/app/apps/github/src/settings/settings-layout.tsx +3 -1
  157. package/app/apps/github/src/signin/approval.ts +4 -3
  158. package/app/apps/github/src/signin/auth.ts +9 -2
  159. package/app/apps/github/src/signin/device-code.ts +8 -3
  160. package/app/apps/github/src/signin/device-prompt.ts +28 -3
  161. package/app/apps/github/src/signin/revoke.ts +36 -0
  162. package/app/apps/github/tsconfig.json +4 -1
  163. package/app/apps/github/vite.config.ts +8 -0
  164. package/app/pnpm-workspace.yaml +9 -5
  165. package/app/tsconfig.base.json +1 -0
  166. package/dist/cli/child.mjs +1 -1
  167. package/dist/cli/credentials.mjs +4 -4
  168. package/dist/cli/documents.mjs +32 -22
  169. package/dist/cli/events.mjs +1 -1
  170. package/dist/cli/failure.mjs +20 -7
  171. package/dist/cli/folders.mjs +2 -2
  172. package/dist/cli/git-remote-ref0.mjs +47 -22
  173. package/dist/cli/git.mjs +1 -1
  174. package/dist/cli/helpers.mjs +2 -2
  175. package/dist/cli/hosts.mjs +3 -3
  176. package/dist/cli/output.mjs +2 -2
  177. package/dist/cli/prompt.mjs +1 -1
  178. package/dist/cli/queue.mjs +7 -7
  179. package/dist/cli/ref0-git.mjs +2 -2
  180. package/dist/cli/repos.mjs +35 -18
  181. package/dist/cli/resources.mjs +5 -5
  182. package/dist/cli/retry-after.mjs +8 -0
  183. package/dist/cli/setup.mjs +172 -66
  184. package/dist/cli/sign-in.mjs +65 -23
  185. package/dist/cli/sign-ins.mjs +4 -4
  186. package/dist/cli/sqlite.mjs +2 -2
  187. package/dist/cli/tokens.mjs +3 -3
  188. package/dist/cli/vendor.mjs +1 -1
  189. package/dist/init/app.mjs +18 -20
  190. package/dist/init/choices.mjs +1 -1
  191. package/dist/init/cloudflare.mjs +15 -19
  192. package/dist/init/create.mjs +3 -2
  193. package/dist/init/detect.mjs +4 -4
  194. package/dist/init/files.mjs +120 -13
  195. package/dist/init/frameworks.mjs +16 -0
  196. package/dist/init/git-module.mjs +22 -7
  197. package/dist/init/jsonc.mjs +9 -7
  198. package/dist/init/metadata.mjs +9 -3
  199. package/dist/init/mounts.mjs +13 -10
  200. package/dist/init/next.mjs +2 -2
  201. package/dist/init/node-app.mjs +3 -3
  202. package/dist/init/node.mjs +42 -45
  203. package/dist/init/nuxt.mjs +153 -27
  204. package/dist/init/stores.mjs +14 -9
  205. package/dist/init/sveltekit.mjs +4 -2
  206. package/dist/init/template.mjs +9 -6
  207. package/dist/init/templates.mjs +63 -47
  208. package/dist/init/vercel.mjs +37 -37
  209. package/dist/init/versions.mjs +3 -3
  210. package/package.json +6 -5
@@ -1,8 +1,7 @@
1
- import { useInfiniteQuery } from '@tanstack/react-query';
2
1
  import { Link, useLocation } from '@tanstack/react-router';
3
- import { useEffect, useState } from 'react';
2
+ import { useState } from 'react';
4
3
 
5
- import { maxFiltered, type Owner, repoList } from '../../hooks/repos.ts';
4
+ import { type Owner, useRepoNames } from '../../hooks/repos.ts';
6
5
  import { RepoIcon } from '../repos/repo-icon.tsx';
7
6
  import { Alert, AlertDescription } from '../ui/alert.tsx';
8
7
  import { Button } from '../ui/button.tsx';
@@ -16,9 +15,7 @@ import {
16
15
  } from '../ui/combobox.tsx';
17
16
  import { Skeleton } from '../ui/skeleton.tsx';
18
17
  import { ComboboxCreateRow } from './create-row.tsx';
19
-
20
- /** The owner's repositories in the list page's own order, so its read is the switcher's too. */
21
- const listed = { search: '', type: 'all', language: '', sort: 'updated' } as const;
18
+ import { switcherList } from './owner-repos.tsx';
22
19
 
23
20
  /** The item that leads back to the owner's level: a name no repo can take. */
24
21
  const everyRepo = '';
@@ -62,20 +59,17 @@ export function RepoSwitcher({
62
59
  }) {
63
60
  const [open, setOpen] = useState(false);
64
61
  const [typed, setTyped] = useState('');
65
- const repos = useInfiniteQuery({ ...repoList(owner, listed), enabled: open && signedIn });
66
- const names = repos.data?.pages.flatMap((page) => page.repos.map((repo) => repo.name)) ?? [];
67
- const reading =
68
- typed.trim() !== '' && repos.hasNextPage && names.length < maxFiltered && !repos.isError;
69
- const { fetchNextPage, isFetchingNextPage } = repos;
62
+ // A typed search reads on, as the list page's search does.
63
+ const { repos, names, reading } = useRepoNames(
64
+ owner,
65
+ switcherList,
66
+ open && signedIn,
67
+ () => typed.trim() !== '',
68
+ );
70
69
  const label = labelOf(current ?? everyRepo);
71
70
  const here = useLocation({ select: (location) => location.href });
72
71
  const found = [everyRepo, ...names].filter((item) => matches(item, typed));
73
72
 
74
- // Each page read asks for the next while a search is typed, as the list page's search does.
75
- useEffect(() => {
76
- if (reading && !isFetchingNextPage) void fetchNextPage();
77
- }, [reading, isFetchingNextPage, fetchNextPage, names.length]);
78
-
79
73
  if (!signedIn) {
80
74
  return (
81
75
  <span className="flex min-w-0 items-center gap-1.5 text-sm font-medium">
@@ -1,3 +1,4 @@
1
+ import type { TokenScope } from '@ref0/git';
1
2
  import { MonitorSmartphoneIcon } from 'lucide-react';
2
3
 
3
4
  import type { DeviceCardState } from '../../signin/device-code.ts';
@@ -18,6 +19,13 @@ import { Input } from '../ui/input.tsx';
18
19
  /** Where the Card's forms post: Git's `/login/device`, which decides and redirects back. */
19
20
  const action = '/login/device';
20
21
 
22
+ /** What each token scope lets a device do, as the Card says it before Approve. */
23
+ const scopeSays: Readonly<Record<TokenScope, string>> = {
24
+ read: 'read every repository you can',
25
+ report: 'read every repository you can, and report CI results and deployments on them',
26
+ write: 'read and write every repository you can',
27
+ };
28
+
21
29
  /**
22
30
  * `/login/code`'s Card, its title the page's heading: the Code field, the confirm, whose Approve and Deny post `user_code`, the
23
31
  * sealed `prompt` and `decision`, or what was decided. Its forms are plain posts, so they work
@@ -61,8 +69,7 @@ export function DeviceCard({ state }: { readonly state: DeviceCardState }) {
61
69
  </Badge>
62
70
  </p>
63
71
  <p>
64
- Approve lets it read and write every repository you can, as you, for {state.days}{' '}
65
- days.
72
+ Approve lets it {scopeSays[state.tokenScope]}, as you, for {state.days} days.
66
73
  </p>
67
74
  {state.scopes.length > 0 && (
68
75
  <div className="flex flex-col gap-2">
@@ -2,6 +2,7 @@ import { cn } from '#/lib/cn';
2
2
  import { useQuery } from '@tanstack/react-query';
3
3
  import { useEffect, useState } from 'react';
4
4
 
5
+ import { useClients } from '../clients/clients.ts';
5
6
  import { avatarAt } from '../hooks/avatars.ts';
6
7
  import { Avatar, AvatarFallback, AvatarImage } from './ui/avatar.tsx';
7
8
 
@@ -91,7 +92,7 @@ function usePicture(
91
92
  account: Account,
92
93
  named: string | null | undefined,
93
94
  ): { readonly picture: string | undefined; readonly uploaded: boolean } {
94
- const asked = useQuery({ ...avatarAt(login), enabled: named === undefined });
95
+ const asked = useQuery({ ...avatarAt(useClients(), login), enabled: named === undefined });
95
96
  const upload = named === undefined ? (asked.isSuccess ? asked.data : undefined) : named;
96
97
  const drawn = upload === null || (named === undefined && asked.isError);
97
98
  const picture = useDrawn(login, account, drawn);
@@ -118,6 +118,19 @@ export interface AppContext {
118
118
  * once its read of the person's role decided what they see.
119
119
  */
120
120
  readonly git: HostGit;
121
+ /**
122
+ * The image proxy's signer (Q860), as `server.ts` makes it: with `IMAGE_PROXY=off`, a text's
123
+ * remote images load from their own origin.
124
+ */
125
+ readonly images: ImageSigner;
126
+ }
127
+
128
+ /** The image proxy's say on a text's remote images, which `signImages` asks it for. */
129
+ export interface ImageSigner {
130
+ /** Whether the host turned the proxy off, so each image loads from its own origin. */
131
+ readonly off: boolean;
132
+ /** Each of `urls` the proxy may fetch, to its path on the proxy: one it refuses is left out. */
133
+ readonly sign: (urls: readonly string[]) => Promise<ReadonlyMap<string, string>>;
121
134
  }
122
135
 
123
136
  /** Git's direct calls a host call reads with, as host code. */
@@ -1,4 +1,4 @@
1
- import { github } from '../clients/clients.ts';
1
+ import type { Clients } from '../clients/clients.ts';
2
2
  import { repoPath, type ChangedFile, type Revisions } from './files.ts';
3
3
 
4
4
  /** A side of a file's diff: its name and its text, or `null` where the file isn't. */
@@ -9,23 +9,34 @@ export interface Side {
9
9
 
10
10
  /**
11
11
  * A file's two sides, read whole for **Load diff**, or why they can't be diffed here: a side
12
- * that isn't text, or one past `diffedBytesMost`, which the browser would take too long on.
12
+ * that isn't text, or one past the `bytes` it may hold, which the browser would take too long on.
13
13
  */
14
14
  export type LoadedFile =
15
15
  | { readonly kind: 'text'; readonly old: Side | null; readonly new: Side | null }
16
16
  | { readonly kind: 'binary' }
17
- | { readonly kind: 'too-large' }
17
+ | { readonly kind: 'too-large'; readonly bytes: number }
18
18
  | { readonly kind: 'unchanged' };
19
19
 
20
- /** The most bytes a side may hold for the browser to diff it. */
20
+ /** The most bytes a side may hold for the page's thread to diff it, with no Web Worker. */
21
21
  export const diffedBytesMost = 1024 * 1024;
22
22
 
23
+ /**
24
+ * The most bytes a side may hold for a diff Worker to diff it: two 4 MiB sides changed on every
25
+ * line held at most 275 MB until the Worker gave up at `workerDiffedMillisMost`.
26
+ */
27
+ export const workerDiffedBytesMost = 4 * 1024 * 1024;
28
+
23
29
  /** A file's path before the change: a rename's old one, or its own. */
24
30
  const oldName = (file: ChangedFile) => file.previous_filename ?? file.filename;
25
31
 
26
32
  /** The blob at `path` in commit `ref`, by the contents API's answer, or `undefined`. */
27
- const blobAt = async (revisions: Revisions, path: string, signal: AbortSignal) => {
28
- const answer = await github.GET('/repos/{owner}/{repo}/contents/{path}', {
33
+ const blobAt = async (
34
+ clients: Clients,
35
+ revisions: Revisions,
36
+ path: string,
37
+ signal: AbortSignal,
38
+ ) => {
39
+ const answer = await clients.github.GET('/repos/{owner}/{repo}/contents/{path}', {
29
40
  params: { path: { ...repoPath(revisions.repo), path }, query: { ref: revisions.base } },
30
41
  signal,
31
42
  });
@@ -35,16 +46,22 @@ const blobAt = async (revisions: Revisions, path: string, signal: AbortSignal) =
35
46
  return Array.isArray(found) || found.type !== 'file' ? undefined : found.sha;
36
47
  };
37
48
 
38
- /** A blob's bytes through `git/blobs`, and its size as Git counts it. */
39
- const blobBytes = async (repo: string, sha: string, signal: AbortSignal) => {
40
- const answer = await github.GET('/repos/{owner}/{repo}/git/blobs/{file_sha}', {
49
+ /** A blob's bytes through `git/blobs`, or `too-large` past `bytesMost` as Git counts its size. */
50
+ const blobBytes = async (
51
+ clients: Clients,
52
+ repo: string,
53
+ sha: string,
54
+ bytesMost: number,
55
+ signal: AbortSignal,
56
+ ) => {
57
+ const answer = await clients.github.GET('/repos/{owner}/{repo}/git/blobs/{file_sha}', {
41
58
  params: { path: { ...repoPath(repo), file_sha: sha } },
42
59
  signal,
43
60
  });
44
61
  const blob = answer.data;
45
62
  if (blob === undefined) throw new Error(`Blob ${sha} didn't load (${answer.response.status})`);
46
63
 
47
- if (blob.size !== null && blob.size > diffedBytesMost) return 'too-large';
64
+ if (blob.size !== null && blob.size > bytesMost) return 'too-large';
48
65
 
49
66
  return Uint8Array.from(atob(blob.content.replaceAll('\n', '')), (char) => char.charCodeAt(0));
50
67
  };
@@ -57,24 +74,30 @@ const textOf = (bytes: Uint8Array) =>
57
74
 
58
75
  /**
59
76
  * `file`'s two sides between `revisions`, read through `git/blobs`, for a file GitHub's REST
60
- * lists with no patch: the new blob by its `sha`, and the old by the path it had in `base`.
77
+ * lists with no patch: the new blob by its `sha`, and the old by the path it had in `base`. A
78
+ * side past `bytesMost` isn't read.
61
79
  */
62
80
  export const loadFile = async (
81
+ clients: Clients,
63
82
  file: ChangedFile,
64
83
  revisions: Revisions,
84
+ bytesMost: number,
65
85
  signal: AbortSignal,
66
86
  ): Promise<LoadedFile> => {
67
87
  const added = file.status === 'added';
68
88
  const removed = file.status === 'removed';
69
- const oldSha = added ? undefined : await blobAt(revisions, oldName(file), signal);
89
+ const oldSha = added ? undefined : await blobAt(clients, revisions, oldName(file), signal);
70
90
  const newSha = removed ? undefined : (file.sha ?? undefined);
71
91
  // A rename, or a mode change, with its blob unchanged; a binary change counts no lines either.
72
92
  if (oldSha !== undefined && oldSha === newSha) return { kind: 'unchanged' };
73
93
 
74
94
  const read = async (sha: string | undefined) =>
75
- sha === undefined ? undefined : blobBytes(revisions.repo, sha, signal);
95
+ sha === undefined ? undefined : blobBytes(clients, revisions.repo, sha, bytesMost, signal);
76
96
  const [oldBytes, newBytes] = await Promise.all([read(oldSha), read(newSha)]);
77
- if (oldBytes === 'too-large' || newBytes === 'too-large') return { kind: 'too-large' };
97
+
98
+ if (oldBytes === 'too-large' || newBytes === 'too-large') {
99
+ return { kind: 'too-large', bytes: bytesMost };
100
+ }
78
101
 
79
102
  const oldText = oldBytes === undefined ? undefined : textOf(oldBytes);
80
103
  const newText = newBytes === undefined ? undefined : textOf(newBytes);
@@ -0,0 +1,53 @@
1
+ import type { FileDiffMetadata } from '@pierre/diffs';
2
+
3
+ import type { Side } from './blobs.ts';
4
+ import { diffOfSides } from './patch.ts';
5
+
6
+ /** Two sides to diff, the key their render is cached by, and how long the diff may take. */
7
+ export interface DiffJob {
8
+ readonly old: Side | null;
9
+ readonly new: Side | null;
10
+ readonly key: string;
11
+ readonly millis: number;
12
+ }
13
+
14
+ /**
15
+ * Two sides' diff, or why it isn't drawn: they differ too much to diff within `millis`, or it
16
+ * adds and removes more than `lines`.
17
+ */
18
+ export type Diffed =
19
+ | { readonly kind: 'diff'; readonly diff: FileDiffMetadata }
20
+ | { readonly kind: 'too-different'; readonly millis: number }
21
+ | { readonly kind: 'too-many-lines'; readonly lines: number };
22
+
23
+ /** What a diff Worker answers: the diff, or the message of what it threw. */
24
+ export type DiffAnswer = Diffed | { readonly kind: 'failed'; readonly message: string };
25
+
26
+ /**
27
+ * The most lines a diff may add and remove for the page to draw it: highlighting 20,000 of them
28
+ * took 6.7 s, and their rows and marks stay in the page's memory.
29
+ */
30
+ export const changedLinesMost = 50_000;
31
+
32
+ /** The lines `diff` adds and removes, its context left out. */
33
+ const changedLinesOf = (diff: FileDiffMetadata) =>
34
+ diff.hunks.reduce((sum, hunk) => sum + hunk.additionLines + hunk.deletionLines, 0);
35
+
36
+ /** `job`'s two sides diffed, on whichever thread runs it: a diff Worker's, or the page's. */
37
+ export const diffedOf = (job: DiffJob): Diffed => {
38
+ const diff = diffOfSides(job.old, job.new, job.key, job.millis);
39
+ if (diff === undefined) return { kind: 'too-different', millis: job.millis };
40
+
41
+ return changedLinesOf(diff) > changedLinesMost
42
+ ? { kind: 'too-many-lines', lines: changedLinesMost }
43
+ : { kind: 'diff', diff };
44
+ };
45
+
46
+ /** What a diff Worker answers `job`: its diff, or what diffing it threw, as a message. */
47
+ export const answerOf = (job: DiffJob): DiffAnswer => {
48
+ try {
49
+ return diffedOf(job);
50
+ } catch (error) {
51
+ return { kind: 'failed', message: error instanceof Error ? error.message : String(error) };
52
+ }
53
+ };
@@ -0,0 +1,6 @@
1
+ import { answerOf, type DiffJob } from './diff-job.ts';
2
+
3
+ // A diff Worker's entry: it diffs each job it's sent off the page's thread, and answers it.
4
+ self.addEventListener('message', (event: MessageEvent<DiffJob>) => {
5
+ self.postMessage(answerOf(event.data));
6
+ });
@@ -0,0 +1,136 @@
1
+ import { diffedBytesMost, workerDiffedBytesMost, type Side } from './blobs.ts';
2
+ import { diffedOf, type DiffAnswer, type Diffed, type DiffJob } from './diff-job.ts';
3
+ import { diffedMillisMost, workerDiffedMillisMost } from './patch.ts';
4
+
5
+ /**
6
+ * How long the page waits on a diff Worker before it terminates it, in milliseconds: past its
7
+ * own `workerDiffedMillisMost`, a Worker that hasn't answered is stuck, and says so as one that
8
+ * gave up does.
9
+ */
10
+ export const answeredMillisMost = 25_000;
11
+
12
+ /** The most diff Workers that run at once: each holds a pair of sides and Myers' paths. */
13
+ export const workersMost = 2;
14
+
15
+ /** Whether this browser runs Web Workers. Without, two sides diff on the page's thread. */
16
+ export const hasWorkers = () => typeof Worker !== 'undefined';
17
+
18
+ /** The most bytes a side may hold to be diffed: a diff Worker takes more than the page's thread. */
19
+ export const sideBytesMost = () => (hasWorkers() ? workerDiffedBytesMost : diffedBytesMost);
20
+
21
+ let running = 0;
22
+
23
+ const waiting: (() => void)[] = [];
24
+
25
+ /** Waits for one of the `workersMost` Workers' places, or rejects once `signal` aborts first. */
26
+ const place = (signal: AbortSignal) =>
27
+ new Promise<void>((resolve, reject) => {
28
+ if (running < workersMost) {
29
+ running += 1;
30
+ resolve();
31
+
32
+ return;
33
+ }
34
+
35
+ const take = () => {
36
+ signal.removeEventListener('abort', drop);
37
+ running += 1;
38
+ resolve();
39
+ };
40
+
41
+ const drop = () => {
42
+ waiting.splice(waiting.indexOf(take), 1);
43
+ reject(signal.reason);
44
+ };
45
+
46
+ waiting.push(take);
47
+ signal.addEventListener('abort', drop, { once: true });
48
+ });
49
+
50
+ /** Gives a Worker's place to the next job waiting, if any. */
51
+ const leave = () => {
52
+ running -= 1;
53
+ waiting.shift()?.();
54
+ };
55
+
56
+ /** A diff Worker, or `undefined` when the browser refuses to make one, as a policy can. */
57
+ const startWorker = () => {
58
+ try {
59
+ return new Worker(new URL('./diff-worker.ts', import.meta.url), {
60
+ type: 'module',
61
+ name: 'diff',
62
+ });
63
+ } catch {
64
+ return undefined;
65
+ }
66
+ };
67
+
68
+ /**
69
+ * `job` diffed in `worker`, which is terminated once it answers, once `signal` aborts, or past
70
+ * `answeredMillisMost`. `undefined` when the Worker couldn't start, as when its script doesn't
71
+ * load.
72
+ */
73
+ const inWorker = (worker: Worker, job: DiffJob, signal: AbortSignal) =>
74
+ new Promise<Diffed | undefined>((resolve, reject) => {
75
+ const end = () => {
76
+ clearTimeout(stuck);
77
+ signal.removeEventListener('abort', cancel);
78
+ worker.terminate();
79
+ };
80
+
81
+ const cancel = () => {
82
+ end();
83
+ reject(signal.reason);
84
+ };
85
+
86
+ const stuck = setTimeout(() => {
87
+ end();
88
+ resolve({ kind: 'too-different', millis: job.millis });
89
+ }, answeredMillisMost);
90
+
91
+ worker.addEventListener('message', (event: MessageEvent<DiffAnswer>) => {
92
+ end();
93
+ const answer = event.data;
94
+ if (answer.kind === 'failed') reject(new Error(answer.message));
95
+ else resolve(answer);
96
+ });
97
+ worker.addEventListener('error', (event) => {
98
+ event.preventDefault();
99
+ end();
100
+ resolve(undefined);
101
+ });
102
+ signal.addEventListener('abort', cancel, { once: true });
103
+ worker.postMessage(job);
104
+ });
105
+
106
+ /**
107
+ * Two sides **Load diff** read, diffed off the page's thread in a diff Worker, at most
108
+ * `workersMost` at once, until `signal` aborts. A browser with no Worker, or one whose Worker
109
+ * can't be made or doesn't start, diffs them on the page's thread within `diffedMillisMost`.
110
+ */
111
+ export const diffSides = async (
112
+ old: Side | null,
113
+ changed: Side | null,
114
+ key: string,
115
+ signal: AbortSignal,
116
+ ): Promise<Diffed> => {
117
+ const onThread = () => diffedOf({ old, new: changed, key, millis: diffedMillisMost });
118
+ signal.throwIfAborted();
119
+
120
+ if (!hasWorkers()) return onThread();
121
+
122
+ await place(signal);
123
+
124
+ try {
125
+ // An abort while the place was handed over starts no Worker.
126
+ signal.throwIfAborted();
127
+ const worker = startWorker();
128
+ if (worker === undefined) return onThread();
129
+
130
+ const job = { old, new: changed, key, millis: workerDiffedMillisMost };
131
+
132
+ return (await inWorker(worker, job, signal)) ?? onThread();
133
+ } finally {
134
+ leave();
135
+ }
136
+ };
@@ -3,7 +3,7 @@ import type { DiffLineAnnotation, SelectedLineRange } from '@pierre/diffs';
3
3
  import * as Schema from 'effect/Schema';
4
4
  import type { ReactNode } from 'react';
5
5
 
6
- import { github } from '../clients/clients.ts';
6
+ import type { Clients } from '../clients/clients.ts';
7
7
  import { nextCursor } from '../hooks/repos.ts';
8
8
 
9
9
  /** A changed file, as GitHub's REST lists a pull request's or a comparison's. */
@@ -64,6 +64,7 @@ const paging = (after: string | undefined) => ({ per_page: perPage, after });
64
64
  * two reads goes unseen.
65
65
  */
66
66
  export const pullFiles = (
67
+ clients: Clients,
67
68
  repo: string,
68
69
  number: number,
69
70
  head: string,
@@ -72,7 +73,7 @@ export const pullFiles = (
72
73
  key: ['pull-files', repo, number, head],
73
74
  page: async (after, signal) => {
74
75
  const path = { ...repoPath(repo), pull_number: number };
75
- const answer = await github.GET('/repos/{owner}/{repo}/pulls/{pull_number}/files', {
76
+ const answer = await clients.github.GET('/repos/{owner}/{repo}/pulls/{pull_number}/files', {
76
77
  // A spread passes the cursor GitHub's types don't name.
77
78
  params: { path, query: { ...paging(after) } },
78
79
  signal,
@@ -82,7 +83,7 @@ export const pullFiles = (
82
83
  throw new Error(`${repo}#${number}'s files didn't load (${answer.response.status})`);
83
84
  }
84
85
 
85
- const now = await github.GET('/repos/{owner}/{repo}/pulls/{pull_number}', {
86
+ const now = await clients.github.GET('/repos/{owner}/{repo}/pulls/{pull_number}', {
86
87
  params: { path },
87
88
  signal,
88
89
  });