@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.
Files changed (72) hide show
  1. package/README.md +6 -6
  2. package/package.json +1 -1
  3. package/src/bootstrap/client/index.ts +1 -1
  4. package/src/bootstrap/dispatcher/index.ts +7 -0
  5. package/src/bootstrap/reconnect/index.ts +14 -4
  6. package/src/bootstrap/server/index.ts +10 -4
  7. package/src/buffer/index.ts +229 -88
  8. package/src/buffer/quantizer/index.ts +16 -3
  9. package/src/channel/context/index.ts +18 -11
  10. package/src/channel/handler/index.ts +1 -1
  11. package/src/channel/index.ts +30 -7
  12. package/src/channel/outbound/index.ts +28 -12
  13. package/src/channel/pipeline/index.ts +45 -5
  14. package/src/channel/queue/index.ts +3 -2
  15. package/src/channel/resolved/index.ts +13 -3
  16. package/src/channel/server/index.ts +19 -10
  17. package/src/cluster/index.ts +73 -32
  18. package/src/codec/binary/index.ts +19 -0
  19. package/src/codec/decoder/index.ts +223 -83
  20. package/src/codec/delimiter/index.ts +95 -37
  21. package/src/codec/encoder/index.ts +30 -5
  22. package/src/codec/fixed/index.ts +5 -1
  23. package/src/codec/http/aggregator/index.ts +45 -16
  24. package/src/codec/http/decoder/object/index.ts +156 -40
  25. package/src/codec/http/decoder/request/index.ts +6 -2
  26. package/src/codec/http/encoder/object/index.ts +2 -7
  27. package/src/codec/http/encoder/request/index.ts +5 -0
  28. package/src/codec/http/headers/index.ts +45 -2
  29. package/src/codec/http/message/full/request/index.ts +1 -1
  30. package/src/codec/http/message/full/response/index.ts +1 -1
  31. package/src/codec/http/message/request/index.ts +14 -1
  32. package/src/codec/http/message/response/index.ts +1 -1
  33. package/src/codec/http/version/index.ts +3 -0
  34. package/src/codec/length/index.ts +66 -44
  35. package/src/codec/message/decoder/index.ts +18 -1
  36. package/src/codec/message/encoder/index.ts +85 -8
  37. package/src/codec/prepender/index.ts +4 -1
  38. package/src/codec/string/decoder/index.ts +6 -1
  39. package/src/codec/websocket/decoder/index.ts +78 -3
  40. package/src/codec/websocket/encoder/index.ts +90 -6
  41. package/src/codec/websocket/frame/base/index.ts +19 -10
  42. package/src/codec/websocket/frame/text/index.ts +2 -2
  43. package/src/codec/websocket/protocol/index.ts +12 -0
  44. package/src/datagram/bootstrap/index.ts +1 -1
  45. package/src/datagram/transport/index.ts +30 -1
  46. package/src/handler/flush/index.ts +25 -5
  47. package/src/handler/idle/index.ts +120 -26
  48. package/src/handler/logging/index.ts +13 -1
  49. package/src/handler/metrics/index.ts +49 -16
  50. package/src/handler/offload/index.ts +7 -0
  51. package/src/handler/timeout/read/index.ts +1 -1
  52. package/src/handler/timeout/write/index.ts +104 -23
  53. package/src/handler/traffic/index.ts +251 -38
  54. package/src/index.ts +1 -0
  55. package/src/reliable/cipher/index.ts +49 -5
  56. package/src/reliable/cookie/index.ts +64 -2
  57. package/src/reliable/handler/index.ts +11 -15
  58. package/src/reliable/index.ts +7 -1
  59. package/src/reliable/lane/index.ts +277 -58
  60. package/src/reliable/peer/index.ts +125 -38
  61. package/src/serve/bootstrap/index.ts +82 -135
  62. package/src/serve/channel/index.ts +84 -2
  63. package/src/serve/gate/index.ts +215 -0
  64. package/src/serve/index.ts +6 -2
  65. package/src/serve/outbox/index.ts +63 -2
  66. package/src/serve/transport/index.ts +33 -3
  67. package/src/serve/writer/index.ts +4 -3
  68. package/src/shard/gateway/index.ts +102 -29
  69. package/src/shard/link/index.ts +161 -175
  70. package/src/worker/group/index.ts +231 -13
  71. package/src/worker/pool/index.ts +100 -19
  72. package/src/worker/service/index.ts +9 -1
package/README.md CHANGED
@@ -86,12 +86,12 @@ One core, 64-byte messages, Ryzen 7 7800X3D. Method and full results are on the
86
86
 
87
87
  | Test | Netify | Compared with |
88
88
  | ------------------------------------- | ------------ | ----------------------------------------------------- |
89
- | TCP echo, length-framed | 2.52M msg/s | `node:net` 2.42M, `Bun.listen` 2.84M |
90
- | UDP echo | 149k msg/s | `node:dgram` 68k, `Bun.udpSocket` 146k |
91
- | WebSocket echo | 1.19M msg/s | `ws` 1.20M, uWebSockets.js 1.17M |
92
- | WebSocket broadcast to 1000 clients | 2.16M msg/s | uWebSockets.js 1.69M, `ws` 200k |
93
- | HTTP plaintext, pipelined | 208k req/s | `Bun.serve` 148k, `node:http` 127k |
94
- | TCP echo on 4 cores | 7.14M msg/s | `node:net` 6.35M, `Bun.listen` 8.23M |
89
+ | TCP echo, length-framed | 2.51M msg/s | `node:net` 2.42M, `Bun.listen` 2.84M |
90
+ | UDP echo | 154k msg/s | `node:dgram` 68k, `Bun.udpSocket` 146k |
91
+ | WebSocket echo | 1.22M msg/s | `ws` 1.20M, uWebSockets.js 1.17M |
92
+ | WebSocket broadcast to 1000 clients | 1.83M msg/s | uWebSockets.js 1.69M, `ws` 200k |
93
+ | HTTP plaintext, pipelined | 231k req/s | `Bun.serve` 148k, `node:http` 127k |
94
+ | TCP echo on 4 cores | 7.29M msg/s | `node:net` 6.35M, `Bun.listen` 8.23M |
95
95
 
96
96
  ## Docs
97
97
 
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@netifyjs/netify",
3
3
  "description": "A Netty-inspired networking framework for Bun, for TCP, UDP, reliable UDP and WebSocket game servers",
4
- "version": "1.0.0-alpha.3",
4
+ "version": "1.0.0-alpha.5",
5
5
  "type": "module",
6
6
  "exports": {
7
7
  ".": "./src/index.ts"
@@ -47,7 +47,7 @@ export type ConnectOptions = TcpConnectOptions | UnixConnectOptions
47
47
  */
48
48
  export type ClientOptions = {
49
49
  /**
50
- * True for Bun's defaults, or your own settings, plain TCP if left out
50
+ * TLS settings, true for Bun's defaults
51
51
  */
52
52
  readonly tls?: TLSOptions | boolean
53
53
  } & SocketOptions
@@ -37,6 +37,12 @@ export type SocketOptions = {
37
37
  * How a dispatcher tells its bootstrap a connection worked or didn't
38
38
  */
39
39
  export type DispatcherEvents = {
40
+ /**
41
+ * Runs on a fresh channel before setup
42
+ * @param channel not active yet
43
+ */
44
+ accepted?(channel: Channel): void
45
+
40
46
  /**
41
47
  * Fires after the initializer has run and the channel is live
42
48
  * @param channel ready to use
@@ -178,6 +184,7 @@ export class Dispatcher {
178
184
 
179
185
  const channel = new Channel(new SocketTransport(socket), this.options)
180
186
  socket.data = channel
187
+ this.events.accepted?.(channel)
181
188
  socket.setNoDelay(this.options.noDelay ?? true)
182
189
  if (this.options.keepAlive === true) socket.setKeepAlive(true)
183
190
  const idle = this.options.idleTimeout ?? 0
@@ -136,6 +136,11 @@ export class ReconnectingClient {
136
136
  */
137
137
  private running: boolean
138
138
 
139
+ /**
140
+ * Bumped on every stop
141
+ */
142
+ private run: number
143
+
139
144
  /**
140
145
  * Throws a RangeError on bad backoff settings but doesn't connect until start
141
146
  * @param bootstrap a configured client bootstrap
@@ -167,6 +172,7 @@ export class ReconnectingClient {
167
172
  this.timer = null
168
173
  this.attempts = 0
169
174
  this.running = false
175
+ this.run = 0
170
176
  }
171
177
 
172
178
  /**
@@ -206,6 +212,7 @@ export class ReconnectingClient {
206
212
  public start(): Promise<Channel> {
207
213
  if (!this.running) {
208
214
  this.running = true
215
+ this.attempts = 0
209
216
  void this.attempt()
210
217
  }
211
218
 
@@ -229,6 +236,7 @@ export class ReconnectingClient {
229
236
  */
230
237
  public async stop(): Promise<void> {
231
238
  this.running = false
239
+ this.run += 1
232
240
  if (this.timer !== null) clearTimeout(this.timer)
233
241
  this.timer = null
234
242
  this.reject(new Error('Client is stopped'))
@@ -248,15 +256,16 @@ export class ReconnectingClient {
248
256
  this.timer = null
249
257
  if (!this.running) return
250
258
 
259
+ const run = this.run
251
260
  let channel: Channel
252
261
  try {
253
262
  channel = await this.bootstrap.connect(this.address)
254
263
  } catch (error) {
255
- this.retry(error)
264
+ if (run === this.run) this.retry(error)
256
265
  return
257
266
  }
258
267
 
259
- if (this.isStopped()) {
268
+ if (this.isStale(run)) {
260
269
  await channel.close()
261
270
  return
262
271
  }
@@ -266,9 +275,10 @@ export class ReconnectingClient {
266
275
 
267
276
  /**
268
277
  * Read fresh after an await, since stop may have run in between
278
+ * @param run which run started it
269
279
  */
270
- private isStopped(): boolean {
271
- return !this.running
280
+ private isStale(run: number): boolean {
281
+ return !this.running || run !== this.run
272
282
  }
273
283
 
274
284
  /**
@@ -18,7 +18,7 @@ import { ServerChannel } from '../../channel/server/index.ts'
18
18
  */
19
19
  export type TcpBindOptions = {
20
20
  /**
21
- * Interface to bind, every interface if left out
21
+ * Interface to bind, or all of them
22
22
  */
23
23
  readonly hostname?: string
24
24
 
@@ -48,7 +48,7 @@ export type BindOptions = TcpBindOptions | UnixBindOptions
48
48
  */
49
49
  export type ServerOptions = {
50
50
  /**
51
- * Certificates and so on, plain TCP if left out
51
+ * Certificates for TLS
52
52
  */
53
53
  readonly tls?: TLSOptions
54
54
 
@@ -174,15 +174,21 @@ export class ServerBootstrap {
174
174
  this.childSettings,
175
175
  {
176
176
  /**
177
- * Tracks the child so ServerChannel.close can reach it
177
+ * Tracks the child from accept
178
178
  * @param channel the new child
179
179
  */
180
- opened: (channel: Channel): void => {
180
+ accepted: (channel: Channel): void => {
181
181
  children.add(channel)
182
182
  void channel.closed.then((): void => {
183
183
  children.delete(channel)
184
184
  })
185
185
  },
186
+ /**
187
+ * Already tracked
188
+ */
189
+ opened: (): void => {
190
+ return
191
+ },
186
192
  /**
187
193
  * A child that never got going has nothing to clean up
188
194
  */