WebsocketCloseSprintf formats a websocket close message and ensures it is truncated to the maximum allowed length.
(format string, vars ...any)
| 276 | // WebsocketCloseSprintf formats a websocket close message and ensures it is |
| 277 | // truncated to the maximum allowed length. |
| 278 | func WebsocketCloseSprintf(format string, vars ...any) string { |
| 279 | msg := fmt.Sprintf(format, vars...) |
| 280 | |
| 281 | // Cap msg length at 123 bytes. coder/websocket only allows close messages |
| 282 | // of this length. |
| 283 | if len(msg) > websocketCloseMaxLen { |
| 284 | // Trim the string to 123 bytes. If we accidentally cut in the middle of |
| 285 | // a UTF-8 character, remove it from the string. |
| 286 | return strings.ToValidUTF8(msg[:websocketCloseMaxLen], "") |
| 287 | } |
| 288 | |
| 289 | return msg |
| 290 | } |
| 291 | |
| 292 | type EventSender func(rw http.ResponseWriter, r *http.Request) ( |
| 293 | sendEvent func(sse codersdk.ServerSentEvent) error, |
no outgoing calls