mirror of
https://github.com/denoland/deno.git
synced 2024-11-25 15:29:32 -05:00
feat(core): streams (#12596)
This allows resources to be "streams" by implementing read/write/shutdown. These streams are implicit since their nature (read/write/duplex) isn't known until called, but we could easily add another method to explicitly tag resources as streams. `op_read/op_write/op_shutdown` are now builtin ops provided by `deno_core` Note: this current implementation is simple & straightforward but it results in an additional alloc per read/write call Closes #12556
This commit is contained in:
parent
1eae6c139e
commit
375ce63c63
18 changed files with 258 additions and 304 deletions
|
@ -16,7 +16,7 @@ unitTest(async function metrics() {
|
|||
assert(m1.bytesSentControl === 0);
|
||||
assert(m1.bytesSentData === 0);
|
||||
assert(m1.bytesReceived === 0);
|
||||
const m1OpWrite = m1.ops["op_write_async"];
|
||||
const m1OpWrite = m1.ops["op_write"];
|
||||
assert(m1OpWrite.opsDispatchedAsync > 0);
|
||||
assert(m1OpWrite.opsCompletedAsync > 0);
|
||||
assert(m1OpWrite.bytesSentControl === 0);
|
||||
|
@ -31,7 +31,7 @@ unitTest(async function metrics() {
|
|||
assert(m2.bytesSentControl === m1.bytesSentControl);
|
||||
assert(m2.bytesSentData === 0);
|
||||
assert(m2.bytesReceived === m1.bytesReceived);
|
||||
const m2OpWrite = m2.ops["op_write_async"];
|
||||
const m2OpWrite = m2.ops["op_write"];
|
||||
assert(m2OpWrite.opsDispatchedAsync > m1OpWrite.opsDispatchedAsync);
|
||||
assert(m2OpWrite.opsCompletedAsync > m1OpWrite.opsCompletedAsync);
|
||||
assert(m2OpWrite.bytesSentControl === m1OpWrite.bytesSentControl);
|
||||
|
|
|
@ -35,8 +35,7 @@ declare global {
|
|||
unitTest(async function opsAsyncBadResource() {
|
||||
try {
|
||||
const nonExistingRid = 9999;
|
||||
await Deno.core.opAsync(
|
||||
"op_read_async",
|
||||
await Deno.core.read(
|
||||
nonExistingRid,
|
||||
new Uint8Array(0),
|
||||
);
|
||||
|
|
|
@ -146,6 +146,18 @@
|
|||
return ObjectFromEntries(opSync("op_resources"));
|
||||
}
|
||||
|
||||
function read(rid, buf) {
|
||||
return opAsync("op_read", rid, buf);
|
||||
}
|
||||
|
||||
function write(rid, buf) {
|
||||
return opAsync("op_write", rid, buf);
|
||||
}
|
||||
|
||||
function shutdown(rid) {
|
||||
return opAsync("op_shutdown", rid);
|
||||
}
|
||||
|
||||
function close(rid) {
|
||||
opSync("op_close", rid);
|
||||
}
|
||||
|
@ -191,6 +203,9 @@
|
|||
ops,
|
||||
close,
|
||||
tryClose,
|
||||
read,
|
||||
write,
|
||||
shutdown,
|
||||
print,
|
||||
resources,
|
||||
metrics,
|
||||
|
|
|
@ -19,28 +19,11 @@ function accept(serverRid) {
|
|||
return Deno.core.opAsync("accept", serverRid);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads a packet from the rid, presumably an http request. data is ignored.
|
||||
* Returns bytes read.
|
||||
*/
|
||||
function read(rid, data) {
|
||||
return Deno.core.opAsync("read", rid, data);
|
||||
}
|
||||
|
||||
/** Writes a fixed HTTP response to the socket rid. Returns bytes written. */
|
||||
function write(rid, data) {
|
||||
return Deno.core.opAsync("write", rid, data);
|
||||
}
|
||||
|
||||
function close(rid) {
|
||||
Deno.core.opSync("close", rid);
|
||||
}
|
||||
|
||||
async function serve(rid) {
|
||||
try {
|
||||
while (true) {
|
||||
await read(rid, requestBuf);
|
||||
await write(rid, responseBuf);
|
||||
await Deno.core.read(rid, requestBuf);
|
||||
await Deno.core.write(rid, responseBuf);
|
||||
}
|
||||
} catch (e) {
|
||||
if (
|
||||
|
@ -50,7 +33,7 @@ async function serve(rid) {
|
|||
throw e;
|
||||
}
|
||||
}
|
||||
close(rid);
|
||||
Deno.core.close(rid);
|
||||
}
|
||||
|
||||
async function main() {
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
||||
use deno_core::error::AnyError;
|
||||
use deno_core::AsyncRefCell;
|
||||
use deno_core::AsyncResult;
|
||||
use deno_core::CancelHandle;
|
||||
use deno_core::CancelTryFuture;
|
||||
use deno_core::JsRuntime;
|
||||
|
@ -77,19 +78,33 @@ struct TcpStream {
|
|||
}
|
||||
|
||||
impl TcpStream {
|
||||
async fn read(self: Rc<Self>, buf: &mut [u8]) -> Result<usize, Error> {
|
||||
async fn read(
|
||||
self: Rc<Self>,
|
||||
mut buf: ZeroCopyBuf,
|
||||
) -> Result<usize, AnyError> {
|
||||
let mut rd = RcRef::map(&self, |r| &r.rd).borrow_mut().await;
|
||||
let cancel = RcRef::map(self, |r| &r.cancel);
|
||||
rd.read(buf).try_or_cancel(cancel).await
|
||||
rd.read(&mut buf)
|
||||
.try_or_cancel(cancel)
|
||||
.await
|
||||
.map_err(AnyError::from)
|
||||
}
|
||||
|
||||
async fn write(self: Rc<Self>, buf: &[u8]) -> Result<usize, Error> {
|
||||
async fn write(self: Rc<Self>, buf: ZeroCopyBuf) -> Result<usize, AnyError> {
|
||||
let mut wr = RcRef::map(self, |r| &r.wr).borrow_mut().await;
|
||||
wr.write(buf).await
|
||||
wr.write(&buf).await.map_err(AnyError::from)
|
||||
}
|
||||
}
|
||||
|
||||
impl Resource for TcpStream {
|
||||
fn read(self: Rc<Self>, buf: ZeroCopyBuf) -> AsyncResult<usize> {
|
||||
Box::pin(self.read(buf))
|
||||
}
|
||||
|
||||
fn write(self: Rc<Self>, buf: ZeroCopyBuf) -> AsyncResult<usize> {
|
||||
Box::pin(self.write(buf))
|
||||
}
|
||||
|
||||
fn close(self: Rc<Self>) {
|
||||
self.cancel.cancel()
|
||||
}
|
||||
|
@ -109,10 +124,7 @@ impl From<tokio::net::TcpStream> for TcpStream {
|
|||
fn create_js_runtime() -> JsRuntime {
|
||||
let mut runtime = JsRuntime::new(Default::default());
|
||||
runtime.register_op("listen", deno_core::op_sync(op_listen));
|
||||
runtime.register_op("close", deno_core::op_sync(op_close));
|
||||
runtime.register_op("accept", deno_core::op_async(op_accept));
|
||||
runtime.register_op("read", deno_core::op_async(op_read));
|
||||
runtime.register_op("write", deno_core::op_async(op_write));
|
||||
runtime.sync_ops_cache();
|
||||
runtime
|
||||
}
|
||||
|
@ -131,15 +143,6 @@ fn op_listen(
|
|||
Ok(rid)
|
||||
}
|
||||
|
||||
fn op_close(
|
||||
state: &mut OpState,
|
||||
rid: ResourceId,
|
||||
_: (),
|
||||
) -> Result<(), AnyError> {
|
||||
log::debug!("close rid={}", rid);
|
||||
state.resource_table.close(rid).map(|_| ())
|
||||
}
|
||||
|
||||
async fn op_accept(
|
||||
state: Rc<RefCell<OpState>>,
|
||||
rid: ResourceId,
|
||||
|
@ -153,30 +156,6 @@ async fn op_accept(
|
|||
Ok(rid)
|
||||
}
|
||||
|
||||
async fn op_read(
|
||||
state: Rc<RefCell<OpState>>,
|
||||
rid: ResourceId,
|
||||
mut buf: ZeroCopyBuf,
|
||||
) -> Result<usize, AnyError> {
|
||||
log::debug!("read rid={}", rid);
|
||||
|
||||
let stream = state.borrow().resource_table.get::<TcpStream>(rid)?;
|
||||
let nread = stream.read(&mut buf).await?;
|
||||
Ok(nread)
|
||||
}
|
||||
|
||||
async fn op_write(
|
||||
state: Rc<RefCell<OpState>>,
|
||||
rid: ResourceId,
|
||||
buf: ZeroCopyBuf,
|
||||
) -> Result<usize, AnyError> {
|
||||
log::debug!("write rid={}", rid);
|
||||
|
||||
let stream = state.borrow().resource_table.get::<TcpStream>(rid)?;
|
||||
let nwritten = stream.write(&buf).await?;
|
||||
Ok(nwritten)
|
||||
}
|
||||
|
||||
fn main() {
|
||||
log::set_logger(&Logger).unwrap();
|
||||
log::set_max_level(
|
||||
|
|
15
core/lib.deno_core.d.ts
vendored
15
core/lib.deno_core.d.ts
vendored
|
@ -45,6 +45,21 @@ declare namespace Deno {
|
|||
*/
|
||||
function tryClose(rid: number): void;
|
||||
|
||||
/**
|
||||
* Read from a (stream) resource that implements read()
|
||||
*/
|
||||
function read(rid: number, buf: Uint8Array): Promise<number>;
|
||||
|
||||
/**
|
||||
* Write to a (stream) resource that implements write()
|
||||
*/
|
||||
function write(rid: number, buf: Uint8Array): Promise<number>;
|
||||
|
||||
/**
|
||||
* Shutdown a resource
|
||||
*/
|
||||
function shutdown(rid: number): Promise<void>;
|
||||
|
||||
/** Get heap stats for current isolate/worker */
|
||||
function heapStats(): Record<string, number>;
|
||||
|
||||
|
|
|
@ -86,6 +86,7 @@ pub use crate::ops_json::op_async_unref;
|
|||
pub use crate::ops_json::op_sync;
|
||||
pub use crate::ops_json::void_op_async;
|
||||
pub use crate::ops_json::void_op_sync;
|
||||
pub use crate::resources::AsyncResult;
|
||||
pub use crate::resources::Resource;
|
||||
pub use crate::resources::ResourceId;
|
||||
pub use crate::resources::ResourceTable;
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
use crate::error::type_error;
|
||||
use crate::error::AnyError;
|
||||
use crate::include_js_files;
|
||||
use crate::op_async;
|
||||
use crate::op_sync;
|
||||
use crate::ops_metrics::OpMetrics;
|
||||
use crate::resources::ResourceId;
|
||||
|
@ -36,6 +37,10 @@ pub(crate) fn init_builtins() -> Extension {
|
|||
("op_metrics", op_sync(op_metrics)),
|
||||
("op_void_sync", void_op_sync()),
|
||||
("op_void_async", void_op_async()),
|
||||
// TODO(@AaronO): track IO metrics for builtin streams
|
||||
("op_read", op_async(op_read)),
|
||||
("op_write", op_async(op_write)),
|
||||
("op_shutdown", op_async(op_shutdown)),
|
||||
])
|
||||
.build()
|
||||
}
|
||||
|
@ -170,3 +175,30 @@ pub fn op_metrics(
|
|||
let per_op = state.tracker.per_op();
|
||||
Ok((aggregate, per_op))
|
||||
}
|
||||
|
||||
async fn op_read(
|
||||
state: Rc<RefCell<OpState>>,
|
||||
rid: ResourceId,
|
||||
buf: ZeroCopyBuf,
|
||||
) -> Result<u32, AnyError> {
|
||||
let resource = state.borrow().resource_table.get_any(rid)?;
|
||||
resource.read(buf).await.map(|n| n as u32)
|
||||
}
|
||||
|
||||
async fn op_write(
|
||||
state: Rc<RefCell<OpState>>,
|
||||
rid: ResourceId,
|
||||
buf: ZeroCopyBuf,
|
||||
) -> Result<u32, AnyError> {
|
||||
let resource = state.borrow().resource_table.get_any(rid)?;
|
||||
resource.write(buf).await.map(|n| n as u32)
|
||||
}
|
||||
|
||||
async fn op_shutdown(
|
||||
state: Rc<RefCell<OpState>>,
|
||||
rid: ResourceId,
|
||||
_: (),
|
||||
) -> Result<(), AnyError> {
|
||||
let resource = state.borrow().resource_table.get_any(rid)?;
|
||||
resource.shutdown().await
|
||||
}
|
||||
|
|
|
@ -7,17 +7,25 @@
|
|||
// file descriptor (hence the different name).
|
||||
|
||||
use crate::error::bad_resource_id;
|
||||
use crate::error::not_supported;
|
||||
use crate::error::AnyError;
|
||||
use crate::ZeroCopyBuf;
|
||||
use futures::Future;
|
||||
use std::any::type_name;
|
||||
use std::any::Any;
|
||||
use std::any::TypeId;
|
||||
use std::borrow::Cow;
|
||||
use std::collections::BTreeMap;
|
||||
use std::iter::Iterator;
|
||||
use std::pin::Pin;
|
||||
use std::rc::Rc;
|
||||
|
||||
/// Returned by resource read/write/shutdown methods
|
||||
pub type AsyncResult<T> = Pin<Box<dyn Future<Output = Result<T, AnyError>>>>;
|
||||
|
||||
/// All objects that can be store in the resource table should implement the
|
||||
/// `Resource` trait.
|
||||
/// TODO(@AaronO): investigate avoiding alloc on read/write/shutdown
|
||||
pub trait Resource: Any + 'static {
|
||||
/// Returns a string representation of the resource which is made available
|
||||
/// to JavaScript code through `op_resources`. The default implementation
|
||||
|
@ -27,6 +35,21 @@ pub trait Resource: Any + 'static {
|
|||
type_name::<Self>().into()
|
||||
}
|
||||
|
||||
/// Resources may implement `read()` to be a readable stream
|
||||
fn read(self: Rc<Self>, _buf: ZeroCopyBuf) -> AsyncResult<usize> {
|
||||
Box::pin(futures::future::err(not_supported()))
|
||||
}
|
||||
|
||||
/// Resources may implement `write()` to be a writable stream
|
||||
fn write(self: Rc<Self>, _buf: ZeroCopyBuf) -> AsyncResult<usize> {
|
||||
Box::pin(futures::future::err(not_supported()))
|
||||
}
|
||||
|
||||
/// Resources may implement `shutdown()` for graceful async shutdowns
|
||||
fn shutdown(self: Rc<Self>) -> AsyncResult<()> {
|
||||
Box::pin(futures::future::err(not_supported()))
|
||||
}
|
||||
|
||||
/// Resources may implement the `close()` trait method if they need to do
|
||||
/// resource specific clean-ups, such as cancelling pending futures, after a
|
||||
/// resource has been removed from the resource table.
|
||||
|
|
|
@ -73,24 +73,6 @@
|
|||
return core.opAsync("op_fetch_send", rid);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {number} rid
|
||||
* @param {Uint8Array} body
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
function opFetchRequestWrite(rid, body) {
|
||||
return core.opAsync("op_fetch_request_write", rid, body);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {number} rid
|
||||
* @param {Uint8Array} body
|
||||
* @returns {Promise<number>}
|
||||
*/
|
||||
function opFetchResponseRead(rid, body) {
|
||||
return core.opAsync("op_fetch_response_read", rid, body);
|
||||
}
|
||||
|
||||
// A finalization registry to clean up underlying fetch resources that are GC'ed.
|
||||
const RESOURCE_REGISTRY = new FinalizationRegistry((rid) => {
|
||||
core.tryClose(rid);
|
||||
|
@ -120,7 +102,8 @@
|
|||
// This is the largest possible size for a single packet on a TLS
|
||||
// stream.
|
||||
const chunk = new Uint8Array(16 * 1024 + 256);
|
||||
const read = await opFetchResponseRead(
|
||||
// TODO(@AaronO): switch to handle nulls if that's moved to core
|
||||
const read = await core.read(
|
||||
responseBodyRid,
|
||||
chunk,
|
||||
);
|
||||
|
@ -260,7 +243,7 @@
|
|||
}
|
||||
try {
|
||||
await PromisePrototypeCatch(
|
||||
opFetchRequestWrite(requestBodyRid, value),
|
||||
core.write(requestBodyRid, value),
|
||||
(err) => {
|
||||
if (terminator.aborted) return;
|
||||
throw err;
|
||||
|
|
|
@ -13,6 +13,7 @@ use deno_core::op_async;
|
|||
use deno_core::op_sync;
|
||||
use deno_core::url::Url;
|
||||
use deno_core::AsyncRefCell;
|
||||
use deno_core::AsyncResult;
|
||||
use deno_core::ByteString;
|
||||
use deno_core::CancelFuture;
|
||||
use deno_core::CancelHandle;
|
||||
|
@ -84,8 +85,6 @@ where
|
|||
.ops(vec![
|
||||
("op_fetch", op_sync(op_fetch::<FP, FH>)),
|
||||
("op_fetch_send", op_async(op_fetch_send)),
|
||||
("op_fetch_request_write", op_async(op_fetch_request_write)),
|
||||
("op_fetch_response_read", op_async(op_fetch_response_read)),
|
||||
(
|
||||
"op_fetch_custom_client",
|
||||
op_sync(op_fetch_custom_client::<FP>),
|
||||
|
@ -420,42 +419,6 @@ pub async fn op_fetch_send(
|
|||
})
|
||||
}
|
||||
|
||||
pub async fn op_fetch_request_write(
|
||||
state: Rc<RefCell<OpState>>,
|
||||
rid: ResourceId,
|
||||
data: ZeroCopyBuf,
|
||||
) -> Result<(), AnyError> {
|
||||
let buf = data.to_vec();
|
||||
|
||||
let resource = state
|
||||
.borrow()
|
||||
.resource_table
|
||||
.get::<FetchRequestBodyResource>(rid)?;
|
||||
let body = RcRef::map(&resource, |r| &r.body).borrow_mut().await;
|
||||
let cancel = RcRef::map(resource, |r| &r.cancel);
|
||||
body.send(Ok(buf)).or_cancel(cancel).await?.map_err(|_| {
|
||||
type_error("request body receiver not connected (request closed)")
|
||||
})?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn op_fetch_response_read(
|
||||
state: Rc<RefCell<OpState>>,
|
||||
rid: ResourceId,
|
||||
data: ZeroCopyBuf,
|
||||
) -> Result<usize, AnyError> {
|
||||
let resource = state
|
||||
.borrow()
|
||||
.resource_table
|
||||
.get::<FetchResponseBodyResource>(rid)?;
|
||||
let mut reader = RcRef::map(&resource, |r| &r.reader).borrow_mut().await;
|
||||
let cancel = RcRef::map(resource, |r| &r.cancel);
|
||||
let mut buf = data.clone();
|
||||
let read = reader.read(&mut buf).try_or_cancel(cancel).await?;
|
||||
Ok(read)
|
||||
}
|
||||
|
||||
type CancelableResponseResult = Result<Result<Response, AnyError>, Canceled>;
|
||||
|
||||
struct FetchRequestResource(
|
||||
|
@ -490,6 +453,20 @@ impl Resource for FetchRequestBodyResource {
|
|||
"fetchRequestBody".into()
|
||||
}
|
||||
|
||||
fn write(self: Rc<Self>, buf: ZeroCopyBuf) -> AsyncResult<usize> {
|
||||
Box::pin(async move {
|
||||
let data = buf.to_vec();
|
||||
let len = data.len();
|
||||
let body = RcRef::map(&self, |r| &r.body).borrow_mut().await;
|
||||
let cancel = RcRef::map(self, |r| &r.cancel);
|
||||
body.send(Ok(data)).or_cancel(cancel).await?.map_err(|_| {
|
||||
type_error("request body receiver not connected (request closed)")
|
||||
})?;
|
||||
|
||||
Ok(len)
|
||||
})
|
||||
}
|
||||
|
||||
fn close(self: Rc<Self>) {
|
||||
self.cancel.cancel()
|
||||
}
|
||||
|
@ -508,6 +485,15 @@ impl Resource for FetchResponseBodyResource {
|
|||
"fetchResponseBody".into()
|
||||
}
|
||||
|
||||
fn read(self: Rc<Self>, mut buf: ZeroCopyBuf) -> AsyncResult<usize> {
|
||||
Box::pin(async move {
|
||||
let mut reader = RcRef::map(&self, |r| &r.reader).borrow_mut().await;
|
||||
let cancel = RcRef::map(self, |r| &r.cancel);
|
||||
let read = reader.read(&mut buf).try_or_cancel(cancel).await?;
|
||||
Ok(read)
|
||||
})
|
||||
}
|
||||
|
||||
fn close(self: Rc<Self>) {
|
||||
self.cancel.cancel()
|
||||
}
|
||||
|
|
|
@ -18,16 +18,16 @@
|
|||
if (buffer.length === 0) {
|
||||
return 0;
|
||||
}
|
||||
const nread = await core.opAsync("op_net_read_async", rid, buffer);
|
||||
const nread = await core.read(rid, buffer);
|
||||
return nread === 0 ? null : nread;
|
||||
}
|
||||
|
||||
async function write(rid, data) {
|
||||
return await core.opAsync("op_net_write_async", rid, data);
|
||||
return await core.write(rid, data);
|
||||
}
|
||||
|
||||
function shutdown(rid) {
|
||||
return core.opAsync("op_net_shutdown", rid);
|
||||
return core.shutdown(rid);
|
||||
}
|
||||
|
||||
function opAccept(rid, transport) {
|
||||
|
|
|
@ -9,9 +9,6 @@ This crate depends on following extensions:
|
|||
|
||||
Following ops are provided:
|
||||
|
||||
- "op_net_read_async"
|
||||
- "op_net_write_async"
|
||||
- "op_net_shutdown"
|
||||
- "op_net_accept"
|
||||
- "op_net_connect"
|
||||
- "op_net_listen"
|
||||
|
|
123
ext/net/io.rs
123
ext/net/io.rs
|
@ -1,21 +1,15 @@
|
|||
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
||||
|
||||
use crate::ops_tls::TlsStreamResource;
|
||||
use deno_core::error::not_supported;
|
||||
use deno_core::error::AnyError;
|
||||
use deno_core::op_async;
|
||||
use deno_core::AsyncMutFuture;
|
||||
use deno_core::AsyncRefCell;
|
||||
use deno_core::AsyncResult;
|
||||
use deno_core::CancelHandle;
|
||||
use deno_core::CancelTryFuture;
|
||||
use deno_core::OpPair;
|
||||
use deno_core::OpState;
|
||||
use deno_core::RcRef;
|
||||
use deno_core::Resource;
|
||||
use deno_core::ResourceId;
|
||||
use deno_core::ZeroCopyBuf;
|
||||
use std::borrow::Cow;
|
||||
use std::cell::RefCell;
|
||||
use std::rc::Rc;
|
||||
use tokio::io::AsyncRead;
|
||||
use tokio::io::AsyncReadExt;
|
||||
|
@ -26,14 +20,6 @@ use tokio::net::tcp;
|
|||
#[cfg(unix)]
|
||||
use tokio::net::unix;
|
||||
|
||||
pub fn init() -> Vec<OpPair> {
|
||||
vec![
|
||||
("op_net_read_async", op_async(op_read_async)),
|
||||
("op_net_write_async", op_async(op_write_async)),
|
||||
("op_net_shutdown", op_async(op_shutdown)),
|
||||
]
|
||||
}
|
||||
|
||||
/// A full duplex resource has a read and write ends that are completely
|
||||
/// independent, like TCP/Unix sockets and TLS streams.
|
||||
#[derive(Debug)]
|
||||
|
@ -80,21 +66,27 @@ where
|
|||
}
|
||||
|
||||
pub async fn read(
|
||||
self: &Rc<Self>,
|
||||
buf: &mut [u8],
|
||||
self: Rc<Self>,
|
||||
mut buf: ZeroCopyBuf,
|
||||
) -> Result<usize, AnyError> {
|
||||
let mut rd = self.rd_borrow_mut().await;
|
||||
let nread = rd.read(buf).try_or_cancel(self.cancel_handle()).await?;
|
||||
let nread = rd
|
||||
.read(&mut buf)
|
||||
.try_or_cancel(self.cancel_handle())
|
||||
.await?;
|
||||
Ok(nread)
|
||||
}
|
||||
|
||||
pub async fn write(self: &Rc<Self>, buf: &[u8]) -> Result<usize, AnyError> {
|
||||
pub async fn write(
|
||||
self: Rc<Self>,
|
||||
buf: ZeroCopyBuf,
|
||||
) -> Result<usize, AnyError> {
|
||||
let mut wr = self.wr_borrow_mut().await;
|
||||
let nwritten = wr.write(buf).await?;
|
||||
let nwritten = wr.write(&buf).await?;
|
||||
Ok(nwritten)
|
||||
}
|
||||
|
||||
pub async fn shutdown(self: &Rc<Self>) -> Result<(), AnyError> {
|
||||
pub async fn shutdown(self: Rc<Self>) -> Result<(), AnyError> {
|
||||
let mut wr = self.wr_borrow_mut().await;
|
||||
wr.shutdown().await?;
|
||||
Ok(())
|
||||
|
@ -109,6 +101,18 @@ impl Resource for TcpStreamResource {
|
|||
"tcpStream".into()
|
||||
}
|
||||
|
||||
fn read(self: Rc<Self>, buf: ZeroCopyBuf) -> AsyncResult<usize> {
|
||||
Box::pin(self.read(buf))
|
||||
}
|
||||
|
||||
fn write(self: Rc<Self>, buf: ZeroCopyBuf) -> AsyncResult<usize> {
|
||||
Box::pin(self.write(buf))
|
||||
}
|
||||
|
||||
fn shutdown(self: Rc<Self>) -> AsyncResult<()> {
|
||||
Box::pin(self.shutdown())
|
||||
}
|
||||
|
||||
fn close(self: Rc<Self>) {
|
||||
self.cancel_read_ops();
|
||||
}
|
||||
|
@ -124,15 +128,18 @@ pub struct UnixStreamResource;
|
|||
#[cfg(not(unix))]
|
||||
impl UnixStreamResource {
|
||||
pub async fn read(
|
||||
self: &Rc<Self>,
|
||||
_buf: &mut [u8],
|
||||
self: Rc<Self>,
|
||||
_buf: ZeroCopyBuf,
|
||||
) -> Result<usize, AnyError> {
|
||||
unreachable!()
|
||||
}
|
||||
pub async fn write(self: &Rc<Self>, _buf: &[u8]) -> Result<usize, AnyError> {
|
||||
pub async fn write(
|
||||
self: Rc<Self>,
|
||||
_buf: ZeroCopyBuf,
|
||||
) -> Result<usize, AnyError> {
|
||||
unreachable!()
|
||||
}
|
||||
pub async fn shutdown(self: &Rc<Self>) -> Result<(), AnyError> {
|
||||
pub async fn shutdown(self: Rc<Self>) -> Result<(), AnyError> {
|
||||
unreachable!()
|
||||
}
|
||||
pub fn cancel_read_ops(&self) {
|
||||
|
@ -145,61 +152,19 @@ impl Resource for UnixStreamResource {
|
|||
"unixStream".into()
|
||||
}
|
||||
|
||||
fn read(self: Rc<Self>, buf: ZeroCopyBuf) -> AsyncResult<usize> {
|
||||
Box::pin(self.read(buf))
|
||||
}
|
||||
|
||||
fn write(self: Rc<Self>, buf: ZeroCopyBuf) -> AsyncResult<usize> {
|
||||
Box::pin(self.write(buf))
|
||||
}
|
||||
|
||||
fn shutdown(self: Rc<Self>) -> AsyncResult<()> {
|
||||
Box::pin(self.shutdown())
|
||||
}
|
||||
|
||||
fn close(self: Rc<Self>) {
|
||||
self.cancel_read_ops();
|
||||
}
|
||||
}
|
||||
|
||||
async fn op_read_async(
|
||||
state: Rc<RefCell<OpState>>,
|
||||
rid: ResourceId,
|
||||
mut buf: ZeroCopyBuf,
|
||||
) -> Result<u32, AnyError> {
|
||||
let resource = state.borrow().resource_table.get_any(rid)?;
|
||||
let nread = if let Some(s) = resource.downcast_rc::<TcpStreamResource>() {
|
||||
s.read(&mut buf).await?
|
||||
} else if let Some(s) = resource.downcast_rc::<TlsStreamResource>() {
|
||||
s.read(&mut buf).await?
|
||||
} else if let Some(s) = resource.downcast_rc::<UnixStreamResource>() {
|
||||
s.read(&mut buf).await?
|
||||
} else {
|
||||
return Err(not_supported());
|
||||
};
|
||||
Ok(nread as u32)
|
||||
}
|
||||
|
||||
async fn op_write_async(
|
||||
state: Rc<RefCell<OpState>>,
|
||||
rid: ResourceId,
|
||||
buf: ZeroCopyBuf,
|
||||
) -> Result<u32, AnyError> {
|
||||
let resource = state.borrow().resource_table.get_any(rid)?;
|
||||
let nwritten = if let Some(s) = resource.downcast_rc::<TcpStreamResource>() {
|
||||
s.write(&buf).await?
|
||||
} else if let Some(s) = resource.downcast_rc::<TlsStreamResource>() {
|
||||
s.write(&buf).await?
|
||||
} else if let Some(s) = resource.downcast_rc::<UnixStreamResource>() {
|
||||
s.write(&buf).await?
|
||||
} else {
|
||||
return Err(not_supported());
|
||||
};
|
||||
Ok(nwritten as u32)
|
||||
}
|
||||
|
||||
async fn op_shutdown(
|
||||
state: Rc<RefCell<OpState>>,
|
||||
rid: ResourceId,
|
||||
_: (),
|
||||
) -> Result<(), AnyError> {
|
||||
let resource = state.borrow().resource_table.get_any(rid)?;
|
||||
if let Some(s) = resource.downcast_rc::<TcpStreamResource>() {
|
||||
s.shutdown().await?;
|
||||
} else if let Some(s) = resource.downcast_rc::<TlsStreamResource>() {
|
||||
s.shutdown().await?;
|
||||
} else if let Some(s) = resource.downcast_rc::<UnixStreamResource>() {
|
||||
s.shutdown().await?;
|
||||
} else {
|
||||
return Err(not_supported());
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
@ -82,7 +82,6 @@ pub fn init<P: NetPermissions + 'static>(
|
|||
unsafely_ignore_certificate_errors: Option<Vec<String>>,
|
||||
) -> Extension {
|
||||
let mut ops_to_register = vec![];
|
||||
ops_to_register.extend(io::init());
|
||||
ops_to_register.extend(ops::init::<P>());
|
||||
ops_to_register.extend(ops_tls::init::<P>());
|
||||
Extension::builder()
|
||||
|
|
|
@ -28,6 +28,7 @@ use deno_core::op_async;
|
|||
use deno_core::op_sync;
|
||||
use deno_core::parking_lot::Mutex;
|
||||
use deno_core::AsyncRefCell;
|
||||
use deno_core::AsyncResult;
|
||||
use deno_core::CancelHandle;
|
||||
use deno_core::CancelTryFuture;
|
||||
use deno_core::OpPair;
|
||||
|
@ -35,6 +36,7 @@ use deno_core::OpState;
|
|||
use deno_core::RcRef;
|
||||
use deno_core::Resource;
|
||||
use deno_core::ResourceId;
|
||||
use deno_core::ZeroCopyBuf;
|
||||
use deno_tls::create_client_config;
|
||||
use deno_tls::load_certs;
|
||||
use deno_tls::load_private_keys;
|
||||
|
@ -715,24 +717,27 @@ impl TlsStreamResource {
|
|||
}
|
||||
|
||||
pub async fn read(
|
||||
self: &Rc<Self>,
|
||||
buf: &mut [u8],
|
||||
self: Rc<Self>,
|
||||
mut buf: ZeroCopyBuf,
|
||||
) -> Result<usize, AnyError> {
|
||||
let mut rd = RcRef::map(self, |r| &r.rd).borrow_mut().await;
|
||||
let cancel_handle = RcRef::map(self, |r| &r.cancel_handle);
|
||||
let nread = rd.read(buf).try_or_cancel(cancel_handle).await?;
|
||||
let mut rd = RcRef::map(&self, |r| &r.rd).borrow_mut().await;
|
||||
let cancel_handle = RcRef::map(&self, |r| &r.cancel_handle);
|
||||
let nread = rd.read(&mut buf).try_or_cancel(cancel_handle).await?;
|
||||
Ok(nread)
|
||||
}
|
||||
|
||||
pub async fn write(self: &Rc<Self>, buf: &[u8]) -> Result<usize, AnyError> {
|
||||
pub async fn write(
|
||||
self: Rc<Self>,
|
||||
buf: ZeroCopyBuf,
|
||||
) -> Result<usize, AnyError> {
|
||||
self.handshake().await?;
|
||||
let mut wr = RcRef::map(self, |r| &r.wr).borrow_mut().await;
|
||||
let nwritten = wr.write(buf).await?;
|
||||
let nwritten = wr.write(&buf).await?;
|
||||
wr.flush().await?;
|
||||
Ok(nwritten)
|
||||
}
|
||||
|
||||
pub async fn shutdown(self: &Rc<Self>) -> Result<(), AnyError> {
|
||||
pub async fn shutdown(self: Rc<Self>) -> Result<(), AnyError> {
|
||||
self.handshake().await?;
|
||||
let mut wr = RcRef::map(self, |r| &r.wr).borrow_mut().await;
|
||||
wr.shutdown().await?;
|
||||
|
@ -755,6 +760,18 @@ impl Resource for TlsStreamResource {
|
|||
"tlsStream".into()
|
||||
}
|
||||
|
||||
fn read(self: Rc<Self>, buf: ZeroCopyBuf) -> AsyncResult<usize> {
|
||||
Box::pin(self.read(buf))
|
||||
}
|
||||
|
||||
fn write(self: Rc<Self>, buf: ZeroCopyBuf) -> AsyncResult<usize> {
|
||||
Box::pin(self.write(buf))
|
||||
}
|
||||
|
||||
fn shutdown(self: Rc<Self>) -> AsyncResult<()> {
|
||||
Box::pin(self.shutdown())
|
||||
}
|
||||
|
||||
fn close(self: Rc<Self>) {
|
||||
self.cancel_handle.cancel();
|
||||
}
|
||||
|
|
|
@ -102,7 +102,7 @@
|
|||
return 0;
|
||||
}
|
||||
|
||||
const nread = await core.opAsync("op_read_async", rid, buffer);
|
||||
const nread = await core.read(rid, buffer);
|
||||
|
||||
return nread === 0 ? null : nread;
|
||||
}
|
||||
|
@ -111,8 +111,8 @@
|
|||
return core.opSync("op_write_sync", rid, data);
|
||||
}
|
||||
|
||||
async function write(rid, data) {
|
||||
return await core.opAsync("op_write_async", rid, data);
|
||||
function write(rid, data) {
|
||||
return core.write(rid, data);
|
||||
}
|
||||
|
||||
const READ_PER_ITER = 16 * 1024; // 16kb, see https://github.com/denoland/deno/issues/10157
|
||||
|
|
|
@ -3,10 +3,10 @@
|
|||
use deno_core::error::not_supported;
|
||||
use deno_core::error::resource_unavailable;
|
||||
use deno_core::error::AnyError;
|
||||
use deno_core::op_async;
|
||||
use deno_core::op_sync;
|
||||
use deno_core::AsyncMutFuture;
|
||||
use deno_core::AsyncRefCell;
|
||||
use deno_core::AsyncResult;
|
||||
use deno_core::CancelHandle;
|
||||
use deno_core::CancelTryFuture;
|
||||
use deno_core::Extension;
|
||||
|
@ -15,11 +15,7 @@ use deno_core::RcRef;
|
|||
use deno_core::Resource;
|
||||
use deno_core::ResourceId;
|
||||
use deno_core::ZeroCopyBuf;
|
||||
use deno_net::io::TcpStreamResource;
|
||||
use deno_net::io::UnixStreamResource;
|
||||
use deno_net::ops_tls::TlsStreamResource;
|
||||
use std::borrow::Cow;
|
||||
use std::cell::RefCell;
|
||||
use std::fs::File as StdFile;
|
||||
use std::io::Read;
|
||||
use std::io::Write;
|
||||
|
@ -70,11 +66,8 @@ lazy_static::lazy_static! {
|
|||
pub fn init() -> Extension {
|
||||
Extension::builder()
|
||||
.ops(vec![
|
||||
("op_read_async", op_async(op_read_async)),
|
||||
("op_write_async", op_async(op_write_async)),
|
||||
("op_read_sync", op_sync(op_read_sync)),
|
||||
("op_write_sync", op_sync(op_write_sync)),
|
||||
("op_shutdown", op_async(op_shutdown)),
|
||||
])
|
||||
.build()
|
||||
}
|
||||
|
@ -126,13 +119,13 @@ where
|
|||
RcRef::map(self, |r| &r.stream).borrow_mut()
|
||||
}
|
||||
|
||||
async fn write(self: &Rc<Self>, buf: &[u8]) -> Result<usize, AnyError> {
|
||||
async fn write(self: Rc<Self>, buf: ZeroCopyBuf) -> Result<usize, AnyError> {
|
||||
let mut stream = self.borrow_mut().await;
|
||||
let nwritten = stream.write(buf).await?;
|
||||
let nwritten = stream.write(&buf).await?;
|
||||
Ok(nwritten)
|
||||
}
|
||||
|
||||
async fn shutdown(self: &Rc<Self>) -> Result<(), AnyError> {
|
||||
async fn shutdown(self: Rc<Self>) -> Result<(), AnyError> {
|
||||
let mut stream = self.borrow_mut().await;
|
||||
stream.shutdown().await?;
|
||||
Ok(())
|
||||
|
@ -170,9 +163,15 @@ where
|
|||
self.cancel_handle.cancel()
|
||||
}
|
||||
|
||||
async fn read(self: &Rc<Self>, buf: &mut [u8]) -> Result<usize, AnyError> {
|
||||
async fn read(
|
||||
self: Rc<Self>,
|
||||
mut buf: ZeroCopyBuf,
|
||||
) -> Result<usize, AnyError> {
|
||||
let mut rd = self.borrow_mut().await;
|
||||
let nread = rd.read(buf).try_or_cancel(self.cancel_handle()).await?;
|
||||
let nread = rd
|
||||
.read(&mut buf)
|
||||
.try_or_cancel(self.cancel_handle())
|
||||
.await?;
|
||||
Ok(nread)
|
||||
}
|
||||
}
|
||||
|
@ -183,6 +182,14 @@ impl Resource for ChildStdinResource {
|
|||
fn name(&self) -> Cow<str> {
|
||||
"childStdin".into()
|
||||
}
|
||||
|
||||
fn write(self: Rc<Self>, buf: ZeroCopyBuf) -> AsyncResult<usize> {
|
||||
Box::pin(self.write(buf))
|
||||
}
|
||||
|
||||
fn shutdown(self: Rc<Self>) -> AsyncResult<()> {
|
||||
Box::pin(self.shutdown())
|
||||
}
|
||||
}
|
||||
|
||||
pub type ChildStdoutResource = ReadOnlyResource<process::ChildStdout>;
|
||||
|
@ -192,6 +199,10 @@ impl Resource for ChildStdoutResource {
|
|||
"childStdout".into()
|
||||
}
|
||||
|
||||
fn read(self: Rc<Self>, buf: ZeroCopyBuf) -> AsyncResult<usize> {
|
||||
Box::pin(self.read(buf))
|
||||
}
|
||||
|
||||
fn close(self: Rc<Self>) {
|
||||
self.cancel_read_ops();
|
||||
}
|
||||
|
@ -204,6 +215,10 @@ impl Resource for ChildStderrResource {
|
|||
"childStderr".into()
|
||||
}
|
||||
|
||||
fn read(self: Rc<Self>, buf: ZeroCopyBuf) -> AsyncResult<usize> {
|
||||
Box::pin(self.read(buf))
|
||||
}
|
||||
|
||||
fn close(self: Rc<Self>) {
|
||||
self.cancel_read_ops();
|
||||
}
|
||||
|
@ -240,24 +255,27 @@ impl StdFileResource {
|
|||
}
|
||||
}
|
||||
|
||||
async fn read(self: &Rc<Self>, buf: &mut [u8]) -> Result<usize, AnyError> {
|
||||
async fn read(
|
||||
self: Rc<Self>,
|
||||
mut buf: ZeroCopyBuf,
|
||||
) -> Result<usize, AnyError> {
|
||||
if self.fs_file.is_some() {
|
||||
let mut fs_file = RcRef::map(&*self, |r| r.fs_file.as_ref().unwrap())
|
||||
let mut fs_file = RcRef::map(&self, |r| r.fs_file.as_ref().unwrap())
|
||||
.borrow_mut()
|
||||
.await;
|
||||
let nwritten = fs_file.0.as_mut().unwrap().read(buf).await?;
|
||||
let nwritten = fs_file.0.as_mut().unwrap().read(&mut buf).await?;
|
||||
Ok(nwritten)
|
||||
} else {
|
||||
Err(resource_unavailable())
|
||||
}
|
||||
}
|
||||
|
||||
async fn write(self: &Rc<Self>, buf: &[u8]) -> Result<usize, AnyError> {
|
||||
async fn write(self: Rc<Self>, buf: ZeroCopyBuf) -> Result<usize, AnyError> {
|
||||
if self.fs_file.is_some() {
|
||||
let mut fs_file = RcRef::map(&*self, |r| r.fs_file.as_ref().unwrap())
|
||||
let mut fs_file = RcRef::map(&self, |r| r.fs_file.as_ref().unwrap())
|
||||
.borrow_mut()
|
||||
.await;
|
||||
let nwritten = fs_file.0.as_mut().unwrap().write(buf).await?;
|
||||
let nwritten = fs_file.0.as_mut().unwrap().write(&buf).await?;
|
||||
fs_file.0.as_mut().unwrap().flush().await?;
|
||||
Ok(nwritten)
|
||||
} else {
|
||||
|
@ -318,6 +336,14 @@ impl Resource for StdFileResource {
|
|||
self.name.as_str().into()
|
||||
}
|
||||
|
||||
fn read(self: Rc<Self>, buf: ZeroCopyBuf) -> AsyncResult<usize> {
|
||||
Box::pin(self.read(buf))
|
||||
}
|
||||
|
||||
fn write(self: Rc<Self>, buf: ZeroCopyBuf) -> AsyncResult<usize> {
|
||||
Box::pin(self.write(buf))
|
||||
}
|
||||
|
||||
fn close(self: Rc<Self>) {
|
||||
// TODO: do not cancel file I/O when file is writable.
|
||||
self.cancel.cancel()
|
||||
|
@ -338,30 +364,6 @@ fn op_read_sync(
|
|||
})
|
||||
}
|
||||
|
||||
async fn op_read_async(
|
||||
state: Rc<RefCell<OpState>>,
|
||||
rid: ResourceId,
|
||||
mut buf: ZeroCopyBuf,
|
||||
) -> Result<u32, AnyError> {
|
||||
let resource = state.borrow().resource_table.get_any(rid)?;
|
||||
let nread = if let Some(s) = resource.downcast_rc::<ChildStdoutResource>() {
|
||||
s.read(&mut buf).await?
|
||||
} else if let Some(s) = resource.downcast_rc::<ChildStderrResource>() {
|
||||
s.read(&mut buf).await?
|
||||
} else if let Some(s) = resource.downcast_rc::<TcpStreamResource>() {
|
||||
s.read(&mut buf).await?
|
||||
} else if let Some(s) = resource.downcast_rc::<TlsStreamResource>() {
|
||||
s.read(&mut buf).await?
|
||||
} else if let Some(s) = resource.downcast_rc::<UnixStreamResource>() {
|
||||
s.read(&mut buf).await?
|
||||
} else if let Some(s) = resource.downcast_rc::<StdFileResource>() {
|
||||
s.read(&mut buf).await?
|
||||
} else {
|
||||
return Err(not_supported());
|
||||
};
|
||||
Ok(nread as u32)
|
||||
}
|
||||
|
||||
fn op_write_sync(
|
||||
state: &mut OpState,
|
||||
rid: ResourceId,
|
||||
|
@ -375,45 +377,3 @@ fn op_write_sync(
|
|||
Err(_) => Err(not_supported()),
|
||||
})
|
||||
}
|
||||
|
||||
async fn op_write_async(
|
||||
state: Rc<RefCell<OpState>>,
|
||||
rid: ResourceId,
|
||||
buf: ZeroCopyBuf,
|
||||
) -> Result<u32, AnyError> {
|
||||
let resource = state.borrow().resource_table.get_any(rid)?;
|
||||
let nwritten = if let Some(s) = resource.downcast_rc::<ChildStdinResource>() {
|
||||
s.write(&buf).await?
|
||||
} else if let Some(s) = resource.downcast_rc::<TcpStreamResource>() {
|
||||
s.write(&buf).await?
|
||||
} else if let Some(s) = resource.downcast_rc::<TlsStreamResource>() {
|
||||
s.write(&buf).await?
|
||||
} else if let Some(s) = resource.downcast_rc::<UnixStreamResource>() {
|
||||
s.write(&buf).await?
|
||||
} else if let Some(s) = resource.downcast_rc::<StdFileResource>() {
|
||||
s.write(&buf).await?
|
||||
} else {
|
||||
return Err(not_supported());
|
||||
};
|
||||
Ok(nwritten as u32)
|
||||
}
|
||||
|
||||
async fn op_shutdown(
|
||||
state: Rc<RefCell<OpState>>,
|
||||
rid: ResourceId,
|
||||
_: (),
|
||||
) -> Result<(), AnyError> {
|
||||
let resource = state.borrow().resource_table.get_any(rid)?;
|
||||
if let Some(s) = resource.downcast_rc::<ChildStdinResource>() {
|
||||
s.shutdown().await?;
|
||||
} else if let Some(s) = resource.downcast_rc::<TcpStreamResource>() {
|
||||
s.shutdown().await?;
|
||||
} else if let Some(s) = resource.downcast_rc::<TlsStreamResource>() {
|
||||
s.shutdown().await?;
|
||||
} else if let Some(s) = resource.downcast_rc::<UnixStreamResource>() {
|
||||
s.shutdown().await?;
|
||||
} else {
|
||||
return Err(not_supported());
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue