2020-01-02 15:13:47 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2020-01-05 11:56:18 -05:00
|
|
|
use deno_core::*;
|
2019-11-16 19:17:47 -05:00
|
|
|
use futures::future::FutureExt;
|
2019-08-23 01:30:14 -04:00
|
|
|
pub use serde_derive::Deserialize;
|
|
|
|
use serde_json::json;
|
|
|
|
pub use serde_json::Value;
|
2019-11-16 19:17:47 -05:00
|
|
|
use std::future::Future;
|
|
|
|
use std::pin::Pin;
|
2020-01-21 08:26:11 -05:00
|
|
|
use tokio::task;
|
2019-08-23 01:30:14 -04:00
|
|
|
|
2019-11-16 19:17:47 -05:00
|
|
|
pub type AsyncJsonOp =
|
|
|
|
Pin<Box<dyn Future<Output = Result<Value, ErrBox>> + Send>>;
|
2019-08-23 01:30:14 -04:00
|
|
|
|
|
|
|
pub enum JsonOp {
|
|
|
|
Sync(Value),
|
|
|
|
Async(AsyncJsonOp),
|
2020-01-21 12:01:10 -05:00
|
|
|
/// AsyncUnref is the variation of Async, which doesn't block the program
|
|
|
|
/// exiting.
|
|
|
|
AsyncUnref(AsyncJsonOp),
|
2019-08-23 01:30:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
fn json_err(err: ErrBox) -> Value {
|
|
|
|
use crate::deno_error::GetErrorKind;
|
|
|
|
json!({
|
|
|
|
"message": err.to_string(),
|
|
|
|
"kind": err.kind() as u32,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn serialize_result(
|
|
|
|
promise_id: Option<u64>,
|
|
|
|
result: Result<Value, ErrBox>,
|
|
|
|
) -> Buf {
|
|
|
|
let value = match result {
|
|
|
|
Ok(v) => json!({ "ok": v, "promiseId": promise_id }),
|
|
|
|
Err(err) => json!({ "err": json_err(err), "promiseId": promise_id }),
|
|
|
|
};
|
2019-08-26 07:48:40 -04:00
|
|
|
let mut vec = serde_json::to_vec(&value).unwrap();
|
|
|
|
debug!("JSON response pre-align, len={}", vec.len());
|
|
|
|
// Align to 32bit word, padding with the space character.
|
|
|
|
vec.resize((vec.len() + 3usize) & !3usize, b' ');
|
2019-08-23 01:30:14 -04:00
|
|
|
vec.into_boxed_slice()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
struct AsyncArgs {
|
|
|
|
promise_id: Option<u64>,
|
|
|
|
}
|
|
|
|
|
2020-01-24 15:10:49 -05:00
|
|
|
pub fn json_op<D>(d: D) -> impl Fn(&[u8], Option<ZeroCopyBuf>) -> CoreOp
|
2019-10-01 18:51:05 -04:00
|
|
|
where
|
2020-01-24 15:10:49 -05:00
|
|
|
D: Fn(Value, Option<ZeroCopyBuf>) -> Result<JsonOp, ErrBox>,
|
2019-10-01 18:51:05 -04:00
|
|
|
{
|
2020-01-24 15:10:49 -05:00
|
|
|
move |control: &[u8], zero_copy: Option<ZeroCopyBuf>| {
|
2019-10-25 13:23:16 -04:00
|
|
|
let async_args: AsyncArgs = match serde_json::from_slice(control) {
|
|
|
|
Ok(args) => args,
|
|
|
|
Err(e) => {
|
|
|
|
let buf = serialize_result(None, Err(ErrBox::from(e)));
|
|
|
|
return CoreOp::Sync(buf);
|
|
|
|
}
|
|
|
|
};
|
2019-10-01 18:51:05 -04:00
|
|
|
let promise_id = async_args.promise_id;
|
|
|
|
let is_sync = promise_id.is_none();
|
2019-08-23 01:30:14 -04:00
|
|
|
|
2019-10-01 18:51:05 -04:00
|
|
|
let result = serde_json::from_slice(control)
|
|
|
|
.map_err(ErrBox::from)
|
|
|
|
.and_then(|args| d(args, zero_copy));
|
|
|
|
|
|
|
|
// Convert to CoreOp
|
|
|
|
match result {
|
|
|
|
Ok(JsonOp::Sync(sync_value)) => {
|
|
|
|
assert!(promise_id.is_none());
|
|
|
|
CoreOp::Sync(serialize_result(promise_id, Ok(sync_value)))
|
|
|
|
}
|
|
|
|
Ok(JsonOp::Async(fut)) => {
|
|
|
|
assert!(promise_id.is_some());
|
2019-11-16 19:17:47 -05:00
|
|
|
let fut2 = fut.then(move |result| {
|
|
|
|
futures::future::ok(serialize_result(promise_id, result))
|
|
|
|
});
|
|
|
|
CoreOp::Async(fut2.boxed())
|
2019-10-01 18:51:05 -04:00
|
|
|
}
|
2020-01-21 12:01:10 -05:00
|
|
|
Ok(JsonOp::AsyncUnref(fut)) => {
|
|
|
|
assert!(promise_id.is_some());
|
|
|
|
let fut2 = fut.then(move |result| {
|
|
|
|
futures::future::ok(serialize_result(promise_id, result))
|
|
|
|
});
|
|
|
|
CoreOp::AsyncUnref(fut2.boxed())
|
|
|
|
}
|
2019-10-01 18:51:05 -04:00
|
|
|
Err(sync_err) => {
|
|
|
|
let buf = serialize_result(promise_id, Err(sync_err));
|
|
|
|
if is_sync {
|
|
|
|
CoreOp::Sync(buf)
|
|
|
|
} else {
|
2019-11-16 19:17:47 -05:00
|
|
|
CoreOp::Async(futures::future::ok(buf).boxed())
|
2019-10-01 18:51:05 -04:00
|
|
|
}
|
2019-08-23 01:30:14 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn blocking_json<F>(is_sync: bool, f: F) -> Result<JsonOp, ErrBox>
|
|
|
|
where
|
2019-11-16 19:17:47 -05:00
|
|
|
F: 'static + Send + FnOnce() -> Result<Value, ErrBox> + Unpin,
|
2019-08-23 01:30:14 -04:00
|
|
|
{
|
|
|
|
if is_sync {
|
|
|
|
Ok(JsonOp::Sync(f()?))
|
|
|
|
} else {
|
2020-01-21 08:26:11 -05:00
|
|
|
let fut = async move {
|
|
|
|
task::spawn_blocking(move || f())
|
|
|
|
.await
|
|
|
|
.map_err(ErrBox::from)?
|
|
|
|
}
|
|
|
|
.boxed();
|
|
|
|
Ok(JsonOp::Async(fut.boxed()))
|
2019-08-23 01:30:14 -04:00
|
|
|
}
|
|
|
|
}
|