diff options
author | Bartek IwaĆczuk <biwanczuk@gmail.com> | 2019-08-26 14:50:21 +0200 |
---|---|---|
committer | Ryan Dahl <ry@tinyclouds.org> | 2019-08-26 08:50:21 -0400 |
commit | 520f9631e09aa720fd8c03513ee8ea967f5ed4b2 (patch) | |
tree | fc3d1bd5182452ca1865a5c2631355e0895af94c /cli/ops/compiler.rs | |
parent | 017f88ee99b0fe40221e6af92e0b6a976fbaf2ad (diff) |
bring back json ops (#2815)
Diffstat (limited to 'cli/ops/compiler.rs')
-rw-r--r-- | cli/ops/compiler.rs | 90 |
1 files changed, 36 insertions, 54 deletions
diff --git a/cli/ops/compiler.rs b/cli/ops/compiler.rs index efdcd2c9b..39d3a6d7f 100644 --- a/cli/ops/compiler.rs +++ b/cli/ops/compiler.rs @@ -1,86 +1,68 @@ // Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. -use super::dispatch_flatbuffers::serialize_response; -use super::utils::*; -use crate::deno_error; -use crate::msg; +use super::dispatch_json::{Deserialize, JsonOp, Value}; use crate::state::ThreadSafeState; use crate::tokio_util; use deno::*; -use flatbuffers::FlatBufferBuilder; -use futures::Future; + +#[derive(Deserialize)] +#[serde(rename_all = "camelCase")] +struct CacheArgs { + module_id: String, + contents: String, + extension: String, +} 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(); + args: Value, + _zero_copy: Option<PinnedBuf>, +) -> Result<JsonOp, ErrBox> { + let args: CacheArgs = serde_json::from_value(args)?; - let module_specifier = ModuleSpecifier::resolve_url(module_id) + let module_specifier = ModuleSpecifier::resolve_url(&args.module_id) .expect("Should be valid module specifier"); state.ts_compiler.cache_compiler_output( &module_specifier, - extension, - contents, + &args.extension, + &args.contents, )?; - ok_buf(empty_buf()) + Ok(JsonOp::Sync(json!({}))) +} + +#[derive(Deserialize)] +struct FetchSourceFileArgs { + specifier: String, + referrer: String, } 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(); + args: Value, + _zero_copy: Option<PinnedBuf>, +) -> Result<JsonOp, ErrBox> { + let args: FetchSourceFileArgs = serde_json::from_value(args)?; // 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)?; + state.resolve(&args.specifier, &args.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() - }, - )) - }); + .fetch_source_file_async(&resolved_specifier); // 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)) + let out = tokio_util::block_on(fut)?; + Ok(JsonOp::Sync(json!({ + "moduleName": out.url.to_string(), + "filename": out.filename.to_str().unwrap(), + "mediaType": out.media_type as i32, + "sourceCode": String::from_utf8(out.source_code).unwrap(), + }))) } |