mirror of
https://github.com/denoland/deno.git
synced 2025-09-27 04:39:10 +00:00
Revert json ops (#2814)
* Revert "port more ops to JSON (#2809)" This reverts commit137f33733d
. * Revert "port ops to JSON: compiler, errors, fetch, files (#2804)" This reverts commit79f82cf10e
. * Revert "Port rest of os ops to JSON (#2802)" This reverts commit5b2baa5c99
.
This commit is contained in:
parent
bdc0a13261
commit
2235dd795d
45 changed files with 1968 additions and 1045 deletions
|
@ -1,68 +1,86 @@
|
|||
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
||||
use super::dispatch_json::{Deserialize, JsonOp, Value};
|
||||
use super::dispatch_flatbuffers::serialize_response;
|
||||
use super::utils::*;
|
||||
use crate::deno_error;
|
||||
use crate::msg;
|
||||
use crate::state::ThreadSafeState;
|
||||
use crate::tokio_util;
|
||||
use deno::*;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
struct CacheArgs {
|
||||
module_id: String,
|
||||
contents: String,
|
||||
extension: String,
|
||||
}
|
||||
use flatbuffers::FlatBufferBuilder;
|
||||
use futures::Future;
|
||||
|
||||
pub fn op_cache(
|
||||
state: &ThreadSafeState,
|
||||
args: Value,
|
||||
_zero_copy: Option<PinnedBuf>,
|
||||
) -> Result<JsonOp, ErrBox> {
|
||||
let args: CacheArgs = serde_json::from_value(args)?;
|
||||
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(&args.module_id)
|
||||
let module_specifier = ModuleSpecifier::resolve_url(module_id)
|
||||
.expect("Should be valid module specifier");
|
||||
|
||||
state.ts_compiler.cache_compiler_output(
|
||||
&module_specifier,
|
||||
&args.extension,
|
||||
&args.contents,
|
||||
extension,
|
||||
contents,
|
||||
)?;
|
||||
|
||||
Ok(JsonOp::Sync(json!({})))
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct FetchSourceFileArgs {
|
||||
specifier: String,
|
||||
referrer: String,
|
||||
ok_buf(empty_buf())
|
||||
}
|
||||
|
||||
pub fn op_fetch_source_file(
|
||||
state: &ThreadSafeState,
|
||||
args: Value,
|
||||
_zero_copy: Option<PinnedBuf>,
|
||||
) -> Result<JsonOp, ErrBox> {
|
||||
let args: FetchSourceFileArgs = serde_json::from_value(args)?;
|
||||
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(&args.specifier, &args.referrer, false, is_dyn_import)?;
|
||||
state.resolve(specifier, referrer, false, is_dyn_import)?;
|
||||
|
||||
let fut = state
|
||||
.file_fetcher
|
||||
.fetch_source_file_async(&resolved_specifier);
|
||||
.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 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(),
|
||||
})))
|
||||
let result_buf = tokio_util::block_on(fut)?;
|
||||
Ok(Op::Sync(result_buf))
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue