From 15cfb675512db5723f7deee9965390f038dc1c41 Mon Sep 17 00:00:00 2001 From: Alessandro Scandone Date: Fri, 22 Sep 2023 11:21:38 +0200 Subject: [PATCH] fix(node/package_json): Avoid panic when "exports" field is null (#20588) Fixes #20558 Implementation: when package.json `exports` field is `null`, treat it as if it was not set --- ext/node/package_json.rs | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/ext/node/package_json.rs b/ext/node/package_json.rs index ae5e159a1b..0b20a019e7 100644 --- a/ext/node/package_json.rs +++ b/ext/node/package_json.rs @@ -114,14 +114,14 @@ impl PackageJson { let version_val = package_json.get("version"); let type_val = package_json.get("type"); let bin = package_json.get("bin").map(ToOwned::to_owned); - let exports = package_json.get("exports").map(|exports| { - if is_conditional_exports_main_sugar(exports) { + let exports = package_json.get("exports").and_then(|exports| { + Some(if is_conditional_exports_main_sugar(exports) { let mut map = Map::new(); map.insert(".".to_string(), exports.to_owned()); map } else { - exports.as_object().unwrap().to_owned() - } + exports.as_object()?.to_owned() + }) }); let imports = imports_val @@ -240,3 +240,19 @@ fn is_conditional_exports_main_sugar(exports: &Value) -> bool { is_conditional_sugar } + +#[cfg(test)] +mod test { + use super::*; + + #[test] + fn null_exports_should_not_crash() { + let package_json = PackageJson::load_from_string( + PathBuf::from("/package.json"), + r#"{ "exports": null }"#.to_string(), + ) + .unwrap(); + + assert!(package_json.exports.is_none()); + } +}