Skip to content
This repository has been archived by the owner on Jul 17, 2024. It is now read-only.

Commit

Permalink
new: Prepare for proto v0.14 release. (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
milesj authored Aug 9, 2023
1 parent 95ef96a commit 15d7e6e
Show file tree
Hide file tree
Showing 17 changed files with 114 additions and 94 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
fail-fast: false
steps:
- uses: actions/checkout@v3
- uses: moonrepo/setup-rust@v0
- uses: moonrepo/setup-rust@v1
with:
components: rustfmt
- run: cargo fmt --all --check
Expand All @@ -29,7 +29,7 @@ jobs:
fail-fast: false
steps:
- uses: actions/checkout@v3
- uses: moonrepo/setup-rust@v0
- uses: moonrepo/setup-rust@v1
with:
components: clippy
- run: cargo clippy --workspace --all-targets
Expand All @@ -42,7 +42,7 @@ jobs:
fail-fast: false
steps:
- uses: actions/checkout@v3
- uses: moonrepo/setup-rust@v0
- uses: moonrepo/setup-rust@v1
with:
bins: cargo-wasi, cargo-nextest
- run: cargo wasi build -p node_plugin
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## 0.1.0

#### 🚀 Updates

- Updated to support proto v0.14 release.

## 0.0.2

#### 🐞 Fixes
Expand Down
14 changes: 6 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@ members = ["crates/*"]

[workspace.dependencies]
extism-pdk = "0.3.3"
# proto_core = { version = "0.13.0", path = "../proto/crates/core" }
# proto_pdk = { version = "0.3.0", path = "../proto/crates/pdk" }
# proto_pdk_test_utils = { version = "0.2.0", path = "../proto/crates/pdk-test-utils" }
proto_core = "0.13.1"
proto_pdk = "0.3.1"
proto_pdk_test_utils = "0.2.1"
serde = "1.0.167"
starbase_sandbox = "0.1.5"
# proto_pdk = { version = "0.4.1", path = "../proto/crates/pdk" }
# proto_pdk_test_utils = { version = "0.3.3", path = "../proto/crates/pdk-test-utils" }
proto_pdk = "0.4.2"
proto_pdk_test_utils = "0.3.4"
serde = "1.0.183"
starbase_sandbox = "0.1.8"
tokio = "1.29.1"
2 changes: 1 addition & 1 deletion crates/common/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "node_common"
version = "0.0.1"
version = "0.1.0"
edition = "2021"
license = "MIT"
publish = false
Expand Down
3 changes: 1 addition & 2 deletions crates/node-depman/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "node_depman_plugin"
version = "0.0.2"
version = "0.1.0"
edition = "2021"
license = "MIT"
publish = false
Expand All @@ -15,7 +15,6 @@ proto_pdk = { workspace = true }
serde = { workspace = true }

[dev-dependencies]
proto_core = { workspace = true }
proto_pdk_test_utils = { workspace = true }
starbase_sandbox = { workspace = true }
tokio = { workspace = true }
84 changes: 53 additions & 31 deletions crates/node-depman/src/proto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ use serde::Deserialize;
use std::collections::HashMap;
use std::fmt;
use std::fs;
use std::path::PathBuf;

#[host_fn]
extern "ExtismHost" {
fn trace(input: Json<TraceInput>);
fn host_log(input: Json<HostLogInput>);
fn exec_command(input: Json<ExecCommandInput>) -> Json<ExecCommandOutput>;
}

Expand Down Expand Up @@ -38,8 +39,14 @@ impl PackageManager {
}
}

pub fn is_yarn_classic(&self, version: &str) -> bool {
self == &PackageManager::Yarn
&& (version.starts_with('1') || version == "legacy" || version == "classic")
}

pub fn is_yarn_berry(&self, version: &str) -> bool {
self == &PackageManager::Yarn && (!version.starts_with('1') || version == "berry")
self == &PackageManager::Yarn
&& (!version.starts_with('1') || version == "berry" || version == "latest")
}
}

Expand All @@ -61,6 +68,12 @@ pub fn register_tool(Json(input): Json<ToolMetadataInput>) -> FnResult<Json<Tool
name: manager.to_string(),
type_of: PluginType::DependencyManager,
env_vars: vec!["PROTO_NODE_VERSION".into()],
default_version: if manager == PackageManager::Npm {
Some("bundled".into())
} else {
None
},
..ToolMetadataOutput::default()
}))
}

Expand All @@ -73,22 +86,22 @@ pub fn download_prebuilt(
let package_name = manager.get_package_name(version);

// Derive values based on package manager
let mut archive_prefix = "package".to_owned();
let archive_prefix = if manager.is_yarn_classic(version) {
format!("yarn-v{version}")
} else {
"package".into()
};

let package_without_scope = if package_name.contains('/') {
package_name.split('/').nth(1).unwrap()
} else {
&package_name
};

if manager == PackageManager::Yarn && !manager.is_yarn_berry(version) {
archive_prefix = format!("yarn-v{version}");
}

Ok(Json(DownloadPrebuiltOutput {
archive_prefix: Some(archive_prefix),
download_url: format!(
"https://registry.npmjs.org/{}/-/{}-{version}.tgz",
package_name, package_without_scope,
"https://registry.npmjs.org/{package_name}/-/{package_without_scope}-{version}.tgz",
),
..DownloadPrebuiltOutput::default()
}))
Expand Down Expand Up @@ -137,9 +150,10 @@ pub fn locate_bins(Json(input): Json<LocateBinsInput>) -> FnResult<Json<LocateBi
}

Ok(Json(LocateBinsOutput {
bin_path,
bin_path: bin_path.map(PathBuf::from),
fallback_last_globals_dir: true,
globals_lookup_dirs: vec!["$PROTO_ROOT/tools/node/globals/bin".into()],
..LocateBinsOutput::default()
}))
}

Expand All @@ -161,22 +175,33 @@ pub fn load_versions(Json(input): Json<LoadVersionsInput>) -> FnResult<Json<Load
let manager = PackageManager::from(&input.env);
let package_name = manager.get_package_name(&input.initial);

let response: RegistryResponse =
fetch_url_with_cache(format!("https://registry.npmjs.org/{}/", package_name))?;
let mut map_output = |res: RegistryResponse| -> Result<(), Error> {
for item in res.versions.values() {
output.versions.push(Version::parse(&item.version)?);
}

for item in response.versions.values() {
output.versions.push(Version::parse(&item.version)?);
}
// Dist tags always includes latest
for (alias, version) in res.dist_tags {
let version = Version::parse(&version)?;

// Dist tags always includes latest
for (alias, version) in response.dist_tags {
let version = Version::parse(&version)?;
if alias == "latest" && output.latest.is_none() {
output.latest = Some(version.clone());
}

if alias == "latest" {
output.latest = Some(version.clone());
output.aliases.entry(alias).or_insert(version);
}

output.aliases.insert(alias, version);
Ok(())
};

map_output(fetch_url_with_cache(format!(
"https://registry.npmjs.org/{}/",
package_name
))?)?;

// Yarn is managed by 2 different packages, so we need to request versions from both of them!
if manager.is_yarn_berry(&input.initial) {
map_output(fetch_url_with_cache("https://registry.npmjs.org/yarn/")?)?;
}

output
Expand Down Expand Up @@ -216,9 +241,7 @@ pub fn resolve_version(

// Otherwise call the current `node` binary and infer from that
if !found_version {
let node_version = unsafe {
exec_command(Json(ExecCommandInput::new("node", ["--version"])))?.0
};
let node_version = exec_command!("node", ["--version"]);
let node_version = node_version.stdout.trim();

for node_release in &response {
Expand All @@ -232,11 +255,9 @@ pub fn resolve_version(
}

if !found_version {
unsafe {
trace(Json(
"Could not find a bundled npm version for Node.js, falling back to latest".into()
))?;
}
host_log!(
"Could not find a bundled npm version for Node.js, falling back to latest"
);

output.candidate = Some("latest".into());
}
Expand All @@ -246,9 +267,9 @@ pub fn resolve_version(
PackageManager::Yarn => {
// Latest currently resolves to a v4-rc version...
if input.initial == "berry" || input.initial == "latest" {
output.candidate = Some("3".into());
output.candidate = Some("~3".into());
} else if input.initial == "legacy" || input.initial == "classic" {
output.candidate = Some("1".into());
output.candidate = Some("~1".into());
}
}

Expand Down Expand Up @@ -308,6 +329,7 @@ pub fn create_shims(Json(input): Json<CreateShimsInput>) -> FnResult<Json<Create
}),
global_shims,
local_shims,
..CreateShimsOutput::default()
}))
}

Expand Down
29 changes: 15 additions & 14 deletions crates/node-depman/tests/download_test.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use proto_core::Installable;
use proto_pdk::*;
use proto_pdk_test_utils::{create_plugin, generate_download_install_tests};
use proto_pdk_test_utils::*;
use starbase_sandbox::create_empty_sandbox;
use std::path::PathBuf;

Expand All @@ -26,7 +24,6 @@ mod npm {
}),
DownloadPrebuiltOutput {
archive_prefix: Some("package".into()),
bin_path: None,
checksum_name: None,
checksum_url: None,
download_name: None,
Expand All @@ -50,7 +47,8 @@ mod npm {
version: "9.0.0".into(),
..Default::default()
},
tool_dir: PathBuf::new()
home_dir: PathBuf::new(),
tool_dir: PathBuf::new(),
})
.bin_path,
Some("bin/npm".into())
Expand Down Expand Up @@ -80,7 +78,6 @@ mod pnpm {
}),
DownloadPrebuiltOutput {
archive_prefix: Some("package".into()),
bin_path: None,
checksum_name: None,
checksum_url: None,
download_name: None,
Expand All @@ -104,7 +101,8 @@ mod pnpm {
version: "8.0.0".into(),
..Default::default()
},
tool_dir: PathBuf::new()
home_dir: PathBuf::new(),
tool_dir: PathBuf::new(),
})
.bin_path,
Some("bin/pnpm.cjs".into())
Expand Down Expand Up @@ -134,7 +132,6 @@ mod yarn {
}),
DownloadPrebuiltOutput {
archive_prefix: Some("yarn-v1.22.0".into()),
bin_path: None,
checksum_name: None,
checksum_url: None,
download_name: None,
Expand All @@ -158,7 +155,8 @@ mod yarn {
version: "1.22.0".into(),
..Default::default()
},
tool_dir: PathBuf::new()
home_dir: PathBuf::new(),
tool_dir: PathBuf::new(),
})
.bin_path,
Some("bin/yarn".into())
Expand Down Expand Up @@ -188,7 +186,6 @@ mod yarn_berry {
}),
DownloadPrebuiltOutput {
archive_prefix: Some("package".into()),
bin_path: None,
checksum_name: None,
checksum_url: None,
download_name: None,
Expand All @@ -213,7 +210,8 @@ mod yarn_berry {
version: "3.6.1".into(),
..Default::default()
},
tool_dir: PathBuf::new()
home_dir: PathBuf::new(),
tool_dir: PathBuf::new(),
})
.bin_path,
Some("bin/yarn".into())
Expand Down Expand Up @@ -245,7 +243,8 @@ fn locates_bin_from_package_json_bin() {
version: "20.0.0".into(),
..Default::default()
},
tool_dir: plugin.to_virtual_path(&plugin.tool.get_install_dir().unwrap()),
home_dir: PathBuf::new(),
tool_dir: plugin.to_virtual_path(&plugin.tool.get_tool_dir()),
})
.bin_path,
Some("./file.js".into())
Expand Down Expand Up @@ -273,7 +272,8 @@ fn locates_bin_from_package_json_bin() {
version: "9.0.0".into(),
..Default::default()
},
tool_dir: plugin.to_virtual_path(&plugin.tool.get_install_dir().unwrap()),
home_dir: PathBuf::new(),
tool_dir: plugin.to_virtual_path(&plugin.tool.get_tool_dir()),
})
.bin_path,
Some("./npm.js".into())
Expand Down Expand Up @@ -303,7 +303,8 @@ fn locates_bin_from_package_json_main() {
version: "8.0.0".into(),
..Default::default()
},
tool_dir: plugin.to_virtual_path(&plugin.tool.get_install_dir().unwrap()),
home_dir: PathBuf::new(),
tool_dir: plugin.to_virtual_path(&plugin.tool.get_tool_dir()),
})
.bin_path,
Some("./index.js".into())
Expand Down
Loading

0 comments on commit 15d7e6e

Please sign in to comment.