Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ServerInfo struct for node rendering #5018

Merged
merged 1 commit into from
May 18, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 62 additions & 4 deletions crates/turbopack-core/src/environment.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use std::{
fmt,
net::SocketAddr,
process::{Command, Stdio},
str::FromStr,
};

use anyhow::{anyhow, Context, Result};
use anyhow::{anyhow, bail, Context, Result};
use serde::{Deserialize, Serialize};
use swc_core::ecma::preset_env::{Version, Versions};
use turbo_tasks::{
primitives::{BoolVc, OptionStringVc, StringVc, StringsVc},
Expand Down Expand Up @@ -55,10 +57,10 @@ impl ServerAddr {
.hostname()
.zip(self.port())
.context("expected some server address")?;
let protocol = Protocol::from(port);
Ok(match port {
80 => format!("http://{hostname}"),
443 => format!("https://{hostname}"),
_ => format!("http://{hostname}:{port}"),
80 | 443 => format!("{protocol}://{hostname}"),
_ => format!("{protocol}://{hostname}:{port}"),
})
}
}
Expand All @@ -71,6 +73,62 @@ impl ServerAddrVc {
}
}

/// A simple serializable structure meant to carry information about Turbopack's
/// server to node rendering processes.
#[derive(Debug, Serialize, Deserialize)]
pub struct ServerInfo {
pub ip: String,
pub port: u16,

/// The protocol, either `http` or `https`
pub protocol: Protocol,

/// A formatted hostname (eg, "localhost") or the IP address of the server
pub hostname: String,
}

#[derive(Copy, Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum Protocol {
HTTP,
HTTPS,
}

impl From<u16> for Protocol {
fn from(value: u16) -> Self {
match value {
443 => Self::HTTPS,
_ => Self::HTTP,
}
}
}

impl fmt::Display for Protocol {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::HTTP => f.write_str("http"),
Self::HTTPS => f.write_str("https"),
}
}
}

impl TryFrom<&ServerAddr> for ServerInfo {
type Error = anyhow::Error;

fn try_from(addr: &ServerAddr) -> Result<Self> {
if addr.0.is_none() {
bail!("cannot unwrap ServerAddr");
};
let port = addr.port().unwrap();
Ok(ServerInfo {
ip: addr.ip().unwrap(),
hostname: addr.hostname().unwrap(),
port,
protocol: Protocol::from(port),
})
}
}

#[turbo_tasks::value]
#[derive(Default)]
pub enum Rendering {
Expand Down