@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,890 @@
1
+ 'use strict'
2
+
3
+ const net = require('node:net')
4
+ const { test } = require('node:test')
5
+ const Fastify = require('fastify')
6
+ const fastifyWebsocket = require('..')
7
+ const WebSocket = require('ws')
8
+ const get = require('node:http').get
9
+
10
+ const withResolvers = function () {
11
+ let promiseResolve, promiseReject
12
+ const promise = new Promise((resolve, reject) => {
13
+ promiseResolve = resolve
14
+ promiseReject = reject
15
+ })
16
+ return { promise, resolve: promiseResolve, reject: promiseReject }
17
+ }
18
+
19
+ test('Should expose a websocket on prefixed route', (t, end) => {
20
+ t.plan(4)
21
+ const fastify = Fastify()
22
+
23
+ t.after(() => fastify.close())
24
+
25
+ fastify.register(fastifyWebsocket)
26
+ fastify.register(
27
+ function (instance, _opts, next) {
28
+ instance.get('/echo', { websocket: true }, function (socket) {
29
+ t.assert.deepStrictEqual(this.prefix, '/baz')
30
+ socket.send('hello client')
31
+ t.after(() => socket.terminate())
32
+
33
+ socket.once('message', (chunk) => {
34
+ t.assert.deepStrictEqual(chunk.toString(), 'hello server')
35
+ end()
36
+ })
37
+ })
38
+ next()
39
+ },
40
+ { prefix: '/baz' }
41
+ )
42
+
43
+ fastify.listen({ port: 0 }, err => {
44
+ t.assert.ifError(err)
45
+ const ws = new WebSocket('ws://localhost:' + (fastify.server.address()).port + '/baz/echo')
46
+ const client = WebSocket.createWebSocketStream(ws, { encoding: 'utf8' })
47
+ t.after(() => client.destroy())
48
+
49
+ client.setEncoding('utf8')
50
+ client.write('hello server')
51
+
52
+ client.once('data', chunk => {
53
+ t.assert.deepStrictEqual(chunk, 'hello client')
54
+ client.end()
55
+ })
56
+ })
57
+ })
58
+
59
+ test('Should expose a websocket on prefixed route with /', (t, end) => {
60
+ t.plan(3)
61
+ const fastify = Fastify()
62
+
63
+ t.after(() => fastify.close())
64
+
65
+ fastify.register(fastifyWebsocket)
66
+ fastify.register(
67
+ function (instance, _opts, next) {
68
+ instance.get('/', { websocket: true }, (socket) => {
69
+ socket.send('hello client')
70
+ t.after(() => socket.terminate())
71
+
72
+ socket.once('message', (chunk) => {
73
+ t.assert.deepStrictEqual(chunk.toString(), 'hello server')
74
+ end()
75
+ })
76
+ })
77
+ next()
78
+ },
79
+ { prefix: '/baz' }
80
+ )
81
+
82
+ fastify.listen({ port: 0 }, err => {
83
+ t.assert.ifError(err)
84
+ const ws = new WebSocket('ws://localhost:' + (fastify.server.address()).port + '/baz')
85
+ const client = WebSocket.createWebSocketStream(ws, { encoding: 'utf8' })
86
+ t.after(() => client.destroy())
87
+
88
+ client.setEncoding('utf8')
89
+ client.write('hello server')
90
+
91
+ client.once('data', chunk => {
92
+ t.assert.deepStrictEqual(chunk, 'hello client')
93
+ client.end()
94
+ })
95
+ })
96
+ })
97
+
98
+ test('Should expose websocket and http route', (t) => {
99
+ t.plan(4)
100
+ const fastify = Fastify()
101
+
102
+ t.after(() => fastify.close())
103
+
104
+ const { promise: clientPromise, resolve: clientResolve } = withResolvers()
105
+ const { promise: serverPromise, resolve: serverResolve } = withResolvers()
106
+
107
+ fastify.register(fastifyWebsocket)
108
+ fastify.register(
109
+ function (instance, _opts, next) {
110
+ instance.route({
111
+ method: 'GET',
112
+ url: '/echo',
113
+ handler: (_request, reply) => {
114
+ reply.send({ hello: 'world' })
115
+ },
116
+ wsHandler: (socket) => {
117
+ socket.send('hello client')
118
+ t.after(() => socket.terminate())
119
+
120
+ socket.once('message', (chunk) => {
121
+ t.assert.deepStrictEqual(chunk.toString(), 'hello server')
122
+ clientResolve()
123
+ })
124
+ }
125
+ })
126
+ next()
127
+ },
128
+ { prefix: '/baz' }
129
+ )
130
+
131
+ fastify.listen({ port: 0 }, err => {
132
+ t.assert.ifError(err)
133
+ const url = '//localhost:' + (fastify.server.address()).port + '/baz/echo'
134
+ const ws = new WebSocket('ws:' + url)
135
+ const client = WebSocket.createWebSocketStream(ws, { encoding: 'utf8' })
136
+ t.after(() => client.destroy())
137
+
138
+ client.setEncoding('utf8')
139
+ client.write('hello server')
140
+
141
+ client.once('data', chunk => {
142
+ t.assert.deepStrictEqual(chunk, 'hello client')
143
+ client.end()
144
+ })
145
+ get('http:' + url, function (response) {
146
+ let data = ''
147
+
148
+ // A chunk of data has been recieved.
149
+ response.on('data', (chunk) => {
150
+ data += chunk
151
+ })
152
+
153
+ // The whole response has been received. Print out the result.
154
+ response.on('end', () => {
155
+ t.assert.deepStrictEqual(data, '{"hello":"world"}')
156
+ serverResolve()
157
+ })
158
+ })
159
+ })
160
+
161
+ return Promise.all([clientPromise, serverPromise])
162
+ })
163
+
164
+ test('Should close on unregistered path (with no wildcard route websocket handler defined)', (t, end) => {
165
+ t.plan(2)
166
+ const fastify = Fastify()
167
+
168
+ t.after(() => fastify.close())
169
+
170
+ fastify
171
+ .register(fastifyWebsocket)
172
+ .register(async function () {
173
+ fastify.get('/*', (_request, reply) => {
174
+ reply.send('hello world')
175
+ })
176
+
177
+ fastify.get('/echo', { websocket: true }, (socket) => {
178
+ socket.on('message', message => {
179
+ try {
180
+ socket.send(message)
181
+ } catch (err) {
182
+ socket.send(err.message)
183
+ }
184
+ })
185
+
186
+ t.after(() => socket.terminate())
187
+ })
188
+ })
189
+
190
+ fastify.listen({ port: 0 }, err => {
191
+ t.assert.ifError(err)
192
+ const ws = new WebSocket('ws://localhost:' + (fastify.server.address()).port)
193
+ const client = WebSocket.createWebSocketStream(ws, { encoding: 'utf8' })
194
+ t.after(() => client.destroy())
195
+ ws.on('close', () => {
196
+ t.assert.ok(true)
197
+ end()
198
+ })
199
+ })
200
+ })
201
+
202
+ test('Should use wildcard websocket route when (with a normal http wildcard route defined as well)', (t, end) => {
203
+ t.plan(2)
204
+ const fastify = Fastify()
205
+
206
+ t.after(() => fastify.close())
207
+
208
+ fastify
209
+ .register(fastifyWebsocket)
210
+ .register(async function (fastify) {
211
+ fastify.route({
212
+ method: 'GET',
213
+ url: '/*',
214
+ handler: (_, reply) => {
215
+ reply.send({ hello: 'world' })
216
+ },
217
+ wsHandler: (socket) => {
218
+ socket.send('hello client')
219
+ t.after(() => socket.terminate())
220
+
221
+ socket.once('message', () => {
222
+ socket.close()
223
+ })
224
+ }
225
+ })
226
+ })
227
+
228
+ fastify.listen({ port: 0 }, err => {
229
+ t.assert.ifError(err)
230
+ const ws = new WebSocket('ws://localhost:' + (fastify.server.address()).port)
231
+ const client = WebSocket.createWebSocketStream(ws, { encoding: 'utf8' })
232
+ t.after(() => client.destroy())
233
+
234
+ client.once('data', chunk => {
235
+ t.assert.deepStrictEqual(chunk, 'hello client')
236
+ client.end()
237
+ end()
238
+ })
239
+ })
240
+ })
241
+
242
+ test('Should call wildcard route handler on unregistered path', (t, end) => {
243
+ t.plan(3)
244
+ const fastify = Fastify()
245
+
246
+ t.after(() => fastify.close())
247
+
248
+ fastify
249
+ .register(fastifyWebsocket)
250
+ .register(async function (fastify) {
251
+ fastify.get('/*', { websocket: true }, (socket) => {
252
+ socket.on('message', () => {
253
+ try {
254
+ socket.send('hi from wildcard route handler')
255
+ } catch (err) {
256
+ socket.send(err.message)
257
+ }
258
+ })
259
+ t.after(() => socket.terminate())
260
+ })
261
+ })
262
+
263
+ fastify.get('/echo', { websocket: true }, (socket) => {
264
+ socket.on('message', () => {
265
+ try {
266
+ socket.send('hi from /echo handler')
267
+ } catch (err) {
268
+ socket.send(err.message)
269
+ }
270
+ })
271
+
272
+ t.after(() => socket.terminate())
273
+ })
274
+
275
+ fastify.listen({ port: 0 }, err => {
276
+ t.assert.ifError(err)
277
+ const ws = new WebSocket('ws://localhost:' + (fastify.server.address()).port)
278
+ const client = WebSocket.createWebSocketStream(ws, { encoding: 'utf8' })
279
+ t.after(() => client.destroy())
280
+
281
+ ws.on('open', () => {
282
+ ws.send('hi from client')
283
+ client.end()
284
+ })
285
+
286
+ ws.on('message', message => {
287
+ t.assert.deepStrictEqual(message.toString(), 'hi from wildcard route handler')
288
+ })
289
+
290
+ ws.on('close', () => {
291
+ t.assert.ok(true)
292
+ end()
293
+ })
294
+ })
295
+ })
296
+
297
+ test('Should invoke the correct handler depending on the headers', (t, end) => {
298
+ t.plan(4)
299
+ const fastify = Fastify()
300
+
301
+ t.after(() => fastify.close())
302
+
303
+ fastify.register(fastifyWebsocket)
304
+ fastify.register(async function () {
305
+ fastify.route({
306
+ method: 'GET',
307
+ url: '/',
308
+ handler: (_request, reply) => {
309
+ reply.send('hi from handler')
310
+ },
311
+ wsHandler: (socket) => {
312
+ socket.send('hi from wsHandler')
313
+ t.after(() => socket.terminate())
314
+ }
315
+ })
316
+ })
317
+
318
+ fastify.listen({ port: 0 }, err => {
319
+ t.assert.ifError(err)
320
+
321
+ const port = fastify.server.address().port
322
+
323
+ const httpClient = net.createConnection({ port }, () => {
324
+ httpClient.write('GET / HTTP/1.1\r\nHOST: localhost\r\n\r\n')
325
+ httpClient.once('data', data => {
326
+ t.assert.match(data.toString(), /hi from handler/i)
327
+ httpClient.end()
328
+ })
329
+ })
330
+
331
+ const wsClient = net.createConnection({ port }, () => {
332
+ wsClient.write('GET / HTTP/1.1\r\nConnection: upgrade\r\nUpgrade: websocket\r\nSec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\nSec-WebSocket-Version: 13\r\n\r\n')
333
+ // The 101 response and the first websocket frame are separate writes, so
334
+ // they can arrive in separate chunks; accumulate instead of asserting on
335
+ // the first chunk only.
336
+ let received = ''
337
+ const onData = data => {
338
+ received += data.toString()
339
+ if (!/hi from wsHandler/i.test(received)) {
340
+ return
341
+ }
342
+ wsClient.removeListener('data', onData)
343
+ t.assert.match(received, /hi from wsHandler/i)
344
+ wsClient.end(() => {
345
+ t.assert.ok(true)
346
+ setTimeout(end, 100)
347
+ })
348
+ }
349
+ wsClient.on('data', onData)
350
+ })
351
+ })
352
+ })
353
+
354
+ test('Should call the wildcard handler if a no other non-websocket route with path exists', (t, end) => {
355
+ const fastify = Fastify()
356
+ t.after(() => fastify.close())
357
+
358
+ fastify.register(fastifyWebsocket)
359
+
360
+ fastify.register(async function (fastify) {
361
+ fastify.get('/*', { websocket: true }, (socket) => {
362
+ t.assert.ok('called', 'wildcard handler')
363
+ socket.close()
364
+ t.after(() => socket.terminate())
365
+ })
366
+
367
+ fastify.get('/http', (_request, reply) => {
368
+ t.assert.fail('Should not call http handler')
369
+ reply.send('http route')
370
+ })
371
+ })
372
+
373
+ fastify.listen({ port: 0 }, err => {
374
+ t.assert.ifError(err)
375
+ const ws = new WebSocket('ws://localhost:' + (fastify.server.address()).port + '/http2')
376
+ const client = WebSocket.createWebSocketStream(ws, { encoding: 'utf8' })
377
+ t.after(() => client.destroy())
378
+
379
+ client.setEncoding('utf8')
380
+ client.end(end)
381
+ })
382
+ })
383
+
384
+ test('Should close the connection if a non-websocket route with path exists', (t, end) => {
385
+ t.plan(2)
386
+ const fastify = Fastify()
387
+ t.after(() => fastify.close())
388
+
389
+ fastify.register(fastifyWebsocket)
390
+ fastify.register(async function (fastify) {
391
+ fastify.get('/*', { websocket: true }, (socket) => {
392
+ t.assert.fail('called', 'wildcard handler')
393
+ t.after(() => socket.terminate())
394
+ })
395
+
396
+ fastify.get('/http', (_request, reply) => {
397
+ t.assert.fail('Should not call /http handler')
398
+ reply.send('http route')
399
+ })
400
+ })
401
+
402
+ fastify.listen({ port: 0 }, err => {
403
+ t.assert.ifError(err)
404
+ const ws = new WebSocket('ws://localhost:' + (fastify.server.address()).port + '/http')
405
+ ws.on('close', (code) => {
406
+ t.assert.deepStrictEqual(code, 1005, 'closed websocket')
407
+ end()
408
+ })
409
+ })
410
+ })
411
+
412
+ test('Should throw on wrong HTTP method', (t, end) => {
413
+ t.plan(2)
414
+ const fastify = Fastify()
415
+
416
+ t.after(() => fastify.close())
417
+
418
+ fastify.register(fastifyWebsocket)
419
+ fastify.register(async function (fastify) {
420
+ fastify.post('/echo', { websocket: true }, (socket) => {
421
+ socket.on('message', message => {
422
+ try {
423
+ socket.send(message)
424
+ } catch (err) {
425
+ socket.send(err.message)
426
+ }
427
+ })
428
+ t.after(() => socket.terminate())
429
+ })
430
+
431
+ fastify.get('/http', (_request, reply) => {
432
+ t.assert.fail('Should not call /http handler')
433
+ reply.send('http route')
434
+ })
435
+ })
436
+
437
+ fastify.listen({ port: 0 }, (err) => {
438
+ t.assert.ok(err)
439
+ t.assert.deepStrictEqual(err.message, 'websocket handler can only be declared in GET method')
440
+ end()
441
+ })
442
+ })
443
+
444
+ test('Should accept an array of GET methods', async t => {
445
+ for (const method of [['GET'], ['GET', 'HEAD']]) {
446
+ const fastify = Fastify()
447
+ t.after(() => fastify.close())
448
+
449
+ await fastify.register(fastifyWebsocket)
450
+ fastify.route({
451
+ method,
452
+ url: '/echo',
453
+ websocket: true,
454
+ handler: (socket) => {
455
+ socket.once('message', message => socket.send(message))
456
+ }
457
+ })
458
+ await fastify.ready()
459
+
460
+ const ws = await fastify.injectWS('/echo')
461
+ const reply = new Promise(resolve => ws.once('message', data => resolve(data.toString())))
462
+ ws.send('hi')
463
+ t.assert.strictEqual(await reply, 'hi')
464
+ ws.terminate()
465
+
466
+ const head = await fastify.inject({ method: 'HEAD', url: '/echo' })
467
+ t.assert.strictEqual(head.statusCode, 404)
468
+ }
469
+ })
470
+
471
+ test('Should reply 404 to HEAD requests on a websocket route', async t => {
472
+ for (const method of ['GET', 'HEAD']) {
473
+ const fastify = Fastify()
474
+ t.after(() => fastify.close())
475
+
476
+ await fastify.register(fastifyWebsocket)
477
+ fastify.route({
478
+ method,
479
+ url: '/echo',
480
+ websocket: true,
481
+ handler: (socket) => {
482
+ socket.once('message', message => socket.send(message))
483
+ }
484
+ })
485
+
486
+ const head = await fastify.inject({ method: 'HEAD', url: '/echo' })
487
+ t.assert.strictEqual(head.statusCode, 404)
488
+ }
489
+ })
490
+
491
+ test('Should use the HTTP handler for HEAD requests on a route with a wsHandler', async t => {
492
+ const fastify = Fastify()
493
+ t.after(() => fastify.close())
494
+
495
+ await fastify.register(fastifyWebsocket)
496
+ fastify.route({
497
+ method: 'GET',
498
+ url: '/echo',
499
+ handler: (_, reply) => {
500
+ reply.header('x-handler', 'http').send('hello')
501
+ },
502
+ wsHandler: (socket) => {
503
+ socket.close()
504
+ }
505
+ })
506
+
507
+ const head = await fastify.inject({ method: 'HEAD', url: '/echo' })
508
+ t.assert.strictEqual(head.statusCode, 200)
509
+ t.assert.strictEqual(head.headers['x-handler'], 'http')
510
+ })
511
+
512
+ test('Should throw on an array with a non-GET method', async t => {
513
+ const fastify = Fastify()
514
+ t.after(() => fastify.close())
515
+
516
+ await fastify.register(fastifyWebsocket)
517
+ t.assert.throws(() => fastify.route({
518
+ method: ['GET', 'POST'],
519
+ url: '/echo',
520
+ websocket: true,
521
+ handler: () => {}
522
+ }), { message: 'websocket handler can only be declared in GET method' })
523
+ })
524
+
525
+ test('Should throw on invalid wsHandler', async t => {
526
+ t.plan(1)
527
+ const fastify = Fastify()
528
+
529
+ t.after(() => fastify.close())
530
+
531
+ await fastify.register(fastifyWebsocket)
532
+ try {
533
+ fastify.route({
534
+ method: 'GET',
535
+ url: '/echo',
536
+ handler: (_, reply) => {
537
+ reply.send({ hello: 'world' })
538
+ },
539
+ wsHandler: 'hello'
540
+ }, { prefix: '/baz' })
541
+ } catch (err) {
542
+ t.assert.deepStrictEqual(err.message, 'invalid wsHandler function')
543
+ }
544
+ })
545
+
546
+ test('Should open on registered path', (t, end) => {
547
+ t.plan(2)
548
+ const fastify = Fastify()
549
+
550
+ t.after(() => fastify.close())
551
+
552
+ fastify.register(fastifyWebsocket)
553
+
554
+ fastify.register(async function (fastify) {
555
+ fastify.get('/echo', { websocket: true }, (socket) => {
556
+ socket.on('message', message => {
557
+ try {
558
+ socket.send(message)
559
+ } catch (err) {
560
+ socket.send(err.message)
561
+ }
562
+ })
563
+
564
+ t.after(() => socket.terminate())
565
+ })
566
+ })
567
+
568
+ fastify.listen({ port: 0 }, err => {
569
+ t.assert.ifError(err)
570
+ const ws = new WebSocket('ws://localhost:' + (fastify.server.address()).port + '/echo')
571
+ ws.on('open', () => {
572
+ t.assert.ok(true)
573
+ client.end()
574
+ end()
575
+ })
576
+
577
+ const client = WebSocket.createWebSocketStream(ws, { encoding: 'utf8' })
578
+ t.after(() => client.destroy())
579
+ })
580
+ })
581
+
582
+ test('Should send message and close', (t) => {
583
+ t.plan(5)
584
+ const fastify = Fastify()
585
+
586
+ t.after(() => fastify.close())
587
+
588
+ fastify.register(fastifyWebsocket)
589
+
590
+ const { promise: clientPromise, resolve: clientResolve } = withResolvers()
591
+ const { promise: serverPromise, resolve: serverResolve } = withResolvers()
592
+
593
+ fastify.register(async function (fastify) {
594
+ fastify.get('/', { websocket: true }, (socket) => {
595
+ socket.on('message', message => {
596
+ t.assert.deepStrictEqual(message.toString(), 'hi from client')
597
+ socket.send('hi from server')
598
+ })
599
+
600
+ socket.on('close', () => {
601
+ t.assert.ok(true)
602
+ serverResolve()
603
+ })
604
+
605
+ t.after(() => socket.terminate())
606
+ })
607
+ })
608
+
609
+ fastify.listen({ port: 0 }, err => {
610
+ t.assert.ifError(err)
611
+ const ws = new WebSocket('ws://localhost:' + (fastify.server.address()).port + '/')
612
+ const client = WebSocket.createWebSocketStream(ws, { encoding: 'utf8' })
613
+ t.after(() => client.destroy())
614
+
615
+ ws.on('message', message => {
616
+ t.assert.deepStrictEqual(message.toString(), 'hi from server')
617
+ })
618
+
619
+ ws.on('open', () => {
620
+ ws.send('hi from client')
621
+ client.end()
622
+ })
623
+
624
+ ws.on('close', () => {
625
+ t.assert.ok(true)
626
+ clientResolve()
627
+ })
628
+ })
629
+
630
+ return Promise.all([clientPromise, serverPromise])
631
+ })
632
+
633
+ test('Should return 404 on http request', (t, end) => {
634
+ const fastify = Fastify()
635
+
636
+ t.after(() => fastify.close())
637
+
638
+ fastify.register(fastifyWebsocket)
639
+ fastify.register(async function (fastify) {
640
+ fastify.get('/', { websocket: true }, (socket) => {
641
+ socket.on('message', message => {
642
+ t.assert.deepStrictEqual(message.toString(), 'hi from client')
643
+ socket.send('hi from server')
644
+ })
645
+
646
+ socket.on('close', () => {
647
+ t.assert.ok(true)
648
+ })
649
+
650
+ t.after(() => socket.terminate())
651
+ })
652
+ })
653
+
654
+ fastify.inject({
655
+ method: 'GET',
656
+ url: '/'
657
+ }).then((response) => {
658
+ t.assert.deepStrictEqual(response.payload, '')
659
+ t.assert.deepStrictEqual(response.statusCode, 404)
660
+ end()
661
+ })
662
+ })
663
+
664
+ test('Should pass route params to per-route handlers', (t, end) => {
665
+ const fastify = Fastify()
666
+
667
+ t.after(() => fastify.close())
668
+
669
+ fastify.register(fastifyWebsocket)
670
+ fastify.register(async function (fastify) {
671
+ fastify.get('/ws', { websocket: true }, (socket, request) => {
672
+ const params = request.params
673
+ t.assert.deepStrictEqual(Object.keys(params).length, 0, 'params are empty')
674
+ socket.send('empty')
675
+ socket.close()
676
+ })
677
+ fastify.get('/ws/:id', { websocket: true }, (socket, request) => {
678
+ const params = request.params
679
+ t.assert.deepStrictEqual(params.id, 'foo', 'params are correct')
680
+ socket.send(params.id)
681
+ socket.close()
682
+ })
683
+ })
684
+
685
+ fastify.listen({ port: 0 }, err => {
686
+ let pending = 2
687
+ t.assert.ifError(err)
688
+ const ws = new WebSocket(
689
+ 'ws://localhost:' + (fastify.server.address()).port + '/ws/foo'
690
+ )
691
+ const client = WebSocket.createWebSocketStream(ws, { encoding: 'utf8' })
692
+ const ws2 = new WebSocket(
693
+ 'ws://localhost:' + (fastify.server.address()).port + '/ws'
694
+ )
695
+ const client2 = WebSocket.createWebSocketStream(ws2, { encoding: 'utf8' })
696
+ t.after(() => client.destroy())
697
+ t.after(() => client2.destroy())
698
+
699
+ client.setEncoding('utf8')
700
+ client2.setEncoding('utf8')
701
+
702
+ client.once('data', chunk => {
703
+ t.assert.deepStrictEqual(chunk, 'foo')
704
+ client.end()
705
+ if (--pending === 0) end()
706
+ })
707
+ client2.once('data', chunk => {
708
+ t.assert.deepStrictEqual(chunk, 'empty')
709
+ client2.end()
710
+ if (--pending === 0) end()
711
+ })
712
+ })
713
+ })
714
+
715
+ test('Should not throw error when register empty get with prefix', (t, end) => {
716
+ const fastify = Fastify()
717
+
718
+ t.after(() => fastify.close())
719
+
720
+ fastify.register(fastifyWebsocket)
721
+
722
+ fastify.register(
723
+ function (instance, _opts, next) {
724
+ instance.get('/', { websocket: true }, (socket) => {
725
+ socket.on('message', message => {
726
+ t.assert.deepStrictEqual(message.toString(), 'hi from client')
727
+ socket.send('hi from server')
728
+ })
729
+ })
730
+ next()
731
+ },
732
+ { prefix: '/baz' }
733
+ )
734
+
735
+ fastify.listen({ port: 0 }, err => {
736
+ if (err) t.assert.ifError(err)
737
+
738
+ const ws = new WebSocket(
739
+ 'ws://localhost:' + fastify.server.address().port + '/baz/'
740
+ )
741
+
742
+ ws.on('open', () => {
743
+ t.assert.ok('Done')
744
+ ws.close()
745
+ end()
746
+ })
747
+ })
748
+ })
749
+
750
+ test('Should expose fastify instance to websocket per-route handler', (t, end) => {
751
+ const fastify = Fastify()
752
+
753
+ t.after(() => fastify.close())
754
+
755
+ fastify.register(fastifyWebsocket)
756
+ fastify.register(async function (fastify) {
757
+ fastify.get('/ws', { websocket: true }, function wsHandler (socket) {
758
+ t.assert.deepStrictEqual(this, fastify, 'this is bound to fastify server')
759
+ socket.send('empty')
760
+ socket.close()
761
+ })
762
+ })
763
+
764
+ fastify.listen({ port: 0 }, err => {
765
+ t.assert.ifError(err)
766
+ const ws = new WebSocket(
767
+ 'ws://localhost:' + (fastify.server.address()).port + '/ws'
768
+ )
769
+ const client = WebSocket.createWebSocketStream(ws, { encoding: 'utf8' })
770
+ t.after(() => client.destroy())
771
+
772
+ client.setEncoding('utf8')
773
+
774
+ client.once('data', chunk => {
775
+ t.assert.deepStrictEqual(chunk, 'empty')
776
+ client.end()
777
+ end()
778
+ })
779
+ })
780
+ })
781
+
782
+ test('Should have access to decorators in per-route handler', (t, end) => {
783
+ const fastify = Fastify()
784
+
785
+ t.after(() => fastify.close())
786
+
787
+ fastify.decorateRequest('str', 'it works!')
788
+ fastify.register(fastifyWebsocket)
789
+ fastify.register(async function (fastify) {
790
+ fastify.get('/ws', { websocket: true }, function wsHandler (socket, request) {
791
+ t.assert.deepStrictEqual(request.str, 'it works!', 'decorator is accessible')
792
+ socket.send('empty')
793
+ socket.close()
794
+ })
795
+ })
796
+
797
+ fastify.listen({ port: 0 }, err => {
798
+ t.assert.ifError(err)
799
+ const ws = new WebSocket('ws://localhost:' + (fastify.server.address()).port + '/ws')
800
+ const client = WebSocket.createWebSocketStream(ws, { encoding: 'utf8' })
801
+ t.after(() => client.destroy())
802
+
803
+ client.once('data', chunk => {
804
+ t.assert.deepStrictEqual(chunk, 'empty')
805
+ client.end()
806
+ end()
807
+ })
808
+ })
809
+ })
810
+
811
+ test('should call `destroy` when exception is thrown inside async handler', (t, end) => {
812
+ t.plan(2)
813
+ const fastify = Fastify()
814
+
815
+ t.after(() => fastify.close())
816
+
817
+ fastify.register(fastifyWebsocket)
818
+ fastify.register(async function (fastify) {
819
+ fastify.get('/ws', { websocket: true }, async function wsHandler (socket) {
820
+ socket.on('close', code => {
821
+ t.assert.deepStrictEqual(code, 1006)
822
+ end()
823
+ })
824
+ throw new Error('something wrong')
825
+ })
826
+ })
827
+
828
+ fastify.listen({ port: 0 }, err => {
829
+ t.assert.ifError(err)
830
+ const ws = new WebSocket(
831
+ 'ws://localhost:' + (fastify.server.address()).port + '/ws'
832
+ )
833
+ const client = WebSocket.createWebSocketStream(ws, { encoding: 'utf8' })
834
+
835
+ client.on('error', (_) => { })
836
+ t.after(() => client.destroy())
837
+ })
838
+ })
839
+
840
+ test('should call default non websocket fastify route when no match is found', (t, end) => {
841
+ t.plan(2)
842
+ const fastify = Fastify()
843
+
844
+ t.after(() => fastify.close())
845
+
846
+ fastify.register(fastifyWebsocket)
847
+ fastify.register(async function (fastify) {
848
+ fastify.get('/ws', function handler (_request, reply) {
849
+ reply.send({ hello: 'world' })
850
+ })
851
+ })
852
+
853
+ fastify.listen({ port: 0 }, err => {
854
+ t.assert.ifError(err)
855
+ get('http://localhost:' + (fastify.server.address()).port + '/wrong-route', function (response) {
856
+ t.assert.deepStrictEqual(response.statusCode, 404)
857
+ end()
858
+ })
859
+ })
860
+ })
861
+
862
+ test('register a non websocket route', (t, end) => {
863
+ t.plan(2)
864
+ const fastify = Fastify()
865
+
866
+ t.after(() => fastify.close())
867
+
868
+ fastify.register(fastifyWebsocket)
869
+ fastify.register(async function (fastify) {
870
+ fastify.get('/ws', function handler (_request, reply) {
871
+ reply.send({ hello: 'world' })
872
+ })
873
+ })
874
+
875
+ fastify.listen({ port: 0 }, err => {
876
+ t.assert.ifError(err)
877
+ get('http://localhost:' + (fastify.server.address()).port + '/ws', function (response) {
878
+ let data = ''
879
+
880
+ response.on('data', (chunk) => {
881
+ data += chunk
882
+ })
883
+
884
+ response.on('end', () => {
885
+ t.assert.deepStrictEqual(data, '{"hello":"world"}')
886
+ end()
887
+ })
888
+ })
889
+ })
890
+ })