websocket-close-codes
v1.0.0
Published
TypeScript library for WebSocket connection close codes based on RFC 6455
Readme
websocket-close-codes
TypeScript library for WebSocket connection close codes based on RFC 6455 and MDN Close Event.
This package provides a strongly-typed enum for standard WebSocket close codes, making your WebSocket handling code more readable and less prone to magic number errors.
Installation
npm i websocket-close-codesUsage
Import the CloseCode enum and use it to handle or send WebSocket close events.
import { CloseCode } from 'websocket-close-codes';
// Example: closing a connection
socket.close(CloseCode.NORMAL_CLOSURE, 'Work complete');
// Example: handling a close event
socket.addEventListener('close', (event) => {
switch (event.code) {
case CloseCode.NORMAL_CLOSURE:
console.log('Clean exit');
break;
case CloseCode.GOING_AWAY:
console.log('Server is going down or client navigated away');
break;
case CloseCode.PROTOCOL_ERROR:
console.error('Protocol error occurred');
break;
default:
console.log(`Closed with code: ${event.code}`);
}
});Available Codes
The CloseCode enum includes standard codes defined in RFC 6455:
| Code | Name | Description |
| ---- | ---------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| 1000 | NORMAL_CLOSURE | Indicates a normal closure, meaning that the purpose for which the connection was established has been fulfilled. |
| 1001 | GOING_AWAY | Indicates that an endpoint is "going away", such as a server going down or a browser having navigated away from a page. |
| 1002 | PROTOCOL_ERROR | Indicates that an endpoint is terminating the connection due to a protocol error. |
| 1003 | UNSUPPORTED_DATA | Indicates that an endpoint is terminating the connection because it has received a type of data it cannot accept (e.g., an endpoint that understands only text data MAY send this if it receives a binary message). |
| 1004 | RESERVED | Reserved. The specific meaning might be defined in the future. |
| 1005 | NO_STATUS_RECEIVED | Reserved value and MUST NOT be set as a status code in a Close control frame by an endpoint. It is designated for use in applications expecting a status code to indicate that no status code was actually present. |
| 1006 | ABNORMAL_CLOSURE | Reserved value and MUST NOT be set as a status code in a Close control frame by an endpoint. It is designated for use in applications expecting a status code to indicate that the connection was closed abnormally, e.g., without sending or receiving a Close control frame. |
| 1007 | INVALID_FRAME_PAYLOAD_DATA | Indicates that an endpoint is terminating the connection because it has received data within a message that was not consistent with the type of the message (e.g., non-UTF-8 [RFC3629] data within a text message). |
| 1008 | POLICY_VIOLATION | Indicates that an endpoint is terminating the connection because it has received a message that violates its policy. This is a generic status code that can be returned when there is no other more suitable status code (e.g., 1003 or 1009) or if there is a need to hide specific details about the policy. |
| 1009 | MESSAGE_TOO_BIG | Indicates that an endpoint is terminating the connection because it has received a message that is too big for it to process. |
| 1010 | MISSING_EXTENSION | Indicates that an endpoint (client) is terminating the connection because it has expected the server to negotiate one or more extension, but the server didn't return them in the response message of the WebSocket handshake. The list of extensions that are needed SHOULD appear in the /reason/ part of the Close frame. Note that this status code is not used by the server, because it can fail the WebSocket handshake instead. |
| 1011 | INTERNAL_ERROR | Indicates that a server is terminating the connection because it encountered an unexpected condition that prevented it from fulfilling the request. |
| 1012 | SERVICE_RESTART | The server is terminating the connection because it is restarting. |
| 1013 | TRY_AGAIN_LATER | The server is terminating the connection due to a temporary condition, e.g., it is overloaded and is casting off some of its clients. |
| 1014 | BAD_GATEWAY | The server was acting as a gateway or proxy and received an invalid response from the upstream server. This is similar to 502 HTTP Status Code. |
| 1015 | TLS_HANDSHAKE | Reserved value and MUST NOT be set as a status code in a Close control frame by an endpoint. It is designated for use in applications expecting a status code to indicate that the connection was closed due to a failure to perform a TLS handshake (e.g., the server certificate can't be verified). |
License
MIT © Robert Kłódka
