mirror of
https://github.com/denoland/deno.git
synced 2025-08-04 10:59:13 +00:00
refactor: use the 'anyhow' crate instead of 'ErrBox' (#7476)
This commit is contained in:
parent
3da20d19a1
commit
f5b40c918c
63 changed files with 898 additions and 861 deletions
|
@ -1,7 +1,7 @@
|
|||
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
||||
|
||||
use deno_core::error::AnyError;
|
||||
use deno_core::BufVec;
|
||||
use deno_core::ErrBox;
|
||||
use deno_core::Op;
|
||||
use deno_core::OpFn;
|
||||
use deno_core::OpState;
|
||||
|
@ -15,8 +15,8 @@ use std::rc::Rc;
|
|||
use std::slice;
|
||||
|
||||
pub enum MinimalOp {
|
||||
Sync(Result<i32, ErrBox>),
|
||||
Async(Pin<Box<dyn Future<Output = Result<i32, ErrBox>>>>),
|
||||
Sync(Result<i32, AnyError>),
|
||||
Async(Pin<Box<dyn Future<Output = Result<i32, AnyError>>>>),
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug, PartialEq)]
|
||||
|
@ -144,14 +144,14 @@ where
|
|||
let mut record = match parse_min_record(&record_buf) {
|
||||
Some(r) => r,
|
||||
None => {
|
||||
let error = ErrBox::type_error("Unparsable control buffer");
|
||||
let error_class = (state.borrow().get_error_class_fn)(&error);
|
||||
let error_class = b"TypeError";
|
||||
let error_message = b"Unparsable control buffer";
|
||||
let error_record = ErrorRecord {
|
||||
promise_id: 0,
|
||||
arg: -1,
|
||||
error_len: error_class.len() as i32,
|
||||
error_class: error_class.as_bytes(),
|
||||
error_message: error.to_string().as_bytes().to_owned(),
|
||||
error_class,
|
||||
error_message: error_message[..].to_owned(),
|
||||
};
|
||||
return Op::Sync(error_record.into());
|
||||
}
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
use crate::diagnostics::Diagnostics;
|
||||
use crate::source_maps::get_orig_position;
|
||||
use crate::source_maps::CachedMaps;
|
||||
use deno_core::ErrBox;
|
||||
use deno_core::error::AnyError;
|
||||
use deno_core::OpState;
|
||||
use deno_core::ZeroCopyBuf;
|
||||
use serde_derive::Deserialize;
|
||||
|
@ -27,7 +27,7 @@ fn op_apply_source_map(
|
|||
state: &mut OpState,
|
||||
args: Value,
|
||||
_zero_copy: &mut [ZeroCopyBuf],
|
||||
) -> Result<Value, ErrBox> {
|
||||
) -> Result<Value, AnyError> {
|
||||
let args: ApplySourceMap = serde_json::from_value(args)?;
|
||||
|
||||
let mut mappings_map: CachedMaps = HashMap::new();
|
||||
|
@ -51,7 +51,7 @@ fn op_format_diagnostic(
|
|||
_state: &mut OpState,
|
||||
args: Value,
|
||||
_zero_copy: &mut [ZeroCopyBuf],
|
||||
) -> Result<Value, ErrBox> {
|
||||
) -> Result<Value, AnyError> {
|
||||
let diagnostic: Diagnostics = serde_json::from_value(args)?;
|
||||
Ok(json!(diagnostic.to_string()))
|
||||
}
|
||||
|
|
|
@ -1,9 +1,13 @@
|
|||
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
||||
|
||||
use super::io::{StreamResource, StreamResourceHolder};
|
||||
use crate::http_util::{create_http_client, HttpBody};
|
||||
use super::io::StreamResource;
|
||||
use super::io::StreamResourceHolder;
|
||||
use crate::http_util::create_http_client;
|
||||
use crate::http_util::HttpBody;
|
||||
use deno_core::error::bad_resource_id;
|
||||
use deno_core::error::type_error;
|
||||
use deno_core::error::AnyError;
|
||||
use deno_core::BufVec;
|
||||
use deno_core::ErrBox;
|
||||
use deno_core::OpState;
|
||||
use deno_core::ZeroCopyBuf;
|
||||
use http::header::HeaderName;
|
||||
|
@ -35,7 +39,7 @@ async fn op_fetch(
|
|||
state: Rc<RefCell<OpState>>,
|
||||
args: Value,
|
||||
data: BufVec,
|
||||
) -> Result<Value, ErrBox> {
|
||||
) -> Result<Value, AnyError> {
|
||||
let args: FetchArgs = serde_json::from_value(args)?;
|
||||
let url = args.url;
|
||||
|
||||
|
@ -44,7 +48,7 @@ async fn op_fetch(
|
|||
let r = state
|
||||
.resource_table
|
||||
.get::<HttpClientResource>(rid)
|
||||
.ok_or_else(ErrBox::bad_resource_id)?;
|
||||
.ok_or_else(bad_resource_id)?;
|
||||
r.client.clone()
|
||||
} else {
|
||||
let cli_state = super::cli_state2(&state);
|
||||
|
@ -62,10 +66,7 @@ async fn op_fetch(
|
|||
// Check scheme before asking for net permission
|
||||
let scheme = url_.scheme();
|
||||
if scheme != "http" && scheme != "https" {
|
||||
return Err(ErrBox::type_error(format!(
|
||||
"scheme '{}' not supported",
|
||||
scheme
|
||||
)));
|
||||
return Err(type_error(format!("scheme '{}' not supported", scheme)));
|
||||
}
|
||||
|
||||
super::cli_state2(&state).check_net_url(&url_)?;
|
||||
|
@ -133,7 +134,7 @@ fn op_create_http_client(
|
|||
state: &mut OpState,
|
||||
args: Value,
|
||||
_zero_copy: &mut [ZeroCopyBuf],
|
||||
) -> Result<Value, ErrBox> {
|
||||
) -> Result<Value, AnyError> {
|
||||
let args: CreateHttpClientOptions = serde_json::from_value(args)?;
|
||||
|
||||
if let Some(ca_file) = args.ca_file.clone() {
|
||||
|
|
203
cli/ops/fs.rs
203
cli/ops/fs.rs
|
@ -2,8 +2,10 @@
|
|||
// Some deserializer fields are only used on Unix and Windows build fails without it
|
||||
use super::io::std_file_resource;
|
||||
use super::io::{FileMetadata, StreamResource, StreamResourceHolder};
|
||||
use deno_core::error::custom_error;
|
||||
use deno_core::error::type_error;
|
||||
use deno_core::error::AnyError;
|
||||
use deno_core::BufVec;
|
||||
use deno_core::ErrBox;
|
||||
use deno_core::OpState;
|
||||
use deno_core::ZeroCopyBuf;
|
||||
use rand::thread_rng;
|
||||
|
@ -20,6 +22,11 @@ use std::rc::Rc;
|
|||
use std::time::SystemTime;
|
||||
use std::time::UNIX_EPOCH;
|
||||
|
||||
#[cfg(not(unix))]
|
||||
use deno_core::error::generic_error;
|
||||
#[cfg(not(unix))]
|
||||
use deno_core::error::not_supported;
|
||||
|
||||
pub fn init(rt: &mut deno_core::JsRuntime) {
|
||||
super::reg_json_sync(rt, "op_open_sync", op_open_sync);
|
||||
super::reg_json_async(rt, "op_open_async", op_open_async);
|
||||
|
@ -96,10 +103,10 @@ pub fn init(rt: &mut deno_core::JsRuntime) {
|
|||
super::reg_json_async(rt, "op_utime_async", op_utime_async);
|
||||
}
|
||||
|
||||
fn into_string(s: std::ffi::OsString) -> Result<String, ErrBox> {
|
||||
fn into_string(s: std::ffi::OsString) -> Result<String, AnyError> {
|
||||
s.into_string().map_err(|s| {
|
||||
let message = format!("File name or path {:?} is not valid UTF-8", s);
|
||||
ErrBox::new("InvalidData", message)
|
||||
custom_error("InvalidData", message)
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -126,7 +133,7 @@ struct OpenOptions {
|
|||
fn open_helper(
|
||||
state: &mut OpState,
|
||||
args: Value,
|
||||
) -> Result<(PathBuf, std::fs::OpenOptions), ErrBox> {
|
||||
) -> Result<(PathBuf, std::fs::OpenOptions), AnyError> {
|
||||
let args: OpenArgs = serde_json::from_value(args)?;
|
||||
let path = Path::new(&args.path).to_path_buf();
|
||||
|
||||
|
@ -170,7 +177,7 @@ fn op_open_sync(
|
|||
state: &mut OpState,
|
||||
args: Value,
|
||||
_zero_copy: &mut [ZeroCopyBuf],
|
||||
) -> Result<Value, ErrBox> {
|
||||
) -> Result<Value, AnyError> {
|
||||
let (path, open_options) = open_helper(state, args)?;
|
||||
let std_file = open_options.open(path)?;
|
||||
let tokio_file = tokio::fs::File::from_std(std_file);
|
||||
|
@ -188,7 +195,7 @@ async fn op_open_async(
|
|||
state: Rc<RefCell<OpState>>,
|
||||
args: Value,
|
||||
_zero_copy: BufVec,
|
||||
) -> Result<Value, ErrBox> {
|
||||
) -> Result<Value, AnyError> {
|
||||
let (path, open_options) = open_helper(&mut state.borrow_mut(), args)?;
|
||||
let tokio_file = tokio::fs::OpenOptions::from(open_options)
|
||||
.open(path)
|
||||
|
@ -211,7 +218,7 @@ struct SeekArgs {
|
|||
whence: i32,
|
||||
}
|
||||
|
||||
fn seek_helper(args: Value) -> Result<(u32, SeekFrom), ErrBox> {
|
||||
fn seek_helper(args: Value) -> Result<(u32, SeekFrom), AnyError> {
|
||||
let args: SeekArgs = serde_json::from_value(args)?;
|
||||
let rid = args.rid as u32;
|
||||
let offset = args.offset;
|
||||
|
@ -222,7 +229,7 @@ fn seek_helper(args: Value) -> Result<(u32, SeekFrom), ErrBox> {
|
|||
1 => SeekFrom::Current(offset),
|
||||
2 => SeekFrom::End(offset),
|
||||
_ => {
|
||||
return Err(ErrBox::type_error(format!("Invalid seek mode: {}", whence)));
|
||||
return Err(type_error(format!("Invalid seek mode: {}", whence)));
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -233,11 +240,11 @@ fn op_seek_sync(
|
|||
state: &mut OpState,
|
||||
args: Value,
|
||||
_zero_copy: &mut [ZeroCopyBuf],
|
||||
) -> Result<Value, ErrBox> {
|
||||
) -> Result<Value, AnyError> {
|
||||
let (rid, seek_from) = seek_helper(args)?;
|
||||
let pos = std_file_resource(state, rid, |r| match r {
|
||||
Ok(std_file) => std_file.seek(seek_from).map_err(ErrBox::from),
|
||||
Err(_) => Err(ErrBox::type_error(
|
||||
Ok(std_file) => std_file.seek(seek_from).map_err(AnyError::from),
|
||||
Err(_) => Err(type_error(
|
||||
"cannot seek on this type of resource".to_string(),
|
||||
)),
|
||||
})?;
|
||||
|
@ -248,13 +255,13 @@ async fn op_seek_async(
|
|||
state: Rc<RefCell<OpState>>,
|
||||
args: Value,
|
||||
_zero_copy: BufVec,
|
||||
) -> Result<Value, ErrBox> {
|
||||
) -> Result<Value, AnyError> {
|
||||
let (rid, seek_from) = seek_helper(args)?;
|
||||
// TODO(ry) This is a fake async op. We need to use poll_fn,
|
||||
// tokio::fs::File::start_seek and tokio::fs::File::poll_complete
|
||||
let pos = std_file_resource(&mut state.borrow_mut(), rid, |r| match r {
|
||||
Ok(std_file) => std_file.seek(seek_from).map_err(ErrBox::from),
|
||||
Err(_) => Err(ErrBox::type_error(
|
||||
Ok(std_file) => std_file.seek(seek_from).map_err(AnyError::from),
|
||||
Err(_) => Err(type_error(
|
||||
"cannot seek on this type of resource".to_string(),
|
||||
)),
|
||||
})?;
|
||||
|
@ -271,7 +278,7 @@ fn op_fdatasync_sync(
|
|||
state: &mut OpState,
|
||||
args: Value,
|
||||
_zero_copy: &mut [ZeroCopyBuf],
|
||||
) -> Result<Value, ErrBox> {
|
||||
) -> Result<Value, AnyError> {
|
||||
{
|
||||
let cli_state = super::cli_state(state);
|
||||
cli_state.check_unstable("Deno.fdatasync");
|
||||
|
@ -279,10 +286,8 @@ fn op_fdatasync_sync(
|
|||
let args: FdatasyncArgs = serde_json::from_value(args)?;
|
||||
let rid = args.rid as u32;
|
||||
std_file_resource(state, rid, |r| match r {
|
||||
Ok(std_file) => std_file.sync_data().map_err(ErrBox::from),
|
||||
Err(_) => Err(ErrBox::type_error(
|
||||
"cannot sync this type of resource".to_string(),
|
||||
)),
|
||||
Ok(std_file) => std_file.sync_data().map_err(AnyError::from),
|
||||
Err(_) => Err(type_error("cannot sync this type of resource".to_string())),
|
||||
})?;
|
||||
Ok(json!({}))
|
||||
}
|
||||
|
@ -291,16 +296,14 @@ async fn op_fdatasync_async(
|
|||
state: Rc<RefCell<OpState>>,
|
||||
args: Value,
|
||||
_zero_copy: BufVec,
|
||||
) -> Result<Value, ErrBox> {
|
||||
) -> Result<Value, AnyError> {
|
||||
super::cli_state2(&state).check_unstable("Deno.fdatasync");
|
||||
|
||||
let args: FdatasyncArgs = serde_json::from_value(args)?;
|
||||
let rid = args.rid as u32;
|
||||
std_file_resource(&mut state.borrow_mut(), rid, |r| match r {
|
||||
Ok(std_file) => std_file.sync_data().map_err(ErrBox::from),
|
||||
Err(_) => Err(ErrBox::type_error(
|
||||
"cannot sync this type of resource".to_string(),
|
||||
)),
|
||||
Ok(std_file) => std_file.sync_data().map_err(AnyError::from),
|
||||
Err(_) => Err(type_error("cannot sync this type of resource".to_string())),
|
||||
})?;
|
||||
Ok(json!({}))
|
||||
}
|
||||
|
@ -315,7 +318,7 @@ fn op_fsync_sync(
|
|||
state: &mut OpState,
|
||||
args: Value,
|
||||
_zero_copy: &mut [ZeroCopyBuf],
|
||||
) -> Result<Value, ErrBox> {
|
||||
) -> Result<Value, AnyError> {
|
||||
{
|
||||
let cli_state = super::cli_state(state);
|
||||
cli_state.check_unstable("Deno.fsync");
|
||||
|
@ -323,10 +326,8 @@ fn op_fsync_sync(
|
|||
let args: FsyncArgs = serde_json::from_value(args)?;
|
||||
let rid = args.rid as u32;
|
||||
std_file_resource(state, rid, |r| match r {
|
||||
Ok(std_file) => std_file.sync_all().map_err(ErrBox::from),
|
||||
Err(_) => Err(ErrBox::type_error(
|
||||
"cannot sync this type of resource".to_string(),
|
||||
)),
|
||||
Ok(std_file) => std_file.sync_all().map_err(AnyError::from),
|
||||
Err(_) => Err(type_error("cannot sync this type of resource".to_string())),
|
||||
})?;
|
||||
Ok(json!({}))
|
||||
}
|
||||
|
@ -335,16 +336,14 @@ async fn op_fsync_async(
|
|||
state: Rc<RefCell<OpState>>,
|
||||
args: Value,
|
||||
_zero_copy: BufVec,
|
||||
) -> Result<Value, ErrBox> {
|
||||
) -> Result<Value, AnyError> {
|
||||
super::cli_state2(&state).check_unstable("Deno.fsync");
|
||||
|
||||
let args: FsyncArgs = serde_json::from_value(args)?;
|
||||
let rid = args.rid as u32;
|
||||
std_file_resource(&mut state.borrow_mut(), rid, |r| match r {
|
||||
Ok(std_file) => std_file.sync_all().map_err(ErrBox::from),
|
||||
Err(_) => Err(ErrBox::type_error(
|
||||
"cannot sync this type of resource".to_string(),
|
||||
)),
|
||||
Ok(std_file) => std_file.sync_all().map_err(AnyError::from),
|
||||
Err(_) => Err(type_error("cannot sync this type of resource".to_string())),
|
||||
})?;
|
||||
Ok(json!({}))
|
||||
}
|
||||
|
@ -359,7 +358,7 @@ fn op_fstat_sync(
|
|||
state: &mut OpState,
|
||||
args: Value,
|
||||
_zero_copy: &mut [ZeroCopyBuf],
|
||||
) -> Result<Value, ErrBox> {
|
||||
) -> Result<Value, AnyError> {
|
||||
{
|
||||
let cli_state = super::cli_state(state);
|
||||
cli_state.check_unstable("Deno.fstat");
|
||||
|
@ -367,10 +366,8 @@ fn op_fstat_sync(
|
|||
let args: FstatArgs = serde_json::from_value(args)?;
|
||||
let rid = args.rid as u32;
|
||||
let metadata = std_file_resource(state, rid, |r| match r {
|
||||
Ok(std_file) => std_file.metadata().map_err(ErrBox::from),
|
||||
Err(_) => Err(ErrBox::type_error(
|
||||
"cannot stat this type of resource".to_string(),
|
||||
)),
|
||||
Ok(std_file) => std_file.metadata().map_err(AnyError::from),
|
||||
Err(_) => Err(type_error("cannot stat this type of resource".to_string())),
|
||||
})?;
|
||||
Ok(get_stat_json(metadata).unwrap())
|
||||
}
|
||||
|
@ -379,17 +376,17 @@ async fn op_fstat_async(
|
|||
state: Rc<RefCell<OpState>>,
|
||||
args: Value,
|
||||
_zero_copy: BufVec,
|
||||
) -> Result<Value, ErrBox> {
|
||||
) -> Result<Value, AnyError> {
|
||||
super::cli_state2(&state).check_unstable("Deno.fstat");
|
||||
|
||||
let args: FstatArgs = serde_json::from_value(args)?;
|
||||
let rid = args.rid as u32;
|
||||
let metadata =
|
||||
std_file_resource(&mut state.borrow_mut(), rid, |r| match r {
|
||||
Ok(std_file) => std_file.metadata().map_err(ErrBox::from),
|
||||
Err(_) => Err(ErrBox::type_error(
|
||||
"cannot stat this type of resource".to_string(),
|
||||
)),
|
||||
Ok(std_file) => std_file.metadata().map_err(AnyError::from),
|
||||
Err(_) => {
|
||||
Err(type_error("cannot stat this type of resource".to_string()))
|
||||
}
|
||||
})?;
|
||||
Ok(get_stat_json(metadata).unwrap())
|
||||
}
|
||||
|
@ -403,7 +400,7 @@ fn op_umask(
|
|||
state: &mut OpState,
|
||||
args: Value,
|
||||
_zero_copy: &mut [ZeroCopyBuf],
|
||||
) -> Result<Value, ErrBox> {
|
||||
) -> Result<Value, AnyError> {
|
||||
{
|
||||
let cli_state = super::cli_state(state);
|
||||
cli_state.check_unstable("Deno.umask");
|
||||
|
@ -415,7 +412,7 @@ fn op_umask(
|
|||
#[cfg(not(unix))]
|
||||
{
|
||||
let _ = args.mask; // avoid unused warning.
|
||||
Err(ErrBox::not_supported())
|
||||
Err(not_supported())
|
||||
}
|
||||
#[cfg(unix)]
|
||||
{
|
||||
|
@ -444,7 +441,7 @@ fn op_chdir(
|
|||
state: &mut OpState,
|
||||
args: Value,
|
||||
_zero_copy: &mut [ZeroCopyBuf],
|
||||
) -> Result<Value, ErrBox> {
|
||||
) -> Result<Value, AnyError> {
|
||||
let args: ChdirArgs = serde_json::from_value(args)?;
|
||||
let d = PathBuf::from(&args.directory);
|
||||
let cli_state = super::cli_state(state);
|
||||
|
@ -465,7 +462,7 @@ fn op_mkdir_sync(
|
|||
state: &mut OpState,
|
||||
args: Value,
|
||||
_zero_copy: &mut [ZeroCopyBuf],
|
||||
) -> Result<Value, ErrBox> {
|
||||
) -> Result<Value, AnyError> {
|
||||
let args: MkdirArgs = serde_json::from_value(args)?;
|
||||
let path = Path::new(&args.path).to_path_buf();
|
||||
let mode = args.mode.unwrap_or(0o777) & 0o777;
|
||||
|
@ -487,7 +484,7 @@ async fn op_mkdir_async(
|
|||
state: Rc<RefCell<OpState>>,
|
||||
args: Value,
|
||||
_zero_copy: BufVec,
|
||||
) -> Result<Value, ErrBox> {
|
||||
) -> Result<Value, AnyError> {
|
||||
let args: MkdirArgs = serde_json::from_value(args)?;
|
||||
let path = Path::new(&args.path).to_path_buf();
|
||||
let mode = args.mode.unwrap_or(0o777) & 0o777;
|
||||
|
@ -521,7 +518,7 @@ fn op_chmod_sync(
|
|||
state: &mut OpState,
|
||||
args: Value,
|
||||
_zero_copy: &mut [ZeroCopyBuf],
|
||||
) -> Result<Value, ErrBox> {
|
||||
) -> Result<Value, AnyError> {
|
||||
let args: ChmodArgs = serde_json::from_value(args)?;
|
||||
let path = Path::new(&args.path).to_path_buf();
|
||||
let mode = args.mode & 0o777;
|
||||
|
@ -541,7 +538,7 @@ fn op_chmod_sync(
|
|||
{
|
||||
// Still check file/dir exists on Windows
|
||||
let _metadata = std::fs::metadata(&path)?;
|
||||
Err(ErrBox::error("Not implemented"))
|
||||
Err(generic_error("Not implemented"))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -549,7 +546,7 @@ async fn op_chmod_async(
|
|||
state: Rc<RefCell<OpState>>,
|
||||
args: Value,
|
||||
_zero_copy: BufVec,
|
||||
) -> Result<Value, ErrBox> {
|
||||
) -> Result<Value, AnyError> {
|
||||
let args: ChmodArgs = serde_json::from_value(args)?;
|
||||
let path = Path::new(&args.path).to_path_buf();
|
||||
let mode = args.mode & 0o777;
|
||||
|
@ -570,7 +567,7 @@ async fn op_chmod_async(
|
|||
{
|
||||
// Still check file/dir exists on Windows
|
||||
let _metadata = std::fs::metadata(&path)?;
|
||||
Err(ErrBox::not_supported())
|
||||
Err(not_supported())
|
||||
}
|
||||
})
|
||||
.await
|
||||
|
@ -589,7 +586,7 @@ fn op_chown_sync(
|
|||
state: &mut OpState,
|
||||
args: Value,
|
||||
_zero_copy: &mut [ZeroCopyBuf],
|
||||
) -> Result<Value, ErrBox> {
|
||||
) -> Result<Value, AnyError> {
|
||||
let args: ChownArgs = serde_json::from_value(args)?;
|
||||
let path = Path::new(&args.path).to_path_buf();
|
||||
let cli_state = super::cli_state(state);
|
||||
|
@ -611,7 +608,7 @@ fn op_chown_sync(
|
|||
// TODO Implement chown for Windows
|
||||
#[cfg(not(unix))]
|
||||
{
|
||||
Err(ErrBox::error("Not implemented"))
|
||||
Err(generic_error("Not implemented"))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -619,7 +616,7 @@ async fn op_chown_async(
|
|||
state: Rc<RefCell<OpState>>,
|
||||
args: Value,
|
||||
_zero_copy: BufVec,
|
||||
) -> Result<Value, ErrBox> {
|
||||
) -> Result<Value, AnyError> {
|
||||
let args: ChownArgs = serde_json::from_value(args)?;
|
||||
let path = Path::new(&args.path).to_path_buf();
|
||||
|
||||
|
@ -642,7 +639,7 @@ async fn op_chown_async(
|
|||
}
|
||||
// TODO Implement chown for Windows
|
||||
#[cfg(not(unix))]
|
||||
Err(ErrBox::not_supported())
|
||||
Err(not_supported())
|
||||
})
|
||||
.await
|
||||
.unwrap()
|
||||
|
@ -659,7 +656,7 @@ fn op_remove_sync(
|
|||
state: &mut OpState,
|
||||
args: Value,
|
||||
_zero_copy: &mut [ZeroCopyBuf],
|
||||
) -> Result<Value, ErrBox> {
|
||||
) -> Result<Value, AnyError> {
|
||||
let args: RemoveArgs = serde_json::from_value(args)?;
|
||||
let path = PathBuf::from(&args.path);
|
||||
let recursive = args.recursive;
|
||||
|
@ -703,7 +700,7 @@ async fn op_remove_async(
|
|||
state: Rc<RefCell<OpState>>,
|
||||
args: Value,
|
||||
_zero_copy: BufVec,
|
||||
) -> Result<Value, ErrBox> {
|
||||
) -> Result<Value, AnyError> {
|
||||
let args: RemoveArgs = serde_json::from_value(args)?;
|
||||
let path = PathBuf::from(&args.path);
|
||||
let recursive = args.recursive;
|
||||
|
@ -757,7 +754,7 @@ fn op_copy_file_sync(
|
|||
state: &mut OpState,
|
||||
args: Value,
|
||||
_zero_copy: &mut [ZeroCopyBuf],
|
||||
) -> Result<Value, ErrBox> {
|
||||
) -> Result<Value, AnyError> {
|
||||
let args: CopyFileArgs = serde_json::from_value(args)?;
|
||||
let from = PathBuf::from(&args.from);
|
||||
let to = PathBuf::from(&args.to);
|
||||
|
@ -771,7 +768,7 @@ fn op_copy_file_sync(
|
|||
// See https://github.com/rust-lang/rust/issues/54800
|
||||
// Once the issue is resolved, we should remove this workaround.
|
||||
if cfg!(unix) && !from.is_file() {
|
||||
return Err(ErrBox::new("NotFound", "File not found"));
|
||||
return Err(custom_error("NotFound", "File not found"));
|
||||
}
|
||||
|
||||
// returns size of from as u64 (we ignore)
|
||||
|
@ -783,7 +780,7 @@ async fn op_copy_file_async(
|
|||
state: Rc<RefCell<OpState>>,
|
||||
args: Value,
|
||||
_zero_copy: BufVec,
|
||||
) -> Result<Value, ErrBox> {
|
||||
) -> Result<Value, AnyError> {
|
||||
let args: CopyFileArgs = serde_json::from_value(args)?;
|
||||
let from = PathBuf::from(&args.from);
|
||||
let to = PathBuf::from(&args.to);
|
||||
|
@ -798,7 +795,7 @@ async fn op_copy_file_async(
|
|||
// See https://github.com/rust-lang/rust/issues/54800
|
||||
// Once the issue is resolved, we should remove this workaround.
|
||||
if cfg!(unix) && !from.is_file() {
|
||||
return Err(ErrBox::new("NotFound", "File not found"));
|
||||
return Err(custom_error("NotFound", "File not found"));
|
||||
}
|
||||
|
||||
// returns size of from as u64 (we ignore)
|
||||
|
@ -825,7 +822,7 @@ fn to_msec(maybe_time: Result<SystemTime, io::Error>) -> Value {
|
|||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn get_stat_json(metadata: std::fs::Metadata) -> Result<Value, ErrBox> {
|
||||
fn get_stat_json(metadata: std::fs::Metadata) -> Result<Value, AnyError> {
|
||||
// Unix stat member (number types only). 0 if not on unix.
|
||||
macro_rules! usm {
|
||||
($member:ident) => {{
|
||||
|
@ -878,7 +875,7 @@ fn op_stat_sync(
|
|||
state: &mut OpState,
|
||||
args: Value,
|
||||
_zero_copy: &mut [ZeroCopyBuf],
|
||||
) -> Result<Value, ErrBox> {
|
||||
) -> Result<Value, AnyError> {
|
||||
let args: StatArgs = serde_json::from_value(args)?;
|
||||
let path = PathBuf::from(&args.path);
|
||||
let lstat = args.lstat;
|
||||
|
@ -897,7 +894,7 @@ async fn op_stat_async(
|
|||
state: Rc<RefCell<OpState>>,
|
||||
args: Value,
|
||||
_zero_copy: BufVec,
|
||||
) -> Result<Value, ErrBox> {
|
||||
) -> Result<Value, AnyError> {
|
||||
let args: StatArgs = serde_json::from_value(args)?;
|
||||
let path = PathBuf::from(&args.path);
|
||||
let lstat = args.lstat;
|
||||
|
@ -927,7 +924,7 @@ fn op_realpath_sync(
|
|||
state: &mut OpState,
|
||||
args: Value,
|
||||
_zero_copy: &mut [ZeroCopyBuf],
|
||||
) -> Result<Value, ErrBox> {
|
||||
) -> Result<Value, AnyError> {
|
||||
let args: RealpathArgs = serde_json::from_value(args)?;
|
||||
let path = PathBuf::from(&args.path);
|
||||
|
||||
|
@ -953,7 +950,7 @@ async fn op_realpath_async(
|
|||
state: Rc<RefCell<OpState>>,
|
||||
args: Value,
|
||||
_zero_copy: BufVec,
|
||||
) -> Result<Value, ErrBox> {
|
||||
) -> Result<Value, AnyError> {
|
||||
let args: RealpathArgs = serde_json::from_value(args)?;
|
||||
let path = PathBuf::from(&args.path);
|
||||
|
||||
|
@ -989,7 +986,7 @@ fn op_read_dir_sync(
|
|||
state: &mut OpState,
|
||||
args: Value,
|
||||
_zero_copy: &mut [ZeroCopyBuf],
|
||||
) -> Result<Value, ErrBox> {
|
||||
) -> Result<Value, AnyError> {
|
||||
let args: ReadDirArgs = serde_json::from_value(args)?;
|
||||
let path = PathBuf::from(&args.path);
|
||||
|
||||
|
@ -1022,7 +1019,7 @@ async fn op_read_dir_async(
|
|||
state: Rc<RefCell<OpState>>,
|
||||
args: Value,
|
||||
_zero_copy: BufVec,
|
||||
) -> Result<Value, ErrBox> {
|
||||
) -> Result<Value, AnyError> {
|
||||
let args: ReadDirArgs = serde_json::from_value(args)?;
|
||||
let path = PathBuf::from(&args.path);
|
||||
super::cli_state2(&state).check_read(&path)?;
|
||||
|
@ -1063,7 +1060,7 @@ fn op_rename_sync(
|
|||
state: &mut OpState,
|
||||
args: Value,
|
||||
_zero_copy: &mut [ZeroCopyBuf],
|
||||
) -> Result<Value, ErrBox> {
|
||||
) -> Result<Value, AnyError> {
|
||||
let args: RenameArgs = serde_json::from_value(args)?;
|
||||
let oldpath = PathBuf::from(&args.oldpath);
|
||||
let newpath = PathBuf::from(&args.newpath);
|
||||
|
@ -1081,7 +1078,7 @@ async fn op_rename_async(
|
|||
state: Rc<RefCell<OpState>>,
|
||||
args: Value,
|
||||
_zero_copy: BufVec,
|
||||
) -> Result<Value, ErrBox> {
|
||||
) -> Result<Value, AnyError> {
|
||||
let args: RenameArgs = serde_json::from_value(args)?;
|
||||
let oldpath = PathBuf::from(&args.oldpath);
|
||||
let newpath = PathBuf::from(&args.newpath);
|
||||
|
@ -1115,7 +1112,7 @@ fn op_link_sync(
|
|||
state: &mut OpState,
|
||||
args: Value,
|
||||
_zero_copy: &mut [ZeroCopyBuf],
|
||||
) -> Result<Value, ErrBox> {
|
||||
) -> Result<Value, AnyError> {
|
||||
let cli_state = super::cli_state(state);
|
||||
cli_state.check_unstable("Deno.link");
|
||||
let args: LinkArgs = serde_json::from_value(args)?;
|
||||
|
@ -1134,7 +1131,7 @@ async fn op_link_async(
|
|||
state: Rc<RefCell<OpState>>,
|
||||
args: Value,
|
||||
_zero_copy: BufVec,
|
||||
) -> Result<Value, ErrBox> {
|
||||
) -> Result<Value, AnyError> {
|
||||
let cli_state = super::cli_state2(&state);
|
||||
cli_state.check_unstable("Deno.link");
|
||||
|
||||
|
@ -1174,7 +1171,7 @@ fn op_symlink_sync(
|
|||
state: &mut OpState,
|
||||
args: Value,
|
||||
_zero_copy: &mut [ZeroCopyBuf],
|
||||
) -> Result<Value, ErrBox> {
|
||||
) -> Result<Value, AnyError> {
|
||||
let cli_state = super::cli_state(state);
|
||||
cli_state.check_unstable("Deno.symlink");
|
||||
let args: SymlinkArgs = serde_json::from_value(args)?;
|
||||
|
@ -1202,7 +1199,7 @@ fn op_symlink_sync(
|
|||
Some(options) => match options._type.as_ref() {
|
||||
"file" => symlink_file(&oldpath, &newpath)?,
|
||||
"dir" => symlink_dir(&oldpath, &newpath)?,
|
||||
_ => return Err(ErrBox::type_error("unsupported type")),
|
||||
_ => return Err(type_error("unsupported type")),
|
||||
},
|
||||
None => {
|
||||
let old_meta = std::fs::metadata(&oldpath);
|
||||
|
@ -1214,7 +1211,7 @@ fn op_symlink_sync(
|
|||
symlink_dir(&oldpath, &newpath)?
|
||||
}
|
||||
}
|
||||
Err(_) => return Err(ErrBox::type_error("you must pass a `options` argument for non-existent target path in windows".to_string())),
|
||||
Err(_) => return Err(type_error("you must pass a `options` argument for non-existent target path in windows".to_string())),
|
||||
}
|
||||
}
|
||||
};
|
||||
|
@ -1226,7 +1223,7 @@ async fn op_symlink_async(
|
|||
state: Rc<RefCell<OpState>>,
|
||||
args: Value,
|
||||
_zero_copy: BufVec,
|
||||
) -> Result<Value, ErrBox> {
|
||||
) -> Result<Value, AnyError> {
|
||||
let cli_state = super::cli_state2(&state);
|
||||
cli_state.check_unstable("Deno.symlink");
|
||||
|
||||
|
@ -1252,7 +1249,7 @@ async fn op_symlink_async(
|
|||
Some(options) => match options._type.as_ref() {
|
||||
"file" => symlink_file(&oldpath, &newpath)?,
|
||||
"dir" => symlink_dir(&oldpath, &newpath)?,
|
||||
_ => return Err(ErrBox::type_error("unsupported type")),
|
||||
_ => return Err(type_error("unsupported type")),
|
||||
},
|
||||
None => {
|
||||
let old_meta = std::fs::metadata(&oldpath);
|
||||
|
@ -1264,7 +1261,7 @@ async fn op_symlink_async(
|
|||
symlink_dir(&oldpath, &newpath)?
|
||||
}
|
||||
}
|
||||
Err(_) => return Err(ErrBox::type_error("you must pass a `options` argument for non-existent target path in windows".to_string())),
|
||||
Err(_) => return Err(type_error("you must pass a `options` argument for non-existent target path in windows".to_string())),
|
||||
}
|
||||
}
|
||||
};
|
||||
|
@ -1285,7 +1282,7 @@ fn op_read_link_sync(
|
|||
state: &mut OpState,
|
||||
args: Value,
|
||||
_zero_copy: &mut [ZeroCopyBuf],
|
||||
) -> Result<Value, ErrBox> {
|
||||
) -> Result<Value, AnyError> {
|
||||
let args: ReadLinkArgs = serde_json::from_value(args)?;
|
||||
let path = PathBuf::from(&args.path);
|
||||
|
||||
|
@ -1302,7 +1299,7 @@ async fn op_read_link_async(
|
|||
state: Rc<RefCell<OpState>>,
|
||||
args: Value,
|
||||
_zero_copy: BufVec,
|
||||
) -> Result<Value, ErrBox> {
|
||||
) -> Result<Value, AnyError> {
|
||||
let args: ReadLinkArgs = serde_json::from_value(args)?;
|
||||
let path = PathBuf::from(&args.path);
|
||||
super::cli_state2(&state).check_read(&path)?;
|
||||
|
@ -1327,7 +1324,7 @@ fn op_ftruncate_sync(
|
|||
state: &mut OpState,
|
||||
args: Value,
|
||||
_zero_copy: &mut [ZeroCopyBuf],
|
||||
) -> Result<Value, ErrBox> {
|
||||
) -> Result<Value, AnyError> {
|
||||
{
|
||||
let cli_state = super::cli_state(state);
|
||||
cli_state.check_unstable("Deno.ftruncate");
|
||||
|
@ -1336,8 +1333,8 @@ fn op_ftruncate_sync(
|
|||
let rid = args.rid as u32;
|
||||
let len = args.len as u64;
|
||||
std_file_resource(state, rid, |r| match r {
|
||||
Ok(std_file) => std_file.set_len(len).map_err(ErrBox::from),
|
||||
Err(_) => Err(ErrBox::type_error("cannot truncate this type of resource")),
|
||||
Ok(std_file) => std_file.set_len(len).map_err(AnyError::from),
|
||||
Err(_) => Err(type_error("cannot truncate this type of resource")),
|
||||
})?;
|
||||
Ok(json!({}))
|
||||
}
|
||||
|
@ -1346,14 +1343,14 @@ async fn op_ftruncate_async(
|
|||
state: Rc<RefCell<OpState>>,
|
||||
args: Value,
|
||||
_zero_copy: BufVec,
|
||||
) -> Result<Value, ErrBox> {
|
||||
) -> Result<Value, AnyError> {
|
||||
super::cli_state2(&state).check_unstable("Deno.ftruncate");
|
||||
let args: FtruncateArgs = serde_json::from_value(args)?;
|
||||
let rid = args.rid as u32;
|
||||
let len = args.len as u64;
|
||||
std_file_resource(&mut state.borrow_mut(), rid, |r| match r {
|
||||
Ok(std_file) => std_file.set_len(len).map_err(ErrBox::from),
|
||||
Err(_) => Err(ErrBox::type_error("cannot truncate this type of resource")),
|
||||
Ok(std_file) => std_file.set_len(len).map_err(AnyError::from),
|
||||
Err(_) => Err(type_error("cannot truncate this type of resource")),
|
||||
})?;
|
||||
Ok(json!({}))
|
||||
}
|
||||
|
@ -1369,7 +1366,7 @@ fn op_truncate_sync(
|
|||
state: &mut OpState,
|
||||
args: Value,
|
||||
_zero_copy: &mut [ZeroCopyBuf],
|
||||
) -> Result<Value, ErrBox> {
|
||||
) -> Result<Value, AnyError> {
|
||||
let args: TruncateArgs = serde_json::from_value(args)?;
|
||||
let path = PathBuf::from(&args.path);
|
||||
let len = args.len;
|
||||
|
@ -1387,7 +1384,7 @@ async fn op_truncate_async(
|
|||
state: Rc<RefCell<OpState>>,
|
||||
args: Value,
|
||||
_zero_copy: BufVec,
|
||||
) -> Result<Value, ErrBox> {
|
||||
) -> Result<Value, AnyError> {
|
||||
let args: TruncateArgs = serde_json::from_value(args)?;
|
||||
let path = PathBuf::from(&args.path);
|
||||
let len = args.len;
|
||||
|
@ -1459,7 +1456,7 @@ fn op_make_temp_dir_sync(
|
|||
state: &mut OpState,
|
||||
args: Value,
|
||||
_zero_copy: &mut [ZeroCopyBuf],
|
||||
) -> Result<Value, ErrBox> {
|
||||
) -> Result<Value, AnyError> {
|
||||
let args: MakeTempArgs = serde_json::from_value(args)?;
|
||||
|
||||
let dir = args.dir.map(|s| PathBuf::from(&s));
|
||||
|
@ -1488,7 +1485,7 @@ async fn op_make_temp_dir_async(
|
|||
state: Rc<RefCell<OpState>>,
|
||||
args: Value,
|
||||
_zero_copy: BufVec,
|
||||
) -> Result<Value, ErrBox> {
|
||||
) -> Result<Value, AnyError> {
|
||||
let args: MakeTempArgs = serde_json::from_value(args)?;
|
||||
|
||||
let dir = args.dir.map(|s| PathBuf::from(&s));
|
||||
|
@ -1521,7 +1518,7 @@ fn op_make_temp_file_sync(
|
|||
state: &mut OpState,
|
||||
args: Value,
|
||||
_zero_copy: &mut [ZeroCopyBuf],
|
||||
) -> Result<Value, ErrBox> {
|
||||
) -> Result<Value, AnyError> {
|
||||
let args: MakeTempArgs = serde_json::from_value(args)?;
|
||||
|
||||
let dir = args.dir.map(|s| PathBuf::from(&s));
|
||||
|
@ -1550,7 +1547,7 @@ async fn op_make_temp_file_async(
|
|||
state: Rc<RefCell<OpState>>,
|
||||
args: Value,
|
||||
_zero_copy: BufVec,
|
||||
) -> Result<Value, ErrBox> {
|
||||
) -> Result<Value, AnyError> {
|
||||
let args: MakeTempArgs = serde_json::from_value(args)?;
|
||||
|
||||
let dir = args.dir.map(|s| PathBuf::from(&s));
|
||||
|
@ -1592,7 +1589,7 @@ fn op_futime_sync(
|
|||
state: &mut OpState,
|
||||
args: Value,
|
||||
_zero_copy: &mut [ZeroCopyBuf],
|
||||
) -> Result<Value, ErrBox> {
|
||||
) -> Result<Value, AnyError> {
|
||||
{
|
||||
let cli_state = super::cli_state(state);
|
||||
cli_state.check_unstable("Deno.futimeSync");
|
||||
|
@ -1605,9 +1602,9 @@ fn op_futime_sync(
|
|||
std_file_resource(state, rid, |r| match r {
|
||||
Ok(std_file) => {
|
||||
filetime::set_file_handle_times(std_file, Some(atime), Some(mtime))
|
||||
.map_err(ErrBox::from)
|
||||
.map_err(AnyError::from)
|
||||
}
|
||||
Err(_) => Err(ErrBox::type_error(
|
||||
Err(_) => Err(type_error(
|
||||
"cannot futime on this type of resource".to_string(),
|
||||
)),
|
||||
})?;
|
||||
|
@ -1619,7 +1616,7 @@ async fn op_futime_async(
|
|||
state: Rc<RefCell<OpState>>,
|
||||
args: Value,
|
||||
_zero_copy: BufVec,
|
||||
) -> Result<Value, ErrBox> {
|
||||
) -> Result<Value, AnyError> {
|
||||
let mut state = state.borrow_mut();
|
||||
let cli_state = super::cli_state(&state);
|
||||
cli_state.check_unstable("Deno.futime");
|
||||
|
@ -1631,9 +1628,9 @@ async fn op_futime_async(
|
|||
std_file_resource(&mut state, rid, |r| match r {
|
||||
Ok(std_file) => {
|
||||
filetime::set_file_handle_times(std_file, Some(atime), Some(mtime))
|
||||
.map_err(ErrBox::from)
|
||||
.map_err(AnyError::from)
|
||||
}
|
||||
Err(_) => Err(ErrBox::type_error(
|
||||
Err(_) => Err(type_error(
|
||||
"cannot futime on this type of resource".to_string(),
|
||||
)),
|
||||
})?;
|
||||
|
@ -1653,7 +1650,7 @@ fn op_utime_sync(
|
|||
state: &mut OpState,
|
||||
args: Value,
|
||||
_zero_copy: &mut [ZeroCopyBuf],
|
||||
) -> Result<Value, ErrBox> {
|
||||
) -> Result<Value, AnyError> {
|
||||
let cli_state = super::cli_state(state);
|
||||
cli_state.check_unstable("Deno.utime");
|
||||
|
||||
|
@ -1671,7 +1668,7 @@ async fn op_utime_async(
|
|||
state: Rc<RefCell<OpState>>,
|
||||
args: Value,
|
||||
_zero_copy: BufVec,
|
||||
) -> Result<Value, ErrBox> {
|
||||
) -> Result<Value, AnyError> {
|
||||
let state = state.borrow();
|
||||
let cli_state = super::cli_state(&state);
|
||||
cli_state.check_unstable("Deno.utime");
|
||||
|
@ -1695,7 +1692,7 @@ fn op_cwd(
|
|||
state: &mut OpState,
|
||||
_args: Value,
|
||||
_zero_copy: &mut [ZeroCopyBuf],
|
||||
) -> Result<Value, ErrBox> {
|
||||
) -> Result<Value, AnyError> {
|
||||
let path = current_dir()?;
|
||||
let cli_state = super::cli_state(state);
|
||||
cli_state.check_read_blind(&path, "CWD")?;
|
||||
|
|
|
@ -1,7 +1,8 @@
|
|||
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
||||
|
||||
use deno_core::error::bad_resource_id;
|
||||
use deno_core::error::AnyError;
|
||||
use deno_core::BufVec;
|
||||
use deno_core::ErrBox;
|
||||
use deno_core::OpState;
|
||||
use deno_core::ZeroCopyBuf;
|
||||
use futures::future::poll_fn;
|
||||
|
@ -28,7 +29,7 @@ pub fn init(rt: &mut deno_core::JsRuntime) {
|
|||
struct FsEventsResource {
|
||||
#[allow(unused)]
|
||||
watcher: RecommendedWatcher,
|
||||
receiver: mpsc::Receiver<Result<FsEvent, ErrBox>>,
|
||||
receiver: mpsc::Receiver<Result<FsEvent, AnyError>>,
|
||||
}
|
||||
|
||||
/// Represents a file system event.
|
||||
|
@ -67,18 +68,18 @@ fn op_fs_events_open(
|
|||
state: &mut OpState,
|
||||
args: Value,
|
||||
_zero_copy: &mut [ZeroCopyBuf],
|
||||
) -> Result<Value, ErrBox> {
|
||||
) -> Result<Value, AnyError> {
|
||||
#[derive(Deserialize)]
|
||||
struct OpenArgs {
|
||||
recursive: bool,
|
||||
paths: Vec<String>,
|
||||
}
|
||||
let args: OpenArgs = serde_json::from_value(args)?;
|
||||
let (sender, receiver) = mpsc::channel::<Result<FsEvent, ErrBox>>(16);
|
||||
let (sender, receiver) = mpsc::channel::<Result<FsEvent, AnyError>>(16);
|
||||
let sender = std::sync::Mutex::new(sender);
|
||||
let mut watcher: RecommendedWatcher =
|
||||
Watcher::new_immediate(move |res: Result<NotifyEvent, NotifyError>| {
|
||||
let res2 = res.map(FsEvent::from).map_err(ErrBox::from);
|
||||
let res2 = res.map(FsEvent::from).map_err(AnyError::from);
|
||||
let mut sender = sender.lock().unwrap();
|
||||
// Ignore result, if send failed it means that watcher was already closed,
|
||||
// but not all messages have been flushed.
|
||||
|
@ -102,7 +103,7 @@ async fn op_fs_events_poll(
|
|||
state: Rc<RefCell<OpState>>,
|
||||
args: Value,
|
||||
_zero_copy: BufVec,
|
||||
) -> Result<Value, ErrBox> {
|
||||
) -> Result<Value, AnyError> {
|
||||
#[derive(Deserialize)]
|
||||
struct PollArgs {
|
||||
rid: u32,
|
||||
|
@ -113,7 +114,7 @@ async fn op_fs_events_poll(
|
|||
let watcher = state
|
||||
.resource_table
|
||||
.get_mut::<FsEventsResource>(rid)
|
||||
.ok_or_else(ErrBox::bad_resource_id)?;
|
||||
.ok_or_else(bad_resource_id)?;
|
||||
watcher
|
||||
.receiver
|
||||
.poll_recv(cx)
|
||||
|
|
|
@ -2,7 +2,8 @@
|
|||
|
||||
//! https://url.spec.whatwg.org/#idna
|
||||
|
||||
use deno_core::ErrBox;
|
||||
use deno_core::error::uri_error;
|
||||
use deno_core::error::AnyError;
|
||||
use deno_core::ZeroCopyBuf;
|
||||
use idna::domain_to_ascii;
|
||||
use idna::domain_to_ascii_strict;
|
||||
|
@ -24,7 +25,7 @@ fn op_domain_to_ascii(
|
|||
_state: &mut deno_core::OpState,
|
||||
args: Value,
|
||||
_zero_copy: &mut [ZeroCopyBuf],
|
||||
) -> Result<Value, ErrBox> {
|
||||
) -> Result<Value, AnyError> {
|
||||
let args: DomainToAscii = serde_json::from_value(args)?;
|
||||
if args.be_strict {
|
||||
domain_to_ascii_strict(args.domain.as_str())
|
||||
|
@ -33,7 +34,7 @@ fn op_domain_to_ascii(
|
|||
}
|
||||
.map_err(|err| {
|
||||
let message = format!("Invalid IDNA encoded domain name: {:?}", err);
|
||||
ErrBox::new("URIError", message)
|
||||
uri_error(message)
|
||||
})
|
||||
.map(|domain| json!(domain))
|
||||
}
|
||||
|
|
|
@ -2,8 +2,11 @@ use super::dispatch_minimal::minimal_op;
|
|||
use super::dispatch_minimal::MinimalOp;
|
||||
use crate::http_util::HttpBody;
|
||||
use crate::metrics::metrics_op;
|
||||
use deno_core::error::bad_resource_id;
|
||||
use deno_core::error::resource_unavailable;
|
||||
use deno_core::error::type_error;
|
||||
use deno_core::error::AnyError;
|
||||
use deno_core::BufVec;
|
||||
use deno_core::ErrBox;
|
||||
use deno_core::JsRuntime;
|
||||
use deno_core::OpState;
|
||||
use futures::future::poll_fn;
|
||||
|
@ -117,8 +120,8 @@ fn get_stdio_stream(
|
|||
}
|
||||
}
|
||||
|
||||
fn no_buffer_specified() -> ErrBox {
|
||||
ErrBox::type_error("no buffer specified")
|
||||
fn no_buffer_specified() -> AnyError {
|
||||
type_error("no buffer specified")
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
|
@ -160,7 +163,7 @@ impl Drop for StreamResourceHolder {
|
|||
}
|
||||
|
||||
impl StreamResourceHolder {
|
||||
pub fn track_task(&mut self, cx: &Context) -> Result<usize, ErrBox> {
|
||||
pub fn track_task(&mut self, cx: &Context) -> Result<usize, AnyError> {
|
||||
let waker = futures::task::AtomicWaker::new();
|
||||
waker.register(cx.waker());
|
||||
// Its OK if it overflows
|
||||
|
@ -201,13 +204,13 @@ impl<T: AsyncRead + Unpin> UnpinAsyncRead for T {}
|
|||
impl<T: AsyncWrite + Unpin> UnpinAsyncWrite for T {}
|
||||
|
||||
/// `DenoAsyncRead` is the same as the `tokio_io::AsyncRead` trait
|
||||
/// but uses an `ErrBox` error instead of `std::io:Error`
|
||||
/// but uses an `AnyError` error instead of `std::io:Error`
|
||||
pub trait DenoAsyncRead {
|
||||
fn poll_read(
|
||||
&mut self,
|
||||
cx: &mut Context,
|
||||
buf: &mut [u8],
|
||||
) -> Poll<Result<usize, ErrBox>>;
|
||||
) -> Poll<Result<usize, AnyError>>;
|
||||
}
|
||||
|
||||
impl DenoAsyncRead for StreamResource {
|
||||
|
@ -215,11 +218,11 @@ impl DenoAsyncRead for StreamResource {
|
|||
&mut self,
|
||||
cx: &mut Context,
|
||||
buf: &mut [u8],
|
||||
) -> Poll<Result<usize, ErrBox>> {
|
||||
) -> Poll<Result<usize, AnyError>> {
|
||||
use StreamResource::*;
|
||||
let f: &mut dyn UnpinAsyncRead = match self {
|
||||
FsFile(Some((f, _))) => f,
|
||||
FsFile(None) => return Poll::Ready(Err(ErrBox::resource_unavailable())),
|
||||
FsFile(None) => return Poll::Ready(Err(resource_unavailable())),
|
||||
Stdin(f, _) => f,
|
||||
TcpStream(Some(f)) => f,
|
||||
#[cfg(not(windows))]
|
||||
|
@ -229,7 +232,7 @@ impl DenoAsyncRead for StreamResource {
|
|||
ChildStdout(f) => f,
|
||||
ChildStderr(f) => f,
|
||||
HttpBody(f) => f,
|
||||
_ => return Err(ErrBox::bad_resource_id()).into(),
|
||||
_ => return Err(bad_resource_id()).into(),
|
||||
};
|
||||
let v = ready!(Pin::new(f).poll_read(cx, buf))?;
|
||||
Ok(v).into()
|
||||
|
@ -258,11 +261,9 @@ pub fn op_read(
|
|||
std_file
|
||||
.read(&mut zero_copy[0])
|
||||
.map(|n: usize| n as i32)
|
||||
.map_err(ErrBox::from)
|
||||
}
|
||||
Err(_) => {
|
||||
Err(ErrBox::type_error("sync read not allowed on this resource"))
|
||||
.map_err(AnyError::from)
|
||||
}
|
||||
Err(_) => Err(type_error("sync read not allowed on this resource")),
|
||||
})
|
||||
})
|
||||
} else {
|
||||
|
@ -273,7 +274,7 @@ pub fn op_read(
|
|||
let resource_holder = state
|
||||
.resource_table
|
||||
.get_mut::<StreamResourceHolder>(rid as u32)
|
||||
.ok_or_else(ErrBox::bad_resource_id)?;
|
||||
.ok_or_else(bad_resource_id)?;
|
||||
|
||||
let mut task_tracker_id: Option<usize> = None;
|
||||
let nread = match resource_holder.resource.poll_read(cx, &mut zero_copy)
|
||||
|
@ -297,17 +298,17 @@ pub fn op_read(
|
|||
}
|
||||
|
||||
/// `DenoAsyncWrite` is the same as the `tokio_io::AsyncWrite` trait
|
||||
/// but uses an `ErrBox` error instead of `std::io:Error`
|
||||
/// but uses an `AnyError` error instead of `std::io:Error`
|
||||
pub trait DenoAsyncWrite {
|
||||
fn poll_write(
|
||||
&mut self,
|
||||
cx: &mut Context,
|
||||
buf: &[u8],
|
||||
) -> Poll<Result<usize, ErrBox>>;
|
||||
) -> Poll<Result<usize, AnyError>>;
|
||||
|
||||
fn poll_close(&mut self, cx: &mut Context) -> Poll<Result<(), ErrBox>>;
|
||||
fn poll_close(&mut self, cx: &mut Context) -> Poll<Result<(), AnyError>>;
|
||||
|
||||
fn poll_flush(&mut self, cx: &mut Context) -> Poll<Result<(), ErrBox>>;
|
||||
fn poll_flush(&mut self, cx: &mut Context) -> Poll<Result<(), AnyError>>;
|
||||
}
|
||||
|
||||
impl DenoAsyncWrite for StreamResource {
|
||||
|
@ -315,7 +316,7 @@ impl DenoAsyncWrite for StreamResource {
|
|||
&mut self,
|
||||
cx: &mut Context,
|
||||
buf: &[u8],
|
||||
) -> Poll<Result<usize, ErrBox>> {
|
||||
) -> Poll<Result<usize, AnyError>> {
|
||||
use StreamResource::*;
|
||||
let f: &mut dyn UnpinAsyncWrite = match self {
|
||||
FsFile(Some((f, _))) => f,
|
||||
|
@ -326,14 +327,14 @@ impl DenoAsyncWrite for StreamResource {
|
|||
ClientTlsStream(f) => f,
|
||||
ServerTlsStream(f) => f,
|
||||
ChildStdin(f) => f,
|
||||
_ => return Err(ErrBox::bad_resource_id()).into(),
|
||||
_ => return Err(bad_resource_id()).into(),
|
||||
};
|
||||
|
||||
let v = ready!(Pin::new(f).poll_write(cx, buf))?;
|
||||
Ok(v).into()
|
||||
}
|
||||
|
||||
fn poll_flush(&mut self, cx: &mut Context) -> Poll<Result<(), ErrBox>> {
|
||||
fn poll_flush(&mut self, cx: &mut Context) -> Poll<Result<(), AnyError>> {
|
||||
use StreamResource::*;
|
||||
let f: &mut dyn UnpinAsyncWrite = match self {
|
||||
FsFile(Some((f, _))) => f,
|
||||
|
@ -344,14 +345,14 @@ impl DenoAsyncWrite for StreamResource {
|
|||
ClientTlsStream(f) => f,
|
||||
ServerTlsStream(f) => f,
|
||||
ChildStdin(f) => f,
|
||||
_ => return Err(ErrBox::bad_resource_id()).into(),
|
||||
_ => return Err(bad_resource_id()).into(),
|
||||
};
|
||||
|
||||
ready!(Pin::new(f).poll_flush(cx))?;
|
||||
Ok(()).into()
|
||||
}
|
||||
|
||||
fn poll_close(&mut self, _cx: &mut Context) -> Poll<Result<(), ErrBox>> {
|
||||
fn poll_close(&mut self, _cx: &mut Context) -> Poll<Result<(), AnyError>> {
|
||||
unimplemented!()
|
||||
}
|
||||
}
|
||||
|
@ -378,11 +379,9 @@ pub fn op_write(
|
|||
std_file
|
||||
.write(&zero_copy[0])
|
||||
.map(|nwritten: usize| nwritten as i32)
|
||||
.map_err(ErrBox::from)
|
||||
}
|
||||
Err(_) => {
|
||||
Err(ErrBox::type_error("sync read not allowed on this resource"))
|
||||
.map_err(AnyError::from)
|
||||
}
|
||||
Err(_) => Err(type_error("sync read not allowed on this resource")),
|
||||
})
|
||||
})
|
||||
} else {
|
||||
|
@ -394,7 +393,7 @@ pub fn op_write(
|
|||
let resource_holder = state
|
||||
.resource_table
|
||||
.get_mut::<StreamResourceHolder>(rid as u32)
|
||||
.ok_or_else(ErrBox::bad_resource_id)?;
|
||||
.ok_or_else(bad_resource_id)?;
|
||||
resource_holder.resource.poll_write(cx, &zero_copy)
|
||||
})
|
||||
.await?;
|
||||
|
@ -408,7 +407,7 @@ pub fn op_write(
|
|||
let resource_holder = state
|
||||
.resource_table
|
||||
.get_mut::<StreamResourceHolder>(rid as u32)
|
||||
.ok_or_else(ErrBox::bad_resource_id)?;
|
||||
.ok_or_else(bad_resource_id)?;
|
||||
resource_holder.resource.poll_flush(cx)
|
||||
})
|
||||
.await?;
|
||||
|
@ -431,10 +430,11 @@ pub fn std_file_resource<F, T>(
|
|||
state: &mut OpState,
|
||||
rid: u32,
|
||||
mut f: F,
|
||||
) -> Result<T, ErrBox>
|
||||
) -> Result<T, AnyError>
|
||||
where
|
||||
F:
|
||||
FnMut(Result<&mut std::fs::File, &mut StreamResource>) -> Result<T, ErrBox>,
|
||||
F: FnMut(
|
||||
Result<&mut std::fs::File, &mut StreamResource>,
|
||||
) -> Result<T, AnyError>,
|
||||
{
|
||||
// First we look up the rid in the resource table.
|
||||
let mut r = state.resource_table.get_mut::<StreamResourceHolder>(rid);
|
||||
|
@ -463,16 +463,16 @@ where
|
|||
// some operation is in-flight.
|
||||
resource_holder.resource =
|
||||
StreamResource::FsFile(Some((tokio_file, metadata)));
|
||||
Err(ErrBox::resource_unavailable())
|
||||
Err(resource_unavailable())
|
||||
}
|
||||
}
|
||||
} else {
|
||||
Err(ErrBox::resource_unavailable())
|
||||
Err(resource_unavailable())
|
||||
}
|
||||
}
|
||||
_ => f(Err(&mut resource_holder.resource)),
|
||||
}
|
||||
} else {
|
||||
Err(ErrBox::bad_resource_id())
|
||||
Err(bad_resource_id())
|
||||
}
|
||||
}
|
||||
|
|
|
@ -31,10 +31,10 @@ pub mod websocket;
|
|||
pub mod worker_host;
|
||||
|
||||
use crate::metrics::metrics_op;
|
||||
use deno_core::error::AnyError;
|
||||
use deno_core::json_op_async;
|
||||
use deno_core::json_op_sync;
|
||||
use deno_core::BufVec;
|
||||
use deno_core::ErrBox;
|
||||
use deno_core::JsRuntime;
|
||||
use deno_core::OpState;
|
||||
use deno_core::ZeroCopyBuf;
|
||||
|
@ -46,14 +46,14 @@ use std::rc::Rc;
|
|||
pub fn reg_json_async<F, R>(rt: &mut JsRuntime, name: &'static str, op_fn: F)
|
||||
where
|
||||
F: Fn(Rc<RefCell<OpState>>, Value, BufVec) -> R + 'static,
|
||||
R: Future<Output = Result<Value, ErrBox>> + 'static,
|
||||
R: Future<Output = Result<Value, AnyError>> + 'static,
|
||||
{
|
||||
rt.register_op(name, metrics_op(json_op_async(op_fn)));
|
||||
}
|
||||
|
||||
pub fn reg_json_sync<F>(rt: &mut JsRuntime, name: &'static str, op_fn: F)
|
||||
where
|
||||
F: Fn(&mut OpState, Value, &mut [ZeroCopyBuf]) -> Result<Value, ErrBox>
|
||||
F: Fn(&mut OpState, Value, &mut [ZeroCopyBuf]) -> Result<Value, AnyError>
|
||||
+ 'static,
|
||||
{
|
||||
rt.register_op(name, metrics_op(json_op_sync(op_fn)));
|
||||
|
|
|
@ -1,9 +1,15 @@
|
|||
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
||||
|
||||
use crate::ops::io::{StreamResource, StreamResourceHolder};
|
||||
use crate::ops::io::StreamResource;
|
||||
use crate::ops::io::StreamResourceHolder;
|
||||
use crate::resolve_addr::resolve_addr;
|
||||
use deno_core::error::bad_resource;
|
||||
use deno_core::error::bad_resource_id;
|
||||
use deno_core::error::custom_error;
|
||||
use deno_core::error::generic_error;
|
||||
use deno_core::error::type_error;
|
||||
use deno_core::error::AnyError;
|
||||
use deno_core::BufVec;
|
||||
use deno_core::ErrBox;
|
||||
use deno_core::OpState;
|
||||
use deno_core::ZeroCopyBuf;
|
||||
use futures::future::poll_fn;
|
||||
|
@ -21,6 +27,8 @@ use tokio::net::UdpSocket;
|
|||
|
||||
#[cfg(unix)]
|
||||
use super::net_unix;
|
||||
#[cfg(unix)]
|
||||
use std::path::Path;
|
||||
|
||||
pub fn init(rt: &mut deno_core::JsRuntime) {
|
||||
super::reg_json_async(rt, "op_accept", op_accept);
|
||||
|
@ -41,7 +49,7 @@ async fn accept_tcp(
|
|||
state: Rc<RefCell<OpState>>,
|
||||
args: AcceptArgs,
|
||||
_zero_copy: BufVec,
|
||||
) -> Result<Value, ErrBox> {
|
||||
) -> Result<Value, AnyError> {
|
||||
let rid = args.rid as u32;
|
||||
|
||||
let accept_fut = poll_fn(|cx| {
|
||||
|
@ -49,9 +57,9 @@ async fn accept_tcp(
|
|||
let listener_resource = state
|
||||
.resource_table
|
||||
.get_mut::<TcpListenerResource>(rid)
|
||||
.ok_or_else(|| ErrBox::bad_resource("Listener has been closed"))?;
|
||||
.ok_or_else(|| bad_resource("Listener has been closed"))?;
|
||||
let listener = &mut listener_resource.listener;
|
||||
match listener.poll_accept(cx).map_err(ErrBox::from) {
|
||||
match listener.poll_accept(cx).map_err(AnyError::from) {
|
||||
Poll::Ready(Ok((stream, addr))) => {
|
||||
listener_resource.untrack_task();
|
||||
Poll::Ready(Ok((stream, addr)))
|
||||
|
@ -96,13 +104,13 @@ async fn op_accept(
|
|||
state: Rc<RefCell<OpState>>,
|
||||
args: Value,
|
||||
bufs: BufVec,
|
||||
) -> Result<Value, ErrBox> {
|
||||
) -> Result<Value, AnyError> {
|
||||
let args: AcceptArgs = serde_json::from_value(args)?;
|
||||
match args.transport.as_str() {
|
||||
"tcp" => accept_tcp(state, args, bufs).await,
|
||||
#[cfg(unix)]
|
||||
"unix" => net_unix::accept_unix(state, args, bufs).await,
|
||||
_ => Err(ErrBox::error(format!(
|
||||
_ => Err(generic_error(format!(
|
||||
"Unsupported transport protocol {}",
|
||||
args.transport
|
||||
))),
|
||||
|
@ -119,7 +127,7 @@ async fn receive_udp(
|
|||
state: Rc<RefCell<OpState>>,
|
||||
args: ReceiveArgs,
|
||||
zero_copy: BufVec,
|
||||
) -> Result<Value, ErrBox> {
|
||||
) -> Result<Value, AnyError> {
|
||||
assert_eq!(zero_copy.len(), 1, "Invalid number of arguments");
|
||||
let mut zero_copy = zero_copy[0].clone();
|
||||
|
||||
|
@ -130,11 +138,11 @@ async fn receive_udp(
|
|||
let resource = state
|
||||
.resource_table
|
||||
.get_mut::<UdpSocketResource>(rid)
|
||||
.ok_or_else(|| ErrBox::bad_resource("Socket has been closed"))?;
|
||||
.ok_or_else(|| bad_resource("Socket has been closed"))?;
|
||||
let socket = &mut resource.socket;
|
||||
socket
|
||||
.poll_recv_from(cx, &mut zero_copy)
|
||||
.map_err(ErrBox::from)
|
||||
.map_err(AnyError::from)
|
||||
});
|
||||
let (size, remote_addr) = receive_fut.await?;
|
||||
Ok(json!({
|
||||
|
@ -151,7 +159,7 @@ async fn op_datagram_receive(
|
|||
state: Rc<RefCell<OpState>>,
|
||||
args: Value,
|
||||
zero_copy: BufVec,
|
||||
) -> Result<Value, ErrBox> {
|
||||
) -> Result<Value, AnyError> {
|
||||
assert_eq!(zero_copy.len(), 1, "Invalid number of arguments");
|
||||
|
||||
let args: ReceiveArgs = serde_json::from_value(args)?;
|
||||
|
@ -159,7 +167,7 @@ async fn op_datagram_receive(
|
|||
"udp" => receive_udp(state, args, zero_copy).await,
|
||||
#[cfg(unix)]
|
||||
"unixpacket" => net_unix::receive_unix_packet(state, args, zero_copy).await,
|
||||
_ => Err(ErrBox::error(format!(
|
||||
_ => Err(generic_error(format!(
|
||||
"Unsupported transport protocol {}",
|
||||
args.transport
|
||||
))),
|
||||
|
@ -178,7 +186,7 @@ async fn op_datagram_send(
|
|||
state: Rc<RefCell<OpState>>,
|
||||
args: Value,
|
||||
zero_copy: BufVec,
|
||||
) -> Result<Value, ErrBox> {
|
||||
) -> Result<Value, AnyError> {
|
||||
assert_eq!(zero_copy.len(), 1, "Invalid number of arguments");
|
||||
let zero_copy = zero_copy[0].clone();
|
||||
let cli_state = super::cli_state2(&state);
|
||||
|
@ -196,12 +204,12 @@ async fn op_datagram_send(
|
|||
let resource = state
|
||||
.resource_table
|
||||
.get_mut::<UdpSocketResource>(rid as u32)
|
||||
.ok_or_else(|| ErrBox::bad_resource("Socket has been closed"))?;
|
||||
.ok_or_else(|| bad_resource("Socket has been closed"))?;
|
||||
resource
|
||||
.socket
|
||||
.poll_send_to(cx, &zero_copy, &addr)
|
||||
.map_ok(|byte_length| json!(byte_length))
|
||||
.map_err(ErrBox::from)
|
||||
.map_err(AnyError::from)
|
||||
})
|
||||
.await
|
||||
}
|
||||
|
@ -211,13 +219,15 @@ async fn op_datagram_send(
|
|||
transport,
|
||||
transport_args: ArgsEnum::Unix(args),
|
||||
} if transport == "unixpacket" => {
|
||||
let address_path = net_unix::Path::new(&args.path);
|
||||
let address_path = Path::new(&args.path);
|
||||
cli_state.check_read(&address_path)?;
|
||||
let mut state = state.borrow_mut();
|
||||
let resource = state
|
||||
.resource_table
|
||||
.get_mut::<net_unix::UnixDatagramResource>(rid as u32)
|
||||
.ok_or_else(|| ErrBox::new("NotConnected", "Socket has been closed"))?;
|
||||
.ok_or_else(|| {
|
||||
custom_error("NotConnected", "Socket has been closed")
|
||||
})?;
|
||||
let socket = &mut resource.socket;
|
||||
let byte_length = socket
|
||||
.send_to(&zero_copy, &resource.local_addr.as_pathname().unwrap())
|
||||
|
@ -225,7 +235,7 @@ async fn op_datagram_send(
|
|||
|
||||
Ok(json!(byte_length))
|
||||
}
|
||||
_ => Err(ErrBox::type_error("Wrong argument format!")),
|
||||
_ => Err(type_error("Wrong argument format!")),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -240,7 +250,7 @@ async fn op_connect(
|
|||
state: Rc<RefCell<OpState>>,
|
||||
args: Value,
|
||||
_zero_copy: BufVec,
|
||||
) -> Result<Value, ErrBox> {
|
||||
) -> Result<Value, AnyError> {
|
||||
let cli_state = super::cli_state2(&state);
|
||||
match serde_json::from_value(args)? {
|
||||
ConnectArgs {
|
||||
|
@ -279,12 +289,11 @@ async fn op_connect(
|
|||
transport,
|
||||
transport_args: ArgsEnum::Unix(args),
|
||||
} if transport == "unix" => {
|
||||
let address_path = net_unix::Path::new(&args.path);
|
||||
let address_path = Path::new(&args.path);
|
||||
cli_state.check_unstable("Deno.connect");
|
||||
cli_state.check_read(&address_path)?;
|
||||
let path = args.path;
|
||||
let unix_stream =
|
||||
net_unix::UnixStream::connect(net_unix::Path::new(&path)).await?;
|
||||
let unix_stream = net_unix::UnixStream::connect(Path::new(&path)).await?;
|
||||
let local_addr = unix_stream.local_addr()?;
|
||||
let remote_addr = unix_stream.peer_addr()?;
|
||||
|
||||
|
@ -307,7 +316,7 @@ async fn op_connect(
|
|||
}
|
||||
}))
|
||||
}
|
||||
_ => Err(ErrBox::type_error("Wrong argument format!")),
|
||||
_ => Err(type_error("Wrong argument format!")),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -321,7 +330,7 @@ fn op_shutdown(
|
|||
state: &mut OpState,
|
||||
args: Value,
|
||||
_zero_copy: &mut [ZeroCopyBuf],
|
||||
) -> Result<Value, ErrBox> {
|
||||
) -> Result<Value, AnyError> {
|
||||
super::cli_state(state).check_unstable("Deno.shutdown");
|
||||
|
||||
let args: ShutdownArgs = serde_json::from_value(args)?;
|
||||
|
@ -338,7 +347,7 @@ fn op_shutdown(
|
|||
let resource_holder = state
|
||||
.resource_table
|
||||
.get_mut::<StreamResourceHolder>(rid)
|
||||
.ok_or_else(ErrBox::bad_resource_id)?;
|
||||
.ok_or_else(bad_resource_id)?;
|
||||
match resource_holder.resource {
|
||||
StreamResource::TcpStream(Some(ref mut stream)) => {
|
||||
TcpStream::shutdown(stream, shutdown_mode)?;
|
||||
|
@ -347,7 +356,7 @@ fn op_shutdown(
|
|||
StreamResource::UnixStream(ref mut stream) => {
|
||||
net_unix::UnixStream::shutdown(stream, shutdown_mode)?;
|
||||
}
|
||||
_ => return Err(ErrBox::bad_resource_id()),
|
||||
_ => return Err(bad_resource_id()),
|
||||
}
|
||||
|
||||
Ok(json!({}))
|
||||
|
@ -371,13 +380,13 @@ impl TcpListenerResource {
|
|||
/// can be notified when listener is closed.
|
||||
///
|
||||
/// Throws an error if another task is already tracked.
|
||||
pub fn track_task(&mut self, cx: &Context) -> Result<(), ErrBox> {
|
||||
pub fn track_task(&mut self, cx: &Context) -> Result<(), AnyError> {
|
||||
// Currently, we only allow tracking a single accept task for a listener.
|
||||
// This might be changed in the future with multiple workers.
|
||||
// Caveat: TcpListener by itself also only tracks an accept task at a time.
|
||||
// See https://github.com/tokio-rs/tokio/issues/846#issuecomment-454208883
|
||||
if self.waker.is_some() {
|
||||
return Err(ErrBox::new("Busy", "Another accept task is ongoing"));
|
||||
return Err(custom_error("Busy", "Another accept task is ongoing"));
|
||||
}
|
||||
|
||||
let waker = futures::task::AtomicWaker::new();
|
||||
|
@ -430,7 +439,7 @@ struct ListenArgs {
|
|||
fn listen_tcp(
|
||||
state: &mut OpState,
|
||||
addr: SocketAddr,
|
||||
) -> Result<(u32, SocketAddr), ErrBox> {
|
||||
) -> Result<(u32, SocketAddr), AnyError> {
|
||||
let std_listener = std::net::TcpListener::bind(&addr)?;
|
||||
let listener = TcpListener::from_std(std_listener)?;
|
||||
let local_addr = listener.local_addr()?;
|
||||
|
@ -449,7 +458,7 @@ fn listen_tcp(
|
|||
fn listen_udp(
|
||||
state: &mut OpState,
|
||||
addr: SocketAddr,
|
||||
) -> Result<(u32, SocketAddr), ErrBox> {
|
||||
) -> Result<(u32, SocketAddr), AnyError> {
|
||||
let std_socket = std::net::UdpSocket::bind(&addr)?;
|
||||
let socket = UdpSocket::from_std(std_socket)?;
|
||||
let local_addr = socket.local_addr()?;
|
||||
|
@ -465,7 +474,7 @@ fn op_listen(
|
|||
state: &mut OpState,
|
||||
args: Value,
|
||||
_zero_copy: &mut [ZeroCopyBuf],
|
||||
) -> Result<Value, ErrBox> {
|
||||
) -> Result<Value, AnyError> {
|
||||
let cli_state = super::cli_state(state);
|
||||
match serde_json::from_value(args)? {
|
||||
ListenArgs {
|
||||
|
@ -504,7 +513,7 @@ fn op_listen(
|
|||
transport,
|
||||
transport_args: ArgsEnum::Unix(args),
|
||||
} if transport == "unix" || transport == "unixpacket" => {
|
||||
let address_path = net_unix::Path::new(&args.path);
|
||||
let address_path = Path::new(&args.path);
|
||||
{
|
||||
if transport == "unix" {
|
||||
cli_state.check_unstable("Deno.listen");
|
||||
|
@ -534,6 +543,6 @@ fn op_listen(
|
|||
}))
|
||||
}
|
||||
#[cfg(unix)]
|
||||
_ => Err(ErrBox::type_error("Wrong argument format!")),
|
||||
_ => Err(type_error("Wrong argument format!")),
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,8 +2,9 @@ use crate::ops::io::StreamResource;
|
|||
use crate::ops::io::StreamResourceHolder;
|
||||
use crate::ops::net::AcceptArgs;
|
||||
use crate::ops::net::ReceiveArgs;
|
||||
use deno_core::error::bad_resource;
|
||||
use deno_core::error::AnyError;
|
||||
use deno_core::BufVec;
|
||||
use deno_core::ErrBox;
|
||||
use deno_core::OpState;
|
||||
use futures::future::poll_fn;
|
||||
use serde_derive::Deserialize;
|
||||
|
@ -11,7 +12,7 @@ use serde_json::Value;
|
|||
use std::cell::RefCell;
|
||||
use std::fs::remove_file;
|
||||
use std::os::unix;
|
||||
pub use std::path::Path;
|
||||
use std::path::Path;
|
||||
use std::rc::Rc;
|
||||
use std::task::Poll;
|
||||
use tokio::net::UnixDatagram;
|
||||
|
@ -36,7 +37,7 @@ pub(crate) async fn accept_unix(
|
|||
state: Rc<RefCell<OpState>>,
|
||||
args: AcceptArgs,
|
||||
_bufs: BufVec,
|
||||
) -> Result<Value, ErrBox> {
|
||||
) -> Result<Value, AnyError> {
|
||||
let rid = args.rid as u32;
|
||||
|
||||
let accept_fut = poll_fn(|cx| {
|
||||
|
@ -44,7 +45,7 @@ pub(crate) async fn accept_unix(
|
|||
let listener_resource = state
|
||||
.resource_table
|
||||
.get_mut::<UnixListenerResource>(rid)
|
||||
.ok_or_else(|| ErrBox::bad_resource("Listener has been closed"))?;
|
||||
.ok_or_else(|| bad_resource("Listener has been closed"))?;
|
||||
let listener = &mut listener_resource.listener;
|
||||
use futures::StreamExt;
|
||||
match listener.poll_next_unpin(cx) {
|
||||
|
@ -58,7 +59,7 @@ pub(crate) async fn accept_unix(
|
|||
Poll::Pending
|
||||
}
|
||||
}
|
||||
.map_err(ErrBox::from)
|
||||
.map_err(AnyError::from)
|
||||
});
|
||||
let unix_stream = accept_fut.await?;
|
||||
|
||||
|
@ -88,7 +89,7 @@ pub(crate) async fn receive_unix_packet(
|
|||
state: Rc<RefCell<OpState>>,
|
||||
args: ReceiveArgs,
|
||||
bufs: BufVec,
|
||||
) -> Result<Value, ErrBox> {
|
||||
) -> Result<Value, AnyError> {
|
||||
assert_eq!(bufs.len(), 1, "Invalid number of arguments");
|
||||
|
||||
let rid = args.rid as u32;
|
||||
|
@ -98,7 +99,7 @@ pub(crate) async fn receive_unix_packet(
|
|||
let resource = state
|
||||
.resource_table
|
||||
.get_mut::<UnixDatagramResource>(rid)
|
||||
.ok_or_else(|| ErrBox::bad_resource("Socket has been closed"))?;
|
||||
.ok_or_else(|| bad_resource("Socket has been closed"))?;
|
||||
let (size, remote_addr) = resource.socket.recv_from(&mut buf).await?;
|
||||
Ok(json!({
|
||||
"size": size,
|
||||
|
@ -112,7 +113,7 @@ pub(crate) async fn receive_unix_packet(
|
|||
pub fn listen_unix(
|
||||
state: &mut OpState,
|
||||
addr: &Path,
|
||||
) -> Result<(u32, unix::net::SocketAddr), ErrBox> {
|
||||
) -> Result<(u32, unix::net::SocketAddr), AnyError> {
|
||||
if addr.exists() {
|
||||
remove_file(&addr).unwrap();
|
||||
}
|
||||
|
@ -129,7 +130,7 @@ pub fn listen_unix(
|
|||
pub fn listen_unix_packet(
|
||||
state: &mut OpState,
|
||||
addr: &Path,
|
||||
) -> Result<(u32, unix::net::SocketAddr), ErrBox> {
|
||||
) -> Result<(u32, unix::net::SocketAddr), AnyError> {
|
||||
if addr.exists() {
|
||||
remove_file(&addr).unwrap();
|
||||
}
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
||||
|
||||
use deno_core::ErrBox;
|
||||
use deno_core::error::AnyError;
|
||||
use deno_core::OpState;
|
||||
use deno_core::ZeroCopyBuf;
|
||||
use serde_derive::Deserialize;
|
||||
|
@ -26,7 +26,7 @@ fn op_exec_path(
|
|||
state: &mut OpState,
|
||||
_args: Value,
|
||||
_zero_copy: &mut [ZeroCopyBuf],
|
||||
) -> Result<Value, ErrBox> {
|
||||
) -> Result<Value, AnyError> {
|
||||
let current_exe = env::current_exe().unwrap();
|
||||
let cli_state = super::cli_state(state);
|
||||
cli_state.check_read_blind(¤t_exe, "exec_path")?;
|
||||
|
@ -47,7 +47,7 @@ fn op_set_env(
|
|||
state: &mut OpState,
|
||||
args: Value,
|
||||
_zero_copy: &mut [ZeroCopyBuf],
|
||||
) -> Result<Value, ErrBox> {
|
||||
) -> Result<Value, AnyError> {
|
||||
let args: SetEnv = serde_json::from_value(args)?;
|
||||
let cli_state = super::cli_state(state);
|
||||
cli_state.check_env()?;
|
||||
|
@ -59,7 +59,7 @@ fn op_env(
|
|||
state: &mut OpState,
|
||||
_args: Value,
|
||||
_zero_copy: &mut [ZeroCopyBuf],
|
||||
) -> Result<Value, ErrBox> {
|
||||
) -> Result<Value, AnyError> {
|
||||
let cli_state = super::cli_state(state);
|
||||
cli_state.check_env()?;
|
||||
let v = env::vars().collect::<HashMap<String, String>>();
|
||||
|
@ -75,7 +75,7 @@ fn op_get_env(
|
|||
state: &mut OpState,
|
||||
args: Value,
|
||||
_zero_copy: &mut [ZeroCopyBuf],
|
||||
) -> Result<Value, ErrBox> {
|
||||
) -> Result<Value, AnyError> {
|
||||
let args: GetEnv = serde_json::from_value(args)?;
|
||||
let cli_state = super::cli_state(state);
|
||||
cli_state.check_env()?;
|
||||
|
@ -95,7 +95,7 @@ fn op_delete_env(
|
|||
state: &mut OpState,
|
||||
args: Value,
|
||||
_zero_copy: &mut [ZeroCopyBuf],
|
||||
) -> Result<Value, ErrBox> {
|
||||
) -> Result<Value, AnyError> {
|
||||
let args: DeleteEnv = serde_json::from_value(args)?;
|
||||
let cli_state = super::cli_state(state);
|
||||
cli_state.check_env()?;
|
||||
|
@ -112,7 +112,7 @@ fn op_exit(
|
|||
_state: &mut OpState,
|
||||
args: Value,
|
||||
_zero_copy: &mut [ZeroCopyBuf],
|
||||
) -> Result<Value, ErrBox> {
|
||||
) -> Result<Value, AnyError> {
|
||||
let args: Exit = serde_json::from_value(args)?;
|
||||
std::process::exit(args.code)
|
||||
}
|
||||
|
@ -121,7 +121,7 @@ fn op_loadavg(
|
|||
state: &mut OpState,
|
||||
_args: Value,
|
||||
_zero_copy: &mut [ZeroCopyBuf],
|
||||
) -> Result<Value, ErrBox> {
|
||||
) -> Result<Value, AnyError> {
|
||||
let cli_state = super::cli_state(state);
|
||||
cli_state.check_unstable("Deno.loadavg");
|
||||
cli_state.check_env()?;
|
||||
|
@ -135,7 +135,7 @@ fn op_hostname(
|
|||
state: &mut OpState,
|
||||
_args: Value,
|
||||
_zero_copy: &mut [ZeroCopyBuf],
|
||||
) -> Result<Value, ErrBox> {
|
||||
) -> Result<Value, AnyError> {
|
||||
let cli_state = super::cli_state(state);
|
||||
cli_state.check_unstable("Deno.hostname");
|
||||
cli_state.check_env()?;
|
||||
|
@ -147,7 +147,7 @@ fn op_os_release(
|
|||
state: &mut OpState,
|
||||
_args: Value,
|
||||
_zero_copy: &mut [ZeroCopyBuf],
|
||||
) -> Result<Value, ErrBox> {
|
||||
) -> Result<Value, AnyError> {
|
||||
let cli_state = super::cli_state(state);
|
||||
cli_state.check_unstable("Deno.osRelease");
|
||||
cli_state.check_env()?;
|
||||
|
@ -159,7 +159,7 @@ fn op_system_memory_info(
|
|||
state: &mut OpState,
|
||||
_args: Value,
|
||||
_zero_copy: &mut [ZeroCopyBuf],
|
||||
) -> Result<Value, ErrBox> {
|
||||
) -> Result<Value, AnyError> {
|
||||
let cli_state = super::cli_state(state);
|
||||
cli_state.check_unstable("Deno.systemMemoryInfo");
|
||||
cli_state.check_env()?;
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
||||
|
||||
use deno_core::ErrBox;
|
||||
use deno_core::error::custom_error;
|
||||
use deno_core::error::AnyError;
|
||||
use deno_core::OpState;
|
||||
use deno_core::ZeroCopyBuf;
|
||||
use serde_derive::Deserialize;
|
||||
|
@ -24,7 +25,7 @@ pub fn op_query_permission(
|
|||
state: &mut OpState,
|
||||
args: Value,
|
||||
_zero_copy: &mut [ZeroCopyBuf],
|
||||
) -> Result<Value, ErrBox> {
|
||||
) -> Result<Value, AnyError> {
|
||||
let args: PermissionArgs = serde_json::from_value(args)?;
|
||||
let cli_state = super::cli_state(state);
|
||||
let permissions = cli_state.permissions.borrow();
|
||||
|
@ -38,7 +39,7 @@ pub fn op_query_permission(
|
|||
"plugin" => permissions.query_plugin(),
|
||||
"hrtime" => permissions.query_hrtime(),
|
||||
n => {
|
||||
return Err(ErrBox::new(
|
||||
return Err(custom_error(
|
||||
"ReferenceError",
|
||||
format!("No such permission name: {}", n),
|
||||
))
|
||||
|
@ -51,7 +52,7 @@ pub fn op_revoke_permission(
|
|||
state: &mut OpState,
|
||||
args: Value,
|
||||
_zero_copy: &mut [ZeroCopyBuf],
|
||||
) -> Result<Value, ErrBox> {
|
||||
) -> Result<Value, AnyError> {
|
||||
let args: PermissionArgs = serde_json::from_value(args)?;
|
||||
let cli_state = super::cli_state(state);
|
||||
let mut permissions = cli_state.permissions.borrow_mut();
|
||||
|
@ -65,7 +66,7 @@ pub fn op_revoke_permission(
|
|||
"plugin" => permissions.revoke_plugin(),
|
||||
"hrtime" => permissions.revoke_hrtime(),
|
||||
n => {
|
||||
return Err(ErrBox::new(
|
||||
return Err(custom_error(
|
||||
"ReferenceError",
|
||||
format!("No such permission name: {}", n),
|
||||
))
|
||||
|
@ -78,7 +79,7 @@ pub fn op_request_permission(
|
|||
state: &mut OpState,
|
||||
args: Value,
|
||||
_zero_copy: &mut [ZeroCopyBuf],
|
||||
) -> Result<Value, ErrBox> {
|
||||
) -> Result<Value, AnyError> {
|
||||
let args: PermissionArgs = serde_json::from_value(args)?;
|
||||
let cli_state = super::cli_state(state);
|
||||
let permissions = &mut cli_state.permissions.borrow_mut();
|
||||
|
@ -92,7 +93,7 @@ pub fn op_request_permission(
|
|||
"plugin" => permissions.request_plugin(),
|
||||
"hrtime" => permissions.request_hrtime(),
|
||||
n => {
|
||||
return Err(ErrBox::new(
|
||||
return Err(custom_error(
|
||||
"ReferenceError",
|
||||
format!("No such permission name: {}", n),
|
||||
))
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
||||
|
||||
use crate::metrics::metrics_op;
|
||||
use deno_core::error::AnyError;
|
||||
use deno_core::plugin_api;
|
||||
use deno_core::BufVec;
|
||||
use deno_core::ErrBox;
|
||||
use deno_core::JsRuntime;
|
||||
use deno_core::Op;
|
||||
use deno_core::OpAsyncFuture;
|
||||
|
@ -35,7 +35,7 @@ pub fn op_open_plugin(
|
|||
state: &mut OpState,
|
||||
args: Value,
|
||||
_zero_copy: &mut [ZeroCopyBuf],
|
||||
) -> Result<Value, ErrBox> {
|
||||
) -> Result<Value, AnyError> {
|
||||
let args: OpenPluginArgs = serde_json::from_value(args)?;
|
||||
let filename = PathBuf::from(&args.filename);
|
||||
|
||||
|
|
|
@ -2,8 +2,10 @@
|
|||
|
||||
use super::io::{std_file_resource, StreamResource, StreamResourceHolder};
|
||||
use crate::signal::kill;
|
||||
use deno_core::error::bad_resource_id;
|
||||
use deno_core::error::type_error;
|
||||
use deno_core::error::AnyError;
|
||||
use deno_core::BufVec;
|
||||
use deno_core::ErrBox;
|
||||
use deno_core::OpState;
|
||||
use deno_core::ZeroCopyBuf;
|
||||
use futures::future::poll_fn;
|
||||
|
@ -23,19 +25,22 @@ pub fn init(rt: &mut deno_core::JsRuntime) {
|
|||
super::reg_json_sync(rt, "op_kill", op_kill);
|
||||
}
|
||||
|
||||
fn clone_file(state: &mut OpState, rid: u32) -> Result<std::fs::File, ErrBox> {
|
||||
fn clone_file(
|
||||
state: &mut OpState,
|
||||
rid: u32,
|
||||
) -> Result<std::fs::File, AnyError> {
|
||||
std_file_resource(state, rid, move |r| match r {
|
||||
Ok(std_file) => std_file.try_clone().map_err(ErrBox::from),
|
||||
Err(_) => Err(ErrBox::bad_resource_id()),
|
||||
Ok(std_file) => std_file.try_clone().map_err(AnyError::from),
|
||||
Err(_) => Err(bad_resource_id()),
|
||||
})
|
||||
}
|
||||
|
||||
fn subprocess_stdio_map(s: &str) -> Result<std::process::Stdio, ErrBox> {
|
||||
fn subprocess_stdio_map(s: &str) -> Result<std::process::Stdio, AnyError> {
|
||||
match s {
|
||||
"inherit" => Ok(std::process::Stdio::inherit()),
|
||||
"piped" => Ok(std::process::Stdio::piped()),
|
||||
"null" => Ok(std::process::Stdio::null()),
|
||||
_ => Err(ErrBox::type_error("Invalid resource for stdio")),
|
||||
_ => Err(type_error("Invalid resource for stdio")),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -61,7 +66,7 @@ fn op_run(
|
|||
state: &mut OpState,
|
||||
args: Value,
|
||||
_zero_copy: &mut [ZeroCopyBuf],
|
||||
) -> Result<Value, ErrBox> {
|
||||
) -> Result<Value, AnyError> {
|
||||
let run_args: RunArgs = serde_json::from_value(args)?;
|
||||
super::cli_state(state).check_run()?;
|
||||
|
||||
|
@ -169,7 +174,7 @@ async fn op_run_status(
|
|||
state: Rc<RefCell<OpState>>,
|
||||
args: Value,
|
||||
_zero_copy: BufVec,
|
||||
) -> Result<Value, ErrBox> {
|
||||
) -> Result<Value, AnyError> {
|
||||
let args: RunStatusArgs = serde_json::from_value(args)?;
|
||||
let rid = args.rid as u32;
|
||||
|
||||
|
@ -180,9 +185,9 @@ async fn op_run_status(
|
|||
let child_resource = state
|
||||
.resource_table
|
||||
.get_mut::<ChildResource>(rid)
|
||||
.ok_or_else(ErrBox::bad_resource_id)?;
|
||||
.ok_or_else(bad_resource_id)?;
|
||||
let child = &mut child_resource.child;
|
||||
child.poll_unpin(cx).map_err(ErrBox::from)
|
||||
child.poll_unpin(cx).map_err(AnyError::from)
|
||||
})
|
||||
.await?;
|
||||
|
||||
|
@ -215,7 +220,7 @@ fn op_kill(
|
|||
state: &mut OpState,
|
||||
args: Value,
|
||||
_zero_copy: &mut [ZeroCopyBuf],
|
||||
) -> Result<Value, ErrBox> {
|
||||
) -> Result<Value, AnyError> {
|
||||
let cli_state = super::cli_state(state);
|
||||
cli_state.check_unstable("Deno.kill");
|
||||
cli_state.check_run()?;
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
||||
|
||||
use deno_core::ErrBox;
|
||||
use deno_core::error::AnyError;
|
||||
use deno_core::OpState;
|
||||
use deno_core::ZeroCopyBuf;
|
||||
use rand::thread_rng;
|
||||
|
@ -15,7 +15,7 @@ fn op_get_random_values(
|
|||
state: &mut OpState,
|
||||
_args: Value,
|
||||
zero_copy: &mut [ZeroCopyBuf],
|
||||
) -> Result<Value, ErrBox> {
|
||||
) -> Result<Value, AnyError> {
|
||||
assert_eq!(zero_copy.len(), 1);
|
||||
let cli_state = super::cli_state(state);
|
||||
if let Some(seeded_rng) = &cli_state.seeded_rng {
|
||||
|
|
|
@ -2,8 +2,9 @@
|
|||
|
||||
use crate::repl;
|
||||
use crate::repl::Repl;
|
||||
use deno_core::error::bad_resource_id;
|
||||
use deno_core::error::AnyError;
|
||||
use deno_core::BufVec;
|
||||
use deno_core::ErrBox;
|
||||
use deno_core::OpState;
|
||||
use deno_core::ZeroCopyBuf;
|
||||
use serde_derive::Deserialize;
|
||||
|
@ -30,7 +31,7 @@ fn op_repl_start(
|
|||
state: &mut OpState,
|
||||
args: Value,
|
||||
_zero_copy: &mut [ZeroCopyBuf],
|
||||
) -> Result<Value, ErrBox> {
|
||||
) -> Result<Value, AnyError> {
|
||||
let args: ReplStartArgs = serde_json::from_value(args)?;
|
||||
debug!("op_repl_start {}", args.history_file);
|
||||
let history_path = {
|
||||
|
@ -53,7 +54,7 @@ async fn op_repl_readline(
|
|||
state: Rc<RefCell<OpState>>,
|
||||
args: Value,
|
||||
_zero_copy: BufVec,
|
||||
) -> Result<Value, ErrBox> {
|
||||
) -> Result<Value, AnyError> {
|
||||
let args: ReplReadlineArgs = serde_json::from_value(args)?;
|
||||
let rid = args.rid as u32;
|
||||
let prompt = args.prompt;
|
||||
|
@ -63,7 +64,7 @@ async fn op_repl_readline(
|
|||
let resource = state
|
||||
.resource_table
|
||||
.get::<ReplResource>(rid)
|
||||
.ok_or_else(ErrBox::bad_resource_id)?;
|
||||
.ok_or_else(bad_resource_id)?;
|
||||
resource.0.clone()
|
||||
};
|
||||
tokio::task::spawn_blocking(move || {
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
||||
|
||||
use deno_core::ErrBox;
|
||||
use deno_core::error::bad_resource_id;
|
||||
use deno_core::error::AnyError;
|
||||
use deno_core::OpState;
|
||||
use deno_core::ZeroCopyBuf;
|
||||
use serde_derive::Deserialize;
|
||||
|
@ -15,7 +16,7 @@ fn op_resources(
|
|||
state: &mut OpState,
|
||||
_args: Value,
|
||||
_zero_copy: &mut [ZeroCopyBuf],
|
||||
) -> Result<Value, ErrBox> {
|
||||
) -> Result<Value, AnyError> {
|
||||
let serialized_resources = state.resource_table.entries();
|
||||
Ok(json!(serialized_resources))
|
||||
}
|
||||
|
@ -25,7 +26,7 @@ fn op_close(
|
|||
state: &mut OpState,
|
||||
args: Value,
|
||||
_zero_copy: &mut [ZeroCopyBuf],
|
||||
) -> Result<Value, ErrBox> {
|
||||
) -> Result<Value, AnyError> {
|
||||
#[derive(Deserialize)]
|
||||
struct CloseArgs {
|
||||
rid: i32,
|
||||
|
@ -34,6 +35,6 @@ fn op_close(
|
|||
state
|
||||
.resource_table
|
||||
.close(args.rid as u32)
|
||||
.ok_or_else(ErrBox::bad_resource_id)?;
|
||||
.ok_or_else(bad_resource_id)?;
|
||||
Ok(json!({}))
|
||||
}
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
use crate::colors;
|
||||
use crate::version;
|
||||
use crate::DenoSubcommand;
|
||||
use deno_core::ErrBox;
|
||||
use deno_core::error::AnyError;
|
||||
use deno_core::ModuleSpecifier;
|
||||
use deno_core::OpState;
|
||||
use deno_core::ZeroCopyBuf;
|
||||
|
@ -20,7 +20,7 @@ fn op_start(
|
|||
state: &mut OpState,
|
||||
_args: Value,
|
||||
_zero_copy: &mut [ZeroCopyBuf],
|
||||
) -> Result<Value, ErrBox> {
|
||||
) -> Result<Value, AnyError> {
|
||||
let gs = &super::cli_state(state).global_state;
|
||||
|
||||
Ok(json!({
|
||||
|
@ -45,7 +45,7 @@ fn op_main_module(
|
|||
state: &mut OpState,
|
||||
_args: Value,
|
||||
_zero_copy: &mut [ZeroCopyBuf],
|
||||
) -> Result<Value, ErrBox> {
|
||||
) -> Result<Value, AnyError> {
|
||||
let cli_state = super::cli_state(state);
|
||||
let main = &cli_state.main_module.to_string();
|
||||
let main_url = ModuleSpecifier::resolve_url_or_path(&main)?;
|
||||
|
@ -60,7 +60,7 @@ fn op_metrics(
|
|||
state: &mut OpState,
|
||||
_args: Value,
|
||||
_zero_copy: &mut [ZeroCopyBuf],
|
||||
) -> Result<Value, ErrBox> {
|
||||
) -> Result<Value, AnyError> {
|
||||
let cli_state = super::cli_state(state);
|
||||
let m = &cli_state.metrics.borrow();
|
||||
|
||||
|
|
|
@ -4,8 +4,8 @@ use crate::futures::FutureExt;
|
|||
use crate::tsc::runtime_bundle;
|
||||
use crate::tsc::runtime_compile;
|
||||
use crate::tsc::runtime_transpile;
|
||||
use deno_core::error::AnyError;
|
||||
use deno_core::BufVec;
|
||||
use deno_core::ErrBox;
|
||||
use deno_core::OpState;
|
||||
use serde_derive::Deserialize;
|
||||
use serde_json::Value;
|
||||
|
@ -31,7 +31,7 @@ async fn op_compile(
|
|||
state: Rc<RefCell<OpState>>,
|
||||
args: Value,
|
||||
_data: BufVec,
|
||||
) -> Result<Value, ErrBox> {
|
||||
) -> Result<Value, AnyError> {
|
||||
let cli_state = super::cli_state2(&state);
|
||||
cli_state.check_unstable("Deno.compile");
|
||||
let args: CompileArgs = serde_json::from_value(args)?;
|
||||
|
@ -70,7 +70,7 @@ async fn op_transpile(
|
|||
state: Rc<RefCell<OpState>>,
|
||||
args: Value,
|
||||
_data: BufVec,
|
||||
) -> Result<Value, ErrBox> {
|
||||
) -> Result<Value, AnyError> {
|
||||
let cli_state = super::cli_state2(&state);
|
||||
cli_state.check_unstable("Deno.transpile");
|
||||
let args: TranspileArgs = serde_json::from_value(args)?;
|
||||
|
|
|
@ -1,13 +1,15 @@
|
|||
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
||||
|
||||
use deno_core::error::AnyError;
|
||||
use deno_core::BufVec;
|
||||
use deno_core::ErrBox;
|
||||
use deno_core::OpState;
|
||||
use deno_core::ZeroCopyBuf;
|
||||
use serde_json::Value;
|
||||
use std::cell::RefCell;
|
||||
use std::rc::Rc;
|
||||
|
||||
#[cfg(unix)]
|
||||
use deno_core::error::bad_resource_id;
|
||||
#[cfg(unix)]
|
||||
use futures::future::poll_fn;
|
||||
#[cfg(unix)]
|
||||
|
@ -45,7 +47,7 @@ fn op_signal_bind(
|
|||
state: &mut OpState,
|
||||
args: Value,
|
||||
_zero_copy: &mut [ZeroCopyBuf],
|
||||
) -> Result<Value, ErrBox> {
|
||||
) -> Result<Value, AnyError> {
|
||||
super::cli_state(state).check_unstable("Deno.signal");
|
||||
let args: BindSignalArgs = serde_json::from_value(args)?;
|
||||
let rid = state.resource_table.add(
|
||||
|
@ -65,7 +67,7 @@ async fn op_signal_poll(
|
|||
state: Rc<RefCell<OpState>>,
|
||||
args: Value,
|
||||
_zero_copy: BufVec,
|
||||
) -> Result<Value, ErrBox> {
|
||||
) -> Result<Value, AnyError> {
|
||||
super::cli_state2(&state).check_unstable("Deno.signal");
|
||||
let args: SignalArgs = serde_json::from_value(args)?;
|
||||
let rid = args.rid as u32;
|
||||
|
@ -89,7 +91,7 @@ pub fn op_signal_unbind(
|
|||
state: &mut OpState,
|
||||
args: Value,
|
||||
_zero_copy: &mut [ZeroCopyBuf],
|
||||
) -> Result<Value, ErrBox> {
|
||||
) -> Result<Value, AnyError> {
|
||||
super::cli_state(state).check_unstable("Deno.signal");
|
||||
let args: SignalArgs = serde_json::from_value(args)?;
|
||||
let rid = args.rid as u32;
|
||||
|
@ -104,7 +106,7 @@ pub fn op_signal_unbind(
|
|||
state
|
||||
.resource_table
|
||||
.close(rid)
|
||||
.ok_or_else(ErrBox::bad_resource_id)?;
|
||||
.ok_or_else(bad_resource_id)?;
|
||||
Ok(json!({}))
|
||||
}
|
||||
|
||||
|
@ -113,7 +115,7 @@ pub fn op_signal_bind(
|
|||
_state: &mut OpState,
|
||||
_args: Value,
|
||||
_zero_copy: &mut [ZeroCopyBuf],
|
||||
) -> Result<Value, ErrBox> {
|
||||
) -> Result<Value, AnyError> {
|
||||
unimplemented!();
|
||||
}
|
||||
|
||||
|
@ -122,7 +124,7 @@ fn op_signal_unbind(
|
|||
_state: &mut OpState,
|
||||
_args: Value,
|
||||
_zero_copy: &mut [ZeroCopyBuf],
|
||||
) -> Result<Value, ErrBox> {
|
||||
) -> Result<Value, AnyError> {
|
||||
unimplemented!();
|
||||
}
|
||||
|
||||
|
@ -131,6 +133,6 @@ async fn op_signal_poll(
|
|||
_state: Rc<RefCell<OpState>>,
|
||||
_args: Value,
|
||||
_zero_copy: BufVec,
|
||||
) -> Result<Value, ErrBox> {
|
||||
) -> Result<Value, AnyError> {
|
||||
unimplemented!();
|
||||
}
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
||||
|
||||
use deno_core::error::AnyError;
|
||||
use deno_core::BufVec;
|
||||
use deno_core::ErrBox;
|
||||
use deno_core::OpState;
|
||||
use deno_core::ZeroCopyBuf;
|
||||
use futures::future::FutureExt;
|
||||
|
@ -22,7 +22,7 @@ fn op_global_timer_stop(
|
|||
state: &mut OpState,
|
||||
_args: Value,
|
||||
_zero_copy: &mut [ZeroCopyBuf],
|
||||
) -> Result<Value, ErrBox> {
|
||||
) -> Result<Value, AnyError> {
|
||||
let cli_state = super::cli_state(state);
|
||||
cli_state.global_timer.borrow_mut().cancel();
|
||||
Ok(json!({}))
|
||||
|
@ -37,7 +37,7 @@ async fn op_global_timer(
|
|||
state: Rc<RefCell<OpState>>,
|
||||
args: Value,
|
||||
_zero_copy: BufVec,
|
||||
) -> Result<Value, ErrBox> {
|
||||
) -> Result<Value, AnyError> {
|
||||
let args: GlobalTimerArgs = serde_json::from_value(args)?;
|
||||
let val = args.timeout;
|
||||
|
||||
|
@ -61,7 +61,7 @@ fn op_now(
|
|||
state: &mut OpState,
|
||||
_args: Value,
|
||||
_zero_copy: &mut [ZeroCopyBuf],
|
||||
) -> Result<Value, ErrBox> {
|
||||
) -> Result<Value, AnyError> {
|
||||
let cli_state = super::cli_state(state);
|
||||
let seconds = cli_state.start_time.elapsed().as_secs();
|
||||
let mut subsec_nanos = cli_state.start_time.elapsed().subsec_nanos();
|
||||
|
|
|
@ -2,8 +2,11 @@
|
|||
|
||||
use super::io::{StreamResource, StreamResourceHolder};
|
||||
use crate::resolve_addr::resolve_addr;
|
||||
use deno_core::error::bad_resource;
|
||||
use deno_core::error::bad_resource_id;
|
||||
use deno_core::error::custom_error;
|
||||
use deno_core::error::AnyError;
|
||||
use deno_core::BufVec;
|
||||
use deno_core::ErrBox;
|
||||
use deno_core::OpState;
|
||||
use deno_core::ZeroCopyBuf;
|
||||
use futures::future::poll_fn;
|
||||
|
@ -59,7 +62,7 @@ async fn op_start_tls(
|
|||
state: Rc<RefCell<OpState>>,
|
||||
args: Value,
|
||||
_zero_copy: BufVec,
|
||||
) -> Result<Value, ErrBox> {
|
||||
) -> Result<Value, AnyError> {
|
||||
let args: StartTLSArgs = serde_json::from_value(args)?;
|
||||
let rid = args.rid as u32;
|
||||
let cert_file = args.cert_file.clone();
|
||||
|
@ -80,7 +83,7 @@ async fn op_start_tls(
|
|||
let mut state_ = state.borrow_mut();
|
||||
match state_.resource_table.remove::<StreamResourceHolder>(rid) {
|
||||
Some(resource) => *resource,
|
||||
None => return Err(ErrBox::bad_resource_id()),
|
||||
None => return Err(bad_resource_id()),
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -128,7 +131,7 @@ async fn op_start_tls(
|
|||
}
|
||||
}))
|
||||
} else {
|
||||
Err(ErrBox::bad_resource_id())
|
||||
Err(bad_resource_id())
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -136,7 +139,7 @@ async fn op_connect_tls(
|
|||
state: Rc<RefCell<OpState>>,
|
||||
args: Value,
|
||||
_zero_copy: BufVec,
|
||||
) -> Result<Value, ErrBox> {
|
||||
) -> Result<Value, AnyError> {
|
||||
let args: ConnectTLSArgs = serde_json::from_value(args)?;
|
||||
let cert_file = args.cert_file.clone();
|
||||
{
|
||||
|
@ -192,31 +195,31 @@ async fn op_connect_tls(
|
|||
}))
|
||||
}
|
||||
|
||||
fn load_certs(path: &str) -> Result<Vec<Certificate>, ErrBox> {
|
||||
fn load_certs(path: &str) -> Result<Vec<Certificate>, AnyError> {
|
||||
let cert_file = File::open(path)?;
|
||||
let reader = &mut BufReader::new(cert_file);
|
||||
|
||||
let certs = certs(reader)
|
||||
.map_err(|_| ErrBox::new("InvalidData", "Unable to decode certificate"))?;
|
||||
.map_err(|_| custom_error("InvalidData", "Unable to decode certificate"))?;
|
||||
|
||||
if certs.is_empty() {
|
||||
let e = ErrBox::new("InvalidData", "No certificates found in cert file");
|
||||
let e = custom_error("InvalidData", "No certificates found in cert file");
|
||||
return Err(e);
|
||||
}
|
||||
|
||||
Ok(certs)
|
||||
}
|
||||
|
||||
fn key_decode_err() -> ErrBox {
|
||||
ErrBox::new("InvalidData", "Unable to decode key")
|
||||
fn key_decode_err() -> AnyError {
|
||||
custom_error("InvalidData", "Unable to decode key")
|
||||
}
|
||||
|
||||
fn key_not_found_err() -> ErrBox {
|
||||
ErrBox::new("InvalidData", "No keys found in key file")
|
||||
fn key_not_found_err() -> AnyError {
|
||||
custom_error("InvalidData", "No keys found in key file")
|
||||
}
|
||||
|
||||
/// Starts with -----BEGIN RSA PRIVATE KEY-----
|
||||
fn load_rsa_keys(path: &str) -> Result<Vec<PrivateKey>, ErrBox> {
|
||||
fn load_rsa_keys(path: &str) -> Result<Vec<PrivateKey>, AnyError> {
|
||||
let key_file = File::open(path)?;
|
||||
let reader = &mut BufReader::new(key_file);
|
||||
let keys = rsa_private_keys(reader).map_err(|_| key_decode_err())?;
|
||||
|
@ -224,14 +227,14 @@ fn load_rsa_keys(path: &str) -> Result<Vec<PrivateKey>, ErrBox> {
|
|||
}
|
||||
|
||||
/// Starts with -----BEGIN PRIVATE KEY-----
|
||||
fn load_pkcs8_keys(path: &str) -> Result<Vec<PrivateKey>, ErrBox> {
|
||||
fn load_pkcs8_keys(path: &str) -> Result<Vec<PrivateKey>, AnyError> {
|
||||
let key_file = File::open(path)?;
|
||||
let reader = &mut BufReader::new(key_file);
|
||||
let keys = pkcs8_private_keys(reader).map_err(|_| key_decode_err())?;
|
||||
Ok(keys)
|
||||
}
|
||||
|
||||
fn load_keys(path: &str) -> Result<Vec<PrivateKey>, ErrBox> {
|
||||
fn load_keys(path: &str) -> Result<Vec<PrivateKey>, AnyError> {
|
||||
let path = path.to_string();
|
||||
let mut keys = load_rsa_keys(&path)?;
|
||||
|
||||
|
@ -265,13 +268,13 @@ impl TlsListenerResource {
|
|||
/// can be notified when listener is closed.
|
||||
///
|
||||
/// Throws an error if another task is already tracked.
|
||||
pub fn track_task(&mut self, cx: &Context) -> Result<(), ErrBox> {
|
||||
pub fn track_task(&mut self, cx: &Context) -> Result<(), AnyError> {
|
||||
// Currently, we only allow tracking a single accept task for a listener.
|
||||
// This might be changed in the future with multiple workers.
|
||||
// Caveat: TcpListener by itself also only tracks an accept task at a time.
|
||||
// See https://github.com/tokio-rs/tokio/issues/846#issuecomment-454208883
|
||||
if self.waker.is_some() {
|
||||
return Err(ErrBox::new("Busy", "Another accept task is ongoing"));
|
||||
return Err(custom_error("Busy", "Another accept task is ongoing"));
|
||||
}
|
||||
|
||||
let waker = futures::task::AtomicWaker::new();
|
||||
|
@ -308,7 +311,7 @@ fn op_listen_tls(
|
|||
state: &mut OpState,
|
||||
args: Value,
|
||||
_zero_copy: &mut [ZeroCopyBuf],
|
||||
) -> Result<Value, ErrBox> {
|
||||
) -> Result<Value, AnyError> {
|
||||
let args: ListenTlsArgs = serde_json::from_value(args)?;
|
||||
assert_eq!(args.transport, "tcp");
|
||||
|
||||
|
@ -359,7 +362,7 @@ async fn op_accept_tls(
|
|||
state: Rc<RefCell<OpState>>,
|
||||
args: Value,
|
||||
_zero_copy: BufVec,
|
||||
) -> Result<Value, ErrBox> {
|
||||
) -> Result<Value, AnyError> {
|
||||
let args: AcceptTlsArgs = serde_json::from_value(args)?;
|
||||
let rid = args.rid as u32;
|
||||
let accept_fut = poll_fn(|cx| {
|
||||
|
@ -367,9 +370,9 @@ async fn op_accept_tls(
|
|||
let listener_resource = state
|
||||
.resource_table
|
||||
.get_mut::<TlsListenerResource>(rid)
|
||||
.ok_or_else(|| ErrBox::bad_resource("Listener has been closed"))?;
|
||||
.ok_or_else(|| bad_resource("Listener has been closed"))?;
|
||||
let listener = &mut listener_resource.listener;
|
||||
match listener.poll_accept(cx).map_err(ErrBox::from) {
|
||||
match listener.poll_accept(cx).map_err(AnyError::from) {
|
||||
Poll::Ready(Ok((stream, addr))) => {
|
||||
listener_resource.untrack_task();
|
||||
Poll::Ready(Ok((stream, addr)))
|
||||
|
@ -392,7 +395,7 @@ async fn op_accept_tls(
|
|||
let resource = state_
|
||||
.resource_table
|
||||
.get::<TlsListenerResource>(rid)
|
||||
.ok_or_else(ErrBox::bad_resource_id)
|
||||
.ok_or_else(bad_resource_id)
|
||||
.expect("Can't find tls listener");
|
||||
resource.tls_acceptor.clone()
|
||||
};
|
||||
|
|
|
@ -1,15 +1,25 @@
|
|||
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
||||
|
||||
use super::io::std_file_resource;
|
||||
use super::io::{StreamResource, StreamResourceHolder};
|
||||
use deno_core::ErrBox;
|
||||
use super::io::StreamResource;
|
||||
use super::io::StreamResourceHolder;
|
||||
use deno_core::error::bad_resource_id;
|
||||
use deno_core::error::last_os_error;
|
||||
use deno_core::error::resource_unavailable;
|
||||
use deno_core::error::AnyError;
|
||||
use deno_core::OpState;
|
||||
use deno_core::ZeroCopyBuf;
|
||||
#[cfg(unix)]
|
||||
use nix::sys::termios;
|
||||
use serde_derive::{Deserialize, Serialize};
|
||||
use serde_derive::Deserialize;
|
||||
use serde_derive::Serialize;
|
||||
use serde_json::Value;
|
||||
|
||||
#[cfg(unix)]
|
||||
use deno_core::error::not_supported;
|
||||
#[cfg(unix)]
|
||||
use nix::sys::termios;
|
||||
|
||||
#[cfg(windows)]
|
||||
use deno_core::error::custom_error;
|
||||
#[cfg(windows)]
|
||||
use winapi::shared::minwindef::DWORD;
|
||||
#[cfg(windows)]
|
||||
|
@ -22,15 +32,15 @@ const RAW_MODE_MASK: DWORD = wincon::ENABLE_LINE_INPUT
|
|||
#[cfg(windows)]
|
||||
fn get_windows_handle(
|
||||
f: &std::fs::File,
|
||||
) -> Result<std::os::windows::io::RawHandle, ErrBox> {
|
||||
) -> Result<std::os::windows::io::RawHandle, AnyError> {
|
||||
use std::os::windows::io::AsRawHandle;
|
||||
use winapi::um::handleapi;
|
||||
|
||||
let handle = f.as_raw_handle();
|
||||
if handle == handleapi::INVALID_HANDLE_VALUE {
|
||||
return Err(ErrBox::last_os_error());
|
||||
return Err(last_os_error());
|
||||
} else if handle.is_null() {
|
||||
return Err(ErrBox::new("ReferenceError", "null handle"));
|
||||
return Err(custom_error("ReferenceError", "null handle"));
|
||||
}
|
||||
Ok(handle)
|
||||
}
|
||||
|
@ -51,7 +61,7 @@ fn op_set_raw(
|
|||
state: &mut OpState,
|
||||
args: Value,
|
||||
_zero_copy: &mut [ZeroCopyBuf],
|
||||
) -> Result<Value, ErrBox> {
|
||||
) -> Result<Value, AnyError> {
|
||||
super::cli_state(state).check_unstable("Deno.setRaw");
|
||||
|
||||
let args: SetRawArgs = serde_json::from_value(args)?;
|
||||
|
@ -72,7 +82,7 @@ fn op_set_raw(
|
|||
let resource_holder =
|
||||
state.resource_table.get_mut::<StreamResourceHolder>(rid);
|
||||
if resource_holder.is_none() {
|
||||
return Err(ErrBox::bad_resource_id());
|
||||
return Err(bad_resource_id());
|
||||
}
|
||||
let resource_holder = resource_holder.unwrap();
|
||||
|
||||
|
@ -97,28 +107,28 @@ fn op_set_raw(
|
|||
// some operation is in-flight.
|
||||
resource_holder.resource =
|
||||
StreamResource::FsFile(Some((tokio_file, metadata)));
|
||||
return Err(ErrBox::resource_unavailable());
|
||||
return Err(resource_unavailable());
|
||||
}
|
||||
}
|
||||
} else {
|
||||
return Err(ErrBox::resource_unavailable());
|
||||
return Err(resource_unavailable());
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
return Err(ErrBox::bad_resource_id());
|
||||
return Err(bad_resource_id());
|
||||
}
|
||||
};
|
||||
|
||||
if handle == handleapi::INVALID_HANDLE_VALUE {
|
||||
return Err(ErrBox::last_os_error());
|
||||
return Err(last_os_error());
|
||||
} else if handle.is_null() {
|
||||
return Err(ErrBox::new("ReferenceError", "null handle"));
|
||||
return Err(custom_error("ReferenceError", "null handle"));
|
||||
}
|
||||
let mut original_mode: DWORD = 0;
|
||||
if unsafe { consoleapi::GetConsoleMode(handle, &mut original_mode) }
|
||||
== FALSE
|
||||
{
|
||||
return Err(ErrBox::last_os_error());
|
||||
return Err(last_os_error());
|
||||
}
|
||||
let new_mode = if is_raw {
|
||||
original_mode & !RAW_MODE_MASK
|
||||
|
@ -126,7 +136,7 @@ fn op_set_raw(
|
|||
original_mode | RAW_MODE_MASK
|
||||
};
|
||||
if unsafe { consoleapi::SetConsoleMode(handle, new_mode) } == FALSE {
|
||||
return Err(ErrBox::last_os_error());
|
||||
return Err(last_os_error());
|
||||
}
|
||||
|
||||
Ok(json!({}))
|
||||
|
@ -138,7 +148,7 @@ fn op_set_raw(
|
|||
let resource_holder =
|
||||
state.resource_table.get_mut::<StreamResourceHolder>(rid);
|
||||
if resource_holder.is_none() {
|
||||
return Err(ErrBox::bad_resource_id());
|
||||
return Err(bad_resource_id());
|
||||
}
|
||||
|
||||
if is_raw {
|
||||
|
@ -150,11 +160,9 @@ fn op_set_raw(
|
|||
StreamResource::FsFile(Some((f, ref mut metadata))) => {
|
||||
(f.as_raw_fd(), &mut metadata.tty.mode)
|
||||
}
|
||||
StreamResource::FsFile(None) => {
|
||||
return Err(ErrBox::resource_unavailable())
|
||||
}
|
||||
StreamResource::FsFile(None) => return Err(resource_unavailable()),
|
||||
_ => {
|
||||
return Err(ErrBox::not_supported());
|
||||
return Err(not_supported());
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -195,10 +203,10 @@ fn op_set_raw(
|
|||
(f.as_raw_fd(), &mut metadata.tty.mode)
|
||||
}
|
||||
StreamResource::FsFile(None) => {
|
||||
return Err(ErrBox::resource_unavailable());
|
||||
return Err(resource_unavailable());
|
||||
}
|
||||
_ => {
|
||||
return Err(ErrBox::bad_resource_id());
|
||||
return Err(bad_resource_id());
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -220,7 +228,7 @@ fn op_isatty(
|
|||
state: &mut OpState,
|
||||
args: Value,
|
||||
_zero_copy: &mut [ZeroCopyBuf],
|
||||
) -> Result<Value, ErrBox> {
|
||||
) -> Result<Value, AnyError> {
|
||||
let args: IsattyArgs = serde_json::from_value(args)?;
|
||||
let rid = args.rid;
|
||||
|
||||
|
@ -264,7 +272,7 @@ fn op_console_size(
|
|||
state: &mut OpState,
|
||||
args: Value,
|
||||
_zero_copy: &mut [ZeroCopyBuf],
|
||||
) -> Result<Value, ErrBox> {
|
||||
) -> Result<Value, AnyError> {
|
||||
super::cli_state(state).check_unstable("Deno.consoleSize");
|
||||
|
||||
let args: ConsoleSizeArgs = serde_json::from_value(args)?;
|
||||
|
@ -286,7 +294,7 @@ fn op_console_size(
|
|||
&mut bufinfo,
|
||||
) == 0
|
||||
{
|
||||
return Err(ErrBox::last_os_error());
|
||||
return Err(last_os_error());
|
||||
}
|
||||
|
||||
Ok(ConsoleSize {
|
||||
|
@ -304,7 +312,7 @@ fn op_console_size(
|
|||
unsafe {
|
||||
let mut size: libc::winsize = std::mem::zeroed();
|
||||
if libc::ioctl(fd, libc::TIOCGWINSZ, &mut size as *mut _) != 0 {
|
||||
return Err(ErrBox::last_os_error());
|
||||
return Err(last_os_error());
|
||||
}
|
||||
|
||||
// TODO (caspervonb) return a tuple instead
|
||||
|
@ -315,7 +323,7 @@ fn op_console_size(
|
|||
}
|
||||
}
|
||||
}
|
||||
Err(_) => Err(ErrBox::bad_resource_id()),
|
||||
Err(_) => Err(bad_resource_id()),
|
||||
})?;
|
||||
|
||||
Ok(json!(size))
|
||||
|
|
|
@ -1,8 +1,10 @@
|
|||
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
||||
|
||||
use core::task::Poll;
|
||||
use deno_core::error::bad_resource_id;
|
||||
use deno_core::error::type_error;
|
||||
use deno_core::error::AnyError;
|
||||
use deno_core::BufVec;
|
||||
use deno_core::ErrBox;
|
||||
use deno_core::OpState;
|
||||
use futures::future::poll_fn;
|
||||
use futures::StreamExt;
|
||||
|
@ -19,9 +21,10 @@ use std::sync::Arc;
|
|||
use tokio::net::TcpStream;
|
||||
use tokio_rustls::{rustls::ClientConfig, TlsConnector};
|
||||
use tokio_tungstenite::stream::Stream as StreamSwitcher;
|
||||
use tokio_tungstenite::tungstenite::Error as TungsteniteError;
|
||||
use tokio_tungstenite::tungstenite::{
|
||||
handshake::client::Response, protocol::frame::coding::CloseCode,
|
||||
protocol::CloseFrame, Error, Message,
|
||||
protocol::CloseFrame, Message,
|
||||
};
|
||||
use tokio_tungstenite::{client_async, WebSocketStream};
|
||||
use webpki::DNSNameRef;
|
||||
|
@ -49,7 +52,7 @@ pub async fn op_ws_create(
|
|||
state: Rc<RefCell<OpState>>,
|
||||
args: Value,
|
||||
_bufs: BufVec,
|
||||
) -> Result<Value, ErrBox> {
|
||||
) -> Result<Value, AnyError> {
|
||||
let args: CreateArgs = serde_json::from_value(args)?;
|
||||
let ca_file = {
|
||||
let cli_state = super::cli_state2(&state);
|
||||
|
@ -70,7 +73,7 @@ pub async fn op_ws_create(
|
|||
});
|
||||
let addr = format!("{}:{}", domain, port);
|
||||
let try_socket = TcpStream::connect(addr).await;
|
||||
let tcp_socket = match try_socket.map_err(Error::Io) {
|
||||
let tcp_socket = match try_socket.map_err(TungsteniteError::Io) {
|
||||
Ok(socket) => socket,
|
||||
Err(_) => return Ok(json!({"success": false})),
|
||||
};
|
||||
|
@ -100,7 +103,7 @@ pub async fn op_ws_create(
|
|||
|
||||
let (stream, response): (WsStream, Response) =
|
||||
client_async(request, socket).await.map_err(|err| {
|
||||
ErrBox::type_error(format!(
|
||||
type_error(format!(
|
||||
"failed to connect to WebSocket: {}",
|
||||
err.to_string()
|
||||
))
|
||||
|
@ -140,7 +143,7 @@ pub async fn op_ws_send(
|
|||
state: Rc<RefCell<OpState>>,
|
||||
args: Value,
|
||||
bufs: BufVec,
|
||||
) -> Result<Value, ErrBox> {
|
||||
) -> Result<Value, AnyError> {
|
||||
let args: SendArgs = serde_json::from_value(args)?;
|
||||
|
||||
let mut maybe_msg = Some(match args.text {
|
||||
|
@ -154,11 +157,10 @@ pub async fn op_ws_send(
|
|||
let stream = state
|
||||
.resource_table
|
||||
.get_mut::<WsStream>(rid)
|
||||
.ok_or_else(ErrBox::bad_resource_id)?;
|
||||
.ok_or_else(bad_resource_id)?;
|
||||
|
||||
// TODO(ry) Handle errors below instead of unwrap.
|
||||
// Need to map tungstenite::error::Error to ErrBox.
|
||||
|
||||
// Need to map `TungsteniteError` to `AnyError`.
|
||||
ready!(stream.poll_ready_unpin(cx)).unwrap();
|
||||
if let Some(msg) = maybe_msg.take() {
|
||||
stream.start_send_unpin(msg).unwrap();
|
||||
|
@ -182,7 +184,7 @@ pub async fn op_ws_close(
|
|||
state: Rc<RefCell<OpState>>,
|
||||
args: Value,
|
||||
_bufs: BufVec,
|
||||
) -> Result<Value, ErrBox> {
|
||||
) -> Result<Value, AnyError> {
|
||||
let args: CloseArgs = serde_json::from_value(args)?;
|
||||
let rid = args.rid;
|
||||
let mut maybe_msg = Some(Message::Close(args.code.map(|c| CloseFrame {
|
||||
|
@ -198,11 +200,10 @@ pub async fn op_ws_close(
|
|||
let stream = state
|
||||
.resource_table
|
||||
.get_mut::<WsStream>(rid)
|
||||
.ok_or_else(ErrBox::bad_resource_id)?;
|
||||
.ok_or_else(bad_resource_id)?;
|
||||
|
||||
// TODO(ry) Handle errors below instead of unwrap.
|
||||
// Need to map tungstenite::error::Error to ErrBox.
|
||||
|
||||
// Need to map `TungsteniteError` to `AnyError`.
|
||||
ready!(stream.poll_ready_unpin(cx)).unwrap();
|
||||
if let Some(msg) = maybe_msg.take() {
|
||||
stream.start_send_unpin(msg).unwrap();
|
||||
|
@ -225,14 +226,14 @@ pub async fn op_ws_next_event(
|
|||
state: Rc<RefCell<OpState>>,
|
||||
args: Value,
|
||||
_bufs: BufVec,
|
||||
) -> Result<Value, ErrBox> {
|
||||
) -> Result<Value, AnyError> {
|
||||
let args: NextEventArgs = serde_json::from_value(args)?;
|
||||
poll_fn(move |cx| {
|
||||
let mut state = state.borrow_mut();
|
||||
let stream = state
|
||||
.resource_table
|
||||
.get_mut::<WsStream>(args.rid)
|
||||
.ok_or_else(ErrBox::bad_resource_id)?;
|
||||
.ok_or_else(bad_resource_id)?;
|
||||
stream
|
||||
.poll_next_unpin(cx)
|
||||
.map(|val| {
|
||||
|
|
|
@ -8,8 +8,8 @@ use crate::tokio_util::create_basic_runtime;
|
|||
use crate::web_worker::WebWorker;
|
||||
use crate::web_worker::WebWorkerHandle;
|
||||
use crate::worker::WorkerEvent;
|
||||
use deno_core::error::AnyError;
|
||||
use deno_core::BufVec;
|
||||
use deno_core::ErrBox;
|
||||
use deno_core::ModuleSpecifier;
|
||||
use deno_core::OpState;
|
||||
use deno_core::ZeroCopyBuf;
|
||||
|
@ -40,7 +40,7 @@ fn create_web_worker(
|
|||
permissions: Permissions,
|
||||
specifier: ModuleSpecifier,
|
||||
has_deno_namespace: bool,
|
||||
) -> Result<WebWorker, ErrBox> {
|
||||
) -> Result<WebWorker, AnyError> {
|
||||
let cli_state = crate::state::State::new_for_worker(
|
||||
global_state,
|
||||
Some(permissions),
|
||||
|
@ -84,10 +84,10 @@ fn run_worker_thread(
|
|||
specifier: ModuleSpecifier,
|
||||
has_deno_namespace: bool,
|
||||
maybe_source_code: Option<String>,
|
||||
) -> Result<(JoinHandle<()>, WebWorkerHandle), ErrBox> {
|
||||
) -> Result<(JoinHandle<()>, WebWorkerHandle), AnyError> {
|
||||
let global_state = global_state.clone();
|
||||
let (handle_sender, handle_receiver) =
|
||||
std::sync::mpsc::sync_channel::<Result<WebWorkerHandle, ErrBox>>(1);
|
||||
std::sync::mpsc::sync_channel::<Result<WebWorkerHandle, AnyError>>(1);
|
||||
|
||||
let builder =
|
||||
std::thread::Builder::new().name(format!("deno-worker-{}", worker_id));
|
||||
|
@ -177,7 +177,7 @@ fn op_create_worker(
|
|||
state: &mut OpState,
|
||||
args: Value,
|
||||
_data: &mut [ZeroCopyBuf],
|
||||
) -> Result<Value, ErrBox> {
|
||||
) -> Result<Value, AnyError> {
|
||||
let cli_state = super::cli_state(state);
|
||||
let args: CreateWorkerArgs = serde_json::from_value(args)?;
|
||||
|
||||
|
@ -229,7 +229,7 @@ fn op_host_terminate_worker(
|
|||
state: &mut OpState,
|
||||
args: Value,
|
||||
_data: &mut [ZeroCopyBuf],
|
||||
) -> Result<Value, ErrBox> {
|
||||
) -> Result<Value, AnyError> {
|
||||
let args: WorkerArgs = serde_json::from_value(args)?;
|
||||
let id = args.id as u32;
|
||||
let cli_state = super::cli_state(state);
|
||||
|
@ -298,7 +298,7 @@ async fn op_host_get_message(
|
|||
state: Rc<RefCell<OpState>>,
|
||||
args: Value,
|
||||
_zero_copy: BufVec,
|
||||
) -> Result<Value, ErrBox> {
|
||||
) -> Result<Value, AnyError> {
|
||||
let args: WorkerArgs = serde_json::from_value(args)?;
|
||||
let id = args.id as u32;
|
||||
let cli_state = super::cli_state2(&state);
|
||||
|
@ -347,7 +347,7 @@ fn op_host_post_message(
|
|||
state: &mut OpState,
|
||||
args: Value,
|
||||
data: &mut [ZeroCopyBuf],
|
||||
) -> Result<Value, ErrBox> {
|
||||
) -> Result<Value, AnyError> {
|
||||
assert_eq!(data.len(), 1, "Invalid number of arguments");
|
||||
let args: WorkerArgs = serde_json::from_value(args)?;
|
||||
let id = args.id as u32;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue