@teammy/server 0.2.9 → 0.2.11

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 (119) hide show
  1. package/dist/main.js +3379 -1395
  2. package/dist/web/assets/Assistant-DLyxXHVH.js +1 -0
  3. package/dist/web/assets/Computers-BNJeNZB6.css +1 -0
  4. package/dist/web/assets/Computers-Bnq9vurA.js +7 -0
  5. package/dist/web/assets/Conversation-BFT_YU_m.js +1 -0
  6. package/dist/web/assets/{Conversation-Dc460fhC.css → Conversation-BVNmvuHR.css} +1 -1
  7. package/dist/web/assets/{Dashboard-wDiUg0LB.css → Dashboard-ChX6TZDn.css} +1 -1
  8. package/dist/web/assets/Dashboard-zwfEddfm.js +1 -0
  9. package/dist/web/assets/Dropdown-BVXg1qjS.js +55 -0
  10. package/dist/web/assets/Files-DvZhUMI0.js +1 -0
  11. package/dist/web/assets/{Help-G99-LK4-.js → Help-D2wGnf7L.js} +187 -61
  12. package/dist/web/assets/HtmlPreview-C3railXk.js +1 -0
  13. package/dist/web/assets/{Markdown-BgEXVy4-.js → Markdown-DwIYP3W0.js} +4 -4
  14. package/dist/web/assets/MessageComposer-Ddu83TIm.js +1 -0
  15. package/dist/web/assets/MessageComposer-e3g3t5jX.css +1 -0
  16. package/dist/web/assets/ResourceLibrary-CwHQw2pF.js +6 -0
  17. package/dist/web/assets/TerminalPane-BrSy-0jI.css +1 -0
  18. package/dist/web/assets/TerminalPane-D6QTLh_P.js +22 -0
  19. package/dist/web/assets/WorkspaceTree-DPnUT5Zj.js +1 -0
  20. package/dist/web/assets/file-links-BcFN8P1K.js +1 -0
  21. package/dist/web/assets/index-4y82i1SF.css +1 -0
  22. package/dist/web/assets/index-DbOamGWI.js +10 -0
  23. package/dist/web/downloads/teammy-inbox-skill.zip +0 -0
  24. package/dist/web/downloads/teammy-skill.zip +0 -0
  25. package/dist/web/index.html +3 -2
  26. package/node_modules/@fastify/websocket/.gitattributes +2 -0
  27. package/node_modules/@fastify/websocket/.github/dependabot.yml +53 -0
  28. package/node_modules/@fastify/websocket/.github/workflows/ci.yml +33 -0
  29. package/node_modules/@fastify/websocket/.github/workflows/lock-threads.yml +19 -0
  30. package/node_modules/@fastify/websocket/.github/workflows/release.yml +24 -0
  31. package/node_modules/@fastify/websocket/LICENSE +21 -0
  32. package/node_modules/@fastify/websocket/README.md +393 -0
  33. package/node_modules/@fastify/websocket/eslint.config.js +6 -0
  34. package/node_modules/@fastify/websocket/index.js +276 -0
  35. package/node_modules/@fastify/websocket/package.json +79 -0
  36. package/node_modules/@fastify/websocket/test/base.test.js +785 -0
  37. package/node_modules/@fastify/websocket/test/hooks.test.js +414 -0
  38. package/node_modules/@fastify/websocket/test/inject.test.js +174 -0
  39. package/node_modules/@fastify/websocket/test/router.test.js +890 -0
  40. package/node_modules/@fastify/websocket/tsconfig.json +3 -0
  41. package/node_modules/@fastify/websocket/types/index.d.ts +128 -0
  42. package/node_modules/@fastify/websocket/types/index.tst.ts +261 -0
  43. package/node_modules/duplexify/.travis.yml +9 -0
  44. package/node_modules/duplexify/LICENSE +21 -0
  45. package/node_modules/duplexify/README.md +97 -0
  46. package/node_modules/duplexify/example.js +21 -0
  47. package/node_modules/duplexify/index.js +238 -0
  48. package/node_modules/duplexify/package.json +39 -0
  49. package/node_modules/duplexify/test.js +339 -0
  50. package/node_modules/end-of-stream/LICENSE +21 -0
  51. package/node_modules/end-of-stream/README.md +54 -0
  52. package/node_modules/end-of-stream/index.js +96 -0
  53. package/node_modules/end-of-stream/package.json +37 -0
  54. package/node_modules/once/LICENSE +15 -0
  55. package/node_modules/once/README.md +79 -0
  56. package/node_modules/once/once.js +42 -0
  57. package/node_modules/once/package.json +33 -0
  58. package/node_modules/readable-stream/CONTRIBUTING.md +38 -0
  59. package/node_modules/readable-stream/GOVERNANCE.md +136 -0
  60. package/node_modules/readable-stream/LICENSE +47 -0
  61. package/node_modules/readable-stream/README.md +106 -0
  62. package/node_modules/readable-stream/errors-browser.js +127 -0
  63. package/node_modules/readable-stream/errors.js +116 -0
  64. package/node_modules/readable-stream/experimentalWarning.js +17 -0
  65. package/node_modules/readable-stream/lib/_stream_duplex.js +126 -0
  66. package/node_modules/readable-stream/lib/_stream_passthrough.js +37 -0
  67. package/node_modules/readable-stream/lib/_stream_readable.js +1027 -0
  68. package/node_modules/readable-stream/lib/_stream_transform.js +190 -0
  69. package/node_modules/readable-stream/lib/_stream_writable.js +641 -0
  70. package/node_modules/readable-stream/lib/internal/streams/async_iterator.js +180 -0
  71. package/node_modules/readable-stream/lib/internal/streams/buffer_list.js +183 -0
  72. package/node_modules/readable-stream/lib/internal/streams/destroy.js +96 -0
  73. package/node_modules/readable-stream/lib/internal/streams/end-of-stream.js +86 -0
  74. package/node_modules/readable-stream/lib/internal/streams/from-browser.js +3 -0
  75. package/node_modules/readable-stream/lib/internal/streams/from.js +52 -0
  76. package/node_modules/readable-stream/lib/internal/streams/pipeline.js +86 -0
  77. package/node_modules/readable-stream/lib/internal/streams/state.js +22 -0
  78. package/node_modules/readable-stream/lib/internal/streams/stream-browser.js +1 -0
  79. package/node_modules/readable-stream/lib/internal/streams/stream.js +1 -0
  80. package/node_modules/readable-stream/package.json +68 -0
  81. package/node_modules/readable-stream/readable-browser.js +9 -0
  82. package/node_modules/readable-stream/readable.js +16 -0
  83. package/node_modules/stream-shift/.github/workflows/test.yml +23 -0
  84. package/node_modules/stream-shift/LICENSE +21 -0
  85. package/node_modules/stream-shift/README.md +25 -0
  86. package/node_modules/stream-shift/index.js +21 -0
  87. package/node_modules/stream-shift/package.json +25 -0
  88. package/node_modules/stream-shift/test.js +48 -0
  89. package/node_modules/string_decoder/LICENSE +48 -0
  90. package/node_modules/string_decoder/README.md +47 -0
  91. package/node_modules/string_decoder/lib/string_decoder.js +296 -0
  92. package/node_modules/string_decoder/package.json +34 -0
  93. package/node_modules/util-deprecate/History.md +16 -0
  94. package/node_modules/util-deprecate/LICENSE +24 -0
  95. package/node_modules/util-deprecate/README.md +53 -0
  96. package/node_modules/util-deprecate/browser.js +67 -0
  97. package/node_modules/util-deprecate/node.js +6 -0
  98. package/node_modules/util-deprecate/package.json +27 -0
  99. package/node_modules/wrappy/LICENSE +15 -0
  100. package/node_modules/wrappy/README.md +36 -0
  101. package/node_modules/wrappy/package.json +29 -0
  102. package/node_modules/wrappy/wrappy.js +33 -0
  103. package/package.json +15 -2
  104. package/dist/web/assets/Assistant-CkKCs2G4.js +0 -1
  105. package/dist/web/assets/Computers-B9wEqpMc.js +0 -4
  106. package/dist/web/assets/Computers-DBnXVk7q.css +0 -1
  107. package/dist/web/assets/Conversation-DK5-Q1RB.js +0 -1
  108. package/dist/web/assets/Dashboard-B2HGjI4a.js +0 -1
  109. package/dist/web/assets/Files-Du-7z96D.js +0 -1
  110. package/dist/web/assets/HtmlPreview-Bi3u6-l3.js +0 -1
  111. package/dist/web/assets/MessageComposer-6eJAo_e7.js +0 -1
  112. package/dist/web/assets/MessageComposer-CJGxkvML.css +0 -1
  113. package/dist/web/assets/PillControl-BRPhbHQm.js +0 -1
  114. package/dist/web/assets/PillControl-CqVA7kIQ.css +0 -1
  115. package/dist/web/assets/ResourceLibrary-DJ9cxDoc.js +0 -6
  116. package/dist/web/assets/WorkspaceTree-DUFGMLRi.js +0 -1
  117. package/dist/web/assets/file-links-Dl1S0Tt0.js +0 -1
  118. package/dist/web/assets/index-C4EXuXVY.css +0 -1
  119. package/dist/web/assets/index-xjf37um4.js +0 -66
@@ -0,0 +1,785 @@
1
+ 'use strict'
2
+
3
+ const http = require('node:http')
4
+ const net = require('node:net')
5
+ const split = require('split2')
6
+ const { test } = require('node:test')
7
+ const Fastify = require('fastify')
8
+ const fastifyWebsocket = require('..')
9
+ const WebSocket = require('ws')
10
+ const { once, on } = require('node:events')
11
+ let timersPromises
12
+
13
+ try {
14
+ timersPromises = require('node:timers/promises')
15
+ } catch {}
16
+
17
+ test('Should expose a websocket', async (t) => {
18
+ t.plan(2)
19
+
20
+ const fastify = Fastify()
21
+ t.after(() => fastify.close())
22
+
23
+ await fastify.register(fastifyWebsocket)
24
+
25
+ fastify.get('/', { websocket: true }, (socket) => {
26
+ t.after(() => socket.terminate())
27
+
28
+ socket.once('message', (chunk) => {
29
+ t.assert.deepStrictEqual(chunk.toString(), 'hello server')
30
+ socket.send('hello client')
31
+ })
32
+ })
33
+
34
+ await fastify.listen({ port: 0 })
35
+
36
+ const ws = new WebSocket('ws://localhost:' + fastify.server.address().port)
37
+ t.after(() => {
38
+ if (ws.readyState) {
39
+ ws.close()
40
+ }
41
+ })
42
+
43
+ const chunkPromise = once(ws, 'message')
44
+ await once(ws, 'open')
45
+ ws.send('hello server')
46
+
47
+ const [chunk] = await chunkPromise
48
+ t.assert.deepStrictEqual(chunk.toString(), 'hello client')
49
+ ws.close()
50
+ })
51
+
52
+ test('Should fail if custom errorHandler is not a function', async (t) => {
53
+ t.plan(2)
54
+
55
+ const fastify = Fastify()
56
+ t.after(() => fastify.close())
57
+
58
+ try {
59
+ await fastify.register(fastifyWebsocket, { errorHandler: {} })
60
+ } catch (err) {
61
+ t.assert.deepStrictEqual(err.message, 'invalid errorHandler function')
62
+ }
63
+
64
+ fastify.get('/', { websocket: true }, (socket) => {
65
+ t.after(() => socket.terminate())
66
+ })
67
+
68
+ try {
69
+ await fastify.listen({ port: 0 })
70
+ } catch (err) {
71
+ t.assert.deepStrictEqual(err.message, 'invalid errorHandler function')
72
+ }
73
+ })
74
+
75
+ test('Should run custom errorHandler on wildcard route handler error', async (t) => {
76
+ t.plan(1)
77
+
78
+ const fastify = Fastify()
79
+ t.after(() => fastify.close())
80
+
81
+ let _resolve
82
+ const p = new Promise((resolve) => {
83
+ _resolve = resolve
84
+ })
85
+
86
+ await fastify.register(fastifyWebsocket, {
87
+ errorHandler: function (error) {
88
+ t.assert.deepStrictEqual(error.message, 'Fail')
89
+ _resolve()
90
+ }
91
+ })
92
+
93
+ fastify.get('/*', { websocket: true }, (socket) => {
94
+ socket.on('message', (data) => socket.send(data))
95
+ t.after(() => socket.terminate())
96
+ return Promise.reject(new Error('Fail'))
97
+ })
98
+
99
+ await fastify.listen({ port: 0 })
100
+
101
+ const ws = new WebSocket('ws://localhost:' + fastify.server.address().port)
102
+ t.after(() => {
103
+ if (ws.readyState) {
104
+ ws.close()
105
+ }
106
+ })
107
+
108
+ await p
109
+ })
110
+
111
+ test('Should run custom errorHandler on error inside websocket handler', async (t) => {
112
+ t.plan(1)
113
+
114
+ const fastify = Fastify()
115
+ t.after(() => fastify.close())
116
+
117
+ let _resolve
118
+ const p = new Promise((resolve) => {
119
+ _resolve = resolve
120
+ })
121
+
122
+ const options = {
123
+ errorHandler: function (error) {
124
+ t.assert.deepStrictEqual(error.message, 'Fail')
125
+ _resolve()
126
+ }
127
+ }
128
+
129
+ await fastify.register(fastifyWebsocket, options)
130
+
131
+ fastify.get('/', { websocket: true }, function wsHandler (socket) {
132
+ socket.on('message', (data) => socket.send(data))
133
+ t.after(() => socket.terminate())
134
+ throw new Error('Fail')
135
+ })
136
+
137
+ await fastify.listen({ port: 0 })
138
+ const ws = new WebSocket('ws://localhost:' + fastify.server.address().port)
139
+
140
+ t.after(() => {
141
+ if (ws.readyState) {
142
+ ws.close()
143
+ }
144
+ })
145
+
146
+ await p
147
+ })
148
+
149
+ test('Should run custom errorHandler on error inside async websocket handler', async (t) => {
150
+ t.plan(1)
151
+
152
+ const fastify = Fastify()
153
+ t.after(() => fastify.close())
154
+
155
+ let _resolve
156
+ const p = new Promise((resolve) => {
157
+ _resolve = resolve
158
+ })
159
+
160
+ const options = {
161
+ errorHandler: function (error) {
162
+ t.assert.deepStrictEqual(error.message, 'Fail')
163
+ _resolve()
164
+ }
165
+ }
166
+
167
+ await fastify.register(fastifyWebsocket, options)
168
+
169
+ fastify.get('/', { websocket: true }, async function wsHandler (socket) {
170
+ socket.on('message', (data) => socket.send(data))
171
+ t.after(() => socket.terminate())
172
+ throw new Error('Fail')
173
+ })
174
+
175
+ await fastify.listen({ port: 0 })
176
+ const ws = new WebSocket('ws://localhost:' + fastify.server.address().port)
177
+ t.after(() => {
178
+ if (ws.readyState) {
179
+ ws.close()
180
+ }
181
+ })
182
+
183
+ await p
184
+ })
185
+
186
+ test('Should run custom errorHandler when the raw socket emits an error', async (t) => {
187
+ t.plan(2)
188
+
189
+ const fastify = Fastify()
190
+ t.after(() => fastify.close())
191
+
192
+ let _resolve
193
+ const p = new Promise((resolve) => {
194
+ _resolve = resolve
195
+ })
196
+
197
+ await fastify.register(fastifyWebsocket, {
198
+ errorHandler: function (error, socket, request, reply) {
199
+ t.assert.deepStrictEqual(error.code, 'WS_ERR_UNEXPECTED_RSV_2_3')
200
+ t.assert.deepStrictEqual(request.ws, true)
201
+ socket.terminate()
202
+ _resolve()
203
+ }
204
+ })
205
+
206
+ fastify.get('/', { websocket: true }, () => {})
207
+
208
+ await fastify.listen({ port: 0 })
209
+
210
+ const client = new WebSocket('ws://localhost:' + fastify.server.address().port)
211
+ t.after(() => {
212
+ if (client.readyState) {
213
+ client.close()
214
+ }
215
+ })
216
+
217
+ await once(client, 'open')
218
+
219
+ // Write an invalid frame directly to the underlying TCP socket so the
220
+ // server-side websocket emits an 'error' event after the connection is
221
+ // established.
222
+ client._socket.write(Buffer.from([0xa2, 0x00]))
223
+
224
+ await p
225
+ })
226
+
227
+ test('Should handle raw socket errors while upgrade hooks are running', async (t) => {
228
+ const fastify = Fastify()
229
+ t.after(() => fastify.close())
230
+
231
+ await fastify.register(fastifyWebsocket)
232
+
233
+ let enterHook
234
+ const hookEntered = new Promise((resolve) => {
235
+ enterHook = resolve
236
+ })
237
+
238
+ fastify.addHook('onRequest', async () => {
239
+ enterHook()
240
+ await new Promise((resolve) => setTimeout(resolve, 100))
241
+ })
242
+
243
+ fastify.get('/', { websocket: true }, () => {})
244
+
245
+ await fastify.listen({ port: 0 })
246
+
247
+ const client = net.connect(fastify.server.address().port, '127.0.0.1', () => {
248
+ client.write(
249
+ 'GET / HTTP/1.1\r\n' +
250
+ 'Host: localhost\r\n' +
251
+ 'Connection: Upgrade\r\n' +
252
+ 'Upgrade: websocket\r\n' +
253
+ 'Sec-WebSocket-Version: 13\r\n' +
254
+ 'Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n\r\n'
255
+ )
256
+ })
257
+ client.on('error', () => {})
258
+
259
+ await hookEntered
260
+ client.resetAndDestroy()
261
+ await new Promise((resolve) => setTimeout(resolve, 200))
262
+
263
+ t.assert.strictEqual(fastify.server.listening, true)
264
+ })
265
+
266
+ test('Should be able to pass custom options to ws', async (t) => {
267
+ t.plan(2)
268
+
269
+ const fastify = Fastify()
270
+ t.after(() => fastify.close())
271
+
272
+ const options = {
273
+ verifyClient: function (info) {
274
+ t.assert.deepStrictEqual(info.req.headers['x-custom-header'], 'fastify is awesome !')
275
+
276
+ return true
277
+ }
278
+ }
279
+
280
+ await fastify.register(fastifyWebsocket, { options })
281
+
282
+ fastify.get('/*', { websocket: true }, (socket) => {
283
+ socket.on('message', (data) => socket.send(data))
284
+ t.after(() => socket.terminate())
285
+ })
286
+
287
+ await fastify.listen({ port: 0 })
288
+
289
+ const clientOptions = { headers: { 'x-custom-header': 'fastify is awesome !' } }
290
+ const ws = new WebSocket('ws://localhost:' + fastify.server.address().port, clientOptions)
291
+ const chunkPromise = once(ws, 'message')
292
+ await once(ws, 'open')
293
+ t.after(() => {
294
+ if (ws.readyState) {
295
+ ws.close()
296
+ }
297
+ })
298
+
299
+ ws.send('hello')
300
+
301
+ const [chunk] = await chunkPromise
302
+ t.assert.deepStrictEqual(chunk.toString(), 'hello')
303
+ ws.close()
304
+ })
305
+
306
+ test('Should warn if path option is provided to ws', async (t) => {
307
+ t.plan(3)
308
+ const logStream = split(JSON.parse)
309
+ const fastify = Fastify({
310
+ logger: {
311
+ stream: logStream,
312
+ level: 'warn'
313
+ }
314
+ })
315
+
316
+ logStream.once('data', line => {
317
+ t.assert.deepStrictEqual(line.msg, 'ws server path option shouldn\'t be provided, use a route instead')
318
+ t.assert.deepStrictEqual(line.level, 40)
319
+ })
320
+
321
+ t.after(() => fastify.close())
322
+
323
+ const options = { path: '/' }
324
+ await fastify.register(fastifyWebsocket, { options })
325
+
326
+ fastify.get('/*', { websocket: true }, (socket) => {
327
+ socket.on('message', (data) => socket.send(data))
328
+ t.after(() => socket.terminate())
329
+ })
330
+
331
+ await fastify.listen({ port: 0 })
332
+
333
+ const clientOptions = { headers: { 'x-custom-header': 'fastify is awesome !' } }
334
+ const ws = new WebSocket('ws://localhost:' + fastify.server.address().port, clientOptions)
335
+ const chunkPromise = once(ws, 'message')
336
+ await once(ws, 'open')
337
+ t.after(() => {
338
+ if (ws.readyState) {
339
+ ws.close()
340
+ }
341
+ })
342
+
343
+ ws.send('hello')
344
+
345
+ const [chunk] = await chunkPromise
346
+ t.assert.deepStrictEqual(chunk.toString(), 'hello')
347
+ ws.close()
348
+ })
349
+
350
+ test('Should be able to pass a custom server option to ws', async (t) => {
351
+ // We create an external server
352
+ const externalServerPort = 3000
353
+ const externalServer = http
354
+ .createServer()
355
+ .on('connection', (socket) => {
356
+ socket.unref()
357
+ })
358
+ .listen(externalServerPort, 'localhost')
359
+
360
+ const fastify = Fastify()
361
+ t.after(() => {
362
+ externalServer.close()
363
+ fastify.close()
364
+ })
365
+
366
+ const options = {
367
+ server: externalServer
368
+ }
369
+
370
+ await fastify.register(fastifyWebsocket, { options })
371
+
372
+ fastify.get('/', { websocket: true }, (socket) => {
373
+ socket.on('message', (data) => socket.send(data))
374
+ t.after(() => socket.terminate())
375
+ })
376
+
377
+ await fastify.ready()
378
+
379
+ const ws = new WebSocket('ws://localhost:' + externalServerPort)
380
+ const chunkPromise = once(ws, 'message')
381
+ await once(ws, 'open')
382
+ t.after(() => {
383
+ if (ws.readyState) {
384
+ ws.close()
385
+ }
386
+ })
387
+
388
+ ws.send('hello')
389
+
390
+ const [chunk] = await chunkPromise
391
+ t.assert.deepStrictEqual(chunk.toString(), 'hello')
392
+ ws.close()
393
+ })
394
+
395
+ test('Should remove the upgrade listener from a custom server on close', async (t) => {
396
+ const externalServer = http.createServer()
397
+ t.after(() => externalServer.close())
398
+ await new Promise((resolve, reject) => {
399
+ externalServer.once('error', reject)
400
+ externalServer.listen(0, resolve)
401
+ })
402
+
403
+ const fastify = Fastify()
404
+ t.after(() => fastify.close())
405
+ await fastify.register(fastifyWebsocket, { options: { server: externalServer } })
406
+ await fastify.ready()
407
+
408
+ t.assert.strictEqual(externalServer.listenerCount('upgrade'), 1)
409
+ await fastify.close()
410
+ t.assert.strictEqual(externalServer.listenerCount('upgrade'), 0)
411
+ })
412
+
413
+ test('Should remove the upgrade listener from a custom server on close with a custom preClose', async (t) => {
414
+ const externalServer = http.createServer()
415
+ t.after(() => externalServer.close())
416
+ await new Promise((resolve, reject) => {
417
+ externalServer.once('error', reject)
418
+ externalServer.listen(0, resolve)
419
+ })
420
+
421
+ const fastify = Fastify()
422
+ t.after(() => fastify.close())
423
+ await fastify.register(fastifyWebsocket, {
424
+ options: { server: externalServer },
425
+ preClose: (done) => {
426
+ fastify.websocketServer.close(done)
427
+ }
428
+ })
429
+ await fastify.ready()
430
+
431
+ t.assert.strictEqual(externalServer.listenerCount('upgrade'), 1)
432
+ await fastify.close()
433
+ t.assert.strictEqual(externalServer.listenerCount('upgrade'), 0)
434
+ })
435
+
436
+ test('Should be able to pass clientTracking option in false to ws', async (t) => {
437
+ const fastify = Fastify()
438
+
439
+ const options = {
440
+ clientTracking: false
441
+ }
442
+
443
+ fastify.register(fastifyWebsocket, { options })
444
+
445
+ fastify.get('/*', { websocket: true }, (socket) => {
446
+ socket.close()
447
+ })
448
+
449
+ await fastify.listen({ port: 0 })
450
+
451
+ await fastify.close()
452
+ })
453
+
454
+ test('Should be able to pass preClose option to override default', async (t) => {
455
+ t.plan(3)
456
+
457
+ const fastify = Fastify()
458
+
459
+ const preClose = (done) => {
460
+ t.assert.ok('Custom preclose successfully called')
461
+
462
+ for (const connection of fastify.websocketServer.clients) {
463
+ connection.close()
464
+ }
465
+ done()
466
+ }
467
+
468
+ await fastify.register(fastifyWebsocket, { preClose })
469
+
470
+ fastify.get('/', { websocket: true }, (socket) => {
471
+ t.after(() => socket.terminate())
472
+
473
+ socket.once('message', (chunk) => {
474
+ t.assert.deepStrictEqual(chunk.toString(), 'hello server')
475
+ socket.send('hello client')
476
+ })
477
+ })
478
+
479
+ await fastify.listen({ port: 0 })
480
+
481
+ const ws = new WebSocket('ws://localhost:' + fastify.server.address().port)
482
+ t.after(() => {
483
+ if (ws.readyState) {
484
+ ws.close()
485
+ }
486
+ })
487
+
488
+ const chunkPromise = once(ws, 'message')
489
+ await once(ws, 'open')
490
+ ws.send('hello server')
491
+
492
+ const [chunk] = await chunkPromise
493
+ t.assert.deepStrictEqual(chunk.toString(), 'hello client')
494
+ ws.close()
495
+
496
+ await fastify.close()
497
+ })
498
+
499
+ test('Should fail if custom preClose is not a function', async (t) => {
500
+ t.plan(2)
501
+
502
+ const fastify = Fastify()
503
+ t.after(() => fastify.close())
504
+
505
+ const preClose = 'Not a function'
506
+
507
+ try {
508
+ await fastify.register(fastifyWebsocket, { preClose })
509
+ } catch (err) {
510
+ t.assert.deepStrictEqual(err.message, 'invalid preClose function')
511
+ }
512
+
513
+ fastify.get('/', { websocket: true }, (socket) => {
514
+ t.after(() => socket.terminate())
515
+ })
516
+
517
+ try {
518
+ await fastify.listen({ port: 0 })
519
+ } catch (err) {
520
+ t.assert.deepStrictEqual(err.message, 'invalid preClose function')
521
+ }
522
+ })
523
+
524
+ test('Should gracefully close with a connected client', async (t) => {
525
+ t.plan(2)
526
+
527
+ const fastify = Fastify()
528
+
529
+ await fastify.register(fastifyWebsocket)
530
+ let serverConnEnded
531
+
532
+ fastify.get('/', { websocket: true }, (socket) => {
533
+ socket.send('hello client')
534
+
535
+ socket.once('message', (chunk) => {
536
+ t.assert.deepStrictEqual(chunk.toString(), 'hello server')
537
+ })
538
+
539
+ serverConnEnded = once(socket, 'close')
540
+ // this connection stays alive untile we close the server
541
+ })
542
+
543
+ await fastify.listen({ port: 0 })
544
+
545
+ const ws = new WebSocket('ws://localhost:' + fastify.server.address().port)
546
+ const chunkPromise = once(ws, 'message')
547
+ await once(ws, 'open')
548
+ ws.send('hello server')
549
+
550
+ const ended = once(ws, 'close')
551
+ const [chunk] = await chunkPromise
552
+ t.assert.deepStrictEqual(chunk.toString(), 'hello client')
553
+ await fastify.close()
554
+ await ended
555
+ await serverConnEnded
556
+ })
557
+
558
+ test('Should gracefully close when clients attempt to connect after calling close', async (t) => {
559
+ t.plan(3)
560
+
561
+ const fastify = Fastify()
562
+
563
+ const oldClose = fastify.server.close
564
+ let p
565
+ fastify.server.close = function (cb) {
566
+ const ws = new WebSocket('ws://localhost:' + fastify.server.address().port)
567
+
568
+ p = once(ws, 'close').catch((err) => {
569
+ t.assert.deepStrictEqual(err.message, 'Unexpected server response: 503')
570
+ oldClose.call(this, cb)
571
+ })
572
+ }
573
+
574
+ await fastify.register(fastifyWebsocket)
575
+
576
+ fastify.get('/', { websocket: true }, (socket) => {
577
+ t.assert.ok('received client connection')
578
+ socket.close()
579
+ // this connection stays alive until we close the server
580
+ })
581
+
582
+ await fastify.listen({ port: 0 })
583
+
584
+ const ws = new WebSocket('ws://localhost:' + fastify.server.address().port)
585
+
586
+ ws.on('close', () => {
587
+ t.assert.ok('client 1 closed')
588
+ })
589
+
590
+ await once(ws, 'open')
591
+ await fastify.close()
592
+ await p
593
+ })
594
+
595
+ /*
596
+ This test sends one message every 10 ms.
597
+ After 50 messages have been sent, we check how many unhandled messages the server has.
598
+ After 100 messages we check this number has not increased but rather decreased
599
+ the number of unhandled messages below a threshold, which means it is still able
600
+ to process message.
601
+ */
602
+ test('Should keep accepting connection', { skip: !timersPromises }, async t => {
603
+ t.plan(1)
604
+
605
+ const fastify = Fastify()
606
+ let sent = 0
607
+ let unhandled = 0
608
+ let threshold = 0
609
+
610
+ await fastify.register(fastifyWebsocket)
611
+
612
+ fastify.get('/', { websocket: true }, (socket) => {
613
+ socket.on('message', () => {
614
+ unhandled--
615
+ })
616
+
617
+ socket.on('error', err => {
618
+ t.error(err)
619
+ })
620
+
621
+ /*
622
+ This is a safety check - If the socket is stuck, fastify.close will not run.
623
+ Therefore after 100 messages we forcibly close the socket.
624
+ */
625
+ const safetyInterval = setInterval(() => {
626
+ if (sent < 100) {
627
+ return
628
+ }
629
+
630
+ clearInterval(safetyInterval)
631
+ socket.terminate()
632
+ }, 100)
633
+ })
634
+
635
+ await fastify.listen({ port: 0 })
636
+
637
+ // Setup a client that sends a lot of messages to the server
638
+ const client = new WebSocket('ws://localhost:' + fastify.server.address().port)
639
+ client.on('error', console.error)
640
+
641
+ await once(client, 'open')
642
+ const message = Buffer.alloc(1024, Date.now())
643
+
644
+ /* eslint-disable no-unused-vars */
645
+ for await (const _ of timersPromises.setInterval(10)) {
646
+ client.send(message.toString(), 10)
647
+ sent++
648
+ unhandled++
649
+ if (sent === 50) {
650
+ threshold = unhandled
651
+ } else if (sent === 100) {
652
+ await fastify.close()
653
+ t.assert.ok(unhandled <= threshold)
654
+ break
655
+ }
656
+ }
657
+ })
658
+
659
+ test('Should keep processing message when many medium sized messages are sent', async t => {
660
+ t.plan(1)
661
+
662
+ const fastify = Fastify()
663
+ const total = 200
664
+ let handled = 0
665
+
666
+ await fastify.register(fastifyWebsocket)
667
+
668
+ fastify.get('/', { websocket: true }, (socket) => {
669
+ socket.on('message', () => {
670
+ socket.send('handled')
671
+ })
672
+
673
+ socket.on('error', err => {
674
+ t.error(err)
675
+ })
676
+ })
677
+
678
+ await fastify.listen({ port: 0 })
679
+
680
+ // Setup a client that sends a lot of messages to the server
681
+ const client = new WebSocket('ws://localhost:' + fastify.server.address().port)
682
+ client.on('error', console.error)
683
+
684
+ await once(client, 'open')
685
+
686
+ for (let i = 0; i < total; i++) {
687
+ client.send(Buffer.alloc(160, `${i}`).toString('utf-8'))
688
+ }
689
+
690
+ /* eslint-disable no-unused-vars */
691
+ for await (const _ of on(client, 'message')) {
692
+ handled++
693
+
694
+ if (handled === total) {
695
+ break
696
+ }
697
+ }
698
+
699
+ await fastify.close()
700
+ t.assert.deepStrictEqual(handled, total)
701
+ })
702
+
703
+ test('Should error server if the noServer option is set', (t) => {
704
+ t.plan(1)
705
+ const fastify = Fastify()
706
+
707
+ fastify.register(fastifyWebsocket, { options: { noServer: true } })
708
+ t.assert.rejects(fastify.ready())
709
+ })
710
+
711
+ test('Should preserve the prefix in non-websocket routes', async (t) => {
712
+ t.plan(2)
713
+
714
+ const fastify = Fastify()
715
+ t.after(() => fastify.close())
716
+
717
+ fastify.register(fastifyWebsocket)
718
+
719
+ fastify.register(async function (fastify) {
720
+ t.assert.deepStrictEqual(fastify.prefix, '/hello')
721
+ fastify.get('/', function (_, reply) {
722
+ t.assert.deepStrictEqual(this.prefix, '/hello')
723
+ reply.send('hello')
724
+ })
725
+ }, { prefix: '/hello' })
726
+
727
+ await fastify.inject('/hello')
728
+ })
729
+
730
+ test('Should Handle WebSocket errors to avoid Node.js crashes', async t => {
731
+ t.plan(1)
732
+
733
+ const fastify = Fastify()
734
+ await fastify.register(fastifyWebsocket)
735
+
736
+ fastify.get('/', { websocket: true }, (socket) => {
737
+ socket.on('error', err => {
738
+ t.assert.deepStrictEqual(err.code, 'WS_ERR_UNEXPECTED_RSV_2_3')
739
+ })
740
+ })
741
+
742
+ await fastify.listen({ port: 0 })
743
+
744
+ const client = new WebSocket('ws://localhost:' + fastify.server.address().port)
745
+ await once(client, 'open')
746
+
747
+ client._socket.write(Buffer.from([0xa2, 0x00]))
748
+
749
+ await fastify.close()
750
+ })
751
+
752
+ test('remove all others websocket handlers on close', async (t) => {
753
+ const fastify = Fastify()
754
+
755
+ await fastify.register(fastifyWebsocket)
756
+
757
+ await fastify.listen({ port: 0 })
758
+
759
+ await fastify.close()
760
+
761
+ t.assert.deepStrictEqual(fastify.server.listeners('upgrade').length, 0)
762
+ })
763
+
764
+ test('clashing upgrade handler', async (t) => {
765
+ const fastify = Fastify()
766
+ t.after(() => fastify.close())
767
+
768
+ fastify.server.on('upgrade', (req, socket) => {
769
+ const res = new http.ServerResponse(req)
770
+ res.assignSocket(socket)
771
+ res.end()
772
+ socket.destroy()
773
+ })
774
+
775
+ await fastify.register(fastifyWebsocket)
776
+
777
+ fastify.get('/', { websocket: true }, () => {
778
+ t.assert.fail('this should never be invoked')
779
+ })
780
+
781
+ await fastify.listen({ port: 0 })
782
+
783
+ const ws = new WebSocket('ws://localhost:' + fastify.server.address().port)
784
+ await once(ws, 'error')
785
+ })