compmd_host/registry/step.rs
1//! What a resolution asks its host to do, and what the host answers ([the registry rules], decision 2): the I/O of
2//! resolving, and nothing else. Both are plain data, so a host in JavaScript passes them as JSON.
3//!
4//! [the registry rules]: https://github.com/comp-markdown/compmd/blob/main/docs/registry-rules.md
5
6use std::collections::BTreeMap;
7
8use compmd_pack::{Integrity, PackName};
9use semver::Version;
10use serde::{Deserialize, Serialize};
11
12use super::metadata::VersionInfo;
13use super::resolved::Release;
14use super::tar::split;
15
16/// A step, by the number the resolution gave it: the host answers it with the same number.
17pub type StepId = u64;
18
19/// A pack an import, or a pack's `peerDependencies`, names that the request has no release of: what the processor's
20/// `sources` lists (`NeededPack`).
21#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
22pub struct Needed {
23 /// The pack.
24 pub pack: PackName,
25 /// The version or range, as written; `*` for none.
26 pub range: String,
27 /// The integrity an import pins with an exact version.
28 #[serde(default, skip_serializing_if = "Option::is_none")]
29 pub integrity: Option<Integrity>,
30 /// Whether it is a library pack, whose version every range asked for it matches.
31 #[serde(default)]
32 pub library: bool,
33}
34
35/// A release as a host's cache keeps it: its pack, version and tarball's integrity, so one pack's files are never
36/// given as another's, whatever integrity an import pins (audit S4).
37#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
38pub struct KeptRelease {
39 /// The pack.
40 pub pack: PackName,
41 /// The exact version.
42 pub version: Version,
43 /// The tarball's integrity, `sha512-…`.
44 pub integrity: String,
45}
46
47impl KeptRelease {
48 /// The release as one key: two are the same only when their pack, version and integrity are.
49 pub(crate) fn key(&self) -> String {
50 format!("{}@{}#{}", self.pack, self.version, self.integrity)
51 }
52}
53
54/// A release's checked files: its text, which the processor reads, and the rest as bytes, its images among them.
55#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
56pub struct Files {
57 /// The UTF-8 files, by path in the package.
58 pub text: BTreeMap<String, String>,
59 /// The other files, by path in the package.
60 #[serde(default)]
61 pub binary: BTreeMap<String, Vec<u8>>,
62}
63
64/// A release's files as bytes, split: the UTF-8 ones are its text, the others stay bytes.
65impl From<BTreeMap<String, Vec<u8>>> for Files {
66 fn from(files: BTreeMap<String, Vec<u8>>) -> Self {
67 let (text, binary) = split(files);
68 Self { text, binary }
69 }
70}
71
72impl Files {
73 /// Their size in bytes, as a registry's `unpackedSize` counts it: each file's bytes.
74 pub(crate) fn size(&self) -> u64 {
75 let text: usize = self.text.values().map(String::len).sum();
76 let binary: usize = self.binary.values().map(Vec::len).sum();
77 u64::try_from(text + binary).unwrap_or(u64::MAX)
78 }
79}
80
81/// A pack's versions as a host's cache keeps them, and whether the list is fresh enough to use without asking the
82/// registry: the host's clock says.
83#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
84pub struct KeptVersions {
85 /// The versions.
86 pub versions: Vec<VersionInfo>,
87 /// Whether they may be used as they are.
88 pub fresh: bool,
89}
90
91/// What the resolution needs the host to do.
92#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
93#[serde(
94 tag = "kind",
95 rename_all = "camelCase",
96 rename_all_fields = "camelCase"
97)]
98pub enum Step {
99 /// Add `releases` to the request's packs and `refused` to its policy's, then ask the processor which packs the
100 /// request still imports that it has no release of (`sources`): [`Answer::Missing`].
101 Missing {
102 /// The step's number.
103 id: StepId,
104 /// The releases resolved since the last time, by pack.
105 releases: Vec<(PackName, Release)>,
106 /// The packs the host's size cap refuses, each with why, for `policy.refused`.
107 refused: BTreeMap<PackName, String>,
108 },
109 /// Read the cache's list of `pack`'s versions at `registry`: [`Answer::KeptVersions`].
110 KeptVersions {
111 /// The step's number.
112 id: StepId,
113 /// The registry's URL.
114 registry: String,
115 /// The pack.
116 pack: PackName,
117 },
118 /// Ask the registry at `registry` for `pack`'s versions: [`Answer::Versions`], none when it doesn't have the pack,
119 /// or [`Answer::Failed`]. A registry that speaks npm's protocol GETs [`super::metadata_url`] and reads the answer
120 /// with [`super::answered_versions`]; a host's own registry gives its versions as it has them.
121 Versions {
122 /// The step's number.
123 id: StepId,
124 /// The registry's URL.
125 registry: String,
126 /// The pack.
127 pack: PackName,
128 },
129 /// Keep `pack`'s `versions` at `registry` in the cache, for `max_age` seconds: [`Answer::Done`].
130 KeepVersions {
131 /// The step's number.
132 id: StepId,
133 /// The registry's URL.
134 registry: String,
135 /// The pack.
136 pack: PackName,
137 /// The versions.
138 versions: Vec<VersionInfo>,
139 /// How long they may be kept, from the registry's `Cache-Control`.
140 max_age: u64,
141 },
142 /// Read the cache's files of `release`: [`Answer::Kept`].
143 Kept {
144 /// The step's number.
145 id: StepId,
146 /// The release.
147 release: KeptRelease,
148 },
149 /// Get `version`'s tarball from the registry at `registry`, its bytes as they come, stopping past `max_bytes`:
150 /// [`Answer::Tarball`], or [`Answer::Failed`]. A registry that speaks npm's protocol fetches it from its own origin
151 /// only ([`super::off_origin`]); the resolution checks the bytes, whoever gives them.
152 Tarball {
153 /// The step's number.
154 id: StepId,
155 /// The registry's URL.
156 registry: String,
157 /// The version, with where its tarball is.
158 version: VersionInfo,
159 /// The most bytes it may have.
160 max_bytes: u64,
161 },
162 /// Keep `release`'s checked `files` in the cache: [`Answer::Done`].
163 Keep {
164 /// The step's number.
165 id: StepId,
166 /// The release.
167 release: KeptRelease,
168 /// Its files.
169 files: Files,
170 },
171}
172
173impl Step {
174 /// Its number, which the answer goes with.
175 #[must_use]
176 pub fn id(&self) -> StepId {
177 match self {
178 Self::Missing { id, .. }
179 | Self::KeptVersions { id, .. }
180 | Self::Versions { id, .. }
181 | Self::KeepVersions { id, .. }
182 | Self::Kept { id, .. }
183 | Self::Tarball { id, .. }
184 | Self::Keep { id, .. } => *id,
185 }
186 }
187}
188
189/// What the host did.
190#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
191#[serde(
192 tag = "kind",
193 rename_all = "camelCase",
194 rename_all_fields = "camelCase"
195)]
196pub enum Answer {
197 /// The packs the request still imports that it has no release of.
198 Missing {
199 /// The packs, as the processor lists them.
200 packs: Vec<Needed>,
201 },
202 /// The cache's list, if it has one.
203 KeptVersions {
204 /// The list.
205 kept: Option<KeptVersions>,
206 },
207 /// The pack's versions at the registry, none when it doesn't have the pack, and how long the list may be kept: for
208 /// npm's protocol, the `max-age` of the answer's `Cache-Control` (0 without one).
209 Versions {
210 /// The versions.
211 versions: Vec<VersionInfo>,
212 /// The seconds it may be kept.
213 max_age: u64,
214 },
215 /// The cache's files of a release, if it has them all (its binary files too, when it keeps them).
216 Kept {
217 /// The files.
218 files: Option<Files>,
219 },
220 /// The tarball's bytes, as they came.
221 Tarball {
222 /// The bytes.
223 bytes: Vec<u8>,
224 },
225 /// A request that got no answer, a registry that failed, or a tarball larger than allowed, with why.
226 Failed {
227 /// Why.
228 message: String,
229 },
230 /// A step that gives nothing back was done.
231 Done,
232}