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

fix: Strip control characters with regex. #14

Merged
merged 6 commits into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## 0.4.3

#### 🐞 Fixes

- Temporarily fixed an issue where Yarn would fail to parse the npm registry response and error with "control character (\u0000-\u001F) found while parsing a string".

## 0.4.2

#### 🚀 Updates
Expand Down
5 changes: 3 additions & 2 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 @@ -7,6 +7,7 @@ extism-pdk = "0.3.4"
proto_pdk = { version = "0.8.1" } # , path = "../../proto/crates/pdk" }
proto_pdk_api = { version = "0.8.1" } # , path = "../../proto/crates/pdk-api" }
proto_pdk_test_utils = { version = "0.8.4" } # , path = "../../proto/crates/pdk-test-utils" }
regex = "1.10.2"
serde = "1.0.189"
serde_json = "1.0.107"
starbase_sandbox = "0.1.11"
Expand Down
3 changes: 2 additions & 1 deletion 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.4.2"
version = "0.4.3"
edition = "2021"
license = "MIT"
publish = false
Expand All @@ -12,6 +12,7 @@ crate-type = ['cdylib']
node_common = { path = "../common" }
extism-pdk = { workspace = true }
proto_pdk = { workspace = true }
regex = { workspace = true }
serde = { workspace = true }

[dev-dependencies]
Expand Down
46 changes: 36 additions & 10 deletions crates/node-depman/src/proto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,13 +180,28 @@ struct RegistryResponse {
versions: HashMap<String, RegistryVersion>,
}

// https://github.com/moonrepo/proto/issues/257
fn parse_registry_response(res: HttpResponse, is_yarn: bool) -> Result<RegistryResponse, Error> {
if !is_yarn {
return res.json();
}

let pattern = regex::bytes::Regex::new("[\u{0000}-\u{001F}]+").unwrap();
let body = res.body();
// let text = String::from_bytes(res.body())?;

Ok(json::from_slice(&pattern.replace_all(&body, b""))?)
}

#[plugin_fn]
pub fn load_versions(Json(input): Json<LoadVersionsInput>) -> FnResult<Json<LoadVersionsOutput>> {
let mut output = LoadVersionsOutput::default();
let manager = PackageManager::detect();
let package_name = manager.get_package_name(&input.initial);

let mut map_output = |res: RegistryResponse| -> Result<(), Error> {
let mut map_output = |res: HttpResponse, is_yarn: bool| -> Result<(), Error> {
let res = parse_registry_response(res, is_yarn)?;

for item in res.versions.values() {
output.versions.push(Version::parse(&item.version)?);
}
Expand All @@ -205,16 +220,27 @@ pub fn load_versions(Json(input): Json<LoadVersionsInput>) -> FnResult<Json<Load
Ok(())
};

map_output(fetch_url(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("https://registry.npmjs.org/yarn/")?)?;
} else if manager.is_yarn_classic(&input.initial) {
map_output(fetch_url("https://registry.npmjs.org/@yarnpkg/cli-dist/")?)?;
if manager == PackageManager::Yarn {
map_output(
fetch(HttpRequest::new("https://registry.npmjs.org/yarn/"), None)?,
true,
)?;
map_output(
fetch(
HttpRequest::new("https://registry.npmjs.org/@yarnpkg/cli-dist/"),
None,
)?,
true,
)?;
} else {
map_output(
fetch(
HttpRequest::new(format!("https://registry.npmjs.org/{}/", package_name)),
None,
)?,
false,
)?;
}

output
Expand Down
2 changes: 1 addition & 1 deletion crates/node/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "node_plugin"
version = "0.4.2"
version = "0.4.3"
edition = "2021"
license = "MIT"
publish = false
Expand Down
6 changes: 3 additions & 3 deletions crates/node/tests/versions_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ generate_resolve_versions_tests!("node-test", {
"10.1" => "10.1.0",
"lts-gallium" => "16.20.2",
"lts/fermium" => "14.21.3",
"stable" => "18.18.2",
"node" => "21.0.0",
"stable" => "20.9.0",
"node" => "21.1.0",
});

#[test]
Expand Down Expand Up @@ -45,7 +45,7 @@ fn sets_lts_aliases() {
aliases,
[
"argon", "boron", "carbon", "dubnium", "erbium", "fermium", "gallium", "hydrogen",
"latest", "stable"
"iron", "latest", "stable"
]
);
}
Expand Down
Loading