mirror of
https://github.com/denoland/deno.git
synced 2024-11-21 15:04:11 -05:00
fix(cli): avoid crash on import of invalid module names (#19523)
Fixes https://github.com/denoland/deno/issues/17748 Closes #17770 Co-authored-by: Anton Bershanskiy <bershanskiy@users.noreply.github.com> Co-authored-by: David Sherret <dsherret@users.noreply.github.com>
This commit is contained in:
parent
7c3a641dae
commit
df233a498d
4 changed files with 36 additions and 1 deletions
|
@ -363,7 +363,23 @@ impl CliNpmRegistryApiInner {
|
|||
}
|
||||
|
||||
fn get_package_url(&self, name: &str) -> Url {
|
||||
self.base_url.join(name).unwrap()
|
||||
// list of all characters used in npm packages:
|
||||
// !, ', (, ), *, -, ., /, [0-9], @, [A-Za-z], _, ~
|
||||
const ASCII_SET: percent_encoding::AsciiSet =
|
||||
percent_encoding::NON_ALPHANUMERIC
|
||||
.remove(b'!')
|
||||
.remove(b'\'')
|
||||
.remove(b'(')
|
||||
.remove(b')')
|
||||
.remove(b'*')
|
||||
.remove(b'-')
|
||||
.remove(b'.')
|
||||
.remove(b'/')
|
||||
.remove(b'@')
|
||||
.remove(b'_')
|
||||
.remove(b'~');
|
||||
let name = percent_encoding::utf8_percent_encode(name, &ASCII_SET);
|
||||
self.base_url.join(&name.to_string()).unwrap()
|
||||
}
|
||||
|
||||
fn get_package_file_cache_path(&self, name: &str) -> PathBuf {
|
||||
|
|
|
@ -183,6 +183,13 @@ itest!(dynamic_import_reload_same_package {
|
|||
http_server: true,
|
||||
});
|
||||
|
||||
itest!(dynamic_import_invalid_package_name {
|
||||
args: "run -A --reload npm/dynamic_import_invalid_package_name/main.ts",
|
||||
output: "npm/dynamic_import_invalid_package_name/main.out",
|
||||
envs: env_vars_for_npm_tests(),
|
||||
http_server: true,
|
||||
});
|
||||
|
||||
itest!(env_var_re_export_dev {
|
||||
args: "run --allow-read --allow-env --quiet npm/env_var_re_export/main.js",
|
||||
output_str: Some("dev\n"),
|
||||
|
|
6
cli/tests/testdata/npm/dynamic_import_invalid_package_name/main.out
vendored
Normal file
6
cli/tests/testdata/npm/dynamic_import_invalid_package_name/main.out
vendored
Normal file
|
@ -0,0 +1,6 @@
|
|||
Download http://localhost:4545/npm/registry/ws%3A
|
||||
FAILED
|
||||
TypeError: npm package 'ws:' does not exist.
|
||||
at async file:///[WILDCARD]/dynamic_import_invalid_package_name/main.ts:2:3 {
|
||||
code: "ERR_MODULE_NOT_FOUND"
|
||||
}
|
6
cli/tests/testdata/npm/dynamic_import_invalid_package_name/main.ts
vendored
Normal file
6
cli/tests/testdata/npm/dynamic_import_invalid_package_name/main.ts
vendored
Normal file
|
@ -0,0 +1,6 @@
|
|||
try {
|
||||
await import(`npm:${"ws:"}`);
|
||||
} catch (err) {
|
||||
console.log("FAILED");
|
||||
console.log(err);
|
||||
}
|
Loading…
Reference in a new issue