From 56fb526e53c392b9c89aa51dc807bcf1e7571726 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Fri, 17 Jan 2025 17:07:13 +0000 Subject: [PATCH] refactor: update deno_graph, deno_npm, deno_semver (#206) --- Cargo.lock | 190 ++++++++++++++++++++++++++-------------- Cargo.toml | 8 +- lib/Cargo.toml | 2 + lib/lib.rs | 20 +++-- rust-toolchain.toml | 2 +- src/examples/builder.rs | 54 +++++++++--- src/v2.rs | 28 +++--- 7 files changed, 208 insertions(+), 96 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d2080b1..c1ee53f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -199,6 +199,28 @@ version = "1.6.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a12916984aab3fa6e39d655a33e09c0071eb36d6ab3aea5c2d78551f1df6d952" +[[package]] +name = "capacity_builder" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f2d24a6dcf0cd402a21b65d35340f3a49ff3475dc5fdac91d22d2733e6641c6" +dependencies = [ + "capacity_builder_macros", + "ecow", + "hipstr", + "itoa", +] + +[[package]] +name = "capacity_builder_macros" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3b4a6cae9efc04cc6cbb8faf338d2c497c165c83e74509cf4dbedea948bbf6e5" +dependencies = [ + "quote", + "syn 2.0.90", +] + [[package]] name = "cast" version = "0.3.0" @@ -470,16 +492,16 @@ dependencies = [ "swc_visit", "swc_visit_macros", "text_lines", - "thiserror 2.0.4", + "thiserror", "unicode-width", "url", ] [[package]] name = "deno_error" -version = "0.5.2" +version = "0.5.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "199c66ffd17ee1a948904d33f3d3f364573951c1f9fb3f859bfe7770bf33862a" +checksum = "9c23dbc46d5804814b08b4675838f9884e3a52916987ec5105af36d42f9911b5" dependencies = [ "deno_error_macro", "libc", @@ -490,9 +512,9 @@ dependencies = [ [[package]] name = "deno_error_macro" -version = "0.5.2" +version = "0.5.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3cd99df6ae75443907e1f959fc42ec6dcea67a7bd083e76cf23a117102c9a2ce" +checksum = "babccedee31ce7e57c3e6dff2cb3ab8d68c49d0df8222fe0d11d628e65192790" dependencies = [ "proc-macro2", "quote", @@ -501,19 +523,22 @@ dependencies = [ [[package]] name = "deno_graph" -version = "0.86.3" +version = "0.87.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fc78ed0b4bbcb4197300f0d6e7d1edc2d2c5019cdb9dedba7ff229158441885b" +checksum = "f56d4eb4b7c81ae920b6d18c45a1866924f93110caee80bbbc362dc28143f2bb" dependencies = [ - "anyhow", "async-trait", + "capacity_builder", "data-url", "deno_ast", + "deno_error", + "deno_media_type", + "deno_path_util", "deno_semver", "deno_unsync", "encoding_rs", "futures", - "import_map 0.20.0", + "import_map", "indexmap", "log", "monch", @@ -523,28 +548,29 @@ dependencies = [ "serde", "serde_json", "sha2", - "thiserror 2.0.4", + "sys_traits", + "thiserror", "url", "wasm_dep_analyzer", ] [[package]] name = "deno_lockfile" -version = "0.23.2" +version = "0.24.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "559c19feb00af0c34f0bd4a20e56e12463fafd5c5069d6005f3ce33008027eea" +checksum = "632e835a53ed667d62fdd766c5780fe8361c831d3e3fbf1a760a0b7896657587" dependencies = [ "deno_semver", "serde", "serde_json", - "thiserror 2.0.4", + "thiserror", ] [[package]] name = "deno_media_type" -version = "0.2.2" +version = "0.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "eaa135b8a9febc9a51c16258e294e268a1276750780d69e46edb31cced2826e4" +checksum = "a417f8bd3f1074185c4c8ccb6ea6261ae173781596cc358e68ad07aaac11009d" dependencies = [ "data-url", "serde", @@ -553,12 +579,13 @@ dependencies = [ [[package]] name = "deno_npm" -version = "0.26.0" +version = "0.27.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f2f125a5dba7839c46394a0a9c835da9fe60f5f412587ab4956a76492a1cc6a8" +checksum = "4adceb4c34f10e837d0e3ae76e88dddefb13e83c05c1ef1699fa5519241c9d27" dependencies = [ - "anyhow", "async-trait", + "capacity_builder", + "deno_error", "deno_lockfile", "deno_semver", "futures", @@ -566,21 +593,37 @@ dependencies = [ "monch", "serde", "serde_json", - "thiserror 2.0.4", + "thiserror", + "url", +] + +[[package]] +name = "deno_path_util" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "420e8211aaba7fde83ccaa9a5dad855c3b940ed988d70c95159acd600a70dc87" +dependencies = [ + "deno_error", + "percent-encoding", + "sys_traits", + "thiserror", "url", ] [[package]] name = "deno_semver" -version = "0.6.0" +version = "0.7.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4756be7351289726087408984db18b9eb5e0186907673f39f858d119d0162071" +checksum = "4775271f9b5602482698f76d24ea9ed8ba27af7f587a7e9a876916300c542435" dependencies = [ + "capacity_builder", "deno_error", + "ecow", + "hipstr", "monch", "once_cell", "serde", - "thiserror 2.0.4", + "thiserror", "url", ] @@ -646,6 +689,15 @@ dependencies = [ "text_lines", ] +[[package]] +name = "ecow" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e42fc0a93992b20c58b99e59d61eaf1635a25bfbe49e4275c34ba0aee98119ba" +dependencies = [ + "serde", +] + [[package]] name = "either" version = "1.13.0" @@ -692,12 +744,13 @@ dependencies = [ "base64", "criterion", "deno_ast", + "deno_error", "deno_graph", "deno_npm", "deno_semver", "futures", "hashlink", - "import_map 0.21.0", + "import_map", "indexmap", "insta", "jsonc-parser", @@ -706,7 +759,7 @@ dependencies = [ "serde", "serde_json", "sha2", - "thiserror 2.0.4", + "thiserror", "tokio", "url", "xxhash-rust", @@ -718,14 +771,16 @@ version = "0.0.0" dependencies = [ "anyhow", "console_error_panic_hook", + "deno_error", "deno_graph", "eszip", "futures", "getrandom", - "import_map 0.21.0", + "import_map", "js-sys", "serde", "serde-wasm-bindgen", + "sys_traits", "wasm-bindgen", "wasm-bindgen-futures", "web-sys", @@ -962,6 +1017,17 @@ version = "0.3.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d231dfb89cfffdbc30e7fc41579ed6066ad03abda9e567ccafae602b97ec5024" +[[package]] +name = "hipstr" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "97971ffc85d4c98de12e2608e992a43f5294ebb625fdb045b27c731b64c4c6d6" +dependencies = [ + "serde", + "serde_bytes", + "sptr", +] + [[package]] name = "hstr" version = "0.2.12" @@ -1206,20 +1272,6 @@ version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cb56e1aa765b4b4f3aadfab769793b7087bb03a4ea4920644a6d238e2df5b9ed" -[[package]] -name = "import_map" -version = "0.20.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "373b8288ad259df0d1314e3e8b2fff0e5e63f22e01bc54ecd2c3c7ad77b9200c" -dependencies = [ - "indexmap", - "log", - "percent-encoding", - "serde", - "serde_json", - "url", -] - [[package]] name = "import_map" version = "0.21.0" @@ -1233,7 +1285,7 @@ dependencies = [ "percent-encoding", "serde", "serde_json", - "thiserror 2.0.4", + "thiserror", "url", ] @@ -1300,9 +1352,9 @@ dependencies = [ [[package]] name = "itoa" -version = "1.0.11" +version = "1.0.14" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "49f1f14873335454500d59611f1cf4a4b0f786f9ac11f4312a78e4cf2566695b" +checksum = "d75a2a4b1b190afb6f5425f10f6a8f959d2ea0b9c2b1d79553551850539e4674" [[package]] name = "js-sys" @@ -2033,6 +2085,15 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "serde_bytes" +version = "0.11.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "387cc504cb06bb40a96c8e04e951fe01854cf6bc921053c954e4a606d9675c6a" +dependencies = [ + "serde", +] + [[package]] name = "serde_derive" version = "1.0.210" @@ -2179,6 +2240,12 @@ version = "0.9.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6980e8d7511241f8acf4aebddbb1ff938df5eebe98691418c4468d0b72a96a67" +[[package]] +name = "sptr" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3b9b39299b249ad65f3b7e96443bad61c02ca5cd3589f46cb6d610a0fd6c0d6a" + [[package]] name = "stable_deref_trait" version = "1.2.0" @@ -2622,6 +2689,16 @@ dependencies = [ "syn 2.0.90", ] +[[package]] +name = "sys_traits" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b46ac05dfbe9fd3a9703eff20e17f5b31e7b6a54daf27a421dcd56c7a27ecdd" +dependencies = [ + "js-sys", + "wasm-bindgen", +] + [[package]] name = "system-configuration" version = "0.5.1" @@ -2679,33 +2756,13 @@ dependencies = [ "serde", ] -[[package]] -name = "thiserror" -version = "1.0.64" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d50af8abc119fb8bb6dbabcfa89656f46f84aa0ac7688088608076ad2b459a84" -dependencies = [ - "thiserror-impl 1.0.64", -] - [[package]] name = "thiserror" version = "2.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2f49a1853cf82743e3b7950f77e0f4d622ca36cf4317cba00c767838bac8d490" dependencies = [ - "thiserror-impl 2.0.4", -] - -[[package]] -name = "thiserror-impl" -version = "1.0.64" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "08904e7672f5eb876eaaf87e0ce17857500934f4981c4a0ab2b4aa98baac7fc3" -dependencies = [ - "proc-macro2", - "quote", - "syn 2.0.90", + "thiserror-impl", ] [[package]] @@ -3029,11 +3086,12 @@ checksum = "af190c94f2773fdb3729c55b007a722abb5384da03bc0986df4c289bf5567e96" [[package]] name = "wasm_dep_analyzer" -version = "0.1.0" +version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7f270206a91783fd90625c8bb0d8fbd459d0b1d1bf209b656f713f01ae7c04b8" +checksum = "2eeee3bdea6257cc36d756fa745a70f9d393571e47d69e0ed97581676a5369ca" dependencies = [ - "thiserror 1.0.64", + "deno_error", + "thiserror", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 704d9a2..aff34ba 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,10 +11,11 @@ license = "MIT" members = ["lib"] [workspace.dependencies] -deno_graph = { version = "0.86.3", default-features = false } +deno_graph = { version = "0.87.0", default-features = false } deno_ast = { version = "0.44.0", features = ["transpiling"] } import_map = "0.21.0" serde = "1" +deno_error = "0.5.5" [profile.release] codegen-units = 1 @@ -48,9 +49,10 @@ anyhow = "1" async-trait = "0.1.68" base64 = "0.21.0" deno_ast = { workspace = true } +deno_error.workspace = true deno_graph = { workspace = true } -deno_npm = "0.26.0" -deno_semver = "0.6.0" +deno_npm = "0.27.2" +deno_semver = "0.7.1" futures = "0.3.26" hashlink = "0.8.2" indexmap = "2" diff --git a/lib/Cargo.toml b/lib/Cargo.toml index 880775b..b0e6844 100644 --- a/lib/Cargo.toml +++ b/lib/Cargo.toml @@ -15,6 +15,7 @@ crate-type = ["cdylib"] anyhow = "1" console_error_panic_hook = "0.1.7" deno_graph = { workspace = true } +deno_error.workspace = true eszip = { path = "../" } getrandom = { version = "*", features = ["js"] } import_map = { workspace = true } @@ -25,4 +26,5 @@ wasm-bindgen-futures = { version = "=0.4.42" } serde = { workspace = true } serde-wasm-bindgen = "0.5.0" web-sys = { version = "=0.3.69", features = ["ReadableStreamByobReader"] } +sys_traits = { version = "0.1.7", features = ["real", "wasm"] } diff --git a/lib/lib.rs b/lib/lib.rs index 8b44fe2..c73e998 100644 --- a/lib/lib.rs +++ b/lib/lib.rs @@ -3,6 +3,7 @@ #![deny(clippy::print_stderr)] #![deny(clippy::print_stdout)] +use deno_error::JsErrorBox; use deno_graph::source::load_data_url; use deno_graph::source::CacheInfo; use deno_graph::source::LoadFuture; @@ -329,7 +330,7 @@ pub async fn build_eszip( imports: Vec::new(), passthrough_jsr_specifiers: false, executor: Default::default(), - file_system: Default::default(), + file_system: &sys_traits::impls::RealSys, jsr_url_provider: Default::default(), locker: None, npm_resolver: None, @@ -385,7 +386,13 @@ impl Loader for GraphLoader { } if specifier.scheme() == "data" { - Box::pin(std::future::ready(load_data_url(specifier))) + Box::pin(std::future::ready(load_data_url(specifier).map_err( + |err| { + deno_graph::source::LoadError::Other(Arc::new(JsErrorBox::from_err( + err, + ))) + }, + ))) } else { let specifier = specifier.clone(); let result = self.0.call2( @@ -411,9 +418,12 @@ impl Loader for GraphLoader { response .map(|value| serde_wasm_bindgen::from_value(value).unwrap()) .map_err(|err| { - anyhow::anyhow!(err + let err_str = err .as_string() - .unwrap_or_else(|| "an error occured during loading".to_string())) + .unwrap_or_else(|| "an error occured during loading".to_string()); + deno_graph::source::LoadError::Other(Arc::new(JsErrorBox::generic( + err_str, + ))) }) }) } @@ -433,7 +443,7 @@ impl Resolver for GraphResolver { if let Some(import_map) = &self.0 { import_map .resolve(specifier, &referrer_range.specifier) - .map_err(|err| ResolveError::Other(err.into())) + .map_err(ResolveError::ImportMap) } else { Ok(deno_graph::resolve_import( specifier, diff --git a/rust-toolchain.toml b/rust-toolchain.toml index e17bbf1..87499a9 100644 --- a/rust-toolchain.toml +++ b/rust-toolchain.toml @@ -1,3 +1,3 @@ [toolchain] -channel = "1.80.0" +channel = "1.82.0" components = ["clippy", "rustfmt"] diff --git a/src/examples/builder.rs b/src/examples/builder.rs index 95a0508..c1a6740 100644 --- a/src/examples/builder.rs +++ b/src/examples/builder.rs @@ -5,6 +5,7 @@ use std::sync::Arc; use deno_ast::EmitOptions; use deno_ast::TranspileOptions; +use deno_error::JsErrorBox; use deno_graph::source::CacheSetting; use deno_graph::source::ResolveError; use deno_graph::BuildOptions; @@ -112,7 +113,7 @@ impl deno_graph::source::Resolver for Resolver { if let Some(import_map) = &self.0 { import_map .resolve(specifier, &referrer_range.specifier) - .map_err(|e| ResolveError::Other(e.into())) + .map_err(ResolveError::ImportMap) } else { Ok(deno_graph::resolve_import( specifier, @@ -134,10 +135,25 @@ impl deno_graph::source::Loader for Loader { Box::pin(async move { match specifier.scheme() { - "data" => deno_graph::source::load_data_url(&specifier), + "data" => { + deno_graph::source::load_data_url(&specifier).map_err(|err| { + deno_graph::source::LoadError::Other(Arc::new( + JsErrorBox::from_err(err), + )) + }) + } "file" => { - let path = std::fs::canonicalize(specifier.to_file_path().unwrap())?; - let content = std::fs::read(&path)?; + let path = std::fs::canonicalize(specifier.to_file_path().unwrap()) + .map_err(|err| { + deno_graph::source::LoadError::Other(Arc::new( + JsErrorBox::from_err(err), + )) + })?; + let content = std::fs::read(&path).map_err(|err| { + deno_graph::source::LoadError::Other(Arc::new( + JsErrorBox::from_err(err), + )) + })?; Ok(Some(deno_graph::source::LoadResponse::Module { specifier: Url::from_file_path(&path).unwrap(), maybe_headers: None, @@ -145,11 +161,19 @@ impl deno_graph::source::Loader for Loader { })) } "http" | "https" => { - let resp = reqwest::get(specifier.as_str()).await?; + let resp = reqwest::get(specifier.as_str()).await.map_err(|err| { + deno_graph::source::LoadError::Other(Arc::new(JsErrorBox::generic( + err.to_string(), + ))) + })?; if resp.status() == StatusCode::NOT_FOUND { Ok(None) } else { - let resp = resp.error_for_status()?; + let resp = resp.error_for_status().map_err(|err| { + deno_graph::source::LoadError::Other(Arc::new( + JsErrorBox::generic(err.to_string()), + )) + })?; let mut headers = HashMap::new(); for key in resp.headers().keys() { let key_str = key.to_string(); @@ -162,7 +186,11 @@ impl deno_graph::source::Loader for Loader { headers.insert(key_str, values_str); } let url = resp.url().clone(); - let content = resp.bytes().await?; + let content = resp.bytes().await.map_err(|err| { + deno_graph::source::LoadError::Other(Arc::new( + JsErrorBox::generic(err.to_string()), + )) + })?; Ok(Some(deno_graph::source::LoadResponse::Module { specifier: url, maybe_headers: Some(headers), @@ -170,10 +198,14 @@ impl deno_graph::source::Loader for Loader { })) } } - _ => Err(anyhow::anyhow!( - "unsupported scheme: {}", - specifier.scheme() - )), + _ => { + let err: Arc = + Arc::new(JsErrorBox::generic(format!( + "unsupported scheme: {}", + specifier.scheme() + ))); + Err(deno_graph::source::LoadError::Other(err)) + } } }) } diff --git a/src/v2.rs b/src/v2.rs index 5c2f5d1..ba2f71a 100644 --- a/src/v2.rs +++ b/src/v2.rs @@ -28,6 +28,7 @@ use deno_npm::NpmPackageId; use deno_semver::npm::NpmPackageNvReference; use deno_semver::package::PackageNv; use deno_semver::package::PackageReq; +use deno_semver::StackString; use futures::future::poll_fn; use futures::io::AsyncReadExt; use hashlink::linked_hash_map::LinkedHashMap; @@ -1551,8 +1552,9 @@ impl EszipV2 { } Ok(None) } - | deno_graph::Module::External(_) - | deno_graph::Module::Node(_) => Ok(None), + deno_graph::Module::External(_) | deno_graph::Module::Node(_) => { + Ok(None) + } } } @@ -1782,7 +1784,7 @@ async fn read_npm_section( )); } }; - dependencies.insert(key, id.clone()); + dependencies.insert(StackString::from_string(key), id.clone()); } final_packages.push(SerializedNpmResolutionSnapshotPackage { id: id.clone(), @@ -1980,6 +1982,7 @@ mod tests { use async_trait::async_trait; use deno_ast::EmitOptions; use deno_ast::TranspileOptions; + use deno_error::JsErrorBox; use deno_graph::source::CacheSetting; use deno_graph::source::LoadOptions; use deno_graph::source::LoadResponse; @@ -2077,7 +2080,12 @@ mod tests { }) } "data" => { - let result = deno_graph::source::load_data_url(specifier); + let result = + deno_graph::source::load_data_url(specifier).map_err(|err| { + deno_graph::source::LoadError::Other(Arc::new( + JsErrorBox::from_err(err), + )) + }); Box::pin(async move { result }) } "npm" => Box::pin(async { Ok(None) }), @@ -2099,7 +2107,7 @@ mod tests { self .0 .resolve(specifier, &referrer_range.specifier) - .map_err(|err| ResolveError::Other(err.into())) + .map_err(ResolveError::ImportMap) } } @@ -2707,7 +2715,7 @@ mod tests { }; let import_map = import_map::parse_from_json( specifier.clone(), - &String::from_utf8(content.to_vec()).unwrap(), + core::str::from_utf8(&content).unwrap(), ) .unwrap(); let roots = vec![ModuleSpecifier::parse("file:///mapped.js").unwrap()]; @@ -2789,7 +2797,7 @@ mod tests { }; let import_map = import_map::parse_from_json( specifier.clone(), - &String::from_utf8(content.to_vec()).unwrap(), + core::str::from_utf8(&content).unwrap(), ) .unwrap(); let roots = @@ -2861,7 +2869,7 @@ mod tests { let import_map = import_map::parse_from_value( specifier.clone(), jsonc_parser::parse_to_serde_value( - &String::from_utf8(content.to_vec()).unwrap(), + core::str::from_utf8(&content).unwrap(), &Default::default(), ) .unwrap() @@ -2943,7 +2951,7 @@ mod tests { let import_map = import_map::parse_from_value( specifier.clone(), jsonc_parser::parse_to_serde_value( - &String::from_utf8(content.to_vec()).unwrap(), + core::str::from_utf8(&content).unwrap(), &Default::default(), ) .unwrap() @@ -3910,7 +3918,7 @@ mod tests { .iter() .map(|(key, value)| { ( - key.to_string(), + deno_semver::StackString::from_str(key), NpmPackageId::from_serialized(value).unwrap(), ) })