Skip to content

Commit

Permalink
feat: add ws_protocol option
Browse files Browse the repository at this point in the history
  • Loading branch information
amrbashir committed Feb 18, 2023
1 parent b41a957 commit a361323
Show file tree
Hide file tree
Showing 11 changed files with 44 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Subheadings to categorize changes are `added, changed, deprecated, removed, fixe
- Added `data-target-path` to `copy-dir`.
- Allow processing `<script>` tags with the asset pipeline.
- Added `data-loader-shim` to workers to create shim script.
- Added `--ws-protocol` cli option and `ws_protocol` config option to specify the WebSockets protocol used for auto-reload.
### changed
- Updated gloo-worker example to use gloo-worker crate v2.1.
- Our website (trunkrs.dev) now only updates on new releases.
Expand Down
2 changes: 2 additions & 0 deletions Trunk.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ port = 8080
open = false
# Disable auto-reload of the web app.
no_autoreload = false
# Protocol used for autoreload WebSockets connection.
ws_protocol = "ws"

[clean]
# The output dir for all final assets.
Expand Down
8 changes: 7 additions & 1 deletion src/autoreload.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
(function () {
var protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:';
var protocol = '{{protocol}}'
protocol =
protocol === 'auto'
? window.location.protocol === 'https:'
? 'wss:'
: 'ws:'
: protocol
var url = protocol + '//' + window.location.host + '/_trunk/ws';
var poll_interval = 5000;
var reload_upon_connect = () => {
Expand Down
7 changes: 6 additions & 1 deletion src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,13 @@ impl BuildSystem {
pub async fn new(
cfg: Arc<RtcBuild>,
ignore_chan: Option<mpsc::Sender<PathBuf>>,
ws_protocol: Option<String>,
) -> Result<Self> {
let html_pipeline = Arc::new(HtmlPipeline::new(cfg.clone(), ignore_chan)?);
let html_pipeline = Arc::new(HtmlPipeline::new(
cfg.clone(),
ignore_chan,
ws_protocol,
)?);
Ok(Self { cfg, html_pipeline })
}

Expand Down
2 changes: 1 addition & 1 deletion src/cmd/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ impl Build {
#[tracing::instrument(level = "trace", skip(self, config))]
pub async fn run(self, config: Option<PathBuf>) -> Result<()> {
let cfg = ConfigOpts::rtc_build(self.build, config)?;
let mut system = BuildSystem::new(cfg, None).await?;
let mut system = BuildSystem::new(cfg, None, None).await?;
system.build().await?;
Ok(())
}
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/watch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ impl Watch {
pub async fn run(self, config: Option<PathBuf>) -> Result<()> {
let (shutdown_tx, _shutdown_rx) = broadcast::channel(1);
let cfg = ConfigOpts::rtc_watch(self.build, self.watch, config)?;
let mut system = WatchSystem::new(cfg, shutdown_tx.clone(), None).await?;
let mut system = WatchSystem::new(cfg, shutdown_tx.clone(), None, None).await?;

system.build().await.ok();
let system_handle = tokio::spawn(system.run());
Expand Down
5 changes: 5 additions & 0 deletions src/config/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,9 @@ pub struct ConfigOptsServe {
#[arg(long = "no-autoreload")]
#[serde(default)]
pub no_autoreload: bool,
/// Protocol used for autoreload WebSockets connection.
#[arg(long = "ws-protocol")]
pub ws_protocol: Option<String>,
}

/// Config options for the serve system.
Expand Down Expand Up @@ -335,6 +338,7 @@ impl ConfigOpts {
proxy_insecure: cli.proxy_insecure,
proxy_ws: cli.proxy_ws,
no_autoreload: cli.no_autoreload,
ws_protocol: cli.ws_protocol,
};
let cfg = ConfigOpts {
build: None,
Expand Down Expand Up @@ -501,6 +505,7 @@ impl ConfigOpts {
g.address = g.address.or(l.address);
g.port = g.port.or(l.port);
g.proxy_ws = g.proxy_ws || l.proxy_ws;
g.ws_protocol = g.ws_protocol.or(l.ws_protocol);
// NOTE: this can not be disabled in the cascade.
if l.no_autoreload {
g.no_autoreload = true;
Expand Down
3 changes: 3 additions & 0 deletions src/config/rt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,8 @@ pub struct RtcServe {
pub proxies: Option<Vec<ConfigOptsProxy>>,
/// Whether to disable auto-reload of the web page when a build completes.
pub no_autoreload: bool,
/// Protocol used for autoreload WebSockets connection.
pub ws_protocol: Option<String>,
}

impl RtcServe {
Expand Down Expand Up @@ -247,6 +249,7 @@ impl RtcServe {
proxy_ws: opts.proxy_ws,
proxies,
no_autoreload: opts.no_autoreload,
ws_protocol: opts.ws_protocol,
})
}
}
Expand Down
19 changes: 15 additions & 4 deletions src/pipelines/html.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,17 @@ pub struct HtmlPipeline {
target_html_dir: Arc<PathBuf>,
/// An optional channel to be used to communicate ignore paths to the watcher.
ignore_chan: Option<mpsc::Sender<PathBuf>>,
/// Protocol used for autoreload WebSockets connection.
pub ws_protocol: Option<String>,
}

impl HtmlPipeline {
/// Create a new instance.
pub fn new(cfg: Arc<RtcBuild>, ignore_chan: Option<mpsc::Sender<PathBuf>>) -> Result<Self> {
pub fn new(
cfg: Arc<RtcBuild>,
ignore_chan: Option<mpsc::Sender<PathBuf>>,
ws_protocol: Option<String>,
) -> Result<Self> {
let target_html_path = cfg
.target
.canonicalize()
Expand All @@ -57,6 +63,7 @@ impl HtmlPipeline {
target_html_path,
target_html_dir,
ignore_chan,
ws_protocol,
})
}

Expand Down Expand Up @@ -204,9 +211,13 @@ impl HtmlPipeline {

// Inject the WebSocket autoloader.
if self.cfg.inject_autoloader {
target_html
.select("body")
.append_html(format!("<script>{}</script>", RELOAD_SCRIPT));
target_html.select("body").append_html(format!(
"<script>{}</script>",
RELOAD_SCRIPT.replace(
"{{protocol}}",
&self.ws_protocol.clone().unwrap_or_else(|| "auto".into())
)
));
}
}
}
1 change: 1 addition & 0 deletions src/serve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ impl ServeSystem {
cfg.watch.clone(),
shutdown.clone(),
Some(build_done_chan.clone()),
cfg.ws_protocol.clone(),
)
.await?;
let http_addr = format!(
Expand Down
3 changes: 2 additions & 1 deletion src/watch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ impl WatchSystem {
cfg: Arc<RtcWatch>,
shutdown: broadcast::Sender<()>,
build_done_tx: Option<broadcast::Sender<()>>,
ws_protocol: Option<String>,
) -> Result<Self> {
// Create a channel for being able to listen for new paths to ignore while running.
let (watch_tx, watch_rx) = mpsc::channel(1);
Expand All @@ -47,7 +48,7 @@ impl WatchSystem {
let _watcher = build_watcher(watch_tx, cfg.paths.clone())?;

// Build dependencies.
let build = BuildSystem::new(cfg.build.clone(), Some(build_tx)).await?;
let build = BuildSystem::new(cfg.build.clone(), Some(build_tx), ws_protocol).await?;
Ok(Self {
build,
ignored_paths: cfg.ignored_paths.clone(),
Expand Down

0 comments on commit a361323

Please sign in to comment.