diff --git a/ext/node/lib.rs b/ext/node/lib.rs index 50e444d76e..875186bd5e 100644 --- a/ext/node/lib.rs +++ b/ext/node/lib.rs @@ -351,9 +351,7 @@ deno_core::extension!(deno_node, ops::zlib::brotli::op_create_brotli_decompress, ops::zlib::brotli::op_brotli_decompress_stream, ops::zlib::brotli::op_brotli_decompress_stream_end, - ops::http::op_node_http_request

, ops::http::op_node_http_fetch_response_upgrade, - ops::http::op_node_http_fetch_send, ops::http::op_node_http_request_with_conn

, ops::http2::op_http2_connect, ops::http2::op_http2_poll_client_connection, diff --git a/ext/node/ops/http.rs b/ext/node/ops/http.rs index 1e778f7683..87d92c82e2 100644 --- a/ext/node/ops/http.rs +++ b/ext/node/ops/http.rs @@ -25,18 +25,12 @@ use deno_core::AsyncRefCell; use deno_core::AsyncResult; use deno_core::BufView; use deno_core::ByteString; -use deno_core::CancelFuture; use deno_core::CancelHandle; use deno_core::CancelTryFuture; use deno_core::OpState; use deno_core::RcRef; use deno_core::Resource; use deno_core::ResourceId; -use deno_fetch::get_or_create_client_from_state; -use deno_fetch::FetchCancelHandle; -use deno_fetch::FetchRequestResource; -use deno_fetch::FetchReturn; -use deno_fetch::HttpClientResource; use deno_fetch::ResBody; use deno_net::io::TcpStreamResource; use http::header::HeaderMap; @@ -52,125 +46,6 @@ use std::cmp::min; use tokio::io::AsyncReadExt; use tokio::io::AsyncWriteExt; -#[op2] -#[serde] -pub fn op_node_http_request

( - state: &mut OpState, - #[serde] method: ByteString, - #[string] url: String, - #[serde] headers: Vec<(ByteString, ByteString)>, - #[smi] client_rid: Option, - #[smi] body: Option, -) -> Result -where - P: crate::NodePermissions + 'static, -{ - let client = if let Some(rid) = client_rid { - let r = state.resource_table.get::(rid)?; - r.client.clone() - } else { - get_or_create_client_from_state(state)? - }; - - let method = Method::from_bytes(&method)?; - let mut url = Url::parse(&url)?; - let maybe_authority = deno_fetch::extract_authority(&mut url); - - { - let permissions = state.borrow_mut::

(); - permissions.check_net_url(&url, "ClientRequest")?; - } - - let mut header_map = HeaderMap::new(); - for (key, value) in headers { - let name = HeaderName::from_bytes(&key) - .map_err(|err| type_error(err.to_string()))?; - let v = HeaderValue::from_bytes(&value) - .map_err(|err| type_error(err.to_string()))?; - - header_map.append(name, v); - } - - let (body, con_len) = if let Some(body) = body { - ( - BodyExt::boxed(NodeHttpResourceToBodyAdapter::new( - state.resource_table.take_any(body)?, - )), - None, - ) - } else { - // POST and PUT requests should always have a 0 length content-length, - // if there is no body. https://fetch.spec.whatwg.org/#http-network-or-cache-fetch - let len = if matches!(method, Method::POST | Method::PUT) { - Some(0) - } else { - None - }; - ( - http_body_util::Empty::new() - .map_err(|never| match never {}) - .boxed(), - len, - ) - }; - - let mut request = http::Request::new(body); - *request.method_mut() = method.clone(); - *request.uri_mut() = url - .as_str() - .parse() - .map_err(|_| type_error("Invalid URL"))?; - *request.headers_mut() = header_map; - - if let Some((username, password)) = maybe_authority { - request.headers_mut().insert( - AUTHORIZATION, - deno_fetch::basic_auth(&username, password.as_deref()), - ); - } - if let Some(len) = con_len { - request.headers_mut().insert(CONTENT_LENGTH, len.into()); - } - - let cancel_handle = CancelHandle::new_rc(); - let cancel_handle_ = cancel_handle.clone(); - - let fut = async move { - client - .send(request) - .or_cancel(cancel_handle_) - .await - .map(|res| res.map_err(|err| type_error(err.to_string()))) - }; - - let request_rid = state.resource_table.add(FetchRequestResource { - future: Box::pin(fut), - url, - }); - - let cancel_handle_rid = - state.resource_table.add(FetchCancelHandle(cancel_handle)); - - Ok(FetchReturn { - request_rid, - cancel_handle_rid: Some(cancel_handle_rid), - }) -} - -#[derive(Default, Serialize)] -#[serde(rename_all = "camelCase")] -pub struct NodeHttpFetchResponse { - pub status: u16, - pub status_text: String, - pub headers: Vec<(ByteString, ByteString)>, - pub url: String, - pub response_rid: ResourceId, - pub content_length: Option, - pub remote_addr_ip: Option, - pub remote_addr_port: Option, - pub error: Option, -} - #[derive(Default, Serialize)] #[serde(rename_all = "camelCase")] pub struct NodeHttpResponse { @@ -307,7 +182,7 @@ where let response_rid = state .borrow_mut() .resource_table - .add(NodeHttpFetchResponseResource::new(res, content_length)); + .add(NodeHttpResponseResource::new(res, content_length)); Ok(NodeHttpResponse { status: status.as_u16(), @@ -322,82 +197,6 @@ where }) } -#[op2(async)] -#[serde] -pub async fn op_node_http_fetch_send( - state: Rc>, - #[smi] rid: ResourceId, -) -> Result { - let request = state - .borrow_mut() - .resource_table - .take::(rid)?; - - let request = Rc::try_unwrap(request) - .ok() - .expect("multiple op_node_http_fetch_send ongoing"); - - let res = match request.future.await { - Ok(Ok(res)) => res, - Ok(Err(err)) => { - // We're going to try and rescue the error cause from a stream and return it from this fetch. - // If any error in the chain is a hyper body error, return that as a special result we can use to - // reconstruct an error chain (eg: `new TypeError(..., { cause: new Error(...) })`). - // TODO(mmastrac): it would be a lot easier if we just passed a v8::Global through here instead - let mut err_ref: &dyn std::error::Error = err.as_ref(); - while let Some(err) = std::error::Error::source(err_ref) { - if let Some(err) = err.downcast_ref::() { - if let Some(err) = std::error::Error::source(err) { - return Ok(NodeHttpFetchResponse { - error: Some(err.to_string()), - ..Default::default() - }); - } - } - err_ref = err; - } - - return Err(type_error(err.to_string())); - } - Err(_) => return Err(type_error("request was cancelled")), - }; - - let status = res.status(); - let url = request.url.into(); - let mut res_headers = Vec::new(); - for (key, val) in res.headers().iter() { - res_headers.push((key.as_str().into(), val.as_bytes().into())); - } - - let content_length = hyper::body::Body::size_hint(res.body()).exact(); - let remote_addr = res - .extensions() - .get::() - .map(|info| info.remote_addr()); - let (remote_addr_ip, remote_addr_port) = if let Some(addr) = remote_addr { - (Some(addr.ip().to_string()), Some(addr.port())) - } else { - (None, None) - }; - - let response_rid = state - .borrow_mut() - .resource_table - .add(NodeHttpFetchResponseResource::new(res, content_length)); - - Ok(NodeHttpFetchResponse { - status: status.as_u16(), - status_text: status.canonical_reason().unwrap_or("").to_string(), - headers: res_headers, - url, - response_rid, - content_length, - remote_addr_ip, - remote_addr_port, - error: None, - }) -} - #[op2(async)] #[smi] pub async fn op_node_http_fetch_response_upgrade( @@ -407,7 +206,7 @@ pub async fn op_node_http_fetch_response_upgrade( let raw_response = state .borrow_mut() .resource_table - .take::(rid)?; + .take::(rid)?; let raw_response = Rc::try_unwrap(raw_response) .expect("Someone is holding onto NodeHttpFetchResponseResource"); @@ -523,13 +322,13 @@ impl Default for NodeHttpFetchResponseReader { } #[derive(Debug)] -pub struct NodeHttpFetchResponseResource { +pub struct NodeHttpResponseResource { pub response_reader: AsyncRefCell, pub cancel: CancelHandle, pub size: Option, } -impl NodeHttpFetchResponseResource { +impl NodeHttpResponseResource { pub fn new(response: http::Response, size: Option) -> Self { Self { response_reader: AsyncRefCell::new(NodeHttpFetchResponseReader::Start( @@ -551,7 +350,7 @@ impl NodeHttpFetchResponseResource { } } -impl Resource for NodeHttpFetchResponseResource { +impl Resource for NodeHttpResponseResource { fn name(&self) -> Cow { "fetchResponse".into() } diff --git a/ext/node/polyfills/http.ts b/ext/node/polyfills/http.ts index f920e8114b..878725ad92 100644 --- a/ext/node/polyfills/http.ts +++ b/ext/node/polyfills/http.ts @@ -6,8 +6,6 @@ import { core, primordials } from "ext:core/mod.js"; import { op_node_http_fetch_response_upgrade, - op_node_http_fetch_send, - op_node_http_request, op_node_http_request_with_conn, } from "ext:core/ops"; @@ -50,7 +48,9 @@ import { Agent, globalAgent } from "node:_http_agent"; import { urlToHttpOptions } from "ext:deno_node/internal/url.ts"; import { kEmptyObject, once } from "ext:deno_node/internal/util.mjs"; import { constants, TCP } from "ext:deno_node/internal_binding/tcp_wrap.ts"; -import { notImplemented, warnNotImplemented } from "ext:deno_node/_utils.ts"; +// import { notImplemented, warnNotImplemented } from "ext:deno_node/_utils.ts"; +import { notImplemented } from "ext:deno_node/_utils.ts"; + import { connResetException, ERR_HTTP_HEADERS_SENT, @@ -64,8 +64,8 @@ import { getTimerDuration } from "ext:deno_node/internal/timers.mjs"; import { serve, upgradeHttpRaw } from "ext:deno_http/00_serve.ts"; import { createHttpClient } from "ext:deno_fetch/22_http_client.js"; import { headersEntries } from "ext:deno_fetch/20_headers.js"; -import { timerId } from "ext:deno_web/03_abort_signal.js"; -import { clearTimeout as webClearTimeout } from "ext:deno_web/02_timers.js"; +// import { timerId } from "ext:deno_web/03_abort_signal.js"; +// import { clearTimeout as webClearTimeout } from "ext:deno_web/02_timers.js"; import { resourceForReadableStream } from "ext:deno_web/06_streams.js"; import { TcpConn } from "ext:deno_net/01_net.js"; import { STATUS_CODES } from "node:_http_server"; @@ -419,7 +419,6 @@ class ClientRequest extends OutgoingMessage { } _writeHeader() { - console.trace("writeHeader"); const url = this._createUrlStrFromOptions(); const headers = []; diff --git a/ext/node/polyfills/internal_binding/tcp_wrap.ts b/ext/node/polyfills/internal_binding/tcp_wrap.ts index d7e573e14b..1bd3294dd5 100644 --- a/ext/node/polyfills/internal_binding/tcp_wrap.ts +++ b/ext/node/polyfills/internal_binding/tcp_wrap.ts @@ -48,7 +48,6 @@ import { import { nextTick } from "ext:deno_node/_next_tick.ts"; import { core } from "ext:core/mod.js"; -import { consoleSize } from "../../../../runtime/js/40_tty.js"; const { internalRidSymbol } = core; /** The type of TCP socket. */ diff --git a/ext/node/polyfills/net.ts b/ext/node/polyfills/net.ts index f60e5caf72..52bed93c51 100644 --- a/ext/node/polyfills/net.ts +++ b/ext/node/polyfills/net.ts @@ -107,7 +107,6 @@ import type { DuplexOptions } from "ext:deno_node/_stream.d.ts"; import type { BufferEncoding } from "ext:deno_node/_global.d.ts"; import type { Abortable } from "ext:deno_node/_events.d.ts"; import { channel } from "node:diagnostics_channel"; -import console from "node:console"; let debug = debuglog("net", (fn) => { debug = fn;