Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve types #24

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion hapi-plugin-websocket.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,17 @@ declare namespace HAPIPluginWebsocket {
initially: boolean
}

interface PluginStateHttp {
mode: "http"
ctx: null
wss: null
ws: null
wsf: null
req: null
peers: null
initially?: null
}

interface PluginSpecificConfiguration {
only?: boolean
subprotocol?: string
Expand Down Expand Up @@ -88,7 +99,7 @@ export = HAPIPluginWebsocket

declare module "@hapi/hapi" {
export interface Request<Refs extends ReqRef = ReqRefDefaults> extends Podium {
websocket(): HAPIPluginWebsocket.PluginState
websocket(): HAPIPluginWebsocket.PluginState | HAPIPluginWebsocket.PluginStateHttp
}
export interface PluginsStates {
websocket: HAPIPluginWebsocket.PluginState
Expand Down
10 changes: 5 additions & 5 deletions hapi-plugin-websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ const register = async (server, pluginOptions) => {
const ctx = {}

/* allow application to hook into WebSocket connection */
routeOptions.connect.call(ctx, { ctx, wss, ws, wsf, req, peers })
routeOptions.connect.call(ctx, { mode: "websocket", ctx, wss, ws, wsf, req, peers })

/* determine HTTP headers for simulated HTTP request:
take headers of initial HTTP upgrade request, but explicitly remove Accept-Encoding,
Expand Down Expand Up @@ -253,7 +253,7 @@ const register = async (server, pluginOptions) => {
/* framed WebSocket communication (correlated request/reply) */
wsf.on("message", async (ev) => {
/* allow application to hook into raw WebSocket frame processing */
routeOptions.frameMessage.call(ctx, { ctx, wss, ws, wsf, req, peers }, ev.frame)
routeOptions.frameMessage.call(ctx, { mode: "websocket", ctx, wss, ws, wsf, req, peers }, ev.frame)

/* process frame of expected type only */
if (ev.frame.type === routeOptions.frameRequest) {
Expand Down Expand Up @@ -322,7 +322,7 @@ const register = async (server, pluginOptions) => {
/* hook into WebSocket disconnection */
ws.on("close", () => {
/* allow application to hook into WebSocket disconnection */
routeOptions.disconnect.call(ctx, { ctx, wss, ws, wsf, req, peers })
routeOptions.disconnect.call(ctx, { mode: "websocket", ctx, wss, ws, wsf, req, peers })

/* stop tracking the peer */
const idx = routePeers[routeId].indexOf(ws)
Expand All @@ -331,11 +331,11 @@ const register = async (server, pluginOptions) => {

/* allow application to hook into WebSocket error processing */
ws.on("error", (error) => {
routeOptions.error.call(ctx, { ctx, wss, ws, wsf, req, peers }, error)
routeOptions.error.call(ctx, { mode: "websocket", ctx, wss, ws, wsf, req, peers }, error)
})
if (routeOptions.frame === true) {
wsf.on("error", (error) => {
routeOptions.error.call(ctx, { ctx, wss, ws, wsf, req, peers }, error)
routeOptions.error.call(ctx, { mode: "websocket", ctx, wss, ws, wsf, req, peers }, error)
})
}
})
Expand Down