cli: refactor deno_dir to use Url instead of String

This commit is contained in:
Bartek Iwańczuk 2019-06-25 22:14:36 +02:00 committed by Bert Belder
parent 9b1997b8b6
commit 72d9045528
No known key found for this signature in database
GPG key ID: 7A77887B2E2ED461
8 changed files with 347 additions and 479 deletions

View file

@ -9,6 +9,7 @@ use crate::state::*;
use crate::tokio_util;
use crate::worker::Worker;
use deno::Buf;
use deno::ModuleSpecifier;
use futures::Future;
use futures::Stream;
use std::path::PathBuf;
@ -17,6 +18,7 @@ use std::sync::atomic::Ordering;
// This corresponds to JS ModuleMetaData.
// TODO Rename one or the other so they correspond.
// TODO(bartlomieju): change `*_name` to `*_url` and use Url type
#[derive(Debug, Clone)]
pub struct ModuleMetaData {
pub module_name: String,
@ -203,6 +205,8 @@ pub fn compile_async(
.and_then(move |maybe_msg: Option<Buf>| {
debug!("Received message from worker");
// TODO: here TS compiler emitted the files to disc and we should signal ModuleMetaData
// cache that source code is available
if let Some(msg) = maybe_msg {
let json_str = std::str::from_utf8(&msg).unwrap();
debug!("Message: {}", json_str);
@ -213,8 +217,10 @@ pub fn compile_async(
Ok(())
}).and_then(move |_| {
let module_specifier = ModuleSpecifier::resolve_url(&module_name)
.expect("Should be valid module specifier");
state.dir.fetch_module_meta_data_async(
&module_name,
&module_specifier,
true,
true,
).map_err(|e| {

File diff suppressed because it is too large Load diff

View file

@ -1,4 +1,5 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
use crate::deno_dir;
use crate::diagnostics;
use crate::fmt_errors::JSErrorColor;
use crate::import_map;
@ -34,6 +35,7 @@ enum Repr {
ModuleResolutionErr(ModuleResolutionError),
Diagnostic(diagnostics::Diagnostic),
JSError(JSError),
DenoDirErr(deno_dir::DenoDirError),
}
/// Create a new simple DenoError.
@ -116,6 +118,7 @@ impl DenoError {
}
Repr::Diagnostic(ref _err) => ErrorKind::Diagnostic,
Repr::JSError(ref _err) => ErrorKind::JSError,
Repr::DenoDirErr(ref _err) => ErrorKind::DenoDirError,
}
}
@ -141,6 +144,7 @@ impl fmt::Display for DenoError {
Repr::ModuleResolutionErr(ref err) => err.fmt(f),
Repr::Diagnostic(ref err) => err.fmt(f),
Repr::JSError(ref err) => JSErrorColor(err).fmt(f),
Repr::DenoDirErr(ref err) => err.fmt(f),
}
}
}
@ -156,6 +160,7 @@ impl std::error::Error for DenoError {
Repr::ModuleResolutionErr(ref err) => err.description(),
Repr::Diagnostic(ref err) => &err.items[0].message,
Repr::JSError(ref err) => &err.description(),
Repr::DenoDirErr(ref err) => err.description(),
}
}
@ -169,6 +174,7 @@ impl std::error::Error for DenoError {
Repr::ModuleResolutionErr(ref err) => err.source(),
Repr::Diagnostic(ref _err) => None,
Repr::JSError(ref err) => Some(err),
Repr::DenoDirErr(ref err) => Some(err),
}
}
}
@ -256,6 +262,14 @@ impl From<import_map::ImportMapError> for DenoError {
}
}
impl From<deno_dir::DenoDirError> for DenoError {
fn from(err: deno_dir::DenoDirError) -> Self {
Self {
repr: Repr::DenoDirErr(err),
}
}
}
impl From<ModuleResolutionError> for DenoError {
fn from(err: ModuleResolutionError) -> Self {
Self {
@ -334,6 +348,8 @@ pub fn err_check<R>(r: Result<R, DenoError>) {
mod tests {
use super::*;
use crate::ansi::strip_ansi_codes;
use crate::deno_dir::DenoDirError;
use crate::deno_dir::DenoDirErrorKind;
use crate::diagnostics::Diagnostic;
use crate::diagnostics::DiagnosticCategory;
use crate::diagnostics::DiagnosticItem;
@ -450,6 +466,13 @@ mod tests {
}
}
fn deno_dir_error() -> DenoDirError {
DenoDirError::new(
"a deno dir error".to_string(),
DenoDirErrorKind::UnsupportedFetchScheme,
)
}
#[test]
fn test_simple_error() {
let err = new(ErrorKind::NoError, "foo".to_string());
@ -494,6 +517,13 @@ mod tests {
assert_eq!(err.to_string(), "an import map error");
}
#[test]
fn test_deno_dir_error() {
let err = DenoError::from(deno_dir_error());
assert_eq!(err.kind(), ErrorKind::DenoDirError);
assert_eq!(err.to_string(), "a deno dir error\n");
}
#[test]
fn test_bad_resource() {
let err = bad_resource();

View file

@ -143,6 +143,7 @@ enum ErrorKind: byte {
NoSyncSupport,
ImportMapError,
ImportPrefixMissing,
DenoDirError,
// other kinds
Diagnostic,

View file

@ -481,8 +481,12 @@ fn op_cache(
// cache path. In the future, checksums will not be used in the cache
// filenames and this requirement can be removed. See
// https://github.com/denoland/deno/issues/2057
let module_specifier = ModuleSpecifier::resolve_url(module_id)
.expect("Should be valid module specifier");
let module_meta_data =
state.dir.fetch_module_meta_data(module_id, true, true)?;
state
.dir
.fetch_module_meta_data(&module_specifier, true, true)?;
let (js_cache_path, source_map_path) = state.dir.cache_path(
&PathBuf::from(&module_meta_data.filename),
@ -525,11 +529,8 @@ fn op_fetch_module_meta_data(
let fut = state
.dir
.fetch_module_meta_data_async(
&resolved_specifier.to_string(),
use_cache,
no_fetch,
).and_then(move |out| {
.fetch_module_meta_data_async(&resolved_specifier, use_cache, no_fetch)
.and_then(move |out| {
let builder = &mut FlatBufferBuilder::new();
let data_off = builder.create_vector(out.source_code.as_slice());
let msg_args = msg::FetchModuleMetaDataResArgs {

View file

@ -126,11 +126,8 @@ pub fn fetch_module_meta_data_and_maybe_compile_async(
state_
.dir
.fetch_module_meta_data_async(
&module_specifier.to_string(),
use_cache,
no_fetch,
).and_then(move |out| {
.fetch_module_meta_data_async(&module_specifier, use_cache, no_fetch)
.and_then(move |out| {
if out.media_type == msg::MediaType::TypeScript
&& !out.has_output_code_and_source_map()
{

View file

@ -179,6 +179,41 @@ mod tests {
#[test]
fn test_resolve_import() {
let tests = vec![
(
"./005_more_imports.ts",
"http://deno.land/core/tests/006_url_imports.ts",
"http://deno.land/core/tests/005_more_imports.ts",
),
(
"../005_more_imports.ts",
"http://deno.land/core/tests/006_url_imports.ts",
"http://deno.land/core/005_more_imports.ts",
),
(
"http://deno.land/core/tests/005_more_imports.ts",
"http://deno.land/core/tests/006_url_imports.ts",
"http://deno.land/core/tests/005_more_imports.ts",
),
(
"data:text/javascript,export default 'grapes';",
"http://deno.land/core/tests/006_url_imports.ts",
"data:text/javascript,export default 'grapes';",
),
(
"blob:https://whatwg.org/d0360e2f-caee-469f-9a2f-87d5b0456f6f",
"http://deno.land/core/tests/006_url_imports.ts",
"blob:https://whatwg.org/d0360e2f-caee-469f-9a2f-87d5b0456f6f",
),
(
"javascript:export default 'artichokes';",
"http://deno.land/core/tests/006_url_imports.ts",
"javascript:export default 'artichokes';",
),
(
"data:text/plain,export default 'kale';",
"http://deno.land/core/tests/006_url_imports.ts",
"data:text/plain,export default 'kale';",
),
(
"/dev/core/tests/005_more_imports.ts",
"file:///home/yeti",
@ -222,6 +257,31 @@ mod tests {
use ModuleResolutionError::*;
let tests = vec![
(
"005_more_imports.ts",
"http://deno.land/core/tests/006_url_imports.ts",
ImportPrefixMissing,
),
(
".tomato",
"http://deno.land/core/tests/006_url_imports.ts",
ImportPrefixMissing,
),
(
"..zucchini.mjs",
"http://deno.land/core/tests/006_url_imports.ts",
ImportPrefixMissing,
),
(
r".\yam.es",
"http://deno.land/core/tests/006_url_imports.ts",
ImportPrefixMissing,
),
(
r"..\yam.es",
"http://deno.land/core/tests/006_url_imports.ts",
ImportPrefixMissing,
),
(
"https://eggplant:b/c",
"http://deno.land/core/tests/006_url_imports.ts",
@ -248,7 +308,16 @@ mod tests {
#[test]
fn test_resolve_url_or_path() {
// Absolute URL.
let mut tests: Vec<(&str, String)> = vec![];
let mut tests: Vec<(&str, String)> = vec![
(
"http://deno.land/core/tests/006_url_imports.ts",
"http://deno.land/core/tests/006_url_imports.ts".to_string(),
),
(
"https://deno.land/core/tests/006_url_imports.ts",
"https://deno.land/core/tests/006_url_imports.ts".to_string(),
),
];
// The local path tests assume that the cwd is the deno repo root.
let cwd = current_dir().unwrap();

View file

@ -7,5 +7,6 @@
at fetchModuleMetaData (js/compiler.ts:[WILDCARD])
at _resolveModule (js/compiler.ts:[WILDCARD])
at js/compiler.ts:[WILDCARD]
at resolveModuleNames (js/compiler.ts:[WILDCARD])
at resolveModuleNamesWorker ([WILDCARD])
at resolveModuleNamesReusingOldState ([WILDCARD]typescript.js:[WILDCARD])