@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,119 @@
1
+ import { ByteBuf } from '../../buffer/index.ts'
2
+ import type { HandlerContext } from '../../channel/context/index.ts'
3
+ import { OutboundHandlerAdapter } from '../../channel/handler/outbound/index.ts'
4
+ import { resolved } from '../../channel/resolved/index.ts'
5
+ import { BinaryWebSocketFrame } from '../../codec/websocket/frame/binary/index.ts'
6
+ import { CloseWebSocketFrame } from '../../codec/websocket/frame/close/index.ts'
7
+ import { PingWebSocketFrame } from '../../codec/websocket/frame/ping/index.ts'
8
+ import { PongWebSocketFrame } from '../../codec/websocket/frame/pong/index.ts'
9
+ import { TextWebSocketFrame } from '../../codec/websocket/frame/text/index.ts'
10
+ import type { WebSocketChannel } from '../channel/index.ts'
11
+ import type { OutgoingMessage } from '../outbox/index.ts'
12
+
13
+ /**
14
+ * First in the pipeline, hands frames, bytes and strings straight to the native socket
15
+ * @class
16
+ */
17
+ export class WebSocketFrameWriter extends OutboundHandlerAdapter {
18
+ /**
19
+ * The channel whose socket gets the writes
20
+ */
21
+ private readonly channel: WebSocketChannel
22
+
23
+ /**
24
+ * Takes the channel
25
+ * @param channel the channel
26
+ */
27
+ public constructor(channel: WebSocketChannel) {
28
+ super()
29
+
30
+ this.channel = channel
31
+ }
32
+
33
+ /**
34
+ * Sends a frame, bytes or a string, anything else goes on to the head
35
+ * @param context where anything else goes
36
+ * @param message what's being sent
37
+ */
38
+ public override write(
39
+ context: HandlerContext,
40
+ message: unknown,
41
+ ): Promise<void> {
42
+ const outgoing = WebSocketFrameWriter.outgoingOf(message)
43
+ if (outgoing === null) return context.write(message)
44
+
45
+ switch (this.channel.sendNative(outgoing)) {
46
+ case 'full':
47
+ return Promise.reject(new Error('Write queue is full'))
48
+ case 'closed':
49
+ return Promise.reject(new Error('Channel is closed'))
50
+ default:
51
+ return resolved
52
+ }
53
+ }
54
+
55
+ /**
56
+ * Sends binary straight to the socket, false for anything else
57
+ * @param context where a full queue is reported
58
+ * @param message what's being sent
59
+ */
60
+ public writeSync(context: HandlerContext, message: unknown): boolean {
61
+ let bytes: Uint8Array
62
+ if (message instanceof ByteBuf) bytes = message.toUint8Array()
63
+ else if (message instanceof BinaryWebSocketFrame) {
64
+ bytes = message.content.toUint8Array()
65
+ } else if (message instanceof Uint8Array) bytes = message
66
+ else return false
67
+
68
+ if (this.channel.sendBinary(bytes) === 'full') {
69
+ void context.fireExceptionCaught(new Error('Write queue is full'))
70
+ }
71
+
72
+ return true
73
+ }
74
+
75
+ /**
76
+ * Turns a frame, bytes or a string into something the socket can send, or null
77
+ * @param message what's being sent
78
+ */
79
+ private static outgoingOf(message: unknown): OutgoingMessage | null {
80
+ if (message instanceof BinaryWebSocketFrame) {
81
+ return { kind: 'binary', bytes: message.content.toUint8Array() }
82
+ }
83
+
84
+ if (message instanceof ByteBuf) {
85
+ return { kind: 'binary', bytes: message.toUint8Array() }
86
+ }
87
+
88
+ if (message instanceof Uint8Array) return { kind: 'binary', bytes: message }
89
+ if (typeof message === 'string') return { kind: 'text', text: message }
90
+ if (message instanceof TextWebSocketFrame) {
91
+ return { kind: 'text', text: message.text() }
92
+ }
93
+
94
+ if (message instanceof CloseWebSocketFrame) {
95
+ return {
96
+ kind: 'close',
97
+ code: message.statusCode ?? 1000,
98
+ reason: message.reasonText,
99
+ }
100
+ }
101
+
102
+ if (message instanceof PingWebSocketFrame) {
103
+ return { kind: 'ping', bytes: message.content.toUint8Array() }
104
+ }
105
+
106
+ if (message instanceof PongWebSocketFrame) {
107
+ return { kind: 'pong', bytes: message.content.toUint8Array() }
108
+ }
109
+
110
+ return null
111
+ }
112
+
113
+ /**
114
+ * Bun sends as it goes, so there's nothing to flush
115
+ */
116
+ public override flush(): Promise<void> {
117
+ return resolved
118
+ }
119
+ }
@@ -0,0 +1,44 @@
1
+ /**
2
+ * What a shard message is for
3
+ */
4
+ export type ShardKind = 'send' | 'request' | 'reply'
5
+
6
+ /**
7
+ * One message between shards, relayed by the cluster parent
8
+ */
9
+ export type ShardMessage = {
10
+ /**
11
+ * What it's for
12
+ */
13
+ readonly kind: ShardKind
14
+
15
+ /**
16
+ * Sending shard
17
+ */
18
+ readonly from: number
19
+
20
+ /**
21
+ * Target shard, or -1 for every other shard
22
+ */
23
+ readonly to: number
24
+
25
+ /**
26
+ * Listener or handler name
27
+ */
28
+ readonly topic: string
29
+
30
+ /**
31
+ * Request id, 0 for plain sends
32
+ */
33
+ readonly id: number
34
+
35
+ /**
36
+ * The payload
37
+ */
38
+ readonly data: unknown
39
+
40
+ /**
41
+ * Why a request failed, or null
42
+ */
43
+ readonly error: string | null
44
+ }
@@ -0,0 +1,119 @@
1
+ import { ByteBuf } from '../../buffer/index.ts'
2
+ import { ChannelGroup } from '../../channel/group/index.ts'
3
+ import type { Channel } from '../../channel/index.ts'
4
+ import type { ShardLink } from '../link/index.ts'
5
+
6
+ /**
7
+ * One publish as it crosses the link
8
+ */
9
+ type Publication = readonly [topic: string, bytes: Uint8Array]
10
+
11
+ /**
12
+ * Topics whose subscribers can live on any shard
13
+ * @class
14
+ */
15
+ export class Gateway {
16
+ /**
17
+ * Link topic that carries publishes
18
+ */
19
+ public static readonly topic: string = 'netify:publish'
20
+
21
+ /**
22
+ * Carries publishes to the other shards
23
+ */
24
+ private readonly link: ShardLink
25
+
26
+ /**
27
+ * This shard's subscribers, by topic
28
+ */
29
+ private readonly groups: Map<string, ChannelGroup>
30
+
31
+ /**
32
+ * Starts listening for publishes from other shards
33
+ * @param link the shard's link
34
+ */
35
+ public constructor(link: ShardLink) {
36
+ this.link = link
37
+ this.groups = new Map<string, ChannelGroup>()
38
+
39
+ link.on(Gateway.topic, (data: unknown): void => {
40
+ const [topic, bytes] = data as Publication
41
+ this.deliver(topic, bytes)
42
+ })
43
+ }
44
+
45
+ /**
46
+ * Adds a local channel to a topic
47
+ * @param channel a connection on this shard
48
+ * @param topic any name, like a zone id
49
+ */
50
+ public subscribe(channel: Channel, topic: string): void {
51
+ let group = this.groups.get(topic)
52
+ if (group === undefined) {
53
+ group = new ChannelGroup()
54
+ this.groups.set(topic, group)
55
+ }
56
+
57
+ group.add(channel)
58
+ }
59
+
60
+ /**
61
+ * Takes a channel off a topic, false if it wasn't on it
62
+ * @param channel a connection on this shard
63
+ * @param topic the topic
64
+ */
65
+ public unsubscribe(channel: Channel, topic: string): boolean {
66
+ const group = this.groups.get(topic)
67
+ if (group === undefined) return false
68
+
69
+ const removed = group.remove(channel)
70
+ if (group.size === 0) this.groups.delete(topic)
71
+ return removed
72
+ }
73
+
74
+ /**
75
+ * Whether a local channel is on a topic
76
+ * @param channel a connection on this shard
77
+ * @param topic the topic
78
+ */
79
+ public isSubscribed(channel: Channel, topic: string): boolean {
80
+ return this.groups.get(topic)?.has(channel) ?? false
81
+ }
82
+
83
+ /**
84
+ * Local channels on a topic
85
+ * @param topic the topic
86
+ */
87
+ public subscriberCount(topic: string): number {
88
+ return this.groups.get(topic)?.size ?? 0
89
+ }
90
+
91
+ /**
92
+ * Sends encoded bytes to subscribers everywhere
93
+ * @param topic the topic
94
+ * @param bytes a whole frame, skipping the pipelines
95
+ */
96
+ public publish(topic: string, bytes: Uint8Array | ByteBuf): number {
97
+ const shared = bytes instanceof ByteBuf ? bytes.toUint8Array() : bytes
98
+
99
+ this.link.broadcast(Gateway.topic, [topic, shared])
100
+ return this.deliver(topic, shared)
101
+ }
102
+
103
+ /**
104
+ * Writes to this shard's subscribers and forgets topics nobody's on
105
+ * @param topic the topic
106
+ * @param bytes the frame
107
+ */
108
+ private deliver(topic: string, bytes: Uint8Array): number {
109
+ const group = this.groups.get(topic)
110
+ if (group === undefined) return 0
111
+
112
+ if (group.size === 0) {
113
+ this.groups.delete(topic)
114
+ return 0
115
+ }
116
+
117
+ return group.broadcastSync(bytes)
118
+ }
119
+ }
@@ -0,0 +1,11 @@
1
+ export type { ShardKind, ShardMessage } from './envelope/index.ts'
2
+ export { Gateway } from './gateway/index.ts'
3
+ export { ShardLink } from './link/index.ts'
4
+ export type {
5
+ ShardLinkOptions,
6
+ ShardListener,
7
+ ShardPeer,
8
+ ShardResponder,
9
+ } from './link/index.ts'
10
+ export { ShardMap } from './map/index.ts'
11
+ export type { ShardMapOptions } from './map/index.ts'