full package json

This commit is contained in:
nora 2022-05-14 16:02:31 +02:00
parent bf1809e025
commit c5e1b8011c
3 changed files with 135 additions and 62 deletions

View file

@ -4,33 +4,26 @@ use color_eyre::Result;
use indexmap::IndexMap; use indexmap::IndexMap;
use reqwest::blocking::Client; use reqwest::blocking::Client;
use serde::Deserialize; use serde::Deserialize;
use tracing::{error, info}; use tracing::info;
#[derive(Debug, Deserialize)] use crate::{
struct Person { manifest::{Bugs, Human, Person, Repository},
name: String, PackageJson,
url: Option<String>, };
email: Option<String>,
}
#[derive(Debug, Deserialize)]
struct Bugs {
url: String,
}
#[derive(Debug, Deserialize)]
struct Repository {
r#type: String,
url: String,
}
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize)]
struct Dist { struct Dist {
shasum: String, shasum: String,
tarball: String, tarball: String,
integrity: Option<String>,
#[serde(rename = "fileCount")]
file_count: Option<u32>,
#[serde(rename = "unpackedSize")]
unpacked_size: Option<u32>,
#[serde(rename = "npm-signature")]
npm_signature: Option<String>,
} }
// todo: this is actually just a package.json with extra stuff
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize)]
struct VersionMeta { struct VersionMeta {
_from: Option<String>, _from: Option<String>,
@ -42,48 +35,38 @@ struct VersionMeta {
#[serde(rename = "_npmVersion")] #[serde(rename = "_npmVersion")]
_npm_version: String, _npm_version: String,
_shasum: Option<String>, _shasum: Option<String>,
author: Person, #[serde(rename = "_hasShrinkwrap")]
bugs: Bugs, _has_shrinkwrap: Option<bool>,
dependencies: IndexMap<String, String>,
#[serde(rename = "devDependencies")]
dev_dependencies: IndexMap<String, String>,
dist: Dist, dist: Dist,
engines: IndexMap<String, String>,
files: Vec<String>, files: Vec<String>,
homepage: String,
keywords: Vec<String>, #[serde(flatten)]
license: String, package_json: PackageJson,
main: String,
maintainers: Vec<Person>,
name: String,
repository: Repository,
scripts: IndexMap<String, String>,
version: String,
} }
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize)]
struct PackageMeta { struct PackageMeta {
_id: String, _id: String,
_rev: String, _rev: String,
author: Person,
bugs: Bugs,
#[serde(default = "Vec::new")]
contributors: Vec<Person>,
description: String,
#[serde(rename = "dist-tags")] #[serde(rename = "dist-tags")]
dist_tags: IndexMap<String, String>, dist_tags: IndexMap<String, String>,
homepage: String,
keywords: Vec<String>,
license: String,
maintainers: Vec<Person>,
name: String, name: String,
readme: String,
#[serde(rename = "readmeFilename")]
readme_filename: String,
repository: Repository,
time: IndexMap<String, String>, time: IndexMap<String, String>,
users: IndexMap<String, bool>, users: IndexMap<String, bool>,
versions: IndexMap<String, VersionMeta>, versions: IndexMap<String, VersionMeta>,
author: Human,
bugs: Option<Bugs>,
contributors: Option<Vec<Human>>,
description: Option<String>,
homepage: Option<String>,
keywords: Option<Vec<String>>,
license: Option<String>,
maintainers: Option<Vec<Human>>,
readme: Option<String>,
#[serde(rename = "readmeFilename")]
readme_filename: Option<String>,
repository: Option<Repository>,
} }
pub struct NpmClient { pub struct NpmClient {
@ -104,15 +87,15 @@ impl NpmClient {
let code = res.status(); let code = res.status();
let body = res.text()?; let body = res.text()?;
let meta = serde_json::from_str::<PackageMeta>(&body); let meta = serde_json::from_str::<PackageMeta>(&body);
if let Err(err) = meta { if let Err(err) = &meta {
error!(?err, "error"); tracing::error!(?err, "error");
let col = err.column(); let col = err.column();
let after = &body[col..][..50]; let after = &body[col..][..10];
let before = &body[col - 50..col]; let before = &body[col - 100..col];
error!(%before, %after, "err"); tracing::error!(%before, %after, "err");
} }
info!(?code, "Received response"); info!(?code, ?meta, "Received response");
Ok(()) Ok(())
} }
} }

View file

@ -22,7 +22,7 @@ fn main() -> Result<()> {
let client = NpmClient::new(); let client = NpmClient::new();
for (name, _) in &manifest.dependencies { for (name, _) in &manifest.dependencies.unwrap() {
client.inspect_package(name)?; client.inspect_package(name)?;
} }

View file

@ -1,16 +1,106 @@
use indexmap::map::IndexMap; use indexmap::map::IndexMap;
use serde::{Deserialize, Serialize}; use serde::Deserialize;
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Deserialize)]
pub struct Human {
pub name: String,
pub url: Option<String>,
pub email: Option<String>,
}
#[derive(Debug, Deserialize)]
#[serde(untagged)]
pub enum Person {
Simple(String),
Expanded(Human),
}
#[derive(Debug, Deserialize)]
pub struct ExpandedFunding {
pub r#type: String,
pub url: String,
}
#[derive(Debug, Deserialize)]
#[serde(untagged)]
pub enum Funding {
Simple(String),
Expanded(ExpandedFunding),
Multiple(Vec<ExpandedFunding>),
}
#[derive(Debug, Deserialize)]
pub struct Bugs {
pub url: Option<String>,
pub email: Option<String>,
}
#[derive(Debug, Deserialize)]
pub struct Repository {
pub r#type: String,
pub url: String,
}
#[derive(Debug, Deserialize)]
#[serde(untagged)]
pub enum Bin {
Single(String),
Multiple(IndexMap<String, String>),
}
#[derive(Debug, Deserialize)]
#[serde(untagged)]
pub enum Man {
Single(String),
Multiple(Vec<String>),
}
#[derive(Debug, Deserialize)]
pub struct PeerDependencyMeta {
pub optional: Option<bool>,
}
#[derive(Debug, Deserialize)]
#[serde(untagged)]
pub enum Override {
Version(String),
Nested(IndexMap<String, Override>),
}
/// https://docs.npmjs.com/cli/v8/configuring-npm/package-json
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub struct PackageJson { pub struct PackageJson {
pub name: String, pub name: String,
pub version: String, pub version: String,
pub description: Option<String>,
pub keywords: Option<Vec<String>>,
pub homepage: Option<String>,
pub bugs: Option<Bugs>,
pub license: Option<String>,
pub author: Option<Person>,
pub contributors: Option<Vec<Person>>,
pub funding: Option<Funding>,
pub files: Option<Vec<String>>,
pub main: Option<String>,
pub browser: Option<String>,
pub bin: Option<Bin>,
pub man: Option<Man>,
pub directories: Option<IndexMap<String, String>>,
pub repository: Option<Repository>,
pub scripts: Option<IndexMap<String, String>>,
pub config: Option<IndexMap<String, serde_json::Value>>,
pub dependencies: Option<IndexMap<String, String>>,
pub dev_dependencies: Option<IndexMap<String, String>>,
pub peer_dependencies: Option<IndexMap<String, String>>,
pub peer_dependencies_meta: Option<IndexMap<String, PeerDependencyMeta>>,
pub bundled_dependencies: Option<Vec<String>>,
pub optional_dependencies: Option<IndexMap<String, String>>,
pub overrides: Option<IndexMap<String, Override>>,
pub engines: Option<IndexMap<String, String>>,
pub os: Option<Vec<String>>,
pub cpu: Option<Vec<String>>,
pub private: Option<bool>, pub private: Option<bool>,
#[serde(default = "IndexMap::new")] pub publish_config: Option<IndexMap<String, serde_json::Value>>,
pub scripts: IndexMap<String, String>, pub workspaces: Option<Vec<String>>,
#[serde(default = "IndexMap::new")]
pub dependencies: IndexMap<String, String>,
#[serde(default = "IndexMap::new")]
pub dev_dependencies: IndexMap<String, String>,
} }