@zabaca/zbc 0.21.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 +25 -0
- package/templates/apps/walgit/README.md +152 -4
- package/templates/apps/walgit/e2e/harness.ts +2 -2
- package/templates/apps/walgit/e2e/scenarios.ts +302 -2
- 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 +25 -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 +120 -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 +68 -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 +232 -1
- /package/templates/apps/walgit/{src → shared}/keys.ts +0 -0
- /package/templates/apps/walgit/{src → shared}/ulid.ts +0 -0
|
@@ -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
|
|
@@ -38,12 +38,13 @@ import * as path from 'node:path'
|
|
|
38
38
|
|
|
39
39
|
import { ensureBareRepo } from './cache'
|
|
40
40
|
import { git } from './git'
|
|
41
|
-
import { siblingIdx } from '
|
|
41
|
+
import { siblingIdx } from '../shared/keys'
|
|
42
42
|
import { acquireLock, type LockRelease } from './mkdir-lock'
|
|
43
43
|
import { reconcile, type ReconcileResult } from './reconcile'
|
|
44
44
|
import type { ResolvedRepo } from './repo'
|
|
45
|
-
import type { ObjectStore } from '
|
|
46
|
-
import { loadIndex,
|
|
45
|
+
import type { ObjectStore } from '../shared/store'
|
|
46
|
+
import { loadIndex, type WalEntry, type WalIndex } from '../shared/wal-index'
|
|
47
|
+
import { sha256 } from './wal-index'
|
|
47
48
|
|
|
48
49
|
/**
|
|
49
50
|
* Removed only when a materialize completes. Its presence means a previous
|
|
@@ -233,7 +234,13 @@ async function placeEntry(store: ObjectStore, packDir: string, entry: WalEntry):
|
|
|
233
234
|
} else {
|
|
234
235
|
// Only when the log predates uploading the index, or the sibling was lost.
|
|
235
236
|
// It is the expensive path the `.idx` upload exists to avoid, so it says so.
|
|
236
|
-
|
|
237
|
+
// `index-pack` is one of the few git commands that parses its own
|
|
238
|
+
// arguments rather than going through parse-options: it accepts neither
|
|
239
|
+
// `--end-of-options` nor `--`, so the pack path goes in the argument
|
|
240
|
+
// vector directly. It is safe there because it is not user input — it is
|
|
241
|
+
// an absolute path this function just built under the repository's own
|
|
242
|
+
// pack directory.
|
|
243
|
+
const built = git([`index-pack`, `${base}.pack`])
|
|
237
244
|
if (built.status !== 0) {
|
|
238
245
|
throw new Error(`walgit: could not index ${entry.key}: ${built.stderr.trim()}`)
|
|
239
246
|
}
|
|
@@ -19,9 +19,9 @@
|
|
|
19
19
|
* garbage.
|
|
20
20
|
*/
|
|
21
21
|
|
|
22
|
-
import { siblingIdx, walPrefix } from '
|
|
23
|
-
import type { ObjectStore } from '
|
|
24
|
-
import { loadIndex } from '
|
|
22
|
+
import { siblingIdx, walPrefix } from '../shared/keys'
|
|
23
|
+
import type { ObjectStore } from '../shared/store'
|
|
24
|
+
import { loadIndex } from '../shared/wal-index'
|
|
25
25
|
|
|
26
26
|
export interface Orphan {
|
|
27
27
|
key: string
|
|
@@ -17,7 +17,7 @@ import * as fs from 'node:fs'
|
|
|
17
17
|
import * as path from 'node:path'
|
|
18
18
|
|
|
19
19
|
import type { PushSigner } from './signers'
|
|
20
|
-
import type { Claim, Provenance, WalEntry } from '
|
|
20
|
+
import type { Claim, Provenance, WalEntry } from '../shared/wal-index'
|
|
21
21
|
|
|
22
22
|
/**
|
|
23
23
|
* What `pre-receive` hands to `reference-transaction`.
|
|
@@ -19,7 +19,7 @@ import { createHmac } from 'node:crypto'
|
|
|
19
19
|
|
|
20
20
|
import { flagEnabled, seedValue } from '../shared/policy'
|
|
21
21
|
import { CHALLENGE_PATH, READ_CHALLENGE_NAMESPACE } from '../shared/protocol'
|
|
22
|
-
import type { Claim } from '
|
|
22
|
+
import type { Claim } from '../shared/wal-index'
|
|
23
23
|
|
|
24
24
|
/**
|
|
25
25
|
* The variables this module reads, named so a literal fixture is checked: a
|
|
@@ -39,7 +39,7 @@
|
|
|
39
39
|
import { flagEnabled } from '../shared/policy'
|
|
40
40
|
import { SIGNERS_REF, ZERO_OID } from '../shared/protocol'
|
|
41
41
|
import { git } from './git'
|
|
42
|
-
import type { Provenance, RefChange } from '
|
|
42
|
+
import type { Provenance, RefChange } from '../shared/wal-index'
|
|
43
43
|
|
|
44
44
|
/** The env flag an instance sets to take Proposals. */
|
|
45
45
|
export function proposalsEnabled(env: Record<string, string | undefined> = process.env): boolean {
|
|
@@ -140,7 +140,7 @@ export interface ProposalSource {
|
|
|
140
140
|
/** The real object reader: the pushed objects are in the hook's object path. */
|
|
141
141
|
export function gitObjectType(gitDir: string): (oid: string) => string | null {
|
|
142
142
|
return (oid) => {
|
|
143
|
-
const res = git(['
|
|
143
|
+
const res = git(['cat-file', '-t'], { gitDir, operands: [oid], inheritObjects: true })
|
|
144
144
|
if (res.status !== 0) return null
|
|
145
145
|
const type = res.stdout.trim()
|
|
146
146
|
return type === '' ? null : type
|
|
@@ -308,7 +308,7 @@ export type Ancestry = (tip: string, ancestorOf: string) => boolean
|
|
|
308
308
|
*/
|
|
309
309
|
export function gitAncestry(gitDir: string): Ancestry {
|
|
310
310
|
return (tip, ancestorOf) =>
|
|
311
|
-
git(['
|
|
311
|
+
git(['merge-base', '--is-ancestor'], { gitDir, operands: [tip, ancestorOf] }).status === 0
|
|
312
312
|
}
|
|
313
313
|
|
|
314
314
|
/**
|
|
@@ -123,9 +123,18 @@ export type BlobReader = (oid: string) => string | null
|
|
|
123
123
|
* no special handling, and returns `null` rather than throwing once the
|
|
124
124
|
* quarantine is gone (which is what makes calling this from a later hook a
|
|
125
125
|
* no-op rather than a crash).
|
|
126
|
+
*
|
|
127
|
+
* The repository and the quarantine are both named rather than inherited: the
|
|
128
|
+
* wrapper hands git no ambient `GIT_*` (see `git.ts`), so `$GIT_DIR` — which
|
|
129
|
+
* git sets for the hook, and which is the repository this certificate belongs
|
|
130
|
+
* to — is read here and passed on.
|
|
126
131
|
*/
|
|
127
132
|
function catBlob(oid: string): string | null {
|
|
128
|
-
const res = git(['cat-file', 'blob',
|
|
133
|
+
const res = git(['cat-file', 'blob'], {
|
|
134
|
+
gitDir: process.env.GIT_DIR ?? '.',
|
|
135
|
+
operands: [oid],
|
|
136
|
+
inheritObjects: true,
|
|
137
|
+
})
|
|
129
138
|
return res.status === 0 ? res.stdout : null
|
|
130
139
|
}
|
|
131
140
|
|
|
@@ -21,7 +21,7 @@ import * as fs from 'node:fs'
|
|
|
21
21
|
import * as path from 'node:path'
|
|
22
22
|
|
|
23
23
|
import { ZERO_OID } from '../shared/protocol'
|
|
24
|
-
import { walKey } from '
|
|
24
|
+
import { walKey } from '../shared/keys'
|
|
25
25
|
import { writePending, type PendingPush } from './pending'
|
|
26
26
|
import { certSigner } from './push-cert'
|
|
27
27
|
import {
|
|
@@ -31,22 +31,24 @@ import {
|
|
|
31
31
|
type ProposalGate,
|
|
32
32
|
type PushSigner,
|
|
33
33
|
} from './signers'
|
|
34
|
-
import type { ObjectStore } from '
|
|
35
|
-
import { ulid } from '
|
|
34
|
+
import type { ObjectStore } from '../shared/store'
|
|
35
|
+
import { ulid } from '../shared/ulid'
|
|
36
36
|
import {
|
|
37
|
-
applyClaim,
|
|
38
|
-
applyProvenance,
|
|
39
|
-
applyRefChanges,
|
|
40
|
-
commitIndex,
|
|
41
37
|
loadIndex,
|
|
42
|
-
nextIndex,
|
|
43
|
-
sha256,
|
|
44
38
|
type Claim,
|
|
45
39
|
type Provenance,
|
|
46
40
|
type PushRecord,
|
|
47
41
|
type RefChange,
|
|
48
42
|
type WalEntry,
|
|
49
43
|
type WalIndex,
|
|
44
|
+
} from '../shared/wal-index'
|
|
45
|
+
import {
|
|
46
|
+
applyClaim,
|
|
47
|
+
applyProvenance,
|
|
48
|
+
applyRefChanges,
|
|
49
|
+
commitIndex,
|
|
50
|
+
nextIndex,
|
|
51
|
+
sha256,
|
|
50
52
|
} from './wal-index'
|
|
51
53
|
|
|
52
54
|
/**
|
|
@@ -19,7 +19,7 @@ import * as fs from 'node:fs'
|
|
|
19
19
|
import * as path from 'node:path'
|
|
20
20
|
|
|
21
21
|
import { git } from './git'
|
|
22
|
-
import type { WalIndex } from '
|
|
22
|
+
import type { WalIndex } from '../shared/wal-index'
|
|
23
23
|
|
|
24
24
|
export interface ReconcileResult {
|
|
25
25
|
changed: boolean
|
|
@@ -37,7 +37,7 @@ export interface ReconcileResult {
|
|
|
37
37
|
}
|
|
38
38
|
|
|
39
39
|
export function localRefs(gitDir: string): Record<string, string> {
|
|
40
|
-
const res = git(['
|
|
40
|
+
const res = git(['for-each-ref', '--format=%(objectname) %(refname)'], { gitDir })
|
|
41
41
|
if (res.status !== 0) throw new Error(`for-each-ref failed: ${res.stderr.trim()}`)
|
|
42
42
|
const refs: Record<string, string> = {}
|
|
43
43
|
for (const line of res.stdout.split('\n')) {
|
|
@@ -50,7 +50,8 @@ export function localRefs(gitDir: string): Record<string, string> {
|
|
|
50
50
|
/** Which of these oids the local object store actually has. One process. */
|
|
51
51
|
export function presentObjects(gitDir: string, oids: readonly string[]): Set<string> {
|
|
52
52
|
if (oids.length === 0) return new Set()
|
|
53
|
-
const res = git(['
|
|
53
|
+
const res = git(['cat-file', '--batch-check'], {
|
|
54
|
+
gitDir,
|
|
54
55
|
input: `${oids.join('\n')}\n`,
|
|
55
56
|
})
|
|
56
57
|
const present = new Set<string>()
|