-
Notifications
You must be signed in to change notification settings - Fork 117
Expand file tree
/
Copy pathmanifest.rs
More file actions
390 lines (350 loc) · 13.1 KB
/
manifest.rs
File metadata and controls
390 lines (350 loc) · 13.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
//! Manifest fetching with retry for registry operations.
//!
//! Uses the shared classification + backoff machinery from
//! [`crate::service::fetch`] so retry policy stays uniform across registry
//! manifest fetches and non-registry resolvers (git, http tarball).
use anyhow::{Result, anyhow};
use bytes::Bytes;
use tokio_retry::RetryIf;
use super::fetch::{
FetchError, classify_reqwest_error, classify_status, is_retryable, retry_strategy,
};
use super::http::get_client;
use crate::model::manifest::{CoreVersionManifest, FullManifest};
use crate::resolver::version::resolve_target_version;
/// Parse a JSON buffer on rayon's CPU thread pool (native) or inline
/// (wasm32). The buffer is consumed because `simd_json` mutates it in-place.
/// Keeps the tokio runtime free of `simd_json` work so other in-flight
/// manifest fetches keep driving network IO while this one parses.
pub(crate) async fn parse_json_vec_off_runtime<T>(
mut parse_buf: Vec<u8>,
) -> Result<T, anyhow::Error>
where
T: serde::de::DeserializeOwned + Send + 'static,
{
#[cfg(not(target_arch = "wasm32"))]
{
let (tx, rx) = tokio::sync::oneshot::channel();
rayon::spawn(move || {
let result = simd_json::serde::from_slice::<T>(&mut parse_buf)
.map_err(|e| anyhow!("JSON parse error: {e}"));
let _ = tx.send(result);
});
rx.await
.map_err(|e| anyhow!("rayon parse channel closed: {e}"))?
}
#[cfg(target_arch = "wasm32")]
{
simd_json::serde::from_slice::<T>(&mut parse_buf)
.map_err(|e| anyhow!("JSON parse error: {e}"))
}
}
/// Parse a full wire-fetched manifest and restore its raw byte payload.
///
/// Intended for the BFS resolver loop follow-up: fetch tasks can return bytes
/// while the loop owns cache/waiter/inflight state and chooses when to parse.
pub(crate) async fn parse_full_manifest_off_runtime(
raw_bytes: Bytes,
) -> Result<FullManifest, anyhow::Error> {
parse_full_manifest_with_core_off_runtime(raw_bytes, None)
.await
.map(|(manifest, _)| manifest)
}
pub(crate) type FullManifestParseResult = (FullManifest, Option<(String, CoreVersionManifest)>);
fn parse_full_manifest_with_core_sync(
raw_bytes: Bytes,
spec: Option<String>,
) -> Result<FullManifestParseResult, anyhow::Error> {
// simd_json mutates the parse buffer; copy inside the worker so
// response-body bytes can stay immutable until parsing starts.
let mut parse_buf = raw_bytes.to_vec();
let mut manifest: FullManifest = simd_json::serde::from_slice::<FullManifest>(&mut parse_buf)
.map_err(|e| anyhow!("JSON parse error: {e}"))?;
manifest.raw = raw_bytes;
let speculative = spec.and_then(|spec| {
resolve_target_version((&manifest).into(), &spec)
.ok()
.and_then(|version| manifest.get_core_version(&version).map(|core| (spec, core)))
});
Ok((manifest, speculative))
}
/// Parse a full wire-fetched manifest and optionally extract the current BFS
/// edge's version in the same off-runtime worker task.
pub(crate) async fn parse_full_manifest_with_core_off_runtime(
raw_bytes: Bytes,
spec: Option<String>,
) -> Result<FullManifestParseResult, anyhow::Error> {
#[cfg(not(target_arch = "wasm32"))]
{
let (tx, rx) = tokio::sync::oneshot::channel();
rayon::spawn(move || {
let result = parse_full_manifest_with_core_sync(raw_bytes, spec);
let _ = tx.send(result);
});
rx.await
.map_err(|e| anyhow!("rayon parse channel closed: {e}"))?
}
#[cfg(target_arch = "wasm32")]
{
parse_full_manifest_with_core_sync(raw_bytes, spec)
}
}
/// Result of a full manifest fetch with ETag support.
/// Transient return value, immediately destructured — Box not needed.
#[allow(clippy::large_enum_variant)]
pub enum FetchManifestResult {
/// 200 OK — fresh manifest with optional new ETag.
Ok(FullManifest, Option<String>),
/// 304 Not Modified — ETag matched, use cached data.
NotModified,
}
/// Raw full-manifest HTTP result.
///
/// This variant intentionally stops before JSON parsing so dependency
/// resolution loops can keep global inflight/cache ownership in one task and
/// reserve spawned work for request I/O.
pub enum FetchManifestBytesResult {
/// 200 OK — response bytes with optional new ETag.
Ok(Bytes, Option<String>),
/// 304 Not Modified — ETag matched, use cached data.
NotModified,
}
/// Manifest metadata format.
#[derive(Debug, Clone, Copy)]
pub enum MetadataFormat {
/// Only install-relevant fields (deps, dist, engines, bin).
/// Uses `application/vnd.npm.install-v1+json`, 10-50x smaller than full.
/// Supported by all major registries.
Abbreviated,
/// Complete metadata including readme, time, maintainers, etc.
/// Only needed for display commands like `utoo view`.
Complete,
}
/// Options for fetching a full manifest.
pub struct FetchManifestOptions<'a> {
pub registry_url: &'a str,
pub name: &'a str,
pub format: MetadataFormat,
pub etag: Option<&'a str>,
}
/// Fetch full manifest bytes with retry and ETag support, without parsing.
pub async fn fetch_full_manifest_bytes(
opts: FetchManifestOptions<'_>,
) -> Result<FetchManifestBytesResult> {
let url = format!("{}/{}", opts.registry_url, opts.name);
let etag_owned = opts.etag.map(|s| s.to_string());
let accept = match opts.format {
MetadataFormat::Abbreviated => "application/vnd.npm.install-v1+json",
MetadataFormat::Complete => "application/json",
};
RetryIf::spawn(
retry_strategy(),
|| {
let url = url.clone();
let etag = etag_owned.clone();
async move {
let mut request = get_client()
.map_err(FetchError::Permanent)?
.get(&url)
.header("Accept", accept);
if let Some(etag_value) = &etag {
request = request.header("If-None-Match", etag_value);
}
let response = request.send().await.map_err(classify_reqwest_error)?;
let status = response.status();
if status == reqwest::StatusCode::NOT_MODIFIED {
if etag.is_some() {
return Ok(FetchManifestBytesResult::NotModified);
}
// Server bug: 304 without If-None-Match. Treat as error.
return Err(classify_status(status, &url));
}
if status.is_success() {
let new_etag = response
.headers()
.get("etag")
.and_then(|v| v.to_str().ok())
.map(|s| s.to_string());
let raw_bytes = response.bytes().await.map_err(classify_reqwest_error)?;
Ok(FetchManifestBytesResult::Ok(raw_bytes, new_etag))
} else {
Err(classify_status(status, &url))
}
}
},
is_retryable,
)
.await
.map_err(|e| match e {
FetchError::Retryable(e) | FetchError::Permanent(e) => {
anyhow!("Failed to fetch {}: {:#}", opts.name, e)
}
})
}
/// Fetch full manifest with retry and ETag support.
pub async fn fetch_full_manifest(opts: FetchManifestOptions<'_>) -> Result<FetchManifestResult> {
match fetch_full_manifest_bytes(opts).await? {
FetchManifestBytesResult::Ok(raw_bytes, etag) => {
let manifest = parse_full_manifest_off_runtime(raw_bytes).await?;
Ok(FetchManifestResult::Ok(manifest, etag))
}
FetchManifestBytesResult::NotModified => Ok(FetchManifestResult::NotModified),
}
}
/// Fetch full manifest without ETag / 304 support.
///
/// Convenience wrapper around [`fetch_full_manifest`] for callers that never
/// send `If-None-Match` (e.g. `utoo view`, corrupted-cache fallback).
/// Returns the manifest directly — no [`FetchManifestResult::NotModified`]
/// to handle.
pub async fn fetch_full_manifest_fresh(
registry_url: &str,
name: &str,
format: MetadataFormat,
) -> Result<(FullManifest, Option<String>)> {
match fetch_full_manifest(FetchManifestOptions {
registry_url,
name,
format,
etag: None,
})
.await?
{
FetchManifestResult::Ok(manifest, etag) => Ok((manifest, etag)),
FetchManifestResult::NotModified => {
// fetch_full_manifest with etag: None treats server 304 as an error,
// so this variant is structurally unreachable.
Err(anyhow!("unexpected 304 without If-None-Match for {name}"))
}
}
}
#[cfg(not(target_arch = "wasm32"))]
async fn read_body_vec(mut response: reqwest::Response) -> Result<Vec<u8>, FetchError> {
let capacity = response
.content_length()
.and_then(|len| usize::try_from(len).ok())
.unwrap_or(0);
let mut body = Vec::with_capacity(capacity);
while let Some(chunk) = response.chunk().await.map_err(classify_reqwest_error)? {
body.extend_from_slice(&chunk);
}
Ok(body)
}
#[cfg(target_arch = "wasm32")]
async fn read_body_vec(response: reqwest::Response) -> Result<Vec<u8>, FetchError> {
response
.bytes()
.await
.map(|bytes| bytes.to_vec())
.map_err(classify_reqwest_error)
}
/// Options for fetching a version manifest.
pub struct FetchVersionManifestOptions<'a> {
pub registry_url: &'a str,
pub name: &'a str,
pub spec: &'a str,
pub format: MetadataFormat,
}
/// Fetch version manifest bytes with retry, without parsing.
pub async fn fetch_version_manifest_bytes(opts: FetchVersionManifestOptions<'_>) -> Result<Bytes> {
fetch_version_manifest_vec(opts).await.map(Bytes::from)
}
/// Fetch version manifest into a mutable parse buffer with retry.
///
/// Unlike full manifests, exact-version manifests do not need to keep raw
/// response bytes for later extraction. Reading directly into `Vec<u8>` avoids
/// the hot-path `Bytes -> Vec` copy before `simd_json` parses in place.
pub(crate) async fn fetch_version_manifest_vec(
opts: FetchVersionManifestOptions<'_>,
) -> Result<Vec<u8>> {
let url = format!("{}/{}/{}", opts.registry_url, opts.name, opts.spec);
let accept = match opts.format {
MetadataFormat::Abbreviated => "application/vnd.npm.install-v1+json",
MetadataFormat::Complete => "application/json",
};
RetryIf::spawn(
retry_strategy(),
|| {
let url = url.clone();
async move {
let response = get_client()
.map_err(FetchError::Permanent)?
.get(&url)
.header("Accept", accept)
.send()
.await
.map_err(classify_reqwest_error)?;
if response.status().is_success() {
read_body_vec(response).await
} else {
Err(classify_status(response.status(), &url))
}
}
},
is_retryable,
)
.await
.map_err(|e| match e {
FetchError::Retryable(e) | FetchError::Permanent(e) => {
anyhow!("Failed to fetch {}@{}: {:#}", opts.name, opts.spec, e)
}
})
}
/// Fetch version manifest with retry.
pub async fn fetch_version_manifest(
opts: FetchVersionManifestOptions<'_>,
) -> Result<CoreVersionManifest> {
let bytes = fetch_version_manifest_vec(opts).await?;
parse_json_vec_off_runtime::<CoreVersionManifest>(bytes).await
}
#[cfg(test)]
mod tests {
use serde::Deserialize;
use super::*;
#[derive(Debug, Deserialize, PartialEq)]
struct TinyManifest {
name: String,
version: String,
}
#[tokio::test]
async fn parse_json_vec_off_runtime_consumes_mutable_buffer() {
let parsed = parse_json_vec_off_runtime::<TinyManifest>(
br#"{"name":"demo","version":"1.0.0"}"#.to_vec(),
)
.await
.unwrap();
assert_eq!(
parsed,
TinyManifest {
name: "demo".to_string(),
version: "1.0.0".to_string(),
}
);
}
#[tokio::test]
async fn parse_full_manifest_with_core_extracts_requested_spec() {
let raw = Bytes::from_static(
br#"{
"name":"demo",
"dist-tags":{"latest":"1.0.0"},
"versions":{
"1.0.0":{
"name":"demo",
"version":"1.0.0",
"dist":{"tarball":"https://registry.example/demo-1.0.0.tgz"}
}
}
}"#,
);
let (manifest, speculative) =
parse_full_manifest_with_core_off_runtime(raw.clone(), Some("latest".to_string()))
.await
.unwrap();
assert_eq!(manifest.name, "demo");
assert_eq!(manifest.raw, raw);
let (spec, core) = speculative.unwrap();
assert_eq!(spec, "latest");
assert_eq!(core.name, "demo");
assert_eq!(core.version, "1.0.0");
}
}