Skip to content

Commit

Permalink
fix: support import map specified as data uri (denoland#17531)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsherret authored Jan 25, 2023
1 parent c6c8c91 commit 34c14db
Show file tree
Hide file tree
Showing 10 changed files with 125 additions and 105 deletions.
31 changes: 30 additions & 1 deletion cli/args/import_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,40 @@
use deno_core::error::AnyError;
use deno_core::serde_json;
use deno_core::url::Url;
use deno_runtime::permissions::PermissionsContainer;
use import_map::ImportMap;
use import_map::ImportMapDiagnostic;
use log::warn;

pub fn import_map_from_value(
use super::ConfigFile;
use crate::file_fetcher::get_source_from_data_url;
use crate::file_fetcher::FileFetcher;

pub async fn resolve_import_map_from_specifier(
specifier: &Url,
maybe_config_file: Option<&ConfigFile>,
file_fetcher: &FileFetcher,
) -> Result<ImportMap, AnyError> {
let value: serde_json::Value = if specifier.scheme() == "data" {
serde_json::from_str(&get_source_from_data_url(specifier)?.0)?
} else {
let import_map_config = maybe_config_file
.as_ref()
.filter(|c| c.specifier == *specifier);
match import_map_config {
Some(config) => config.to_import_map_value(),
None => {
let file = file_fetcher
.fetch(specifier, PermissionsContainer::allow_all())
.await?;
serde_json::from_str(&file.source)?
}
}
};
import_map_from_value(specifier, value)
}

fn import_map_from_value(
specifier: &Url,
json_value: serde_json::Value,
) -> Result<ImportMap, AnyError> {
Expand Down
44 changes: 12 additions & 32 deletions cli/args/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ mod flags_allow_net;
mod import_map;
mod lockfile;

pub use self::import_map::import_map_from_value;
pub use self::import_map::resolve_import_map_from_specifier;
use ::import_map::ImportMap;
pub use config_file::BenchConfig;
pub use config_file::CompilerOptions;
Expand All @@ -21,8 +21,6 @@ pub use config_file::TsConfig;
pub use config_file::TsConfigForEmit;
pub use config_file::TsConfigType;
pub use config_file::TsTypeLib;
use deno_core::serde_json;
use deno_runtime::permissions::PermissionsContainer;
pub use flags::*;
pub use lockfile::Lockfile;
pub use lockfile::LockfileError;
Expand Down Expand Up @@ -574,35 +572,17 @@ impl CliOptions {
Some(specifier) => specifier,
None => return Ok(None),
};
self
.resolve_import_map_from_specifier(&import_map_specifier, file_fetcher)
.await
.context(format!(
"Unable to load '{}' import map",
import_map_specifier
))
.map(Some)
}

async fn resolve_import_map_from_specifier(
&self,
import_map_specifier: &ModuleSpecifier,
file_fetcher: &FileFetcher,
) -> Result<ImportMap, AnyError> {
let import_map_config = self
.get_maybe_config_file()
.as_ref()
.filter(|c| c.specifier == *import_map_specifier);
let value: serde_json::Value = match import_map_config {
Some(config) => config.to_import_map_value(),
None => {
let file = file_fetcher
.fetch(import_map_specifier, PermissionsContainer::allow_all())
.await?;
serde_json::from_str(&file.source)?
}
};
import_map_from_value(import_map_specifier, value)
resolve_import_map_from_specifier(
&import_map_specifier,
self.get_maybe_config_file().as_ref(),
file_fetcher,
)
.await
.context(format!(
"Unable to load '{}' import map",
import_map_specifier
))
.map(Some)
}

/// Overrides the import map specifier to use.
Expand Down
33 changes: 12 additions & 21 deletions cli/file_fetcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,8 @@ impl FileFetcher {
http_client: HttpClient,
blob_store: BlobStore,
progress_bar: Option<ProgressBar>,
) -> Result<Self, AnyError> {
Ok(Self {
) -> Self {
Self {
auth_tokens: AuthTokens::new(env::var("DENO_AUTH_TOKENS").ok()),
allow_remote,
cache: Default::default(),
Expand All @@ -204,7 +204,7 @@ impl FileFetcher {
blob_store,
download_log_level: log::Level::Info,
progress_bar,
})
}
}

/// Sets the log level to use when outputting the download message.
Expand Down Expand Up @@ -778,8 +778,7 @@ mod tests {
HttpClient::new(None, None).unwrap(),
blob_store.clone(),
None,
)
.unwrap();
);
(file_fetcher, temp_dir, blob_store)
}

Expand Down Expand Up @@ -1215,8 +1214,7 @@ mod tests {
HttpClient::new(None, None).unwrap(),
BlobStore::default(),
None,
)
.unwrap();
);
let result = file_fetcher
.fetch(&specifier, PermissionsContainer::allow_all())
.await;
Expand All @@ -1241,8 +1239,7 @@ mod tests {
HttpClient::new(None, None).unwrap(),
BlobStore::default(),
None,
)
.unwrap();
);
let specifier =
resolve_url("http://localhost:4545/subdir/mismatch_ext.ts").unwrap();
let cache_filename = file_fetcher_01
Expand All @@ -1267,8 +1264,7 @@ mod tests {
HttpClient::new(None, None).unwrap(),
BlobStore::default(),
None,
)
.unwrap();
);
let result = file_fetcher_02
.fetch(&specifier, PermissionsContainer::allow_all())
.await;
Expand Down Expand Up @@ -1409,8 +1405,7 @@ mod tests {
HttpClient::new(None, None).unwrap(),
BlobStore::default(),
None,
)
.unwrap();
);
let specifier =
resolve_url("http://localhost:4548/subdir/mismatch_ext.ts").unwrap();
let redirected_specifier =
Expand Down Expand Up @@ -1438,8 +1433,7 @@ mod tests {
HttpClient::new(None, None).unwrap(),
BlobStore::default(),
None,
)
.unwrap();
);
let result = file_fetcher_02
.fetch(&redirected_specifier, PermissionsContainer::allow_all())
.await;
Expand Down Expand Up @@ -1538,8 +1532,7 @@ mod tests {
HttpClient::new(None, None).unwrap(),
BlobStore::default(),
None,
)
.unwrap();
);
let specifier =
resolve_url("http://localhost:4545/run/002_hello.ts").unwrap();

Expand All @@ -1564,17 +1557,15 @@ mod tests {
HttpClient::new(None, None).unwrap(),
BlobStore::default(),
None,
)
.unwrap();
);
let file_fetcher_02 = FileFetcher::new(
HttpCache::new(&location),
CacheSetting::Use,
true,
HttpClient::new(None, None).unwrap(),
BlobStore::default(),
None,
)
.unwrap();
);
let specifier =
resolve_url("http://localhost:4545/run/002_hello.ts").unwrap();

Expand Down
4 changes: 2 additions & 2 deletions cli/lsp/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ pub struct CacheMetadata {
}

impl CacheMetadata {
pub fn new(location: &Path) -> Self {
pub fn new(cache: HttpCache) -> Self {
Self {
cache: HttpCache::new(location),
cache,
metadata: Default::default(),
}
}
Expand Down
Loading

0 comments on commit 34c14db

Please sign in to comment.