forked from denoland/deno
-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
port ops to JSON: compiler, errors, fetch, files (denoland#2804)
- Loading branch information
1 parent
5b2baa5
commit 79f82cf
Showing
20 changed files
with
322 additions
and
639 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(), | ||
}))) | ||
} |
Oops, something went wrong.