Skip to content

Commit

Permalink
Add ServerInfo struct for node rendering (vercel/turborepo#5018)
Browse files Browse the repository at this point in the history
### Description

This adds a new serializable helper struct for Turbopack's server
information (`hostname`, `ip`, `port`, `protocol`).

### Testing Instructions

None

Re: WEB-1064
  • Loading branch information
jridgewell authored May 18, 2023
1 parent fc8e707 commit 8e6020b
Showing 1 changed file with 62 additions and 4 deletions.
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

0 comments on commit 8e6020b

Please sign in to comment.