Skip to content

Commit c13f830

Browse files
committed
Start try extension attempt
1 parent 50b8935 commit c13f830

File tree

2 files changed

+53
-19
lines changed

2 files changed

+53
-19
lines changed

src/deno_dir.rs

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -133,18 +133,55 @@ impl DenoDir {
133133
self: &DenoDir,
134134
module_name: &str,
135135
filename: &str,
136-
) -> DenoResult<String> {
136+
) -> DenoResult<CodeFetchOutput> {
137137
if is_remote(module_name) {
138-
self.fetch_remote_source(module_name, filename)
138+
let try_extension = |ext| {
139+
let module_name = format!("{}{}", module_name, ext);
140+
let filename = format!("{}{}", filename, ext);
141+
let source_code = self.fetch_remote_source(&module_name, &filename)?;
142+
return Ok(CodeFetchOutput {
143+
module_name: module_name.to_string(),
144+
filename: filename.to_string(),
145+
source_code,
146+
maybe_output_code: None,
147+
});
148+
};
149+
if module_name.ends_with(".ts") || module_name.ends_with(".js") {
150+
return try_extension("");
151+
}
152+
println!("Trying {}.ts...", module_name);
153+
if let Ok(v) = try_extension(".ts") {
154+
return Ok(v);
155+
}
156+
println!("Trying {}.js...", module_name);
157+
try_extension(".js")
139158
} else if module_name.starts_with(ASSET_PREFIX) {
140159
panic!("Asset resolution should be done in JS, not Rust.");
141160
} else {
142161
assert_eq!(
143162
module_name, filename,
144163
"if a module isn't remote, it should have the same filename"
145164
);
146-
let src = fs::read_to_string(Path::new(filename))?;
147-
Ok(src)
165+
let try_extension = |ext| {
166+
let module_name = format!("{}{}", module_name, ext);
167+
let filename = format!("{}{}", filename, ext);
168+
let source_code = fs::read_to_string(Path::new(&filename))?;
169+
return Ok(CodeFetchOutput {
170+
module_name: module_name.to_string(),
171+
filename: filename.to_string(),
172+
source_code,
173+
maybe_output_code: None,
174+
});
175+
};
176+
if module_name.ends_with(".ts") || module_name.ends_with(".js") {
177+
return try_extension("");
178+
}
179+
println!("Trying {}.ts...", module_name);
180+
if let Ok(v) = try_extension(".ts") {
181+
return Ok(v);
182+
}
183+
println!("Trying {}.js...", module_name);
184+
try_extension(".js")
148185
}
149186
}
150187

@@ -161,16 +198,7 @@ impl DenoDir {
161198
let (module_name, filename) =
162199
self.resolve_module(module_specifier, containing_file)?;
163200

164-
let result = self
165-
.get_source_code(module_name.as_str(), filename.as_str())
166-
.and_then(|source_code| {
167-
Ok(CodeFetchOutput {
168-
module_name,
169-
filename,
170-
source_code,
171-
maybe_output_code: None,
172-
})
173-
});
201+
let result = self.get_source_code(module_name.as_str(), filename.as_str());
174202
let out = match result {
175203
Err(err) => {
176204
if err.kind() == ErrorKind::NotFound {

src/http.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33
use errors::DenoResult;
44
use tokio_util;
55

6-
use futures::Future;
6+
// use futures::Future;
77
use futures::Stream;
88
use hyper;
99
use hyper::client::Client;
1010
use hyper::client::HttpConnector;
1111
use hyper::Uri;
1212
use hyper_rustls;
13+
use std::io;
1314

1415
type Connector = hyper_rustls::HttpsConnector<HttpConnector>;
1516

@@ -31,10 +32,15 @@ pub fn get_client() -> Client<Connector, hyper::Body> {
3132
pub fn fetch_sync_string(module_name: &str) -> DenoResult<String> {
3233
let url = module_name.parse::<Uri>().unwrap();
3334
let client = get_client();
34-
let future = client
35-
.get(url)
36-
.and_then(|response| response.into_body().concat2());
37-
let body = tokio_util::block_on(future)?;
35+
let get_future = client.get(url);
36+
let response = tokio_util::block_on(get_future)?;
37+
if !response.status().is_success() {
38+
Err(io::Error::new(
39+
io::ErrorKind::NotFound,
40+
format!("cannot load from '{}'", module_name),
41+
))?;
42+
}
43+
let body = tokio_util::block_on(response.into_body().concat2())?;
3844
Ok(String::from_utf8(body.to_vec()).unwrap())
3945
}
4046

0 commit comments

Comments
 (0)