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
use std::io::{BufRead, BufReader, Read};
use std::str;
use crate::decoder::{decode_data_url, strip_junk_header, StripHeaderReader};
use crate::errors::Result;
use crate::jsontypes::MinimalRawSourceMap;
use crate::types::DecodedMap;
use url::Url;
#[derive(PartialEq, Eq, Debug)]
pub enum SourceMapRef {
Ref(String),
LegacyRef(String),
}
fn resolve_url(ref_url: &str, minified_url: &Url) -> Option<Url> {
minified_url.join(ref_url).ok()
}
impl SourceMapRef {
pub fn get_url(&self) -> &str {
match *self {
SourceMapRef::Ref(ref u) => u.as_str(),
SourceMapRef::LegacyRef(ref u) => u.as_str(),
}
}
pub fn resolve(&self, minified_url: &str) -> Option<String> {
let url = self.get_url();
if url.starts_with("data:") {
return None;
}
resolve_url(url, &Url::parse(minified_url).ok()?).map(|x| x.to_string())
}
#[cfg(any(unix, windows, target_os = "redox"))]
pub fn resolve_path(&self, minified_path: &std::path::Path) -> Option<std::path::PathBuf> {
let url = self.get_url();
if url.starts_with("data:") {
return None;
}
resolve_url(url, &Url::from_file_path(minified_path).ok()?)
.and_then(|x| x.to_file_path().ok())
}
pub fn get_embedded_sourcemap(&self) -> Result<Option<DecodedMap>> {
let url = self.get_url();
if url.starts_with("data:") {
Ok(Some(decode_data_url(url)?))
} else {
Ok(None)
}
}
}
pub fn locate_sourcemap_reference<R: Read>(rdr: R) -> Result<Option<SourceMapRef>> {
for line in BufReader::new(rdr).lines() {
let line = line?;
if line.starts_with("//# sourceMappingURL=") || line.starts_with("//@ sourceMappingURL=") {
let url = str::from_utf8(&line.as_bytes()[21..])?.trim().to_owned();
if line.starts_with("//@") {
return Ok(Some(SourceMapRef::LegacyRef(url)));
} else {
return Ok(Some(SourceMapRef::Ref(url)));
}
}
}
Ok(None)
}
pub fn locate_sourcemap_reference_slice(slice: &[u8]) -> Result<Option<SourceMapRef>> {
locate_sourcemap_reference(slice)
}
fn is_sourcemap_common(rsm: MinimalRawSourceMap) -> bool {
(rsm.version.is_some() || rsm.file.is_some())
&& ((rsm.sources.is_some()
|| rsm.source_root.is_some()
|| rsm.sources_content.is_some()
|| rsm.names.is_some())
&& rsm.mappings.is_some())
|| rsm.sections.is_some()
}
fn is_sourcemap_impl<R: Read>(rdr: R) -> Result<bool> {
let mut rdr = StripHeaderReader::new(rdr);
let mut rdr = BufReader::new(&mut rdr);
let rsm: MinimalRawSourceMap = serde_json::from_reader(&mut rdr)?;
Ok(is_sourcemap_common(rsm))
}
fn is_sourcemap_slice_impl(slice: &[u8]) -> Result<bool> {
let content = strip_junk_header(slice)?;
let rsm: MinimalRawSourceMap = serde_json::from_slice(content)?;
Ok(is_sourcemap_common(rsm))
}
pub fn is_sourcemap<R: Read>(rdr: R) -> bool {
is_sourcemap_impl(rdr).unwrap_or(false)
}
pub fn is_sourcemap_slice(slice: &[u8]) -> bool {
is_sourcemap_slice_impl(slice).unwrap_or(false)
}