Your browser sucks, JavaScript disabled! Gunnar's console needs JavaScript and WebAssembly. Git over SSH on port 2222 does not.

nordisk/knut

crates/knut-wasm-host/src/entry.rs

//! The entry redirect: `/` → the loader page, told two things.
//!
//! The two things are **the caller's**: which component to mount, and where its
//! data comes from. This crate builds the URL and carries both strings verbatim; it
//! does not know what a component name means, it does not validate one against a
//! list (there is no list — a host that knew the components would be a host that
//! has to be edited when a component is added), and it does not encode them.
//!
//! The two query parameter NAMES — `component` and `events` — are this crate's own
//! protocol with whatever loader page the bundle ships. That is the whole contract:
//! *mount this, read frames from there*. A loader that wants different names is a
//! loader for a different host.

use crate::response::{HostBody, redirect_response};
use hyper::http::Response;

/// The query parameter naming the component to mount.
pub const COMPONENT_PARAM: &str = "component";

/// The query parameter naming where the loader reads its data frames from.
pub const EVENTS_PARAM: &str = "events";

/// Where `GET /` sends a browser.
#[derive(Clone, Debug)]
pub struct EntryPoint {
    loader_path: String,
    component: String,
    data_url: String,
}

impl EntryPoint {
    /// `loader_path` is the page in the bundle that boots the wasm (its name is the
    /// bundle's business, so it is a caller string like every other one here).
    /// `component` and `data_url` are handed to that page, verbatim.
    pub fn new(
        loader_path: impl Into<String>,
        component: impl Into<String>,
        data_url: impl Into<String>,
    ) -> Self {
        EntryPoint {
            loader_path: loader_path.into(),
            component: component.into(),
            data_url: data_url.into(),
        }
    }

    /// The loader page inside the bundle.
    #[must_use]
    pub fn loader_path(&self) -> &str {
        &self.loader_path
    }

    /// The component the loader is told to mount.
    #[must_use]
    pub fn component(&self) -> &str {
        &self.component
    }

    /// Where the loader is told to read frames from.
    #[must_use]
    pub fn data_url(&self) -> &str {
        &self.data_url
    }

    /// The URL `GET /` redirects to: the loader plus the two caller strings.
    #[must_use]
    pub fn url(&self) -> String {
        format!(
            "{}?{COMPONENT_PARAM}={}&{EVENTS_PARAM}={}",
            self.loader_path, self.component, self.data_url
        )
    }

    /// The `303 See Other` carrying [`EntryPoint::url`].
    #[must_use]
    pub fn redirect(&self) -> Response<HostBody> {
        redirect_response(&self.url())
    }
}