File size: 10,934 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 |
use std::{
ops::ControlFlow,
pin::Pin,
task::{Context, Poll},
};
use anyhow::{Context as _, Error, Result};
use futures::{SinkExt, prelude::*, ready, stream::FusedStream};
use hyper::{HeaderMap, Uri, upgrade::Upgraded};
use hyper_tungstenite::{HyperWebsocket, WebSocketStream, tungstenite::Message};
use pin_project_lite::pin_project;
use tokio::select;
use tokio_stream::StreamMap;
use tracing::{Level, instrument};
use turbo_tasks::{
NonLocalValue, OperationVc, ReadRef, TransientInstance, TurboTasksApi, Vc, trace::TraceRawVcs,
};
use turbo_tasks_fs::json::parse_json_with_source_context;
use turbopack_core::{error::PrettyPrintError, issue::IssueReporter, version::Update};
use turbopack_ecmascript_hmr_protocol::{
ClientMessage, ClientUpdateInstruction, Issue, ResourceIdentifier,
};
use crate::{
SourceProvider,
source::{
Body,
request::SourceRequest,
resolve::{ResolveSourceRequestResult, resolve_source_request},
},
update::stream::{GetContentFn, UpdateStream, UpdateStreamItem},
};
/// A server that listens for updates and sends them to connected clients.
pub(crate) struct UpdateServer<P: SourceProvider> {
source_provider: P,
#[allow(dead_code)]
issue_reporter: Vc<Box<dyn IssueReporter>>,
}
impl<P> UpdateServer<P>
where
P: SourceProvider + NonLocalValue + TraceRawVcs + Clone + Send + Sync,
{
/// Create a new update server with the given websocket and content source.
pub fn new(source_provider: P, issue_reporter: Vc<Box<dyn IssueReporter>>) -> Self {
Self {
source_provider,
issue_reporter,
}
}
/// Run the update server loop.
pub fn run(self, tt: &dyn TurboTasksApi, ws: HyperWebsocket) {
tt.run_once_process(Box::pin(async move {
if let Err(err) = self.run_internal(ws).await {
println!("[UpdateServer]: error {err:#}");
}
Ok(())
}));
}
#[instrument(level = Level::TRACE, skip_all, name = "UpdateServer::run_internal")]
async fn run_internal(self, ws: HyperWebsocket) -> Result<()> {
let mut client: UpdateClient = ws.await?.into();
let mut streams = StreamMap::new();
loop {
// most logic is in helper functions as rustfmt cannot format code inside the macro
select! {
message = client.try_next() => {
if Self::on_message(
&mut client,
&mut streams,
&self.source_provider,
message?,
).await?.is_break() {
break;
}
}
Some((resource, update_result)) = streams.next() => {
Self::on_stream(
&mut client,
&mut streams,
resource,
update_result,
).await?
}
else => break
}
}
Ok(())
}
/// Helper for `on_message` used to construct a `GetContentFn`. Argument must match
/// `get_content_capture`.
fn get_content(
(source_provider, request): &(P, SourceRequest),
) -> OperationVc<ResolveSourceRequestResult> {
let request = request.clone();
let source = source_provider.get_source();
resolve_source_request(source, TransientInstance::new(request))
}
/// receives ClientMessages and passes subscriptions to `on_stream` via the `streams` map.
async fn on_message(
client: &mut UpdateClient,
streams: &mut StreamMap<ResourceIdentifier, UpdateStream>,
source_provider: &P,
message: Option<ClientMessage>,
) -> Result<ControlFlow<()>> {
match message {
Some(ClientMessage::Subscribe { resource }) => {
let get_content_capture =
(source_provider.clone(), resource_to_request(&resource)?);
match UpdateStream::new(
resource.to_string().into(),
TransientInstance::new(GetContentFn::new(
get_content_capture,
Self::get_content,
)),
)
.await
{
Ok(stream) => {
streams.insert(resource, stream);
}
Err(err) => {
eprintln!(
"Failed to create update stream for {resource}: {}",
PrettyPrintError(&err),
);
client
.send(ClientUpdateInstruction::not_found(&resource))
.await?;
}
}
}
Some(ClientMessage::Unsubscribe { resource }) => {
streams.remove(&resource);
}
None => {
// WebSocket was closed, stop sending updates
return Ok(ControlFlow::Break(()));
}
}
Ok(ControlFlow::Continue(()))
}
async fn on_stream(
client: &mut UpdateClient,
streams: &mut StreamMap<ResourceIdentifier, UpdateStream>,
resource: ResourceIdentifier,
update_result: Result<ReadRef<UpdateStreamItem>>,
) -> Result<()> {
match update_result {
Ok(update_item) => Self::send_update(client, streams, resource, &update_item).await,
Err(err) => {
eprintln!(
"Failed to get update for {resource}: {}",
PrettyPrintError(&err)
);
Ok(())
}
}
}
async fn send_update(
client: &mut UpdateClient,
streams: &mut StreamMap<ResourceIdentifier, UpdateStream>,
resource: ResourceIdentifier,
update_item: &UpdateStreamItem,
) -> Result<()> {
match update_item {
UpdateStreamItem::NotFound => {
// If the resource was not found, we remove the stream and indicate that to the
// client.
streams.remove(&resource);
client
.send(ClientUpdateInstruction::not_found(&resource))
.await?;
}
UpdateStreamItem::Found { update, issues } => {
let issues = issues
.iter()
.map(|p| Issue::from(&**p))
.collect::<Vec<Issue<'_>>>();
match &**update {
Update::Partial(partial) => {
let partial_instruction = &partial.instruction;
client
.send(ClientUpdateInstruction::partial(
&resource,
partial_instruction,
&issues,
))
.await?;
}
Update::Missing | Update::Total(_) => {
client
.send(ClientUpdateInstruction::restart(&resource, &issues))
.await?;
}
Update::None => {
client
.send(ClientUpdateInstruction::issues(&resource, &issues))
.await?;
}
}
}
}
Ok(())
}
}
fn resource_to_request(resource: &ResourceIdentifier) -> Result<SourceRequest> {
let mut headers = HeaderMap::new();
if let Some(res_headers) = &resource.headers {
for (name, value) in res_headers {
headers.append(
hyper::header::HeaderName::from_bytes(name.as_bytes()).unwrap(),
hyper::header::HeaderValue::from_bytes(value.as_bytes()).unwrap(),
);
}
}
Ok(SourceRequest {
uri: Uri::try_from(format!("/{}", resource.path))?,
headers,
method: "GET".to_string(),
body: Body::new(vec![]),
})
}
pin_project! {
struct UpdateClient {
#[pin]
ws: WebSocketStream<Upgraded>,
ended: bool,
}
}
impl Stream for UpdateClient {
type Item = Result<ClientMessage>;
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
if self.ended {
return Poll::Ready(None);
}
let this = self.project();
let item = ready!(this.ws.poll_next(cx));
let msg = match item {
Some(Ok(Message::Text(msg))) => msg,
Some(Err(err)) => {
*this.ended = true;
let err = Error::new(err).context("reading from websocket");
return Poll::Ready(Some(Err(err)));
}
_ => {
*this.ended = true;
return Poll::Ready(None);
}
};
match parse_json_with_source_context(&msg).context("deserializing websocket message") {
Ok(msg) => Poll::Ready(Some(Ok(msg))),
Err(err) => {
*this.ended = true;
Poll::Ready(Some(Err(err)))
}
}
}
}
impl FusedStream for UpdateClient {
fn is_terminated(&self) -> bool {
self.ended || self.ws.is_terminated()
}
}
impl<'a> Sink<ClientUpdateInstruction<'a>> for UpdateClient {
type Error = Error;
fn poll_ready(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<std::result::Result<(), Self::Error>> {
self.project()
.ws
.poll_ready(cx)
.map(|res| res.context("polling WebSocket ready"))
}
fn start_send(
self: Pin<&mut Self>,
item: ClientUpdateInstruction<'a>,
) -> std::result::Result<(), Self::Error> {
let msg = Message::text(serde_json::to_string(&item)?);
self.project()
.ws
.start_send(msg)
.context("sending to WebSocket")
}
fn poll_flush(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<std::result::Result<(), Self::Error>> {
self.project()
.ws
.poll_flush(cx)
.map(|res| res.context("flushing WebSocket"))
}
fn poll_close(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<std::result::Result<(), Self::Error>> {
self.project()
.ws
.poll_close(cx)
.map(|res| res.context("closing WebSocket"))
}
}
impl From<WebSocketStream<Upgraded>> for UpdateClient {
fn from(ws: WebSocketStream<Upgraded>) -> Self {
Self { ws, ended: false }
}
}
|