Skip to content

Commit

Permalink
fix: remove the ability for users to contruct websocket types
Browse files Browse the repository at this point in the history
  • Loading branch information
bgwdotdev committed Aug 24, 2024
1 parent 577cb6d commit 0e8a405
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
15 changes: 9 additions & 6 deletions src/wisp.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -1915,17 +1915,20 @@ pub type WsMessage(a) {
/// This connection is used from within a websocket handler function to
/// send data to the client via `WsSendText` or `WsSendBinary`
///
pub type WsConnection =
type WsConnection =
fn(WsSend) -> Result(Nil, WsError)

/// A socket connection used to connect to clients.
/// A capability used to support websocket execution from a capable server.
///
/// This is provided by a websocket capable server's handler
/// function. It is required to turn a http connection into an
/// active websocket (`WsConnection`).
///
pub type WsCapability(state, msg) {
WsCapability(fn(Request, WsHandler(state, msg)) -> Response)
WsCapability(
handler: fn(Request, WsHandler(state, msg)) -> Response,
ws: internal.WsCapability,
)
}

/// Configuration for a websockets creation and life-cycle.
Expand Down Expand Up @@ -2049,8 +2052,8 @@ pub type WsError {
/// Start a websocket session.
///
/// This takes all the parameters required to begin a websocket session with a
/// client and requires a websocket capabile web server such as mist which
/// provides the `WsCapability` as part of it's handler function.
/// client and requires a websocket capable web server such as mist which
/// provides the `WsCapability` as part of its handler function.
///
/// ```gleam
/// wisp.WsHandler(handler, on_init, on_close) |> wisp.websocket(req, ws_capability)
Expand All @@ -2061,6 +2064,6 @@ pub fn websocket(
req: Request,
capability: WsCapability(state, msg),
) -> Response {
let WsCapability(do_websocket) = capability
let WsCapability(do_websocket, _) = capability
do_websocket(req, handler)
}
11 changes: 11 additions & 0 deletions src/wisp/internal.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,14 @@ pub fn random_string(length: Int) -> String {
pub fn random_slug() -> String {
random_string(16)
}

//
// Websockets
//

/// A type used purely to block the user constructing a `wisp.WsCapability`
/// via the public api as it should only be constructed by the web server if it
/// supports websockets.
pub type WsCapability {
WsCapability
}
2 changes: 1 addition & 1 deletion src/wisp/wisp_mist.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ pub fn handler(
fn(req: wisp.Request, wsh: wisp.WsHandler(a, b)) -> wisp.Response {
request.set_body(req, mist) |> websocket(wsh)
}
|> wisp.WsCapability
|> wisp.WsCapability(internal.WsCapability)

let response =
request
Expand Down

0 comments on commit 0e8a405

Please sign in to comment.