gunk-dev/the-valley

doc.go

sourcec0b8faba075dmain's published tip history
5.5 KiB
1// Package export is the contract between the web UI's publisher, which
2// runs on a valley host, and its receiver and server, which run on a web
3// server the host pushes to and never hears back from. Both sides build
4// against this package, so neither reinvents its rules.
5//
6// # The export
7//
8// An export is export.json and one git bundle of main per project. It
9// holds only public projects, and only what an anonymous reader of one
10// may see. Build projects the valley's own JSON onto it: `valley plan
11// --all --json`, `valley pending --all --json`, and `valley knot show
12// --project P <id> --json` for each knot of each public project, with
13// each project's visibility, its main, and a display mapping for
14// principal names. Visibility is per project: every knot and comment of
15// an exported project is exported.
16//
17// Digest is the export's content digest. It leaves out every
18// display-time field, so the publisher uploads only when the valley
19// changed, or when the receiver's inventory says it lacks something.
20//
21// # The manifest
22//
23// A Manifest is a signed note that names the instance that signed it,
24// the destination it is for, its sequence (the signing time in Unix
25// nanoseconds), export.json's hash and size, and each project's
26// visibility, graph tip, source tip and bundle. VerifyManifest admits one
27// in a fixed order of steps, and CheckExport holds export.json to it.
28//
29// # The upload
30//
31// The publisher uploads an export to the receiver in three steps, each
32// one HTTP request. Every reply is JSON.
33//
34// Every request carries the receiver's token, which ReadToken reads, in
35// its Authorization header (Authorization). The token authenticates the
36// transport; the manifest's signature still authenticates the export. The
37// receiver refuses a request without the token as unauthenticated, with
38// 401, before it reads the body or anything else of the request.
39//
40// 1. POST ManifestPath with the signed manifest as the body. The
41// receiver admits it with VerifyManifest, holds the bodies it declares
42// to the receiver's caps, and stages it with Store.Stage. It replies
43// 200 with an Inventory: the export it
44// serves, with its sequence and content digest; per project, the
45// source tip it holds a verified repository at; and the bodies it
46// still needs. Posting the same manifest again is harmless and gets
47// the same inventory, less what has been sent since. A publisher may
48// stop here when the inventory shows the receiver serves its digest
49// and needs no bundle; the next manifest it posts discards the
50// staged upload.
51// 2. PUT BodyPath(sequence, sha256) for each needed body, export.json
52// and the bundles, with the body's bytes as they are: no
53// Content-Encoding, and a Content-Length, when given, equal to the
54// declared size. The receiver streams it into staging with
55// StageBody, and replies 204 once it verified. A body already staged
56// is accepted again without being read.
57// 3. POST CommitPath(sequence) with an empty body. The receiver
58// activates the upload with Store.Activate only when every body has
59// verified, its bundles pass AcceptBundle, and export.json agrees
60// with the manifest. It replies 200 with Committed.
61//
62// A refusal is replied with Code.Status and an ErrorReply, whose error is
63// a Code and stable. What each step refuses:
64//
65// - every step: unauthenticated, before anything else;
66// - step 1: too-large, bad-signature, bad-manifest, wrong-instance,
67// wrong-destination, replayed, out-of-window, in VerifyManifest's
68// order; then too-large for a body over the receiver's cap, or bodies
69// over its cap on one upload together; superseded when a newer
70// manifest is staged;
71// - step 2: no-manifest when nothing is staged for the sequence,
72// superseded when a newer manifest replaced it, not-declared for a
73// hash the manifest does not declare, too-large, content-encoding,
74// size-mismatch, hash-mismatch;
75// - step 3: no-manifest, superseded, replayed, incomplete with the
76// bodies still needed, bad-export, bad-bundle.
77//
78// A replayed manifest, or one older than the receiver's high-water mark,
79// is refused as replayed, with the high-water mark. The publisher's
80// clock is behind the receiver's record, and it waits for its clock to
81// pass the mark rather than publish out of order. A refused upload
82// leaves the active export as it was.
83//
84// A commit has succeeded once the receiver has written its sequence to
85// the high-water mark and swapped the active export to it, and the reply
86// is then 200 with Committed. A later failure does not change the reply:
87// deleting old exports, stale uploads and the trash, or syncing the swap
88// to disk. Store passes it to the report function given to OpenStore,
89// for the receiver to log, and the next sweep or OpenStore repairs it. A
90// commit that fails before then leaves the active export as it was. If
91// it failed after the high-water mark was written, its sequence is
92// accepted, and the commit retried is refused as replayed. The
93// publisher's next manifest, signed later, replaces the upload.
94//
95// # The receiver's state
96//
97// Store is the receiver's state directory: its generations, the active
98// symlink, the high-water mark and the staging area. Open resolves the
99// active generation once per request and holds it until released, and
100// Activate makes a staged upload the active generation. The receiver
101// opens the state directory with OpenStore, and the server with
102// OpenReadOnly, which needs only read access to it.
103package export