Skip to content

Commit

Permalink
split up ops.rs (denoland#2753)
Browse files Browse the repository at this point in the history
Note cli/dispatch_minimal.rs ops are not yet included in cli/ops.

This is part of work towards denoland#2730
  • Loading branch information
bartlomieju authored and ry committed Aug 14, 2019
1 parent 58f0e9b commit e6c349a
Show file tree
Hide file tree
Showing 18 changed files with 2,497 additions and 2,279 deletions.
2,279 changes: 0 additions & 2,279 deletions cli/ops.rs

This file was deleted.

88 changes: 88 additions & 0 deletions cli/ops/compiler.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
use crate::deno_error;
use crate::msg;
use crate::ops::empty_buf;
use crate::ops::ok_buf;
use crate::ops::serialize_response;
use crate::ops::CliOpResult;
use crate::state::ThreadSafeState;
use crate::tokio_util;
use deno::*;
use flatbuffers::FlatBufferBuilder;
use futures::Future;

pub fn op_cache(
state: &ThreadSafeState,
base: &msg::Base<'_>,
data: Option<PinnedBuf>,
) -> CliOpResult {
assert!(data.is_none());
let inner = base.inner_as_cache().unwrap();
let extension = inner.extension().unwrap();
// TODO: rename to something with 'url'
let module_id = inner.module_id().unwrap();
let contents = inner.contents().unwrap();

let module_specifier = ModuleSpecifier::resolve_url(module_id)
.expect("Should be valid module specifier");

state.ts_compiler.cache_compiler_output(
&module_specifier,
extension,
contents,
)?;

ok_buf(empty_buf())
}

pub fn op_fetch_source_file(
state: &ThreadSafeState,
base: &msg::Base<'_>,
data: Option<PinnedBuf>,
) -> CliOpResult {
if !base.sync() {
return Err(deno_error::no_async_support());
}
assert!(data.is_none());
let inner = base.inner_as_fetch_source_file().unwrap();
let cmd_id = base.cmd_id();
let specifier = inner.specifier().unwrap();
let referrer = inner.referrer().unwrap();

// TODO(ry) Maybe a security hole. Only the compiler worker should have access
// to this. Need a test to demonstrate the hole.
let is_dyn_import = false;

let resolved_specifier =
state.resolve(specifier, referrer, false, is_dyn_import)?;

let fut = state
.file_fetcher
.fetch_source_file_async(&resolved_specifier)
.and_then(move |out| {
let builder = &mut FlatBufferBuilder::new();
let data_off = builder.create_vector(out.source_code.as_slice());
let msg_args = msg::FetchSourceFileResArgs {
module_name: Some(builder.create_string(&out.url.to_string())),
filename: Some(builder.create_string(&out.filename.to_str().unwrap())),
media_type: out.media_type,
data: Some(data_off),
};
let inner = msg::FetchSourceFileRes::create(builder, &msg_args);
Ok(serialize_response(
cmd_id,
builder,
msg::BaseArgs {
inner: Some(inner.as_union_value()),
inner_type: msg::Any::FetchSourceFileRes,
..Default::default()
},
))
});

// WARNING: Here we use tokio_util::block_on() which starts a new Tokio
// runtime for executing the future. This is so we don't inadvernently run
// out of threads in the main runtime.
let result_buf = tokio_util::block_on(fut)?;
Ok(Op::Sync(result_buf))
}
89 changes: 89 additions & 0 deletions cli/ops/errors.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
use crate::deno_error;
use crate::fmt_errors::JSError;
use crate::msg;
use crate::ops::ok_buf;
use crate::ops::serialize_response;
use crate::ops::CliOpResult;
use crate::source_maps::get_orig_position;
use crate::source_maps::CachedMaps;
use crate::state::ThreadSafeState;
use deno::*;
use flatbuffers::FlatBufferBuilder;
use std::collections::HashMap;

pub fn op_format_error(
state: &ThreadSafeState,
base: &msg::Base<'_>,
data: Option<PinnedBuf>,
) -> CliOpResult {
assert!(data.is_none());
let inner = base.inner_as_format_error().unwrap();
let json_str = inner.error().unwrap();
let error = JSError::from_json(json_str, &state.ts_compiler);
let error_string = error.to_string();

let mut builder = FlatBufferBuilder::new();
let new_error = builder.create_string(&error_string);

let inner = msg::FormatErrorRes::create(
&mut builder,
&msg::FormatErrorResArgs {
error: Some(new_error),
},
);

let response_buf = serialize_response(
base.cmd_id(),
&mut builder,
msg::BaseArgs {
inner_type: msg::Any::FormatErrorRes,
inner: Some(inner.as_union_value()),
..Default::default()
},
);

ok_buf(response_buf)
}

pub fn op_apply_source_map(
state: &ThreadSafeState,
base: &msg::Base<'_>,
data: Option<PinnedBuf>,
) -> CliOpResult {
if !base.sync() {
return Err(deno_error::no_async_support());
}
assert!(data.is_none());
let inner = base.inner_as_apply_source_map().unwrap();
let cmd_id = base.cmd_id();
let filename = inner.filename().unwrap();
let line = inner.line();
let column = inner.column();

let mut mappings_map: CachedMaps = HashMap::new();
let (orig_filename, orig_line, orig_column) = get_orig_position(
filename.to_owned(),
line.into(),
column.into(),
&mut mappings_map,
&state.ts_compiler,
);

let builder = &mut FlatBufferBuilder::new();
let msg_args = msg::ApplySourceMapArgs {
filename: Some(builder.create_string(&orig_filename)),
line: orig_line as i32,
column: orig_column as i32,
};
let res_inner = msg::ApplySourceMap::create(builder, &msg_args);
ok_buf(serialize_response(
cmd_id,
builder,
msg::BaseArgs {
inner: Some(res_inner.as_union_value()),
inner_type: msg::Any::ApplySourceMap,
..Default::default()
},
))
}
73 changes: 73 additions & 0 deletions cli/ops/fetch.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
use crate::http_util;
use crate::msg;
use crate::msg_util;
use crate::ops::serialize_response;
use crate::ops::CliOpResult;
use crate::resources;
use crate::state::ThreadSafeState;
use deno::*;
use flatbuffers::FlatBufferBuilder;
use hyper;
use hyper::rt::Future;
use std;
use std::convert::From;

pub fn op_fetch(
state: &ThreadSafeState,
base: &msg::Base<'_>,
data: Option<PinnedBuf>,
) -> CliOpResult {
let inner = base.inner_as_fetch().unwrap();
let cmd_id = base.cmd_id();

let header = inner.header().unwrap();
assert!(header.is_request());
let url = header.url().unwrap();

let body = match data {
None => hyper::Body::empty(),
Some(buf) => hyper::Body::from(Vec::from(&*buf)),
};

let req = msg_util::deserialize_request(header, body)?;

let url_ = url::Url::parse(url).map_err(ErrBox::from)?;
state.check_net_url(&url_)?;

let client = http_util::get_client();

debug!("Before fetch {}", url);
let future = client
.request(req)
.map_err(ErrBox::from)
.and_then(move |res| {
let builder = &mut FlatBufferBuilder::new();
let header_off = msg_util::serialize_http_response(builder, &res);
let body = res.into_body();
let body_resource = resources::add_hyper_body(body);
let inner = msg::FetchRes::create(
builder,
&msg::FetchResArgs {
header: Some(header_off),
body_rid: body_resource.rid,
},
);

Ok(serialize_response(
cmd_id,
builder,
msg::BaseArgs {
inner: Some(inner.as_union_value()),
inner_type: msg::Any::FetchRes,
..Default::default()
},
))
});
if base.sync() {
let result_buf = future.wait()?;
Ok(Op::Sync(result_buf))
} else {
Ok(Op::Async(Box::new(future)))
}
}
Loading

0 comments on commit e6c349a

Please sign in to comment.