@netifyjs/netify 0.0.0-stage → 1.0.0-alpha.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/LICENSE +21 -0
- package/README.md +105 -2
- package/package.json +65 -4
- package/src/bootstrap/client/index.ts +191 -0
- package/src/bootstrap/dispatcher/index.ts +204 -0
- package/src/bootstrap/index.ts +22 -0
- package/src/bootstrap/reconnect/index.ts +334 -0
- package/src/bootstrap/server/index.ts +284 -0
- package/src/buffer/bits/index.ts +2 -0
- package/src/buffer/bits/reader/index.ts +64 -0
- package/src/buffer/bits/writer/index.ts +79 -0
- package/src/buffer/index.ts +1375 -0
- package/src/buffer/pool/index.ts +46 -0
- package/src/buffer/quantizer/index.ts +101 -0
- package/src/channel/attribute/index.ts +89 -0
- package/src/channel/context/index.ts +621 -0
- package/src/channel/group/index.ts +256 -0
- package/src/channel/handler/duplex/index.ts +105 -0
- package/src/channel/handler/inbound/index.ts +78 -0
- package/src/channel/handler/index.ts +135 -0
- package/src/channel/handler/outbound/index.ts +33 -0
- package/src/channel/index.ts +619 -0
- package/src/channel/initializer/index.ts +77 -0
- package/src/channel/outbound/index.ts +611 -0
- package/src/channel/pipeline/head/index.ts +100 -0
- package/src/channel/pipeline/index.ts +414 -0
- package/src/channel/pipeline/tail/index.ts +45 -0
- package/src/channel/pipeline/tracker/index.ts +120 -0
- package/src/channel/queue/index.ts +203 -0
- package/src/channel/resolved/index.ts +34 -0
- package/src/channel/server/index.ts +164 -0
- package/src/channel/shutdown/index.ts +25 -0
- package/src/channel/simple/index.ts +38 -0
- package/src/channel/transport/index.ts +74 -0
- package/src/channel/transport/socket/index.ts +80 -0
- package/src/cluster/index.ts +360 -0
- package/src/codec/binary/index.ts +102 -0
- package/src/codec/decoder/index.ts +479 -0
- package/src/codec/delimiter/index.ts +189 -0
- package/src/codec/encoder/index.ts +48 -0
- package/src/codec/error/index.ts +33 -0
- package/src/codec/fixed/index.ts +45 -0
- package/src/codec/http/aggregator/index.ts +236 -0
- package/src/codec/http/client/index.ts +120 -0
- package/src/codec/http/decoder/index.ts +4 -0
- package/src/codec/http/decoder/object/index.ts +644 -0
- package/src/codec/http/decoder/request/index.ts +49 -0
- package/src/codec/http/decoder/response/index.ts +59 -0
- package/src/codec/http/encoder/index.ts +3 -0
- package/src/codec/http/encoder/object/index.ts +196 -0
- package/src/codec/http/encoder/request/index.ts +24 -0
- package/src/codec/http/encoder/response/index.ts +26 -0
- package/src/codec/http/headers/index.ts +287 -0
- package/src/codec/http/index.ts +33 -0
- package/src/codec/http/message/content/index.ts +36 -0
- package/src/codec/http/message/full/index.ts +10 -0
- package/src/codec/http/message/full/request/index.ts +45 -0
- package/src/codec/http/message/full/response/index.ts +43 -0
- package/src/codec/http/message/head/index.ts +79 -0
- package/src/codec/http/message/index.ts +16 -0
- package/src/codec/http/message/last/index.ts +27 -0
- package/src/codec/http/message/request/index.ts +39 -0
- package/src/codec/http/message/response/index.ts +31 -0
- package/src/codec/http/method/index.ts +101 -0
- package/src/codec/http/server/index.ts +81 -0
- package/src/codec/http/status/index.ts +170 -0
- package/src/codec/http/version/index.ts +70 -0
- package/src/codec/index.ts +83 -0
- package/src/codec/length/index.ts +330 -0
- package/src/codec/line/index.ts +45 -0
- package/src/codec/message/decoder/index.ts +84 -0
- package/src/codec/message/encoder/index.ts +56 -0
- package/src/codec/message/index.ts +2 -0
- package/src/codec/msgpack/index.ts +51 -0
- package/src/codec/prepender/index.ts +296 -0
- package/src/codec/protobuf/index.ts +57 -0
- package/src/codec/string/decoder/index.ts +47 -0
- package/src/codec/string/encoder/index.ts +26 -0
- package/src/codec/string/index.ts +2 -0
- package/src/codec/websocket/client/index.ts +235 -0
- package/src/codec/websocket/decoder/index.ts +371 -0
- package/src/codec/websocket/encoder/index.ts +170 -0
- package/src/codec/websocket/error/index.ts +42 -0
- package/src/codec/websocket/frame/base/index.ts +55 -0
- package/src/codec/websocket/frame/binary/index.ts +24 -0
- package/src/codec/websocket/frame/close/index.ts +82 -0
- package/src/codec/websocket/frame/continuation/index.ts +31 -0
- package/src/codec/websocket/frame/index.ts +10 -0
- package/src/codec/websocket/frame/opcode/index.ts +35 -0
- package/src/codec/websocket/frame/ping/index.ts +24 -0
- package/src/codec/websocket/frame/pong/index.ts +24 -0
- package/src/codec/websocket/frame/text/index.ts +31 -0
- package/src/codec/websocket/handshake/index.ts +38 -0
- package/src/codec/websocket/index.ts +25 -0
- package/src/codec/websocket/key/index.ts +41 -0
- package/src/codec/websocket/protocol/index.ts +147 -0
- package/src/codec/websocket/server/index.ts +233 -0
- package/src/codec/websocket/status/index.ts +97 -0
- package/src/codec/websocket/upgrade/index.ts +116 -0
- package/src/datagram/bootstrap/index.ts +269 -0
- package/src/datagram/channel/index.ts +103 -0
- package/src/datagram/index.ts +10 -0
- package/src/datagram/packet/index.ts +72 -0
- package/src/datagram/transport/index.ts +239 -0
- package/src/handler/flush/index.ts +187 -0
- package/src/handler/idle/index.ts +295 -0
- package/src/handler/index.ts +22 -0
- package/src/handler/logging/index.ts +195 -0
- package/src/handler/metrics/index.ts +300 -0
- package/src/handler/offload/index.ts +141 -0
- package/src/handler/timeout/error/index.ts +29 -0
- package/src/handler/timeout/index.ts +3 -0
- package/src/handler/timeout/read/index.ts +44 -0
- package/src/handler/timeout/write/index.ts +89 -0
- package/src/handler/traffic/index.ts +259 -0
- package/src/index.ts +258 -0
- package/src/logger/index.ts +47 -0
- package/src/reliable/bootstrap/index.ts +154 -0
- package/src/reliable/cipher/index.ts +227 -0
- package/src/reliable/cookie/index.ts +78 -0
- package/src/reliable/handler/index.ts +1202 -0
- package/src/reliable/handshake/index.ts +144 -0
- package/src/reliable/index.ts +38 -0
- package/src/reliable/lane/index.ts +420 -0
- package/src/reliable/message/index.ts +102 -0
- package/src/reliable/packet/index.ts +117 -0
- package/src/reliable/peer/index.ts +929 -0
- package/src/reliable/registry/index.ts +107 -0
- package/src/serve/bootstrap/index.ts +606 -0
- package/src/serve/channel/index.ts +246 -0
- package/src/serve/index.ts +17 -0
- package/src/serve/outbox/index.ts +292 -0
- package/src/serve/transport/index.ts +96 -0
- package/src/serve/writer/index.ts +119 -0
- package/src/shard/envelope/index.ts +44 -0
- package/src/shard/index.ts +10 -0
- package/src/shard/link/index.ts +743 -0
- package/src/shard/map/index.ts +91 -0
- package/src/worker/group/index.ts +175 -0
- package/src/worker/index.ts +10 -0
- package/src/worker/pool/index.ts +235 -0
- package/src/worker/service/index.ts +55 -0
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* An HTTP version like HTTP/1.1
|
|
3
|
+
* @class
|
|
4
|
+
*/
|
|
5
|
+
export class HttpVersion {
|
|
6
|
+
/**
|
|
7
|
+
* HTTP/1.0, closes after each response by default
|
|
8
|
+
*/
|
|
9
|
+
public static readonly HTTP_1_0: HttpVersion = new HttpVersion(1, 0)
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* HTTP/1.1, keeps connections open by default
|
|
13
|
+
*/
|
|
14
|
+
public static readonly HTTP_1_1: HttpVersion = new HttpVersion(1, 1)
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* The 1 in HTTP/1.1
|
|
18
|
+
*/
|
|
19
|
+
public readonly major: number
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* The second 1 in HTTP/1.1
|
|
23
|
+
*/
|
|
24
|
+
public readonly minor: number
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Formatted as it goes on the wire
|
|
28
|
+
*/
|
|
29
|
+
public readonly text: string
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Builds a version from its two numbers
|
|
33
|
+
* @param major number before the dot
|
|
34
|
+
* @param minor number after the dot
|
|
35
|
+
*/
|
|
36
|
+
public constructor(major: number, minor: number) {
|
|
37
|
+
this.major = major
|
|
38
|
+
this.minor = minor
|
|
39
|
+
this.text = `HTTP/${String(major)}.${String(minor)}`
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Parses something like HTTP/1.1, reusing the shared instances where it can
|
|
44
|
+
* @param text version as read off the wire
|
|
45
|
+
*/
|
|
46
|
+
public static parse(text: string): HttpVersion | null {
|
|
47
|
+
const match = /^HTTP\/(\d)\.(\d)$/.exec(text)
|
|
48
|
+
if (match === null) return null
|
|
49
|
+
|
|
50
|
+
const major = Number(match[1])
|
|
51
|
+
const minor = Number(match[2])
|
|
52
|
+
if (major === 1 && minor === 1) return HttpVersion.HTTP_1_1
|
|
53
|
+
if (major === 1 && minor === 0) return HttpVersion.HTTP_1_0
|
|
54
|
+
return new HttpVersion(major, minor)
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* True from HTTP/1.1 onwards
|
|
59
|
+
*/
|
|
60
|
+
public get keepAliveDefault(): boolean {
|
|
61
|
+
return this.major > 1 || (this.major === 1 && this.minor >= 1)
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Wire format, such as HTTP/1.0
|
|
66
|
+
*/
|
|
67
|
+
public toString(): string {
|
|
68
|
+
return this.text
|
|
69
|
+
}
|
|
70
|
+
}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
export { ByteToMessageDecoder } from './decoder/index.ts'
|
|
2
|
+
export type { DecoderOptions } from './decoder/index.ts'
|
|
3
|
+
export { DelimiterBasedFrameDecoder } from './delimiter/index.ts'
|
|
4
|
+
export type { DelimiterOptions } from './delimiter/index.ts'
|
|
5
|
+
export { MessageToByteEncoder } from './encoder/index.ts'
|
|
6
|
+
export { DecoderError, EncoderError } from './error/index.ts'
|
|
7
|
+
export { FixedLengthFrameDecoder } from './fixed/index.ts'
|
|
8
|
+
export { LengthFieldBasedFrameDecoder } from './length/index.ts'
|
|
9
|
+
export type { LengthFieldLength, LengthFieldOptions } from './length/index.ts'
|
|
10
|
+
export { LineBasedFrameDecoder } from './line/index.ts'
|
|
11
|
+
export type { LineOptions } from './line/index.ts'
|
|
12
|
+
export {
|
|
13
|
+
MessageToMessageDecoder,
|
|
14
|
+
MessageToMessageEncoder,
|
|
15
|
+
} from './message/index.ts'
|
|
16
|
+
export { LengthFieldPrepender } from './prepender/index.ts'
|
|
17
|
+
export type { PrependerOptions } from './prepender/index.ts'
|
|
18
|
+
export { StringDecoder, StringEncoder } from './string/index.ts'
|
|
19
|
+
export {
|
|
20
|
+
FullHttpRequest,
|
|
21
|
+
FullHttpResponse,
|
|
22
|
+
HttpClientCodec,
|
|
23
|
+
HttpContent,
|
|
24
|
+
HttpHeaders,
|
|
25
|
+
HttpMessage,
|
|
26
|
+
HttpMethod,
|
|
27
|
+
HttpObjectAggregator,
|
|
28
|
+
HttpObjectDecoder,
|
|
29
|
+
HttpObjectEncoder,
|
|
30
|
+
HttpRequest,
|
|
31
|
+
HttpRequestDecoder,
|
|
32
|
+
HttpRequestEncoder,
|
|
33
|
+
HttpResponse,
|
|
34
|
+
HttpResponseDecoder,
|
|
35
|
+
HttpResponseEncoder,
|
|
36
|
+
HttpResponseStatus,
|
|
37
|
+
HttpServerCodec,
|
|
38
|
+
HttpVersion,
|
|
39
|
+
LastHttpContent,
|
|
40
|
+
} from './http/index.ts'
|
|
41
|
+
export type {
|
|
42
|
+
ContentSource,
|
|
43
|
+
FullHttpMessage,
|
|
44
|
+
HeaderEntry,
|
|
45
|
+
HeaderValue,
|
|
46
|
+
HttpDecoderOptions,
|
|
47
|
+
HttpObject,
|
|
48
|
+
} from './http/index.ts'
|
|
49
|
+
export {
|
|
50
|
+
BinaryWebSocketFrame,
|
|
51
|
+
CloseWebSocketFrame,
|
|
52
|
+
ContinuationWebSocketFrame,
|
|
53
|
+
CorruptedFrameError,
|
|
54
|
+
HandshakeError,
|
|
55
|
+
PingWebSocketFrame,
|
|
56
|
+
PongWebSocketFrame,
|
|
57
|
+
TextWebSocketFrame,
|
|
58
|
+
WebSocketClientProtocolHandler,
|
|
59
|
+
WebSocketCloseStatus,
|
|
60
|
+
WebSocketFrame,
|
|
61
|
+
WebSocketFrameDecoder,
|
|
62
|
+
WebSocketFrameEncoder,
|
|
63
|
+
WebSocketHandshakeComplete,
|
|
64
|
+
WebSocketKey,
|
|
65
|
+
WebSocketOpcode,
|
|
66
|
+
WebSocketProtocolHandler,
|
|
67
|
+
WebSocketServerProtocolHandler,
|
|
68
|
+
WebSocketUpgrade,
|
|
69
|
+
} from './websocket/index.ts'
|
|
70
|
+
export type {
|
|
71
|
+
FrameSource,
|
|
72
|
+
WebSocketClientOptions,
|
|
73
|
+
WebSocketDecoderOptions,
|
|
74
|
+
WebSocketEncoderOptions,
|
|
75
|
+
WebSocketOpcodeValue,
|
|
76
|
+
WebSocketServerOptions,
|
|
77
|
+
} from './websocket/index.ts'
|
|
78
|
+
export { BinaryCodec } from './binary/index.ts'
|
|
79
|
+
export type { BinaryCodecOptions } from './binary/index.ts'
|
|
80
|
+
export { MessagePackCodec } from './msgpack/index.ts'
|
|
81
|
+
export type { MessagePacker } from './msgpack/index.ts'
|
|
82
|
+
export { ProtobufCodec } from './protobuf/index.ts'
|
|
83
|
+
export type { ProtobufType } from './protobuf/index.ts'
|
|
@@ -0,0 +1,330 @@
|
|
|
1
|
+
import type { HandlerContext } from '../../channel/context/index.ts'
|
|
2
|
+
import { ByteToMessageDecoder, type DecoderOptions } from '../decoder/index.ts'
|
|
3
|
+
import { DecoderError } from '../error/index.ts'
|
|
4
|
+
import type { ByteBuf, Endian } from '../../buffer/index.ts'
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Byte widths a length field can have
|
|
8
|
+
*/
|
|
9
|
+
export type LengthFieldLength = 1 | 2 | 3 | 4 | 8
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Settings for LengthFieldBasedFrameDecoder
|
|
13
|
+
*/
|
|
14
|
+
export type LengthFieldOptions = DecoderOptions & {
|
|
15
|
+
/**
|
|
16
|
+
* Frames bigger than this get dropped and reported
|
|
17
|
+
*/
|
|
18
|
+
readonly maxFrameLength: number
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Where the length field starts, counted from the frame start
|
|
22
|
+
*/
|
|
23
|
+
readonly lengthFieldOffset?: number
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* How many bytes the length takes up
|
|
27
|
+
*/
|
|
28
|
+
readonly lengthFieldLength: LengthFieldLength
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Added to the length field, for protocols that count the header too
|
|
32
|
+
*/
|
|
33
|
+
readonly lengthAdjustment?: number
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Bytes cut off the front of each frame, often the header
|
|
37
|
+
*/
|
|
38
|
+
readonly initialBytesToStrip?: number
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Big endian unless told otherwise
|
|
42
|
+
*/
|
|
43
|
+
readonly endian?: Endian
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Frames bytes by reading a length prefix out of each header
|
|
48
|
+
* @class
|
|
49
|
+
*/
|
|
50
|
+
export class LengthFieldBasedFrameDecoder extends ByteToMessageDecoder<ByteBuf> {
|
|
51
|
+
/**
|
|
52
|
+
* Anything over this gets skipped and reported
|
|
53
|
+
*/
|
|
54
|
+
private readonly maxFrameLength: number
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Bytes before the length field
|
|
58
|
+
*/
|
|
59
|
+
private readonly lengthFieldOffset: number
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Width of the length field in bytes
|
|
63
|
+
*/
|
|
64
|
+
private readonly lengthFieldLength: LengthFieldLength
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Added to whatever the length field says
|
|
68
|
+
*/
|
|
69
|
+
private readonly lengthAdjustment: number
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Stripped off the front before a frame is passed on
|
|
73
|
+
*/
|
|
74
|
+
private readonly initialBytesToStrip: number
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Endianness used when reading the length
|
|
78
|
+
*/
|
|
79
|
+
private readonly endian: Endian
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Rest of an oversized frame we're still skipping over
|
|
83
|
+
*/
|
|
84
|
+
private discardRemaining: number
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Offset plus field width
|
|
88
|
+
*/
|
|
89
|
+
private readonly headerLength: number
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Field width when it can be read inline, otherwise 0
|
|
93
|
+
*/
|
|
94
|
+
private readonly fast: 0 | 1 | 2 | 4
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Sets up a decoder for length prefixed frames
|
|
98
|
+
* @param options field layout and limits
|
|
99
|
+
*/
|
|
100
|
+
public constructor(options: LengthFieldOptions) {
|
|
101
|
+
super(options)
|
|
102
|
+
|
|
103
|
+
const reason = LengthFieldBasedFrameDecoder.refusal(options)
|
|
104
|
+
if (reason !== null) throw new RangeError(reason)
|
|
105
|
+
|
|
106
|
+
this.maxFrameLength = options.maxFrameLength
|
|
107
|
+
this.lengthFieldOffset = options.lengthFieldOffset ?? 0
|
|
108
|
+
this.lengthFieldLength = options.lengthFieldLength
|
|
109
|
+
this.lengthAdjustment = options.lengthAdjustment ?? 0
|
|
110
|
+
this.initialBytesToStrip = options.initialBytesToStrip ?? 0
|
|
111
|
+
this.endian = options.endian ?? 'big'
|
|
112
|
+
this.discardRemaining = 0
|
|
113
|
+
this.headerLength = this.lengthFieldOffset + this.lengthFieldLength
|
|
114
|
+
this.fast =
|
|
115
|
+
this.lengthFieldLength === 1 ||
|
|
116
|
+
(this.endian === 'big' &&
|
|
117
|
+
(this.lengthFieldLength === 2 || this.lengthFieldLength === 4))
|
|
118
|
+
? this.lengthFieldLength
|
|
119
|
+
: 0
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Catches bad field sizes, offsets and limits up front
|
|
124
|
+
* @param options what the caller passed in
|
|
125
|
+
*/
|
|
126
|
+
private static refusal(options: LengthFieldOptions): string | null {
|
|
127
|
+
const offset = options.lengthFieldOffset ?? 0
|
|
128
|
+
const strip = options.initialBytesToStrip ?? 0
|
|
129
|
+
|
|
130
|
+
if (![1, 2, 3, 4, 8].includes(options.lengthFieldLength)) {
|
|
131
|
+
return 'Length field size is invalid'
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
if (!Number.isInteger(options.maxFrameLength)) {
|
|
135
|
+
return 'Frame length is invalid'
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
if (options.maxFrameLength <= 0) return 'Frame length is invalid'
|
|
139
|
+
if (!Number.isInteger(offset) || offset < 0) return 'Offset is invalid'
|
|
140
|
+
if (!Number.isInteger(strip) || strip < 0) return 'Strip is invalid'
|
|
141
|
+
if (offset + options.lengthFieldLength > options.maxFrameLength) {
|
|
142
|
+
return 'Length field is past the frame limit'
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
return null
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Slices out every whole frame, falling back to one at a time for errors and odd layouts
|
|
150
|
+
* @param context unused here
|
|
151
|
+
* @param input bytes gathered so far
|
|
152
|
+
* @param out where finished frames go
|
|
153
|
+
*/
|
|
154
|
+
protected decode(
|
|
155
|
+
context: HandlerContext,
|
|
156
|
+
input: ByteBuf,
|
|
157
|
+
out: ByteBuf[],
|
|
158
|
+
): void {
|
|
159
|
+
if (this.discardRemaining > 0 || this.fast === 0) {
|
|
160
|
+
this.decodeOne(input, out)
|
|
161
|
+
return
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
const start = input.readerIndex
|
|
165
|
+
const end = this.scan(input, start, start + input.readableBytes, out)
|
|
166
|
+
if (end > start) input.skipBytes(end - start)
|
|
167
|
+
else this.decodeOne(input, out)
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Slices out every whole, valid frame in one pass and returns where it stopped
|
|
172
|
+
* @param input the buffer
|
|
173
|
+
* @param start reader position
|
|
174
|
+
* @param limit writer position
|
|
175
|
+
* @param out where frames go
|
|
176
|
+
*/
|
|
177
|
+
private scan(
|
|
178
|
+
input: ByteBuf,
|
|
179
|
+
start: number,
|
|
180
|
+
limit: number,
|
|
181
|
+
out: ByteBuf[],
|
|
182
|
+
): number {
|
|
183
|
+
const bytes = input.array
|
|
184
|
+
const base = input.arrayOffset + this.lengthFieldOffset
|
|
185
|
+
const header = this.headerLength
|
|
186
|
+
const strip = this.initialBytesToStrip
|
|
187
|
+
|
|
188
|
+
let at = start
|
|
189
|
+
while (limit - at >= header) {
|
|
190
|
+
const frameLength =
|
|
191
|
+
this.lengthAt(bytes, base + at) + this.lengthAdjustment + header
|
|
192
|
+
const valid =
|
|
193
|
+
frameLength >= header &&
|
|
194
|
+
frameLength <= this.maxFrameLength &&
|
|
195
|
+
strip <= frameLength
|
|
196
|
+
if (!valid || limit - at < frameLength) break
|
|
197
|
+
out.push(this.frameOf(input, at + strip, frameLength - strip))
|
|
198
|
+
at += frameLength
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
return at
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* Puts the reader back at the start of a frame that was sliced from input
|
|
206
|
+
* @param input the buffer
|
|
207
|
+
* @param frame the frame
|
|
208
|
+
*/
|
|
209
|
+
protected override rewind(input: ByteBuf, frame: ByteBuf): boolean {
|
|
210
|
+
const start =
|
|
211
|
+
frame.arrayOffset - input.arrayOffset - this.initialBytesToStrip
|
|
212
|
+
if (frame.array !== input.array || start < 0) return false
|
|
213
|
+
|
|
214
|
+
input.readerIndex = start
|
|
215
|
+
return true
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* Reads an inline length field straight from the array
|
|
220
|
+
* @param bytes the backing array
|
|
221
|
+
* @param at where the field starts
|
|
222
|
+
*/
|
|
223
|
+
private lengthAt(bytes: Uint8Array, at: number): number {
|
|
224
|
+
switch (this.fast) {
|
|
225
|
+
case 1:
|
|
226
|
+
return bytes[at] ?? 0
|
|
227
|
+
case 2:
|
|
228
|
+
return (((bytes[at] ?? 0) << 8) | (bytes[at + 1] ?? 0)) >>> 0
|
|
229
|
+
default:
|
|
230
|
+
return (
|
|
231
|
+
(((bytes[at] ?? 0) << 24) |
|
|
232
|
+
((bytes[at + 1] ?? 0) << 16) |
|
|
233
|
+
((bytes[at + 2] ?? 0) << 8) |
|
|
234
|
+
(bytes[at + 3] ?? 0)) >>>
|
|
235
|
+
0
|
|
236
|
+
)
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* Waits for the header and body, then slices out one frame
|
|
242
|
+
* @param input bytes gathered so far
|
|
243
|
+
* @param out where finished frames go
|
|
244
|
+
*/
|
|
245
|
+
private decodeOne(input: ByteBuf, out: ByteBuf[]): void {
|
|
246
|
+
if (this.discardRemaining > 0) {
|
|
247
|
+
this.discard(input)
|
|
248
|
+
return
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
const headerLength = this.headerLength
|
|
252
|
+
const readable = input.readableBytes
|
|
253
|
+
if (readable < headerLength) return
|
|
254
|
+
|
|
255
|
+
const frameLength =
|
|
256
|
+
this.peekLength(input) + this.lengthAdjustment + headerLength
|
|
257
|
+
if (frameLength < headerLength) {
|
|
258
|
+
input.skipBytes(headerLength)
|
|
259
|
+
throw new DecoderError('Frame length is invalid')
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
if (frameLength > this.maxFrameLength) {
|
|
263
|
+
this.discardRemaining = frameLength
|
|
264
|
+
this.discard(input)
|
|
265
|
+
throw new DecoderError('Frame is too large')
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
if (readable < frameLength) return
|
|
269
|
+
if (this.initialBytesToStrip > frameLength) {
|
|
270
|
+
input.skipBytes(frameLength)
|
|
271
|
+
throw new DecoderError('Strip is past the frame length')
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
const strip = this.initialBytesToStrip
|
|
275
|
+
if (strip > 0) input.skipBytes(strip)
|
|
276
|
+
out.push(input.readSlice(frameLength - strip))
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
/**
|
|
280
|
+
* Reads the length field at the reader without moving it
|
|
281
|
+
* @param input the buffer to peek into
|
|
282
|
+
*/
|
|
283
|
+
private peekLength(input: ByteBuf): number {
|
|
284
|
+
if (this.fast === 0) {
|
|
285
|
+
return this.readLength(input, input.readerIndex + this.lengthFieldOffset)
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
return this.lengthAt(
|
|
289
|
+
input.array,
|
|
290
|
+
input.arrayOffset + input.readerIndex + this.lengthFieldOffset,
|
|
291
|
+
)
|
|
292
|
+
}
|
|
293
|
+
|
|
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
|
+
}
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
/**
|
|
321
|
+
* Skips as much of the oversized frame as has arrived
|
|
322
|
+
* @param input bytes to skip through
|
|
323
|
+
*/
|
|
324
|
+
private discard(input: ByteBuf): void {
|
|
325
|
+
const size = Math.min(this.discardRemaining, input.readableBytes)
|
|
326
|
+
|
|
327
|
+
input.skipBytes(size)
|
|
328
|
+
this.discardRemaining -= size
|
|
329
|
+
}
|
|
330
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import type { DecoderOptions } from '../decoder/index.ts'
|
|
2
|
+
import { DelimiterBasedFrameDecoder } from '../delimiter/index.ts'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Settings for LineBasedFrameDecoder
|
|
6
|
+
*/
|
|
7
|
+
export type LineOptions = DecoderOptions & {
|
|
8
|
+
/**
|
|
9
|
+
* Lines longer than this are dropped
|
|
10
|
+
*/
|
|
11
|
+
readonly maxLength: number
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Leaves the CRLF or LF off, on by default
|
|
15
|
+
*/
|
|
16
|
+
readonly stripDelimiter?: boolean
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Splits text into lines on CRLF or a bare LF
|
|
21
|
+
* @class
|
|
22
|
+
*/
|
|
23
|
+
export class LineBasedFrameDecoder extends DelimiterBasedFrameDecoder {
|
|
24
|
+
/**
|
|
25
|
+
* Sets up a line splitter
|
|
26
|
+
* @param options length cap and whether to keep line endings
|
|
27
|
+
*/
|
|
28
|
+
public constructor(options: LineOptions) {
|
|
29
|
+
super({
|
|
30
|
+
...options,
|
|
31
|
+
/**
|
|
32
|
+
* Line length cap
|
|
33
|
+
*/
|
|
34
|
+
maxFrameLength: options.maxLength,
|
|
35
|
+
/**
|
|
36
|
+
* CRLF or LF
|
|
37
|
+
*/
|
|
38
|
+
delimiters: DelimiterBasedFrameDecoder.lineDelimiter,
|
|
39
|
+
/**
|
|
40
|
+
* Strip the ending unless asked not to
|
|
41
|
+
*/
|
|
42
|
+
stripDelimiter: options.stripDelimiter ?? true,
|
|
43
|
+
})
|
|
44
|
+
}
|
|
45
|
+
}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { InboundHandlerAdapter } from '../../../channel/handler/inbound/index.ts'
|
|
2
|
+
import { resolved } from '../../../channel/resolved/index.ts'
|
|
3
|
+
import { DecoderError } from '../../error/index.ts'
|
|
4
|
+
import type { HandlerContext } from '../../../channel/context/index.ts'
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Base for decoders that turn one message into zero or more others
|
|
8
|
+
* @class
|
|
9
|
+
*/
|
|
10
|
+
export abstract class MessageToMessageDecoder<
|
|
11
|
+
I,
|
|
12
|
+
O = unknown,
|
|
13
|
+
> extends InboundHandlerAdapter {
|
|
14
|
+
/**
|
|
15
|
+
* Decodes messages it accepts and lets the rest through
|
|
16
|
+
* @param context where decoded messages go
|
|
17
|
+
* @param message anything coming in
|
|
18
|
+
*/
|
|
19
|
+
public override channelRead(
|
|
20
|
+
context: HandlerContext,
|
|
21
|
+
message: unknown,
|
|
22
|
+
): Promise<void> {
|
|
23
|
+
if (!this.accepts(message)) return context.fireChannelRead(message)
|
|
24
|
+
|
|
25
|
+
const out: O[] = []
|
|
26
|
+
let failure: DecoderError | null = null
|
|
27
|
+
try {
|
|
28
|
+
this.decode(context, message, out)
|
|
29
|
+
} catch (error) {
|
|
30
|
+
failure =
|
|
31
|
+
error instanceof DecoderError
|
|
32
|
+
? error
|
|
33
|
+
: new DecoderError('Decoding failed', { cause: error })
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
for (let index = 0; index < out.length; index++) {
|
|
37
|
+
const result = context.fireChannelRead(out[index])
|
|
38
|
+
if (result !== resolved) {
|
|
39
|
+
return this.deliverLater(result, context, out, index + 1, failure)
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
if (failure !== null) throw failure
|
|
44
|
+
return resolved
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Delivers the rest one at a time once a handler went async
|
|
49
|
+
* @param first the promise that broke the sync run
|
|
50
|
+
* @param context where to fire them
|
|
51
|
+
* @param out everything decode produced
|
|
52
|
+
* @param start first message not yet delivered
|
|
53
|
+
* @param failure rethrown at the end, or null
|
|
54
|
+
*/
|
|
55
|
+
private async deliverLater(
|
|
56
|
+
first: Promise<void>,
|
|
57
|
+
context: HandlerContext,
|
|
58
|
+
out: readonly O[],
|
|
59
|
+
start: number,
|
|
60
|
+
failure: DecoderError | null,
|
|
61
|
+
): Promise<void> {
|
|
62
|
+
await first
|
|
63
|
+
|
|
64
|
+
for (let index = start; index < out.length; index++) {
|
|
65
|
+
await context.fireChannelRead(out[index])
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
if (failure !== null) throw failure
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Says whether a message is one this decoder should handle
|
|
73
|
+
* @param message the inbound message
|
|
74
|
+
*/
|
|
75
|
+
protected abstract accepts(message: unknown): message is I
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Pushing nothing is fine, the message is then swallowed
|
|
79
|
+
* @param context the handler's context
|
|
80
|
+
* @param message one that passed accepts
|
|
81
|
+
* @param out push the results here
|
|
82
|
+
*/
|
|
83
|
+
protected abstract decode(context: HandlerContext, message: I, out: O[]): void
|
|
84
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { OutboundHandlerAdapter } from '../../../channel/handler/outbound/index.ts'
|
|
2
|
+
import { EncoderError } from '../../error/index.ts'
|
|
3
|
+
import type { HandlerContext } from '../../../channel/context/index.ts'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Base for encoders that turn one message into one or more others
|
|
7
|
+
* @class
|
|
8
|
+
*/
|
|
9
|
+
export abstract class MessageToMessageEncoder<
|
|
10
|
+
I,
|
|
11
|
+
O = unknown,
|
|
12
|
+
> extends OutboundHandlerAdapter {
|
|
13
|
+
/**
|
|
14
|
+
* Encodes messages it accepts and lets the rest through
|
|
15
|
+
* @param context where the results get written
|
|
16
|
+
* @param message anything heading out
|
|
17
|
+
*/
|
|
18
|
+
public override async write(
|
|
19
|
+
context: HandlerContext,
|
|
20
|
+
message: unknown,
|
|
21
|
+
): Promise<void> {
|
|
22
|
+
if (!this.accepts(message)) {
|
|
23
|
+
await context.write(message)
|
|
24
|
+
return
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const out: O[] = []
|
|
28
|
+
try {
|
|
29
|
+
this.encode(context, message, out)
|
|
30
|
+
} catch (error) {
|
|
31
|
+
throw error instanceof EncoderError
|
|
32
|
+
? error
|
|
33
|
+
: new EncoderError('Encoding failed', { cause: error })
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
if (out.length === 0) throw new EncoderError('Encoder wrote nothing')
|
|
37
|
+
|
|
38
|
+
await Promise.all(
|
|
39
|
+
out.map((encoded: O): Promise<void> => context.write(encoded)),
|
|
40
|
+
)
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Says whether a message is one this encoder should handle
|
|
45
|
+
* @param message the outbound message
|
|
46
|
+
*/
|
|
47
|
+
protected abstract accepts(message: unknown): message is I
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Must push at least one message or the write fails
|
|
51
|
+
* @param context the handler's context
|
|
52
|
+
* @param message one that passed accepts
|
|
53
|
+
* @param out push the results here
|
|
54
|
+
*/
|
|
55
|
+
protected abstract encode(context: HandlerContext, message: I, out: O[]): void
|
|
56
|
+
}
|