summaryrefslogtreecommitdiff
path: root/ext/node/package_json.rs
blob: 19a79da961db4c6350d29b7be9cdadff09b2ecd0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.

use super::DenoDirNpmResolver;
use deno_core::anyhow;
use deno_core::anyhow::bail;
use deno_core::error::AnyError;
use deno_core::serde_json;
use deno_core::serde_json::Map;
use deno_core::serde_json::Value;
use serde::Serialize;
use std::io::ErrorKind;
use std::path::PathBuf;

// TODO(bartlomieju): deduplicate with cli/compat/esm_resolver.rs
#[derive(Clone, Debug, Serialize)]
pub struct PackageJson {
  pub exists: bool,
  pub exports: Option<Map<String, Value>>,
  pub imports: Option<Map<String, Value>>,
  pub bin: Option<Value>,
  pub main: Option<String>,
  pub name: Option<String>,
  pub path: PathBuf,
  pub typ: String,
  pub types: Option<String>,
}

impl PackageJson {
  pub fn empty(path: PathBuf) -> PackageJson {
    PackageJson {
      exists: false,
      exports: None,
      imports: None,
      bin: None,
      main: None,
      name: None,
      path,
      typ: "none".to_string(),
      types: None,
    }
  }

  pub fn load(
    resolver: &dyn DenoDirNpmResolver,
    path: PathBuf,
  ) -> Result<PackageJson, AnyError> {
    resolver.ensure_read_permission(&path)?;
    let source = match std::fs::read_to_string(&path) {
      Ok(source) => source,
      Err(err) if err.kind() == ErrorKind::NotFound => {
        return Ok(PackageJson::empty(path));
      }
      Err(err) => bail!(
        "Error loading package.json at {}. {:#}",
        path.display(),
        err
      ),
    };

    if source.trim().is_empty() {
      return Ok(PackageJson::empty(path));
    }

    let package_json: Value = serde_json::from_str(&source)
      .map_err(|err| anyhow::anyhow!("malformed package.json {}", err))?;

    let imports_val = package_json.get("imports");
    let main_val = package_json.get("main");
    let name_val = package_json.get("name");
    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 mut map = Map::new();
        map.insert(".".to_string(), exports.to_owned());
        map
      } else {
        exports.as_object().unwrap().to_owned()
      }
    });

    let imports = if let Some(imp) = imports_val {
      imp.as_object().map(|imp| imp.to_owned())
    } else {
      None
    };
    let main = if let Some(m) = main_val {
      m.as_str().map(|m| m.to_string())
    } else {
      None
    };
    let name = if let Some(n) = name_val {
      n.as_str().map(|n| n.to_string())
    } else {
      None
    };

    // Ignore unknown types for forwards compatibility
    let typ = if let Some(t) = type_val {
      if let Some(t) = t.as_str() {
        if t != "module" && t != "commonjs" {
          "none".to_string()
        } else {
          t.to_string()
        }
      } else {
        "none".to_string()
      }
    } else {
      "none".to_string()
    };

    // for typescript, it looks for "typings" first, then "types"
    let types = package_json
      .get("typings")
      .or_else(|| package_json.get("types"))
      .and_then(|t| t.as_str().map(|s| s.to_string()));

    let package_json = PackageJson {
      exists: true,
      path,
      main,
      name,
      typ,
      types,
      exports,
      imports,
      bin,
    };
    Ok(package_json)
  }
}

fn is_conditional_exports_main_sugar(exports: &Value) -> bool {
  if exports.is_string() || exports.is_array() {
    return true;
  }

  if exports.is_null() || !exports.is_object() {
    return false;
  }

  let exports_obj = exports.as_object().unwrap();
  let mut is_conditional_sugar = false;
  let mut i = 0;
  for key in exports_obj.keys() {
    let cur_is_conditional_sugar = key.is_empty() || !key.starts_with('.');
    if i == 0 {
      is_conditional_sugar = cur_is_conditional_sugar;
      i += 1;
    } else if is_conditional_sugar != cur_is_conditional_sugar {
      panic!("\"exports\" cannot contains some keys starting with \'.\' and some not.
        The exports object must either be an object of package subpath keys
        or an object of main entry condition name keys only.")
    }
  }

  is_conditional_sugar
}