@zabaca/zbc 0.20.0 → 0.21.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/templates/apps/walgit/CONTEXT.md +37 -0
- package/templates/apps/walgit/README.md +193 -4
- package/templates/apps/walgit/e2e/harness.ts +2 -2
- package/templates/apps/walgit/e2e/scenarios.ts +302 -2
- package/templates/apps/walgit/package.json +5 -1
- package/templates/apps/walgit/registry.json +1 -1
- package/templates/apps/walgit/shared/browse.ts +733 -0
- package/templates/apps/walgit/shared/capabilities.ts +17 -0
- package/templates/apps/walgit/shared/container-env.ts +9 -0
- package/templates/apps/walgit/shared/landing.ts +8 -27
- package/templates/apps/walgit/shared/llms.ts +41 -0
- package/templates/apps/walgit/shared/mcp.ts +286 -0
- package/templates/apps/walgit/shared/policy.ts +21 -0
- package/templates/apps/walgit/shared/pooled.ts +31 -0
- package/templates/apps/walgit/shared/protocol.ts +140 -0
- package/templates/apps/walgit/shared/repo-list.ts +478 -0
- package/templates/apps/walgit/shared/store.ts +307 -0
- package/templates/apps/walgit/shared/telemetry.ts +79 -6
- package/templates/apps/walgit/shared/wal-index.ts +249 -0
- package/templates/apps/walgit/shared/web.ts +46 -0
- package/templates/apps/walgit/src/announce-main.ts +1 -1
- package/templates/apps/walgit/src/announce.ts +1 -1
- package/templates/apps/walgit/src/append-only.ts +5 -8
- package/templates/apps/walgit/src/browse.ts +230 -0
- package/templates/apps/walgit/src/cache.ts +4 -4
- package/templates/apps/walgit/src/compact.ts +6 -12
- package/templates/apps/walgit/src/delete-repo.ts +4 -3
- package/templates/apps/walgit/src/expire.ts +3 -3
- package/templates/apps/walgit/src/gc.ts +4 -3
- package/templates/apps/walgit/src/git.ts +127 -10
- package/templates/apps/walgit/src/hook-main.ts +1 -1
- package/templates/apps/walgit/src/http.ts +386 -3
- package/templates/apps/walgit/src/limits.ts +1 -1
- package/templates/apps/walgit/src/materialize.bench.ts +4 -10
- package/templates/apps/walgit/src/materialize.ts +11 -4
- package/templates/apps/walgit/src/orphans.ts +3 -3
- package/templates/apps/walgit/src/pending.ts +1 -1
- package/templates/apps/walgit/src/private.ts +1 -1
- package/templates/apps/walgit/src/proposals.ts +3 -3
- package/templates/apps/walgit/src/push-cert.ts +10 -1
- package/templates/apps/walgit/src/push.ts +11 -9
- package/templates/apps/walgit/src/reconcile.ts +4 -3
- package/templates/apps/walgit/src/server.ts +29 -2
- package/templates/apps/walgit/src/signers.ts +9 -3
- package/templates/apps/walgit/src/store-env.ts +8 -21
- package/templates/apps/walgit/src/store.ts +12 -241
- package/templates/apps/walgit/src/sync.ts +2 -2
- package/templates/apps/walgit/src/usage.ts +4 -18
- package/templates/apps/walgit/src/verify.ts +2 -2
- package/templates/apps/walgit/src/wal-index.ts +20 -239
- package/templates/apps/walgit/worker/index.ts +278 -1
- package/templates/apps/walgit/worker/mcp.ts +281 -0
- /package/templates/apps/walgit/{src → shared}/keys.ts +0 -0
- /package/templates/apps/walgit/{src → shared}/ulid.ts +0 -0
|
@@ -21,10 +21,11 @@
|
|
|
21
21
|
*/
|
|
22
22
|
|
|
23
23
|
import { configuredGraceMs } from './compact'
|
|
24
|
-
import { siblingIdx, walKeyUploadedAt } from '
|
|
24
|
+
import { siblingIdx, walKeyUploadedAt } from '../shared/keys'
|
|
25
25
|
import { findOrphans } from './orphans'
|
|
26
|
-
import type { ObjectStore } from '
|
|
27
|
-
import { loadIndex,
|
|
26
|
+
import type { ObjectStore } from '../shared/store'
|
|
27
|
+
import { loadIndex, type WalIndex } from '../shared/wal-index'
|
|
28
|
+
import { updateIndex } from './wal-index'
|
|
28
29
|
|
|
29
30
|
export interface GcResult {
|
|
30
31
|
/** Superseded keys whose grace period elapsed, now deleted. */
|
|
@@ -13,14 +13,31 @@
|
|
|
13
13
|
* with inherited stdio, and `git-backend.ts` streams `git http-backend` as CGI.
|
|
14
14
|
* Both speak the pack protocol on their own stdio and must never be buffered.
|
|
15
15
|
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
*
|
|
20
|
-
*
|
|
21
|
-
*
|
|
22
|
-
* `
|
|
23
|
-
*
|
|
16
|
+
* **Hygiene is this module's job, not the caller's.** Two kinds of input reach
|
|
17
|
+
* git here, and neither is safe left as it arrives:
|
|
18
|
+
*
|
|
19
|
+
* - **Arguments.** A ref, an oid or a path is DATA, and git reads an argument
|
|
20
|
+
* beginning with `-` as an option wherever one is allowed. So a caller
|
|
21
|
+
* never concatenates them into `args`: revisions go in `operands` (placed
|
|
22
|
+
* after git's own `--end-of-options` fence) and paths go in `paths` (after
|
|
23
|
+
* `--`). `args` is the subcommand and its flags, which this repository
|
|
24
|
+
* writes and nobody else supplies.
|
|
25
|
+
* - **The environment.** Every inherited `GIT_*` variable is dropped at
|
|
26
|
+
* spawn. A global or system git config that sets `core.hooksPath`
|
|
27
|
+
* redirects `git-receive-pack` away from `$GIT_DIR/hooks`, so walgit's
|
|
28
|
+
* `pre-receive` and `reference-transaction` never run and a push is
|
|
29
|
+
* acknowledged with nothing written to the log — the one outcome
|
|
30
|
+
* docs/adr/0007 exists to prevent. Dropping `GIT_CONFIG_GLOBAL` would
|
|
31
|
+
* RE-OPEN that door (`e2e/harness.ts` sets it to `/dev/null` precisely to
|
|
32
|
+
* close it), so the strip is paired with pinning both config paths at
|
|
33
|
+
* `/dev/null` here. A caller that needs a variable says so in `env`.
|
|
34
|
+
*
|
|
35
|
+
* It covers the commands walgit runs and READS. It deliberately does not cover
|
|
36
|
+
* the two places git is not a subprocess to be read but a transport to be
|
|
37
|
+
* handed the socket: `git-backend.ts` runs `git http-backend` with inherited
|
|
38
|
+
* stdio, and streams it as CGI. Both speak the pack protocol on their own
|
|
39
|
+
* stdio and must never be buffered — and both are given an explicit
|
|
40
|
+
* environment of their own for the same reason this one is.
|
|
24
41
|
*/
|
|
25
42
|
|
|
26
43
|
import { spawnSync } from 'node:child_process'
|
|
@@ -35,6 +52,81 @@ export interface GitResult {
|
|
|
35
52
|
export interface GitOptions {
|
|
36
53
|
/** Fed to the command on stdin, e.g. the oid list `cat-file` batches over. */
|
|
37
54
|
input?: string
|
|
55
|
+
/**
|
|
56
|
+
* The repository, named rather than discovered. git's own discovery walks up
|
|
57
|
+
* from the working directory and would find whichever repository the process
|
|
58
|
+
* happens to sit in — which for a hook or a CLI command is not necessarily
|
|
59
|
+
* the one being operated on.
|
|
60
|
+
*/
|
|
61
|
+
gitDir?: string
|
|
62
|
+
/**
|
|
63
|
+
* Revisions, oids and other non-path operands. Placed after
|
|
64
|
+
* `--end-of-options`, so `-p` or `--output=x` arriving as a ref name is read
|
|
65
|
+
* as the ref it is.
|
|
66
|
+
*/
|
|
67
|
+
operands?: readonly string[]
|
|
68
|
+
/** Pathspecs. Placed after `--`, which is git's fence for exactly this. */
|
|
69
|
+
paths?: readonly string[]
|
|
70
|
+
/**
|
|
71
|
+
* Variables to set on the child on top of the stripped environment. An
|
|
72
|
+
* `undefined` value removes one. This is the only way a `GIT_*` variable
|
|
73
|
+
* reaches git from here.
|
|
74
|
+
*/
|
|
75
|
+
env?: Record<string, string | undefined>
|
|
76
|
+
/**
|
|
77
|
+
* Keep the three variables git itself sets so a hook can see the objects a
|
|
78
|
+
* push is offering (`GIT_OBJECT_DIRECTORY`, `GIT_ALTERNATE_OBJECT_DIRECTORIES`,
|
|
79
|
+
* `GIT_QUARANTINE_PATH`).
|
|
80
|
+
*
|
|
81
|
+
* Only `pre-receive`'s verdicts need it — they ask git about commits that are
|
|
82
|
+
* still in the quarantine and not yet in the repository — and it is opt-in
|
|
83
|
+
* rather than inherited because for every other caller the variable points at
|
|
84
|
+
* an object store that has nothing to do with the command being run.
|
|
85
|
+
*/
|
|
86
|
+
inheritObjects?: boolean
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** What git sets for a hook so the pushed objects are readable. */
|
|
90
|
+
const QUARANTINE_VARS = [
|
|
91
|
+
'GIT_OBJECT_DIRECTORY',
|
|
92
|
+
'GIT_ALTERNATE_OBJECT_DIRECTORIES',
|
|
93
|
+
'GIT_QUARANTINE_PATH',
|
|
94
|
+
] as const
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* The child's environment: the parent's, minus every `GIT_*`, with ambient
|
|
98
|
+
* config pinned out of the way and the caller's additions last.
|
|
99
|
+
*/
|
|
100
|
+
function childEnv(opts: GitOptions): NodeJS.ProcessEnv {
|
|
101
|
+
const env: NodeJS.ProcessEnv = {}
|
|
102
|
+
for (const [key, value] of Object.entries(process.env)) {
|
|
103
|
+
if (!key.startsWith('GIT_')) env[key] = value
|
|
104
|
+
}
|
|
105
|
+
env.GIT_CONFIG_GLOBAL = '/dev/null'
|
|
106
|
+
env.GIT_CONFIG_SYSTEM = '/dev/null'
|
|
107
|
+
if (opts.inheritObjects) {
|
|
108
|
+
for (const name of QUARANTINE_VARS) {
|
|
109
|
+
const value = process.env[name]
|
|
110
|
+
if (value !== undefined) env[name] = value
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
for (const [key, value] of Object.entries(opts.env ?? {})) {
|
|
114
|
+
if (value === undefined) delete env[key]
|
|
115
|
+
else env[key] = value
|
|
116
|
+
}
|
|
117
|
+
return env
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/** The full argument vector, with git's two fences where they belong. */
|
|
121
|
+
function argv(args: readonly string[], opts: GitOptions): string[] {
|
|
122
|
+
const operands = opts.operands ?? []
|
|
123
|
+
const paths = opts.paths ?? []
|
|
124
|
+
return [
|
|
125
|
+
...(opts.gitDir ? ['--git-dir', opts.gitDir] : []),
|
|
126
|
+
...args,
|
|
127
|
+
...(operands.length > 0 ? ['--end-of-options', ...operands] : []),
|
|
128
|
+
...(paths.length > 0 ? ['--', ...paths] : []),
|
|
129
|
+
]
|
|
38
130
|
}
|
|
39
131
|
|
|
40
132
|
/**
|
|
@@ -56,10 +148,11 @@ export interface GitOptions {
|
|
|
56
148
|
const MAX_BUFFER = 64 * 1024 * 1024
|
|
57
149
|
|
|
58
150
|
export function git(args: readonly string[], opts: GitOptions = {}): GitResult {
|
|
59
|
-
const res = spawnSync('git',
|
|
151
|
+
const res = spawnSync('git', argv(args, opts), {
|
|
60
152
|
encoding: 'utf8',
|
|
61
153
|
input: opts.input,
|
|
62
154
|
maxBuffer: MAX_BUFFER,
|
|
155
|
+
env: childEnv(opts),
|
|
63
156
|
})
|
|
64
157
|
return {
|
|
65
158
|
status: res.status ?? 1,
|
|
@@ -68,11 +161,35 @@ export function git(args: readonly string[], opts: GitOptions = {}): GitResult {
|
|
|
68
161
|
}
|
|
69
162
|
}
|
|
70
163
|
|
|
164
|
+
/**
|
|
165
|
+
* git, where the output is BYTES rather than text.
|
|
166
|
+
*
|
|
167
|
+
* One caller: reading a blob (`src/browse.ts`). A repository holds whatever was
|
|
168
|
+
* pushed to it, and decoding a PNG as UTF-8 replaces every byte git actually
|
|
169
|
+
* stored with U+FFFD — which is both the wrong file to hand back and a NUL
|
|
170
|
+
* sniff that says "text". `stderr` stays a string, because a diagnostic is one.
|
|
171
|
+
*/
|
|
172
|
+
export function gitBytes(
|
|
173
|
+
args: readonly string[],
|
|
174
|
+
opts: GitOptions = {},
|
|
175
|
+
): { status: number; stdout: Uint8Array; stderr: string } {
|
|
176
|
+
const res = spawnSync('git', argv(args, opts), {
|
|
177
|
+
input: opts.input,
|
|
178
|
+
maxBuffer: MAX_BUFFER,
|
|
179
|
+
env: childEnv(opts),
|
|
180
|
+
})
|
|
181
|
+
return {
|
|
182
|
+
status: res.status ?? 1,
|
|
183
|
+
stdout: res.stdout ?? new Uint8Array(),
|
|
184
|
+
stderr: res.stderr?.toString('utf8') ?? '',
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
71
188
|
/** git, where a non-zero exit is a bug rather than an answer. */
|
|
72
189
|
export function gitOrThrow(args: readonly string[], opts: GitOptions = {}): GitResult {
|
|
73
190
|
const res = git(args, opts)
|
|
74
191
|
if (res.status !== 0) {
|
|
75
|
-
throw new Error(`git ${args.join(' ')} failed: ${res.stderr.trim()}`)
|
|
192
|
+
throw new Error(`git ${argv(args, opts).join(' ')} failed: ${res.stderr.trim()}`)
|
|
76
193
|
}
|
|
77
194
|
return res
|
|
78
195
|
}
|
|
@@ -48,7 +48,7 @@ import {
|
|
|
48
48
|
signerListsEnabled,
|
|
49
49
|
} from './signers'
|
|
50
50
|
import { requireStore, storeFromEnv } from './store-env'
|
|
51
|
-
import { loadIndex, type LoadedIndex, type RefChange } from '
|
|
51
|
+
import { loadIndex, type LoadedIndex, type RefChange } from '../shared/wal-index'
|
|
52
52
|
|
|
53
53
|
const hook = process.argv[2]
|
|
54
54
|
const phase = process.argv[3]
|
|
@@ -10,9 +10,22 @@
|
|
|
10
10
|
* a container hold a token long before they hold an identity of any other kind.
|
|
11
11
|
*/
|
|
12
12
|
|
|
13
|
+
import {
|
|
14
|
+
BLOB_MAX_BYTES,
|
|
15
|
+
cleanPath,
|
|
16
|
+
defaultBranch,
|
|
17
|
+
OID,
|
|
18
|
+
readmeEntry,
|
|
19
|
+
resolveRef,
|
|
20
|
+
splitRefPath,
|
|
21
|
+
type Commit,
|
|
22
|
+
type TreeEntry,
|
|
23
|
+
} from '../shared/browse'
|
|
13
24
|
import { capabilitiesFrom, type Capabilities } from '../shared/capabilities'
|
|
14
25
|
import { authorizedBy, presentedSignature } from '../shared/credentials'
|
|
15
26
|
import {
|
|
27
|
+
BASIC_CHALLENGE,
|
|
28
|
+
BROWSE_PATH,
|
|
16
29
|
CHALLENGE_PATH,
|
|
17
30
|
EXPIRE_PATH,
|
|
18
31
|
HEALTH_PATH,
|
|
@@ -35,7 +48,7 @@ import type { ProposalListing } from './proposals'
|
|
|
35
48
|
import { countsNewRepos, createSourceLimiter, rateLimitsEnforced, rateLimitsOf } from './rate-limit'
|
|
36
49
|
import type { ResolvedRepo } from './repo'
|
|
37
50
|
import { resolveRepo } from './repo'
|
|
38
|
-
import type { Claim, Provenance } from '
|
|
51
|
+
import type { Claim, Provenance } from '../shared/wal-index'
|
|
39
52
|
|
|
40
53
|
export type BackendRequest = {
|
|
41
54
|
repo: ResolvedRepo
|
|
@@ -141,6 +154,22 @@ export type HttpHandlerDeps = {
|
|
|
141
154
|
* exactly the wrong thing to invent out of a missing log.
|
|
142
155
|
*/
|
|
143
156
|
readProposals?: (repoId: string) => Promise<ProposalListing[]>
|
|
157
|
+
/**
|
|
158
|
+
* What a browse of one repository reads (`shared/browse.ts`): the Index, for
|
|
159
|
+
* the refs the pages are built from, and the Cache, for one level of a tree.
|
|
160
|
+
*
|
|
161
|
+
* One group rather than two sibling options, like `privateReads` below and
|
|
162
|
+
* for the same reason: the two are worthless apart. The Index read alone
|
|
163
|
+
* could name refs and never open a directory; the tree read alone would have
|
|
164
|
+
* to ask the Cache which refs exist, which is exactly the answer a disposable
|
|
165
|
+
* cache may not give (docs/adr/0007).
|
|
166
|
+
*
|
|
167
|
+
* Optional, and absent means the endpoint does not exist rather than
|
|
168
|
+
* answering emptily — a deployment with no store has no Index to read, and
|
|
169
|
+
* "this repository holds no refs" invented out of a missing log is a page
|
|
170
|
+
* that says a repository is empty when the log says otherwise.
|
|
171
|
+
*/
|
|
172
|
+
browse?: BrowseReads
|
|
144
173
|
/**
|
|
145
174
|
* Read gating for Private repositories (docs/adr/0013), or `undefined` for a
|
|
146
175
|
* deployment that does none — which is every deployment until an operator
|
|
@@ -195,6 +224,71 @@ export type PrivateReads = {
|
|
|
195
224
|
now?: () => number
|
|
196
225
|
}
|
|
197
226
|
|
|
227
|
+
/** Everything a browse reads, present or absent as a whole. */
|
|
228
|
+
export type BrowseReads = {
|
|
229
|
+
/**
|
|
230
|
+
* The Index facts a browse needs — the full ref state and when the last push
|
|
231
|
+
* landed — or `null` when the log holds no Index for this name.
|
|
232
|
+
*
|
|
233
|
+
* `null` is the whole reason this is not `readRefs`: a repository with no
|
|
234
|
+
* Index and one whose Index holds no refs are different answers here. The
|
|
235
|
+
* first is a name nobody has pushed to, which is a 404 and must create
|
|
236
|
+
* nothing; the second is a repository, and gets a page.
|
|
237
|
+
*/
|
|
238
|
+
readIndex: (repoId: string) => Promise<BrowseIndex | null>
|
|
239
|
+
/**
|
|
240
|
+
* One level of the tree at `rev`:`path` on the Cache, or `null` when that
|
|
241
|
+
* path is not a directory there.
|
|
242
|
+
*
|
|
243
|
+
* Takes the already-ensured, already-synced repository rather than a name,
|
|
244
|
+
* because the ordering is the handler's: nothing may reach the Cache until
|
|
245
|
+
* the Index has said the repository exists.
|
|
246
|
+
*/
|
|
247
|
+
listTree: (repo: ResolvedRepo, rev: string, path: string) => Promise<TreeEntry[] | null>
|
|
248
|
+
/**
|
|
249
|
+
* What git knows about one path at one ref WITHOUT reading it: its object id
|
|
250
|
+
* and its size, or `null` when that path is not a blob there.
|
|
251
|
+
*
|
|
252
|
+
* Separate from `readBlob` deliberately, and the separation is the cap: the
|
|
253
|
+
* handler asks the size first and only then decides whether the bytes may be
|
|
254
|
+
* read at all (`BLOB_MAX_BYTES`). One reader that did both would have the
|
|
255
|
+
* file in memory before anyone could refuse it.
|
|
256
|
+
*/
|
|
257
|
+
statBlob: (
|
|
258
|
+
repo: ResolvedRepo,
|
|
259
|
+
rev: string,
|
|
260
|
+
path: string,
|
|
261
|
+
) => Promise<{ oid: string; size: number } | null>
|
|
262
|
+
/**
|
|
263
|
+
* The bytes of one blob, as bytes.
|
|
264
|
+
*
|
|
265
|
+
* Not a string: a repository holds whatever was pushed to it, and decoding
|
|
266
|
+
* before the NUL sniff would turn a PNG into replacement characters that
|
|
267
|
+
* sniff as text. The handler decides what, if anything, to decode.
|
|
268
|
+
*/
|
|
269
|
+
readBlob: (repo: ResolvedRepo, rev: string, path: string) => Promise<Uint8Array | null>
|
|
270
|
+
/**
|
|
271
|
+
* One page of history walking back from `rev`, or from `before` when the
|
|
272
|
+
* reader is paging. `null` when the Cache cannot walk it.
|
|
273
|
+
*
|
|
274
|
+
* The limit is the caller's rather than the reader's for the same reason the
|
|
275
|
+
* blob cap is: it is a bound on work this request may do, and the handler is
|
|
276
|
+
* where a request's bounds are stated.
|
|
277
|
+
*/
|
|
278
|
+
listCommits: (
|
|
279
|
+
repo: ResolvedRepo,
|
|
280
|
+
rev: string,
|
|
281
|
+
before: string | null,
|
|
282
|
+
limit: number,
|
|
283
|
+
) => Promise<Commit[] | null>
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
/** What a browse reads out of one Index. */
|
|
287
|
+
export type BrowseIndex = {
|
|
288
|
+
refs: Record<string, string>
|
|
289
|
+
lastPush: string | null
|
|
290
|
+
}
|
|
291
|
+
|
|
198
292
|
/** Verify a `walgit-read` signature over a message; name the key, or `null`. */
|
|
199
293
|
export type ReadVerifier = (message: string, signature: string) => string | null
|
|
200
294
|
|
|
@@ -229,11 +323,86 @@ const UNAUTHORIZED = () =>
|
|
|
229
323
|
// git prompts for a credential only when challenged in this scheme, so the
|
|
230
324
|
// header is what makes `git clone https://…` work interactively at all.
|
|
231
325
|
reject(401, 'unauthorized', 'unauthorized\n', {
|
|
232
|
-
'www-authenticate':
|
|
326
|
+
'www-authenticate': BASIC_CHALLENGE,
|
|
233
327
|
})
|
|
234
328
|
|
|
235
329
|
const NOT_FOUND = () => reject(404, 'not-found', 'not found\n')
|
|
236
330
|
|
|
331
|
+
/**
|
|
332
|
+
* A JSON answer, with the newline every other body here ends in.
|
|
333
|
+
*
|
|
334
|
+
* `no-store` because every browse answer is derived from where refs point
|
|
335
|
+
* RIGHT NOW: a cached copy is a directory listing for a branch that has since
|
|
336
|
+
* moved, and on a Private name it is a page served after a revocation. The edge
|
|
337
|
+
* puts its own cache header on the page it renders from this
|
|
338
|
+
* (`shared/browse.ts`), where the rule is about a document a browser holds
|
|
339
|
+
* rather than about the log.
|
|
340
|
+
*/
|
|
341
|
+
const json = (body: unknown): Response =>
|
|
342
|
+
new Response(`${JSON.stringify(body)}\n`, {
|
|
343
|
+
headers: {
|
|
344
|
+
'content-type': 'application/json; charset=utf-8',
|
|
345
|
+
'cache-control': 'no-store',
|
|
346
|
+
},
|
|
347
|
+
})
|
|
348
|
+
|
|
349
|
+
/**
|
|
350
|
+
* How many commits one history page holds.
|
|
351
|
+
*
|
|
352
|
+
* Fifty, like the tree's one level: a bound on work done on the request path in
|
|
353
|
+
* the one container that is also serving pushes. A reader who wants the whole
|
|
354
|
+
* history clones it — that is what walgit is.
|
|
355
|
+
*/
|
|
356
|
+
const LOG_PAGE = 50
|
|
357
|
+
|
|
358
|
+
/**
|
|
359
|
+
* One blob, as bytes, with the type its CONTENT earns and never its name.
|
|
360
|
+
*
|
|
361
|
+
* This is the security property of the whole web view, stated in one place. A
|
|
362
|
+
* repository holds whatever was pushed to it, and walgit serves it from the
|
|
363
|
+
* same origin as every other walgit page — so a pushed `index.html` returned as
|
|
364
|
+
* `text/html` is stored XSS against the deployment, and a pushed `.js` served
|
|
365
|
+
* as a script is worse. Nothing here looks at the extension:
|
|
366
|
+
*
|
|
367
|
+
* - text (no NUL byte) → `text/plain; charset=utf-8`, with `nosniff` so a
|
|
368
|
+
* browser that would have guessed HTML from the bytes does not;
|
|
369
|
+
* - anything else → `application/octet-stream` as an attachment, which a
|
|
370
|
+
* browser saves rather than opens.
|
|
371
|
+
*
|
|
372
|
+
* `no-store` for the reason every other browse answer has it: the bytes are
|
|
373
|
+
* whatever the ref points at right now, and on a Private name a cached copy is
|
|
374
|
+
* a file served after a revocation.
|
|
375
|
+
*/
|
|
376
|
+
function rawBlob(bytes: Uint8Array, path: string, binary: boolean): Response {
|
|
377
|
+
const name = path.slice(path.lastIndexOf('/') + 1)
|
|
378
|
+
const headers: Record<string, string> = {
|
|
379
|
+
'cache-control': 'no-store',
|
|
380
|
+
'x-content-type-options': 'nosniff',
|
|
381
|
+
// A raw file is somebody's source, not a document to index — and on a
|
|
382
|
+
// credentialed deployment not one a crawler can read at all.
|
|
383
|
+
'x-robots-tag': 'noindex',
|
|
384
|
+
}
|
|
385
|
+
if (binary) {
|
|
386
|
+
headers['content-type'] = 'application/octet-stream'
|
|
387
|
+
// The quotes are the whole grammar here, and `cleanPath` has already
|
|
388
|
+
// refused every segment walgit will not carry — but a name may still hold a
|
|
389
|
+
// quote or a newline, and either would end the header early. Both are
|
|
390
|
+
// stripped rather than escaped: this is a suggested filename, and a browser
|
|
391
|
+
// falling back to the URL's last segment is a fine outcome.
|
|
392
|
+
const safe = name.replace(/["\\\r\n]/g, '')
|
|
393
|
+
headers['content-disposition'] = `attachment; filename="${safe}"`
|
|
394
|
+
} else {
|
|
395
|
+
headers['content-type'] = 'text/plain; charset=utf-8'
|
|
396
|
+
}
|
|
397
|
+
// `slice()` rather than `bytes.buffer`, and the copy is the point: what
|
|
398
|
+
// `spawnSync` returns is a Node `Buffer`, which for a small payload is a VIEW
|
|
399
|
+
// into a shared pool — handing over its underlying buffer would ship the rest
|
|
400
|
+
// of that pool (another repository's bytes among it) at the wrong length.
|
|
401
|
+
// `slice` is a copy into an exactly-sized buffer, which is the one thing
|
|
402
|
+
// `BodyInit` can be handed without a claim about the allocator.
|
|
403
|
+
return new Response(bytes.slice().buffer as ArrayBuffer, { headers })
|
|
404
|
+
}
|
|
405
|
+
|
|
237
406
|
/**
|
|
238
407
|
* The Claim a repository is treated as holding when its real one cannot be
|
|
239
408
|
* read: Private, and nobody is listed.
|
|
@@ -351,11 +520,209 @@ function createRouter(deps: HttpHandlerDeps): (req: Request) => Promise<Response
|
|
|
351
520
|
// answer. Two header lines rather than one comma-joined value, because
|
|
352
521
|
// the nonce's `=` inside a comma-separated list is where parsers differ.
|
|
353
522
|
return reject(401, 'unauthorized', renderReadChallenge(publicOrigin(request, url)), [
|
|
354
|
-
['www-authenticate',
|
|
523
|
+
['www-authenticate', BASIC_CHALLENGE],
|
|
355
524
|
['www-authenticate', `${READ_CHALLENGE_SCHEME} nonce=${nonce}`],
|
|
356
525
|
])
|
|
357
526
|
}
|
|
358
527
|
|
|
528
|
+
/**
|
|
529
|
+
* One browse, in the order the rules demand (`shared/browse.ts`).
|
|
530
|
+
*
|
|
531
|
+
* The ORDER is the whole of it, and it is why this lives at the handler
|
|
532
|
+
* rather than behind one reader: the Index says whether the name exists, the
|
|
533
|
+
* Claim says whether this reader may see it, and only then may the Cache be
|
|
534
|
+
* touched at all. `ensureRepo` CREATES a directory, so a browse that reached
|
|
535
|
+
* it before the Index had answered would bring a name into existence by
|
|
536
|
+
* reading it — the defect `src/empty-read.ts` exists to prevent on the git
|
|
537
|
+
* path, arriving by a different door.
|
|
538
|
+
*/
|
|
539
|
+
async function browse(reads: BrowseReads, request: Request, url: URL): Promise<Response> {
|
|
540
|
+
let resolved: ResolvedRepo
|
|
541
|
+
try {
|
|
542
|
+
resolved = resolveRepo(deps.reposDir, url.searchParams.get('repo') ?? '')
|
|
543
|
+
} catch {
|
|
544
|
+
// A name walgit would not serve is a 404, exactly as it is on the git
|
|
545
|
+
// path — indistinguishable from a missing one, deliberately.
|
|
546
|
+
return NOT_FOUND()
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
// The Private gate FIRST, before the Index this endpoint reads: a refusal
|
|
550
|
+
// must not depend on whether the name exists, or the challenge itself
|
|
551
|
+
// becomes an oracle for which Private names a deployment holds.
|
|
552
|
+
if (priv) {
|
|
553
|
+
let claim: Claim | undefined
|
|
554
|
+
try {
|
|
555
|
+
claim = await priv.readClaim(resolved.repoId)
|
|
556
|
+
} catch {
|
|
557
|
+
claim = LOCKED
|
|
558
|
+
}
|
|
559
|
+
const refused = await refuseRead(request, url, claim)
|
|
560
|
+
if (refused) return refused
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
let index: BrowseIndex | null
|
|
564
|
+
try {
|
|
565
|
+
index = await reads.readIndex(resolved.repoId)
|
|
566
|
+
} catch (err) {
|
|
567
|
+
// Refused rather than answered empty, for the reason the Proposal read
|
|
568
|
+
// is: a repository reported as holding no refs, read out of an Index we
|
|
569
|
+
// could not reach, is a wrong answer a reader cannot tell from a right
|
|
570
|
+
// one.
|
|
571
|
+
return reject(503, 'unavailable', `walgit: ${(err as Error).message}\n`)
|
|
572
|
+
}
|
|
573
|
+
// No Index is a name nobody has pushed to. 404, and nothing created.
|
|
574
|
+
if (!index) return NOT_FOUND()
|
|
575
|
+
|
|
576
|
+
const refs = Object.keys(index.refs)
|
|
577
|
+
.toSorted()
|
|
578
|
+
.map((name) => ({ name, oid: index.refs[name]! }))
|
|
579
|
+
const head = defaultBranch(index.refs)
|
|
580
|
+
const common = { repo: resolved.repoId, defaultBranch: head, refs, lastPush: index.lastPush }
|
|
581
|
+
|
|
582
|
+
const op = url.searchParams.get('op')
|
|
583
|
+
if (op === 'refs') return json(common)
|
|
584
|
+
if (op !== 'tree' && op !== 'blob' && op !== 'raw' && op !== 'log') return NOT_FOUND()
|
|
585
|
+
|
|
586
|
+
// The history cursor, checked before anything else this request would do.
|
|
587
|
+
// A full oid or nothing: an abbreviation, a rev-parse expression and an
|
|
588
|
+
// option-shaped string are all refused here rather than resolved on the
|
|
589
|
+
// Cache, for the reason a ref is (`shared/browse.ts`). 400 rather than the
|
|
590
|
+
// 404 every other refusal here gets, because unlike a ref or a path this
|
|
591
|
+
// is not a thing that might have existed — a malformed cursor is a
|
|
592
|
+
// malformed request, and a reader paging by hand should be told so.
|
|
593
|
+
const before = url.searchParams.get('before')
|
|
594
|
+
if (before !== null && !OID.test(before)) {
|
|
595
|
+
return reject(400, 'invalid', 'walgit: a history cursor is a full object id\n')
|
|
596
|
+
}
|
|
597
|
+
|
|
598
|
+
// A ref and a path, from the one run-together remainder the browse URL
|
|
599
|
+
// carries — or from the two parameters an API caller may send instead,
|
|
600
|
+
// which resolve exactly the same way.
|
|
601
|
+
const requested = url.searchParams.get('ref') ?? ''
|
|
602
|
+
const rawPath = url.searchParams.get('path')
|
|
603
|
+
let at: { ref: string; path: string } | null
|
|
604
|
+
if (requested === '') {
|
|
605
|
+
// No ref named: the default branch, and a repository with no branch is a
|
|
606
|
+
// page with no tree rather than a refusal — it exists, it is just empty.
|
|
607
|
+
// A file and a download have nothing to be empty ABOUT, so those are the
|
|
608
|
+
// 404 a missing path always is.
|
|
609
|
+
if (head === null) {
|
|
610
|
+
if (op === 'tree') return json({ ...common, ref: '', path: '', entries: [] })
|
|
611
|
+
if (op === 'log') return json({ ...common, ref: '', commits: [], next: null })
|
|
612
|
+
return NOT_FOUND()
|
|
613
|
+
}
|
|
614
|
+
at = { ref: head, path: rawPath ?? '' }
|
|
615
|
+
} else if (rawPath === null) {
|
|
616
|
+
at = splitRefPath(index.refs, requested)
|
|
617
|
+
} else {
|
|
618
|
+
const ref = resolveRef(index.refs, requested)
|
|
619
|
+
at = ref === null ? null : { ref, path: rawPath }
|
|
620
|
+
}
|
|
621
|
+
// A ref that is neither an Index key nor a full oid. Not resolved against
|
|
622
|
+
// the Cache and not passed to git: 404.
|
|
623
|
+
if (!at) return NOT_FOUND()
|
|
624
|
+
|
|
625
|
+
// A path walgit will not carry is a 404, exactly as an unknown ref and an
|
|
626
|
+
// unroutable name are: "there is no such directory here" is the honest
|
|
627
|
+
// answer, and a refusal kind of its own would be a distinction a reader
|
|
628
|
+
// could use to tell a rejected shape from an absent one.
|
|
629
|
+
const path = cleanPath(at.path)
|
|
630
|
+
if (path === null) return NOT_FOUND()
|
|
631
|
+
|
|
632
|
+
const repo = deps.ensureRepo(resolved)
|
|
633
|
+
if (deps.syncRepo) {
|
|
634
|
+
try {
|
|
635
|
+
await deps.syncRepo(repo)
|
|
636
|
+
} catch (err) {
|
|
637
|
+
// Serving a tree from a Cache we could not verify against the log would
|
|
638
|
+
// show a directory that may already have been superseded. Refuse, as
|
|
639
|
+
// the git path does.
|
|
640
|
+
return reject(503, 'unavailable', `walgit: ${(err as Error).message}\n`)
|
|
641
|
+
}
|
|
642
|
+
}
|
|
643
|
+
|
|
644
|
+
// The oid the ref stands at, or the oid the reader named. Everything below
|
|
645
|
+
// reads the Cache AT that revision rather than at a name, so a ref moving
|
|
646
|
+
// between two reads of one page cannot show half of each.
|
|
647
|
+
const rev = index.refs[at.ref] ?? at.ref
|
|
648
|
+
|
|
649
|
+
if (op === 'log') {
|
|
650
|
+
const commits = await reads.listCommits(repo, rev, before, LOG_PAGE)
|
|
651
|
+
// A ref the Cache cannot walk — a cursor naming an object it does not
|
|
652
|
+
// hold, most often. 404, as an absent ref is.
|
|
653
|
+
if (commits === null) return NOT_FOUND()
|
|
654
|
+
return json({
|
|
655
|
+
...common,
|
|
656
|
+
ref: at.ref,
|
|
657
|
+
commits,
|
|
658
|
+
// A full page is the only thing that implies another one. The cursor is
|
|
659
|
+
// the last commit SHOWN, which the next page re-reads and drops — one
|
|
660
|
+
// duplicate row's worth of work to avoid naming an object the reader
|
|
661
|
+
// has not been shown.
|
|
662
|
+
next: commits.length < LOG_PAGE ? null : (commits[commits.length - 1]?.oid ?? null),
|
|
663
|
+
})
|
|
664
|
+
}
|
|
665
|
+
|
|
666
|
+
if (op === 'blob' || op === 'raw') {
|
|
667
|
+
// The size FIRST, from git's object header and not from the content: a
|
|
668
|
+
// file over the cap must never reach this process's memory.
|
|
669
|
+
const stat = await reads.statBlob(repo, rev, path)
|
|
670
|
+
// Not a blob at this ref — a directory, or nothing. 404, as everything
|
|
671
|
+
// absent here is.
|
|
672
|
+
if (stat === null) return NOT_FOUND()
|
|
673
|
+
|
|
674
|
+
// Raw has no cap. It is the answer FOR a file too big to render, and it
|
|
675
|
+
// is the same read a clone of the same repository would do anyway.
|
|
676
|
+
if (op === 'blob' && stat.size > BLOB_MAX_BYTES) {
|
|
677
|
+
return json({
|
|
678
|
+
...common,
|
|
679
|
+
ref: at.ref,
|
|
680
|
+
path,
|
|
681
|
+
size: stat.size,
|
|
682
|
+
text: null,
|
|
683
|
+
binary: false,
|
|
684
|
+
oversize: true,
|
|
685
|
+
})
|
|
686
|
+
}
|
|
687
|
+
|
|
688
|
+
const bytes = await reads.readBlob(repo, rev, path)
|
|
689
|
+
if (bytes === null) return NOT_FOUND()
|
|
690
|
+
const binary = bytes.includes(0)
|
|
691
|
+
|
|
692
|
+
if (op === 'raw') return rawBlob(bytes, path, binary)
|
|
693
|
+
return json({
|
|
694
|
+
...common,
|
|
695
|
+
ref: at.ref,
|
|
696
|
+
path,
|
|
697
|
+
size: stat.size,
|
|
698
|
+
// A binary file has no text to show, and decoding it would invent one.
|
|
699
|
+
text: binary ? null : new TextDecoder().decode(bytes),
|
|
700
|
+
binary,
|
|
701
|
+
oversize: false,
|
|
702
|
+
})
|
|
703
|
+
}
|
|
704
|
+
|
|
705
|
+
const entries = await reads.listTree(repo, rev, path)
|
|
706
|
+
// git has no empty tree, so "no entries" is the absence rather than an
|
|
707
|
+
// empty directory: the path is not a directory at this ref.
|
|
708
|
+
if (entries === null) return NOT_FOUND()
|
|
709
|
+
|
|
710
|
+
// The README, at the ROOT only — a README is what a repository says about
|
|
711
|
+
// itself, and one in `src/` is a note about that directory rather than a
|
|
712
|
+
// second front page. It is read under the same cap as any other file, and
|
|
713
|
+
// a binary or oversized one is simply not shown.
|
|
714
|
+
let readme: { name: string; text: string } | undefined
|
|
715
|
+
const wanted = path === '' ? readmeEntry(entries) : null
|
|
716
|
+
if (wanted && (wanted.size ?? 0) <= BLOB_MAX_BYTES) {
|
|
717
|
+
const bytes = await reads.readBlob(repo, rev, wanted.name)
|
|
718
|
+
if (bytes && !bytes.includes(0)) {
|
|
719
|
+
readme = { name: wanted.name, text: new TextDecoder().decode(bytes) }
|
|
720
|
+
}
|
|
721
|
+
}
|
|
722
|
+
|
|
723
|
+
return json({ ...common, ref: at.ref, path, entries, ...(readme ? { readme } : {}) })
|
|
724
|
+
}
|
|
725
|
+
|
|
359
726
|
return async (request) => {
|
|
360
727
|
const url = new URL(request.url)
|
|
361
728
|
|
|
@@ -509,6 +876,22 @@ function createRouter(deps: HttpHandlerDeps): (req: Request) => Promise<Response
|
|
|
509
876
|
})
|
|
510
877
|
}
|
|
511
878
|
|
|
879
|
+
// One repository's refs and one level of its tree — what the two web pages
|
|
880
|
+
// are rendered from (`shared/browse.ts`). Beside the provenance read and
|
|
881
|
+
// gated identically: the deployment credential above, then the Read
|
|
882
|
+
// Challenge below, because a browse is a read of the same repository a
|
|
883
|
+
// clone reads and must not be a second authorization model.
|
|
884
|
+
//
|
|
885
|
+
// It exists only where the deployment offers the web view — `caps.web`, the
|
|
886
|
+
// same field the list at the edge is claimed from — so with `WALGIT_WEB`
|
|
887
|
+
// off this path is a 404, which is what "with the flag off it does not
|
|
888
|
+
// exist" means.
|
|
889
|
+
if (url.pathname === BROWSE_PATH) {
|
|
890
|
+
const offered = (deps.capabilities ?? ADVERTISES_NOTHING).web
|
|
891
|
+
if (!offered || !deps.browse || request.method !== 'GET') return NOT_FOUND()
|
|
892
|
+
return browse(deps.browse, request, url)
|
|
893
|
+
}
|
|
894
|
+
|
|
512
895
|
// What Proposals a repository holds (docs/adr/0018). Beside the provenance
|
|
513
896
|
// read and gated identically — the deployment credential above, then the
|
|
514
897
|
// Read Challenge below — because it answers the same kind of question about
|
|
@@ -28,7 +28,7 @@
|
|
|
28
28
|
|
|
29
29
|
import type { Capabilities } from '../shared/capabilities'
|
|
30
30
|
import { describeBytes } from '../shared/policy'
|
|
31
|
-
import type { WalIndex } from '
|
|
31
|
+
import type { WalIndex } from '../shared/wal-index'
|
|
32
32
|
|
|
33
33
|
export interface Limits {
|
|
34
34
|
/** Largest single push, in bytes. `null` means unlimited. */
|
|
@@ -25,19 +25,13 @@ import * as os from 'node:os'
|
|
|
25
25
|
import * as path from 'node:path'
|
|
26
26
|
|
|
27
27
|
import { compact } from './compact'
|
|
28
|
-
import { indexKey, siblingIdx, walKey } from '
|
|
28
|
+
import { indexKey, siblingIdx, walKey } from '../shared/keys'
|
|
29
29
|
import { materialize } from './materialize'
|
|
30
30
|
import { resolveRepo } from './repo'
|
|
31
31
|
import { FileStore } from './store'
|
|
32
|
-
import { ulid } from '
|
|
33
|
-
import {
|
|
34
|
-
|
|
35
|
-
emptyIndex,
|
|
36
|
-
loadIndex,
|
|
37
|
-
sha256,
|
|
38
|
-
type WalEntry,
|
|
39
|
-
type WalIndex,
|
|
40
|
-
} from './wal-index'
|
|
32
|
+
import { ulid } from '../shared/ulid'
|
|
33
|
+
import { emptyIndex, loadIndex, type WalEntry, type WalIndex } from '../shared/wal-index'
|
|
34
|
+
import { commitIndex, sha256 } from './wal-index'
|
|
41
35
|
|
|
42
36
|
interface Sample {
|
|
43
37
|
entries: number
|