diff options
author | Divy Srivastava <dj.srivastava23@gmail.com> | 2022-08-18 17:35:02 +0530 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-08-18 17:35:02 +0530 |
commit | cd21cff29942f24ba7d38287186cce64d0e84e56 (patch) | |
tree | e663eff884526ee762ae9141a3cf5a0f6967a84e /runtime | |
parent | 0b0843e4a54d7c1ddf293ac1ccee2479b69a5ba9 (diff) |
feat(ext/flash): An optimized http/1.1 server (#15405)
Co-authored-by: Bartek IwaĆczuk <biwanczuk@gmail.com>
Co-authored-by: Ben Noordhuis <info@bnoordhuis.nl>
Co-authored-by: crowlkats <crowlkats@toaxl.com>
Co-authored-by: Ryan Dahl <ry@tinyclouds.org>
Diffstat (limited to 'runtime')
-rw-r--r-- | runtime/Cargo.toml | 2 | ||||
-rw-r--r-- | runtime/build.rs | 10 | ||||
-rw-r--r-- | runtime/js/90_deno_ns.js | 2 | ||||
-rw-r--r-- | runtime/ops/http.rs | 23 | ||||
-rw-r--r-- | runtime/ops/io.rs | 10 | ||||
-rw-r--r-- | runtime/permissions.rs | 9 | ||||
-rw-r--r-- | runtime/web_worker.rs | 1 | ||||
-rw-r--r-- | runtime/worker.rs | 1 |
8 files changed, 57 insertions, 1 deletions
diff --git a/runtime/Cargo.toml b/runtime/Cargo.toml index 49ec952e3..e009f04b7 100644 --- a/runtime/Cargo.toml +++ b/runtime/Cargo.toml @@ -28,6 +28,7 @@ deno_core = { version = "0.147.0", path = "../core" } deno_crypto = { version = "0.79.0", path = "../ext/crypto" } deno_fetch = { version = "0.88.0", path = "../ext/fetch" } deno_ffi = { version = "0.52.0", path = "../ext/ffi" } +deno_flash = { path = "../ext/flash" } deno_http = { version = "0.59.0", path = "../ext/http" } deno_net = { version = "0.57.0", path = "../ext/net" } deno_node = { version = "0.2.0", path = "../ext/node" } @@ -52,6 +53,7 @@ deno_core = { version = "0.147.0", path = "../core" } deno_crypto = { version = "0.79.0", path = "../ext/crypto" } deno_fetch = { version = "0.88.0", path = "../ext/fetch" } deno_ffi = { version = "0.52.0", path = "../ext/ffi" } +deno_flash = { path = "../ext/flash" } deno_http = { version = "0.59.0", path = "../ext/http" } deno_net = { version = "0.57.0", path = "../ext/net" } deno_node = { version = "0.2.0", path = "../ext/node" } diff --git a/runtime/build.rs b/runtime/build.rs index 0c9c0d0d0..0f3d7fb46 100644 --- a/runtime/build.rs +++ b/runtime/build.rs @@ -116,6 +116,15 @@ mod not_docs { } } + impl deno_flash::FlashPermissions for Permissions { + fn check_net<T: AsRef<str>>( + &mut self, + _host: &(T, Option<u16>), + ) -> Result<(), deno_core::error::AnyError> { + unreachable!("snapshotting!") + } + } + impl deno_net::NetPermissions for Permissions { fn check_net<T: AsRef<str>>( &mut self, @@ -165,6 +174,7 @@ mod not_docs { None, ), deno_http::init(), + deno_flash::init::<Permissions>(false), // No --unstable ]; let js_runtime = JsRuntime::new(RuntimeOptions { diff --git a/runtime/js/90_deno_ns.js b/runtime/js/90_deno_ns.js index f21b261db..b97c05556 100644 --- a/runtime/js/90_deno_ns.js +++ b/runtime/js/90_deno_ns.js @@ -153,5 +153,7 @@ spawnChild: __bootstrap.spawn.spawnChild, spawn: __bootstrap.spawn.spawn, spawnSync: __bootstrap.spawn.spawnSync, + serve: __bootstrap.flash.serve, + serveTls: __bootstrap.flash.serveTls, }; })(this); diff --git a/runtime/ops/http.rs b/runtime/ops/http.rs index 67d939976..7a58da361 100644 --- a/runtime/ops/http.rs +++ b/runtime/ops/http.rs @@ -27,7 +27,11 @@ use tokio::net::UnixStream; pub fn init() -> Extension { Extension::builder() - .ops(vec![op_http_start::decl(), op_http_upgrade::decl()]) + .ops(vec![ + op_http_start::decl(), + op_http_upgrade::decl(), + op_flash_upgrade_http::decl(), + ]) .build() } @@ -78,6 +82,23 @@ fn op_http_start( Err(bad_resource_id()) } +#[op] +fn op_flash_upgrade_http( + state: &mut OpState, + token: u32, + server_id: u32, +) -> Result<deno_core::ResourceId, AnyError> { + let flash_ctx = state.borrow_mut::<deno_flash::FlashContext>(); + let ctx = flash_ctx.servers.get_mut(&server_id).unwrap(); + + let tcp_stream = deno_flash::detach_socket(ctx, token)?; + Ok( + state + .resource_table + .add(TcpStreamResource::new(tcp_stream.into_split())), + ) +} + #[derive(Serialize)] #[serde(rename_all = "camelCase")] pub struct HttpUpgradeResult { diff --git a/runtime/ops/io.rs b/runtime/ops/io.rs index 2dcb9964a..900be1034 100644 --- a/runtime/ops/io.rs +++ b/runtime/ops/io.rs @@ -582,6 +582,16 @@ impl Resource for StdFileResource { fn write(self: Rc<Self>, buf: ZeroCopyBuf) -> AsyncResult<usize> { Box::pin(self.write(buf)) } + + #[cfg(unix)] + fn backing_fd(self: Rc<Self>) -> Option<std::os::unix::prelude::RawFd> { + use std::os::unix::io::AsRawFd; + self + .with_inner_and_metadata(move |std_file, _| { + Ok(std_file.with_file(|f| f.as_raw_fd())) + }) + .ok() + } } // override op_print to use the stdout and stderr in the resource table diff --git a/runtime/permissions.rs b/runtime/permissions.rs index 9602b94d5..0a5560833 100644 --- a/runtime/permissions.rs +++ b/runtime/permissions.rs @@ -1313,6 +1313,15 @@ impl Permissions { } } +impl deno_flash::FlashPermissions for Permissions { + fn check_net<T: AsRef<str>>( + &mut self, + host: &(T, Option<u16>), + ) -> Result<(), AnyError> { + self.net.check(host) + } +} + impl deno_net::NetPermissions for Permissions { fn check_net<T: AsRef<str>>( &mut self, diff --git a/runtime/web_worker.rs b/runtime/web_worker.rs index a4a2516a9..dd7f49aef 100644 --- a/runtime/web_worker.rs +++ b/runtime/web_worker.rs @@ -429,6 +429,7 @@ impl WebWorker { ops::signal::init(), ops::tty::init(), deno_http::init(), + deno_flash::init::<Permissions>(unstable), ops::http::init(), // Permissions ext (worker specific state) perm_ext, diff --git a/runtime/worker.rs b/runtime/worker.rs index 0336e8340..53d8c6377 100644 --- a/runtime/worker.rs +++ b/runtime/worker.rs @@ -170,6 +170,7 @@ impl MainWorker { ops::signal::init(), ops::tty::init(), deno_http::init(), + deno_flash::init::<Permissions>(unstable), ops::http::init(), // Permissions ext (worker specific state) perm_ext, |