pray-core 1.6.0

Core library for Prayfile, the package manager for the language placed before inference
Documentation
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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
use crate::constraint::version_satisfies;
use crate::derived_metadata::RegistryDerivedMetadata;
use crate::manifest::ManifestPackage;
use crate::package_integrity::{artifact_content_digest, require_remote_integrity_fields};
use crate::paths::remove_path_if_exists;
use crate::registry_http::{http_get, http_post, http_put, join_url};
use crate::registry_ssh::{
    resolve_ssh_registry_package_root, submit_confession_ssh, upload_registry_artifact_ssh,
};
use crate::resolve_context::PackageResolutionContext;
use crate::{PrayError, PrayResult};
use semver::Version;
use serde::{Deserialize, Serialize};
use std::fs;
use std::path::{Path, PathBuf};

#[derive(Debug, Clone)]
pub struct RegistryPackageResolution {
    pub root: PathBuf,
    pub signer_fingerprint: Option<String>,
    /// Highest non-yanked version published in registry metadata, regardless of Prayfile constraint.
    pub registry_latest_version: Option<String>,
}

pub fn lockfile_signer_fingerprint(version: &RegistryPackageVersion) -> Option<String> {
    version
        .signer_fingerprint
        .as_deref()
        .filter(|value| crate::ssh_identity::looks_like_ssh_fingerprint(value))
        .map(crate::ssh_identity::normalize_identity)
}

#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq, Eq)]
pub struct RegistryIndex {
    pub spec: String,
    #[serde(default)]
    pub packages: Vec<String>,
}

#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq, Eq)]
pub struct RegistryPackageMetadata {
    pub name: String,
    #[serde(default)]
    pub versions: Vec<RegistryPackageVersion>,
}

#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq, Eq)]
pub struct RegistryPackageVersion {
    pub version: String,
    pub artifact: String,
    #[serde(default)]
    pub artifact_hash: Option<String>,
    #[serde(default)]
    pub tree_hash: Option<String>,
    #[serde(default)]
    pub yanked: bool,
    #[serde(default)]
    pub targets: Vec<String>,
    #[serde(default)]
    pub exports: Vec<String>,
    #[serde(default)]
    pub signer: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub signer_fingerprint: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub signer_public_key: Option<String>,
    #[serde(default)]
    pub published_at: Option<String>,
    #[serde(default)]
    pub signature: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub derived_metadata: Option<RegistryDerivedMetadata>,
}

impl RegistryPackageVersion {
    pub fn same_identity(&self, other: &Self) -> bool {
        self.version == other.version
            && self.artifact == other.artifact
            && self.artifact_hash == other.artifact_hash
            && self.tree_hash == other.tree_hash
            && self.yanked == other.yanked
            && self.targets == other.targets
            && self.exports == other.exports
            && self.signer == other.signer
            && self.signer_fingerprint == other.signer_fingerprint
            && self.signer_public_key == other.signer_public_key
            && self.published_at == other.published_at
            && self.signature == other.signature
    }

    pub fn merge_annotations_from(&mut self, other: &Self) {
        if self.derived_metadata.is_none() {
            self.derived_metadata = other.derived_metadata.clone();
        }
    }
}

#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq, Eq)]
pub struct ConfessionSubmission {
    pub package: String,
    pub version: String,
    pub status: String,
    #[serde(default)]
    pub note: Option<String>,
    #[serde(default)]
    pub lockfile: Option<String>,
    #[serde(default)]
    pub distribution_point: Option<String>,
    #[serde(default)]
    pub signer: Option<String>,
    #[serde(default)]
    pub timestamp: Option<String>,
    #[serde(default)]
    pub signature: Option<String>,
}

pub fn resolve_registry_package_root(
    project_root: &Path,
    source_url: &str,
    declaration: &ManifestPackage,
    context: &PackageResolutionContext,
) -> PrayResult<RegistryPackageResolution> {
    if crate::ssh_client::is_pray_ssh_url(source_url) {
        return resolve_ssh_registry_package_root(project_root, source_url, declaration, context);
    }

    let metadata = fetch_registry_package_metadata(source_url, &declaration.name)?;
    let registry_latest_version = registry_latest_version_label(&metadata);
    let selected = select_package_version(
        &metadata,
        &declaration.constraint,
        context.preferred_version.as_deref(),
    )?;
    let signer_fingerprint = lockfile_signer_fingerprint(&selected);
    require_remote_integrity_fields(&declaration.name, &selected.version, &selected)?;
    crate::client_trust::gate_pray_ssh_publisher_optional(
        source_url,
        signer_fingerprint.as_deref(),
    )?;
    if let Some(vendored_root) = crate::registry_cache::try_vendored_package_root(
        project_root,
        &declaration.name,
        &selected,
    )? {
        return Ok(RegistryPackageResolution {
            root: vendored_root,
            signer_fingerprint,
            registry_latest_version,
        });
    }
    let cache_directory = crate::registry_cache::registry_cache_directory(
        project_root,
        source_url,
        &declaration.name,
        &selected.version,
    );

    if let Some(mut cached) = crate::registry_cache::try_reuse_cached_registry_package(
        &cache_directory,
        &selected,
        signer_fingerprint.clone(),
    )? {
        cached.registry_latest_version = registry_latest_version.clone();
        return Ok(cached);
    }
    if context.offline {
        return Err(crate::registry_cache::offline_package_error(
            &declaration.name,
            &selected.version,
        ));
    }

    crate::registry_cache::install_registry_artifact_to_cache(
        &cache_directory,
        source_url,
        declaration,
        &selected,
    )?;

    Ok(RegistryPackageResolution {
        root: cache_directory,
        signer_fingerprint,
        registry_latest_version,
    })
}

pub fn resolve_local_registry_package_root(
    project_root: &Path,
    source_key: &str,
    source_root: &Path,
    declaration: &ManifestPackage,
    context: &PackageResolutionContext,
) -> PrayResult<RegistryPackageResolution> {
    let metadata_path = source_root.join(format!("v1/packages/{}.json", declaration.name));
    let metadata_text = fs::read_to_string(&metadata_path).map_err(|error| {
        if error.kind() == std::io::ErrorKind::NotFound {
            PrayError::Resolution(format!(
                "package {} not found in distribution {:?}. \
                 Missing {}. Check the package name, version constraint `{}`, and that the source publishes registry metadata.",
                declaration.name,
                source_root,
                metadata_path.display(),
                declaration.constraint
            ))
        } else {
            PrayError::Resolution(format!(
                "failed to read package metadata {}: {error}",
                metadata_path.display()
            ))
        }
    })?;
    let metadata: RegistryPackageMetadata =
        serde_json::from_str(&metadata_text).map_err(|error| PrayError::Parse {
            kind: "registry metadata",
            message: error.to_string(),
        })?;
    let registry_latest_version = registry_latest_version_label(&metadata);
    let selected = select_package_version(
        &metadata,
        &declaration.constraint,
        context.preferred_version.as_deref(),
    )?;
    let signer_fingerprint = lockfile_signer_fingerprint(&selected);
    require_remote_integrity_fields(&declaration.name, &selected.version, &selected)?;
    crate::client_trust::gate_pray_ssh_publisher_optional(
        source_key,
        signer_fingerprint.as_deref(),
    )?;
    if let Some(vendored_root) = crate::registry_cache::try_vendored_package_root(
        project_root,
        &declaration.name,
        &selected,
    )? {
        return Ok(RegistryPackageResolution {
            root: vendored_root,
            signer_fingerprint,
            registry_latest_version,
        });
    }
    let cache_identifier = format!(
        "{}:{}:{}:{}",
        source_key,
        declaration.name,
        selected.version,
        selected
            .artifact_hash
            .as_deref()
            .unwrap_or("no-artifact-hash")
    );
    let cache_directory = crate::registry_cache::registry_cache_directory(
        project_root,
        &cache_identifier,
        &declaration.name,
        &selected.version,
    );

    if let Some(mut cached) = crate::registry_cache::try_reuse_cached_registry_package(
        &cache_directory,
        &selected,
        signer_fingerprint.clone(),
    )? {
        cached.registry_latest_version = registry_latest_version.clone();
        return Ok(cached);
    }
    if context.offline {
        return Err(crate::registry_cache::offline_package_error(
            &declaration.name,
            &selected.version,
        ));
    }

    if cache_directory.exists() {
        remove_path_if_exists(&cache_directory)?;
    }
    fs::create_dir_all(&cache_directory)?;

    let artifact_bytes =
        crate::registry_cache::read_local_registry_artifact_bytes(source_root, &selected.artifact)?;
    crate::registry_cache::validate_and_unpack_registry_package(
        &cache_directory,
        declaration,
        &selected,
        &artifact_bytes,
    )?;

    Ok(RegistryPackageResolution {
        root: cache_directory,
        signer_fingerprint,
        registry_latest_version,
    })
}

pub fn registry_package_signing_identity(version: &RegistryPackageVersion) -> Option<String> {
    crate::ssh_identity::package_signing_identity(
        version.signer.as_deref(),
        version.signer_fingerprint.as_deref(),
    )
}

pub fn registry_artifact_signature(artifact_bytes: &[u8], tree_hash: &str, signer: &str) -> String {
    artifact_content_digest(artifact_bytes, tree_hash, signer)
}

pub fn submit_confession(source_url: &str, confession: &ConfessionSubmission) -> PrayResult<()> {
    if crate::ssh_client::is_pray_ssh_url(source_url) {
        return submit_confession_ssh(source_url, confession);
    }
    let endpoint = join_url(source_url, "v1/confessions");
    let payload =
        serde_json::to_vec(confession).map_err(|error| PrayError::Manifest(error.to_string()))?;
    let response = http_post(&endpoint, "application/json", &payload)?;
    if response.status / 100 != 2 {
        return Err(PrayError::Resolution(format!(
            "confession submission failed with HTTP {}",
            response.status
        )));
    }
    Ok(())
}

pub fn upload_registry_artifact(
    source_url: &str,
    artifact_path: &str,
    bytes: &[u8],
) -> PrayResult<()> {
    if crate::ssh_client::is_pray_ssh_url(source_url) {
        return upload_registry_artifact_ssh(source_url, artifact_path, bytes);
    }
    let endpoint = join_url(source_url, artifact_path);
    let response = http_put(&endpoint, "application/octet-stream", bytes)?;
    if response.status / 100 != 2 {
        return Err(PrayError::Resolution(format!(
            "artifact upload failed with HTTP {}",
            response.status
        )));
    }
    Ok(())
}

fn fetch_registry_package_metadata(
    source_url: &str,
    package_name: &str,
) -> PrayResult<RegistryPackageMetadata> {
    let url = join_url(source_url, &format!("v1/packages/{}.json", package_name));
    let response = http_get(&url)?;
    serde_json::from_slice(&response).map_err(|error| PrayError::Parse {
        kind: "registry metadata",
        message: error.to_string(),
    })
}

pub fn highest_registry_version(
    metadata: &RegistryPackageMetadata,
) -> PrayResult<Option<RegistryPackageVersion>> {
    let mut selected: Option<RegistryPackageVersion> = None;
    for version in &metadata.versions {
        if version.yanked {
            continue;
        }
        match &selected {
            Some(existing) if compare_versions(&version.version, &existing.version)? <= 0 => {}
            _ => selected = Some(version.clone()),
        }
    }
    Ok(selected)
}

pub fn registry_latest_version_label(metadata: &RegistryPackageMetadata) -> Option<String> {
    highest_registry_version(metadata)
        .ok()
        .flatten()
        .map(|version| version.version)
}

pub fn version_is_greater_than(left: &str, right: &str) -> PrayResult<bool> {
    Ok(compare_versions(left, right)? > 0)
}

pub(crate) fn select_package_version(
    metadata: &RegistryPackageMetadata,
    constraint: &str,
    preferred_version: Option<&str>,
) -> PrayResult<RegistryPackageVersion> {
    if let Some(preferred_version) = preferred_version {
        if let Some(version) = metadata
            .versions
            .iter()
            .find(|version| version.version == preferred_version && !version.yanked)
        {
            if version_satisfies(&version.version, constraint)? {
                return Ok(version.clone());
            }
            // Prayfile constraint changed; fall through to the highest satisfying version.
        }
    }
    let mut selected: Option<RegistryPackageVersion> = None;
    for version in &metadata.versions {
        if version.yanked {
            continue;
        }
        if !version_satisfies(&version.version, constraint)? {
            continue;
        }
        match &selected {
            Some(existing) if compare_versions(&version.version, &existing.version)? <= 0 => {}
            _ => selected = Some(version.clone()),
        }
    }
    selected.ok_or_else(|| {
        PrayError::Resolution(format!(
            "no registry version for {} satisfies {}",
            metadata.name, constraint
        ))
    })
}

#[doc(hidden)]
pub fn select_package_version_for_test(
    metadata: &RegistryPackageMetadata,
    constraint: &str,
    preferred_version: Option<&str>,
) -> PrayResult<RegistryPackageVersion> {
    select_package_version(metadata, constraint, preferred_version)
}

fn compare_versions(left: &str, right: &str) -> PrayResult<i32> {
    let left = Version::parse(left).map_err(|error| PrayError::Resolution(error.to_string()))?;
    let right = Version::parse(right).map_err(|error| PrayError::Resolution(error.to_string()))?;
    Ok(match left.cmp(&right) {
        std::cmp::Ordering::Less => -1,
        std::cmp::Ordering::Equal => 0,
        std::cmp::Ordering::Greater => 1,
    })
}

pub fn fetch_optional_distribution_bytes(
    source_url: &str,
    relative_path: &str,
) -> PrayResult<Option<Vec<u8>>> {
    if !source_url.starts_with("http://") && !source_url.starts_with("https://") {
        return Err(PrayError::Unsupported(format!(
            "distribution fetch requires http or https source, got {source_url}"
        )));
    }
    let url = join_url(source_url, relative_path);
    match http_get(&url) {
        Ok(bytes) => Ok(Some(bytes)),
        Err(PrayError::Resolution(message)) if message.contains("404") => Ok(None),
        Err(error) => Err(error),
    }
}