vortez 5.0.3 → 6.0.0-dev.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/Template/Template.d.ts +0 -5
- package/build/Template/Template.js +5 -5
- package/build/Template/Template.js.map +1 -1
- package/build/Vortez.d.ts +2 -2
- package/build/Vortez.js +2 -2
- package/build/Vortez.js.map +1 -1
- package/build/beta/Mail.d.ts +2 -8
- package/build/beta/Mail.js +7 -2
- package/build/beta/Mail.js.map +1 -1
- package/build/server/Cookie.d.ts +9 -5
- package/build/server/Cookie.js +28 -24
- package/build/server/Cookie.js.map +1 -1
- package/build/server/LoggerManager.d.ts +1 -1
- package/build/server/LoggerManager.js +5 -5
- package/build/server/LoggerManager.js.map +1 -1
- package/build/server/Response.js +11 -11
- package/build/server/Response.js.map +1 -1
- package/build/server/Server.js +4 -4
- package/build/server/Server.js.map +1 -1
- package/build/server/ServerDebug.d.ts +1 -1
- package/build/server/ServerDebug.js +1 -1
- package/build/server/ServerDebug.js.map +1 -1
- package/build/server/config/Config.d.ts +7 -7
- package/build/server/config/Config.js +4 -3
- package/build/server/config/Config.js.map +1 -1
- package/build/server/config/Loader.js +5 -5
- package/build/server/config/Loader.js.map +1 -1
- package/build/server/router/HttpRule.d.ts +2 -2
- package/build/server/router/HttpRule.js +1 -1
- package/build/server/router/HttpRule.js.map +1 -1
- package/build/server/router/Router.d.ts +1 -1
- package/build/server/router/Router.js +5 -5
- package/build/server/router/Router.js.map +1 -1
- package/build/server/router/Rule.d.ts +1 -1
- package/build/server/router/WsRule.d.ts +2 -2
- package/build/server/router/WsRule.js +2 -2
- package/build/server/router/WsRule.js.map +1 -1
- package/build/server/router/algorithm/Algorithm.d.ts +1 -1
- package/build/server/router/algorithm/Algorithm.js +2 -2
- package/build/server/router/algorithm/Algorithm.js.map +1 -1
- package/build/server/router/algorithm/FIFO.d.ts +1 -1
- package/build/server/router/algorithm/FIFO.js +2 -2
- package/build/server/router/algorithm/FIFO.js.map +1 -1
- package/build/server/router/algorithm/Tree.d.ts +1 -1
- package/build/server/router/algorithm/Tree.js.map +1 -1
- package/build/server/router/middleware/Middleware.d.ts +5 -5
- package/build/server/router/middleware/WsMiddleware.d.ts +4 -4
- package/build/server/router/middleware/WsMiddleware.js +24 -23
- package/build/server/router/middleware/WsMiddleware.js.map +1 -1
- package/build/server/security/PathSecurity.js +5 -5
- package/build/server/security/PathSecurity.js.map +1 -1
- package/build/server/websocket/Codec.d.ts +128 -0
- package/build/server/websocket/Codec.js +192 -0
- package/build/server/websocket/Codec.js.map +1 -0
- package/build/server/websocket/Websocket.d.ts +38 -89
- package/build/server/websocket/Websocket.js +41 -249
- package/build/server/websocket/Websocket.js.map +1 -1
- package/build/server/websocket/WebsocketBase.d.ts +130 -0
- package/build/server/websocket/WebsocketBase.js +272 -0
- package/build/server/websocket/WebsocketBase.js.map +1 -0
- package/build/server/websocket/WebsocketCSInit.d.ts +57 -0
- package/build/server/websocket/WebsocketCSInit.js +95 -0
- package/build/server/websocket/WebsocketCSInit.js.map +1 -0
- package/build/server/websocket/WebsocketSSInit.d.ts +24 -0
- package/build/server/websocket/WebsocketSSInit.js +46 -0
- package/build/server/websocket/WebsocketSSInit.js.map +1 -0
- package/build/server/websocket/frame/Frame.d.ts +31 -0
- package/build/server/websocket/frame/Frame.js +45 -0
- package/build/server/websocket/frame/Frame.js.map +1 -0
- package/build/server/websocket/frame/Header.d.ts +31 -0
- package/build/server/websocket/frame/Header.js +31 -0
- package/build/server/websocket/frame/Header.js.map +1 -0
- package/build/server/websocket/frame/HeaderParser.d.ts +58 -0
- package/build/server/websocket/frame/HeaderParser.js +109 -0
- package/build/server/websocket/frame/HeaderParser.js.map +1 -0
- package/build/server/websocket/handshake/CSHandshaker.d.ts +46 -0
- package/build/server/websocket/handshake/CSHandshaker.js +122 -0
- package/build/server/websocket/handshake/CSHandshaker.js.map +1 -0
- package/build/server/websocket/handshake/SSHandshaker.d.ts +57 -0
- package/build/server/websocket/handshake/SSHandshaker.js +113 -0
- package/build/server/websocket/handshake/SSHandshaker.js.map +1 -0
- package/build/server/websocket/messageAssembler/Message.d.ts +21 -0
- package/build/server/websocket/messageAssembler/Message.js +20 -0
- package/build/server/websocket/messageAssembler/Message.js.map +1 -0
- package/build/server/websocket/messageAssembler/MessageAssembler.d.ts +25 -0
- package/build/server/websocket/messageAssembler/MessageAssembler.js +47 -0
- package/build/server/websocket/messageAssembler/MessageAssembler.js.map +1 -0
- package/changes.md +28 -0
- package/package.json +6 -1
- package/build/logger/Debug.d.ts +0 -174
- package/build/logger/Debug.js +0 -295
- package/build/logger/Debug.js.map +0 -1
- package/build/logger/Logger.d.ts +0 -38
- package/build/logger/Logger.js +0 -48
- package/build/logger/Logger.js.map +0 -1
- package/build/server/websocket/Chunk.d.ts +0 -36
- package/build/server/websocket/Chunk.js +0 -81
- package/build/server/websocket/Chunk.js.map +0 -1
- package/build/utilities/ConsoleUI.d.ts +0 -89
- package/build/utilities/ConsoleUI.js +0 -142
- package/build/utilities/ConsoleUI.js.map +0 -1
- package/build/utilities/DebugUI.d.ts +0 -66
- package/build/utilities/DebugUI.js +0 -98
- package/build/utilities/DebugUI.js.map +0 -1
- package/build/utilities/Encoding.d.ts +0 -22
- package/build/utilities/Encoding.js +0 -26
- package/build/utilities/Encoding.js.map +0 -1
- package/build/utilities/Env.d.ts +0 -81
- package/build/utilities/Env.js +0 -140
- package/build/utilities/Env.js.map +0 -1
- package/build/utilities/File.d.ts +0 -10
- package/build/utilities/File.js +0 -19
- package/build/utilities/File.js.map +0 -1
- package/build/utilities/Flatten.d.ts +0 -73
- package/build/utilities/Flatten.js +0 -76
- package/build/utilities/Flatten.js.map +0 -1
- package/build/utilities/Object.d.ts +0 -16
- package/build/utilities/Object.js +0 -48
- package/build/utilities/Object.js.map +0 -1
- package/build/utilities/Path.d.ts +0 -44
- package/build/utilities/Path.js +0 -69
- package/build/utilities/Path.js.map +0 -1
- package/build/utilities/Time.d.ts +0 -21
- package/build/utilities/Time.js +0 -25
- package/build/utilities/Time.js.map +0 -1
- package/build/utilities/Utilities.d.ts +0 -118
- package/build/utilities/Utilities.js +0 -112
- package/build/utilities/Utilities.js.map +0 -1
- package/build/utilities/schema/Introspection.d.ts +0 -24
- package/build/utilities/schema/Introspection.js +0 -87
- package/build/utilities/schema/Introspection.js.map +0 -1
- package/build/utilities/schema/JSONSchema.d.ts +0 -68
- package/build/utilities/schema/JSONSchema.js +0 -13
- package/build/utilities/schema/JSONSchema.js.map +0 -1
- package/build/utilities/schema/Schema.d.ts +0 -253
- package/build/utilities/schema/Schema.js +0 -241
- package/build/utilities/schema/Schema.js.map +0 -1
- package/build/utilities/schema/SchemaError.d.ts +0 -10
- package/build/utilities/schema/SchemaError.js +0 -13
- package/build/utilities/schema/SchemaError.js.map +0 -1
- package/build/utilities/schema/Validator.d.ts +0 -94
- package/build/utilities/schema/Validator.js +0 -246
- package/build/utilities/schema/Validator.js.map +0 -1
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
export declare class Codec {
|
|
2
|
+
/**
|
|
3
|
+
* Decodes a WebSocket frame's payload using the provided information about the frame.
|
|
4
|
+
*
|
|
5
|
+
* @param data - The complete WebSocket frame data to decode.
|
|
6
|
+
* @param bytesInfo - The number of bytes used for the frame's header and extended payload length.
|
|
7
|
+
* @param maskKeys - The masking keys if the frame is masked, or null if it is not masked.
|
|
8
|
+
* @return A Buffer containing the decoded payload data.
|
|
9
|
+
*
|
|
10
|
+
* Note: The `decode` method is designed for server use and expects the data to be unmasked.
|
|
11
|
+
* For **client-side** decoding, use `clientDecode` which can handle masked frames as per the WebSocket protocol requirements.
|
|
12
|
+
*/
|
|
13
|
+
static decode(data: Buffer, bytesInfo: number, maskKeys?: Buffer | null): Buffer;
|
|
14
|
+
/**
|
|
15
|
+
* Encodes a WebSocket frame with the given data and opcode.
|
|
16
|
+
*
|
|
17
|
+
* By default, it encodes as a binary frame **(opcode 0x2)** without masking, suitable **for server-to-client** communication.
|
|
18
|
+
* For **client-to-server** communication, use `clientEncode` which applies masking as required by the WebSocket protocol.
|
|
19
|
+
*
|
|
20
|
+
* @param data - The payload data to encode.
|
|
21
|
+
* @param opCode - The opcode for the frame.
|
|
22
|
+
* @return A Buffer containing the encoded WebSocket frame.
|
|
23
|
+
*
|
|
24
|
+
* Note: The `encode` method is intended for server use and does not apply masking, while `clientEncode` should be used for **client-side** encoding to ensure compliance with the WebSocket protocol.
|
|
25
|
+
* |OpCode|Description|
|
|
26
|
+
* |:-----|:------------|
|
|
27
|
+
* | 0x0 | Continuation frame |
|
|
28
|
+
* | 0x1 | Text frame |
|
|
29
|
+
* | 0x2 | Binary frame |
|
|
30
|
+
* | 0x8 | Connection close frame |
|
|
31
|
+
* | 0x9 | Ping frame |
|
|
32
|
+
* | 0xA | Pong frame |
|
|
33
|
+
|
|
34
|
+
*/
|
|
35
|
+
static encode(data: Buffer, opCode?: number): Buffer;
|
|
36
|
+
/**
|
|
37
|
+
* Encodes a WebSocket message into multiple frames when the payload is too large or when fragmentation is desired.
|
|
38
|
+
*
|
|
39
|
+
* The first frame keeps the provided opcode and the rest use opcode `0x0` continuation frames.
|
|
40
|
+
* The last frame is marked with FIN = 1.
|
|
41
|
+
*
|
|
42
|
+
* @param data - The payload data to encode.
|
|
43
|
+
* @param opCode - The opcode for the first frame.
|
|
44
|
+
* @param fragmentSize - The maximum payload size per frame.
|
|
45
|
+
* @return An array of encoded frames ready to be written in order.
|
|
46
|
+
*/
|
|
47
|
+
static encodeFragments(data: Buffer, opCode?: number, fragmentSize?: number): Buffer[];
|
|
48
|
+
/**
|
|
49
|
+
* Encodes a WebSocket frame for **client-to-server** communication, applying masking as required by the WebSocket protocol.
|
|
50
|
+
* This method should be used when encoding frames on the client side, as it ensures that the payload is masked with a random 4-byte key, which is a requirement for frames sent from clients to servers.
|
|
51
|
+
*
|
|
52
|
+
* @param data - The payload data to encode.
|
|
53
|
+
* @param opCode - The opcode for the frame.
|
|
54
|
+
* @return A Buffer containing the encoded WebSocket frame with masking applied.
|
|
55
|
+
*
|
|
56
|
+
* Note: The `clientEncode` method is specifically designed for client-side use and applies masking to the payload, which is mandatory for frames sent from clients to servers according to the WebSocket protocol. For server-side encoding, use the `encode` method which does not apply masking.
|
|
57
|
+
* |OpCode|Description|
|
|
58
|
+
* |:-----|:------------|
|
|
59
|
+
* | 0x0 | Continuation frame |
|
|
60
|
+
* | 0x1 | Text frame |
|
|
61
|
+
* | 0x2 | Binary frame |
|
|
62
|
+
* | 0x8 | Connection close frame |
|
|
63
|
+
* | 0x9 | Ping frame |
|
|
64
|
+
* | 0xA | Pong frame |
|
|
65
|
+
*/
|
|
66
|
+
static clientEncode(data: Buffer, opCode?: number): Buffer;
|
|
67
|
+
/**
|
|
68
|
+
* Encodes a client WebSocket message into multiple masked frames.
|
|
69
|
+
*
|
|
70
|
+
* @param data - The payload data to encode.
|
|
71
|
+
* @param opCode - The opcode for the first frame.
|
|
72
|
+
* @param fragmentSize - The maximum payload size per frame.
|
|
73
|
+
* @return An array of masked frames ready to be written in order.
|
|
74
|
+
*/
|
|
75
|
+
static clientEncodeFragments(data: Buffer, opCode?: number, fragmentSize?: number): Buffer[];
|
|
76
|
+
/**
|
|
77
|
+
* Builds a WebSocket frame with the specified data, opcode, and masking option.
|
|
78
|
+
* This method is a helper function used by both `encode` and `clientEncode` to construct the WebSocket frame according to the protocol specifications.
|
|
79
|
+
* It handles the framing of the data, including setting the appropriate header fields based on the opcode and masking requirements.
|
|
80
|
+
*
|
|
81
|
+
* @param data - The payload data to include in the frame.
|
|
82
|
+
* @param opCode - The opcode for the frame, indicating the type of frame being sent.
|
|
83
|
+
* @param mask - A boolean indicating whether to apply masking to the payload (true for client-to-server frames, false for server-to-client frames).
|
|
84
|
+
* @return A Buffer containing the complete WebSocket frame ready for transmission.
|
|
85
|
+
*
|
|
86
|
+
* Note: The `buildFrame` method is an internal helper function and is not intended for direct use outside of the `Codec` class. It is responsible for constructing the WebSocket frame according to the protocol specifications, including handling the framing of the data and applying masking when required.
|
|
87
|
+
* |OpCode|Description|
|
|
88
|
+
* |:-----|:------------|
|
|
89
|
+
* | 0x0 | Continuation frame |
|
|
90
|
+
* | 0x1 | Text frame |
|
|
91
|
+
* | 0x2 | Binary frame |
|
|
92
|
+
* | 0x8 | Connection close frame |
|
|
93
|
+
* | 0x9 | Ping frame |
|
|
94
|
+
* | 0xA | Pong frame |
|
|
95
|
+
*/
|
|
96
|
+
private static buildFrame;
|
|
97
|
+
/**
|
|
98
|
+
* Splits a payload into protocol-compliant WebSocket fragments.
|
|
99
|
+
*
|
|
100
|
+
* @param data - The payload data to fragment.
|
|
101
|
+
* @param opCode - The opcode for the first frame.
|
|
102
|
+
* @param mask - Whether the frames should be masked.
|
|
103
|
+
* @param fragmentSize - The maximum payload size per frame.
|
|
104
|
+
* @return An array of encoded frames.
|
|
105
|
+
*/
|
|
106
|
+
private static buildFragments;
|
|
107
|
+
/**
|
|
108
|
+
* Applies a masking key to the given data using the XOR operation, as required by the WebSocket protocol for client-to-server frames.
|
|
109
|
+
* This method takes the payload data and the masking key, and returns a new Buffer containing the masked data.
|
|
110
|
+
* The masking is performed by XORing each byte of the payload with the corresponding byte of the masking key, which is repeated as necessary.
|
|
111
|
+
*
|
|
112
|
+
* @param data - The payload data to be masked.
|
|
113
|
+
* @param maskKey - The 4-byte masking key to apply to the data.
|
|
114
|
+
* @return A Buffer containing the masked payload data.
|
|
115
|
+
*
|
|
116
|
+
* Note: The `applyMask` method is an internal helper function used by the `buildFrame` method when encoding client-to-server frames. It is responsible for applying the masking to the payload data according to the WebSocket protocol specifications.
|
|
117
|
+
*/
|
|
118
|
+
private static applyMask;
|
|
119
|
+
/**
|
|
120
|
+
* Decodes the payload of a WebSocket frame, applying the masking key if the frame is masked.
|
|
121
|
+
* This method takes the complete frame data, the number of bytes used for the header and extended payload length, and the masking keys if applicable.
|
|
122
|
+
* It returns a Buffer containing the decoded payload data, which is obtained by applying the XOR operation with the masking keys if the frame is masked, or by simply slicing the data if it is not masked.
|
|
123
|
+
*
|
|
124
|
+
* @param data - The complete WebSocket frame data to decode.
|
|
125
|
+
*/
|
|
126
|
+
private static decodePayload;
|
|
127
|
+
}
|
|
128
|
+
export default Codec;
|
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @author NetFeez <netfeez.dev@gmail.com>
|
|
3
|
+
* @description WebSocket frame codec for Vortez.
|
|
4
|
+
* @license Apache-2.0
|
|
5
|
+
*/
|
|
6
|
+
import CRYPTO from 'crypto';
|
|
7
|
+
export class Codec {
|
|
8
|
+
/**
|
|
9
|
+
* Decodes a WebSocket frame's payload using the provided information about the frame.
|
|
10
|
+
*
|
|
11
|
+
* @param data - The complete WebSocket frame data to decode.
|
|
12
|
+
* @param bytesInfo - The number of bytes used for the frame's header and extended payload length.
|
|
13
|
+
* @param maskKeys - The masking keys if the frame is masked, or null if it is not masked.
|
|
14
|
+
* @return A Buffer containing the decoded payload data.
|
|
15
|
+
*
|
|
16
|
+
* Note: The `decode` method is designed for server use and expects the data to be unmasked.
|
|
17
|
+
* For **client-side** decoding, use `clientDecode` which can handle masked frames as per the WebSocket protocol requirements.
|
|
18
|
+
*/
|
|
19
|
+
static decode(data, bytesInfo, maskKeys) {
|
|
20
|
+
return Codec.decodePayload(data, bytesInfo, maskKeys ?? null);
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Encodes a WebSocket frame with the given data and opcode.
|
|
24
|
+
*
|
|
25
|
+
* By default, it encodes as a binary frame **(opcode 0x2)** without masking, suitable **for server-to-client** communication.
|
|
26
|
+
* For **client-to-server** communication, use `clientEncode` which applies masking as required by the WebSocket protocol.
|
|
27
|
+
*
|
|
28
|
+
* @param data - The payload data to encode.
|
|
29
|
+
* @param opCode - The opcode for the frame.
|
|
30
|
+
* @return A Buffer containing the encoded WebSocket frame.
|
|
31
|
+
*
|
|
32
|
+
* Note: The `encode` method is intended for server use and does not apply masking, while `clientEncode` should be used for **client-side** encoding to ensure compliance with the WebSocket protocol.
|
|
33
|
+
* |OpCode|Description|
|
|
34
|
+
* |:-----|:------------|
|
|
35
|
+
* | 0x0 | Continuation frame |
|
|
36
|
+
* | 0x1 | Text frame |
|
|
37
|
+
* | 0x2 | Binary frame |
|
|
38
|
+
* | 0x8 | Connection close frame |
|
|
39
|
+
* | 0x9 | Ping frame |
|
|
40
|
+
* | 0xA | Pong frame |
|
|
41
|
+
|
|
42
|
+
*/
|
|
43
|
+
static encode(data, opCode = 0x2) {
|
|
44
|
+
return Codec.buildFrame(data, opCode, false, true);
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Encodes a WebSocket message into multiple frames when the payload is too large or when fragmentation is desired.
|
|
48
|
+
*
|
|
49
|
+
* The first frame keeps the provided opcode and the rest use opcode `0x0` continuation frames.
|
|
50
|
+
* The last frame is marked with FIN = 1.
|
|
51
|
+
*
|
|
52
|
+
* @param data - The payload data to encode.
|
|
53
|
+
* @param opCode - The opcode for the first frame.
|
|
54
|
+
* @param fragmentSize - The maximum payload size per frame.
|
|
55
|
+
* @return An array of encoded frames ready to be written in order.
|
|
56
|
+
*/
|
|
57
|
+
static encodeFragments(data, opCode = 0x2, fragmentSize = 16384) {
|
|
58
|
+
return Codec.buildFragments(data, opCode, false, fragmentSize);
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Encodes a WebSocket frame for **client-to-server** communication, applying masking as required by the WebSocket protocol.
|
|
62
|
+
* This method should be used when encoding frames on the client side, as it ensures that the payload is masked with a random 4-byte key, which is a requirement for frames sent from clients to servers.
|
|
63
|
+
*
|
|
64
|
+
* @param data - The payload data to encode.
|
|
65
|
+
* @param opCode - The opcode for the frame.
|
|
66
|
+
* @return A Buffer containing the encoded WebSocket frame with masking applied.
|
|
67
|
+
*
|
|
68
|
+
* Note: The `clientEncode` method is specifically designed for client-side use and applies masking to the payload, which is mandatory for frames sent from clients to servers according to the WebSocket protocol. For server-side encoding, use the `encode` method which does not apply masking.
|
|
69
|
+
* |OpCode|Description|
|
|
70
|
+
* |:-----|:------------|
|
|
71
|
+
* | 0x0 | Continuation frame |
|
|
72
|
+
* | 0x1 | Text frame |
|
|
73
|
+
* | 0x2 | Binary frame |
|
|
74
|
+
* | 0x8 | Connection close frame |
|
|
75
|
+
* | 0x9 | Ping frame |
|
|
76
|
+
* | 0xA | Pong frame |
|
|
77
|
+
*/
|
|
78
|
+
static clientEncode(data, opCode = 0x2) {
|
|
79
|
+
return Codec.buildFrame(data, opCode, true, true);
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Encodes a client WebSocket message into multiple masked frames.
|
|
83
|
+
*
|
|
84
|
+
* @param data - The payload data to encode.
|
|
85
|
+
* @param opCode - The opcode for the first frame.
|
|
86
|
+
* @param fragmentSize - The maximum payload size per frame.
|
|
87
|
+
* @return An array of masked frames ready to be written in order.
|
|
88
|
+
*/
|
|
89
|
+
static clientEncodeFragments(data, opCode = 0x2, fragmentSize = 16384) {
|
|
90
|
+
return Codec.buildFragments(data, opCode, true, fragmentSize);
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Builds a WebSocket frame with the specified data, opcode, and masking option.
|
|
94
|
+
* This method is a helper function used by both `encode` and `clientEncode` to construct the WebSocket frame according to the protocol specifications.
|
|
95
|
+
* It handles the framing of the data, including setting the appropriate header fields based on the opcode and masking requirements.
|
|
96
|
+
*
|
|
97
|
+
* @param data - The payload data to include in the frame.
|
|
98
|
+
* @param opCode - The opcode for the frame, indicating the type of frame being sent.
|
|
99
|
+
* @param mask - A boolean indicating whether to apply masking to the payload (true for client-to-server frames, false for server-to-client frames).
|
|
100
|
+
* @return A Buffer containing the complete WebSocket frame ready for transmission.
|
|
101
|
+
*
|
|
102
|
+
* Note: The `buildFrame` method is an internal helper function and is not intended for direct use outside of the `Codec` class. It is responsible for constructing the WebSocket frame according to the protocol specifications, including handling the framing of the data and applying masking when required.
|
|
103
|
+
* |OpCode|Description|
|
|
104
|
+
* |:-----|:------------|
|
|
105
|
+
* | 0x0 | Continuation frame |
|
|
106
|
+
* | 0x1 | Text frame |
|
|
107
|
+
* | 0x2 | Binary frame |
|
|
108
|
+
* | 0x8 | Connection close frame |
|
|
109
|
+
* | 0x9 | Ping frame |
|
|
110
|
+
* | 0xA | Pong frame |
|
|
111
|
+
*/
|
|
112
|
+
static buildFrame(data, opCode, mask, fin) {
|
|
113
|
+
const header = [(fin ? 0x80 : 0x00) | (opCode & 0x0f)];
|
|
114
|
+
const length = data.length;
|
|
115
|
+
let maskKey = null;
|
|
116
|
+
if (length <= 125) {
|
|
117
|
+
header.push((mask ? 0x80 : 0x00) | length);
|
|
118
|
+
}
|
|
119
|
+
else if (length <= 65535) {
|
|
120
|
+
header.push((mask ? 0x80 : 0x00) | 126, (length >> 8) & 255, length & 255);
|
|
121
|
+
}
|
|
122
|
+
else {
|
|
123
|
+
header.push((mask ? 0x80 : 0x00) | 127);
|
|
124
|
+
const extendedLength = BigInt(length);
|
|
125
|
+
for (let shift = 56n; shift >= 0n; shift -= 8n) {
|
|
126
|
+
header.push(Number((extendedLength >> shift) & 0xffn));
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
const payload = mask ? Codec.applyMask(data, maskKey = CRYPTO.randomBytes(4)) : data;
|
|
130
|
+
return Buffer.concat(maskKey ? [Buffer.from(header), maskKey, payload] : [Buffer.from(header), payload]);
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Splits a payload into protocol-compliant WebSocket fragments.
|
|
134
|
+
*
|
|
135
|
+
* @param data - The payload data to fragment.
|
|
136
|
+
* @param opCode - The opcode for the first frame.
|
|
137
|
+
* @param mask - Whether the frames should be masked.
|
|
138
|
+
* @param fragmentSize - The maximum payload size per frame.
|
|
139
|
+
* @return An array of encoded frames.
|
|
140
|
+
*/
|
|
141
|
+
static buildFragments(data, opCode, mask, fragmentSize) {
|
|
142
|
+
if (fragmentSize < 1)
|
|
143
|
+
throw new RangeError('fragmentSize must be greater than 0');
|
|
144
|
+
if (data.length === 0)
|
|
145
|
+
return [Codec.buildFrame(data, opCode, mask, true)];
|
|
146
|
+
const frames = [];
|
|
147
|
+
for (let offset = 0, index = 0; offset < data.length; offset += fragmentSize, index++) {
|
|
148
|
+
const chunk = data.subarray(offset, offset + fragmentSize);
|
|
149
|
+
const isFirst = index === 0;
|
|
150
|
+
const isLast = offset + fragmentSize >= data.length;
|
|
151
|
+
const frameOpCode = isFirst ? opCode : 0x0;
|
|
152
|
+
frames.push(Codec.buildFrame(chunk, frameOpCode, mask, isLast));
|
|
153
|
+
}
|
|
154
|
+
return frames;
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Applies a masking key to the given data using the XOR operation, as required by the WebSocket protocol for client-to-server frames.
|
|
158
|
+
* This method takes the payload data and the masking key, and returns a new Buffer containing the masked data.
|
|
159
|
+
* The masking is performed by XORing each byte of the payload with the corresponding byte of the masking key, which is repeated as necessary.
|
|
160
|
+
*
|
|
161
|
+
* @param data - The payload data to be masked.
|
|
162
|
+
* @param maskKey - The 4-byte masking key to apply to the data.
|
|
163
|
+
* @return A Buffer containing the masked payload data.
|
|
164
|
+
*
|
|
165
|
+
* Note: The `applyMask` method is an internal helper function used by the `buildFrame` method when encoding client-to-server frames. It is responsible for applying the masking to the payload data according to the WebSocket protocol specifications.
|
|
166
|
+
*/
|
|
167
|
+
static applyMask(data, maskKey) {
|
|
168
|
+
const masked = Buffer.allocUnsafe(data.length);
|
|
169
|
+
for (let index = 0; index < data.length; index++) {
|
|
170
|
+
masked[index] = data[index] ^ maskKey[index % 4];
|
|
171
|
+
}
|
|
172
|
+
return masked;
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Decodes the payload of a WebSocket frame, applying the masking key if the frame is masked.
|
|
176
|
+
* This method takes the complete frame data, the number of bytes used for the header and extended payload length, and the masking keys if applicable.
|
|
177
|
+
* It returns a Buffer containing the decoded payload data, which is obtained by applying the XOR operation with the masking keys if the frame is masked, or by simply slicing the data if it is not masked.
|
|
178
|
+
*
|
|
179
|
+
* @param data - The complete WebSocket frame data to decode.
|
|
180
|
+
*/
|
|
181
|
+
static decodePayload(data, bytesInfo, maskKeys) {
|
|
182
|
+
if (!maskKeys)
|
|
183
|
+
return data.subarray(bytesInfo);
|
|
184
|
+
const payload = Buffer.allocUnsafe(data.length - bytesInfo);
|
|
185
|
+
for (let index = bytesInfo, payloadIndex = 0; index < data.length; index++, payloadIndex++) {
|
|
186
|
+
payload[payloadIndex] = data[index] ^ maskKeys[payloadIndex % 4];
|
|
187
|
+
}
|
|
188
|
+
return payload;
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
export default Codec;
|
|
192
|
+
//# sourceMappingURL=Codec.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Codec.js","sourceRoot":"","sources":["../../../src/server/websocket/Codec.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,OAAO,MAAM,MAAM,QAAQ,CAAC;AAE5B,MAAM,OAAO,KAAK;IACd;;;;;;;;;;OAUG;IACI,MAAM,CAAC,MAAM,CAAC,IAAY,EAAE,SAAiB,EAAE,QAAwB;QAC1E,OAAO,KAAK,CAAC,aAAa,CAAC,IAAI,EAAE,SAAS,EAAE,QAAQ,IAAI,IAAI,CAAC,CAAC;IAClE,CAAC;IACD;;;;;;;;;;;;;;;;;;;;OAoBG;IACI,MAAM,CAAC,MAAM,CAAC,IAAY,EAAE,SAAiB,GAAG;QACnD,OAAO,KAAK,CAAC,UAAU,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,IAAI,CAAC,CAAC;IACvD,CAAC;IACD;;;;;;;;;;OAUG;IACI,MAAM,CAAC,eAAe,CAAC,IAAY,EAAE,SAAiB,GAAG,EAAE,eAAuB,KAAK;QAC1F,OAAO,KAAK,CAAC,cAAc,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,YAAY,CAAC,CAAC;IACnE,CAAC;IACD;;;;;;;;;;;;;;;;;OAiBG;IACI,MAAM,CAA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CAAC,YAAY,CAAC,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,QAAQ,CAAC,YAAY,GAAG,CAAC,CAAC,CAAC;QACrE,CAAC;QAED,OAAO,OAAO,CAAC;IACnB,CAAC;CACJ;AAED,eAAe,KAAK,CAAC"}
|
|
@@ -1,93 +1,42 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
export
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
*
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
*
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* Send data to the client.
|
|
39
|
-
* @param data The data that will be sent.
|
|
40
|
-
*/
|
|
41
|
-
send(data: string | Buffer): void;
|
|
42
|
-
/**
|
|
43
|
-
* Send data to the client in JSON format.
|
|
44
|
-
* @param data The data that will be sent.
|
|
45
|
-
*/
|
|
46
|
-
sendJson(data: any): void;
|
|
47
|
-
/**
|
|
48
|
-
* Writes data to the client socket.
|
|
49
|
-
* @param data The data that will be sent.
|
|
50
|
-
*/
|
|
51
|
-
private write;
|
|
52
|
-
/**
|
|
53
|
-
* Encodes the data to be sent to the client.
|
|
54
|
-
* @param data The data that will be encoded.
|
|
55
|
-
* @param opCode The opcode of the message.
|
|
56
|
-
* - `0x1` for text,
|
|
57
|
-
* - `0x2` for binary,
|
|
58
|
-
* - `0x8` for close,
|
|
59
|
-
* - `0x9` for ping,
|
|
60
|
-
* - `0xA` for pong.
|
|
61
|
-
*/
|
|
62
|
-
private encode;
|
|
63
|
-
/**
|
|
64
|
-
* Converts a string to a buffer.
|
|
65
|
-
* @param message The string that will be converted.
|
|
66
|
-
*/
|
|
67
|
-
private stringToBuffer;
|
|
68
|
-
/** Initializes the events. */
|
|
69
|
-
private initEvents;
|
|
70
|
-
on(event: 'close', listener: Websocket.listener.close): this;
|
|
71
|
-
on(event: 'error', listener: Websocket.listener.error): this;
|
|
72
|
-
on(event: 'finish', listener: Websocket.listener.finish): this;
|
|
73
|
-
on(event: 'message', listener: Websocket.listener.message): this;
|
|
74
|
-
off(event: 'close', listener: Websocket.listener.close): this;
|
|
75
|
-
off(event: 'error', listener: Websocket.listener.error): this;
|
|
76
|
-
off(event: 'finish', listener: Websocket.listener.finish): this;
|
|
77
|
-
off(event: 'message', listener: Websocket.listener.message): this;
|
|
78
|
-
private buildHeaders;
|
|
1
|
+
import type { Duplex } from 'stream';
|
|
2
|
+
import type Request from '../Request.js';
|
|
3
|
+
import WebsocketBase from "./WebsocketBase.js";
|
|
4
|
+
import _WebsocketCSInit from './WebsocketCSInit.js';
|
|
5
|
+
import _WebsocketSSInit from './WebsocketSSInit.js';
|
|
6
|
+
export { Codec } from './Codec.js';
|
|
7
|
+
export { WebsocketCSInit } from './WebsocketCSInit.js';
|
|
8
|
+
export { WebsocketSSInit } from './WebsocketSSInit.js';
|
|
9
|
+
export declare class Websocket extends WebsocketBase {
|
|
10
|
+
/**
|
|
11
|
+
* Creates a new ServerInit with handshake functions for accepting or rejecting WebSocket connections.
|
|
12
|
+
* This method is intended to be used in server-side applications to handle incoming WebSocket handshake requests and establish WebSocket connections with clients.
|
|
13
|
+
* Before handshakes finish you should get WebSocket instance with get method `websocket`
|
|
14
|
+
* @example
|
|
15
|
+
* ```typescript
|
|
16
|
+
* const serverSocketInit = Websocket.SS(socket, request);
|
|
17
|
+
* serverSocketInit.accept(); // To accept the handshake and establish the WebSocket connection
|
|
18
|
+
* // or
|
|
19
|
+
* serverSocketInit.reject(400, 'Bad Request'); // To reject the handshake with a specific status code and reason
|
|
20
|
+
*
|
|
21
|
+
* const socket = serverSocketInit.websocket; // Access the WebSocket instance for further communication after accepting the handshake
|
|
22
|
+
* ```
|
|
23
|
+
* @param socket - The Duplex stream representing the incoming socket connection from a client, which will be used for the WebSocket handshake and subsequent communication if the handshake is accepted.
|
|
24
|
+
* @param request - The Request object representing the client's handshake request, which contains the necessary information for validating and processing the handshake.
|
|
25
|
+
* @returns A ServerInit instance that provides methods for accepting or rejecting the WebSocket handshake, as well as access to the WebSocket instance for communication after a successful handshake.
|
|
26
|
+
* @remarks The method creates a new ServerInit instance with the provided socket and request, which encapsulates the logic for handling the WebSocket handshake process. The returned ServerInit instance can then be used to accept or reject the handshake based on the validation of the client's request, and to access the WebSocket instance for communication if the handshake is accepted.
|
|
27
|
+
*/
|
|
28
|
+
static SS(socket: Duplex, request: Request): Websocket.WebsocketSSInit;
|
|
29
|
+
/**
|
|
30
|
+
* Creates a new ClientInit and initiates a WebSocket connection to the specified URL by performing the necessary handshake process.
|
|
31
|
+
* This method is intended to be used in client-side applications to establish WebSocket connections to servers by providing the appropriate WebSocket URL.
|
|
32
|
+
* @param url - The WebSocket URL to connect to, which should include the protocol (ws:// or wss://), host, optional port, and path.
|
|
33
|
+
* @returns A promise that resolves to a Websocket instance representing the established connection if the handshake is successful, or rejects with an error if the handshake fails or if there are issues during the connection process.
|
|
34
|
+
* @remarks The method calls the static `connect` method of the ClientInit class, which handles parsing the URL, creating the socket connection, and performing the WebSocket handshake. If the handshake is successful, it resolves with a Websocket instance that can be used for communication. If any errors occur during the process, it rejects with the corresponding error.
|
|
35
|
+
*/
|
|
36
|
+
static CS(url: string): Promise<Websocket>;
|
|
79
37
|
}
|
|
80
38
|
export declare namespace Websocket {
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
type error = (error: Error) => void;
|
|
84
|
-
type finish = () => void;
|
|
85
|
-
type message = (data: Buffer, info: dataInfo) => void;
|
|
86
|
-
}
|
|
87
|
-
interface dataInfo {
|
|
88
|
-
opCode: number;
|
|
89
|
-
size: number | bigint;
|
|
90
|
-
}
|
|
91
|
-
type handshakeStatus = 'accepted' | 'rejected' | 'pending';
|
|
39
|
+
export import WebsocketCSInit = _WebsocketCSInit;
|
|
40
|
+
export import WebsocketSSInit = _WebsocketSSInit;
|
|
92
41
|
}
|
|
93
42
|
export default Websocket;
|