Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Guess extensions on extension not provided #859

Merged
merged 11 commits into from
Oct 2, 2018

Conversation

kevinkassimo
Copy link
Contributor

@kevinkassimo kevinkassimo commented Sep 29, 2018

Trying to close #857

Guess .ts -> Guess .js -> Give up

Added tests in both tests/ and deno_dir.rs.
(Hope I have not missed something...)

@kevinkassimo kevinkassimo changed the title [WIP] Start try extension attempt [WIP] Guess extension on extension not provided Sep 29, 2018
@kevinkassimo kevinkassimo changed the title [WIP] Guess extension on extension not provided Guess extensions on extension not provided Sep 29, 2018
src/deno_dir.rs Show resolved Hide resolved
src/http.rs Outdated
format!("cannot load from '{}'", module_name),
))?;
}
let body = tokio_util::block_on(response.into_body().concat2())?;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it doesn't matter but this could be done using only one block_on. It would be nice to have a test showing we get NotFound error when trying to hit 404 url.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I have somehow combined into a single block_on, but had to explicitly implement Future for a struct holding Concat2 and StatusCode (to implement using enum that either holds body or and error seems more problematic)
(I started to use Rust only very recently, so this might not be a very good solution from the viewpoint of elegance. Would like to learn about better alternatives!)

btw a NotFound test is added

src/deno_dir.rs Outdated Show resolved Hide resolved
src/deno_dir.rs Outdated Show resolved Hide resolved
src/deno_dir.rs Show resolved Hide resolved
src/http.rs Outdated
Async::NotReady => Ok(Async::NotReady),
}
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems not so much related to the current problem - although something like this may be necessary at some point when we address fetch body streaming. I'd rather you not add this here. Either go back to using block_on twice or find some other way of doing this. Have you tried using map() ?

@kevinkassimo
Copy link
Contributor Author

kevinkassimo commented Oct 1, 2018

@ry So I have somehow forcefully used map to allow a single block_on (which I have to admit it looks ergonomically very ugly). As I am a Rust user only quite recently, I myself could not found a better solution for the implementation (without messing with manually creating a hyper::Error, which hyper itself is preventing me from doing).

If this still looks pretty bad, I could either shift back to the original 2 block_on implementation, or probably let someone more familiar with Rust to take over this PR and address this very last piece.

Aside: looks like async/await syntax is recently introduced to Rust

src/http.rs Show resolved Hide resolved
.concat2()
.map(|body| String::from_utf8(body.to_vec()).unwrap())
.map_err(|err| DenoError::from(err)),
)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to do this more simply myself but couldn't. I think this is correct. Rust is annoyingly painful at times.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah... I found threads complaining similar stuff this morning, and supposedly these issues could be better addressed with the currently experimental async-await syntax

src/deno_dir.rs Show resolved Hide resolved
src/deno_dir.rs Outdated
});
};
let default_attempt = use_extension("");
if let Ok(_) = default_attempt {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If no need to use the value in Ok, would it be better to use with is_some()?

if default_attempt.is_some() {
  return default_attempt;
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW should be

if default_attempt.is_ok() {
  return default_attempt;
}

Thanks!

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, yes! Thanks for catching the typo :P

Copy link
Member

@ry ry left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM - Thanks Kevin!

@ry ry merged commit eba58b7 into denoland:master Oct 2, 2018
ry added a commit to ry/deno that referenced this pull request Oct 4, 2018
- Improve fetch headers (denoland#853)
- Add deno.truncate (denoland#805)
- Add copyFile/copyFileSync (denoland#863)
- Limit depth of output in console.log for nested objects, and add
  console.dir (denoland#826)
- Guess extensions on extension not provided (denoland#859)
- Renames:
  deno.platform -> deno.platform.os
  deno.arch -> deno.platform.arch
- Upgrade TS to 3.0.3
- Add readDirSync(), readDir()
- Add support for TCP servers and clients. (denoland#884)
  Adds deno.listen(), deno.dial(), deno.Listener and deno.Conn.
@ry ry mentioned this pull request Oct 4, 2018
ry added a commit that referenced this pull request Oct 4, 2018
- Improve fetch headers (#853)
- Add deno.truncate (#805)
- Add copyFile/copyFileSync (#863)
- Limit depth of output in console.log for nested objects, and add
  console.dir (#826)
- Guess extensions on extension not provided (#859)
- Renames:
  deno.platform -> deno.platform.os
  deno.arch -> deno.platform.arch
- Upgrade TS to 3.0.3
- Add readDirSync(), readDir()
- Add support for TCP servers and clients. (#884)
  Adds deno.listen(), deno.dial(), deno.Listener and deno.Conn.
@kevinkassimo kevinkassimo deleted the import/try_ext branch December 27, 2019 07:52
hardfist pushed a commit to hardfist/deno that referenced this pull request Aug 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Supporting extension-less module resolution
3 participants