Skip to content

Commit

Permalink
Add v1-v2 release conversion
Browse files Browse the repository at this point in the history
Dupe the pattern from the `meta` package by adding `v1` and `v2`
sub-packages that know how to parse into the canonical form. Make
`meta::v1` public to the crate so that `release::v1` can use it to
migrate everything but the release metadata, then add `v1_to_v2_release`
to handle that bit.

The v2 `release` object takes the form of a JWS-JS data structure, but
since we're not signing anything yet, and this is just to get things to
load properly, for now just generate random values for the JWS header
and signature.

While at it, change the `uri` property to start with `dist` instead of
`/dist`, because the latter implies relative to the root of a domain,
and mirrors may not serve from a domain root.
  • Loading branch information
theory committed Sep 18, 2024
1 parent 2923488 commit c1f24b7
Show file tree
Hide file tree
Showing 13 changed files with 269 additions and 61 deletions.
47 changes: 47 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ chrono = { version = "0.4.38", features = ["serde"] }
email_address = "0.2.9"
json-patch = "2.0.0"
lexopt = "0.3.0"
rand = "0.8.5"
relative-path = { version = "1.9", features = ["serde"] }
semver = { version = "1.0", features = ["std", "serde"] }
serde = { version = "1", features = ["derive"] }
Expand Down
14 changes: 7 additions & 7 deletions schema/v2/pgxn-jws.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@
"uri": {
"type": "string",
"format": "uri-reference",
"pattern": "^/dist/",
"pattern": "^dist/",
"description": "Path to the release file relative to a PGXN base URL.",
"examples": [
"/dist/pair/0.1.7/pair-0.1.7.zip",
"/dist/plv8/3.2.3/plv8-3.2.3.zip"
"dist/pair/0.1.7/pair-0.1.7.zip",
"dist/plv8/3.2.3/plv8-3.2.3.zip"
]
},
"digests": {
Expand All @@ -60,15 +60,15 @@
{
"user": "theory",
"date": "2024-07-20T20:34:34Z",
"uri": "/dist/semver/0.40.0/semver-0.40.0.zip",
"uri": "dist/semver/0.40.0/semver-0.40.0.zip",
"digests": {
"sha1": "fe8c013f991b5f537c39fb0c0b04bc955457675a"
}
},
{
"user": "theory",
"date": "2024-09-13T17:32:55Z",
"uri": "/dist/pair/0.1.7/pair-0.1.7.zip",
"uri": "dist/pair/0.1.7/pair-0.1.7.zip",
"digests": {
"sha256": "257b71aa57a28d62ddbb301333b3521ea3dc56f17551fa0e4516b03998abb089",
"sha512": "b353b5a82b3b54e95f4a2859e7a2bd0648abcb35a7c3612b126c2c75438fc2f8e8ee1f19e61f30fa54d7bb64bcf217ed1264722b497bcb613f82d78751515b67"
Expand All @@ -88,7 +88,7 @@
"payload": {
"user": "theory",
"date": "2024-07-20T20:34:34Z",
"uri": "/dist/semver/0.40.0/semver-0.40.0.zip",
"uri": "dist/semver/0.40.0/semver-0.40.0.zip",
"digests": {
"sha1": "fe8c013f991b5f537c39fb0c0b04bc955457675a"
}
Expand All @@ -103,7 +103,7 @@
"payload": {
"user": "theory",
"date": "2024-09-13T17:32:55Z",
"uri": "/dist/pair/0.1.7/pair-0.1.7.zip",
"uri": "dist/pair/0.1.7/pair-0.1.7.zip",
"digests": {
"sha256": "257b71aa57a28d62ddbb301333b3521ea3dc56f17551fa0e4516b03998abb089",
"sha512": "b353b5a82b3b54e95f4a2859e7a2bd0648abcb35a7c3612b126c2c75438fc2f8e8ee1f19e61f30fa54d7bb64bcf217ed1264722b497bcb613f82d78751515b67"
Expand Down
2 changes: 1 addition & 1 deletion src/meta/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use semver::Version;
use serde::{Deserialize, Deserializer, Serialize};
use serde_json::Value;

mod v1;
pub(crate) mod v1;
mod v2;

/// Represents the `meta-spec` object in [`Distribution`].
Expand Down
4 changes: 2 additions & 2 deletions src/meta/v1/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub fn to_v2(v1: &Value) -> Result<Value, Box<dyn Error>> {
// Copy common fields.
let mut v2 = v1_to_v2_common(v1);

// Convert maintainer.
// Convert maintainers.
v2.insert("maintainers".to_string(), v1_to_v2_maintainers(v1)?);

// Convert license.
Expand Down Expand Up @@ -45,7 +45,7 @@ pub fn to_v2(v1: &Value) -> Result<Value, Box<dyn Error>> {
/// from_value parses v1, which contains PGXN v1 metadata, into a
/// [`Distribution`] object containing valid PGXN v2 metadata.
pub fn from_value(v1: Value) -> Result<Distribution, Box<dyn Error>> {
Distribution::try_from(to_v2(&v1)?)
to_v2(&v1)?.try_into()
}

/// v1_to_v2_common sets up a new v2 map with compatible fields copied from v1
Expand Down
4 changes: 3 additions & 1 deletion src/meta/v2/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use super::*;
use super::Distribution;
use serde_json::Value;
use std::error::Error;

pub fn from_value(meta: Value) -> Result<Distribution, Box<dyn Error>> {
match serde_json::from_value(meta) {
Expand Down
22 changes: 11 additions & 11 deletions src/release/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::{borrow::Borrow, collections::HashMap, error::Error, fs::File, path::Path};

mod v1;
mod v2;

/// Digests represents Hash digests for a file that can be used to verify its
/// integrity.
#[serde_with::serde_as]
Expand Down Expand Up @@ -142,15 +145,12 @@ impl Release {
// copy/pasting all the Distribution methods, but this will do for now.
// [delegation]: https://github.com/rust-lang/rfcs/pull/3530

/// Deserializes `meta`, which contains PGXN disribution release metadata,
/// into a [`Release`].
/// Deserializes `meta`, which contains PGXN distribution release
/// metadata, into a [`Release`].
fn from_version(version: u8, meta: Value) -> Result<Self, Box<dyn Error>> {
match version {
// 1 => v1::from_value(meta),
2 => match serde_json::from_value(meta) {
Ok(m) => Ok(m),
Err(e) => Err(Box::from(e)),
},
1 => v1::from_value(meta),
2 => v2::from_value(meta),
_ => Err(Box::from(format!("Unknown meta version {version}"))),
}

Expand Down Expand Up @@ -284,7 +284,7 @@ impl TryFrom<Value> for Release {
/// "payload": {
/// "user": "xxx",
/// "date": "2024-07-20T20:34:34Z",
/// "uri": "/dist/semver/0.40.0/semver-0.40.0.zip",
/// "uri": "dist/semver/0.40.0/semver-0.40.0.zip",
/// "digests": {
/// "sha1": "fe8c013f991b5f537c39fb0c0b04bc955457675a"
/// }
Expand Down Expand Up @@ -348,7 +348,7 @@ impl TryFrom<&[&Value]> for Release {
/// "payload": {
/// "user": "xxx",
/// "date": "2024-07-20T20:34:34Z",
/// "uri": "/dist/semver/0.40.0/semver-0.40.0.zip",
/// "uri": "dist/semver/0.40.0/semver-0.40.0.zip",
/// "digests": {
/// "sha1": "fe8c013f991b5f537c39fb0c0b04bc955457675a"
/// }
Expand Down Expand Up @@ -376,7 +376,7 @@ impl TryFrom<&[&Value]> for Release {

// Convert the first doc to v2 if necessary.
let mut v2 = match version {
// 1 => v1::to_v2(meta[0])?,
1 => v1::to_v2(meta[0])?,

Check warning on line 379 in src/release/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/release/mod.rs#L379

Added line #L379 was not covered by tests
2 => meta[0].clone(),
_ => unreachable!(),
};
Expand Down Expand Up @@ -429,7 +429,7 @@ impl TryFrom<Release> for Value {
/// "payload": {
/// "user": "xxx",
/// "date": "2024-07-20T20:34:34Z",
/// "uri": "/dist/semver/0.40.0/semver-0.40.0.zip",
/// "uri": "dist/semver/0.40.0/semver-0.40.0.zip",
/// "digests": {
/// "sha1": "fe8c013f991b5f537c39fb0c0b04bc955457675a"
/// }
Expand Down
7 changes: 1 addition & 6 deletions src/release/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ fn release_meta() -> Value {
"payload": {
"user": "theory",
"date": "2024-07-20T20:34:34Z",
"uri": "/dist/semver/0.40.0/semver-0.40.0.zip",
"uri": "dist/semver/0.40.0/semver-0.40.0.zip",
"digests": {
"sha1": "fe8c013f991b5f537c39fb0c0b04bc955457675a"
}
Expand All @@ -39,11 +39,6 @@ fn test_corpus() -> Result<(), Box<dyn Error>> {
),
(2, release_meta()),
] {
// Skip version 1 for now.
if version == 1 {
continue;
}

let v_dir = format!("v{version}");
let dir: PathBuf = [env!("CARGO_MANIFEST_DIR"), "corpus", &v_dir]
.iter()
Expand Down
65 changes: 65 additions & 0 deletions src/release/v1/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
use super::Release;
use crate::meta::v1 as dist;
use serde_json::{json, Value};
use std::error::Error;

/// to_v2 parses v1, which contains PGXN v1 release metadata, into a JSON
/// object containing valid PGXN v2 release metadata.
pub fn to_v2(v1: &Value) -> Result<Value, Box<dyn Error>> {
let mut v2_val = dist::to_v2(v1)?;
let v2 = v2_val
.as_object_mut()
.ok_or("Date returned from v1::to_v2 is not an object")?;

// Convert release.
v2.insert("release".to_string(), v1_to_v2_release(v1)?);

Ok(v2_val)
}

/// from_value parses v1, which contains PGXN v1 metadata, into a
/// [`Release`] object containing valid PGXN v2 metadata.
pub fn from_value(v1: Value) -> Result<Release, Box<dyn Error>> {
to_v2(&v1)?.try_into()
}

/// v1_to_v2_release clones release metadata from v1 to the v2 format. The
/// included signature information will be random and un-verifiable, but
/// adequate for v2 JSON Schema validation.
fn v1_to_v2_release(v1: &Value) -> Result<Value, Box<dyn Error>> {
use rand::distributions::{Alphanumeric, DistString};
let mut field = "user";
if let Some(Value::String(user)) = v1.get(field) {
field = "date";
if let Some(Value::String(date)) = v1.get(field) {
field = "sha1";
if let Some(Value::String(sha1)) = v1.get(field) {
field = "name";
if let Some(Value::String(name)) = v1.get(field) {
field = "version";
if let Some(Value::String(version)) = v1.get(field) {
let uri =
format!("dist/{1}/{0}/{1}-{0}.zip", version.as_str(), name.as_str());
// Generate random base64-ish values to mock headers
// and signatures.
let mut rng = rand::thread_rng();
return Ok(json!({
"headers": [format!("eyJ{}", Alphanumeric.sample_string(&mut rng, 13))],
"signatures": [Alphanumeric.sample_string(&mut rng, 32)],
"payload": {
"user": user,
"date": date,
"uri": uri,
"digests": {"sha1": sha1},
}
}));
}
}
}
}
}
Err(Box::from(format!("missing release property {:?}", field)))
}

#[cfg(test)]
mod tests;
Loading

0 comments on commit c1f24b7

Please sign in to comment.