@teammy/server 0.2.8 → 0.2.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/main.js +3744 -1673
- package/dist/web/assets/Assistant-C9Zo5FN_.js +1 -0
- package/dist/web/assets/Computers-DNIWiHu4.js +5 -0
- package/dist/web/assets/Computers-XUdWIkZr.css +1 -0
- package/dist/web/assets/{Conversation-CNmGkVy7.css → Conversation-BVNmvuHR.css} +1 -1
- package/dist/web/assets/Conversation-DiVjLVVt.js +1 -0
- package/dist/web/assets/Dashboard-C6M9c0SB.js +1 -0
- package/dist/web/assets/Dashboard-ChX6TZDn.css +1 -0
- package/dist/web/assets/Dropdown-BnYLBw2s.js +55 -0
- package/dist/web/assets/Files-CQcAEYH9.js +1 -0
- package/dist/web/assets/{Help-DwUoT8ZM.js → Help-CDOqVRjJ.js} +158 -37
- package/dist/web/assets/HtmlPreview-BzvYKdju.js +1 -0
- package/dist/web/assets/{Markdown-DorC2YjE.js → Markdown-zJQlHcvy.js} +4 -4
- package/dist/web/assets/MessageComposer-CBI6RiJq.js +1 -0
- package/dist/web/assets/MessageComposer-CJGxkvML.css +1 -0
- package/dist/web/assets/ResourceLibrary-CEFCe7QW.js +6 -0
- package/dist/web/assets/TerminalPane-BrSy-0jI.css +1 -0
- package/dist/web/assets/TerminalPane-GU69G9Ak.js +22 -0
- package/dist/web/assets/WorkspaceTree-BhDZe1go.js +1 -0
- package/dist/web/assets/file-links-V3R--z0b.js +1 -0
- package/dist/web/assets/index-D9m13C7l.js +12 -0
- package/dist/web/assets/index-qX1GRw9I.css +1 -0
- package/dist/web/assets/teammy-logo-dark-GROHRRMa.svg +6 -0
- package/dist/web/assets/teammy-logo-light-B6BoNVcp.svg +6 -0
- package/dist/web/assets/teammy-y-standard-dark-C4LRaRpE.svg +6 -0
- package/dist/web/assets/teammy-y-standard-light-Bl5D78Uj.svg +6 -0
- package/dist/web/downloads/teammy-inbox-skill.zip +0 -0
- package/dist/web/downloads/teammy-skill.zip +0 -0
- package/dist/web/index.html +10 -3
- package/node_modules/@fastify/websocket/.gitattributes +2 -0
- package/node_modules/@fastify/websocket/.github/dependabot.yml +53 -0
- package/node_modules/@fastify/websocket/.github/workflows/ci.yml +33 -0
- package/node_modules/@fastify/websocket/.github/workflows/lock-threads.yml +19 -0
- package/node_modules/@fastify/websocket/.github/workflows/release.yml +24 -0
- package/node_modules/@fastify/websocket/LICENSE +21 -0
- package/node_modules/@fastify/websocket/README.md +393 -0
- package/node_modules/@fastify/websocket/eslint.config.js +6 -0
- package/node_modules/@fastify/websocket/index.js +276 -0
- package/node_modules/@fastify/websocket/package.json +79 -0
- package/node_modules/@fastify/websocket/test/base.test.js +785 -0
- package/node_modules/@fastify/websocket/test/hooks.test.js +414 -0
- package/node_modules/@fastify/websocket/test/inject.test.js +174 -0
- package/node_modules/@fastify/websocket/test/router.test.js +890 -0
- package/node_modules/@fastify/websocket/tsconfig.json +3 -0
- package/node_modules/@fastify/websocket/types/index.d.ts +128 -0
- package/node_modules/@fastify/websocket/types/index.tst.ts +261 -0
- package/node_modules/duplexify/.travis.yml +9 -0
- package/node_modules/duplexify/LICENSE +21 -0
- package/node_modules/duplexify/README.md +97 -0
- package/node_modules/duplexify/example.js +21 -0
- package/node_modules/duplexify/index.js +238 -0
- package/node_modules/duplexify/package.json +39 -0
- package/node_modules/duplexify/test.js +339 -0
- package/node_modules/end-of-stream/LICENSE +21 -0
- package/node_modules/end-of-stream/README.md +54 -0
- package/node_modules/end-of-stream/index.js +96 -0
- package/node_modules/end-of-stream/package.json +37 -0
- package/node_modules/once/LICENSE +15 -0
- package/node_modules/once/README.md +79 -0
- package/node_modules/once/once.js +42 -0
- package/node_modules/once/package.json +33 -0
- package/node_modules/readable-stream/CONTRIBUTING.md +38 -0
- package/node_modules/readable-stream/GOVERNANCE.md +136 -0
- package/node_modules/readable-stream/LICENSE +47 -0
- package/node_modules/readable-stream/README.md +106 -0
- package/node_modules/readable-stream/errors-browser.js +127 -0
- package/node_modules/readable-stream/errors.js +116 -0
- package/node_modules/readable-stream/experimentalWarning.js +17 -0
- package/node_modules/readable-stream/lib/_stream_duplex.js +126 -0
- package/node_modules/readable-stream/lib/_stream_passthrough.js +37 -0
- package/node_modules/readable-stream/lib/_stream_readable.js +1027 -0
- package/node_modules/readable-stream/lib/_stream_transform.js +190 -0
- package/node_modules/readable-stream/lib/_stream_writable.js +641 -0
- package/node_modules/readable-stream/lib/internal/streams/async_iterator.js +180 -0
- package/node_modules/readable-stream/lib/internal/streams/buffer_list.js +183 -0
- package/node_modules/readable-stream/lib/internal/streams/destroy.js +96 -0
- package/node_modules/readable-stream/lib/internal/streams/end-of-stream.js +86 -0
- package/node_modules/readable-stream/lib/internal/streams/from-browser.js +3 -0
- package/node_modules/readable-stream/lib/internal/streams/from.js +52 -0
- package/node_modules/readable-stream/lib/internal/streams/pipeline.js +86 -0
- package/node_modules/readable-stream/lib/internal/streams/state.js +22 -0
- package/node_modules/readable-stream/lib/internal/streams/stream-browser.js +1 -0
- package/node_modules/readable-stream/lib/internal/streams/stream.js +1 -0
- package/node_modules/readable-stream/package.json +68 -0
- package/node_modules/readable-stream/readable-browser.js +9 -0
- package/node_modules/readable-stream/readable.js +16 -0
- package/node_modules/stream-shift/.github/workflows/test.yml +23 -0
- package/node_modules/stream-shift/LICENSE +21 -0
- package/node_modules/stream-shift/README.md +25 -0
- package/node_modules/stream-shift/index.js +21 -0
- package/node_modules/stream-shift/package.json +25 -0
- package/node_modules/stream-shift/test.js +48 -0
- package/node_modules/string_decoder/LICENSE +48 -0
- package/node_modules/string_decoder/README.md +47 -0
- package/node_modules/string_decoder/lib/string_decoder.js +296 -0
- package/node_modules/string_decoder/package.json +34 -0
- package/node_modules/util-deprecate/History.md +16 -0
- package/node_modules/util-deprecate/LICENSE +24 -0
- package/node_modules/util-deprecate/README.md +53 -0
- package/node_modules/util-deprecate/browser.js +67 -0
- package/node_modules/util-deprecate/node.js +6 -0
- package/node_modules/util-deprecate/package.json +27 -0
- package/node_modules/wrappy/LICENSE +15 -0
- package/node_modules/wrappy/README.md +36 -0
- package/node_modules/wrappy/package.json +29 -0
- package/node_modules/wrappy/wrappy.js +33 -0
- package/package.json +15 -2
- package/dist/web/assets/Assistant-CYSa4zNi.js +0 -1
- package/dist/web/assets/Computers-4L48XuFg.js +0 -4
- package/dist/web/assets/Computers-_ugR447I.css +0 -1
- package/dist/web/assets/Conversation-BBWm5w-C.js +0 -1
- package/dist/web/assets/Dashboard-Bdh00JcC.css +0 -1
- package/dist/web/assets/Dashboard-Ck2NsAbX.js +0 -1
- package/dist/web/assets/Files-CjNjoKS3.js +0 -1
- package/dist/web/assets/HtmlPreview-B6aQIaMB.js +0 -1
- package/dist/web/assets/MessageComposer-DQBDG5yF.js +0 -1
- package/dist/web/assets/MessageComposer-DavJdQPa.css +0 -1
- package/dist/web/assets/ResourceLibrary-104NuVRn.js +0 -6
- package/dist/web/assets/WorkspaceTree-B4FMRlCw.js +0 -1
- package/dist/web/assets/file-links-DnngiTbC.js +0 -1
- package/dist/web/assets/index-6PvvMCD7.css +0 -1
- package/dist/web/assets/index-DiR0ePCJ.js +0 -66
- package/dist/web/assets/teammy-calm-DVuoOK0F.png +0 -0
- package/dist/web/assets/teammy-favicon-BiDvlPj2.png +0 -0
- package/dist/web/assets/teammy-mark-CxBtF1-X.png +0 -0
- package/dist/web/assets/teammy-wave-CVTgecuC.png +0 -0
- package/dist/web/assets/teammy-wordmark-ZFwxG0yl.png +0 -0
|
@@ -0,0 +1,414 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const { test } = require('node:test')
|
|
4
|
+
const net = require('node:net')
|
|
5
|
+
const Fastify = require('fastify')
|
|
6
|
+
const fastifyWebsocket = require('..')
|
|
7
|
+
const WebSocket = require('ws')
|
|
8
|
+
const split = require('split2')
|
|
9
|
+
|
|
10
|
+
test('Should run onRequest, preValidation, preHandler hooks', (t, end) => {
|
|
11
|
+
t.plan(8)
|
|
12
|
+
const fastify = Fastify()
|
|
13
|
+
|
|
14
|
+
t.after(() => fastify.close())
|
|
15
|
+
|
|
16
|
+
fastify.register(fastifyWebsocket)
|
|
17
|
+
|
|
18
|
+
fastify.register(async function (fastify) {
|
|
19
|
+
fastify.addHook('onRequest', async ({ routeOptions: { schema: { hide } } }) => {
|
|
20
|
+
t.assert.ok('called', 'onRequest')
|
|
21
|
+
t.assert.strictEqual(hide, true, 'schema hide property should be set to true when route option is websocket')
|
|
22
|
+
})
|
|
23
|
+
fastify.addHook('preParsing', async () => t.assert.ok('called', 'preParsing'))
|
|
24
|
+
fastify.addHook('preValidation', async () => t.assert.ok('called', 'preValidation'))
|
|
25
|
+
fastify.addHook('preHandler', async () => t.assert.ok('called', 'preHandler'))
|
|
26
|
+
|
|
27
|
+
fastify.get('/echo', { websocket: true }, (socket) => {
|
|
28
|
+
socket.send('hello client')
|
|
29
|
+
t.after(() => socket.terminate())
|
|
30
|
+
|
|
31
|
+
socket.once('message', (chunk) => {
|
|
32
|
+
t.assert.deepStrictEqual(chunk.toString(), 'hello server')
|
|
33
|
+
end()
|
|
34
|
+
})
|
|
35
|
+
})
|
|
36
|
+
})
|
|
37
|
+
|
|
38
|
+
fastify.listen({ port: 0 }, err => {
|
|
39
|
+
t.assert.ifError(err)
|
|
40
|
+
const ws = new WebSocket('ws://localhost:' + (fastify.server.address()).port + '/echo')
|
|
41
|
+
const client = WebSocket.createWebSocketStream(ws, { encoding: 'utf8' })
|
|
42
|
+
t.after(() => client.destroy())
|
|
43
|
+
|
|
44
|
+
client.setEncoding('utf8')
|
|
45
|
+
client.write('hello server')
|
|
46
|
+
|
|
47
|
+
client.once('data', chunk => {
|
|
48
|
+
t.assert.deepStrictEqual(chunk, 'hello client')
|
|
49
|
+
client.end()
|
|
50
|
+
})
|
|
51
|
+
})
|
|
52
|
+
})
|
|
53
|
+
|
|
54
|
+
test('Should not run onTimeout hook', (t, end) => {
|
|
55
|
+
t.plan(2)
|
|
56
|
+
const fastify = Fastify()
|
|
57
|
+
|
|
58
|
+
t.after(() => fastify.close())
|
|
59
|
+
|
|
60
|
+
fastify.register(fastifyWebsocket)
|
|
61
|
+
|
|
62
|
+
fastify.register(async function () {
|
|
63
|
+
fastify.addHook('onTimeout', async () => t.assert.fail('called', 'onTimeout'))
|
|
64
|
+
|
|
65
|
+
fastify.get('/echo', { websocket: true }, (socket, request) => {
|
|
66
|
+
socket.send('hello client')
|
|
67
|
+
request.raw.setTimeout(50)
|
|
68
|
+
t.after(() => socket.terminate())
|
|
69
|
+
})
|
|
70
|
+
})
|
|
71
|
+
|
|
72
|
+
fastify.listen({ port: 0 }, err => {
|
|
73
|
+
t.assert.ifError(err)
|
|
74
|
+
const ws = new WebSocket('ws://localhost:' + (fastify.server.address()).port + '/echo')
|
|
75
|
+
const client = WebSocket.createWebSocketStream(ws, { encoding: 'utf8' })
|
|
76
|
+
t.after(() => client.destroy())
|
|
77
|
+
|
|
78
|
+
client.once('data', chunk => {
|
|
79
|
+
t.assert.deepStrictEqual(chunk, 'hello client')
|
|
80
|
+
end()
|
|
81
|
+
})
|
|
82
|
+
})
|
|
83
|
+
})
|
|
84
|
+
|
|
85
|
+
test('Should run onError hook before handler is executed (error thrown in onRequest hook)', (t, end) => {
|
|
86
|
+
t.plan(3)
|
|
87
|
+
const fastify = Fastify()
|
|
88
|
+
|
|
89
|
+
t.after(() => fastify.close())
|
|
90
|
+
|
|
91
|
+
fastify.register(fastifyWebsocket)
|
|
92
|
+
|
|
93
|
+
fastify.register(async function (fastify) {
|
|
94
|
+
fastify.addHook('onRequest', async () => { throw new Error('Fail') })
|
|
95
|
+
fastify.addHook('onError', async () => t.assert.ok('called', 'onError'))
|
|
96
|
+
|
|
97
|
+
fastify.get('/echo', { websocket: true }, () => {
|
|
98
|
+
t.assert.fail()
|
|
99
|
+
})
|
|
100
|
+
})
|
|
101
|
+
|
|
102
|
+
fastify.listen({ port: 0 }, function (err) {
|
|
103
|
+
t.assert.ifError(err)
|
|
104
|
+
const ws = new WebSocket('ws://localhost:' + (fastify.server.address()).port + '/echo')
|
|
105
|
+
ws.on('unexpected-response', (_request, response) => {
|
|
106
|
+
t.assert.deepStrictEqual(response.statusCode, 500)
|
|
107
|
+
end()
|
|
108
|
+
})
|
|
109
|
+
})
|
|
110
|
+
})
|
|
111
|
+
|
|
112
|
+
test('Should run onError hook before handler is executed (error thrown in preValidation hook)', (t, end) => {
|
|
113
|
+
t.plan(3)
|
|
114
|
+
const fastify = Fastify()
|
|
115
|
+
|
|
116
|
+
t.after(() => fastify.close())
|
|
117
|
+
|
|
118
|
+
fastify.register(fastifyWebsocket)
|
|
119
|
+
|
|
120
|
+
fastify.register(async function (fastify) {
|
|
121
|
+
fastify.addHook('preValidation', async () => {
|
|
122
|
+
await Promise.resolve()
|
|
123
|
+
throw new Error('Fail')
|
|
124
|
+
})
|
|
125
|
+
|
|
126
|
+
fastify.addHook('onError', async () => t.assert.ok('called', 'onError'))
|
|
127
|
+
|
|
128
|
+
fastify.get('/echo', { websocket: true }, () => {
|
|
129
|
+
t.assert.fail()
|
|
130
|
+
})
|
|
131
|
+
})
|
|
132
|
+
|
|
133
|
+
fastify.listen({ port: 0 }, function (err) {
|
|
134
|
+
t.assert.ifError(err)
|
|
135
|
+
const ws = new WebSocket('ws://localhost:' + (fastify.server.address()).port + '/echo')
|
|
136
|
+
ws.on('unexpected-response', (_request, response) => {
|
|
137
|
+
t.assert.deepStrictEqual(response.statusCode, 500)
|
|
138
|
+
end()
|
|
139
|
+
})
|
|
140
|
+
})
|
|
141
|
+
})
|
|
142
|
+
|
|
143
|
+
test('onError hooks can send a reply and prevent hijacking', (t, end) => {
|
|
144
|
+
t.plan(3)
|
|
145
|
+
const fastify = Fastify()
|
|
146
|
+
|
|
147
|
+
t.after(() => fastify.close())
|
|
148
|
+
|
|
149
|
+
fastify.register(fastifyWebsocket)
|
|
150
|
+
|
|
151
|
+
fastify.register(async function (fastify) {
|
|
152
|
+
fastify.addHook('preValidation', async () => {
|
|
153
|
+
await Promise.resolve()
|
|
154
|
+
throw new Error('Fail')
|
|
155
|
+
})
|
|
156
|
+
|
|
157
|
+
fastify.addHook('onError', async (_request, reply) => {
|
|
158
|
+
t.assert.ok('called', 'onError')
|
|
159
|
+
await reply.code(501).send('there was an error')
|
|
160
|
+
})
|
|
161
|
+
|
|
162
|
+
fastify.get('/echo', { websocket: true }, () => {
|
|
163
|
+
t.assert.fail()
|
|
164
|
+
})
|
|
165
|
+
})
|
|
166
|
+
|
|
167
|
+
fastify.listen({ port: 0 }, function (err) {
|
|
168
|
+
t.assert.ifError(err)
|
|
169
|
+
const ws = new WebSocket('ws://localhost:' + (fastify.server.address()).port + '/echo')
|
|
170
|
+
ws.on('unexpected-response', (_request, response) => {
|
|
171
|
+
t.assert.deepStrictEqual(response.statusCode, 501)
|
|
172
|
+
end()
|
|
173
|
+
})
|
|
174
|
+
})
|
|
175
|
+
})
|
|
176
|
+
|
|
177
|
+
test('setErrorHandler functions can send a reply and prevent hijacking', (t, end) => {
|
|
178
|
+
t.plan(4)
|
|
179
|
+
const fastify = Fastify()
|
|
180
|
+
|
|
181
|
+
t.after(() => fastify.close())
|
|
182
|
+
|
|
183
|
+
fastify.register(fastifyWebsocket)
|
|
184
|
+
|
|
185
|
+
fastify.register(async function (fastify) {
|
|
186
|
+
fastify.addHook('preValidation', async () => {
|
|
187
|
+
await Promise.resolve()
|
|
188
|
+
throw new Error('Fail')
|
|
189
|
+
})
|
|
190
|
+
|
|
191
|
+
fastify.setErrorHandler(async (error, _request, reply) => {
|
|
192
|
+
t.assert.ok('called', 'onError')
|
|
193
|
+
t.assert.ok(error)
|
|
194
|
+
await reply.code(501).send('there was an error')
|
|
195
|
+
})
|
|
196
|
+
|
|
197
|
+
fastify.get('/echo', { websocket: true }, () => {
|
|
198
|
+
t.assert.fail()
|
|
199
|
+
})
|
|
200
|
+
})
|
|
201
|
+
|
|
202
|
+
fastify.listen({ port: 0 }, function (err) {
|
|
203
|
+
t.assert.ifError(err)
|
|
204
|
+
const ws = new WebSocket('ws://localhost:' + (fastify.server.address()).port + '/echo')
|
|
205
|
+
ws.on('unexpected-response', (_request, response) => {
|
|
206
|
+
t.assert.deepStrictEqual(response.statusCode, 501)
|
|
207
|
+
end()
|
|
208
|
+
})
|
|
209
|
+
})
|
|
210
|
+
})
|
|
211
|
+
|
|
212
|
+
test('Should not run onError hook if reply was already hijacked (error thrown in websocket handler)', (t, end) => {
|
|
213
|
+
t.plan(2)
|
|
214
|
+
const fastify = Fastify()
|
|
215
|
+
|
|
216
|
+
t.after(() => fastify.close())
|
|
217
|
+
|
|
218
|
+
fastify.register(fastifyWebsocket)
|
|
219
|
+
|
|
220
|
+
fastify.register(async function (fastify) {
|
|
221
|
+
fastify.addHook('onError', async () => t.assert.fail('called', 'onError'))
|
|
222
|
+
|
|
223
|
+
fastify.get('/echo', { websocket: true }, async (socket) => {
|
|
224
|
+
t.after(() => socket.terminate())
|
|
225
|
+
throw new Error('Fail')
|
|
226
|
+
})
|
|
227
|
+
})
|
|
228
|
+
|
|
229
|
+
fastify.listen({ port: 0 }, function (err) {
|
|
230
|
+
t.assert.ifError(err)
|
|
231
|
+
const ws = new WebSocket('ws://localhost:' + (fastify.server.address()).port + '/echo')
|
|
232
|
+
const client = WebSocket.createWebSocketStream(ws, { encoding: 'utf8' })
|
|
233
|
+
t.after(() => client.destroy())
|
|
234
|
+
ws.on('close', code => {
|
|
235
|
+
t.assert.deepStrictEqual(code, 1006)
|
|
236
|
+
end()
|
|
237
|
+
})
|
|
238
|
+
})
|
|
239
|
+
})
|
|
240
|
+
|
|
241
|
+
test('Should not run preSerialization/onSend hooks', (t, end) => {
|
|
242
|
+
t.plan(2)
|
|
243
|
+
const fastify = Fastify()
|
|
244
|
+
|
|
245
|
+
t.after(() => fastify.close())
|
|
246
|
+
|
|
247
|
+
fastify.register(fastifyWebsocket)
|
|
248
|
+
|
|
249
|
+
fastify.register(async function (fastify) {
|
|
250
|
+
fastify.addHook('onSend', async () => t.assert.fail('called', 'onSend'))
|
|
251
|
+
fastify.addHook('preSerialization', async () => t.assert.fail('called', 'preSerialization'))
|
|
252
|
+
|
|
253
|
+
fastify.get('/echo', { websocket: true }, async (socket) => {
|
|
254
|
+
socket.send('hello client')
|
|
255
|
+
t.after(() => socket.terminate())
|
|
256
|
+
})
|
|
257
|
+
})
|
|
258
|
+
|
|
259
|
+
fastify.listen({ port: 0 }, err => {
|
|
260
|
+
t.assert.ifError(err)
|
|
261
|
+
const ws = new WebSocket('ws://localhost:' + (fastify.server.address()).port + '/echo')
|
|
262
|
+
const client = WebSocket.createWebSocketStream(ws, { encoding: 'utf8' })
|
|
263
|
+
t.after(() => client.destroy())
|
|
264
|
+
|
|
265
|
+
client.once('data', chunk => {
|
|
266
|
+
t.assert.deepStrictEqual(chunk, 'hello client')
|
|
267
|
+
client.end()
|
|
268
|
+
end()
|
|
269
|
+
})
|
|
270
|
+
})
|
|
271
|
+
})
|
|
272
|
+
|
|
273
|
+
test('Should not hijack reply for a normal http request in the internal onError hook', (t, end) => {
|
|
274
|
+
t.plan(2)
|
|
275
|
+
const fastify = Fastify()
|
|
276
|
+
|
|
277
|
+
t.after(() => fastify.close())
|
|
278
|
+
|
|
279
|
+
fastify.register(fastifyWebsocket)
|
|
280
|
+
|
|
281
|
+
fastify.register(async function (fastify) {
|
|
282
|
+
fastify.get('/', async () => {
|
|
283
|
+
throw new Error('Fail')
|
|
284
|
+
})
|
|
285
|
+
})
|
|
286
|
+
|
|
287
|
+
fastify.listen({ port: 0 }, err => {
|
|
288
|
+
t.assert.ifError(err)
|
|
289
|
+
|
|
290
|
+
const port = fastify.server.address().port
|
|
291
|
+
|
|
292
|
+
const httpClient = net.createConnection({ port }, () => {
|
|
293
|
+
t.after(() => httpClient.destroy())
|
|
294
|
+
|
|
295
|
+
httpClient.write('GET / HTTP/1.1\r\nHOST: localhost\r\n\r\n')
|
|
296
|
+
httpClient.once('data', data => {
|
|
297
|
+
t.assert.match(data.toString(), /Fail/i)
|
|
298
|
+
end()
|
|
299
|
+
})
|
|
300
|
+
httpClient.end()
|
|
301
|
+
})
|
|
302
|
+
})
|
|
303
|
+
})
|
|
304
|
+
|
|
305
|
+
test('Should run async hooks and still deliver quickly sent messages', (t, end) => {
|
|
306
|
+
t.plan(3)
|
|
307
|
+
const fastify = Fastify()
|
|
308
|
+
|
|
309
|
+
t.after(() => fastify.close())
|
|
310
|
+
|
|
311
|
+
fastify.register(fastifyWebsocket)
|
|
312
|
+
|
|
313
|
+
fastify.register(async function (fastify) {
|
|
314
|
+
fastify.addHook(
|
|
315
|
+
'preValidation',
|
|
316
|
+
async () => await new Promise((resolve) => setTimeout(resolve, 25))
|
|
317
|
+
)
|
|
318
|
+
|
|
319
|
+
fastify.get('/echo', { websocket: true }, (socket) => {
|
|
320
|
+
socket.send('hello client')
|
|
321
|
+
t.after(() => socket.terminate())
|
|
322
|
+
|
|
323
|
+
socket.on('message', (message) => {
|
|
324
|
+
t.assert.deepStrictEqual(message.toString('utf-8'), 'hello server')
|
|
325
|
+
end()
|
|
326
|
+
})
|
|
327
|
+
})
|
|
328
|
+
})
|
|
329
|
+
|
|
330
|
+
fastify.listen({ port: 0 }, (err) => {
|
|
331
|
+
t.assert.ifError(err)
|
|
332
|
+
const ws = new WebSocket(
|
|
333
|
+
'ws://localhost:' + fastify.server.address().port + '/echo'
|
|
334
|
+
)
|
|
335
|
+
const client = WebSocket.createWebSocketStream(ws, { encoding: 'utf8' })
|
|
336
|
+
t.after(() => client.destroy())
|
|
337
|
+
|
|
338
|
+
client.setEncoding('utf8')
|
|
339
|
+
client.write('hello server')
|
|
340
|
+
|
|
341
|
+
client.once('data', (chunk) => {
|
|
342
|
+
t.assert.deepStrictEqual(chunk, 'hello client')
|
|
343
|
+
client.end()
|
|
344
|
+
})
|
|
345
|
+
})
|
|
346
|
+
})
|
|
347
|
+
|
|
348
|
+
test('Should not hijack reply for an normal request to a websocket route that is sent a normal HTTP response in a hook', (t, end) => {
|
|
349
|
+
t.plan(2)
|
|
350
|
+
const fastify = Fastify()
|
|
351
|
+
t.after(() => fastify.close())
|
|
352
|
+
|
|
353
|
+
fastify.register(fastifyWebsocket)
|
|
354
|
+
fastify.register(async function (fastify) {
|
|
355
|
+
fastify.addHook('preValidation', async (_request, reply) => {
|
|
356
|
+
await Promise.resolve()
|
|
357
|
+
await reply.code(404).send('not found')
|
|
358
|
+
})
|
|
359
|
+
fastify.get('/echo', { websocket: true }, () => {
|
|
360
|
+
t.assert.fail()
|
|
361
|
+
})
|
|
362
|
+
})
|
|
363
|
+
|
|
364
|
+
fastify.listen({ port: 0 }, err => {
|
|
365
|
+
t.assert.ifError(err)
|
|
366
|
+
|
|
367
|
+
const port = fastify.server.address().port
|
|
368
|
+
|
|
369
|
+
const httpClient = net.createConnection({ port }, () => {
|
|
370
|
+
t.after(() => httpClient.destroy())
|
|
371
|
+
httpClient.write('GET /echo HTTP/1.1\r\nHOST: localhost\r\n\r\n')
|
|
372
|
+
httpClient.once('data', data => {
|
|
373
|
+
t.assert.match(data.toString(), /not found/i)
|
|
374
|
+
end()
|
|
375
|
+
})
|
|
376
|
+
httpClient.end()
|
|
377
|
+
})
|
|
378
|
+
})
|
|
379
|
+
})
|
|
380
|
+
|
|
381
|
+
test('Should not hijack reply for an WS request to a WS route that gets sent a normal HTTP response in a hook', (t, end) => {
|
|
382
|
+
t.plan(2)
|
|
383
|
+
const stream = split(JSON.parse)
|
|
384
|
+
const fastify = Fastify({ logger: { stream } })
|
|
385
|
+
|
|
386
|
+
fastify.register(fastifyWebsocket)
|
|
387
|
+
fastify.register(async function (fastify) {
|
|
388
|
+
fastify.addHook('preValidation', async (_request, reply) => {
|
|
389
|
+
await reply.code(404).send('not found')
|
|
390
|
+
})
|
|
391
|
+
fastify.get('/echo', { websocket: true }, () => {
|
|
392
|
+
t.assert.fail()
|
|
393
|
+
})
|
|
394
|
+
})
|
|
395
|
+
|
|
396
|
+
stream.on('data', (chunk) => {
|
|
397
|
+
if (chunk.level >= 50) {
|
|
398
|
+
t.assert.fail()
|
|
399
|
+
}
|
|
400
|
+
})
|
|
401
|
+
|
|
402
|
+
fastify.listen({ port: 0 }, err => {
|
|
403
|
+
t.assert.ifError(err)
|
|
404
|
+
|
|
405
|
+
const ws = new WebSocket('ws://localhost:' + (fastify.server.address()).port + '/echo')
|
|
406
|
+
|
|
407
|
+
ws.on('error', error => {
|
|
408
|
+
t.assert.ok(error)
|
|
409
|
+
ws.close()
|
|
410
|
+
fastify.close()
|
|
411
|
+
end()
|
|
412
|
+
})
|
|
413
|
+
})
|
|
414
|
+
})
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const { test } = require('node:test')
|
|
4
|
+
const Fastify = require('fastify')
|
|
5
|
+
const fastifyWebsocket = require('..')
|
|
6
|
+
|
|
7
|
+
function buildFastify (t) {
|
|
8
|
+
const fastify = Fastify()
|
|
9
|
+
t.after(() => { fastify.close() })
|
|
10
|
+
fastify.register(fastifyWebsocket)
|
|
11
|
+
return fastify
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
test('routes correctly the message', async (t) => {
|
|
15
|
+
const fastify = buildFastify(t)
|
|
16
|
+
const message = 'hi from client'
|
|
17
|
+
|
|
18
|
+
let _resolve
|
|
19
|
+
const promise = new Promise((resolve) => { _resolve = resolve })
|
|
20
|
+
|
|
21
|
+
fastify.register(
|
|
22
|
+
async function (instance) {
|
|
23
|
+
instance.get('/ws', { websocket: true }, function (socket) {
|
|
24
|
+
socket.once('message', chunk => {
|
|
25
|
+
_resolve(chunk.toString())
|
|
26
|
+
})
|
|
27
|
+
})
|
|
28
|
+
})
|
|
29
|
+
|
|
30
|
+
await fastify.ready()
|
|
31
|
+
const ws = await fastify.injectWS('/ws')
|
|
32
|
+
ws.send(message)
|
|
33
|
+
t.assert.deepStrictEqual(await promise, message)
|
|
34
|
+
ws.terminate()
|
|
35
|
+
})
|
|
36
|
+
|
|
37
|
+
test('redirect on / if no path specified', async (t) => {
|
|
38
|
+
const fastify = buildFastify(t)
|
|
39
|
+
const message = 'hi from client'
|
|
40
|
+
|
|
41
|
+
let _resolve
|
|
42
|
+
const promise = new Promise((resolve) => { _resolve = resolve })
|
|
43
|
+
|
|
44
|
+
fastify.register(
|
|
45
|
+
async function (instance) {
|
|
46
|
+
instance.get('/', { websocket: true }, function (socket) {
|
|
47
|
+
socket.once('message', chunk => {
|
|
48
|
+
_resolve(chunk.toString())
|
|
49
|
+
})
|
|
50
|
+
})
|
|
51
|
+
})
|
|
52
|
+
|
|
53
|
+
await fastify.ready()
|
|
54
|
+
const ws = await fastify.injectWS()
|
|
55
|
+
ws.send(message)
|
|
56
|
+
t.assert.deepStrictEqual(await promise, message)
|
|
57
|
+
ws.terminate()
|
|
58
|
+
})
|
|
59
|
+
|
|
60
|
+
test('routes correctly the message between two routes', async (t) => {
|
|
61
|
+
const fastify = buildFastify(t)
|
|
62
|
+
const message = 'hi from client'
|
|
63
|
+
|
|
64
|
+
let _resolve
|
|
65
|
+
let _reject
|
|
66
|
+
const promise = new Promise((resolve, reject) => { _resolve = resolve; _reject = reject })
|
|
67
|
+
|
|
68
|
+
fastify.register(
|
|
69
|
+
async function (instance) {
|
|
70
|
+
instance.get('/ws', { websocket: true }, function (socket) {
|
|
71
|
+
socket.once('message', () => {
|
|
72
|
+
_reject('wrong-route')
|
|
73
|
+
})
|
|
74
|
+
})
|
|
75
|
+
|
|
76
|
+
instance.get('/ws-2', { websocket: true }, function (socket) {
|
|
77
|
+
socket.once('message', chunk => {
|
|
78
|
+
_resolve(chunk.toString())
|
|
79
|
+
})
|
|
80
|
+
})
|
|
81
|
+
})
|
|
82
|
+
|
|
83
|
+
await fastify.ready()
|
|
84
|
+
const ws = await fastify.injectWS('/ws-2')
|
|
85
|
+
ws.send(message)
|
|
86
|
+
t.assert.deepStrictEqual(await promise, message)
|
|
87
|
+
ws.terminate()
|
|
88
|
+
})
|
|
89
|
+
|
|
90
|
+
test('use the upgrade context to upgrade if there is some hook', async (t) => {
|
|
91
|
+
const fastify = buildFastify(t)
|
|
92
|
+
const message = 'hi from client'
|
|
93
|
+
|
|
94
|
+
let _resolve
|
|
95
|
+
const promise = new Promise((resolve) => { _resolve = resolve })
|
|
96
|
+
|
|
97
|
+
fastify.register(
|
|
98
|
+
async function (instance) {
|
|
99
|
+
instance.addHook('preValidation', async (request, reply) => {
|
|
100
|
+
if (request.headers['api-key'] !== 'some-random-key') {
|
|
101
|
+
return reply.code(401).send()
|
|
102
|
+
}
|
|
103
|
+
})
|
|
104
|
+
|
|
105
|
+
instance.get('/', { websocket: true }, function (socket) {
|
|
106
|
+
socket.once('message', chunk => {
|
|
107
|
+
_resolve(chunk.toString())
|
|
108
|
+
})
|
|
109
|
+
})
|
|
110
|
+
})
|
|
111
|
+
|
|
112
|
+
await fastify.ready()
|
|
113
|
+
const ws = await fastify.injectWS('/', { headers: { 'api-key': 'some-random-key' } })
|
|
114
|
+
ws.send(message)
|
|
115
|
+
t.assert.deepStrictEqual(await promise, message)
|
|
116
|
+
ws.terminate()
|
|
117
|
+
})
|
|
118
|
+
|
|
119
|
+
test('rejects if the websocket is not upgraded', async (t) => {
|
|
120
|
+
const fastify = buildFastify(t)
|
|
121
|
+
|
|
122
|
+
fastify.register(
|
|
123
|
+
async function (instance) {
|
|
124
|
+
instance.addHook('preValidation', async (_request, reply) => {
|
|
125
|
+
return reply.code(401).send()
|
|
126
|
+
})
|
|
127
|
+
|
|
128
|
+
instance.get('/', { websocket: true }, function () {
|
|
129
|
+
})
|
|
130
|
+
})
|
|
131
|
+
|
|
132
|
+
await fastify.ready()
|
|
133
|
+
await t.assert.rejects(fastify.injectWS('/'), new Error('Unexpected server response: 401'))
|
|
134
|
+
})
|
|
135
|
+
|
|
136
|
+
test('inject hooks', async (t) => {
|
|
137
|
+
const fastify = buildFastify(t)
|
|
138
|
+
const message = 'hi from client'
|
|
139
|
+
|
|
140
|
+
let _resolve
|
|
141
|
+
const promise = new Promise((resolve) => { _resolve = resolve })
|
|
142
|
+
|
|
143
|
+
fastify.register(
|
|
144
|
+
async function (instance) {
|
|
145
|
+
instance.get('/ws', { websocket: true }, function (socket) {
|
|
146
|
+
socket.once('message', chunk => {
|
|
147
|
+
_resolve(chunk.toString())
|
|
148
|
+
})
|
|
149
|
+
})
|
|
150
|
+
})
|
|
151
|
+
|
|
152
|
+
await fastify.ready()
|
|
153
|
+
|
|
154
|
+
let order = 0
|
|
155
|
+
let initWS, openWS
|
|
156
|
+
const ws = await fastify.injectWS('/ws', {}, {
|
|
157
|
+
onInit (ws) {
|
|
158
|
+
t.assert.strictEqual(order, 0)
|
|
159
|
+
order++
|
|
160
|
+
initWS = ws
|
|
161
|
+
},
|
|
162
|
+
onOpen (ws) {
|
|
163
|
+
t.assert.strictEqual(order, 1)
|
|
164
|
+
order++
|
|
165
|
+
openWS = ws
|
|
166
|
+
}
|
|
167
|
+
})
|
|
168
|
+
ws.send(message)
|
|
169
|
+
t.assert.strictEqual(order, 2)
|
|
170
|
+
t.assert.deepStrictEqual(ws, initWS)
|
|
171
|
+
t.assert.deepStrictEqual(ws, openWS)
|
|
172
|
+
t.assert.deepStrictEqual(await promise, message)
|
|
173
|
+
ws.terminate()
|
|
174
|
+
})
|