diff options
author | Luca Casonato <hello@lcas.dev> | 2021-07-12 12:44:49 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-07-12 12:44:49 +0200 |
commit | f649960f87a408124b5b0d6f55f3be7f5724a4e7 (patch) | |
tree | 0a303a918828f602623c64c50a912645df55b772 /runtime | |
parent | 6460eadaa178b3a9d09d04205977e4f659fe8fff (diff) |
refactor: deno_http op crate (#11335)
Diffstat (limited to 'runtime')
-rw-r--r-- | runtime/Cargo.toml | 2 | ||||
-rw-r--r-- | runtime/build.rs | 1 | ||||
-rw-r--r-- | runtime/js/40_http.js | 14 | ||||
-rw-r--r-- | runtime/lib.rs | 1 | ||||
-rw-r--r-- | runtime/ops/http.rs | 48 | ||||
-rw-r--r-- | runtime/ops/mod.rs | 1 | ||||
-rw-r--r-- | runtime/web_worker.rs | 2 | ||||
-rw-r--r-- | runtime/worker.rs | 2 |
8 files changed, 71 insertions, 0 deletions
diff --git a/runtime/Cargo.toml b/runtime/Cargo.toml index a9ad9b384..65c9aa091 100644 --- a/runtime/Cargo.toml +++ b/runtime/Cargo.toml @@ -23,6 +23,7 @@ deno_console = { version = "0.10.1", path = "../extensions/console" } deno_core = { version = "0.92.0", path = "../core" } deno_crypto = { version = "0.24.1", path = "../extensions/crypto" } deno_fetch = { version = "0.32.1", path = "../extensions/fetch" } +deno_http = { version = "0.1.0", path = "../extensions/http" } deno_net = { version = "0.1.0", path = "../extensions/net" } deno_timers = { version = "0.8.1", path = "../extensions/timers" } deno_url = { version = "0.10.1", path = "../extensions/url" } @@ -42,6 +43,7 @@ deno_console = { version = "0.10.1", path = "../extensions/console" } deno_core = { version = "0.92.0", path = "../core" } deno_crypto = { version = "0.24.1", path = "../extensions/crypto" } deno_fetch = { version = "0.32.1", path = "../extensions/fetch" } +deno_http = { version = "0.1.0", path = "../extensions/http" } deno_net = { version = "0.1.0", path = "../extensions/net" } deno_timers = { version = "0.8.1", path = "../extensions/timers" } deno_url = { version = "0.10.1", path = "../extensions/url" } diff --git a/runtime/build.rs b/runtime/build.rs index 52433b70e..8c5772c67 100644 --- a/runtime/build.rs +++ b/runtime/build.rs @@ -60,6 +60,7 @@ fn create_runtime_snapshot(snapshot_path: &Path, files: Vec<PathBuf>) { false, // No --unstable. ), deno_net::init::<deno_net::NoNetPermissions>(false), // No --unstable. + deno_http::init(), ]; let js_runtime = JsRuntime::new(RuntimeOptions { diff --git a/runtime/js/40_http.js b/runtime/js/40_http.js new file mode 100644 index 000000000..d68b4f45c --- /dev/null +++ b/runtime/js/40_http.js @@ -0,0 +1,14 @@ +// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. +"use strict"; + +((window) => { + const core = window.__bootstrap.core; + const { HttpConn } = window.__bootstrap.http; + + function serveHttp(conn) { + const rid = core.opSync("op_http_start", conn.rid); + return new HttpConn(rid); + } + + window.__bootstrap.http.serveHttp = serveHttp; +})(globalThis); diff --git a/runtime/lib.rs b/runtime/lib.rs index aa95aefbc..2358899d4 100644 --- a/runtime/lib.rs +++ b/runtime/lib.rs @@ -4,6 +4,7 @@ pub use deno_broadcast_channel; pub use deno_console; pub use deno_crypto; pub use deno_fetch; +pub use deno_http; pub use deno_net; pub use deno_timers; pub use deno_url; diff --git a/runtime/ops/http.rs b/runtime/ops/http.rs new file mode 100644 index 000000000..5b156fc11 --- /dev/null +++ b/runtime/ops/http.rs @@ -0,0 +1,48 @@ +use std::rc::Rc; + +use deno_core::error::bad_resource_id; +use deno_core::error::AnyError; +use deno_core::op_sync; +use deno_core::Extension; +use deno_core::OpState; +use deno_core::ResourceId; +use deno_net::io::TcpStreamResource; +use deno_net::io::TlsStreamResource; + +pub fn init() -> Extension { + Extension::builder() + .ops(vec![("op_http_start", op_sync(op_http_start))]) + .build() +} + +fn op_http_start( + state: &mut OpState, + tcp_stream_rid: ResourceId, + _: (), +) -> Result<ResourceId, AnyError> { + if let Some(resource_rc) = state + .resource_table + .take::<TcpStreamResource>(tcp_stream_rid) + { + let resource = Rc::try_unwrap(resource_rc) + .expect("Only a single use of this resource should happen"); + let (read_half, write_half) = resource.into_inner(); + let tcp_stream = read_half.reunite(write_half)?; + let addr = tcp_stream.local_addr()?; + return deno_http::start_http(state, tcp_stream, addr, "http"); + } + + if let Some(resource_rc) = state + .resource_table + .take::<TlsStreamResource>(tcp_stream_rid) + { + let resource = Rc::try_unwrap(resource_rc) + .expect("Only a single use of this resource should happen"); + let (read_half, write_half) = resource.into_inner(); + let tls_stream = read_half.reunite(write_half); + let addr = tls_stream.get_ref().0.local_addr()?; + return deno_http::start_http(state, tls_stream, addr, "https"); + } + + Err(bad_resource_id()) +} diff --git a/runtime/ops/mod.rs b/runtime/ops/mod.rs index c94020780..82ccf0506 100644 --- a/runtime/ops/mod.rs +++ b/runtime/ops/mod.rs @@ -2,6 +2,7 @@ pub mod fs; pub mod fs_events; +pub mod http; pub mod io; pub mod os; pub mod permissions; diff --git a/runtime/web_worker.rs b/runtime/web_worker.rs index 57a8142be..acafb086b 100644 --- a/runtime/web_worker.rs +++ b/runtime/web_worker.rs @@ -339,6 +339,8 @@ impl WebWorker { ops::process::init(), ops::signal::init(), ops::tty::init(), + deno_http::init(), + ops::http::init(), ops::io::init_stdio(), ] } else { diff --git a/runtime/worker.rs b/runtime/worker.rs index 91810449d..41e63914d 100644 --- a/runtime/worker.rs +++ b/runtime/worker.rs @@ -130,6 +130,8 @@ impl MainWorker { ops::process::init(), ops::signal::init(), ops::tty::init(), + deno_http::init(), + ops::http::init(), // Permissions ext (worker specific state) perm_ext, ]; |