|
| 1 | +/* |
| 2 | +** hapi-plugin-websocket -- HAPI plugin for seamless WebSocket integration |
| 3 | +** Copyright (c) 2016-2022 Dr. Ralf S. Engelschall <[email protected]> |
| 4 | +** |
| 5 | +** Permission is hereby granted, free of charge, to any person obtaining |
| 6 | +** a copy of this software and associated documentation files (the |
| 7 | +** "Software"), to deal in the Software without restriction, including |
| 8 | +** without limitation the rights to use, copy, modify, merge, publish, |
| 9 | +** distribute, sublicense, and/or sell copies of the Software, and to |
| 10 | +** permit persons to whom the Software is furnished to do so, subject to |
| 11 | +** the following conditions: |
| 12 | +** |
| 13 | +** The above copyright notice and this permission notice shall be included |
| 14 | +** in all copies or substantial portions of the Software. |
| 15 | +** |
| 16 | +** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 17 | +** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 18 | +** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 19 | +** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 20 | +** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
| 21 | +** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| 22 | +** SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 23 | +*/ |
| 24 | + |
| 25 | +import * as ws from "ws" |
| 26 | +import * as http from "node:http" |
| 27 | + |
| 28 | +interface HapiWebsocketPluginState<WSF = any> { |
| 29 | + mode: "websocket" |
| 30 | + ctx: Record<string, any> |
| 31 | + wss: ws.Server |
| 32 | + ws: ws.WebSocket |
| 33 | + wsf: WSF |
| 34 | + req: http.IncomingMessage |
| 35 | + peers: ws.WebSocket[] |
| 36 | + initially: boolean |
| 37 | +} |
| 38 | + |
| 39 | +interface HapiWebsocketPluginSpecificConfiguration { |
| 40 | + only?: boolean |
| 41 | + subprotocol?: string |
| 42 | + error?: ( |
| 43 | + this: HapiWebsocketPluginState["ctx"], |
| 44 | + pluginState: HapiWebsocketPluginState, |
| 45 | + error: Error |
| 46 | + ) => void |
| 47 | + connect?: ( |
| 48 | + this: HapiWebsocketPluginState["ctx"], |
| 49 | + pluginState: HapiWebsocketPluginState |
| 50 | + ) => void |
| 51 | + disconnect?: ( |
| 52 | + this: HapiWebsocketPluginState["ctx"], |
| 53 | + pluginState: HapiWebsocketPluginState |
| 54 | + ) => void |
| 55 | + request?: ( |
| 56 | + ctx: any, |
| 57 | + request: any, |
| 58 | + h?: any |
| 59 | + ) => void |
| 60 | + response?: ( |
| 61 | + ctx: any, |
| 62 | + request: any, |
| 63 | + h?: any |
| 64 | + ) => void |
| 65 | + frame?: boolean |
| 66 | + frameEncoding?: "json" | string |
| 67 | + frameRequest?: "REQUEST" | string |
| 68 | + frameResponse?: "RESPONSE" | string |
| 69 | + frameMessage?: ( |
| 70 | + this: HapiWebsocketPluginState["ctx"], |
| 71 | + pluginState: HapiWebsocketPluginState, |
| 72 | + frame: any |
| 73 | + ) => void |
| 74 | + autoping?: number |
| 75 | + initially?: boolean |
| 76 | +} |
| 77 | + |
| 78 | +declare module "@hapi/hapi" { |
| 79 | + export interface PluginsStates { |
| 80 | + websocket: HapiWebsocketPluginState |
| 81 | + } |
| 82 | + export interface PluginSpecificConfiguration { |
| 83 | + websocket: HapiWebsocketPluginSpecificConfiguration |
| 84 | + } |
| 85 | +} |
| 86 | + |
0 commit comments