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
mod universal_loader;
use crate::ModuleStore;
use data_url::DataUrl;
use deno_core::{anyhow::bail, error::AnyError, ModuleSpecifier};
use std::{sync::Arc, time::Duration};
#[derive(Clone, Debug)]
pub struct UniversalModuleLoader {
store: Option<Arc<dyn ModuleStore>>,
#[allow(dead_code)]
compile: bool,
}
pub async fn get_source_code(m: &ModuleSpecifier) -> Result<String, AnyError> {
let code = match m.scheme() {
"http" | "https" => {
let client = reqwest::Client::builder()
.timeout(Duration::from_millis(5000))
.build()?;
let res = client.get(m.to_owned()).send().await?;
let res = res.error_for_status()?;
res.text().await?
}
"file" => {
let path = match m.to_file_path() {
Ok(path) => path,
Err(_) => bail!("Invalid file URL."),
};
tokio::fs::read_to_string(path).await?
}
"data" => {
let url = match DataUrl::process(m.as_str()) {
Ok(url) => url,
Err(_) => bail!("Not a valid data URL."),
};
let bytes = match url.decode_to_vec() {
Ok((bytes, _)) => bytes,
Err(_) => bail!("Not a valid data URL."),
};
match String::from_utf8(bytes) {
Ok(s) => s,
Err(_) => bail!("Not a valid data URL code."),
}
}
schema => bail!("Invalid schema {}", schema),
};
Ok(code)
}