@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.
Files changed (142) 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/index.ts +10 -0
  137. package/src/shard/link/index.ts +743 -0
  138. package/src/shard/map/index.ts +91 -0
  139. package/src/worker/group/index.ts +175 -0
  140. package/src/worker/index.ts +10 -0
  141. package/src/worker/pool/index.ts +235 -0
  142. package/src/worker/service/index.ts +55 -0
@@ -0,0 +1,46 @@
1
+ import type { ByteBuf } from '../index.ts'
2
+
3
+ /**
4
+ * Free list of buffer objects that release hands back
5
+ * @class
6
+ */
7
+ export class ByteBufPool {
8
+ /**
9
+ * Most free buffers kept by default
10
+ */
11
+ public static readonly defaultLimit: number = 1024
12
+
13
+ /**
14
+ * Buffers ready to be handed out again
15
+ */
16
+ private readonly free: ByteBuf[]
17
+
18
+ /**
19
+ * Most free buffers kept, the rest are left to the garbage collector
20
+ */
21
+ private readonly limit: number
22
+
23
+ /**
24
+ * Starts empty
25
+ * @param limit most free buffers kept
26
+ */
27
+ public constructor(limit: number = ByteBufPool.defaultLimit) {
28
+ this.free = []
29
+ this.limit = limit
30
+ }
31
+
32
+ /**
33
+ * A free buffer, or null when there are none
34
+ */
35
+ public take(): ByteBuf | null {
36
+ return this.free.pop() ?? null
37
+ }
38
+
39
+ /**
40
+ * Keeps a released buffer for later
41
+ * @param buffer one whose count reached zero
42
+ */
43
+ public give(buffer: ByteBuf): void {
44
+ if (this.free.length < this.limit) this.free.push(buffer)
45
+ }
46
+ }
@@ -0,0 +1,101 @@
1
+ /**
2
+ * The range and resolution of a quantised value
3
+ */
4
+ export type QuantizerOptions = {
5
+ /**
6
+ * Smallest value, anything below is clamped up to it
7
+ */
8
+ readonly min: number
9
+
10
+ /**
11
+ * Largest value, anything above is clamped down to it
12
+ */
13
+ readonly max: number
14
+
15
+ /**
16
+ * Bits to spend on it, 1 to 32
17
+ */
18
+ readonly bits: number
19
+ }
20
+
21
+ /**
22
+ * Squeezes a float in a known range into a small integer and back
23
+ * @class
24
+ */
25
+ export class Quantizer {
26
+ /**
27
+ * Bottom of the range
28
+ */
29
+ private readonly min: number
30
+
31
+ /**
32
+ * Top step, 2 to the bits minus 1
33
+ */
34
+ private readonly steps: number
35
+
36
+ /**
37
+ * Size of one step
38
+ */
39
+ private readonly step: number
40
+
41
+ /**
42
+ * Bits each value takes
43
+ */
44
+ public readonly bits: number
45
+
46
+ /**
47
+ * Sets up the range, positions from -2048 to 2048 in 20 bits gives about 4 mm steps
48
+ * @param options range and bit width
49
+ */
50
+ public constructor(options: QuantizerOptions) {
51
+ const reason = Quantizer.refusal(options)
52
+ if (reason !== null) throw new RangeError(reason)
53
+
54
+ this.min = options.min
55
+ this.bits = options.bits
56
+ this.steps = 2 ** options.bits - 1
57
+ this.step = (options.max - options.min) / this.steps
58
+ }
59
+
60
+ /**
61
+ * Checks the range and width make sense
62
+ * @param options range and bit width
63
+ */
64
+ private static refusal(options: QuantizerOptions): string | null {
65
+ if (!(options.max > options.min)) return 'Range is empty'
66
+ if (
67
+ !Number.isInteger(options.bits) ||
68
+ options.bits < 1 ||
69
+ options.bits > 32
70
+ ) {
71
+ return 'Bit width is invalid'
72
+ }
73
+
74
+ return null
75
+ }
76
+
77
+ /**
78
+ * Gap between two neighbouring values, the worst error is half of this
79
+ */
80
+ public get precision(): number {
81
+ return this.step
82
+ }
83
+
84
+ /**
85
+ * Turns a float into its integer step
86
+ * @param value the float, clamped to the range
87
+ */
88
+ public encode(value: number): number {
89
+ const index = Math.round((value - this.min) / this.step)
90
+
91
+ return index < 0 ? 0 : index > this.steps ? this.steps : index
92
+ }
93
+
94
+ /**
95
+ * Turns a step back into a float
96
+ * @param index what encode gave
97
+ */
98
+ public decode(index: number): number {
99
+ return this.min + index * this.step
100
+ }
101
+ }
@@ -0,0 +1,89 @@
1
+ /**
2
+ * Typed handle for per-channel state, compared by identity rather than name
3
+ * @class
4
+ */
5
+ export class AttributeKey<T> {
6
+ /**
7
+ * Only for debugging, two keys with the same name are still different
8
+ */
9
+ public readonly name: string
10
+
11
+ /**
12
+ * Never set at runtime, only here so T sticks to the key
13
+ */
14
+ declare protected readonly type: T
15
+
16
+ /**
17
+ * Make one per piece of state and share it as a constant
18
+ * @param name label to help when debugging
19
+ */
20
+ public constructor(name: string) {
21
+ this.name = name
22
+ }
23
+ }
24
+
25
+ /**
26
+ * Per-channel bag of state that handlers can share
27
+ * @class
28
+ */
29
+ export class AttributeMap {
30
+ /**
31
+ * Backing store, keyed by AttributeKey instance
32
+ */
33
+ private readonly values: Map<AttributeKey<unknown>, unknown>
34
+
35
+ /**
36
+ * Starts out empty
37
+ */
38
+ public constructor() {
39
+ this.values = new Map<AttributeKey<unknown>, unknown>()
40
+ }
41
+
42
+ /**
43
+ * Null if nothing's stored under this key
44
+ * @param key which attribute to look up
45
+ */
46
+ public get<T>(key: AttributeKey<T>): T | null {
47
+ if (!this.values.has(key)) return null
48
+
49
+ return this.values.get(key) as T
50
+ }
51
+
52
+ /**
53
+ * Overwrites whatever was there, chainable
54
+ * @param key which attribute to set
55
+ * @param value new value
56
+ */
57
+ public set<T>(key: AttributeKey<T>, value: T): this {
58
+ this.values.set(key, value)
59
+ return this
60
+ }
61
+
62
+ /**
63
+ * Keeps the existing value if there is one and returns whichever won
64
+ * @param key which attribute to set
65
+ * @param value used only if nothing's there yet
66
+ */
67
+ public setIfAbsent<T>(key: AttributeKey<T>, value: T): T {
68
+ if (this.values.has(key)) return this.values.get(key) as T
69
+
70
+ this.values.set(key, value)
71
+ return value
72
+ }
73
+
74
+ /**
75
+ * True even if the stored value is null or undefined
76
+ * @param key which attribute to check
77
+ */
78
+ public has<T>(key: AttributeKey<T>): boolean {
79
+ return this.values.has(key)
80
+ }
81
+
82
+ /**
83
+ * False if there was nothing to remove
84
+ * @param key which attribute to clear
85
+ */
86
+ public delete<T>(key: AttributeKey<T>): boolean {
87
+ return this.values.delete(key)
88
+ }
89
+ }