Skip to content

Commit 896eacb

Browse files
committed
fix: remove the ability for users to contruct websocket types
1 parent 577cb6d commit 896eacb

File tree

3 files changed

+20
-6
lines changed

3 files changed

+20
-6
lines changed

src/wisp.gleam

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1915,17 +1915,20 @@ pub type WsMessage(a) {
19151915
/// This connection is used from within a websocket handler function to
19161916
/// send data to the client via `WsSendText` or `WsSendBinary`
19171917
///
1918-
pub type WsConnection =
1918+
type WsConnection =
19191919
fn(WsSend) -> Result(Nil, WsError)
19201920

1921-
/// A socket connection used to connect to clients.
1921+
/// A capability used to support websocket execution from a capable server.
19221922
///
19231923
/// This is provided by a websocket capable server's handler
19241924
/// function. It is required to turn a http connection into an
19251925
/// active websocket (`WsConnection`).
19261926
///
19271927
pub type WsCapability(state, msg) {
1928-
WsCapability(fn(Request, WsHandler(state, msg)) -> Response)
1928+
WsCapability(
1929+
handler: fn(Request, WsHandler(state, msg)) -> Response,
1930+
ws: internal.WsCapability,
1931+
)
19291932
}
19301933

19311934
/// Configuration for a websockets creation and life-cycle.
@@ -2050,7 +2053,7 @@ pub type WsError {
20502053
///
20512054
/// This takes all the parameters required to begin a websocket session with a
20522055
/// client and requires a websocket capabile web server such as mist which
2053-
/// provides the `WsCapability` as part of it's handler function.
2056+
/// provides the `WsCapability` as part of its handler function.
20542057
///
20552058
/// ```gleam
20562059
/// wisp.WsHandler(handler, on_init, on_close) |> wisp.websocket(req, ws_capability)
@@ -2061,6 +2064,6 @@ pub fn websocket(
20612064
req: Request,
20622065
capability: WsCapability(state, msg),
20632066
) -> Response {
2064-
let WsCapability(do_websocket) = capability
2067+
let WsCapability(do_websocket, _) = capability
20652068
do_websocket(req, handler)
20662069
}

src/wisp/internal.gleam

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,14 @@ pub fn random_string(length: Int) -> String {
8282
pub fn random_slug() -> String {
8383
random_string(16)
8484
}
85+
86+
//
87+
// Websockets
88+
//
89+
90+
/// A type used purely to block the user constructing a `wisp.WsCapability`
91+
/// via the public api as it should only be constructed by the web server if it
92+
/// supports websockets.
93+
pub type WsCapability {
94+
WsCapability
95+
}

src/wisp/wisp_mist.gleam

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ pub fn handler(
5959
fn(req: wisp.Request, wsh: wisp.WsHandler(a, b)) -> wisp.Response {
6060
request.set_body(req, mist) |> websocket(wsh)
6161
}
62-
|> wisp.WsCapability
62+
|> wisp.WsCapability(internal.WsCapability)
6363

6464
let response =
6565
request

0 commit comments

Comments
 (0)