Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

3.7 KB
Raw Blame
use std::sync::Arc;
use std::time::Duration;

use anyhow::{Context as _, Result, anyhow};
use cloud_api_types::websocket_protocol::{PROTOCOL_VERSION, PROTOCOL_VERSION_HEADER_NAME};
use futures::channel::mpsc::unbounded;
use futures::stream::{SplitSink, SplitStream};
use futures::{FutureExt as _, SinkExt as _, StreamExt as _, TryStreamExt as _};
use futures_lite::future::yield_now;
use gpui::{App, AppContext, Task};
use http_client::http::request;
use tokio_rustls::TlsConnector;
use yawc::frame::Frame;
use yawc::{TcpWebSocket, WebSocket};

use super::{MessageStream, forward_frame};
use crate::CloudApiClient;

const KEEPALIVE_INTERVAL: Duration = Duration::from_secs(1);

pub struct Connection {
    tx: SplitSink<TcpWebSocket, Frame>,
    rx: SplitStream<TcpWebSocket>,
}

impl Connection {
    fn new(websocket: TcpWebSocket) -> Self {
        let (tx, rx) = websocket.split();
        Self { tx, rx }
    }

    pub fn spawn(self, cx: &App) -> (MessageStream, Task<()>) {
        let mut tx = self.tx;
        let rx = self.rx.fuse();
        let (message_tx, message_rx) = unbounded();
        let executor = cx.background_executor().clone();
        let task = cx.background_spawn(async move {
            let keepalive_timer = executor.timer(KEEPALIVE_INTERVAL).fuse();
            futures::pin_mut!(keepalive_timer, rx);

            loop {
                futures::select_biased! {
                    _ = keepalive_timer => {
                        if tx.send(Frame::ping(Vec::new())).await.is_err() {
                            break;
                        }
                        keepalive_timer.set(executor.timer(KEEPALIVE_INTERVAL).fuse());
                    }
                    frame = rx.next() => {
                        let Some(frame) = frame else {
                            break;
                        };
                        if !forward_frame(frame, &message_tx) {
                            break;
                        }
                    }
                }

                yield_now().await;
            }
        });

        (message_rx.into_stream().boxed(), task)
    }
}

impl CloudApiClient {
    pub fn connect(self: &std::sync::Arc<Self>, cx: &App) -> Result<Task<Result<Connection>>> {
        let mut connect_url = self
            .http_client
            .build_zed_cloud_url("/client/users/connect")?;
        connect_url
            .set_scheme(match connect_url.scheme() {
                "https" => "wss",
                "http" => "ws",
                scheme => Err(anyhow!("invalid URL scheme: {scheme}"))?,
            })
            .map_err(|_| anyhow!("failed to set URL scheme"))?;

        let credentials = self.credentials.read();
        let credentials = credentials.as_ref().context("no credentials provided")?;
        let authorization_header = format!("{} {}", credentials.user_id, credentials.access_token);

        Ok(gpui_tokio::Tokio::spawn_result(cx, async move {
            // Use the same TLS configuration as the rest of Zed (the platform
            // certificate verifier), instead of yawc's default connector which only
            // trusts the bundled webpki roots and ignores the system trust store.
            let tls_connector = TlsConnector::from(Arc::new(http_client_tls::tls_config()));
            let websocket = WebSocket::connect(connect_url)
                .with_connector(tls_connector)
                .with_request(
                    request::Builder::new()
                        .header("Authorization", authorization_header)
                        .header(PROTOCOL_VERSION_HEADER_NAME, PROTOCOL_VERSION.to_string()),
                )
                .await?;

            Ok(Connection::new(websocket))
        }))
    }
}