Skip to content

Commit

Permalink
port ops to JSON: compiler, errors, fetch, files (denoland#2804)
Browse files Browse the repository at this point in the history
  • Loading branch information
bartlomieju authored and ry committed Aug 24, 2019
1 parent 5b2baa5 commit 79f82cf
Show file tree
Hide file tree
Showing 20 changed files with 322 additions and 639 deletions.
1 change: 0 additions & 1 deletion cli/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ mod http_body;
mod http_util;
mod import_map;
pub mod msg;
pub mod msg_util;
pub mod ops;
pub mod permissions;
mod progress;
Expand Down
75 changes: 0 additions & 75 deletions cli/msg.fbs
Original file line number Diff line number Diff line change
@@ -1,23 +1,14 @@
union Any {
Accept,
ApplySourceMap,
Cache,
Chdir,
Chmod,
Chown,
Close,
CopyFile,
CreateWorker,
CreateWorkerRes,
Cwd,
CwdRes,
Dial,
Fetch,
FetchSourceFile,
FetchSourceFileRes,
FetchRes,
FormatError,
FormatErrorRes,
GetRandomValues,
GlobalTimer,
GlobalTimerRes,
Expand All @@ -38,8 +29,6 @@ union Any {
NewConn,
Now,
NowRes,
Open,
OpenRes,
PermissionRevoke,
Permissions,
PermissionsRes,
Expand Down Expand Up @@ -215,33 +204,6 @@ table WorkerPostMessage {
// data passed thru the zero-copy data parameter.
}

table FetchSourceFile {
specifier: string;
referrer: string;
}

table FetchSourceFileRes {
// If it's a non-http module, moduleName and filename will be the same.
// For http modules, module_name is its resolved http URL, and filename
// is the location of the locally downloaded source code.
module_name: string;
filename: string;
media_type: MediaType;
data: [ubyte];
}

table ApplySourceMap {
filename: string;
line: int;
column: int;
}

table Cache {
extension: string;
module_id: string;
contents: string;
}

table Chdir {
directory: string;
}
Expand Down Expand Up @@ -274,29 +236,6 @@ table PermissionsRes {
hrtime: bool;
}

// Note this represents The WHOLE header of an http message, not just the key
// value pairs. That means it includes method and url for Requests and status
// for responses. This is why it is singular "Header" instead of "Headers".
table HttpHeader {
is_request: bool;
// Request only:
method: string;
url: string;
// Response only:
status: uint16;
// Both:
fields: [KeyValue];
}

table Fetch {
header: HttpHeader;
}

table FetchRes {
header: HttpHeader;
body_rid: uint32;
}

table MakeTempDir {
dir: string;
prefix: string;
Expand Down Expand Up @@ -416,16 +355,6 @@ table Truncate {
len: uint;
}

table Open {
filename: string;
perm: uint;
mode: string;
}

table OpenRes {
rid: uint32;
}

table Read {
rid: uint32;
// (ptr, len) is passed as second parameter to Deno.core.send().
Expand All @@ -444,10 +373,6 @@ table WriteRes {
nbyte: uint;
}

table Close {
rid: uint32;
}

table Kill {
pid: int32;
signo: int32;
Expand Down
124 changes: 0 additions & 124 deletions cli/msg_util.rs

This file was deleted.

90 changes: 36 additions & 54 deletions cli/ops/compiler.rs
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(),
})))
}
Loading

0 comments on commit 79f82cf

Please sign in to comment.