@netifyjs/netify 1.0.0-alpha.0 → 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.
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.0",
4
+ "version": "1.0.0-alpha.1",
5
5
  "type": "module",
6
6
  "exports": {
7
7
  ".": "./src/index.ts"
package/src/index.ts CHANGED
@@ -216,7 +216,7 @@ export type {
216
216
  } from './codec/index.ts'
217
217
  export { Cluster } from './cluster/index.ts'
218
218
  export type { ClusterOptions, ReloadOrder } from './cluster/index.ts'
219
- export { ShardLink, ShardMap } from './shard/index.ts'
219
+ export { Gateway, ShardLink, ShardMap } from './shard/index.ts'
220
220
  export type {
221
221
  ShardLinkOptions,
222
222
  ShardListener,
@@ -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
+ }
@@ -1,4 +1,5 @@
1
1
  export type { ShardKind, ShardMessage } from './envelope/index.ts'
2
+ export { Gateway } from './gateway/index.ts'
2
3
  export { ShardLink } from './link/index.ts'
3
4
  export type {
4
5
  ShardLinkOptions,