serverless-ircd 0.4.0 → 0.6.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 (243) hide show
  1. package/.github/workflows/ci.yml +34 -2
  2. package/.github/workflows/deploy-aws.yml +1 -2
  3. package/.github/workflows/deploy-cf-tcp.yml +1 -5
  4. package/.github/workflows/deploy-cf.yml +1 -2
  5. package/CHANGELOG.md +435 -0
  6. package/README.md +206 -27
  7. package/apps/aws-stack/README.md +37 -3
  8. package/apps/aws-stack/package.json +1 -1
  9. package/apps/aws-stack/src/aws-stack.ts +106 -13
  10. package/apps/aws-stack/tests/coverage-command.test.ts +155 -0
  11. package/apps/aws-stack/tests/stack.test.ts +49 -3
  12. package/apps/aws-stack/tests/synth-no-bundle.test.ts +129 -0
  13. package/apps/aws-stack/tests/vitest.setup.ts +18 -0
  14. package/apps/aws-stack/vitest.config.ts +3 -0
  15. package/apps/cf-tcp-container/package.json +1 -1
  16. package/apps/cf-tcp-container/src/config-loader.ts +6 -7
  17. package/apps/cf-tcp-container/src/container-server.ts +3 -0
  18. package/apps/cf-tcp-container/tests/config-loader.test.ts +10 -3
  19. package/apps/cf-tcp-container/wrangler.toml +1 -1
  20. package/apps/cf-worker/package.json +2 -1
  21. package/apps/cf-worker/vitest.config.ts +1 -1
  22. package/apps/cf-worker/wrangler.toml +5 -3
  23. package/apps/local-cli/package.json +1 -1
  24. package/apps/local-cli/src/config-loader.ts +6 -6
  25. package/apps/local-cli/src/main.ts +20 -0
  26. package/apps/local-cli/src/server.ts +138 -28
  27. package/apps/local-cli/tests/e2e.test.ts +113 -29
  28. package/apps/local-cli/tests/tcp.test.ts +71 -19
  29. package/apps/local-cli/tests/ws-subprotocol.test.ts +262 -0
  30. package/biome.json +1 -0
  31. package/docs/ADR-001-pure-reducers-and-effect-system.md +74 -0
  32. package/docs/ADR-002-location-of-authority.md +82 -0
  33. package/docs/ADR-003-durable-object-sharding.md +110 -0
  34. package/docs/ADR-004-dynamodb-schema.md +96 -0
  35. package/docs/ADR-005-wss-only-transport-v1.md +83 -0
  36. package/docs/ADR-006-sasl-mechanism-scope.md +96 -0
  37. package/docs/ADR-007-deterministic-ports.md +82 -0
  38. package/docs/ADR-008-monorepo-tooling.md +60 -0
  39. package/docs/ADR-009-dual-transport-wss-and-irc-tls.md +138 -0
  40. package/docs/ADR-Index.md +15 -0
  41. package/docs/AWS-Adapter-Architecture.md +496 -0
  42. package/docs/AWS-Deployment.md +1275 -0
  43. package/docs/AWS-TCP-Deployment.md +410 -0
  44. package/docs/Cloudflare-Deployment-Guide.md +719 -0
  45. package/docs/Cloudflare-TCP-Deployment.md +510 -0
  46. package/docs/Home.md +15 -0
  47. package/docs/Observability.md +87 -0
  48. package/docs/PlanExtensions.md +812 -0
  49. package/docs/PlanIRCv3Websocket.md +503 -0
  50. package/docs/PlanWebClient.md +451 -0
  51. package/docs/Release-Process.md +443 -0
  52. package/package.json +2 -2
  53. package/packages/aws-adapter/package.json +1 -1
  54. package/packages/aws-adapter/src/aws-runtime.ts +69 -0
  55. package/packages/aws-adapter/src/handlers/connect.ts +36 -5
  56. package/packages/aws-adapter/src/handlers/default.ts +66 -5
  57. package/packages/aws-adapter/src/handlers/index.ts +41 -2
  58. package/packages/aws-adapter/src/handlers/nlb-stream.ts +18 -0
  59. package/packages/aws-adapter/src/index.ts +2 -0
  60. package/packages/aws-adapter/src/serialize.ts +40 -2
  61. package/packages/aws-adapter/src/stats.ts +80 -0
  62. package/packages/aws-adapter/tests/account-store-dynamo.test.ts +49 -8
  63. package/packages/aws-adapter/tests/account-store.test.ts +4 -7
  64. package/packages/aws-adapter/tests/aws-harness.ts +5 -2
  65. package/packages/aws-adapter/tests/aws-integration.test.ts +8 -4
  66. package/packages/aws-adapter/tests/aws-runtime.test.ts +140 -0
  67. package/packages/aws-adapter/tests/config-loader.test.ts +11 -2
  68. package/packages/aws-adapter/tests/connect.test.ts +100 -4
  69. package/packages/aws-adapter/tests/disconnect-fanout.test.ts +3 -4
  70. package/packages/aws-adapter/tests/global-setup.ts +55 -30
  71. package/packages/aws-adapter/tests/gone-exception.test.ts +4 -9
  72. package/packages/aws-adapter/tests/handlers.test.ts +238 -4
  73. package/packages/aws-adapter/tests/message-store.test.ts +6 -5
  74. package/packages/aws-adapter/tests/nlb-stream.test.ts +62 -8
  75. package/packages/aws-adapter/tests/stats.test.ts +317 -0
  76. package/packages/cf-adapter/package.json +6 -1
  77. package/packages/cf-adapter/src/cf-runtime.ts +66 -1
  78. package/packages/cf-adapter/src/channel-do.ts +2 -2
  79. package/packages/cf-adapter/src/connection-do.ts +185 -54
  80. package/packages/cf-adapter/src/env.ts +25 -6
  81. package/packages/cf-adapter/src/index.ts +2 -0
  82. package/packages/cf-adapter/src/registry-do.ts +22 -3
  83. package/packages/cf-adapter/src/serialize.ts +25 -4
  84. package/packages/cf-adapter/src/sharding.ts +1 -2
  85. package/packages/cf-adapter/src/stats.ts +65 -0
  86. package/packages/cf-adapter/tests/cf-harness.ts +1 -1
  87. package/packages/cf-adapter/tests/cf-integration.test.ts +4 -4
  88. package/packages/cf-adapter/tests/cf-runtime.test.ts +307 -2
  89. package/packages/cf-adapter/tests/channel-do.test.ts +119 -2
  90. package/packages/cf-adapter/tests/config-loader.test.ts +49 -0
  91. package/packages/cf-adapter/tests/connection-do-channel-registration.test.ts +2 -2
  92. package/packages/cf-adapter/tests/connection-do-no-batching-reservation.test.ts +2 -2
  93. package/packages/cf-adapter/tests/connection-do-pure.test.ts +91 -0
  94. package/packages/cf-adapter/tests/connection-do-ws-spec-contract.test.ts +289 -0
  95. package/packages/cf-adapter/tests/connection-do-ws-subprotocol.test.ts +184 -0
  96. package/packages/cf-adapter/tests/connection-do.test.ts +27 -2
  97. package/packages/cf-adapter/tests/d1-account-store.test.ts +48 -0
  98. package/packages/cf-adapter/tests/registry-do.test.ts +108 -4
  99. package/packages/cf-adapter/tests/serialize.test.ts +308 -0
  100. package/packages/cf-adapter/tests/sharding.test.ts +1 -1
  101. package/packages/cf-adapter/tests/stats.test.ts +120 -0
  102. package/packages/cf-adapter/tests/worker/main.ts +7 -7
  103. package/packages/cf-adapter/tests/worker/stubs/channel-stub.ts +2 -2
  104. package/packages/cf-adapter/tests/worker/stubs/registry-stub.ts +8 -2
  105. package/packages/cf-adapter/vitest.config.ts +1 -1
  106. package/packages/cf-adapter/wrangler.test.toml +7 -0
  107. package/packages/in-memory-runtime/package.json +1 -1
  108. package/packages/in-memory-runtime/src/in-memory-runtime.ts +39 -0
  109. package/packages/in-memory-runtime/tests/in-memory-runtime.test.ts +259 -0
  110. package/packages/irc-core/package.json +1 -1
  111. package/packages/irc-core/src/admission.ts +16 -15
  112. package/packages/irc-core/src/caps/capabilities.ts +38 -4
  113. package/packages/irc-core/src/caps/index.ts +1 -0
  114. package/packages/irc-core/src/caps/sts.ts +84 -0
  115. package/packages/irc-core/src/commands/account-notify.ts +53 -0
  116. package/packages/irc-core/src/commands/away.ts +9 -3
  117. package/packages/irc-core/src/commands/cap.ts +23 -5
  118. package/packages/irc-core/src/commands/chathistory.ts +40 -11
  119. package/packages/irc-core/src/commands/index.ts +42 -0
  120. package/packages/irc-core/src/commands/invite.ts +2 -4
  121. package/packages/irc-core/src/commands/isupport.ts +59 -2
  122. package/packages/irc-core/src/commands/kick.ts +2 -4
  123. package/packages/irc-core/src/commands/kill.ts +127 -0
  124. package/packages/irc-core/src/commands/list.ts +1 -1
  125. package/packages/irc-core/src/commands/lusers.ts +204 -0
  126. package/packages/irc-core/src/commands/mode.ts +12 -9
  127. package/packages/irc-core/src/commands/monitor.ts +327 -0
  128. package/packages/irc-core/src/commands/multiline.ts +256 -0
  129. package/packages/irc-core/src/commands/names.ts +3 -5
  130. package/packages/irc-core/src/commands/part.ts +2 -4
  131. package/packages/irc-core/src/commands/pre-away.ts +112 -0
  132. package/packages/irc-core/src/commands/privmsg.ts +19 -12
  133. package/packages/irc-core/src/commands/read-marker.ts +108 -0
  134. package/packages/irc-core/src/commands/registration.ts +8 -0
  135. package/packages/irc-core/src/commands/rehash.ts +119 -0
  136. package/packages/irc-core/src/commands/sasl.ts +24 -1
  137. package/packages/irc-core/src/commands/setname.ts +109 -0
  138. package/packages/irc-core/src/commands/stats.ts +152 -0
  139. package/packages/irc-core/src/commands/tagmsg.ts +71 -1
  140. package/packages/irc-core/src/commands/topic.ts +2 -4
  141. package/packages/irc-core/src/commands/trace.ts +137 -0
  142. package/packages/irc-core/src/commands/wallops.ts +118 -0
  143. package/packages/irc-core/src/commands/whois.ts +5 -0
  144. package/packages/irc-core/src/config.ts +72 -10
  145. package/packages/irc-core/src/effects.ts +41 -1
  146. package/packages/irc-core/src/index.ts +2 -0
  147. package/packages/irc-core/src/ports.ts +568 -0
  148. package/packages/irc-core/src/protocol/index.ts +14 -0
  149. package/packages/irc-core/src/protocol/labeled-response.ts +133 -0
  150. package/packages/irc-core/src/protocol/numerics.ts +57 -11
  151. package/packages/irc-core/src/protocol/outbound.ts +36 -4
  152. package/packages/irc-core/src/protocol/standard-replies.ts +277 -0
  153. package/packages/irc-core/src/state/connection.ts +32 -1
  154. package/packages/irc-core/src/types.ts +120 -1
  155. package/packages/irc-core/src/ws-framing.ts +132 -0
  156. package/packages/irc-core/src/ws-subprotocol.ts +66 -0
  157. package/packages/irc-core/stryker.commands.conf.json +1 -2
  158. package/packages/irc-core/tests/admission.test.ts +18 -0
  159. package/packages/irc-core/tests/away-store.test.ts +73 -0
  160. package/packages/irc-core/tests/caps/capabilities.test.ts +119 -2
  161. package/packages/irc-core/tests/caps/sts.test.ts +123 -0
  162. package/packages/irc-core/tests/commands/account-notify.test.ts +171 -0
  163. package/packages/irc-core/tests/commands/away.test.ts +85 -2
  164. package/packages/irc-core/tests/commands/cap.test.ts +165 -0
  165. package/packages/irc-core/tests/commands/chathistory.test.ts +148 -1
  166. package/packages/irc-core/tests/commands/echo-message.test.ts +9 -3
  167. package/packages/irc-core/tests/commands/isupport.test.ts +80 -0
  168. package/packages/irc-core/tests/commands/kill.test.ts +243 -0
  169. package/packages/irc-core/tests/commands/lusers.test.ts +368 -0
  170. package/packages/irc-core/tests/commands/mode.test.ts +129 -0
  171. package/packages/irc-core/tests/commands/monitor.test.ts +411 -0
  172. package/packages/irc-core/tests/commands/multiline.test.ts +428 -0
  173. package/packages/irc-core/tests/commands/pre-away.test.ts +241 -0
  174. package/packages/irc-core/tests/commands/privmsg.test.ts +154 -3
  175. package/packages/irc-core/tests/commands/read-marker.test.ts +211 -0
  176. package/packages/irc-core/tests/commands/registration.test.ts +64 -0
  177. package/packages/irc-core/tests/commands/rehash.test.ts +171 -0
  178. package/packages/irc-core/tests/commands/sasl.test.ts +116 -2
  179. package/packages/irc-core/tests/commands/setname.test.ts +225 -0
  180. package/packages/irc-core/tests/commands/stats.test.ts +294 -0
  181. package/packages/irc-core/tests/commands/tagmsg.test.ts +357 -0
  182. package/packages/irc-core/tests/commands/trace.test.ts +282 -0
  183. package/packages/irc-core/tests/commands/wallops.test.ts +231 -0
  184. package/packages/irc-core/tests/commands/whois.test.ts +34 -0
  185. package/packages/irc-core/tests/config.test.ts +103 -13
  186. package/packages/irc-core/tests/dropped-s2s-and-obsolete-verbs.test.ts +90 -0
  187. package/packages/irc-core/tests/effects.test.ts +14 -0
  188. package/packages/irc-core/tests/labeled-response.test.ts +181 -0
  189. package/packages/irc-core/tests/message-tags.test.ts +114 -0
  190. package/packages/irc-core/tests/monitor-store.test.ts +200 -0
  191. package/packages/irc-core/tests/numerics.test.ts +90 -0
  192. package/packages/irc-core/tests/outbound.test.ts +51 -0
  193. package/packages/irc-core/tests/ports.test.ts +22 -0
  194. package/packages/irc-core/tests/raw-modules.d.ts +11 -0
  195. package/packages/irc-core/tests/read-marker-store.test.ts +108 -0
  196. package/packages/irc-core/tests/standard-replies.test.ts +412 -0
  197. package/packages/irc-core/tests/stats-store.test.ts +222 -0
  198. package/packages/irc-core/tests/types.test.ts +27 -0
  199. package/packages/irc-core/tests/ws-framing.test.ts +213 -0
  200. package/packages/irc-core/tests/ws-subprotocol.test.ts +111 -0
  201. package/packages/irc-server/package.json +1 -1
  202. package/packages/irc-server/src/actor.ts +699 -19
  203. package/packages/irc-server/src/dispatch.ts +109 -16
  204. package/packages/irc-server/src/routing.ts +3 -0
  205. package/packages/irc-server/src/runtime.ts +31 -0
  206. package/packages/irc-server/src/transport.ts +10 -7
  207. package/packages/irc-server/tests/actor.test.ts +2523 -42
  208. package/packages/irc-server/tests/dispatch.test.ts +300 -2
  209. package/packages/irc-server/tests/raw-modules.d.ts +11 -0
  210. package/packages/irc-server/tests/routing.test.ts +1 -0
  211. package/packages/irc-server/tests/runtime.test.ts +7 -0
  212. package/packages/irc-test-support/package.json +1 -1
  213. package/packages/irc-test-support/src/index.ts +6 -0
  214. package/packages/irc-test-support/src/scenarios.ts +9 -1
  215. package/packages/irc-test-support/src/test-config.ts +54 -0
  216. package/packages/irc-test-support/tests/in-memory-scenarios.test.ts +1 -1
  217. package/packages/irc-test-support/tests/test-config.test.ts +51 -0
  218. package/pnpm-workspace.yaml +1 -0
  219. package/tools/ci-hardening/package.json +1 -1
  220. package/tools/ci-hardening/tests/ddb-local.test.ts +82 -0
  221. package/tools/load-test/package.json +33 -0
  222. package/tools/load-test/src/client.ts +351 -0
  223. package/tools/load-test/src/config.ts +313 -0
  224. package/tools/load-test/src/harness.ts +116 -0
  225. package/tools/load-test/src/main.ts +120 -0
  226. package/tools/load-test/src/metrics.ts +168 -0
  227. package/tools/load-test/src/report.ts +106 -0
  228. package/tools/load-test/tests/client.test.ts +212 -0
  229. package/tools/load-test/tests/config.test.ts +152 -0
  230. package/tools/load-test/tests/framing.test.ts +37 -0
  231. package/tools/load-test/tests/harness.test.ts +165 -0
  232. package/tools/load-test/tests/metrics.test.ts +174 -0
  233. package/tools/load-test/tests/report.test.ts +161 -0
  234. package/tools/load-test/tests/smoke.test.ts +67 -0
  235. package/tools/load-test/tsconfig.build.json +12 -0
  236. package/tools/load-test/tsconfig.test.json +10 -0
  237. package/tools/load-test/vitest.config.ts +29 -0
  238. package/tools/package.json +6 -1
  239. package/tools/seed-cf-accounts.ts +4 -1
  240. package/tools/tcp-ws-forwarder/package.json +1 -1
  241. package/tools/tcp-ws-forwarder/src/forwarder.ts +57 -9
  242. package/tools/tcp-ws-forwarder/tests/forwarder.test.ts +34 -1
  243. package/tools/tcp-ws-forwarder/tests/framing.test.ts +65 -1
@@ -0,0 +1,222 @@
1
+ import { describe, expect, it } from 'vitest';
2
+ import {
3
+ FakeClock,
4
+ InMemoryStats,
5
+ type ServerStats,
6
+ type ServerStatsSnapshot,
7
+ computeStatsSnapshot,
8
+ } from '../src/ports';
9
+ import type { ChannelState } from '../src/state/channel';
10
+ import { createChannel } from '../src/state/channel';
11
+ import type { ConnId, ConnectionState } from '../src/state/connection';
12
+ import { createConnection } from '../src/state/connection';
13
+
14
+ /** Builds a registered connection with the supplied modes. */
15
+ function makeConn(
16
+ id: ConnId,
17
+ opts: { oper?: boolean; invisible?: boolean; registered?: boolean } = {},
18
+ ): ConnectionState {
19
+ const s = createConnection({ id, connectedSince: 0 });
20
+ s.nick = id;
21
+ s.user = id;
22
+ s.host = 'example.com';
23
+ s.realname = id;
24
+ s.registration = opts.registered === false ? 'pre-registration' : 'registered';
25
+ if (opts.oper !== undefined) s.userModes.oper = opts.oper;
26
+ if (opts.invisible !== undefined) s.userModes.invisible = opts.invisible;
27
+ return s;
28
+ }
29
+
30
+ function emptyChannel(name: string): ChannelState {
31
+ return createChannel({ name, nameLower: name.toLowerCase(), createdAt: 0 });
32
+ }
33
+
34
+ describe('InMemoryStats', () => {
35
+ it('implements the ServerStats port', () => {
36
+ const stats: ServerStats = new InMemoryStats({
37
+ connections: new Map(),
38
+ channels: new Map(),
39
+ uptimeStartedAt: 0,
40
+ });
41
+ expect(stats).toBeDefined();
42
+ });
43
+
44
+ it('returns zero counts for an empty deployment', async () => {
45
+ const stats = new InMemoryStats({
46
+ connections: new Map(),
47
+ channels: new Map(),
48
+ uptimeStartedAt: 0,
49
+ });
50
+
51
+ const snap = await stats.getStats();
52
+
53
+ expect(snap).toEqual<ServerStatsSnapshot>({
54
+ users: 0,
55
+ invisible: 0,
56
+ opers: 0,
57
+ unknownConnections: 0,
58
+ channels: 0,
59
+ servers: 1,
60
+ localConns: 0,
61
+ globalConns: 0,
62
+ uptimeStartedAt: 0,
63
+ maxLocalConns: 0,
64
+ maxGlobalConns: 0,
65
+ });
66
+ });
67
+
68
+ it('derives the seed counts from the maps (3 conns, 1 oper, 2 channels)', async () => {
69
+ const conns = new Map<ConnId, ConnectionState>([
70
+ ['c1', makeConn('c1')],
71
+ ['c2', makeConn('c2', { invisible: true })],
72
+ ['c3', makeConn('c3', { oper: true })],
73
+ ]);
74
+ const channels = new Map<string, ChannelState>([
75
+ ['#a', emptyChannel('#a')],
76
+ ['#b', emptyChannel('#b')],
77
+ ]);
78
+ const stats = new InMemoryStats({
79
+ connections: conns,
80
+ channels,
81
+ uptimeStartedAt: 1_000,
82
+ });
83
+
84
+ const snap = await stats.getStats();
85
+
86
+ // 3 registered (2 visible + 1 invisible), 1 oper, 0 unknown, 2 channels.
87
+ expect(snap.users).toBe(2); // visible registered
88
+ expect(snap.invisible).toBe(1);
89
+ expect(snap.opers).toBe(1);
90
+ expect(snap.unknownConnections).toBe(0);
91
+ expect(snap.channels).toBe(2);
92
+ expect(snap.servers).toBe(1);
93
+ expect(snap.localConns).toBe(3);
94
+ expect(snap.globalConns).toBe(3);
95
+ expect(snap.uptimeStartedAt).toBe(1_000);
96
+ });
97
+
98
+ it('counts pre-registration connections as unknown', async () => {
99
+ const conns = new Map<ConnId, ConnectionState>([
100
+ ['c1', makeConn('c1')],
101
+ ['c2', makeConn('c2', { registered: false })],
102
+ ]);
103
+ const stats = new InMemoryStats({
104
+ connections: conns,
105
+ channels: new Map(),
106
+ uptimeStartedAt: 0,
107
+ });
108
+
109
+ const snap = await stats.getStats();
110
+
111
+ expect(snap.users).toBe(1);
112
+ expect(snap.unknownConnections).toBe(1);
113
+ expect(snap.localConns).toBe(2);
114
+ });
115
+
116
+ it('reports the live max as equal to current count when never higher', async () => {
117
+ const conns = new Map<ConnId, ConnectionState>([['c1', makeConn('c1')]]);
118
+ const stats = new InMemoryStats({
119
+ connections: conns,
120
+ channels: new Map(),
121
+ uptimeStartedAt: 0,
122
+ maxLocalConns: 5,
123
+ maxGlobalConns: 7,
124
+ });
125
+
126
+ const snap = await stats.getStats();
127
+
128
+ expect(snap.maxLocalConns).toBe(5);
129
+ expect(snap.maxGlobalConns).toBe(7);
130
+ });
131
+
132
+ it('clamps max to the current count when the live count exceeds the recorded max', async () => {
133
+ const conns = new Map<ConnId, ConnectionState>([
134
+ ['c1', makeConn('c1')],
135
+ ['c2', makeConn('c2')],
136
+ ['c3', makeConn('c3')],
137
+ ]);
138
+ const stats = new InMemoryStats({
139
+ connections: conns,
140
+ channels: new Map(),
141
+ uptimeStartedAt: 0,
142
+ maxLocalConns: 2,
143
+ maxGlobalConns: 2,
144
+ });
145
+
146
+ const snap = await stats.getStats();
147
+
148
+ // Live count (3) > seeded max (2); the snapshot reports the live value
149
+ // so the wire numeric never claims a max below the current count.
150
+ expect(snap.maxLocalConns).toBe(3);
151
+ expect(snap.maxGlobalConns).toBe(3);
152
+ });
153
+
154
+ it('reflects map mutations on subsequent getStats() calls', async () => {
155
+ const conns = new Map<ConnId, ConnectionState>([['c1', makeConn('c1')]]);
156
+ const stats = new InMemoryStats({
157
+ connections: conns,
158
+ channels: new Map(),
159
+ uptimeStartedAt: 0,
160
+ });
161
+
162
+ let snap = await stats.getStats();
163
+ expect(snap.users).toBe(1);
164
+
165
+ conns.set('c2', makeConn('c2'));
166
+ conns.set('c3', makeConn('c3', { invisible: true }));
167
+ snap = await stats.getStats();
168
+ expect(snap.users).toBe(2);
169
+ expect(snap.invisible).toBe(1);
170
+ expect(snap.localConns).toBe(3);
171
+ });
172
+ });
173
+
174
+ describe('computeStatsSnapshot', () => {
175
+ it('counts opers separately from users (an oper still counts as a user)', () => {
176
+ const snap = computeStatsSnapshot([makeConn('c1'), makeConn('c2', { oper: true })], 0, 0);
177
+ expect(snap.users).toBe(2);
178
+ expect(snap.opers).toBe(1);
179
+ });
180
+
181
+ it('counts an oper who is also invisible under invisible + opers (not users)', () => {
182
+ const snap = computeStatsSnapshot([makeConn('c1', { oper: true, invisible: true })], 0, 0);
183
+ expect(snap.invisible).toBe(1);
184
+ expect(snap.opers).toBe(1);
185
+ // users = visible registered (total registered − invisible).
186
+ expect(snap.users).toBe(0);
187
+ });
188
+
189
+ it('reports users as the visible-only count (total registered − invisible)', () => {
190
+ const snap = computeStatsSnapshot(
191
+ [
192
+ makeConn('c1'),
193
+ makeConn('c2'),
194
+ makeConn('c3', { invisible: true }),
195
+ makeConn('c4', { invisible: true }),
196
+ ],
197
+ 0,
198
+ 0,
199
+ );
200
+ expect(snap.users).toBe(2);
201
+ expect(snap.invisible).toBe(2);
202
+ expect(snap.localConns).toBe(4);
203
+ });
204
+
205
+ it('defaults maxConns to the live count when seed omitted', () => {
206
+ const snap = computeStatsSnapshot([makeConn('c1'), makeConn('c2')], 0, 0);
207
+ expect(snap.maxLocalConns).toBe(2);
208
+ expect(snap.maxGlobalConns).toBe(2);
209
+ });
210
+
211
+ it('uses the FakeClock only for uptime (not in the snapshot)', async () => {
212
+ const clock = new FakeClock(99_000);
213
+ const stats = new InMemoryStats({
214
+ connections: new Map(),
215
+ channels: new Map(),
216
+ uptimeStartedAt: 1_000,
217
+ clock,
218
+ });
219
+ const snap = await stats.getStats();
220
+ expect(snap.uptimeStartedAt).toBe(1_000);
221
+ });
222
+ });
@@ -131,6 +131,33 @@ describe('Ctx / Reducer types', () => {
131
131
  const ctx = makeCtx();
132
132
  expect(ctx.history).toBeUndefined();
133
133
  });
134
+
135
+ it('threads an inbound label through buildCtx onto ctx.label', () => {
136
+ const ctx = buildCtx({
137
+ serverConfig: {
138
+ serverName: 'irc.example.com',
139
+ networkName: 'ExampleNet',
140
+ maxChannelsPerUser: 30,
141
+ maxTargetsPerCommand: 10,
142
+ maxListEntries: 50,
143
+ nickLen: 30,
144
+ channelLen: 50,
145
+ topicLen: 390,
146
+ quitMessage: 'Client Quit',
147
+ },
148
+ clock: new FakeClock(1_000),
149
+ ids: new SequentialIdFactory(),
150
+ motd: EmptyMotdProvider,
151
+ connection: createConnection({ id: 'c1', connectedSince: 0 }),
152
+ label: 'foo',
153
+ });
154
+ expect(ctx.label).toBe('foo');
155
+ });
156
+
157
+ it('omits ctx.label when no label is supplied', () => {
158
+ const ctx = makeCtx();
159
+ expect(ctx.label).toBeUndefined();
160
+ });
134
161
  });
135
162
 
136
163
  describe('ServerConfig shape', () => {
@@ -0,0 +1,213 @@
1
+ import { describe, expect, it } from 'vitest';
2
+ import {
3
+ MAX_WS_MESSAGE_BYTES,
4
+ type WsFrameMode,
5
+ frameToLines,
6
+ isWithinWsByteBudget,
7
+ sanitizeForTextMode,
8
+ stripTrailingCrLf,
9
+ wsFrameModeFor,
10
+ } from '../src/ws-framing';
11
+
12
+ // ============================================================================
13
+ // MAX_WS_MESSAGE_BYTES constant
14
+ // ============================================================================
15
+
16
+ describe('MAX_WS_MESSAGE_BYTES', () => {
17
+ it('is 510 (the IRC line-content limit; WS spec omits the trailing CRLF)', () => {
18
+ expect(MAX_WS_MESSAGE_BYTES).toBe(510);
19
+ });
20
+ });
21
+
22
+ // ============================================================================
23
+ // wsFrameModeFor — subprotocol → WsFrameMode mapping
24
+ // ============================================================================
25
+
26
+ describe('wsFrameModeFor', () => {
27
+ it('maps text.ircv3.net to spec-text', () => {
28
+ expect(wsFrameModeFor('text.ircv3.net')).toBe('spec-text');
29
+ });
30
+
31
+ it('maps binary.ircv3.net to spec-binary', () => {
32
+ expect(wsFrameModeFor('binary.ircv3.net')).toBe('spec-binary');
33
+ });
34
+
35
+ it('maps undefined (no subprotocol) to legacy', () => {
36
+ expect(wsFrameModeFor(undefined)).toBe('legacy');
37
+ });
38
+ });
39
+
40
+ // ============================================================================
41
+ // stripTrailingCrLf
42
+ // ============================================================================
43
+
44
+ describe('stripTrailingCrLf', () => {
45
+ it('strips a trailing CR-LF', () => {
46
+ expect(stripTrailingCrLf('NICK bob\r\n')).toBe('NICK bob');
47
+ });
48
+
49
+ it('strips a trailing bare LF', () => {
50
+ expect(stripTrailingCrLf('NICK bob\n')).toBe('NICK bob');
51
+ });
52
+
53
+ it('leaves a string with no trailing terminator unchanged', () => {
54
+ expect(stripTrailingCrLf('NICK bob')).toBe('NICK bob');
55
+ });
56
+
57
+ it('strips only a single trailing terminator', () => {
58
+ expect(stripTrailingCrLf('a\r\n\r\n')).toBe('a\r\n');
59
+ });
60
+
61
+ it('returns the empty string unchanged', () => {
62
+ expect(stripTrailingCrLf('')).toBe('');
63
+ });
64
+
65
+ it('does not strip a leading CR-LF', () => {
66
+ expect(stripTrailingCrLf('\r\nNICK bob')).toBe('\r\nNICK bob');
67
+ });
68
+ });
69
+
70
+ // ============================================================================
71
+ // frameToLines — spec-text / spec-binary modes (one IRC message per frame)
72
+ // ============================================================================
73
+
74
+ describe('frameToLines — spec-text', () => {
75
+ it('strips a trailing CR-LF and returns a single-element array', () => {
76
+ expect(frameToLines('NICK bob\r\n', 'spec-text')).toEqual(['NICK bob']);
77
+ });
78
+
79
+ it('strips a trailing bare LF', () => {
80
+ expect(frameToLines('NICK bob\n', 'spec-text')).toEqual(['NICK bob']);
81
+ });
82
+
83
+ it('does not split on an embedded LF (one WS message === one IRC line)', () => {
84
+ expect(frameToLines('NICK bob\nextra', 'spec-text')).toEqual(['NICK bob\nextra']);
85
+ });
86
+
87
+ it('returns an empty array for an empty frame', () => {
88
+ expect(frameToLines('', 'spec-text')).toEqual([]);
89
+ });
90
+
91
+ it('returns an empty array for a frame that is only a terminator', () => {
92
+ expect(frameToLines('\r\n', 'spec-text')).toEqual([]);
93
+ });
94
+
95
+ it('returns a single bare line unchanged when there is no terminator', () => {
96
+ expect(frameToLines('PING :tok', 'spec-text')).toEqual(['PING :tok']);
97
+ });
98
+ });
99
+
100
+ describe('frameToLines — spec-binary', () => {
101
+ it('behaves identically to spec-text (single line, terminator stripped)', () => {
102
+ expect(frameToLines('NICK bob\n', 'spec-binary')).toEqual(['NICK bob']);
103
+ });
104
+
105
+ it('does not split on an embedded LF', () => {
106
+ expect(frameToLines('a\nb', 'spec-binary')).toEqual(['a\nb']);
107
+ });
108
+ });
109
+
110
+ // ============================================================================
111
+ // frameToLines — legacy mode (the pre-spec split-on-/\r?\n/ behaviour)
112
+ // ============================================================================
113
+
114
+ describe('frameToLines — legacy', () => {
115
+ it('splits on CR-LF into separate lines', () => {
116
+ expect(frameToLines('a\r\nb', 'legacy')).toEqual(['a', 'b']);
117
+ });
118
+
119
+ it('splits on bare LF', () => {
120
+ expect(frameToLines('a\nb', 'legacy')).toEqual(['a', 'b']);
121
+ });
122
+
123
+ it('returns an empty array for the empty string', () => {
124
+ expect(frameToLines('', 'legacy')).toEqual([]);
125
+ });
126
+
127
+ it('returns a single bare line with no terminator unchanged', () => {
128
+ expect(frameToLines('PING :tok', 'legacy')).toEqual(['PING :tok']);
129
+ });
130
+
131
+ it('emits a trailing empty string for a terminated frame', () => {
132
+ expect(frameToLines('PING :tok\r\n', 'legacy')).toEqual(['PING :tok', '']);
133
+ });
134
+
135
+ it('splits several joined messages keeping a trailing empty string', () => {
136
+ expect(frameToLines('a\r\nb\r\n', 'legacy')).toEqual(['a', 'b', '']);
137
+ });
138
+ });
139
+
140
+ // Exhaustiveness guard: every declared mode is covered by the specs above.
141
+ describe('frameToLines — mode coverage', () => {
142
+ const modes: WsFrameMode[] = ['spec-text', 'spec-binary', 'legacy'];
143
+ for (const mode of modes) {
144
+ it(`accepts the '${mode}' mode without throwing`, () => {
145
+ expect(Array.isArray(frameToLines('x', mode))).toBe(true);
146
+ });
147
+ }
148
+ });
149
+
150
+ // ============================================================================
151
+ // isWithinWsByteBudget — the IRCv3 WS message byte cap
152
+ // ============================================================================
153
+
154
+ describe('isWithinWsByteBudget', () => {
155
+ it('accepts a frame whose length equals the 510-byte budget', () => {
156
+ expect(isWithinWsByteBudget('a'.repeat(510))).toBe(true);
157
+ });
158
+
159
+ it('rejects a frame whose length is one byte over the budget', () => {
160
+ expect(isWithinWsByteBudget('a'.repeat(511))).toBe(false);
161
+ });
162
+
163
+ it('accepts the empty string', () => {
164
+ expect(isWithinWsByteBudget('')).toBe(true);
165
+ });
166
+
167
+ it('accepts a short frame', () => {
168
+ expect(isWithinWsByteBudget('NICK bob')).toBe(true);
169
+ });
170
+ });
171
+
172
+ // ============================================================================
173
+ // sanitizeForTextMode — replace lone surrogates with U+FFFD
174
+ // ============================================================================
175
+
176
+ describe('sanitizeForTextMode', () => {
177
+ it('replaces a lone high surrogate with U+FFFD', () => {
178
+ expect(sanitizeForTextMode('\uD800')).toBe('\uFFFD');
179
+ });
180
+
181
+ it('replaces a lone low surrogate with U+FFFD', () => {
182
+ expect(sanitizeForTextMode('\uDC00')).toBe('\uFFFD');
183
+ });
184
+
185
+ it('leaves a valid surrogate pair (a supplementary code point) unchanged', () => {
186
+ // U+10000 = \uD800\uDC00
187
+ expect(sanitizeForTextMode('\uD800\uDC00')).toBe('\uD800\uDC00');
188
+ });
189
+
190
+ it('leaves plain ASCII text unchanged', () => {
191
+ expect(sanitizeForTextMode('hello')).toBe('hello');
192
+ });
193
+
194
+ it('replaces a lone surrogate embedded in normal text', () => {
195
+ expect(sanitizeForTextMode('a\uD800b')).toBe('a\uFFFDb');
196
+ });
197
+
198
+ it('replaces two consecutive lone high surrogates with two U+FFFD', () => {
199
+ expect(sanitizeForTextMode('\uD800\uD800')).toBe('\uFFFD\uFFFD');
200
+ });
201
+
202
+ it('preserves a valid pair surrounded by normal text', () => {
203
+ expect(sanitizeForTextMode('x\uD83D\uDE00y')).toBe('x\uD83D\uDE00y');
204
+ });
205
+
206
+ it('replaces a lone high surrogate followed later by a lone low surrogate', () => {
207
+ expect(sanitizeForTextMode('\uD800a\uDC00')).toBe('\uFFFDa\uFFFD');
208
+ });
209
+
210
+ it('returns the empty string unchanged', () => {
211
+ expect(sanitizeForTextMode('')).toBe('');
212
+ });
213
+ });
@@ -0,0 +1,111 @@
1
+ import { describe, expect, it } from 'vitest';
2
+ import {
3
+ WS_SUBPROTO_BINARY,
4
+ WS_SUBPROTO_TEXT,
5
+ type WsSubprotocol,
6
+ parseSecWsProtocolOffers,
7
+ selectSubprotocol,
8
+ } from '../src/ws-subprotocol';
9
+
10
+ // ============================================================================
11
+ // constants & type
12
+ // ============================================================================
13
+
14
+ describe('WS subprotocol constants', () => {
15
+ it('exposes the IRCv3 text subprotocol name', () => {
16
+ expect(WS_SUBPROTO_TEXT).toBe('text.ircv3.net');
17
+ });
18
+
19
+ it('exposes the IRCv3 binary subprotocol name', () => {
20
+ expect(WS_SUBPROTO_BINARY).toBe('binary.ircv3.net');
21
+ });
22
+
23
+ it('the WsSubprotocol type resolves to the two literal names', () => {
24
+ const a: WsSubprotocol = WS_SUBPROTO_TEXT;
25
+ const b: WsSubprotocol = WS_SUBPROTO_BINARY;
26
+ expect([a, b]).toEqual([WS_SUBPROTO_TEXT, WS_SUBPROTO_BINARY]);
27
+ });
28
+ });
29
+
30
+ // ============================================================================
31
+ // parseSecWsProtocolOffers — header parsing
32
+ // ============================================================================
33
+
34
+ describe('parseSecWsProtocolOffers', () => {
35
+ it('splits a comma-separated list into ordered offers', () => {
36
+ expect(parseSecWsProtocolOffers('text.ircv3.net, binary.ircv3.net')).toEqual([
37
+ 'text.ircv3.net',
38
+ 'binary.ircv3.net',
39
+ ]);
40
+ });
41
+
42
+ it('trims whitespace around each item', () => {
43
+ expect(parseSecWsProtocolOffers(' text.ircv3.net , binary.ircv3.net ')).toEqual([
44
+ 'text.ircv3.net',
45
+ 'binary.ircv3.net',
46
+ ]);
47
+ });
48
+
49
+ it('preserves client-preference order', () => {
50
+ expect(parseSecWsProtocolOffers('binary.ircv3.net, text.ircv3.net')).toEqual([
51
+ 'binary.ircv3.net',
52
+ 'text.ircv3.net',
53
+ ]);
54
+ });
55
+
56
+ it('returns an empty array for null', () => {
57
+ expect(parseSecWsProtocolOffers(null)).toEqual([]);
58
+ });
59
+
60
+ it('returns an empty array for undefined', () => {
61
+ expect(parseSecWsProtocolOffers(undefined)).toEqual([]);
62
+ });
63
+
64
+ it('returns an empty array for an empty string', () => {
65
+ expect(parseSecWsProtocolOffers('')).toEqual([]);
66
+ });
67
+
68
+ it('drops empty entries produced by stray commas', () => {
69
+ expect(parseSecWsProtocolOffers('text.ircv3.net,, ,binary.ircv3.net')).toEqual([
70
+ 'text.ircv3.net',
71
+ 'binary.ircv3.net',
72
+ ]);
73
+ });
74
+
75
+ it('preserves duplicate entries in order', () => {
76
+ expect(parseSecWsProtocolOffers('text.ircv3.net, text.ircv3.net')).toEqual([
77
+ 'text.ircv3.net',
78
+ 'text.ircv3.net',
79
+ ]);
80
+ });
81
+ });
82
+
83
+ // ============================================================================
84
+ // selectSubprotocol — preference-ordered selection
85
+ // ============================================================================
86
+
87
+ describe('selectSubprotocol', () => {
88
+ it('returns the first supported offer in client-preference order (text first)', () => {
89
+ expect(selectSubprotocol(['text.ircv3.net', 'binary.ircv3.net'])).toBe('text.ircv3.net');
90
+ });
91
+
92
+ it('honours client preference when binary is offered first', () => {
93
+ expect(selectSubprotocol(['binary.ircv3.net', 'text.ircv3.net'])).toBe('binary.ircv3.net');
94
+ });
95
+
96
+ it('returns null when only unsupported protocols are offered', () => {
97
+ expect(selectSubprotocol(['foo', 'bar'])).toBeNull();
98
+ });
99
+
100
+ it('returns null for an empty offer list', () => {
101
+ expect(selectSubprotocol([])).toBeNull();
102
+ });
103
+
104
+ it('skips unsupported offers and selects the first supported one', () => {
105
+ expect(selectSubprotocol(['foo', 'binary.ircv3.net'])).toBe('binary.ircv3.net');
106
+ });
107
+
108
+ it('returns the first occurrence for duplicate supported offers', () => {
109
+ expect(selectSubprotocol(['text.ircv3.net', 'text.ircv3.net'])).toBe('text.ircv3.net');
110
+ });
111
+ });
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@serverless-ircd/irc-server",
3
- "version": "0.4.0",
3
+ "version": "0.6.0",
4
4
  "private": true,
5
5
  "description": "Orchestration layer: IrcRuntime port, dispatch interpreter, ConnectionActor",
6
6
  "license": "BSD-3-Clause",