@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
Binary file
@@ -12,8 +12,9 @@
12
12
  href="./assets/teammy-y-standard-dark-C4LRaRpE.svg"
13
13
  media="(prefers-color-scheme: dark)"
14
14
  />
15
- <script type="module" crossorigin src="./assets/index-xjf37um4.js"></script>
16
- <link rel="stylesheet" crossorigin href="./assets/index-C4EXuXVY.css">
15
+ <script type="module" crossorigin src="./assets/index-DbOamGWI.js"></script>
16
+ <link rel="modulepreload" crossorigin href="./assets/Dropdown-BVXg1qjS.js">
17
+ <link rel="stylesheet" crossorigin href="./assets/index-4y82i1SF.css">
17
18
  </head>
18
19
  <body>
19
20
  <div id="root" data-teammy-ui-version="11"></div>
@@ -0,0 +1,2 @@
1
+ # Set default behavior to automatically convert line endings
2
+ * text=auto eol=lf
@@ -0,0 +1,53 @@
1
+ version: 2
2
+ updates:
3
+ - package-ecosystem: "github-actions"
4
+ directory: "/"
5
+ commit-message:
6
+ # Prefix all commit messages with "chore: "
7
+ prefix: "chore"
8
+ schedule:
9
+ interval: "weekly"
10
+ cooldown:
11
+ default-days: 7
12
+ allow:
13
+ - dependency-name: "*"
14
+ update-types:
15
+ - "version-update:semver-major"
16
+ open-pull-requests-limit: 10
17
+
18
+ - package-ecosystem: "npm"
19
+ directory: "/"
20
+ commit-message:
21
+ # Prefix all commit messages with "chore: "
22
+ prefix: "chore"
23
+ schedule:
24
+ interval: "weekly"
25
+ cooldown:
26
+ default-days: 7
27
+ versioning-strategy: "increase-if-necessary"
28
+ allow:
29
+ - dependency-name: "*"
30
+ update-types:
31
+ - "version-update:semver-major"
32
+ ignore:
33
+ # TODO: remove ignore until neostandard support ESLint 10
34
+ - dependency-name: "eslint"
35
+ - dependency-name: "neostandard"
36
+ - dependency-name: "@stylistic/*"
37
+ open-pull-requests-limit: 10
38
+ groups:
39
+ # Production dependencies with breaking changes
40
+ dependencies:
41
+ dependency-type: "production"
42
+ # ESLint related dependencies
43
+ dev-dependencies-eslint:
44
+ patterns:
45
+ - "eslint"
46
+ - "neostandard"
47
+ - "@stylistic/*"
48
+ # TypeScript related dependencies
49
+ dev-dependencies-typescript:
50
+ patterns:
51
+ - "@types/*"
52
+ - "tstyche"
53
+ - "typescript"
@@ -0,0 +1,33 @@
1
+ name: CI
2
+
3
+ on:
4
+ push:
5
+ branches:
6
+ - main
7
+ - next
8
+ - 'v*'
9
+ paths-ignore:
10
+ - 'docs/**'
11
+ - '*.md'
12
+ pull_request:
13
+ paths-ignore:
14
+ - 'docs/**'
15
+ - '*.md'
16
+
17
+ # This allows a subsequently queued workflow run to interrupt previous runs
18
+ concurrency:
19
+ group: "${{ github.workflow }}-${{ github.event.pull_request.head.label || github.head_ref || github.ref }}"
20
+ cancel-in-progress: true
21
+
22
+ permissions:
23
+ contents: read
24
+
25
+ jobs:
26
+ test:
27
+ permissions:
28
+ contents: write
29
+ pull-requests: write
30
+ uses: fastify/workflows/.github/workflows/plugins-ci.yml@ef591e2186785d5ab36b9fe6a79c7ce2f1d94e57 # v7.0.0
31
+ with:
32
+ license-check: true
33
+ lint: true
@@ -0,0 +1,19 @@
1
+ name: Lock Threads
2
+
3
+ on:
4
+ schedule:
5
+ - cron: '0 0 1 * *'
6
+ workflow_dispatch:
7
+
8
+ concurrency:
9
+ group: lock
10
+
11
+ permissions:
12
+ contents: read
13
+
14
+ jobs:
15
+ lock-threads:
16
+ permissions:
17
+ issues: write
18
+ pull-requests: write
19
+ uses: fastify/workflows/.github/workflows/lock-threads.yml@ef591e2186785d5ab36b9fe6a79c7ce2f1d94e57 # v7.0.0
@@ -0,0 +1,24 @@
1
+ name: Release the package
2
+
3
+ on:
4
+ workflow_dispatch:
5
+ inputs:
6
+ semver:
7
+ description: 'Release bump type'
8
+ required: true
9
+ type: choice
10
+ options:
11
+ - patch
12
+ - minor
13
+ - major
14
+
15
+ permissions: {}
16
+
17
+ jobs:
18
+ release:
19
+ permissions:
20
+ id-token: write # required for npm provenance via OIDC
21
+ contents: write # required to push the release commit and the tag
22
+ uses: fastify/workflows/.github/workflows/reusable-release.yml@731d0cca9a6a5b9586dad6b0326d49001ab9216f
23
+ with:
24
+ semver: ${{ inputs.semver }}
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2017-present The Fastify team <https://github.com/fastify/fastify#team>
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,393 @@
1
+ # @fastify/websocket
2
+
3
+ [![CI](https://github.com/fastify/fastify-websocket/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/fastify/fastify-websocket/actions/workflows/ci.yml)
4
+ [![NPM version](https://img.shields.io/npm/v/@fastify/websocket.svg?style=flat)](https://www.npmjs.com/package/@fastify/websocket)
5
+ [![neostandard javascript style](https://img.shields.io/badge/code_style-neostandard-brightgreen?style=flat)](https://github.com/neostandard/neostandard)
6
+
7
+ WebSocket support for [Fastify](https://github.com/fastify/fastify).
8
+ Built upon [ws@8](https://www.npmjs.com/package/ws).
9
+
10
+ ## Install
11
+
12
+ ```shell
13
+ npm i @fastify/websocket
14
+ # or
15
+ yarn add @fastify/websocket
16
+ ```
17
+
18
+ If you're a TypeScript user, this package has its own TypeScript types built in, but you will also need to install the types for the `ws` package:
19
+
20
+ ```shell
21
+ npm i @types/ws -D
22
+ # or
23
+ yarn add -D @types/ws
24
+ ```
25
+
26
+ If you use TypeScript and Yarn 2, you'll need to add a `packageExtension` to your `.yarnrc.yml` file:
27
+
28
+ ```yaml
29
+ packageExtensions:
30
+ "@fastify/websocket@*":
31
+ peerDependencies:
32
+ fastify: "*"
33
+ ```
34
+
35
+ ## Usage
36
+
37
+ After registering this plugin, you can choose on which routes the WS server will respond. This can be achieved by adding `websocket: true` property to `routeOptions` on a fastify's `.get` route. In this case, two arguments will be passed to the handler, the socket connection, and the `fastify` request object:
38
+
39
+ ```js
40
+ 'use strict'
41
+
42
+ const fastify = require('fastify')()
43
+ fastify.register(require('@fastify/websocket'))
44
+ fastify.register(async function (fastify) {
45
+ fastify.get('/', { websocket: true }, (socket /* WebSocket */, req /* FastifyRequest */) => {
46
+ socket.on('message', message => {
47
+ // message.toString() === 'hi from client'
48
+ socket.send('hi from server')
49
+ })
50
+ })
51
+ })
52
+
53
+ fastify.listen({ port: 3000 }, err => {
54
+ if (err) {
55
+ fastify.log.error(err)
56
+ process.exit(1)
57
+ }
58
+ })
59
+ ```
60
+
61
+ In this case, it will respond with a 404 error on every unregistered route, closing the incoming upgrade connection requests.
62
+
63
+ However, you can still define a wildcard route, that will be used as the default handler:
64
+
65
+ ```js
66
+ 'use strict'
67
+
68
+ const fastify = require('fastify')()
69
+
70
+ fastify.register(require('@fastify/websocket'), {
71
+ options: { maxPayload: 1048576 }
72
+ })
73
+
74
+ fastify.register(async function (fastify) {
75
+ fastify.get('/*', { websocket: true }, (socket /* WebSocket */, req /* FastifyRequest */) => {
76
+ socket.on('message', message => {
77
+ // message.toString() === 'hi from client'
78
+ socket.send('hi from wildcard route')
79
+ })
80
+ })
81
+
82
+ fastify.get('/', { websocket: true }, (socket /* WebSocket */, req /* FastifyRequest */) => {
83
+ socket.on('message', message => {
84
+ // message.toString() === 'hi from client'
85
+ socket.send('hi from server')
86
+ })
87
+ })
88
+ })
89
+
90
+ fastify.listen({ port: 3000 }, err => {
91
+ if (err) {
92
+ fastify.log.error(err)
93
+ process.exit(1)
94
+ }
95
+ })
96
+ ```
97
+
98
+ ### Attaching event handlers
99
+ Websocket route handlers must attach event handlers synchronously during handler execution to avoid accidentally dropping messages. If you want to do any async work in your websocket handler, say to authenticate a user or load data from a datastore, ensure you attach any `on('message')` handlers *before* you trigger this async work. Otherwise, messages might arrive whilst this async work is underway, and if there is no handler listening for this data it will be silently dropped.
100
+
101
+ Here is an example of how to attach message handlers synchronously while still accessing asynchronous resources. We store a promise for the async thing in a local variable, attach the message handler synchronously, and then make the message handler itself asynchronous to grab the async data and do some processing:
102
+
103
+ ```javascript
104
+ fastify.get('/*', { websocket: true }, (socket, request) => {
105
+ const sessionPromise = request.getSession() // example async session getter, called synchronously to return a promise
106
+
107
+ socket.on('message', async (message) => {
108
+ const session = await sessionPromise()
109
+ // do something with the message and session
110
+ })
111
+ })
112
+ ```
113
+ ### Using hooks
114
+
115
+ Routes registered with `@fastify/websocket` respect the Fastify plugin encapsulation contexts. Hooks that run **before** the websocket connection is established will be called - this includes `onRequest`, `preParsing`, `preValidation`, and `preHandler`. These hooks can be used for authentication or other request-level processing.
116
+
117
+ However, hooks related to response serialization and transmission (`preSerialization`, `onSend`) **do not run** for websocket routes. Once the connection is upgraded, message handling is outside Fastify's HTTP lifecycle. If you need to transform outgoing websocket messages, implement that logic in your handler before calling `socket.send()`.
118
+
119
+ ```js
120
+ fastify.addHook('preValidation', async (request, reply) => {
121
+ // check if the request is authenticated
122
+ if (!request.isAuthenticated()) {
123
+ await reply.code(401).send("not authenticated");
124
+ }
125
+ })
126
+ fastify.get('/', { websocket: true }, (socket, req) => {
127
+ // the connection will only be opened for authenticated incoming requests
128
+ socket.on('message', message => {
129
+ // ...
130
+ })
131
+ })
132
+ ```
133
+
134
+ **NB**
135
+ This plugin uses the same router as the `fastify` instance, this has a few implications to take into account:
136
+ - Websocket route handlers follow the usual `fastify` request lifecycle, which means hooks, error handlers, and decorators all work the same way as other route handlers.
137
+ - You can access the fastify server via `this` in your handlers
138
+ - When using `@fastify/websocket`, it needs to be registered before all routes in order to be able to intercept websocket connections to existing routes and close the connection on non-websocket routes.
139
+
140
+ ```js
141
+ import Fastify from 'fastify'
142
+ import websocket from '@fastify/websocket'
143
+
144
+ const fastify = Fastify()
145
+ await fastify.register(websocket)
146
+
147
+ fastify.get('/', { websocket: true }, function wsHandler (socket, req) {
148
+ // bound to fastify server
149
+ this.myDecoration.someFunc()
150
+
151
+ socket.on('message', message => {
152
+ // message.toString() === 'hi from client'
153
+ socket.send('hi from server')
154
+ })
155
+ })
156
+
157
+ await fastify.listen({ port: 3000 })
158
+ ```
159
+
160
+ If you need to handle both HTTP requests and incoming socket connections on the same route, you can still do it using the [full declaration syntax](https://fastify.dev/docs/latest/Reference/Routes/#full-declaration), adding a `wsHandler` property.
161
+
162
+ ```js
163
+ 'use strict'
164
+
165
+ const fastify = require('fastify')()
166
+
167
+ function handle (socket, req) {
168
+ socket.on('message', (data) => socket.send(data)) // creates an echo server
169
+ }
170
+
171
+ fastify.register(require('@fastify/websocket'), {
172
+ handle,
173
+ options: { maxPayload: 1048576 }
174
+ })
175
+
176
+ fastify.register(async function () {
177
+ fastify.route({
178
+ method: 'GET',
179
+ url: '/hello',
180
+ handler: (req, reply) => {
181
+ // this will handle http requests
182
+ reply.send({ hello: 'world' })
183
+ },
184
+ wsHandler: (socket, req) => {
185
+ // this will handle websockets connections
186
+ socket.send('hello client')
187
+
188
+ socket.once('message', chunk => {
189
+ socket.close()
190
+ })
191
+ }
192
+ })
193
+ })
194
+
195
+ fastify.listen({ port: 3000 }, err => {
196
+ if (err) {
197
+ fastify.log.error(err)
198
+ process.exit(1)
199
+ }
200
+ })
201
+ ```
202
+
203
+ ### Custom error handler:
204
+
205
+ You can optionally provide a custom `errorHandler` that will be used to handle any cleaning up of established websocket connections. The `errorHandler` will be called if any errors are thrown by your websocket route handler after the connection has been established, as well as for `error` events emitted by the established websocket connection itself (for example when a client disconnects uncleanly), so applications can classify expected disconnect errors instead of having them logged unconditionally at the error level. Note that neither Fastify's `onError` hook or functions registered with `fastify.setErrorHandler` will be called for errors thrown during a websocket request handler.
206
+
207
+ Neither the `errorHandler` passed to this plugin or fastify's `onError` hook will be called for errors encountered during message processing for your connection. If you want to handle unexpected errors within your `message` event handlers, you'll need to use your own `try { } catch {}` statements and decide what to send back over the websocket.
208
+
209
+ ```js
210
+ const fastify = require('fastify')()
211
+
212
+ fastify.register(require('@fastify/websocket'), {
213
+ errorHandler: function (error, socket /* WebSocket */, req /* FastifyRequest */, reply /* FastifyReply */) {
214
+ // Do stuff
215
+ // destroy/close connection
216
+ socket.terminate()
217
+ },
218
+ options: {
219
+ maxPayload: 1048576, // we set the maximum allowed messages size to 1 MiB (1024 bytes * 1024 bytes)
220
+ verifyClient: function (info, next) {
221
+ if (info.req.headers['x-fastify-header'] !== 'fastify is awesome !') {
222
+ return next(false) // the connection is not allowed
223
+ }
224
+ next(true) // the connection is allowed
225
+ }
226
+ }
227
+ })
228
+
229
+ fastify.get('/', { websocket: true }, (socket /* WebSocket */, req /* FastifyRequest */) => {
230
+ socket.on('message', message => {
231
+ // message.toString() === 'hi from client'
232
+ socket.send('hi from server')
233
+ })
234
+ })
235
+
236
+ fastify.listen({ port: 3000 }, err => {
237
+ if (err) {
238
+ fastify.log.error(err)
239
+ process.exit(1)
240
+ }
241
+ })
242
+ ```
243
+
244
+ Note: Fastify's `onError` and error handlers registered by `setErrorHandler` will still be called for errors encountered *before* the websocket connection is established. This means errors thrown by `onRequest` hooks, `preValidation` handlers, and hooks registered by plugins will use the normal error handling mechanisms in Fastify. Once the websocket is established and your websocket route handler is called, `fastify-websocket`'s `errorHandler` takes over.
245
+
246
+ ### Custom preClose hook:
247
+
248
+ By default, all ws connections are closed when the server closes. If you wish to modify this behavior, you can pass your own `preClose` function.
249
+
250
+ Note that `preClose` is responsible for closing all connections and closing the websocket server.
251
+
252
+ ```js
253
+ const fastify = require('fastify')()
254
+
255
+ fastify.register(require('@fastify/websocket'), {
256
+ preClose: (done) => { // Note: can also use async style, without done-callback
257
+ const server = this.websocketServer
258
+
259
+ for (const socket of server.clients) {
260
+ socket.close(1001, 'WS server is going offline in custom manner, sending a code + message')
261
+ }
262
+
263
+ server.close(done)
264
+ }
265
+ })
266
+ ```
267
+
268
+ ### Creating a stream from the WebSocket
269
+
270
+ ```js
271
+ const Fastify = require('fastify')
272
+ const FastifyWebSocket = require('@fastify/websocket')
273
+ const ws = require('ws')
274
+
275
+ const fastify = Fastify()
276
+ await fastify.register(FastifyWebSocket)
277
+
278
+ fastify.get('/', { websocket: true }, (socket, req) => {
279
+ const stream = ws.createWebSocketStream(socket, { /* options */ })
280
+ stream.setEncoding('utf8')
281
+ stream.write('hello client')
282
+
283
+ stream.on('data', function (data) {
284
+ // Make sure to set up a data handler or read all the incoming
285
+ // data in another way, otherwise stream backpressure will cause
286
+ // the underlying WebSocket object to get paused.
287
+ })
288
+ })
289
+
290
+ await fastify.listen({ port: 3000 })
291
+ ```
292
+
293
+ ### Testing
294
+
295
+ Testing the ws handler can be quite tricky, luckily `fastify-websocket` decorates fastify instance with `injectWS`,
296
+ which allows easy testing of a websocket endpoint.
297
+
298
+ The signature of injectWS is the following: `([path], [upgradeContext])`.
299
+
300
+ #### App.js
301
+
302
+ ```js
303
+ 'use strict'
304
+
305
+ const Fastify = require('fastify')
306
+ const FastifyWebSocket = require('@fastify/websocket')
307
+
308
+ const App = Fastify()
309
+
310
+ App.register(FastifyWebSocket);
311
+
312
+ App.register(async function(fastify) {
313
+ fastify.addHook('preValidation', async (request, reply) => {
314
+ if (request.headers['api-key'] !== 'some-random-key') {
315
+ return reply.code(401).send()
316
+ }
317
+ })
318
+
319
+ fastify.get('/', { websocket: true }, (socket) => {
320
+ socket.on('message', message => {
321
+ socket.send('hi from server')
322
+ })
323
+ })
324
+ })
325
+
326
+ module.exports = App
327
+ ```
328
+
329
+ #### App.test.js
330
+
331
+ ```js
332
+ 'use strict'
333
+
334
+ const { test } = require('node:test')
335
+ const fastify = require('./app.js')
336
+
337
+ test('connect to /', async (t) => {
338
+ t.plan(1)
339
+
340
+ t.after(() => fastify.close())
341
+ await fastify.ready()
342
+
343
+ const ws = await fastify.injectWS('/', {headers: { "api-key" : "some-random-key" }})
344
+
345
+ let resolve;
346
+ const promise = new Promise(r => { resolve = r })
347
+
348
+ ws.on('message', (data) => {
349
+ resolve(data.toString());
350
+ })
351
+ ws.send('hi from client')
352
+
353
+ t.assert.deepStrictEqual(await promise, 'hi from server')
354
+ // Remember to close the ws at the end
355
+ ws.terminate()
356
+ })
357
+ ```
358
+
359
+ #### Things to know
360
+ - Websocket needs to be closed manually at the end of each test.
361
+ - `fastify.ready()` needs to be awaited to ensure that fastify has been decorated.
362
+ - You need to register the event listener before sending the message if you need to process the server response.
363
+
364
+ ## Options
365
+
366
+ `@fastify/websocket` accept these options for [`ws`](https://github.com/websockets/ws/blob/master/doc/ws.md#new-websocketserveroptions-callback) :
367
+
368
+ - `host` - The hostname where to bind the server.
369
+ - `port` - The port where to bind the server.
370
+ - `backlog` - The maximum length of the queue of pending connections.
371
+ - `server` - A pre-created Node.js HTTP/S server.
372
+ - `verifyClient` - A function that can be used to validate incoming connections.
373
+ - `handleProtocols` - A function that can be used to handle the WebSocket subprotocols.
374
+ - `clientTracking` - Specifies whether or not to track clients.
375
+ - `perMessageDeflate` - Enable/disable permessage-deflate.
376
+ - `maxPayload` - The maximum allowed message size in bytes.
377
+
378
+ For more information, you can check [`ws` options documentation](https://github.com/websockets/ws/blob/master/doc/ws.md#new-websocketserveroptions-callback).
379
+
380
+ _**NB** By default if you do not provide a `server` option `@fastify/websocket` will bind your websocket server instance to the scoped `fastify` instance._
381
+
382
+ _**NB** The `path` option from `ws` should not be provided since the routing is handled by fastify itself_
383
+
384
+ _**NB** The `noServer` option from `ws` should not be provided since the point of @fastify/websocket is to listen on the fastify server. If you want a custom server, you can use the `server` option, and if you want more control, you can use the `ws` library directly_
385
+
386
+ [ws](https://github.com/websockets/ws) does not allow you to set `objectMode` or `writableObjectMode` to true
387
+ ## Acknowledgments
388
+
389
+ This project is kindly sponsored by [nearForm](https://nearform.com).
390
+
391
+ ## License
392
+
393
+ Licensed under [MIT](./LICENSE).
@@ -0,0 +1,6 @@
1
+ 'use strict'
2
+
3
+ module.exports = require('neostandard')({
4
+ ignores: require('neostandard').resolveIgnoresFromGitignore(),
5
+ ts: true
6
+ })