@netifyjs/netify 1.0.0-alpha.3 → 1.0.0-alpha.5
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/README.md +6 -6
- package/package.json +1 -1
- package/src/bootstrap/client/index.ts +1 -1
- package/src/bootstrap/dispatcher/index.ts +7 -0
- package/src/bootstrap/reconnect/index.ts +14 -4
- package/src/bootstrap/server/index.ts +10 -4
- package/src/buffer/index.ts +229 -88
- package/src/buffer/quantizer/index.ts +16 -3
- package/src/channel/context/index.ts +18 -11
- package/src/channel/handler/index.ts +1 -1
- package/src/channel/index.ts +30 -7
- package/src/channel/outbound/index.ts +28 -12
- package/src/channel/pipeline/index.ts +45 -5
- package/src/channel/queue/index.ts +3 -2
- package/src/channel/resolved/index.ts +13 -3
- package/src/channel/server/index.ts +19 -10
- package/src/cluster/index.ts +73 -32
- package/src/codec/binary/index.ts +19 -0
- package/src/codec/decoder/index.ts +223 -83
- package/src/codec/delimiter/index.ts +95 -37
- package/src/codec/encoder/index.ts +30 -5
- package/src/codec/fixed/index.ts +5 -1
- package/src/codec/http/aggregator/index.ts +45 -16
- package/src/codec/http/decoder/object/index.ts +156 -40
- package/src/codec/http/decoder/request/index.ts +6 -2
- package/src/codec/http/encoder/object/index.ts +2 -7
- package/src/codec/http/encoder/request/index.ts +5 -0
- package/src/codec/http/headers/index.ts +45 -2
- package/src/codec/http/message/full/request/index.ts +1 -1
- package/src/codec/http/message/full/response/index.ts +1 -1
- package/src/codec/http/message/request/index.ts +14 -1
- package/src/codec/http/message/response/index.ts +1 -1
- package/src/codec/http/version/index.ts +3 -0
- package/src/codec/length/index.ts +66 -44
- package/src/codec/message/decoder/index.ts +18 -1
- package/src/codec/message/encoder/index.ts +85 -8
- package/src/codec/prepender/index.ts +4 -1
- package/src/codec/string/decoder/index.ts +6 -1
- package/src/codec/websocket/decoder/index.ts +78 -3
- package/src/codec/websocket/encoder/index.ts +90 -6
- package/src/codec/websocket/frame/base/index.ts +19 -10
- package/src/codec/websocket/frame/text/index.ts +2 -2
- package/src/codec/websocket/protocol/index.ts +12 -0
- package/src/datagram/bootstrap/index.ts +1 -1
- package/src/datagram/transport/index.ts +30 -1
- package/src/handler/flush/index.ts +25 -5
- package/src/handler/idle/index.ts +120 -26
- package/src/handler/logging/index.ts +13 -1
- package/src/handler/metrics/index.ts +49 -16
- package/src/handler/offload/index.ts +7 -0
- package/src/handler/timeout/read/index.ts +1 -1
- package/src/handler/timeout/write/index.ts +104 -23
- package/src/handler/traffic/index.ts +251 -38
- package/src/index.ts +1 -0
- package/src/reliable/cipher/index.ts +49 -5
- package/src/reliable/cookie/index.ts +64 -2
- package/src/reliable/handler/index.ts +11 -15
- package/src/reliable/index.ts +7 -1
- package/src/reliable/lane/index.ts +277 -58
- package/src/reliable/peer/index.ts +125 -38
- package/src/serve/bootstrap/index.ts +82 -135
- package/src/serve/channel/index.ts +84 -2
- package/src/serve/gate/index.ts +215 -0
- package/src/serve/index.ts +6 -2
- package/src/serve/outbox/index.ts +63 -2
- package/src/serve/transport/index.ts +33 -3
- package/src/serve/writer/index.ts +4 -3
- package/src/shard/gateway/index.ts +102 -29
- package/src/shard/link/index.ts +161 -175
- package/src/worker/group/index.ts +231 -13
- package/src/worker/pool/index.ts +100 -19
- package/src/worker/service/index.ts +9 -1
|
@@ -74,6 +74,33 @@ export class HttpHeaders implements Iterable<HeaderEntry> {
|
|
|
74
74
|
return !HttpHeaders.forbidden.test(value)
|
|
75
75
|
}
|
|
76
76
|
|
|
77
|
+
/**
|
|
78
|
+
* Trims spaces and tabs
|
|
79
|
+
* @param text a raw value or list member
|
|
80
|
+
*/
|
|
81
|
+
public static trim(text: string): string {
|
|
82
|
+
let start = 0
|
|
83
|
+
let end = text.length
|
|
84
|
+
|
|
85
|
+
while (start < end && HttpHeaders.isBlank(text.charCodeAt(start))) {
|
|
86
|
+
start += 1
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
while (end > start && HttpHeaders.isBlank(text.charCodeAt(end - 1))) {
|
|
90
|
+
end -= 1
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
return start === 0 && end === text.length ? text : text.slice(start, end)
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Space or tab
|
|
98
|
+
* @param code character to check
|
|
99
|
+
*/
|
|
100
|
+
private static isBlank(code: number): boolean {
|
|
101
|
+
return code === 0x20 || code === 0x09
|
|
102
|
+
}
|
|
103
|
+
|
|
77
104
|
/**
|
|
78
105
|
* How many entries there are, repeats included
|
|
79
106
|
*/
|
|
@@ -152,7 +179,10 @@ export class HttpHeaders implements Iterable<HeaderEntry> {
|
|
|
152
179
|
while (start <= value.length) {
|
|
153
180
|
let end = value.indexOf(',', start)
|
|
154
181
|
if (end === -1) end = value.length
|
|
155
|
-
if (value.slice(start, end)
|
|
182
|
+
if (HttpHeaders.trim(value.slice(start, end)).toLowerCase() === wanted) {
|
|
183
|
+
return true
|
|
184
|
+
}
|
|
185
|
+
|
|
156
186
|
start = end + 1
|
|
157
187
|
}
|
|
158
188
|
|
|
@@ -168,7 +198,7 @@ export class HttpHeaders implements Iterable<HeaderEntry> {
|
|
|
168
198
|
|
|
169
199
|
for (const value of this.getAll(name)) {
|
|
170
200
|
for (const part of value.split(',')) {
|
|
171
|
-
const token =
|
|
201
|
+
const token = HttpHeaders.trim(part).toLowerCase()
|
|
172
202
|
if (token !== '') tokens.push(token)
|
|
173
203
|
}
|
|
174
204
|
}
|
|
@@ -259,6 +289,19 @@ export class HttpHeaders implements Iterable<HeaderEntry> {
|
|
|
259
289
|
])
|
|
260
290
|
}
|
|
261
291
|
|
|
292
|
+
/**
|
|
293
|
+
* Every header as wire lines
|
|
294
|
+
*/
|
|
295
|
+
public serialize(): string {
|
|
296
|
+
let text = ''
|
|
297
|
+
|
|
298
|
+
for (const header of this.list) {
|
|
299
|
+
text += `${header.name}: ${header.value}\r\n`
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
return text
|
|
303
|
+
}
|
|
304
|
+
|
|
262
305
|
/**
|
|
263
306
|
* A separate copy that can be changed freely
|
|
264
307
|
*/
|
|
@@ -26,7 +26,7 @@ export class FullHttpRequest extends HttpRequest {
|
|
|
26
26
|
* @param uri path and query
|
|
27
27
|
* @param content body as bytes or text
|
|
28
28
|
* @param version defaults to HTTP/1.1
|
|
29
|
-
* @param headers
|
|
29
|
+
* @param headers any to start with
|
|
30
30
|
* @param trailers usually left empty
|
|
31
31
|
*/
|
|
32
32
|
public constructor(
|
|
@@ -25,7 +25,7 @@ export class FullHttpResponse extends HttpResponse {
|
|
|
25
25
|
* @param status code and phrase to send
|
|
26
26
|
* @param content body as bytes or text
|
|
27
27
|
* @param version defaults to HTTP/1.1
|
|
28
|
-
* @param headers
|
|
28
|
+
* @param headers any to start with
|
|
29
29
|
* @param trailers usually left empty
|
|
30
30
|
*/
|
|
31
31
|
public constructor(
|
|
@@ -23,7 +23,7 @@ export class HttpRequest extends HttpMessage {
|
|
|
23
23
|
* @param method request method
|
|
24
24
|
* @param uri path and query
|
|
25
25
|
* @param version defaults to HTTP/1.1
|
|
26
|
-
* @param headers
|
|
26
|
+
* @param headers any to start with
|
|
27
27
|
*/
|
|
28
28
|
public constructor(
|
|
29
29
|
method: HttpMethod,
|
|
@@ -36,4 +36,17 @@ export class HttpRequest extends HttpMessage {
|
|
|
36
36
|
this.method = method
|
|
37
37
|
this.uri = uri
|
|
38
38
|
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Target has no spaces or control bytes
|
|
42
|
+
* @param uri request target to check
|
|
43
|
+
*/
|
|
44
|
+
public static isValidUri(uri: string): boolean {
|
|
45
|
+
for (let index = 0; index < uri.length; index++) {
|
|
46
|
+
const code = uri.charCodeAt(index)
|
|
47
|
+
if (code <= 0x20 || code === 0x7f) return false
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
return uri !== ''
|
|
51
|
+
}
|
|
39
52
|
}
|
|
@@ -17,7 +17,7 @@ export class HttpResponse extends HttpMessage {
|
|
|
17
17
|
* Builds a response head, HTTP/1.1 by default
|
|
18
18
|
* @param status code and phrase to send
|
|
19
19
|
* @param version defaults to HTTP/1.1
|
|
20
|
-
* @param headers
|
|
20
|
+
* @param headers any to start with
|
|
21
21
|
*/
|
|
22
22
|
public constructor(
|
|
23
23
|
status: HttpResponseStatus,
|
|
@@ -44,6 +44,9 @@ export class HttpVersion {
|
|
|
44
44
|
* @param text version as read off the wire
|
|
45
45
|
*/
|
|
46
46
|
public static parse(text: string): HttpVersion | null {
|
|
47
|
+
if (text === HttpVersion.HTTP_1_1.text) return HttpVersion.HTTP_1_1
|
|
48
|
+
if (text === HttpVersion.HTTP_1_0.text) return HttpVersion.HTTP_1_0
|
|
49
|
+
|
|
47
50
|
const match = /^HTTP\/(\d)\.(\d)$/.exec(text)
|
|
48
51
|
if (match === null) return null
|
|
49
52
|
|
|
@@ -89,16 +89,21 @@ export class LengthFieldBasedFrameDecoder extends ByteToMessageDecoder<ByteBuf>
|
|
|
89
89
|
private readonly headerLength: number
|
|
90
90
|
|
|
91
91
|
/**
|
|
92
|
-
*
|
|
92
|
+
* Inline reader for lengthAt, 0 for none
|
|
93
93
|
*/
|
|
94
|
-
private readonly fast: 0 | 1 | 2 | 4
|
|
94
|
+
private readonly fast: 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7
|
|
95
95
|
|
|
96
96
|
/**
|
|
97
97
|
* Sets up a decoder for length prefixed frames
|
|
98
98
|
* @param options field layout and limits
|
|
99
99
|
*/
|
|
100
100
|
public constructor(options: LengthFieldOptions) {
|
|
101
|
-
super(
|
|
101
|
+
super(
|
|
102
|
+
ByteToMessageDecoder.sized<LengthFieldOptions>(
|
|
103
|
+
options,
|
|
104
|
+
options.maxFrameLength,
|
|
105
|
+
),
|
|
106
|
+
)
|
|
102
107
|
|
|
103
108
|
const reason = LengthFieldBasedFrameDecoder.refusal(options)
|
|
104
109
|
if (reason !== null) throw new RangeError(reason)
|
|
@@ -111,12 +116,25 @@ export class LengthFieldBasedFrameDecoder extends ByteToMessageDecoder<ByteBuf>
|
|
|
111
116
|
this.endian = options.endian ?? 'big'
|
|
112
117
|
this.discardRemaining = 0
|
|
113
118
|
this.headerLength = this.lengthFieldOffset + this.lengthFieldLength
|
|
114
|
-
this.fast =
|
|
115
|
-
this.lengthFieldLength
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
119
|
+
this.fast = LengthFieldBasedFrameDecoder.layout(
|
|
120
|
+
this.lengthFieldLength,
|
|
121
|
+
this.endian,
|
|
122
|
+
)
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Picks the inline reader
|
|
127
|
+
* @param width bytes in the field
|
|
128
|
+
* @param endian its byte order
|
|
129
|
+
*/
|
|
130
|
+
private static layout(
|
|
131
|
+
width: LengthFieldLength,
|
|
132
|
+
endian: Endian,
|
|
133
|
+
): 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 {
|
|
134
|
+
if (width === 8) return 0
|
|
135
|
+
if (width === 1 || endian === 'big') return width
|
|
136
|
+
|
|
137
|
+
return width === 2 ? 5 : width === 3 ? 6 : 7
|
|
120
138
|
}
|
|
121
139
|
|
|
122
140
|
/**
|
|
@@ -142,6 +160,10 @@ export class LengthFieldBasedFrameDecoder extends ByteToMessageDecoder<ByteBuf>
|
|
|
142
160
|
return 'Length field is past the frame limit'
|
|
143
161
|
}
|
|
144
162
|
|
|
163
|
+
if (ByteToMessageDecoder.oversized(options, options.maxFrameLength)) {
|
|
164
|
+
return 'Frame limit is above the buffer limit'
|
|
165
|
+
}
|
|
166
|
+
|
|
145
167
|
return null
|
|
146
168
|
}
|
|
147
169
|
|
|
@@ -226,7 +248,7 @@ export class LengthFieldBasedFrameDecoder extends ByteToMessageDecoder<ByteBuf>
|
|
|
226
248
|
return bytes[at] ?? 0
|
|
227
249
|
case 2:
|
|
228
250
|
return (((bytes[at] ?? 0) << 8) | (bytes[at + 1] ?? 0)) >>> 0
|
|
229
|
-
|
|
251
|
+
case 4:
|
|
230
252
|
return (
|
|
231
253
|
(((bytes[at] ?? 0) << 24) |
|
|
232
254
|
((bytes[at + 1] ?? 0) << 16) |
|
|
@@ -234,6 +256,36 @@ export class LengthFieldBasedFrameDecoder extends ByteToMessageDecoder<ByteBuf>
|
|
|
234
256
|
(bytes[at + 3] ?? 0)) >>>
|
|
235
257
|
0
|
|
236
258
|
)
|
|
259
|
+
default:
|
|
260
|
+
return this.oddLengthAt(bytes, at)
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
/**
|
|
265
|
+
* Reads a 3 byte or little endian length
|
|
266
|
+
* @param bytes the backing array
|
|
267
|
+
* @param at where the field starts
|
|
268
|
+
*/
|
|
269
|
+
private oddLengthAt(bytes: Uint8Array, at: number): number {
|
|
270
|
+
const first = bytes[at] ?? 0
|
|
271
|
+
const second = bytes[at + 1] ?? 0
|
|
272
|
+
const third = bytes[at + 2] ?? 0
|
|
273
|
+
|
|
274
|
+
switch (this.fast) {
|
|
275
|
+
case 3:
|
|
276
|
+
return (first << 16) | (second << 8) | third
|
|
277
|
+
case 5:
|
|
278
|
+
return (second << 8) | first
|
|
279
|
+
case 6:
|
|
280
|
+
return (third << 16) | (second << 8) | first
|
|
281
|
+
default:
|
|
282
|
+
return (
|
|
283
|
+
(((bytes[at + 3] ?? 0) << 24) |
|
|
284
|
+
(third << 16) |
|
|
285
|
+
(second << 8) |
|
|
286
|
+
first) >>>
|
|
287
|
+
0
|
|
288
|
+
)
|
|
237
289
|
}
|
|
238
290
|
}
|
|
239
291
|
|
|
@@ -277,44 +329,14 @@ export class LengthFieldBasedFrameDecoder extends ByteToMessageDecoder<ByteBuf>
|
|
|
277
329
|
}
|
|
278
330
|
|
|
279
331
|
/**
|
|
280
|
-
*
|
|
332
|
+
* Peeks the length field
|
|
281
333
|
* @param input the buffer to peek into
|
|
282
334
|
*/
|
|
283
335
|
private peekLength(input: ByteBuf): number {
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
}
|
|
287
|
-
|
|
288
|
-
return this.lengthAt(
|
|
289
|
-
input.array,
|
|
290
|
-
input.arrayOffset + input.readerIndex + this.lengthFieldOffset,
|
|
291
|
-
)
|
|
292
|
-
}
|
|
336
|
+
const index = input.readerIndex + this.lengthFieldOffset
|
|
337
|
+
if (this.fast === 0) return Number(input.getUint64(index, this.endian))
|
|
293
338
|
|
|
294
|
-
|
|
295
|
-
* Reads the raw length, refusing 8 byte values that won't fit in a number
|
|
296
|
-
* @param input the buffer to peek into
|
|
297
|
-
* @param index absolute position, not relative to the reader
|
|
298
|
-
*/
|
|
299
|
-
private readLength(input: ByteBuf, index: number): number {
|
|
300
|
-
switch (this.lengthFieldLength) {
|
|
301
|
-
case 1:
|
|
302
|
-
return input.getUint8(index)
|
|
303
|
-
case 2:
|
|
304
|
-
return input.getUint16(index, this.endian)
|
|
305
|
-
case 3:
|
|
306
|
-
return input.getUint24(index, this.endian)
|
|
307
|
-
case 4:
|
|
308
|
-
return input.getUint32(index, this.endian)
|
|
309
|
-
case 8: {
|
|
310
|
-
const length = input.getUint64(index, this.endian)
|
|
311
|
-
if (length > BigInt(Number.MAX_SAFE_INTEGER)) {
|
|
312
|
-
throw new DecoderError('Frame is too large')
|
|
313
|
-
}
|
|
314
|
-
|
|
315
|
-
return Number(length)
|
|
316
|
-
}
|
|
317
|
-
}
|
|
339
|
+
return this.lengthAt(input.array, input.arrayOffset + index)
|
|
318
340
|
}
|
|
319
341
|
|
|
320
342
|
/**
|
|
@@ -11,6 +11,20 @@ export abstract class MessageToMessageDecoder<
|
|
|
11
11
|
I,
|
|
12
12
|
O = unknown,
|
|
13
13
|
> extends InboundHandlerAdapter {
|
|
14
|
+
/**
|
|
15
|
+
* Output array kept between reads, or null
|
|
16
|
+
*/
|
|
17
|
+
private spare: O[] | null
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Starts with no output
|
|
21
|
+
*/
|
|
22
|
+
public constructor() {
|
|
23
|
+
super()
|
|
24
|
+
|
|
25
|
+
this.spare = []
|
|
26
|
+
}
|
|
27
|
+
|
|
14
28
|
/**
|
|
15
29
|
* Decodes messages it accepts and lets the rest through
|
|
16
30
|
* @param context where decoded messages go
|
|
@@ -22,7 +36,8 @@ export abstract class MessageToMessageDecoder<
|
|
|
22
36
|
): Promise<void> {
|
|
23
37
|
if (!this.accepts(message)) return context.fireChannelRead(message)
|
|
24
38
|
|
|
25
|
-
const out
|
|
39
|
+
const out = this.spare ?? []
|
|
40
|
+
this.spare = null
|
|
26
41
|
let failure: DecoderError | null = null
|
|
27
42
|
try {
|
|
28
43
|
this.decode(context, message, out)
|
|
@@ -40,6 +55,8 @@ export abstract class MessageToMessageDecoder<
|
|
|
40
55
|
}
|
|
41
56
|
}
|
|
42
57
|
|
|
58
|
+
out.length = 0
|
|
59
|
+
this.spare = out
|
|
43
60
|
if (failure !== null) throw failure
|
|
44
61
|
return resolved
|
|
45
62
|
}
|
|
@@ -10,36 +10,113 @@ export abstract class MessageToMessageEncoder<
|
|
|
10
10
|
I,
|
|
11
11
|
O = unknown,
|
|
12
12
|
> extends OutboundHandlerAdapter {
|
|
13
|
+
/**
|
|
14
|
+
* Output array kept between writes, or null
|
|
15
|
+
*/
|
|
16
|
+
private spare: O[] | null
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Starts with an empty output array
|
|
20
|
+
*/
|
|
21
|
+
public constructor() {
|
|
22
|
+
super()
|
|
23
|
+
|
|
24
|
+
this.spare = []
|
|
25
|
+
}
|
|
26
|
+
|
|
13
27
|
/**
|
|
14
28
|
* Encodes messages it accepts and lets the rest through
|
|
15
29
|
* @param context where the results get written
|
|
16
30
|
* @param message anything heading out
|
|
17
31
|
*/
|
|
18
|
-
public override
|
|
32
|
+
public override write(
|
|
19
33
|
context: HandlerContext,
|
|
20
34
|
message: unknown,
|
|
21
35
|
): Promise<void> {
|
|
22
|
-
if (!this.accepts(message))
|
|
23
|
-
|
|
24
|
-
|
|
36
|
+
if (!this.accepts(message)) return context.write(message)
|
|
37
|
+
|
|
38
|
+
const out = this.spare ?? []
|
|
39
|
+
this.spare = null
|
|
40
|
+
try {
|
|
41
|
+
const failure = this.fill(context, message, out)
|
|
42
|
+
if (failure !== null) return Promise.reject(failure)
|
|
43
|
+
|
|
44
|
+
return MessageToMessageEncoder.send<O>(context, out)
|
|
45
|
+
} finally {
|
|
46
|
+
out.length = 0
|
|
47
|
+
this.spare = out
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Encodes and writes without a promise, false for other messages
|
|
53
|
+
* @param context where the results get written
|
|
54
|
+
* @param message anything heading out
|
|
55
|
+
*/
|
|
56
|
+
public writeSync(context: HandlerContext, message: unknown): boolean {
|
|
57
|
+
if (!this.accepts(message)) return false
|
|
58
|
+
|
|
59
|
+
const out = this.spare ?? []
|
|
60
|
+
this.spare = null
|
|
61
|
+
try {
|
|
62
|
+
const failure = this.fill(context, message, out)
|
|
63
|
+
if (failure !== null) throw failure
|
|
64
|
+
|
|
65
|
+
for (const encoded of out) context.writeSync(encoded)
|
|
66
|
+
} finally {
|
|
67
|
+
out.length = 0
|
|
68
|
+
this.spare = out
|
|
25
69
|
}
|
|
26
70
|
|
|
27
|
-
|
|
71
|
+
return true
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Runs encode, returning the error or null
|
|
76
|
+
* @param context handler context
|
|
77
|
+
* @param message one that passed accepts
|
|
78
|
+
* @param out where the results go
|
|
79
|
+
*/
|
|
80
|
+
private fill(
|
|
81
|
+
context: HandlerContext,
|
|
82
|
+
message: I,
|
|
83
|
+
out: O[],
|
|
84
|
+
): EncoderError | null {
|
|
28
85
|
try {
|
|
29
86
|
this.encode(context, message, out)
|
|
30
87
|
} catch (error) {
|
|
31
|
-
|
|
88
|
+
return error instanceof EncoderError
|
|
32
89
|
? error
|
|
33
90
|
: new EncoderError('Encoding failed', { cause: error })
|
|
34
91
|
}
|
|
35
92
|
|
|
36
|
-
|
|
93
|
+
return out.length === 0 ? new EncoderError('Encoder wrote nothing') : null
|
|
94
|
+
}
|
|
37
95
|
|
|
38
|
-
|
|
96
|
+
/**
|
|
97
|
+
* Writes every result
|
|
98
|
+
* @param context where the results get written
|
|
99
|
+
* @param out what encode produced
|
|
100
|
+
*/
|
|
101
|
+
private static send<O>(
|
|
102
|
+
context: HandlerContext,
|
|
103
|
+
out: readonly O[],
|
|
104
|
+
): Promise<void> {
|
|
105
|
+
if (out.length === 1) return context.write(out[0])
|
|
106
|
+
|
|
107
|
+
return MessageToMessageEncoder.all(
|
|
39
108
|
out.map((encoded: O): Promise<void> => context.write(encoded)),
|
|
40
109
|
)
|
|
41
110
|
}
|
|
42
111
|
|
|
112
|
+
/**
|
|
113
|
+
* Settles after every write
|
|
114
|
+
* @param writes one promise per result
|
|
115
|
+
*/
|
|
116
|
+
private static async all(writes: Promise<void>[]): Promise<void> {
|
|
117
|
+
await Promise.all<Promise<void>[]>(writes)
|
|
118
|
+
}
|
|
119
|
+
|
|
43
120
|
/**
|
|
44
121
|
* Says whether a message is one this encoder should handle
|
|
45
122
|
* @param message the outbound message
|
|
@@ -146,7 +146,10 @@ export class LengthFieldPrepender extends MessageToByteEncoder<
|
|
|
146
146
|
* @param context where bytes go
|
|
147
147
|
* @param message whatever is being written
|
|
148
148
|
*/
|
|
149
|
-
public writeSync(
|
|
149
|
+
public override writeSync(
|
|
150
|
+
context: HandlerContext,
|
|
151
|
+
message: unknown,
|
|
152
|
+
): boolean {
|
|
150
153
|
if (!this.accepts(message) || !context.writesToSocket) return false
|
|
151
154
|
|
|
152
155
|
const size =
|
|
@@ -20,7 +20,12 @@ export class StringDecoder extends MessageToMessageDecoder<ByteBuf, string> {
|
|
|
20
20
|
public constructor(encoding: Encoding = 'utf-8') {
|
|
21
21
|
super()
|
|
22
22
|
|
|
23
|
-
this.decoder = new TextDecoder(encoding
|
|
23
|
+
this.decoder = new TextDecoder(encoding, {
|
|
24
|
+
/**
|
|
25
|
+
* Keep a leading BOM
|
|
26
|
+
*/
|
|
27
|
+
ignoreBOM: true,
|
|
28
|
+
})
|
|
24
29
|
}
|
|
25
30
|
|
|
26
31
|
/**
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { isUtf8 } from 'node:buffer'
|
|
2
|
+
|
|
1
3
|
import { ByteBuf } from '../../../buffer/index.ts'
|
|
2
4
|
import type { HandlerContext } from '../../../channel/context/index.ts'
|
|
3
5
|
import { ByteToMessageDecoder } from '../../decoder/index.ts'
|
|
@@ -104,6 +106,21 @@ export class WebSocketFrameDecoder extends ByteToMessageDecoder<WebSocketFrame>
|
|
|
104
106
|
*/
|
|
105
107
|
private failed: boolean
|
|
106
108
|
|
|
109
|
+
/**
|
|
110
|
+
* Peer sent a close
|
|
111
|
+
*/
|
|
112
|
+
private closed: boolean
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Reading a text message
|
|
116
|
+
*/
|
|
117
|
+
private textual: boolean
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* UTF-8 checker for fragmented text
|
|
121
|
+
*/
|
|
122
|
+
private stream: TextDecoder | null
|
|
123
|
+
|
|
107
124
|
/**
|
|
108
125
|
* Header of the frame being read, reused so no frame allocates one
|
|
109
126
|
*/
|
|
@@ -127,6 +144,9 @@ export class WebSocketFrameDecoder extends ByteToMessageDecoder<WebSocketFrame>
|
|
|
127
144
|
this.maxFramePayloadLength = limit
|
|
128
145
|
this.fragmented = false
|
|
129
146
|
this.failed = false
|
|
147
|
+
this.closed = false
|
|
148
|
+
this.textual = false
|
|
149
|
+
this.stream = null
|
|
130
150
|
this.head = {
|
|
131
151
|
finalFragment: false,
|
|
132
152
|
reserved: 0,
|
|
@@ -147,7 +167,7 @@ export class WebSocketFrameDecoder extends ByteToMessageDecoder<WebSocketFrame>
|
|
|
147
167
|
input: ByteBuf,
|
|
148
168
|
out: WebSocketFrame[],
|
|
149
169
|
): void {
|
|
150
|
-
if (this.failed) {
|
|
170
|
+
if (this.failed || this.closed) {
|
|
151
171
|
input.skipBytes(input.readableBytes)
|
|
152
172
|
return
|
|
153
173
|
}
|
|
@@ -326,25 +346,80 @@ export class WebSocketFrameDecoder extends ByteToMessageDecoder<WebSocketFrame>
|
|
|
326
346
|
if (!WebSocketFrameDecoder.isControl(head.opcode)) this.fragmented = !final
|
|
327
347
|
switch (head.opcode) {
|
|
328
348
|
case WebSocketOpcode.TEXT:
|
|
349
|
+
this.checkText(payload, final)
|
|
350
|
+
this.textual = !final
|
|
329
351
|
return new TextWebSocketFrame(content, final)
|
|
330
352
|
case WebSocketOpcode.BINARY:
|
|
331
353
|
return new BinaryWebSocketFrame(content, final)
|
|
332
354
|
case WebSocketOpcode.CONTINUATION:
|
|
355
|
+
if (this.textual) {
|
|
356
|
+
this.checkText(payload, final)
|
|
357
|
+
this.textual = !final
|
|
358
|
+
}
|
|
359
|
+
|
|
333
360
|
return new ContinuationWebSocketFrame(content, final)
|
|
334
361
|
case WebSocketOpcode.PING:
|
|
335
362
|
return new PingWebSocketFrame(content)
|
|
336
363
|
case WebSocketOpcode.PONG:
|
|
337
364
|
return new PongWebSocketFrame(content)
|
|
338
365
|
default:
|
|
339
|
-
return
|
|
366
|
+
return this.createClose(payload)
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
/**
|
|
371
|
+
* Rejects bad UTF-8
|
|
372
|
+
* @param payload text bytes, already unmasked
|
|
373
|
+
* @param final whether the message ends here
|
|
374
|
+
*/
|
|
375
|
+
private checkText(payload: Uint8Array, final: boolean): void {
|
|
376
|
+
if (final && !this.textual) {
|
|
377
|
+
if (isUtf8(payload)) return
|
|
378
|
+
|
|
379
|
+
throw new CorruptedFrameError(
|
|
380
|
+
'Text is invalid',
|
|
381
|
+
WebSocketCloseStatus.INVALID_PAYLOAD.code,
|
|
382
|
+
)
|
|
340
383
|
}
|
|
384
|
+
|
|
385
|
+
this.stream ??= new TextDecoder('utf-8', {
|
|
386
|
+
/**
|
|
387
|
+
* Throw on bad bytes
|
|
388
|
+
*/
|
|
389
|
+
fatal: true,
|
|
390
|
+
})
|
|
391
|
+
try {
|
|
392
|
+
this.stream.decode(payload, {
|
|
393
|
+
/**
|
|
394
|
+
* Hold a split character
|
|
395
|
+
*/
|
|
396
|
+
stream: !final,
|
|
397
|
+
})
|
|
398
|
+
} catch (cause) {
|
|
399
|
+
throw new CorruptedFrameError(
|
|
400
|
+
'Text is invalid',
|
|
401
|
+
WebSocketCloseStatus.INVALID_PAYLOAD.code,
|
|
402
|
+
{ cause },
|
|
403
|
+
)
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
/**
|
|
408
|
+
* Reads the peer's close
|
|
409
|
+
* @param payload raw close payload
|
|
410
|
+
*/
|
|
411
|
+
private createClose(payload: Uint8Array): CloseWebSocketFrame {
|
|
412
|
+
const frame = WebSocketFrameDecoder.parseClose(payload)
|
|
413
|
+
|
|
414
|
+
this.closed = true
|
|
415
|
+
return frame
|
|
341
416
|
}
|
|
342
417
|
|
|
343
418
|
/**
|
|
344
419
|
* Parses the close code and UTF-8 reason, an empty payload meaning no code
|
|
345
420
|
* @param payload raw close payload
|
|
346
421
|
*/
|
|
347
|
-
private static
|
|
422
|
+
private static parseClose(payload: Uint8Array): CloseWebSocketFrame {
|
|
348
423
|
if (payload.length === 0) return new CloseWebSocketFrame()
|
|
349
424
|
|
|
350
425
|
const status = ((payload[0] ?? 0) << 8) | (payload[1] ?? 0)
|