@netifyjs/netify 0.0.0-stage → 1.0.0-alpha.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (143) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +105 -2
  3. package/package.json +65 -4
  4. package/src/bootstrap/client/index.ts +191 -0
  5. package/src/bootstrap/dispatcher/index.ts +204 -0
  6. package/src/bootstrap/index.ts +22 -0
  7. package/src/bootstrap/reconnect/index.ts +334 -0
  8. package/src/bootstrap/server/index.ts +284 -0
  9. package/src/buffer/bits/index.ts +2 -0
  10. package/src/buffer/bits/reader/index.ts +64 -0
  11. package/src/buffer/bits/writer/index.ts +79 -0
  12. package/src/buffer/index.ts +1375 -0
  13. package/src/buffer/pool/index.ts +46 -0
  14. package/src/buffer/quantizer/index.ts +101 -0
  15. package/src/channel/attribute/index.ts +89 -0
  16. package/src/channel/context/index.ts +621 -0
  17. package/src/channel/group/index.ts +256 -0
  18. package/src/channel/handler/duplex/index.ts +105 -0
  19. package/src/channel/handler/inbound/index.ts +78 -0
  20. package/src/channel/handler/index.ts +135 -0
  21. package/src/channel/handler/outbound/index.ts +33 -0
  22. package/src/channel/index.ts +619 -0
  23. package/src/channel/initializer/index.ts +77 -0
  24. package/src/channel/outbound/index.ts +611 -0
  25. package/src/channel/pipeline/head/index.ts +100 -0
  26. package/src/channel/pipeline/index.ts +414 -0
  27. package/src/channel/pipeline/tail/index.ts +45 -0
  28. package/src/channel/pipeline/tracker/index.ts +120 -0
  29. package/src/channel/queue/index.ts +203 -0
  30. package/src/channel/resolved/index.ts +34 -0
  31. package/src/channel/server/index.ts +164 -0
  32. package/src/channel/shutdown/index.ts +25 -0
  33. package/src/channel/simple/index.ts +38 -0
  34. package/src/channel/transport/index.ts +74 -0
  35. package/src/channel/transport/socket/index.ts +80 -0
  36. package/src/cluster/index.ts +360 -0
  37. package/src/codec/binary/index.ts +102 -0
  38. package/src/codec/decoder/index.ts +479 -0
  39. package/src/codec/delimiter/index.ts +189 -0
  40. package/src/codec/encoder/index.ts +48 -0
  41. package/src/codec/error/index.ts +33 -0
  42. package/src/codec/fixed/index.ts +45 -0
  43. package/src/codec/http/aggregator/index.ts +236 -0
  44. package/src/codec/http/client/index.ts +120 -0
  45. package/src/codec/http/decoder/index.ts +4 -0
  46. package/src/codec/http/decoder/object/index.ts +644 -0
  47. package/src/codec/http/decoder/request/index.ts +49 -0
  48. package/src/codec/http/decoder/response/index.ts +59 -0
  49. package/src/codec/http/encoder/index.ts +3 -0
  50. package/src/codec/http/encoder/object/index.ts +196 -0
  51. package/src/codec/http/encoder/request/index.ts +24 -0
  52. package/src/codec/http/encoder/response/index.ts +26 -0
  53. package/src/codec/http/headers/index.ts +287 -0
  54. package/src/codec/http/index.ts +33 -0
  55. package/src/codec/http/message/content/index.ts +36 -0
  56. package/src/codec/http/message/full/index.ts +10 -0
  57. package/src/codec/http/message/full/request/index.ts +45 -0
  58. package/src/codec/http/message/full/response/index.ts +43 -0
  59. package/src/codec/http/message/head/index.ts +79 -0
  60. package/src/codec/http/message/index.ts +16 -0
  61. package/src/codec/http/message/last/index.ts +27 -0
  62. package/src/codec/http/message/request/index.ts +39 -0
  63. package/src/codec/http/message/response/index.ts +31 -0
  64. package/src/codec/http/method/index.ts +101 -0
  65. package/src/codec/http/server/index.ts +81 -0
  66. package/src/codec/http/status/index.ts +170 -0
  67. package/src/codec/http/version/index.ts +70 -0
  68. package/src/codec/index.ts +83 -0
  69. package/src/codec/length/index.ts +330 -0
  70. package/src/codec/line/index.ts +45 -0
  71. package/src/codec/message/decoder/index.ts +84 -0
  72. package/src/codec/message/encoder/index.ts +56 -0
  73. package/src/codec/message/index.ts +2 -0
  74. package/src/codec/msgpack/index.ts +51 -0
  75. package/src/codec/prepender/index.ts +296 -0
  76. package/src/codec/protobuf/index.ts +57 -0
  77. package/src/codec/string/decoder/index.ts +47 -0
  78. package/src/codec/string/encoder/index.ts +26 -0
  79. package/src/codec/string/index.ts +2 -0
  80. package/src/codec/websocket/client/index.ts +235 -0
  81. package/src/codec/websocket/decoder/index.ts +371 -0
  82. package/src/codec/websocket/encoder/index.ts +170 -0
  83. package/src/codec/websocket/error/index.ts +42 -0
  84. package/src/codec/websocket/frame/base/index.ts +55 -0
  85. package/src/codec/websocket/frame/binary/index.ts +24 -0
  86. package/src/codec/websocket/frame/close/index.ts +82 -0
  87. package/src/codec/websocket/frame/continuation/index.ts +31 -0
  88. package/src/codec/websocket/frame/index.ts +10 -0
  89. package/src/codec/websocket/frame/opcode/index.ts +35 -0
  90. package/src/codec/websocket/frame/ping/index.ts +24 -0
  91. package/src/codec/websocket/frame/pong/index.ts +24 -0
  92. package/src/codec/websocket/frame/text/index.ts +31 -0
  93. package/src/codec/websocket/handshake/index.ts +38 -0
  94. package/src/codec/websocket/index.ts +25 -0
  95. package/src/codec/websocket/key/index.ts +41 -0
  96. package/src/codec/websocket/protocol/index.ts +147 -0
  97. package/src/codec/websocket/server/index.ts +233 -0
  98. package/src/codec/websocket/status/index.ts +97 -0
  99. package/src/codec/websocket/upgrade/index.ts +116 -0
  100. package/src/datagram/bootstrap/index.ts +269 -0
  101. package/src/datagram/channel/index.ts +103 -0
  102. package/src/datagram/index.ts +10 -0
  103. package/src/datagram/packet/index.ts +72 -0
  104. package/src/datagram/transport/index.ts +239 -0
  105. package/src/handler/flush/index.ts +187 -0
  106. package/src/handler/idle/index.ts +295 -0
  107. package/src/handler/index.ts +22 -0
  108. package/src/handler/logging/index.ts +195 -0
  109. package/src/handler/metrics/index.ts +300 -0
  110. package/src/handler/offload/index.ts +141 -0
  111. package/src/handler/timeout/error/index.ts +29 -0
  112. package/src/handler/timeout/index.ts +3 -0
  113. package/src/handler/timeout/read/index.ts +44 -0
  114. package/src/handler/timeout/write/index.ts +89 -0
  115. package/src/handler/traffic/index.ts +259 -0
  116. package/src/index.ts +258 -0
  117. package/src/logger/index.ts +47 -0
  118. package/src/reliable/bootstrap/index.ts +154 -0
  119. package/src/reliable/cipher/index.ts +227 -0
  120. package/src/reliable/cookie/index.ts +78 -0
  121. package/src/reliable/handler/index.ts +1202 -0
  122. package/src/reliable/handshake/index.ts +144 -0
  123. package/src/reliable/index.ts +38 -0
  124. package/src/reliable/lane/index.ts +420 -0
  125. package/src/reliable/message/index.ts +102 -0
  126. package/src/reliable/packet/index.ts +117 -0
  127. package/src/reliable/peer/index.ts +929 -0
  128. package/src/reliable/registry/index.ts +107 -0
  129. package/src/serve/bootstrap/index.ts +606 -0
  130. package/src/serve/channel/index.ts +246 -0
  131. package/src/serve/index.ts +17 -0
  132. package/src/serve/outbox/index.ts +292 -0
  133. package/src/serve/transport/index.ts +96 -0
  134. package/src/serve/writer/index.ts +119 -0
  135. package/src/shard/envelope/index.ts +44 -0
  136. package/src/shard/gateway/index.ts +119 -0
  137. package/src/shard/index.ts +11 -0
  138. package/src/shard/link/index.ts +743 -0
  139. package/src/shard/map/index.ts +91 -0
  140. package/src/worker/group/index.ts +175 -0
  141. package/src/worker/index.ts +10 -0
  142. package/src/worker/pool/index.ts +235 -0
  143. package/src/worker/service/index.ts +55 -0
@@ -0,0 +1,33 @@
1
+ /**
2
+ * Thrown when inbound bytes can't be turned into a message
3
+ * @class
4
+ */
5
+ export class DecoderError extends Error {
6
+ /**
7
+ * Builds a decoding error
8
+ * @param message short description of what went wrong
9
+ * @param options usually carries the underlying cause
10
+ */
11
+ public constructor(message: string, options?: ErrorOptions) {
12
+ super(message, options)
13
+
14
+ this.name = 'DecoderError'
15
+ }
16
+ }
17
+
18
+ /**
19
+ * Thrown when an outbound message can't be encoded
20
+ * @class
21
+ */
22
+ export class EncoderError extends Error {
23
+ /**
24
+ * Builds an encoding error
25
+ * @param message short description of what went wrong
26
+ * @param options usually carries the underlying cause
27
+ */
28
+ public constructor(message: string, options?: ErrorOptions) {
29
+ super(message, options)
30
+
31
+ this.name = 'EncoderError'
32
+ }
33
+ }
@@ -0,0 +1,45 @@
1
+ import type { HandlerContext } from '../../channel/context/index.ts'
2
+ import { ByteToMessageDecoder, type DecoderOptions } from '../decoder/index.ts'
3
+ import type { ByteBuf } from '../../buffer/index.ts'
4
+
5
+ /**
6
+ * Chops the byte stream into frames of the same size
7
+ * @class
8
+ */
9
+ export class FixedLengthFrameDecoder extends ByteToMessageDecoder<ByteBuf> {
10
+ /**
11
+ * Bytes per frame
12
+ */
13
+ private readonly frameLength: number
14
+
15
+ /**
16
+ * Sets up a decoder for frames of this length
17
+ * @param frameLength a positive whole number of bytes
18
+ * @param options buffering and pooling
19
+ */
20
+ public constructor(frameLength: number, options: DecoderOptions = {}) {
21
+ super(options)
22
+
23
+ if (!Number.isInteger(frameLength) || frameLength <= 0) {
24
+ throw new RangeError('Frame length is invalid')
25
+ }
26
+
27
+ this.frameLength = frameLength
28
+ }
29
+
30
+ /**
31
+ * Slices off one frame once enough bytes are in
32
+ * @param context unused here
33
+ * @param input bytes gathered so far
34
+ * @param out where finished frames go
35
+ */
36
+ protected decode(
37
+ context: HandlerContext,
38
+ input: ByteBuf,
39
+ out: ByteBuf[],
40
+ ): void {
41
+ if (input.isReadable(this.frameLength)) {
42
+ out.push(this.takeFrame(input, this.frameLength))
43
+ }
44
+ }
45
+ }
@@ -0,0 +1,236 @@
1
+ import { InboundHandlerAdapter } from '../../../channel/handler/inbound/index.ts'
2
+ import { resolved } from '../../../channel/resolved/index.ts'
3
+ import { ByteBuf } from '../../../buffer/index.ts'
4
+ import type { HandlerContext } from '../../../channel/context/index.ts'
5
+ import { DecoderError } from '../../error/index.ts'
6
+ import type { HttpHeaders } from '../headers/index.ts'
7
+ import { HttpContent } from '../message/content/index.ts'
8
+ import { FullHttpRequest } from '../message/full/request/index.ts'
9
+ import { FullHttpResponse } from '../message/full/response/index.ts'
10
+ import type { FullHttpMessage } from '../message/full/index.ts'
11
+ import { HttpMessage } from '../message/head/index.ts'
12
+ import { LastHttpContent } from '../message/last/index.ts'
13
+ import { HttpRequest } from '../message/request/index.ts'
14
+ import { HttpResponse } from '../message/response/index.ts'
15
+ import { HttpResponseStatus } from '../status/index.ts'
16
+
17
+ /**
18
+ * Buffers a head and its body chunks into one FullHttpRequest or FullHttpResponse
19
+ * @class
20
+ */
21
+ export class HttpObjectAggregator extends InboundHandlerAdapter {
22
+ /**
23
+ * Body size cap in bytes
24
+ */
25
+ private readonly maxContentLength: number
26
+
27
+ /**
28
+ * Head we're collecting a body for, or null
29
+ */
30
+ private message: HttpMessage | null
31
+
32
+ /**
33
+ * Body bytes collected so far
34
+ */
35
+ private content: ByteBuf
36
+
37
+ /**
38
+ * Set while skipping the rest of a body that was too big
39
+ */
40
+ private discarding: boolean
41
+
42
+ /**
43
+ * Sets up an aggregator with a body size cap
44
+ * @param maxContentLength bodies past this many bytes get a 413
45
+ */
46
+ public constructor(maxContentLength: number) {
47
+ super()
48
+
49
+ if (!Number.isInteger(maxContentLength) || maxContentLength < 0) {
50
+ throw new RangeError('Content length is invalid')
51
+ }
52
+
53
+ this.maxContentLength = maxContentLength
54
+ this.message = null
55
+ this.content = ByteBuf.allocate(0)
56
+ this.discarding = false
57
+ }
58
+
59
+ /**
60
+ * Collects heads and chunks, letting full messages and anything else straight through
61
+ * @param context where full messages go
62
+ * @param message whatever the decoder produced
63
+ */
64
+ public override channelRead(
65
+ context: HandlerContext,
66
+ message: unknown,
67
+ ): Promise<void> {
68
+ if (
69
+ message instanceof FullHttpRequest ||
70
+ message instanceof FullHttpResponse
71
+ ) {
72
+ return context.fireChannelRead(message)
73
+ }
74
+ if (message instanceof HttpMessage) return this.begin(context, message)
75
+ if (message instanceof HttpContent) return this.append(context, message)
76
+
77
+ return context.fireChannelRead(message)
78
+ }
79
+
80
+ /**
81
+ * Throws away a half built message when the connection drops
82
+ * @param context the handler's context
83
+ */
84
+ public override channelInactive(context: HandlerContext): Promise<void> {
85
+ this.message = null
86
+ this.content = ByteBuf.allocate(0)
87
+
88
+ return context.fireChannelInactive()
89
+ }
90
+
91
+ /**
92
+ * Starts a new message, answering Expect 100-continue and refusing oversized ones early
93
+ * @param context used for 100 Continue and 413 replies
94
+ * @param message head that has arrived
95
+ */
96
+ private begin(context: HandlerContext, message: HttpMessage): Promise<void> {
97
+ this.discarding = false
98
+ this.message = null
99
+
100
+ const length = message.contentLength()
101
+ if (length !== null && length > this.maxContentLength) {
102
+ return this.oversized(context, message, true)
103
+ }
104
+
105
+ if (message instanceof HttpRequest && this.expectsContinue(message)) {
106
+ message.headers.remove('expect')
107
+ context.writeAndFlushSync(
108
+ new FullHttpResponse(HttpResponseStatus.CONTINUE),
109
+ )
110
+ }
111
+
112
+ this.message = message
113
+ this.content = ByteBuf.allocate(length ?? 0)
114
+ return resolved
115
+ }
116
+
117
+ /**
118
+ * Adds a chunk and fires the full message once the last one lands
119
+ * @param context where the full message goes
120
+ * @param content next piece of the body
121
+ */
122
+ private append(context: HandlerContext, content: HttpContent): Promise<void> {
123
+ const last = content instanceof LastHttpContent
124
+ if (this.discarding) {
125
+ this.discarding = !last
126
+ return resolved
127
+ }
128
+
129
+ const message = this.message
130
+ if (message === null) return context.fireChannelRead(content)
131
+ const size = this.content.readableBytes + content.content.readableBytes
132
+ if (size > this.maxContentLength) {
133
+ this.message = null
134
+ return this.oversized(context, message, !last)
135
+ }
136
+
137
+ if (content.content.isReadable()) {
138
+ this.content.writeBytes(content.content)
139
+ }
140
+
141
+ if (!(content instanceof LastHttpContent)) return resolved
142
+
143
+ const full = this.complete(message, content.trailers)
144
+ this.message = null
145
+ this.content = ByteBuf.allocate(0)
146
+ return context.fireChannelRead(full)
147
+ }
148
+
149
+ /**
150
+ * Puts the full message together, swapping chunked encoding for a Content-Length
151
+ * @param message head we've been collecting for
152
+ * @param trailers from the last chunk
153
+ */
154
+ private complete(
155
+ message: HttpMessage,
156
+ trailers: HttpHeaders,
157
+ ): FullHttpMessage {
158
+ const full =
159
+ message instanceof HttpResponse
160
+ ? new FullHttpResponse(
161
+ message.status,
162
+ this.content,
163
+ message.version,
164
+ message.headers,
165
+ trailers,
166
+ )
167
+ : this.completeRequest(message, trailers)
168
+
169
+ if (full.isChunked()) full.headers.remove('transfer-encoding')
170
+ if (!full.headers.has('content-length')) {
171
+ full.setContentLength(this.content.readableBytes)
172
+ }
173
+
174
+ return full
175
+ }
176
+
177
+ /**
178
+ * Request half of complete
179
+ * @param message should be a request head
180
+ * @param trailers from the last chunk
181
+ */
182
+ private completeRequest(
183
+ message: HttpMessage,
184
+ trailers: HttpHeaders,
185
+ ): FullHttpRequest {
186
+ if (!(message instanceof HttpRequest)) {
187
+ throw new DecoderError('Message is unsupported')
188
+ }
189
+
190
+ return new FullHttpRequest(
191
+ message.method,
192
+ message.uri,
193
+ this.content,
194
+ message.version,
195
+ message.headers,
196
+ trailers,
197
+ )
198
+ }
199
+
200
+ /**
201
+ * True for an HTTP/1.1 request with Expect 100-continue
202
+ * @param request head to check
203
+ */
204
+ private expectsContinue(request: HttpRequest): boolean {
205
+ return (
206
+ request.version.keepAliveDefault &&
207
+ request.headers.hasToken('expect', '100-continue')
208
+ )
209
+ }
210
+
211
+ /**
212
+ * Sends 413 and closes for requests, throws for responses
213
+ * @param context where the 413 goes
214
+ * @param message head of the oversized message
215
+ * @param discard true if more body chunks are still on the way
216
+ */
217
+ private async oversized(
218
+ context: HandlerContext,
219
+ message: HttpMessage,
220
+ discard: boolean,
221
+ ): Promise<void> {
222
+ this.discarding = discard
223
+ this.content = ByteBuf.allocate(0)
224
+ if (!(message instanceof HttpRequest)) {
225
+ throw new DecoderError('Content is too large')
226
+ }
227
+
228
+ const response = new FullHttpResponse(
229
+ HttpResponseStatus.REQUEST_ENTITY_TOO_LARGE,
230
+ )
231
+ response.setContentLength(0)
232
+ response.headers.set('Connection', 'close')
233
+ await context.writeAndFlush(response)
234
+ await context.close()
235
+ }
236
+ }
@@ -0,0 +1,120 @@
1
+ import { DuplexHandlerAdapter } from '../../../channel/handler/duplex/index.ts'
2
+ import type { HttpDecoderOptions } from '../decoder/object/index.ts'
3
+ import { HttpResponseDecoder } from '../decoder/response/index.ts'
4
+ import { HttpRequestEncoder } from '../encoder/request/index.ts'
5
+ import type { HttpMessage } from '../message/head/index.ts'
6
+ import { HttpRequest } from '../message/request/index.ts'
7
+ import { HttpResponse } from '../message/response/index.ts'
8
+ import { HttpMethod } from '../method/index.ts'
9
+ import type { HandlerContext } from '../../../channel/context/index.ts'
10
+
11
+ /**
12
+ * Response decoder that tracks requests in flight, so HEAD replies aren't read as having a body
13
+ * @class
14
+ */
15
+ class AnsweringResponseDecoder extends HttpResponseDecoder {
16
+ /**
17
+ * Shared with the codec, oldest request first
18
+ */
19
+ private readonly methods: HttpMethod[]
20
+
21
+ /**
22
+ * Sets up the decoder with the codec's queue
23
+ * @param options size limits for incoming responses
24
+ * @param methods queue the codec pushes to on each write
25
+ */
26
+ public constructor(options: HttpDecoderOptions, methods: HttpMethod[]) {
27
+ super(options)
28
+
29
+ this.methods = methods
30
+ }
31
+
32
+ /**
33
+ * Interim 1xx replies don't use up a request, anything else pops one
34
+ * @param message head that has come in
35
+ */
36
+ protected override isContentAlwaysEmpty(message: HttpMessage): boolean {
37
+ if (!(message instanceof HttpResponse)) return false
38
+ const interim =
39
+ message.status.isInformational && message.status.code !== 101
40
+ if (interim) return true
41
+
42
+ const method = this.methods.shift()
43
+ return method === HttpMethod.HEAD || super.isContentAlwaysEmpty(message)
44
+ }
45
+ }
46
+
47
+ /**
48
+ * Client side HTTP in one handler, requests out and responses in
49
+ * @class
50
+ */
51
+ export class HttpClientCodec extends DuplexHandlerAdapter {
52
+ /**
53
+ * Methods of requests sent but not answered yet
54
+ */
55
+ private readonly methods: HttpMethod[]
56
+
57
+ /**
58
+ * Handles everything inbound
59
+ */
60
+ private readonly decoder: HttpResponseDecoder
61
+
62
+ /**
63
+ * Handles everything outbound
64
+ */
65
+ private readonly encoder: HttpRequestEncoder
66
+
67
+ /**
68
+ * Sets up both halves with a shared request queue
69
+ * @param options size limits for incoming responses
70
+ */
71
+ public constructor(options: HttpDecoderOptions = {}) {
72
+ super()
73
+
74
+ this.methods = []
75
+ this.decoder = new AnsweringResponseDecoder(options, this.methods)
76
+ this.encoder = new HttpRequestEncoder()
77
+ }
78
+
79
+ /**
80
+ * Hands inbound bytes to the response decoder
81
+ * @param context where responses go
82
+ * @param message raw bytes, usually
83
+ */
84
+ public override channelRead(
85
+ context: HandlerContext,
86
+ message: unknown,
87
+ ): Promise<void> {
88
+ return this.decoder.channelRead(context, message)
89
+ }
90
+
91
+ /**
92
+ * Lets the decoder finish up when the connection drops
93
+ * @param context the handler's context
94
+ */
95
+ public override channelInactive(context: HandlerContext): Promise<void> {
96
+ return this.decoder.channelInactive(context)
97
+ }
98
+
99
+ /**
100
+ * Forwards leftover bytes, handy after an upgrade
101
+ * @param context where the leftovers go
102
+ */
103
+ public handlerRemoved(context: HandlerContext): Promise<void> {
104
+ return this.decoder.handlerRemoved(context)
105
+ }
106
+
107
+ /**
108
+ * Notes the method of each request, then encodes it
109
+ * @param context where the bytes go
110
+ * @param message a request head, body chunk or full request
111
+ */
112
+ public override write(
113
+ context: HandlerContext,
114
+ message: unknown,
115
+ ): Promise<void> {
116
+ if (message instanceof HttpRequest) this.methods.push(message.method)
117
+
118
+ return this.encoder.write(context, message)
119
+ }
120
+ }
@@ -0,0 +1,4 @@
1
+ export { HttpObjectDecoder } from './object/index.ts'
2
+ export type { HttpDecoderOptions } from './object/index.ts'
3
+ export { HttpRequestDecoder } from './request/index.ts'
4
+ export { HttpResponseDecoder } from './response/index.ts'