| 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 | // 1. POST ManifestPath with the signed manifest as the body. The |
| 35 | // receiver admits it with VerifyManifest and stages it with |
| 36 | // Store.Stage. It replies 200 with an Inventory: the export it |
| 37 | // serves, with its sequence and content digest; per project, the |
| 38 | // source tip it holds a verified repository at; and the bodies it |
| 39 | // still needs. Posting the same manifest again is harmless and gets |
| 40 | // the same inventory, less what has been sent since. A publisher may |
| 41 | // stop here when the inventory shows the receiver serves its digest |
| 42 | // and needs no bundle; the next manifest it posts discards the |
| 43 | // staged upload. |
| 44 | // 2. PUT BodyPath(sequence, sha256) for each needed body, export.json |
| 45 | // and the bundles, with the body's bytes as they are: no |
| 46 | // Content-Encoding, and a Content-Length, when given, equal to the |
| 47 | // declared size. The receiver streams it into staging with |
| 48 | // StageBody, and replies 204 once it verified. A body already staged |
| 49 | // is accepted again without being read. |
| 50 | // 3. POST CommitPath(sequence) with an empty body. The receiver |
| 51 | // activates the upload with Store.Activate only when every body has |
| 52 | // verified, its bundles pass AcceptBundle, and export.json agrees |
| 53 | // with the manifest. It replies 200 with Committed. |
| 54 | // |
| 55 | // A refusal is replied with Code.Status and an ErrorReply, whose error is |
| 56 | // a Code and stable. What each step refuses: |
| 57 | // |
| 58 | // - step 1: too-large, bad-signature, bad-manifest, wrong-instance, |
| 59 | // wrong-destination, replayed, out-of-window, in VerifyManifest's |
| 60 | // order; superseded when a newer manifest is staged; |
| 61 | // - step 2: no-manifest when nothing is staged for the sequence, |
| 62 | // superseded when a newer manifest replaced it, not-declared for a |
| 63 | // hash the manifest does not declare, too-large, content-encoding, |
| 64 | // size-mismatch, hash-mismatch; |
| 65 | // - step 3: no-manifest, superseded, replayed, incomplete with the |
| 66 | // bodies still needed, bad-export, bad-bundle. |
| 67 | // |
| 68 | // A replayed manifest, or one older than the receiver's high-water mark, |
| 69 | // is refused as replayed, with the high-water mark. The publisher's |
| 70 | // clock is behind the receiver's record, and it waits for its clock to |
| 71 | // pass the mark rather than publish out of order. A refused upload |
| 72 | // leaves the active export as it was. |
| 73 | // |
| 74 | // A commit has succeeded once the receiver has written its sequence to |
| 75 | // the high-water mark and swapped the active export to it, and the reply |
| 76 | // is then 200 with Committed. A later failure does not change the reply: |
| 77 | // deleting old exports, stale uploads and the trash, or syncing the swap |
| 78 | // to disk. Store passes it to the report function given to OpenStore, |
| 79 | // for the receiver to log, and the next sweep or OpenStore repairs it. A |
| 80 | // commit that fails before then leaves the active export as it was. If |
| 81 | // it failed after the high-water mark was written, its sequence is |
| 82 | // accepted, and the commit retried is refused as replayed. The |
| 83 | // publisher's next manifest, signed later, replaces the upload. |
| 84 | // |
| 85 | // # The receiver's state |
| 86 | // |
| 87 | // Store is the receiver's state directory: its generations, the active |
| 88 | // symlink, the high-water mark and the staging area. Open resolves the |
| 89 | // active generation once per request and holds it until released, and |
| 90 | // Activate makes a staged upload the active generation. The server and |
| 91 | // the receiver both use them. |
| 92 | package export |