undici 7.27.2 → 7.28.0
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/docs/docs/api/Client.md +3 -0
- package/docs/docs/api/Cookies.md +27 -0
- package/docs/docs/api/Socks5ProxyAgent.md +1 -0
- package/lib/core/socks5-client.js +14 -4
- package/lib/core/socks5-utils.js +18 -4
- package/lib/dispatcher/agent.js +1 -1
- package/lib/dispatcher/balanced-pool.js +1 -1
- package/lib/dispatcher/client-h1.js +69 -1
- package/lib/dispatcher/client.js +3 -2
- package/lib/dispatcher/dispatcher-base.js +19 -0
- package/lib/dispatcher/pool.js +1 -1
- package/lib/dispatcher/proxy-agent.js +2 -1
- package/lib/dispatcher/socks5-proxy-agent.js +45 -34
- package/lib/util/cache.js +8 -2
- package/lib/web/cookies/parse.js +17 -25
- package/lib/web/websocket/permessage-deflate.js +13 -31
- package/lib/web/websocket/receiver.js +80 -23
- package/lib/web/websocket/stream/websocketstream.js +8 -1
- package/lib/web/websocket/websocket.js +8 -1
- package/package.json +1 -1
- package/types/client.d.ts +16 -0
package/docs/docs/api/Client.md
CHANGED
|
@@ -24,6 +24,9 @@ Returns: `Client`
|
|
|
24
24
|
* **keepAliveTimeoutThreshold** `number | null` (optional) - Default: `2e3` - A number of milliseconds subtracted from server *keep-alive* hints when overriding `keepAliveTimeout` to account for timing inaccuracies caused by e.g. transport latency. Defaults to 2 seconds.
|
|
25
25
|
* **maxHeaderSize** `number | null` (optional) - Default: `--max-http-header-size` or `16384` - The maximum length of request headers in bytes. Defaults to Node.js' --max-http-header-size or 16KiB.
|
|
26
26
|
* **maxResponseSize** `number | null` (optional) - Default: `-1` - The maximum length of response body in bytes. Set to `-1` to disable.
|
|
27
|
+
* **webSocket** `WebSocketOptions` (optional) - WebSocket-specific configuration options.
|
|
28
|
+
* **maxFragments** `number` (optional) - Default: `131072` - Maximum number of fragments in a message. Set to 0 to disable the limit.
|
|
29
|
+
* **maxPayloadSize** `number` (optional) - Default: `134217728` (128 MB) - Maximum allowed payload size in bytes for WebSocket messages. Applied to uncompressed messages, compressed frame payloads, and decompressed (permessage-deflate) messages. Set to 0 to disable the limit.
|
|
27
30
|
* **pipelining** `number | null` (optional) - Default: `1` - The amount of concurrent requests to be sent over the single TCP/TLS connection according to [RFC7230](https://tools.ietf.org/html/rfc7230#section-6.3.2). Carefully consider your workload and environment before enabling concurrent requests as pipelining may reduce performance if used incorrectly. Pipelining is sensitive to network stack settings as well as head of line blocking caused by e.g. long running requests. Set to `0` to disable keep-alive connections.
|
|
28
31
|
* **connect** `ConnectOptions | Function | null` (optional) - Default: `null`.
|
|
29
32
|
* **strictContentLength** `Boolean` (optional) - Default: `true` - Whether to treat request content length mismatches as errors. If true, an error is thrown when the request content-length header doesn't match the length of the request body. **Security Warning:** Disabling this option can expose your application to HTTP Request Smuggling attacks, where mismatched content-length headers cause servers and proxies to interpret request boundaries differently. This can lead to cache poisoning, credential hijacking, and bypassing security controls. Only disable this in controlled environments where you fully trust the request source.
|
package/docs/docs/api/Cookies.md
CHANGED
|
@@ -80,6 +80,33 @@ Arguments:
|
|
|
80
80
|
|
|
81
81
|
Returns: `Cookie[]`
|
|
82
82
|
|
|
83
|
+
## `parseCookie(cookie)`
|
|
84
|
+
|
|
85
|
+
Parses a single `Set-Cookie` header value into a `Cookie` object.
|
|
86
|
+
|
|
87
|
+
```js
|
|
88
|
+
import { parseCookie } from 'undici'
|
|
89
|
+
|
|
90
|
+
console.log(parseCookie('undici=getSetCookies; Secure; SameSite=Lax'))
|
|
91
|
+
// {
|
|
92
|
+
// name: 'undici',
|
|
93
|
+
// value: 'getSetCookies',
|
|
94
|
+
// secure: true,
|
|
95
|
+
// sameSite: 'Lax'
|
|
96
|
+
// }
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
Notes:
|
|
100
|
+
|
|
101
|
+
* The cookie value is returned as it appears in the header. Percent-encoded sequences such as `%20` or `%0D%0A` are **not** decoded.
|
|
102
|
+
* `sameSite` is only set for exact case-insensitive matches of `Strict`, `Lax`, or `None`.
|
|
103
|
+
|
|
104
|
+
Arguments:
|
|
105
|
+
|
|
106
|
+
* **cookie** `string`
|
|
107
|
+
|
|
108
|
+
Returns: `Cookie | null`
|
|
109
|
+
|
|
83
110
|
## `setCookie(headers, cookie)`
|
|
84
111
|
|
|
85
112
|
Appends a cookie to the `Set-Cookie` header.
|
|
@@ -22,6 +22,7 @@ Extends: [`PoolOptions`](/docs/docs/api/Pool.md#parameter-pooloptions)
|
|
|
22
22
|
* **password** `string` (optional) - SOCKS5 proxy password for authentication. Can also be provided in the proxy URL.
|
|
23
23
|
* **connect** `Function` (optional) - Custom connector function for the proxy connection.
|
|
24
24
|
* **proxyTls** `BuildOptions` (optional) - TLS options for the proxy connection (when using SOCKS5 over TLS).
|
|
25
|
+
* **requestTls** `BuildOptions` (optional) - TLS options applied to the HTTPS connection to the target server through the SOCKS5 tunnel. Use this to configure `ca`, `cert`, `key`, `rejectUnauthorized`, `servername`, etc. for the target HTTPS endpoint.
|
|
25
26
|
|
|
26
27
|
Examples:
|
|
27
28
|
|
|
@@ -52,6 +52,7 @@ const STATES = {
|
|
|
52
52
|
INITIAL: 'initial',
|
|
53
53
|
HANDSHAKING: 'handshaking',
|
|
54
54
|
AUTHENTICATING: 'authenticating',
|
|
55
|
+
AUTHENTICATED: 'authenticated',
|
|
55
56
|
CONNECTING: 'connecting',
|
|
56
57
|
CONNECTED: 'connected',
|
|
57
58
|
ERROR: 'error',
|
|
@@ -143,6 +144,11 @@ class Socks5Client extends EventEmitter {
|
|
|
143
144
|
}
|
|
144
145
|
}
|
|
145
146
|
|
|
147
|
+
markAuthenticated () {
|
|
148
|
+
this.state = STATES.AUTHENTICATED
|
|
149
|
+
this.emit('authenticated')
|
|
150
|
+
}
|
|
151
|
+
|
|
146
152
|
/**
|
|
147
153
|
* Start the SOCKS5 handshake
|
|
148
154
|
*/
|
|
@@ -193,7 +199,7 @@ class Socks5Client extends EventEmitter {
|
|
|
193
199
|
debug('server selected auth method', method)
|
|
194
200
|
|
|
195
201
|
if (method === AUTH_METHODS.NO_AUTH) {
|
|
196
|
-
this.
|
|
202
|
+
this.markAuthenticated()
|
|
197
203
|
} else if (method === AUTH_METHODS.USERNAME_PASSWORD) {
|
|
198
204
|
this.state = STATES.AUTHENTICATING
|
|
199
205
|
this.sendAuthRequest()
|
|
@@ -258,7 +264,7 @@ class Socks5Client extends EventEmitter {
|
|
|
258
264
|
|
|
259
265
|
this.buffer = this.buffer.subarray(2)
|
|
260
266
|
debug('authentication successful')
|
|
261
|
-
this.
|
|
267
|
+
this.markAuthenticated()
|
|
262
268
|
}
|
|
263
269
|
|
|
264
270
|
/**
|
|
@@ -267,8 +273,12 @@ class Socks5Client extends EventEmitter {
|
|
|
267
273
|
* @param {number} port - Target port
|
|
268
274
|
*/
|
|
269
275
|
connect (address, port) {
|
|
270
|
-
if (this.state === STATES.CONNECTED) {
|
|
271
|
-
throw new InvalidArgumentError('
|
|
276
|
+
if (this.state === STATES.CONNECTING || this.state === STATES.CONNECTED) {
|
|
277
|
+
throw new InvalidArgumentError('Connection already in progress')
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
if (this.state !== STATES.AUTHENTICATED) {
|
|
281
|
+
throw new InvalidArgumentError('Client must be authenticated before CONNECT')
|
|
272
282
|
}
|
|
273
283
|
|
|
274
284
|
debug('connecting to', address, port)
|
package/lib/core/socks5-utils.js
CHANGED
|
@@ -46,12 +46,26 @@ function parseAddress (address) {
|
|
|
46
46
|
*/
|
|
47
47
|
function parseIPv6 (address) {
|
|
48
48
|
const buffer = Buffer.alloc(16)
|
|
49
|
+
let normalizedAddress = address
|
|
50
|
+
|
|
51
|
+
// Expand an embedded IPv4 tail into the last two IPv6 groups.
|
|
52
|
+
if (address.includes('.')) {
|
|
53
|
+
const lastColonIndex = address.lastIndexOf(':')
|
|
54
|
+
const ipv4Part = address.slice(lastColonIndex + 1)
|
|
55
|
+
|
|
56
|
+
if (net.isIPv4(ipv4Part)) {
|
|
57
|
+
const octets = ipv4Part.split('.').map(Number)
|
|
58
|
+
const high = ((octets[0] << 8) | octets[1]).toString(16)
|
|
59
|
+
const low = ((octets[2] << 8) | octets[3]).toString(16)
|
|
60
|
+
normalizedAddress = `${address.slice(0, lastColonIndex)}:${high}:${low}`
|
|
61
|
+
}
|
|
62
|
+
}
|
|
49
63
|
|
|
50
64
|
// Handle compressed notation (::)
|
|
51
|
-
const doubleColonIndex =
|
|
65
|
+
const doubleColonIndex = normalizedAddress.indexOf('::')
|
|
52
66
|
if (doubleColonIndex !== -1) {
|
|
53
|
-
const before =
|
|
54
|
-
const after =
|
|
67
|
+
const before = normalizedAddress.slice(0, doubleColonIndex)
|
|
68
|
+
const after = normalizedAddress.slice(doubleColonIndex + 2)
|
|
55
69
|
const beforeParts = before === '' ? [] : before.split(':')
|
|
56
70
|
const afterParts = after === '' ? [] : after.split(':')
|
|
57
71
|
|
|
@@ -66,7 +80,7 @@ function parseIPv6 (address) {
|
|
|
66
80
|
bufferIndex += 2
|
|
67
81
|
}
|
|
68
82
|
} else {
|
|
69
|
-
const parts =
|
|
83
|
+
const parts = normalizedAddress.split(':')
|
|
70
84
|
for (let i = 0; i < parts.length; i++) {
|
|
71
85
|
buffer.writeUInt16BE(parseInt(parts[i], 16), i * 2)
|
|
72
86
|
}
|
package/lib/dispatcher/agent.js
CHANGED
|
@@ -57,6 +57,9 @@ const constants = require('../llhttp/constants.js')
|
|
|
57
57
|
const EMPTY_BUF = Buffer.alloc(0)
|
|
58
58
|
const FastBuffer = Buffer[Symbol.species]
|
|
59
59
|
const removeAllListeners = util.removeAllListeners
|
|
60
|
+
const kIdleSocketValidation = Symbol('kIdleSocketValidation')
|
|
61
|
+
const kIdleSocketValidationTimeout = Symbol('kIdleSocketValidationTimeout')
|
|
62
|
+
const kSocketUsed = Symbol('kSocketUsed')
|
|
60
63
|
|
|
61
64
|
let extractBody
|
|
62
65
|
|
|
@@ -440,6 +443,11 @@ class Parser {
|
|
|
440
443
|
return -1
|
|
441
444
|
}
|
|
442
445
|
|
|
446
|
+
if (client[kRunning] === 0) {
|
|
447
|
+
util.destroy(socket, new SocketError('bad response', util.getSocketInfo(socket)))
|
|
448
|
+
return -1
|
|
449
|
+
}
|
|
450
|
+
|
|
443
451
|
const request = client[kQueue][client[kRunningIdx]]
|
|
444
452
|
if (!request) {
|
|
445
453
|
return -1
|
|
@@ -568,6 +576,11 @@ class Parser {
|
|
|
568
576
|
return -1
|
|
569
577
|
}
|
|
570
578
|
|
|
579
|
+
if (client[kRunning] === 0) {
|
|
580
|
+
util.destroy(socket, new SocketError('bad response', util.getSocketInfo(socket)))
|
|
581
|
+
return -1
|
|
582
|
+
}
|
|
583
|
+
|
|
571
584
|
const request = client[kQueue][client[kRunningIdx]]
|
|
572
585
|
|
|
573
586
|
if (!request) {
|
|
@@ -746,6 +759,7 @@ class Parser {
|
|
|
746
759
|
request.onComplete(headers)
|
|
747
760
|
|
|
748
761
|
client[kQueue][client[kRunningIdx]++] = null
|
|
762
|
+
socket[kSocketUsed] = client[kPending] === 0
|
|
749
763
|
|
|
750
764
|
if (socket[kWriting]) {
|
|
751
765
|
assert(client[kRunning] === 0)
|
|
@@ -822,6 +836,9 @@ function connectH1 (client, socket) {
|
|
|
822
836
|
socket[kWriting] = false
|
|
823
837
|
socket[kReset] = false
|
|
824
838
|
socket[kBlocking] = false
|
|
839
|
+
socket[kIdleSocketValidation] = 0
|
|
840
|
+
socket[kIdleSocketValidationTimeout] = null
|
|
841
|
+
socket[kSocketUsed] = false
|
|
825
842
|
socket[kParser] = new Parser(client, socket, llhttpInstance)
|
|
826
843
|
|
|
827
844
|
util.addListener(socket, 'error', onHttpSocketError)
|
|
@@ -864,7 +881,7 @@ function connectH1 (client, socket) {
|
|
|
864
881
|
* @returns {boolean}
|
|
865
882
|
*/
|
|
866
883
|
busy (request) {
|
|
867
|
-
if (socket[kWriting] || socket[kReset] || socket[kBlocking]) {
|
|
884
|
+
if (socket[kWriting] || socket[kReset] || socket[kBlocking] || socket[kIdleSocketValidation] === 1) {
|
|
868
885
|
return true
|
|
869
886
|
}
|
|
870
887
|
|
|
@@ -944,6 +961,8 @@ function onHttpSocketEnd () {
|
|
|
944
961
|
function onHttpSocketClose () {
|
|
945
962
|
const parser = this[kParser]
|
|
946
963
|
|
|
964
|
+
clearIdleSocketValidation(this)
|
|
965
|
+
|
|
947
966
|
if (parser) {
|
|
948
967
|
if (!this[kError] && parser.statusCode && !parser.shouldKeepAlive) {
|
|
949
968
|
this[kError] = parser.finish() || this[kError]
|
|
@@ -990,6 +1009,28 @@ function onSocketClose () {
|
|
|
990
1009
|
this[kClosed] = true
|
|
991
1010
|
}
|
|
992
1011
|
|
|
1012
|
+
function clearIdleSocketValidation (socket) {
|
|
1013
|
+
if (socket[kIdleSocketValidationTimeout]) {
|
|
1014
|
+
clearTimeout(socket[kIdleSocketValidationTimeout])
|
|
1015
|
+
socket[kIdleSocketValidationTimeout] = null
|
|
1016
|
+
}
|
|
1017
|
+
|
|
1018
|
+
socket[kIdleSocketValidation] = 0
|
|
1019
|
+
}
|
|
1020
|
+
|
|
1021
|
+
function scheduleIdleSocketValidation (client, socket) {
|
|
1022
|
+
socket[kIdleSocketValidation] = 1
|
|
1023
|
+
socket[kIdleSocketValidationTimeout] = setTimeout(() => {
|
|
1024
|
+
socket[kIdleSocketValidationTimeout] = null
|
|
1025
|
+
socket[kIdleSocketValidation] = 2
|
|
1026
|
+
|
|
1027
|
+
if (client[kSocket] === socket && !socket.destroyed) {
|
|
1028
|
+
client[kResume]()
|
|
1029
|
+
}
|
|
1030
|
+
}, 0)
|
|
1031
|
+
socket[kIdleSocketValidationTimeout].unref?.()
|
|
1032
|
+
}
|
|
1033
|
+
|
|
993
1034
|
/**
|
|
994
1035
|
* @param {import('./client.js')} client
|
|
995
1036
|
*/
|
|
@@ -1007,6 +1048,32 @@ function resumeH1 (client) {
|
|
|
1007
1048
|
socket[kNoRef] = false
|
|
1008
1049
|
}
|
|
1009
1050
|
|
|
1051
|
+
if (client[kRunning] === 0 && client[kPending] > 0 && socket[kSocketUsed]) {
|
|
1052
|
+
if (socket[kIdleSocketValidation] === 0) {
|
|
1053
|
+
scheduleIdleSocketValidation(client, socket)
|
|
1054
|
+
socket[kParser].readMore()
|
|
1055
|
+
if (socket.destroyed) {
|
|
1056
|
+
return
|
|
1057
|
+
}
|
|
1058
|
+
return
|
|
1059
|
+
}
|
|
1060
|
+
|
|
1061
|
+
if (socket[kIdleSocketValidation] === 1) {
|
|
1062
|
+
socket[kParser].readMore()
|
|
1063
|
+
if (socket.destroyed) {
|
|
1064
|
+
return
|
|
1065
|
+
}
|
|
1066
|
+
return
|
|
1067
|
+
}
|
|
1068
|
+
}
|
|
1069
|
+
|
|
1070
|
+
if (client[kRunning] === 0) {
|
|
1071
|
+
socket[kParser].readMore()
|
|
1072
|
+
if (socket.destroyed) {
|
|
1073
|
+
return
|
|
1074
|
+
}
|
|
1075
|
+
}
|
|
1076
|
+
|
|
1010
1077
|
if (client[kSize] === 0) {
|
|
1011
1078
|
if (socket[kParser].timeoutType !== TIMEOUT_KEEP_ALIVE) {
|
|
1012
1079
|
socket[kParser].setTimeout(client[kKeepAliveTimeoutValue], TIMEOUT_KEEP_ALIVE)
|
|
@@ -1105,6 +1172,7 @@ function writeH1 (client, request) {
|
|
|
1105
1172
|
}
|
|
1106
1173
|
|
|
1107
1174
|
const socket = client[kSocket]
|
|
1175
|
+
clearIdleSocketValidation(socket)
|
|
1108
1176
|
|
|
1109
1177
|
/**
|
|
1110
1178
|
* @param {Error} [err]
|
package/lib/dispatcher/client.js
CHANGED
|
@@ -114,7 +114,8 @@ class Client extends DispatcherBase {
|
|
|
114
114
|
useH2c,
|
|
115
115
|
initialWindowSize,
|
|
116
116
|
connectionWindowSize,
|
|
117
|
-
pingInterval
|
|
117
|
+
pingInterval,
|
|
118
|
+
webSocket
|
|
118
119
|
} = {}) {
|
|
119
120
|
if (keepAlive !== undefined) {
|
|
120
121
|
throw new InvalidArgumentError('unsupported keepAlive, use pipelining=0 instead')
|
|
@@ -222,7 +223,7 @@ class Client extends DispatcherBase {
|
|
|
222
223
|
throw new InvalidArgumentError('pingInterval must be a positive integer, greater or equal to 0')
|
|
223
224
|
}
|
|
224
225
|
|
|
225
|
-
super()
|
|
226
|
+
super({ webSocket })
|
|
226
227
|
|
|
227
228
|
if (typeof connect !== 'function') {
|
|
228
229
|
connect = buildConnector({
|
|
@@ -11,6 +11,7 @@ const { kDestroy, kClose, kClosed, kDestroyed, kDispatch } = require('../core/sy
|
|
|
11
11
|
|
|
12
12
|
const kOnDestroyed = Symbol('onDestroyed')
|
|
13
13
|
const kOnClosed = Symbol('onClosed')
|
|
14
|
+
const kWebSocketOptions = Symbol('webSocketOptions')
|
|
14
15
|
|
|
15
16
|
class DispatcherBase extends Dispatcher {
|
|
16
17
|
/** @type {boolean} */
|
|
@@ -25,6 +26,24 @@ class DispatcherBase extends Dispatcher {
|
|
|
25
26
|
/** @type {Array<Function>|null} */
|
|
26
27
|
[kOnClosed] = null
|
|
27
28
|
|
|
29
|
+
/**
|
|
30
|
+
* @param {import('../../types/dispatcher').DispatcherOptions} [opts]
|
|
31
|
+
*/
|
|
32
|
+
constructor (opts) {
|
|
33
|
+
super()
|
|
34
|
+
this[kWebSocketOptions] = opts?.webSocket ?? {}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* @returns {import('../../types/dispatcher').WebSocketOptions}
|
|
39
|
+
*/
|
|
40
|
+
get webSocketOptions () {
|
|
41
|
+
return {
|
|
42
|
+
maxFragments: this[kWebSocketOptions].maxFragments ?? 131072,
|
|
43
|
+
maxPayloadSize: this[kWebSocketOptions].maxPayloadSize ?? 128 * 1024 * 1024 // 128 MB default
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
28
47
|
/** @returns {boolean} */
|
|
29
48
|
get destroyed () {
|
|
30
49
|
return this[kDestroyed]
|
package/lib/dispatcher/pool.js
CHANGED
|
@@ -142,7 +142,8 @@ class ProxyAgent extends DispatcherBase {
|
|
|
142
142
|
factory: agentFactory,
|
|
143
143
|
username: opts.username || username,
|
|
144
144
|
password: opts.password || password,
|
|
145
|
-
proxyTls: opts.proxyTls
|
|
145
|
+
proxyTls: opts.proxyTls,
|
|
146
|
+
requestTls: opts.requestTls
|
|
146
147
|
})
|
|
147
148
|
}
|
|
148
149
|
|
|
@@ -1,12 +1,11 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
|
-
const net = require('node:net')
|
|
4
3
|
const { URL } = require('node:url')
|
|
5
4
|
|
|
6
5
|
let tls // include tls conditionally since it is not always available
|
|
7
6
|
const DispatcherBase = require('./dispatcher-base')
|
|
8
7
|
const { InvalidArgumentError } = require('../core/errors')
|
|
9
|
-
const { Socks5Client } = require('../core/socks5-client')
|
|
8
|
+
const { Socks5Client, STATES } = require('../core/socks5-client')
|
|
10
9
|
const { kDispatch, kClose, kDestroy } = require('../core/symbols')
|
|
11
10
|
const Pool = require('./pool')
|
|
12
11
|
const buildConnector = require('../core/connect')
|
|
@@ -17,8 +16,10 @@ const debug = debuglog('undici:socks5-proxy')
|
|
|
17
16
|
const kProxyUrl = Symbol('proxy url')
|
|
18
17
|
const kProxyHeaders = Symbol('proxy headers')
|
|
19
18
|
const kProxyAuth = Symbol('proxy auth')
|
|
20
|
-
const
|
|
19
|
+
const kProxyProtocol = Symbol('proxy protocol')
|
|
20
|
+
const kPools = Symbol('pools')
|
|
21
21
|
const kConnector = Symbol('connector')
|
|
22
|
+
const kRequestTls = Symbol('request tls settings')
|
|
22
23
|
|
|
23
24
|
// Static flag to ensure warning is only emitted once per process
|
|
24
25
|
let experimentalWarningEmitted = false
|
|
@@ -52,6 +53,8 @@ class Socks5ProxyAgent extends DispatcherBase {
|
|
|
52
53
|
|
|
53
54
|
this[kProxyUrl] = url
|
|
54
55
|
this[kProxyHeaders] = options.headers || {}
|
|
56
|
+
this[kProxyProtocol] = options.proxyTls ? 'https:' : 'http:'
|
|
57
|
+
this[kRequestTls] = options.requestTls
|
|
55
58
|
|
|
56
59
|
// Extract auth from URL or options
|
|
57
60
|
this[kProxyAuth] = {
|
|
@@ -65,8 +68,8 @@ class Socks5ProxyAgent extends DispatcherBase {
|
|
|
65
68
|
servername: options.proxyTls?.servername || url.hostname
|
|
66
69
|
})
|
|
67
70
|
|
|
68
|
-
//
|
|
69
|
-
this[
|
|
71
|
+
// Pools for the actual HTTP connections (with SOCKS5 tunnel connect function), keyed by origin
|
|
72
|
+
this[kPools] = new Map()
|
|
70
73
|
}
|
|
71
74
|
|
|
72
75
|
/**
|
|
@@ -80,23 +83,18 @@ class Socks5ProxyAgent extends DispatcherBase {
|
|
|
80
83
|
|
|
81
84
|
// Connect to the SOCKS5 proxy
|
|
82
85
|
const socket = await new Promise((resolve, reject) => {
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
resolve(socket)
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
const onError = (err) => {
|
|
89
|
-
socket.removeListener('connect', onConnect)
|
|
90
|
-
reject(err)
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
const socket = net.connect({
|
|
86
|
+
this[kConnector]({
|
|
87
|
+
hostname: proxyHost,
|
|
94
88
|
host: proxyHost,
|
|
95
|
-
port: proxyPort
|
|
89
|
+
port: proxyPort,
|
|
90
|
+
protocol: this[kProxyProtocol]
|
|
91
|
+
}, (err, socket) => {
|
|
92
|
+
if (err) {
|
|
93
|
+
reject(err)
|
|
94
|
+
} else {
|
|
95
|
+
resolve(socket)
|
|
96
|
+
}
|
|
96
97
|
})
|
|
97
|
-
|
|
98
|
-
socket.once('connect', onConnect)
|
|
99
|
-
socket.once('error', onError)
|
|
100
98
|
})
|
|
101
99
|
|
|
102
100
|
// Create SOCKS5 client
|
|
@@ -130,7 +128,7 @@ class Socks5ProxyAgent extends DispatcherBase {
|
|
|
130
128
|
}
|
|
131
129
|
|
|
132
130
|
// Check if already authenticated (for NO_AUTH method)
|
|
133
|
-
if (socks5Client.state ===
|
|
131
|
+
if (socks5Client.state === STATES.AUTHENTICATED) {
|
|
134
132
|
clearTimeout(timeout)
|
|
135
133
|
resolve()
|
|
136
134
|
} else {
|
|
@@ -171,15 +169,17 @@ class Socks5ProxyAgent extends DispatcherBase {
|
|
|
171
169
|
/**
|
|
172
170
|
* Dispatch a request through the SOCKS5 proxy
|
|
173
171
|
*/
|
|
174
|
-
|
|
172
|
+
[kDispatch] (opts, handler) {
|
|
175
173
|
const { origin } = opts
|
|
176
174
|
|
|
177
175
|
debug('dispatching request to', origin, 'via SOCKS5')
|
|
178
176
|
|
|
179
177
|
try {
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
178
|
+
const originKey = String(origin)
|
|
179
|
+
let pool = this[kPools].get(originKey)
|
|
180
|
+
// Create a Pool per origin so requests are not routed to the wrong host
|
|
181
|
+
if (!pool || pool.destroyed || pool.closed) {
|
|
182
|
+
pool = new Pool(origin, {
|
|
183
183
|
pipelining: opts.pipelining,
|
|
184
184
|
connections: opts.connections,
|
|
185
185
|
connect: async (connectOpts, callback) => {
|
|
@@ -201,9 +201,9 @@ class Socks5ProxyAgent extends DispatcherBase {
|
|
|
201
201
|
}
|
|
202
202
|
debug('upgrading to TLS')
|
|
203
203
|
finalSocket = tls.connect({
|
|
204
|
+
...this[kRequestTls],
|
|
204
205
|
socket,
|
|
205
|
-
servername: targetHost
|
|
206
|
-
...connectOpts.tls || {}
|
|
206
|
+
servername: this[kRequestTls]?.servername || targetHost
|
|
207
207
|
})
|
|
208
208
|
|
|
209
209
|
await new Promise((resolve, reject) => {
|
|
@@ -219,14 +219,19 @@ class Socks5ProxyAgent extends DispatcherBase {
|
|
|
219
219
|
}
|
|
220
220
|
}
|
|
221
221
|
})
|
|
222
|
+
this[kPools].set(originKey, pool)
|
|
222
223
|
}
|
|
223
224
|
|
|
224
|
-
// Dispatch the request through the pool
|
|
225
|
-
return
|
|
225
|
+
// Dispatch the request through the per-origin pool
|
|
226
|
+
return pool[kDispatch](opts, handler)
|
|
226
227
|
} catch (err) {
|
|
227
228
|
debug('dispatch error:', err)
|
|
228
|
-
if (typeof handler.
|
|
229
|
+
if (typeof handler.onResponseError === 'function') {
|
|
230
|
+
handler.onResponseError(null, err)
|
|
231
|
+
return false
|
|
232
|
+
} else if (typeof handler.onError === 'function') {
|
|
229
233
|
handler.onError(err)
|
|
234
|
+
return false
|
|
230
235
|
} else {
|
|
231
236
|
throw err
|
|
232
237
|
}
|
|
@@ -234,15 +239,21 @@ class Socks5ProxyAgent extends DispatcherBase {
|
|
|
234
239
|
}
|
|
235
240
|
|
|
236
241
|
async [kClose] () {
|
|
237
|
-
|
|
238
|
-
|
|
242
|
+
const closePromises = []
|
|
243
|
+
for (const pool of this[kPools].values()) {
|
|
244
|
+
closePromises.push(pool.close())
|
|
239
245
|
}
|
|
246
|
+
this[kPools].clear()
|
|
247
|
+
await Promise.all(closePromises)
|
|
240
248
|
}
|
|
241
249
|
|
|
242
250
|
async [kDestroy] (err) {
|
|
243
|
-
|
|
244
|
-
|
|
251
|
+
const destroyPromises = []
|
|
252
|
+
for (const pool of this[kPools].values()) {
|
|
253
|
+
destroyPromises.push(pool.destroy(err))
|
|
245
254
|
}
|
|
255
|
+
this[kPools].clear()
|
|
256
|
+
await Promise.all(destroyPromises)
|
|
246
257
|
}
|
|
247
258
|
}
|
|
248
259
|
|
package/lib/util/cache.js
CHANGED
|
@@ -228,6 +228,10 @@ function parseCacheControlHeader (header) {
|
|
|
228
228
|
headers[headers.length - 1] = lastHeader
|
|
229
229
|
}
|
|
230
230
|
|
|
231
|
+
for (let j = 0; j < headers.length; j++) {
|
|
232
|
+
headers[j] = headers[j].trim()
|
|
233
|
+
}
|
|
234
|
+
|
|
231
235
|
if (key in output) {
|
|
232
236
|
output[key] = output[key].concat(headers)
|
|
233
237
|
} else {
|
|
@@ -236,10 +240,12 @@ function parseCacheControlHeader (header) {
|
|
|
236
240
|
}
|
|
237
241
|
} else {
|
|
238
242
|
// Something like `no-cache="some-header"`
|
|
243
|
+
const fieldName = value.trim()
|
|
244
|
+
|
|
239
245
|
if (key in output) {
|
|
240
|
-
output[key] = output[key].concat(
|
|
246
|
+
output[key] = output[key].concat(fieldName)
|
|
241
247
|
} else {
|
|
242
|
-
output[key] = [
|
|
248
|
+
output[key] = [fieldName]
|
|
243
249
|
}
|
|
244
250
|
}
|
|
245
251
|
|
package/lib/web/cookies/parse.js
CHANGED
|
@@ -4,7 +4,6 @@ const { collectASequenceOfCodePointsFast } = require('../infra')
|
|
|
4
4
|
const { maxNameValuePairSize, maxAttributeValueSize } = require('./constants')
|
|
5
5
|
const { isCTLExcludingHtab } = require('./util')
|
|
6
6
|
const assert = require('node:assert')
|
|
7
|
-
const { unescape: qsUnescape } = require('node:querystring')
|
|
8
7
|
|
|
9
8
|
/**
|
|
10
9
|
* @description Parses the field-value attributes of a set-cookie header string.
|
|
@@ -82,7 +81,7 @@ function parseSetCookie (header) {
|
|
|
82
81
|
// store arbitrary data in a cookie-value SHOULD encode that data, for
|
|
83
82
|
// example, using Base64 [RFC4648].
|
|
84
83
|
return {
|
|
85
|
-
name, value
|
|
84
|
+
name, value, ...parseUnparsedAttributes(unparsedAttributes)
|
|
86
85
|
}
|
|
87
86
|
}
|
|
88
87
|
|
|
@@ -280,32 +279,25 @@ function parseUnparsedAttributes (unparsedAttributes, cookieAttributeList = {})
|
|
|
280
279
|
// If the attribute-name case-insensitively matches the string
|
|
281
280
|
// "SameSite", the user agent MUST process the cookie-av as follows:
|
|
282
281
|
|
|
283
|
-
// 1. Let enforcement be "Default".
|
|
284
|
-
let enforcement = 'Default'
|
|
285
|
-
|
|
286
282
|
const attributeValueLowercase = attributeValue.toLowerCase()
|
|
287
|
-
// 2. If cookie-av's attribute-value is a case-insensitive match for
|
|
288
|
-
// "None", set enforcement to "None".
|
|
289
|
-
if (attributeValueLowercase.includes('none')) {
|
|
290
|
-
enforcement = 'None'
|
|
291
|
-
}
|
|
292
283
|
|
|
293
|
-
//
|
|
294
|
-
// "
|
|
295
|
-
|
|
296
|
-
|
|
284
|
+
// 1. If cookie-av's attribute-value is a case-insensitive match for
|
|
285
|
+
// "None", append an attribute to the cookie-attribute-list with an
|
|
286
|
+
// attribute-name of "SameSite" and an attribute-value of "None".
|
|
287
|
+
if (attributeValueLowercase === 'none') {
|
|
288
|
+
cookieAttributeList.sameSite = 'None'
|
|
289
|
+
} else if (attributeValueLowercase === 'strict') {
|
|
290
|
+
// 2. If cookie-av's attribute-value is a case-insensitive match for
|
|
291
|
+
// "Strict", append an attribute to the cookie-attribute-list with
|
|
292
|
+
// an attribute-name of "SameSite" and an attribute-value of
|
|
293
|
+
// "Strict".
|
|
294
|
+
cookieAttributeList.sameSite = 'Strict'
|
|
295
|
+
} else if (attributeValueLowercase === 'lax') {
|
|
296
|
+
// 3. If cookie-av's attribute-value is a case-insensitive match for
|
|
297
|
+
// "Lax", append an attribute to the cookie-attribute-list with an
|
|
298
|
+
// attribute-name of "SameSite" and an attribute-value of "Lax".
|
|
299
|
+
cookieAttributeList.sameSite = 'Lax'
|
|
297
300
|
}
|
|
298
|
-
|
|
299
|
-
// 4. If cookie-av's attribute-value is a case-insensitive match for
|
|
300
|
-
// "Lax", set enforcement to "Lax".
|
|
301
|
-
if (attributeValueLowercase.includes('lax')) {
|
|
302
|
-
enforcement = 'Lax'
|
|
303
|
-
}
|
|
304
|
-
|
|
305
|
-
// 5. Append an attribute to the cookie-attribute-list with an
|
|
306
|
-
// attribute-name of "SameSite" and an attribute-value of
|
|
307
|
-
// enforcement.
|
|
308
|
-
cookieAttributeList.sameSite = enforcement
|
|
309
301
|
} else {
|
|
310
302
|
cookieAttributeList.unparsed ??= []
|
|
311
303
|
|
|
@@ -8,40 +8,35 @@ const tail = Buffer.from([0x00, 0x00, 0xff, 0xff])
|
|
|
8
8
|
const kBuffer = Symbol('kBuffer')
|
|
9
9
|
const kLength = Symbol('kLength')
|
|
10
10
|
|
|
11
|
-
// Default maximum decompressed message size: 4 MB
|
|
12
|
-
const kDefaultMaxDecompressedSize = 4 * 1024 * 1024
|
|
13
|
-
|
|
14
11
|
class PerMessageDeflate {
|
|
15
12
|
/** @type {import('node:zlib').InflateRaw} */
|
|
16
13
|
#inflate
|
|
17
14
|
|
|
18
15
|
#options = {}
|
|
19
16
|
|
|
20
|
-
|
|
21
|
-
#aborted = false
|
|
22
|
-
|
|
23
|
-
/** @type {Function|null} */
|
|
24
|
-
#currentCallback = null
|
|
17
|
+
#maxPayloadSize = 0
|
|
25
18
|
|
|
26
19
|
/**
|
|
27
20
|
* @param {Map<string, string>} extensions
|
|
28
21
|
*/
|
|
29
|
-
constructor (extensions) {
|
|
22
|
+
constructor (extensions, options) {
|
|
30
23
|
this.#options.serverNoContextTakeover = extensions.has('server_no_context_takeover')
|
|
31
24
|
this.#options.serverMaxWindowBits = extensions.get('server_max_window_bits')
|
|
25
|
+
|
|
26
|
+
this.#maxPayloadSize = options.maxPayloadSize
|
|
32
27
|
}
|
|
33
28
|
|
|
29
|
+
/**
|
|
30
|
+
* Decompress a compressed payload.
|
|
31
|
+
* @param {Buffer} chunk Compressed data
|
|
32
|
+
* @param {boolean} fin Final fragment flag
|
|
33
|
+
* @param {Function} callback Callback function
|
|
34
|
+
*/
|
|
34
35
|
decompress (chunk, fin, callback) {
|
|
35
36
|
// An endpoint uses the following algorithm to decompress a message.
|
|
36
37
|
// 1. Append 4 octets of 0x00 0x00 0xff 0xff to the tail end of the
|
|
37
38
|
// payload of the message.
|
|
38
39
|
// 2. Decompress the resulting data using DEFLATE.
|
|
39
|
-
|
|
40
|
-
if (this.#aborted) {
|
|
41
|
-
callback(new MessageSizeExceededError())
|
|
42
|
-
return
|
|
43
|
-
}
|
|
44
|
-
|
|
45
40
|
if (!this.#inflate) {
|
|
46
41
|
let windowBits = Z_DEFAULT_WINDOWBITS
|
|
47
42
|
|
|
@@ -64,23 +59,12 @@ class PerMessageDeflate {
|
|
|
64
59
|
this.#inflate[kLength] = 0
|
|
65
60
|
|
|
66
61
|
this.#inflate.on('data', (data) => {
|
|
67
|
-
if (this.#aborted) {
|
|
68
|
-
return
|
|
69
|
-
}
|
|
70
|
-
|
|
71
62
|
this.#inflate[kLength] += data.length
|
|
72
63
|
|
|
73
|
-
if (this.#inflate[kLength] >
|
|
74
|
-
|
|
64
|
+
if (this.#maxPayloadSize > 0 && this.#inflate[kLength] > this.#maxPayloadSize) {
|
|
65
|
+
callback(new MessageSizeExceededError())
|
|
75
66
|
this.#inflate.removeAllListeners()
|
|
76
|
-
this.#inflate.destroy()
|
|
77
67
|
this.#inflate = null
|
|
78
|
-
|
|
79
|
-
if (this.#currentCallback) {
|
|
80
|
-
const cb = this.#currentCallback
|
|
81
|
-
this.#currentCallback = null
|
|
82
|
-
cb(new MessageSizeExceededError())
|
|
83
|
-
}
|
|
84
68
|
return
|
|
85
69
|
}
|
|
86
70
|
|
|
@@ -93,14 +77,13 @@ class PerMessageDeflate {
|
|
|
93
77
|
})
|
|
94
78
|
}
|
|
95
79
|
|
|
96
|
-
this.#currentCallback = callback
|
|
97
80
|
this.#inflate.write(chunk)
|
|
98
81
|
if (fin) {
|
|
99
82
|
this.#inflate.write(tail)
|
|
100
83
|
}
|
|
101
84
|
|
|
102
85
|
this.#inflate.flush(() => {
|
|
103
|
-
if (
|
|
86
|
+
if (!this.#inflate) {
|
|
104
87
|
return
|
|
105
88
|
}
|
|
106
89
|
|
|
@@ -108,7 +91,6 @@ class PerMessageDeflate {
|
|
|
108
91
|
|
|
109
92
|
this.#inflate[kBuffer].length = 0
|
|
110
93
|
this.#inflate[kLength] = 0
|
|
111
|
-
this.#currentCallback = null
|
|
112
94
|
|
|
113
95
|
callback(null, full)
|
|
114
96
|
})
|
|
@@ -39,18 +39,27 @@ class ByteParser extends Writable {
|
|
|
39
39
|
/** @type {import('./websocket').Handler} */
|
|
40
40
|
#handler
|
|
41
41
|
|
|
42
|
+
/** @type {number} */
|
|
43
|
+
#maxFragments
|
|
44
|
+
|
|
45
|
+
/** @type {number} */
|
|
46
|
+
#maxPayloadSize
|
|
47
|
+
|
|
42
48
|
/**
|
|
43
49
|
* @param {import('./websocket').Handler} handler
|
|
44
50
|
* @param {Map<string, string>|null} extensions
|
|
51
|
+
* @param {{ maxFragments?: number, maxPayloadSize?: number }} [options]
|
|
45
52
|
*/
|
|
46
|
-
constructor (handler, extensions) {
|
|
53
|
+
constructor (handler, extensions, options = {}) {
|
|
47
54
|
super()
|
|
48
55
|
|
|
49
56
|
this.#handler = handler
|
|
50
57
|
this.#extensions = extensions == null ? new Map() : extensions
|
|
58
|
+
this.#maxFragments = options.maxFragments ?? 0
|
|
59
|
+
this.#maxPayloadSize = options.maxPayloadSize ?? 0
|
|
51
60
|
|
|
52
61
|
if (this.#extensions.has('permessage-deflate')) {
|
|
53
|
-
this.#extensions.set('permessage-deflate', new PerMessageDeflate(extensions))
|
|
62
|
+
this.#extensions.set('permessage-deflate', new PerMessageDeflate(extensions, options))
|
|
54
63
|
}
|
|
55
64
|
}
|
|
56
65
|
|
|
@@ -66,6 +75,19 @@ class ByteParser extends Writable {
|
|
|
66
75
|
this.run(callback)
|
|
67
76
|
}
|
|
68
77
|
|
|
78
|
+
#validatePayloadLength () {
|
|
79
|
+
if (
|
|
80
|
+
this.#maxPayloadSize > 0 &&
|
|
81
|
+
!isControlFrame(this.#info.opcode) &&
|
|
82
|
+
this.#info.payloadLength + this.#fragmentsBytes > this.#maxPayloadSize
|
|
83
|
+
) {
|
|
84
|
+
failWebsocketConnection(this.#handler, 1009, 'Payload size exceeds maximum allowed size')
|
|
85
|
+
return false
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
return true
|
|
89
|
+
}
|
|
90
|
+
|
|
69
91
|
/**
|
|
70
92
|
* Runs whenever a new chunk is received.
|
|
71
93
|
* Callback is called whenever there are no more chunks buffering,
|
|
@@ -154,6 +176,10 @@ class ByteParser extends Writable {
|
|
|
154
176
|
if (payloadLength <= 125) {
|
|
155
177
|
this.#info.payloadLength = payloadLength
|
|
156
178
|
this.#state = parserStates.READ_DATA
|
|
179
|
+
|
|
180
|
+
if (!this.#validatePayloadLength()) {
|
|
181
|
+
return
|
|
182
|
+
}
|
|
157
183
|
} else if (payloadLength === 126) {
|
|
158
184
|
this.#state = parserStates.PAYLOADLENGTH_16
|
|
159
185
|
} else if (payloadLength === 127) {
|
|
@@ -178,6 +204,10 @@ class ByteParser extends Writable {
|
|
|
178
204
|
|
|
179
205
|
this.#info.payloadLength = buffer.readUInt16BE(0)
|
|
180
206
|
this.#state = parserStates.READ_DATA
|
|
207
|
+
|
|
208
|
+
if (!this.#validatePayloadLength()) {
|
|
209
|
+
return
|
|
210
|
+
}
|
|
181
211
|
} else if (this.#state === parserStates.PAYLOADLENGTH_64) {
|
|
182
212
|
if (this.#byteOffset < 8) {
|
|
183
213
|
return callback()
|
|
@@ -200,6 +230,10 @@ class ByteParser extends Writable {
|
|
|
200
230
|
|
|
201
231
|
this.#info.payloadLength = lower
|
|
202
232
|
this.#state = parserStates.READ_DATA
|
|
233
|
+
|
|
234
|
+
if (!this.#validatePayloadLength()) {
|
|
235
|
+
return
|
|
236
|
+
}
|
|
203
237
|
} else if (this.#state === parserStates.READ_DATA) {
|
|
204
238
|
if (this.#byteOffset < this.#info.payloadLength) {
|
|
205
239
|
return callback()
|
|
@@ -212,7 +246,9 @@ class ByteParser extends Writable {
|
|
|
212
246
|
this.#state = parserStates.INFO
|
|
213
247
|
} else {
|
|
214
248
|
if (!this.#info.compressed) {
|
|
215
|
-
this.writeFragments(body)
|
|
249
|
+
if (!this.writeFragments(body)) {
|
|
250
|
+
return
|
|
251
|
+
}
|
|
216
252
|
|
|
217
253
|
// If the frame is not fragmented, a message has been received.
|
|
218
254
|
// If the frame is fragmented, it will terminate with a fin bit set
|
|
@@ -224,29 +260,41 @@ class ByteParser extends Writable {
|
|
|
224
260
|
|
|
225
261
|
this.#state = parserStates.INFO
|
|
226
262
|
} else {
|
|
227
|
-
this.#extensions.get('permessage-deflate').decompress(
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
263
|
+
this.#extensions.get('permessage-deflate').decompress(
|
|
264
|
+
body,
|
|
265
|
+
this.#info.fin,
|
|
266
|
+
(error, data) => {
|
|
267
|
+
if (error) {
|
|
268
|
+
const code = error instanceof MessageSizeExceededError ? 1009 : 1007
|
|
269
|
+
failWebsocketConnection(this.#handler, code, error.message)
|
|
270
|
+
return
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
if (!this.writeFragments(data)) {
|
|
274
|
+
return
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
// Check cumulative fragment size
|
|
278
|
+
if (this.#maxPayloadSize > 0 && this.#fragmentsBytes > this.#maxPayloadSize) {
|
|
279
|
+
failWebsocketConnection(this.#handler, 1009, new MessageSizeExceededError().message)
|
|
280
|
+
return
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
if (!this.#info.fin) {
|
|
284
|
+
this.#state = parserStates.INFO
|
|
285
|
+
this.#loop = true
|
|
286
|
+
this.run(callback)
|
|
287
|
+
return
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
websocketMessageReceived(this.#handler, this.#info.binaryType, this.consumeFragments())
|
|
236
291
|
|
|
237
|
-
if (!this.#info.fin) {
|
|
238
|
-
this.#state = parserStates.INFO
|
|
239
292
|
this.#loop = true
|
|
293
|
+
this.#state = parserStates.INFO
|
|
240
294
|
this.run(callback)
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
websocketMessageReceived(this.#handler, this.#info.binaryType, this.consumeFragments())
|
|
245
|
-
|
|
246
|
-
this.#loop = true
|
|
247
|
-
this.#state = parserStates.INFO
|
|
248
|
-
this.run(callback)
|
|
249
|
-
})
|
|
295
|
+
},
|
|
296
|
+
this.#fragmentsBytes
|
|
297
|
+
)
|
|
250
298
|
|
|
251
299
|
this.#loop = false
|
|
252
300
|
break
|
|
@@ -305,8 +353,17 @@ class ByteParser extends Writable {
|
|
|
305
353
|
}
|
|
306
354
|
|
|
307
355
|
writeFragments (fragment) {
|
|
356
|
+
if (
|
|
357
|
+
this.#maxFragments > 0 &&
|
|
358
|
+
this.#fragments.length === this.#maxFragments
|
|
359
|
+
) {
|
|
360
|
+
failWebsocketConnection(this.#handler, 1008, 'Too many message fragments')
|
|
361
|
+
return false
|
|
362
|
+
}
|
|
363
|
+
|
|
308
364
|
this.#fragmentsBytes += fragment.length
|
|
309
365
|
this.#fragments.push(fragment)
|
|
366
|
+
return true
|
|
310
367
|
}
|
|
311
368
|
|
|
312
369
|
consumeFragments () {
|
|
@@ -258,7 +258,14 @@ class WebSocketStream {
|
|
|
258
258
|
#onConnectionEstablished (response, parsedExtensions) {
|
|
259
259
|
this.#handler.socket = response.socket
|
|
260
260
|
|
|
261
|
-
|
|
261
|
+
// Get options from dispatcher options
|
|
262
|
+
const maxFragments = this.#handler.controller.dispatcher?.webSocketOptions?.maxFragments
|
|
263
|
+
const maxPayloadSize = this.#handler.controller.dispatcher?.webSocketOptions?.maxPayloadSize
|
|
264
|
+
|
|
265
|
+
const parser = new ByteParser(this.#handler, parsedExtensions, {
|
|
266
|
+
maxFragments,
|
|
267
|
+
maxPayloadSize
|
|
268
|
+
})
|
|
262
269
|
parser.on('drain', () => this.#handler.onParserDrain())
|
|
263
270
|
parser.on('error', (err) => this.#handler.onParserError(err))
|
|
264
271
|
|
|
@@ -468,7 +468,14 @@ class WebSocket extends EventTarget {
|
|
|
468
468
|
// once this happens, the connection is open
|
|
469
469
|
this.#handler.socket = response.socket
|
|
470
470
|
|
|
471
|
-
const
|
|
471
|
+
const webSocketOptions = this.#handler.controller.dispatcher?.webSocketOptions
|
|
472
|
+
const maxFragments = webSocketOptions?.maxFragments
|
|
473
|
+
const maxPayloadSize = webSocketOptions?.maxPayloadSize
|
|
474
|
+
|
|
475
|
+
const parser = new ByteParser(this.#handler, parsedExtensions, {
|
|
476
|
+
maxFragments,
|
|
477
|
+
maxPayloadSize
|
|
478
|
+
})
|
|
472
479
|
parser.on('drain', () => this.#handler.onParserDrain())
|
|
473
480
|
parser.on('error', (err) => this.#handler.onParserError(err))
|
|
474
481
|
|
package/package.json
CHANGED
package/types/client.d.ts
CHANGED
|
@@ -107,6 +107,8 @@ export declare namespace Client {
|
|
|
107
107
|
* @default 60000
|
|
108
108
|
*/
|
|
109
109
|
pingInterval?: number;
|
|
110
|
+
/** WebSocket-specific configuration options. */
|
|
111
|
+
webSocket?: WebSocketOptions;
|
|
110
112
|
}
|
|
111
113
|
export interface SocketInfo {
|
|
112
114
|
localAddress?: string
|
|
@@ -118,6 +120,20 @@ export declare namespace Client {
|
|
|
118
120
|
bytesWritten?: number
|
|
119
121
|
bytesRead?: number
|
|
120
122
|
}
|
|
123
|
+
export interface WebSocketOptions {
|
|
124
|
+
/**
|
|
125
|
+
* Maximum number of fragments in a message. Set to 0 to disable the limit.
|
|
126
|
+
* @default 131072
|
|
127
|
+
*/
|
|
128
|
+
maxFragments?: number;
|
|
129
|
+
/**
|
|
130
|
+
* Maximum allowed payload size in bytes for WebSocket messages.
|
|
131
|
+
* Applied to uncompressed messages, compressed frame payloads, and decompressed (permessage-deflate) messages.
|
|
132
|
+
* Set to 0 to disable the limit.
|
|
133
|
+
* @default 134217728 (128 MB)
|
|
134
|
+
*/
|
|
135
|
+
maxPayloadSize?: number;
|
|
136
|
+
}
|
|
121
137
|
}
|
|
122
138
|
|
|
123
139
|
export default Client
|