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

Proxy support for WebSockets is g2g! #137

Merged
merged 1 commit into from
Mar 5, 2021
Merged
Show file tree
Hide file tree
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
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,18 @@ Subheadings to categorize changes are `added, changed, deprecated, removed, fixe

## Unreleased

## 0.9.0
### added
Added support for proxying WebSockets. This was a long-standing feature request. Due to changes upstream in the async-std/tide ecosystem, we are now able to properly support this. This will also unlock some nice features such as HMR via WebSockets, and other such niceties.

- Added the `--proxy-ws` CLI option for enabling WebSocket proxying on a CLI defined proxy.
- Added the `ws = true` field to the `Trunk.toml` `[[proxy]]` sections which will enable WebSocket proxying for proxies defined in the `Trunk.toml`.

### fixed
- Closed [#81](https://github.com/thedodd/trunk/issues/81): this is no longer needed as we now have support for WebSockets. HTTP2 is still outstanding, but that will not be a blocker for use from the web.
- Closed [#95](https://github.com/thedodd/trunk/issues/95): fixed via a few small changes to precendce in routing.
- Closed [#53](https://github.com/thedodd/trunk/issues/53): we've now implemented support for proxying WebSockets.

## 0.8.3
### fixed
- Fixed [#133](https://github.com/thedodd/trunk/issues/133) where `watch` was infinitely looping on Windows
Expand Down
107 changes: 90 additions & 17 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 8 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "trunk"
version = "0.8.3"
version = "0.9.0"
edition = "2018"
description = "Build, bundle & ship your Rust WASM application to the web."
license = "MIT/Apache-2.0"
Expand All @@ -18,7 +18,8 @@ panic = "abort"
[dependencies]
anyhow = "1"
async-process = "1"
async-std = { version="1.9", features=["attributes", "unstable"] }
async-std = { version = "1.9", default-features = false, features = ["attributes", "std", "unstable"] }
async-tungstenite = { version = "0.10.0", default-features = false, features = ["async-std-runtime"] }
cargo_metadata = "0.12"
console = "0.14"
dunce = "1"
Expand All @@ -27,18 +28,19 @@ fs_extra = "1"
futures = "0.3"
http-types = "2"
indicatif = "0.15"
nipper = "0.1"
nipper = { git = "https://github.com/thedodd/nipper.git", branch = "iter-attrs" }
notify = "4"
open = "1"
remove_dir_all = "0.6"
sass-rs = "0.2.2"
seahash = "4"
serde = { version="1", features=["derive"] }
serde = { version = "1", features = ["derive"] }
structopt = "0.3"
structopt-derive = "0.4"
surf = "2"
tide = { version="0.16.0", features=["unstable"] }
tide = { version = "0.16.0", features = ["unstable"] }
tide-websockets = { git = "https://github.com/http-rs/tide-websockets.git", rev = "270f408cdf4e5ee2bd28c7f5fcb57e5085d49ead" }
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jbr thanks for your work on this. I'll update to the next release (which supports sending raw messages) once it lands. It works quite nicely!

toml = "0.5"
remove_dir_all = "0.6"

[dev-dependencies]
insta = "0.16.1"
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,10 @@ The `trunk serve` command accepts two proxy related flags.

`--proxy-rewrite` specifies an alternative URI on which the Trunk server is to listen for proxy requests. Any requests received on the given URI will be rewritten to match the URI of the proxy backend, effectively stripping the rewrite prefix. E.G., `trunk serve --proxy-backend=http://localhost:9000/ --proxy-rewrite=/api/` will proxy any requests received on `/api/` over to `http://localhost:9000/` with the `/api/` prefix stripped from the request, while everything following the `/api/` prefix will be left unchanged.

`--proxy-ws` specifies that the proxy is for a WebSocket endpoint.

### config file
The `Trunk.toml` config file accepts multiple `[[proxy]]` sections, which allows for multiple proxies to be configured. Each section requires at least the `backend` field, and optionally accepts the `rewrite` field, both corresponding to the `--proxy-*` CLI flags discussed above.
The `Trunk.toml` config file accepts multiple `[[proxy]]` sections, which allows for multiple proxies to be configured. Each section requires at least the `backend` field, and optionally accepts the `rewrite` and `ws` fields, corresponding to the `--proxy-*` CLI flags discussed above.

As it is with other Trunk config, a proxy declared via CLI will take final precedence and will cause any config file proxies to be ignored, even if there are multiple proxies declared in the config file.

Expand Down
6 changes: 6 additions & 0 deletions Trunk.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ cargo = false
# Proxies are optional, and default to `None`.
# Proxies are only run as part of the `trunk serve` command.

[[proxy]]
# This WebSocket proxy example has a backend and ws field. This example will listen for
# WebSocket connections at `/api/ws` and proxy them to `ws://localhost:9000/api/ws`.
backend = "ws://localhost:9000/api/ws"
ws = true

[[proxy]]
# This proxy example has a backend and a rewrite field. Requests received on `rewrite` will be
# proxied to the backend after rewriting the `rewrite` prefix to the `backend`'s URI prefix.
Expand Down
4 changes: 3 additions & 1 deletion site/content/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@ The `trunk serve` command accepts two proxy related flags.

`--proxy-rewrite` specifies an alternative URI on which the Trunk server is to listen for proxy requests. Any requests received on the given URI will be rewritten to match the URI of the proxy backend, effectively stripping the rewrite prefix. E.G., `trunk serve --proxy-backend=http://localhost:9000/ --proxy-rewrite=/api/` will proxy any requests received on `/api/` over to `http://localhost:9000/` with the `/api/` prefix stripped from the request, while everything following the `/api/` prefix will be left unchanged.

`--proxy-ws` specifies that the proxy is for a WebSocket endpoint.

## Config File
The `Trunk.toml` config file accepts multiple `[[proxy]]` sections, which allows for multiple proxies to be configured. Each section requires at least the `backend` field, and optionally accepts the `rewrite` field, both corresponding to the `--proxy-*` CLI flags discussed above.
The `Trunk.toml` config file accepts multiple `[[proxy]]` sections, which allows for multiple proxies to be configured. Each section requires at least the `backend` field, and optionally accepts the `rewrite` and `ws` fields, both corresponding to the `--proxy-*` CLI flags discussed above.

As it is with other Trunk config, a proxy declared via CLI will take final precedence and will cause any config file proxies to be ignored, even if there are multiple proxies declared in the config file.

Expand Down
5 changes: 2 additions & 3 deletions src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use std::sync::Arc;

use anyhow::{Context, Result};
use async_std::fs;
use async_std::path::Path;
use futures::channel::mpsc::Sender;
use futures::stream::StreamExt;
use indicatif::ProgressBar;
Expand Down Expand Up @@ -77,7 +76,7 @@ impl BuildSystem {

// Spawn the source HTML pipeline. This will spawn all other pipelines derived from
// the source HTML, and will ultimately generate and write the final HTML.
self.html_pipeline.clone().spawn().await?;
self.html_pipeline.clone().spawn().await.context("error HTML pipeline")?;

// Move distrbution from staging dist to final dist
self.finalize_dist().await.context("error applying built distribution")?;
Expand All @@ -87,7 +86,7 @@ impl BuildSystem {
/// Creates a "staging area" (dist/.stage) for storing intermediate build results.
async fn prepare_staging_dist(&self) -> Result<()> {
// Prepare staging area in which we will assemble the latest build
let staging_dist: &Path = self.cfg.staging_dist.as_path().into();
let staging_dist = self.cfg.staging_dist.as_path();

// Clean staging area, if applicable
remove_dir_all(staging_dist.into()).await.context("error cleaning staging dist dir")?;
Expand Down
Loading