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

deno add - support http: and https: specifiers #23216

Open
bartlomieju opened this issue Apr 3, 2024 · 7 comments
Open

deno add - support http: and https: specifiers #23216

bartlomieju opened this issue Apr 3, 2024 · 7 comments
Labels
feat new feature (which has been agreed to/accepted) good first issue Good for newcomers

Comments

@bartlomieju
Copy link
Member

Currently deno add only works with jsr: and npm: specifiers.

We should add an ability to do deno add https://deno.land/x/foo/bar.ts.

To do this, we should leverage existing infer_name_from_url() helper, that is already used in deno install and deno compile

deno/cli/tools/installer.rs

Lines 131 to 185 in d3d3582

pub async fn infer_name_from_url(url: &Url) -> Option<String> {
// If there's an absolute url with no path, eg. https://my-cli.com
// perform a request, and see if it redirects another file instead.
let mut url = url.clone();
if url.path() == "/" {
let client = HttpClient::new(None, None);
if let Ok(res) = client.get_redirected_response(url.clone()).await {
url = res.url().clone();
}
}
if let Ok(npm_ref) = NpmPackageReqReference::from_specifier(&url) {
let npm_ref = npm_ref.into_inner();
if let Some(sub_path) = npm_ref.sub_path {
if !sub_path.contains('/') {
return Some(sub_path);
}
}
if !npm_ref.req.name.contains('/') {
return Some(npm_ref.req.name);
}
return None;
}
let percent_decode = percent_encoding::percent_decode(url.path().as_bytes());
#[cfg(unix)]
let path = {
use std::os::unix::prelude::OsStringExt;
PathBuf::from(std::ffi::OsString::from_vec(
percent_decode.collect::<Vec<u8>>(),
))
};
#[cfg(windows)]
let path = PathBuf::from(percent_decode.decode_utf8_lossy().as_ref());
let mut stem = path.file_stem()?.to_string_lossy();
if matches!(stem.as_ref(), "main" | "mod" | "index" | "cli") {
if let Some(parent_name) = path.parent().and_then(|p| p.file_name()) {
stem = parent_name.to_string_lossy();
}
}
// if atmark symbol appears in the index other than 0 (e.g. `foo@bar`) we use
// the former part as the inferred name because the latter part is most likely
// a version number.
match stem.find('@') {
Some(at_index) if at_index > 0 => {
stem = stem.split_at(at_index).0.to_string().into();
}
_ => {}
}
Some(stem.to_string())
}

@bartlomieju bartlomieju added good first issue Good for newcomers feat new feature (which has been agreed to/accepted) labels Apr 3, 2024
@dsherret
Copy link
Member

dsherret commented Apr 3, 2024

Note that there is #23144 for aliases

@tristanisham
Copy link

This would require modifying https://github.com/denoland/deno_semver.

@dsherret
Copy link
Member

dsherret commented Apr 5, 2024

@tristanisham why?

@tristanisham
Copy link

@tristanisham why?

I believe that's where all the code for specifiers is. If the goal is to add "http(s):" that's the package where the actual structs and implementation details reside.

image

@tristanisham
Copy link

For the record:

Had a discussion about adding the feature to the deno_semver crate, but concluded that it would be better implemented here.

@jmj0502
Copy link

jmj0502 commented May 10, 2024

@bartlomieju @tristanisham Can I take this issue? I may have some questions regarding certain internals of the project but I would like to give it a shot c:

@tristanisham
Copy link

Be my guess. I haven't finished my PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feat new feature (which has been agreed to/accepted) good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

4 participants