Skip to content

Commit

Permalink
fix: auto protocol (#124)
Browse files Browse the repository at this point in the history
  • Loading branch information
lomirus authored Jan 6, 2025
1 parent c49f7e6 commit b27eb83
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 15 deletions.
15 changes: 5 additions & 10 deletions src/http_layer/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ pub(crate) struct AppState {
pub(crate) index_listing: bool,
pub(crate) tx: Arc<broadcast::Sender<()>>,
pub(crate) root: PathBuf,
pub(crate) addr: String,
}

impl Default for Options {
Expand Down Expand Up @@ -154,8 +153,7 @@ async fn static_assets(
let status_code = match err.kind() {
ErrorKind::NotFound => {
if state.index_listing && is_accessing_dir {
let script =
format_script(&state.addr, state.hard_reload, is_reload, false);
let script = format_script(state.hard_reload, is_reload, false);
let html = format!(
include_str!("../templates/index.html"),
uri_path,
Expand All @@ -170,7 +168,7 @@ async fn static_assets(
_ => StatusCode::INTERNAL_SERVER_ERROR,
};
if mime == "text/html" {
let script = format_script(&state.addr, state.hard_reload, is_reload, true);
let script = format_script(state.hard_reload, is_reload, true);
let html = format!(include_str!("../templates/error.html"), script, err);
let body = Body::from(html);

Expand All @@ -190,7 +188,7 @@ async fn static_assets(
return (StatusCode::INTERNAL_SERVER_ERROR, headers, body);
}
};
let script = format_script(&state.addr, state.hard_reload, is_reload, false);
let script = format_script(state.hard_reload, is_reload, false);
file = format!("{text}{script}").into_bytes();
} else if state.hard_reload {
// allow client to cache assets for a smoother reload.
Expand All @@ -205,7 +203,7 @@ async fn static_assets(
}

/// Inject the address into the websocket script and wrap it in a script tag
fn format_script(addr: &str, hard_reload: bool, is_reload: bool, is_error: bool) -> String {
fn format_script(hard_reload: bool, is_reload: bool, is_error: bool) -> String {
match (is_reload, is_error) {
// successful reload, inject the reload payload
(true, false) => format!("<script>{}</script>", RELOAD_PAYLOAD),
Expand All @@ -214,10 +212,7 @@ fn format_script(addr: &str, hard_reload: bool, is_reload: bool, is_error: bool)
// normal connection, inject the websocket client
_ => {
let hard = if hard_reload { "true" } else { "false" };
format!(
r#"<script>{}("{}", {})</script>"#,
WEBSOCKET_FUNCTION, addr, hard
)
format!(r#"<script>{WEBSOCKET_FUNCTION}({hard})</script>"#)
}
}
}
1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ impl Listener {
index_listing: options.index_listing,
tx: arc_tx.clone(),
root: self.root_path.clone(),
addr: self.link().unwrap()[7..].to_string(),
};

let watcher_future = tokio::spawn(watch(self.root_path, self.debouncer, self.rx, arc_tx));
Expand Down
5 changes: 3 additions & 2 deletions src/templates/websocket.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
(async (addr, hard) => {
addr = `ws://${addr}/live-server-ws`;
(async (hard) => {
const wsProtocol = location.protocol === 'https:' ? 'wss:' : 'ws:';
const addr = `${wsProtocol}//${location.host}/live-server-ws`;
const sleep = (x) => new Promise((r) => setTimeout(r, x));
const preload = async (url, requireSuccess) => {
const resp = await fetch(url, { cache: "reload" }); // reset cache
Expand Down
3 changes: 1 addition & 2 deletions tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,9 @@ async fn request() {

let text = response.text().await.unwrap().replace("\r\n", "\n");
let target_text = format!(
r#"{}<script>{}("{}", false)</script>"#,
r#"{}<script>{}(false)</script>"#,
include_str!("./page/index.html"),
include_str!("../src/templates/websocket.js"),
HOST
)
.replace("\r\n", "\n");
assert_eq!(text, target_text);
Expand Down

0 comments on commit b27eb83

Please sign in to comment.