@capyclaw/rsp-suite 0.0.0-stage → 0.1.1

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 (146) hide show
  1. package/README.md +84 -2
  2. package/cordis.patch.yml +8 -0
  3. package/dsh-suite.json +91 -0
  4. package/node_modules/@capyclaw/foundation/README.md +51 -0
  5. package/node_modules/@capyclaw/foundation/abi/assets.mjs +26 -0
  6. package/node_modules/@capyclaw/foundation/abi/bridge.mjs +88 -0
  7. package/node_modules/@capyclaw/foundation/abi/errors.mjs +63 -0
  8. package/node_modules/@capyclaw/foundation/abi/index.mjs +50 -0
  9. package/node_modules/@capyclaw/foundation/abi/kinds.mjs +271 -0
  10. package/node_modules/@capyclaw/foundation/abi/loader.mjs +352 -0
  11. package/node_modules/@capyclaw/foundation/abi/schema.mjs +98 -0
  12. package/node_modules/@capyclaw/foundation/abi/spec.mjs +258 -0
  13. package/node_modules/@capyclaw/foundation/abi/validate.mjs +24 -0
  14. package/node_modules/@capyclaw/foundation/abi/version.mjs +140 -0
  15. package/node_modules/@capyclaw/foundation/abi/visibility.mjs +90 -0
  16. package/node_modules/@capyclaw/foundation/cordis.patch.yml +5 -0
  17. package/node_modules/@capyclaw/foundation/kit/index.mjs +6 -0
  18. package/node_modules/@capyclaw/foundation/lib/adapters/client-panel.mjs +98 -0
  19. package/node_modules/@capyclaw/foundation/lib/adapters/host-event.mjs +35 -0
  20. package/node_modules/@capyclaw/foundation/lib/adapters/http-router.mjs +69 -0
  21. package/node_modules/@capyclaw/foundation/lib/adapters/lifecycle-hook.mjs +72 -0
  22. package/node_modules/@capyclaw/foundation/lib/adapters/own-server.mjs +113 -0
  23. package/node_modules/@capyclaw/foundation/lib/adapters/raw.mjs +69 -0
  24. package/node_modules/@capyclaw/foundation/lib/adapters/runtime-driver.mjs +120 -0
  25. package/node_modules/@capyclaw/foundation/lib/adapters/service-provider.mjs +77 -0
  26. package/node_modules/@capyclaw/foundation/lib/adapters/skill-provider.mjs +56 -0
  27. package/node_modules/@capyclaw/foundation/lib/adapters/tool-provider.mjs +50 -0
  28. package/node_modules/@capyclaw/foundation/lib/adapters/ui-panel.mjs +64 -0
  29. package/node_modules/@capyclaw/foundation/lib/ctx-service.mjs +42 -0
  30. package/node_modules/@capyclaw/foundation/lib/host/caps.mjs +38 -0
  31. package/node_modules/@capyclaw/foundation/lib/host/contract.mjs +88 -0
  32. package/node_modules/@capyclaw/foundation/lib/host/dsh.mjs +54 -0
  33. package/node_modules/@capyclaw/foundation/lib/host/memory.mjs +187 -0
  34. package/node_modules/@capyclaw/foundation/lib/host.mjs +23 -0
  35. package/node_modules/@capyclaw/foundation/lib/kernel.mjs +195 -0
  36. package/node_modules/@capyclaw/foundation/lib/skill/scan.mjs +115 -0
  37. package/node_modules/@capyclaw/foundation/lib/subsystems/events.mjs +60 -0
  38. package/node_modules/@capyclaw/foundation/lib/subsystems/log.mjs +51 -0
  39. package/node_modules/@capyclaw/foundation/lib/subsystems/net.mjs +198 -0
  40. package/node_modules/@capyclaw/foundation/package.json +53 -0
  41. package/node_modules/@capyclaw/foundation/runtime/index.mjs +20 -0
  42. package/node_modules/@capyclaw/plugin-kit/INTEGRATION.md +100 -0
  43. package/node_modules/@capyclaw/plugin-kit/README.md +70 -0
  44. package/node_modules/@capyclaw/plugin-kit/bin/sync.mjs +103 -0
  45. package/node_modules/@capyclaw/plugin-kit/package.json +72 -0
  46. package/node_modules/@capyclaw/plugin-kit/src/cli.mjs +56 -0
  47. package/node_modules/@capyclaw/plugin-kit/src/config.mjs +142 -0
  48. package/node_modules/@capyclaw/plugin-kit/src/editor/heartbeat-worker.js +132 -0
  49. package/node_modules/@capyclaw/plugin-kit/src/editor/heartbeat.mjs +26 -0
  50. package/node_modules/@capyclaw/plugin-kit/src/editor/lease.mjs +60 -0
  51. package/node_modules/@capyclaw/plugin-kit/src/editor/shadow.mjs +219 -0
  52. package/node_modules/@capyclaw/plugin-kit/src/editor/transport.js +13 -0
  53. package/node_modules/@capyclaw/plugin-kit/src/index.mjs +13 -0
  54. package/node_modules/@capyclaw/plugin-kit/src/ledger.mjs +151 -0
  55. package/node_modules/@capyclaw/plugin-kit/src/pack.mjs +230 -0
  56. package/node_modules/@capyclaw/plugin-kit/src/runtime/context.mjs +128 -0
  57. package/node_modules/@capyclaw/plugin-kit/src/self-contained.mjs +307 -0
  58. package/node_modules/@capyclaw/plugin-kit/src/service/api.mjs +45 -0
  59. package/node_modules/@capyclaw/plugin-kit/src/service/http.mjs +113 -0
  60. package/node_modules/@capyclaw/plugin-kit/src/service/mcp-http.mjs +150 -0
  61. package/node_modules/@capyclaw/plugin-kit/src/service/server.mjs +131 -0
  62. package/node_modules/@capyclaw/plugin-kit/src/service/static.mjs +79 -0
  63. package/node_modules/@capyclaw/plugin-kit/src/skills/provider.mjs +82 -0
  64. package/node_modules/@capyclaw/plugin-kit/src/store/blob-store.mjs +109 -0
  65. package/node_modules/@capyclaw/plugin-kit/src/store/content-store.mjs +17 -0
  66. package/node_modules/@capyclaw/plugin-kit/src/store/journal-store.mjs +89 -0
  67. package/node_modules/@capyclaw/plugin-kit/src/tools/result.mjs +57 -0
  68. package/node_modules/@capyclaw/plugin-kit/src/tools/validate.mjs +150 -0
  69. package/node_modules/@capyclaw/plugin-kit/src/tuning.mjs +82 -0
  70. package/node_modules/@capyclaw/plugin-kit/src/util/fs-lock.mjs +132 -0
  71. package/node_modules/@capyclaw/plugin-kit/src/util/http.mjs +130 -0
  72. package/node_modules/@capyclaw/plugin-kit/src/workspace/manager.mjs +664 -0
  73. package/node_modules/@capyclaw/plugin-kit/src/zip.mjs +181 -0
  74. package/node_modules/@capyclaw/rsp-skill-remote-screen/README.md +7 -0
  75. package/node_modules/@capyclaw/rsp-skill-remote-screen/contract/rsp-contract.json +478 -0
  76. package/node_modules/@capyclaw/rsp-skill-remote-screen/cordis.patch.yml +4 -0
  77. package/node_modules/@capyclaw/rsp-skill-remote-screen/package.json +33 -0
  78. package/node_modules/@capyclaw/rsp-skill-remote-screen/plugin.mjs +15 -0
  79. package/node_modules/@capyclaw/rsp-skill-remote-screen/skills/remote-screen/SKILL.md +131 -0
  80. package/node_modules/@capyclaw/rsp-workbench/contract/capabilities.json +203 -0
  81. package/node_modules/@capyclaw/rsp-workbench/lib/client.mjs +681 -0
  82. package/node_modules/@capyclaw/rsp-workbench/lib/descriptors.mjs +28 -0
  83. package/node_modules/@capyclaw/rsp-workbench/lib/host.mjs +51 -0
  84. package/node_modules/@capyclaw/rsp-workbench/lib/manager.mjs +333 -0
  85. package/node_modules/@capyclaw/rsp-workbench/lib/session-mode.mjs +68 -0
  86. package/node_modules/@capyclaw/rsp-workbench/lib/tools.mjs +485 -0
  87. package/node_modules/@capyclaw/rsp-workbench/lib/workbench.mjs +184 -0
  88. package/node_modules/@capyclaw/rsp-workbench/package.json +47 -0
  89. package/node_modules/@capyclaw/rsp-workbench/plugin.mjs +17 -0
  90. package/node_modules/@capyclaw/rsp-workbench/ui/index.html +578 -0
  91. package/node_modules/@capyclaw/service-host/README.md +86 -0
  92. package/node_modules/@capyclaw/service-host/cordis.patch.yml +4 -0
  93. package/node_modules/@capyclaw/service-host/dsh-suite.json +29 -0
  94. package/node_modules/@capyclaw/service-host/lib/host.mjs +22 -0
  95. package/node_modules/@capyclaw/service-host/package.json +60 -0
  96. package/node_modules/@capyclaw/service-host/src/index.mjs +597 -0
  97. package/node_modules/@capyclaw/service-host/src/tuning.mjs +63 -0
  98. package/node_modules/@capyclaw/suite-kit/README.md +289 -0
  99. package/node_modules/@capyclaw/suite-kit/bin/dsh-domain.mjs +98 -0
  100. package/node_modules/@capyclaw/suite-kit/bin/dsh-suite.mjs +174 -0
  101. package/node_modules/@capyclaw/suite-kit/client/embed.js +35 -0
  102. package/node_modules/@capyclaw/suite-kit/domain.schema.json +129 -0
  103. package/node_modules/@capyclaw/suite-kit/package.json +58 -0
  104. package/node_modules/@capyclaw/suite-kit/src/activation-registry.mjs +184 -0
  105. package/node_modules/@capyclaw/suite-kit/src/artifacts.mjs +131 -0
  106. package/node_modules/@capyclaw/suite-kit/src/capability.mjs +181 -0
  107. package/node_modules/@capyclaw/suite-kit/src/compose.mjs +201 -0
  108. package/node_modules/@capyclaw/suite-kit/src/domain.mjs +138 -0
  109. package/node_modules/@capyclaw/suite-kit/src/embed.mjs +81 -0
  110. package/node_modules/@capyclaw/suite-kit/src/host.mjs +251 -0
  111. package/node_modules/@capyclaw/suite-kit/src/index.mjs +24 -0
  112. package/node_modules/@capyclaw/suite-kit/src/manifest.mjs +263 -0
  113. package/node_modules/@capyclaw/suite-kit/src/orchestration.mjs +100 -0
  114. package/node_modules/@capyclaw/suite-kit/src/profile.mjs +584 -0
  115. package/node_modules/@capyclaw/suite-kit/src/surface.mjs +28 -0
  116. package/node_modules/@capyclaw/suite-kit/suite.schema.json +128 -0
  117. package/node_modules/@capyclaw/tool-host/README.md +56 -0
  118. package/node_modules/@capyclaw/tool-host/cordis.patch.yml +4 -0
  119. package/node_modules/@capyclaw/tool-host/dsh-suite.json +30 -0
  120. package/node_modules/@capyclaw/tool-host/lib/host.mjs +46 -0
  121. package/node_modules/@capyclaw/tool-host/package.json +59 -0
  122. package/node_modules/@capyclaw/tool-host/src/admit-images.mjs +80 -0
  123. package/node_modules/@capyclaw/tool-host/src/caller-session.mjs +25 -0
  124. package/node_modules/@capyclaw/tool-host/src/descriptors.mjs +166 -0
  125. package/node_modules/@capyclaw/tool-host/src/index.mjs +461 -0
  126. package/node_modules/@capyclaw/tool-host/src/mode-gate.mjs +272 -0
  127. package/node_modules/ws/LICENSE +20 -0
  128. package/node_modules/ws/README.md +539 -0
  129. package/node_modules/ws/browser.js +8 -0
  130. package/node_modules/ws/index.js +22 -0
  131. package/node_modules/ws/lib/buffer-util.js +142 -0
  132. package/node_modules/ws/lib/constants.js +19 -0
  133. package/node_modules/ws/lib/event-target.js +292 -0
  134. package/node_modules/ws/lib/extension.js +203 -0
  135. package/node_modules/ws/lib/limiter.js +55 -0
  136. package/node_modules/ws/lib/permessage-deflate.js +530 -0
  137. package/node_modules/ws/lib/receiver.js +751 -0
  138. package/node_modules/ws/lib/sender.js +610 -0
  139. package/node_modules/ws/lib/stream.js +161 -0
  140. package/node_modules/ws/lib/subprotocol.js +62 -0
  141. package/node_modules/ws/lib/validation.js +152 -0
  142. package/node_modules/ws/lib/websocket-server.js +562 -0
  143. package/node_modules/ws/lib/websocket.js +1440 -0
  144. package/node_modules/ws/package.json +74 -0
  145. package/node_modules/ws/wrapper.mjs +21 -0
  146. package/package.json +53 -4
@@ -0,0 +1,539 @@
1
+ # ws: a Node.js WebSocket library
2
+
3
+ [![Version npm](https://img.shields.io/npm/v/ws.svg?logo=npm)](https://www.npmjs.com/package/ws)
4
+ [![CI](https://img.shields.io/github/actions/workflow/status/websockets/ws/ci.yml?branch=master&label=CI&logo=github)](https://github.com/websockets/ws/actions?query=workflow%3ACI+branch%3Amaster)
5
+ [![Coverage Status](https://img.shields.io/coveralls/websockets/ws/master.svg?logo=coveralls)](https://coveralls.io/github/websockets/ws)
6
+
7
+ ws is a simple to use, blazing fast, and thoroughly tested WebSocket client and
8
+ server implementation.
9
+
10
+ Passes the quite extensive Autobahn test suite: [server][server-report],
11
+ [client][client-report].
12
+
13
+ **Note**: This module does not work in the browser. The client in the docs is a
14
+ reference to a backend with the role of a client in the WebSocket communication.
15
+ Browser clients must use the native
16
+ [`WebSocket`](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket)
17
+ object. To make the same code work seamlessly on Node.js and the browser, you
18
+ can use one of the many wrappers available on npm, like
19
+ [isomorphic-ws](https://github.com/heineiuo/isomorphic-ws).
20
+
21
+ ## Table of Contents
22
+
23
+ - [Protocol support](#protocol-support)
24
+ - [Installing](#installing)
25
+ - [Legacy opt-in for performance](#legacy-opt-in-for-performance)
26
+ - [API docs](#api-docs)
27
+ - [WebSocket compression](#websocket-compression)
28
+ - [Usage examples](#usage-examples)
29
+ - [Sending and receiving text data](#sending-and-receiving-text-data)
30
+ - [Sending binary data](#sending-binary-data)
31
+ - [Simple server](#simple-server)
32
+ - [External HTTP/S server](#external-https-server)
33
+ - [Multiple servers sharing a single HTTP/S server](#multiple-servers-sharing-a-single-https-server)
34
+ - [Client authentication](#client-authentication)
35
+ - [Server broadcast](#server-broadcast)
36
+ - [Round-trip time](#round-trip-time)
37
+ - [Use the Node.js streams API](#use-the-nodejs-streams-api)
38
+ - [Other examples](#other-examples)
39
+ - [FAQ](#faq)
40
+ - [How to get the IP address of the client?](#how-to-get-the-ip-address-of-the-client)
41
+ - [How to detect and close broken connections?](#how-to-detect-and-close-broken-connections)
42
+ - [How to connect via a proxy?](#how-to-connect-via-a-proxy)
43
+ - [Changelog](#changelog)
44
+ - [License](#license)
45
+
46
+ ## Protocol support
47
+
48
+ - **HyBi drafts 07-12** (Use the option `protocolVersion: 8`)
49
+ - **HyBi drafts 13-17** (Current default, alternatively option
50
+ `protocolVersion: 13`)
51
+
52
+ ## Installing
53
+
54
+ ```
55
+ npm install ws
56
+ ```
57
+
58
+ ### Legacy opt-in for performance
59
+
60
+ On older Node.js versions, ws can leverage optional binary modules to optimize
61
+ performance.
62
+
63
+ - `npm install --save-optional bufferutil`: Provides binary polyfills for
64
+ [`http.websocketMask()`][] and [`http.websocketUnmask()`][].
65
+ - `npm install --save-optional utf-8-validate`: Provides a binary polyfill for
66
+ [`buffer.isUtf8()`][].
67
+
68
+ On newer Node.js versions (v26.12.0 or later for bufferutil, and
69
+ v18.14.0 or later for utf-8-validate) that feature built-in alternatives, these
70
+ modules are not loaded even if installed.
71
+
72
+ To force ws not to use [bufferutil][] or [utf-8-validate][] on the older Node.js
73
+ versions where they would otherwise be loaded, set the
74
+ [`WS_NO_BUFFER_UTIL`](./doc/ws.md#ws_no_buffer_util) and
75
+ [`WS_NO_UTF_8_VALIDATE`](./doc/ws.md#ws_no_utf_8_validate) environment
76
+ variables. This can be useful to enhance security in systems where a user can
77
+ put a package in the package search path of an application of another user, due
78
+ to how the Node.js resolver algorithm works.
79
+
80
+ ## API docs
81
+
82
+ See [`/doc/ws.md`](./doc/ws.md) for Node.js-like documentation of ws classes and
83
+ utility functions.
84
+
85
+ ## WebSocket compression
86
+
87
+ ws supports the [permessage-deflate extension][permessage-deflate] which enables
88
+ the client and server to negotiate a compression algorithm and its parameters,
89
+ and then selectively apply it to the data payloads of each WebSocket message.
90
+
91
+ The extension is disabled by default on the server and enabled by default on the
92
+ client. It adds a significant overhead in terms of performance and memory
93
+ consumption so we suggest to enable it only if it is really needed.
94
+
95
+ Note that Node.js has a variety of issues with high-performance compression,
96
+ where increased concurrency, especially on Linux, can lead to [catastrophic
97
+ memory fragmentation][node-zlib-bug] and slow performance. If you intend to use
98
+ permessage-deflate in production, it is worthwhile to set up a test
99
+ representative of your workload and ensure Node.js/zlib will handle it with
100
+ acceptable performance and memory usage.
101
+
102
+ Tuning of permessage-deflate can be done via the options defined below. You can
103
+ also use `zlibDeflateOptions` and `zlibInflateOptions`, which is passed directly
104
+ into the creation of [raw deflate/inflate streams][node-zlib-deflaterawdocs].
105
+
106
+ See [the docs][ws-server-options] for more options.
107
+
108
+ ```js
109
+ import WebSocket, { WebSocketServer } from 'ws';
110
+
111
+ const wss = new WebSocketServer({
112
+ port: 8080,
113
+ perMessageDeflate: {
114
+ zlibDeflateOptions: {
115
+ // See zlib defaults.
116
+ chunkSize: 1024,
117
+ memLevel: 7,
118
+ level: 3
119
+ },
120
+ zlibInflateOptions: {
121
+ chunkSize: 10 * 1024
122
+ },
123
+ // Other options settable:
124
+ clientNoContextTakeover: true, // Defaults to negotiated value.
125
+ serverNoContextTakeover: true, // Defaults to negotiated value.
126
+ serverMaxWindowBits: 10, // Defaults to negotiated value.
127
+ // Below options specified as default values.
128
+ concurrencyLimit: 10, // Limits zlib concurrency for perf.
129
+ threshold: 1024 // Size (in bytes) below which messages
130
+ // should not be compressed if context takeover is disabled.
131
+ }
132
+ });
133
+ ```
134
+
135
+ The client will only use the extension if it is supported and enabled on the
136
+ server. To always disable the extension on the client, set the
137
+ `perMessageDeflate` option to `false`.
138
+
139
+ ```js
140
+ import WebSocket from 'ws';
141
+
142
+ const ws = new WebSocket('ws://www.host.com/path', {
143
+ perMessageDeflate: false
144
+ });
145
+ ```
146
+
147
+ ## Usage examples
148
+
149
+ ### Sending and receiving text data
150
+
151
+ ```js
152
+ import WebSocket from 'ws';
153
+
154
+ const ws = new WebSocket('ws://www.host.com/path');
155
+
156
+ ws.on('error', console.error);
157
+
158
+ ws.on('open', function open() {
159
+ ws.send('something');
160
+ });
161
+
162
+ ws.on('message', function message(data) {
163
+ console.log('received: %s', data);
164
+ });
165
+ ```
166
+
167
+ ### Sending binary data
168
+
169
+ ```js
170
+ import WebSocket from 'ws';
171
+
172
+ const ws = new WebSocket('ws://www.host.com/path');
173
+
174
+ ws.on('error', console.error);
175
+
176
+ ws.on('open', function open() {
177
+ const array = new Float32Array(5);
178
+
179
+ for (var i = 0; i < array.length; ++i) {
180
+ array[i] = i / 2;
181
+ }
182
+
183
+ ws.send(array);
184
+ });
185
+ ```
186
+
187
+ ### Simple server
188
+
189
+ ```js
190
+ import { WebSocketServer } from 'ws';
191
+
192
+ const wss = new WebSocketServer({ port: 8080 });
193
+
194
+ wss.on('connection', function connection(ws) {
195
+ ws.on('error', console.error);
196
+
197
+ ws.on('message', function message(data) {
198
+ console.log('received: %s', data);
199
+ });
200
+
201
+ ws.send('something');
202
+ });
203
+ ```
204
+
205
+ ### External HTTP/S server
206
+
207
+ ```js
208
+ import { createServer } from 'https';
209
+ import { readFileSync } from 'fs';
210
+ import { WebSocketServer } from 'ws';
211
+
212
+ const server = createServer({
213
+ cert: readFileSync('/path/to/cert.pem'),
214
+ key: readFileSync('/path/to/key.pem')
215
+ });
216
+ const wss = new WebSocketServer({ server });
217
+
218
+ wss.on('connection', function connection(ws) {
219
+ ws.on('error', console.error);
220
+
221
+ ws.on('message', function message(data) {
222
+ console.log('received: %s', data);
223
+ });
224
+
225
+ ws.send('something');
226
+ });
227
+
228
+ server.listen(8080);
229
+ ```
230
+
231
+ ### Multiple servers sharing a single HTTP/S server
232
+
233
+ ```js
234
+ import { createServer } from 'http';
235
+ import { WebSocketServer } from 'ws';
236
+
237
+ const server = createServer();
238
+ const wss1 = new WebSocketServer({ noServer: true });
239
+ const wss2 = new WebSocketServer({ noServer: true });
240
+
241
+ wss1.on('connection', function connection(ws) {
242
+ ws.on('error', console.error);
243
+
244
+ // ...
245
+ });
246
+
247
+ wss2.on('connection', function connection(ws) {
248
+ ws.on('error', console.error);
249
+
250
+ // ...
251
+ });
252
+
253
+ server.on('upgrade', function upgrade(request, socket, head) {
254
+ const { pathname } = new URL(request.url, 'wss://base.url');
255
+
256
+ if (pathname === '/foo') {
257
+ wss1.handleUpgrade(request, socket, head, function done(ws) {
258
+ wss1.emit('connection', ws, request);
259
+ });
260
+ } else if (pathname === '/bar') {
261
+ wss2.handleUpgrade(request, socket, head, function done(ws) {
262
+ wss2.emit('connection', ws, request);
263
+ });
264
+ } else {
265
+ socket.destroy();
266
+ }
267
+ });
268
+
269
+ server.listen(8080);
270
+ ```
271
+
272
+ ### Client authentication
273
+
274
+ ```js
275
+ import { createServer } from 'http';
276
+ import { WebSocketServer } from 'ws';
277
+
278
+ function onSocketError(err) {
279
+ console.error(err);
280
+ }
281
+
282
+ const server = createServer();
283
+ const wss = new WebSocketServer({ noServer: true });
284
+
285
+ wss.on('connection', function connection(ws, request, client) {
286
+ ws.on('error', console.error);
287
+
288
+ ws.on('message', function message(data) {
289
+ console.log(`Received message ${data} from user ${client}`);
290
+ });
291
+ });
292
+
293
+ server.on('upgrade', function upgrade(request, socket, head) {
294
+ socket.on('error', onSocketError);
295
+
296
+ // This function is not defined on purpose. Implement it with your own logic.
297
+ authenticate(request, function next(err, client) {
298
+ if (err || !client) {
299
+ socket.write('HTTP/1.1 401 Unauthorized\r\n\r\n');
300
+ socket.destroy();
301
+ return;
302
+ }
303
+
304
+ socket.removeListener('error', onSocketError);
305
+
306
+ wss.handleUpgrade(request, socket, head, function done(ws) {
307
+ wss.emit('connection', ws, request, client);
308
+ });
309
+ });
310
+ });
311
+
312
+ server.listen(8080);
313
+ ```
314
+
315
+ Also see the provided [example][session-parse-example] using `express-session`.
316
+
317
+ ### Server broadcast
318
+
319
+ A client WebSocket broadcasting to all connected WebSocket clients, including
320
+ itself.
321
+
322
+ ```js
323
+ import WebSocket, { WebSocketServer } from 'ws';
324
+
325
+ const wss = new WebSocketServer({ port: 8080 });
326
+
327
+ wss.on('connection', function connection(ws) {
328
+ ws.on('error', console.error);
329
+
330
+ ws.on('message', function message(data, isBinary) {
331
+ wss.clients.forEach(function each(client) {
332
+ if (client.readyState === WebSocket.OPEN) {
333
+ client.send(data, { binary: isBinary });
334
+ }
335
+ });
336
+ });
337
+ });
338
+ ```
339
+
340
+ A client WebSocket broadcasting to every other connected WebSocket clients,
341
+ excluding itself.
342
+
343
+ ```js
344
+ import WebSocket, { WebSocketServer } from 'ws';
345
+
346
+ const wss = new WebSocketServer({ port: 8080 });
347
+
348
+ wss.on('connection', function connection(ws) {
349
+ ws.on('error', console.error);
350
+
351
+ ws.on('message', function message(data, isBinary) {
352
+ wss.clients.forEach(function each(client) {
353
+ if (client !== ws && client.readyState === WebSocket.OPEN) {
354
+ client.send(data, { binary: isBinary });
355
+ }
356
+ });
357
+ });
358
+ });
359
+ ```
360
+
361
+ ### Round-trip time
362
+
363
+ ```js
364
+ import WebSocket from 'ws';
365
+
366
+ const ws = new WebSocket('wss://websocket-echo.com/');
367
+
368
+ ws.on('error', console.error);
369
+
370
+ ws.on('open', function open() {
371
+ console.log('connected');
372
+ ws.send(Date.now());
373
+ });
374
+
375
+ ws.on('close', function close() {
376
+ console.log('disconnected');
377
+ });
378
+
379
+ ws.on('message', function message(data) {
380
+ console.log(`Round-trip time: ${Date.now() - data} ms`);
381
+
382
+ setTimeout(function timeout() {
383
+ ws.send(Date.now());
384
+ }, 500);
385
+ });
386
+ ```
387
+
388
+ ### Use the Node.js streams API
389
+
390
+ ```js
391
+ import WebSocket, { createWebSocketStream } from 'ws';
392
+
393
+ const ws = new WebSocket('wss://websocket-echo.com/');
394
+
395
+ const duplex = createWebSocketStream(ws, { encoding: 'utf8' });
396
+
397
+ duplex.on('error', console.error);
398
+
399
+ duplex.pipe(process.stdout);
400
+ process.stdin.pipe(duplex);
401
+ ```
402
+
403
+ ### Other examples
404
+
405
+ For a full example with a browser client communicating with a ws server, see the
406
+ examples folder.
407
+
408
+ Otherwise, see the test cases.
409
+
410
+ ## FAQ
411
+
412
+ ### How to get the IP address of the client?
413
+
414
+ The remote IP address can be obtained from the raw socket.
415
+
416
+ ```js
417
+ import { WebSocketServer } from 'ws';
418
+
419
+ const wss = new WebSocketServer({ port: 8080 });
420
+
421
+ wss.on('connection', function connection(ws, req) {
422
+ const ip = req.socket.remoteAddress;
423
+
424
+ ws.on('error', console.error);
425
+ });
426
+ ```
427
+
428
+ When the server runs behind a proxy like NGINX, the de-facto standard is to use
429
+ the `X-Forwarded-For` header.
430
+
431
+ ```js
432
+ wss.on('connection', function connection(ws, req) {
433
+ const ip = req.headers['x-forwarded-for'].split(',')[0].trim();
434
+
435
+ ws.on('error', console.error);
436
+ });
437
+ ```
438
+
439
+ ### How to detect and close broken connections?
440
+
441
+ Sometimes, the link between the server and the client can be interrupted in a
442
+ way that keeps both the server and the client unaware of the broken state of the
443
+ connection (e.g. when pulling the cord).
444
+
445
+ In these cases, ping messages can be used as a means to verify that the remote
446
+ endpoint is still responsive.
447
+
448
+ ```js
449
+ import { WebSocketServer } from 'ws';
450
+
451
+ function heartbeat() {
452
+ this.isAlive = true;
453
+ }
454
+
455
+ const wss = new WebSocketServer({ port: 8080 });
456
+
457
+ wss.on('connection', function connection(ws) {
458
+ ws.isAlive = true;
459
+ ws.on('error', console.error);
460
+ ws.on('pong', heartbeat);
461
+ });
462
+
463
+ const interval = setInterval(function ping() {
464
+ wss.clients.forEach(function each(ws) {
465
+ if (ws.isAlive === false) return ws.terminate();
466
+
467
+ ws.isAlive = false;
468
+ ws.ping();
469
+ });
470
+ }, 30000);
471
+
472
+ wss.on('close', function close() {
473
+ clearInterval(interval);
474
+ });
475
+ ```
476
+
477
+ Pong messages are automatically sent in response to ping messages as required by
478
+ the spec.
479
+
480
+ Just like the server example above, your clients might as well lose connection
481
+ without knowing it. You might want to add a ping listener on your clients to
482
+ prevent that. A simple implementation would be:
483
+
484
+ ```js
485
+ import WebSocket from 'ws';
486
+
487
+ function heartbeat() {
488
+ clearTimeout(this.pingTimeout);
489
+
490
+ // Use `WebSocket#terminate()`, which immediately destroys the connection,
491
+ // instead of `WebSocket#close()`, which waits for the close timer.
492
+ // Delay should be equal to the interval at which your server
493
+ // sends out pings plus a conservative assumption of the latency.
494
+ this.pingTimeout = setTimeout(() => {
495
+ this.terminate();
496
+ }, 30000 + 1000);
497
+ }
498
+
499
+ const client = new WebSocket('wss://websocket-echo.com/');
500
+
501
+ client.on('error', console.error);
502
+ client.on('open', heartbeat);
503
+ client.on('ping', heartbeat);
504
+ client.on('close', function clear() {
505
+ clearTimeout(this.pingTimeout);
506
+ });
507
+ ```
508
+
509
+ ### How to connect via a proxy?
510
+
511
+ Use a custom `http.Agent` implementation like [https-proxy-agent][] or
512
+ [socks-proxy-agent][].
513
+
514
+ ## Changelog
515
+
516
+ We're using the GitHub [releases][changelog] for changelog entries.
517
+
518
+ ## License
519
+
520
+ [MIT](LICENSE)
521
+
522
+ [`buffer.isutf8()`]: https://nodejs.org/api/buffer.html#bufferisutf8input
523
+ [`http.websocketMask()`]:
524
+ https://nodejs.org/api/http.html#httpwebsocketmasksource-mask-output-offset-length
525
+ [`http.websocketUnmask()`]:
526
+ https://nodejs.org/api/http.html#httpwebsocketunmaskbuffer-mask
527
+ [bufferutil]: https://github.com/websockets/bufferutil
528
+ [changelog]: https://github.com/websockets/ws/releases
529
+ [client-report]: http://websockets.github.io/ws/autobahn/clients/
530
+ [https-proxy-agent]: https://github.com/TooTallNate/node-https-proxy-agent
531
+ [node-zlib-bug]: https://github.com/nodejs/node/issues/8871
532
+ [node-zlib-deflaterawdocs]:
533
+ https://nodejs.org/api/zlib.html#zlib_zlib_createdeflateraw_options
534
+ [permessage-deflate]: https://tools.ietf.org/html/rfc7692
535
+ [server-report]: http://websockets.github.io/ws/autobahn/servers/
536
+ [session-parse-example]: ./examples/express-session-parse
537
+ [socks-proxy-agent]: https://github.com/TooTallNate/node-socks-proxy-agent
538
+ [utf-8-validate]: https://github.com/websockets/utf-8-validate
539
+ [ws-server-options]: ./doc/ws.md#new-websocketserveroptions-callback
@@ -0,0 +1,8 @@
1
+ 'use strict';
2
+
3
+ module.exports = function () {
4
+ throw new Error(
5
+ 'ws does not work in the browser. Browser clients must use the native ' +
6
+ 'WebSocket object'
7
+ );
8
+ };
@@ -0,0 +1,22 @@
1
+ 'use strict';
2
+
3
+ const createWebSocketStream = require('./lib/stream');
4
+ const extension = require('./lib/extension');
5
+ const PerMessageDeflate = require('./lib/permessage-deflate');
6
+ const Receiver = require('./lib/receiver');
7
+ const Sender = require('./lib/sender');
8
+ const subprotocol = require('./lib/subprotocol');
9
+ const WebSocket = require('./lib/websocket');
10
+ const WebSocketServer = require('./lib/websocket-server');
11
+
12
+ WebSocket.createWebSocketStream = createWebSocketStream;
13
+ WebSocket.extension = extension;
14
+ WebSocket.PerMessageDeflate = PerMessageDeflate;
15
+ WebSocket.Receiver = Receiver;
16
+ WebSocket.Sender = Sender;
17
+ WebSocket.Server = WebSocketServer;
18
+ WebSocket.subprotocol = subprotocol;
19
+ WebSocket.WebSocket = WebSocket;
20
+ WebSocket.WebSocketServer = WebSocketServer;
21
+
22
+ module.exports = WebSocket;
@@ -0,0 +1,142 @@
1
+ 'use strict';
2
+
3
+ const { websocketMask, websocketUnmask } = require('http');
4
+
5
+ const { EMPTY_BUFFER } = require('./constants');
6
+
7
+ const FastBuffer = Buffer[Symbol.species];
8
+
9
+ /**
10
+ * Merges an array of buffers into a new buffer.
11
+ *
12
+ * @param {Buffer[]} list The array of buffers to concat
13
+ * @param {Number} totalLength The total length of buffers in the list
14
+ * @return {Buffer} The resulting buffer
15
+ * @public
16
+ */
17
+ function concat(list, totalLength) {
18
+ if (list.length === 0) return EMPTY_BUFFER;
19
+ if (list.length === 1) return list[0];
20
+
21
+ const target = Buffer.allocUnsafe(totalLength);
22
+ let offset = 0;
23
+
24
+ for (let i = 0; i < list.length; i++) {
25
+ const buf = list[i];
26
+ target.set(buf, offset);
27
+ offset += buf.length;
28
+ }
29
+
30
+ if (offset < totalLength) {
31
+ return new FastBuffer(target.buffer, target.byteOffset, offset);
32
+ }
33
+
34
+ return target;
35
+ }
36
+
37
+ /**
38
+ * Masks a buffer using the given mask.
39
+ *
40
+ * @param {Buffer} source The buffer to mask
41
+ * @param {Buffer} mask The mask to use
42
+ * @param {Buffer} output The buffer where to store the result
43
+ * @param {Number} offset The offset at which to start writing
44
+ * @param {Number} length The number of bytes to mask.
45
+ * @public
46
+ */
47
+ function _mask(source, mask, output, offset, length) {
48
+ for (let i = 0; i < length; i++) {
49
+ output[offset + i] = source[i] ^ mask[i & 3];
50
+ }
51
+ }
52
+
53
+ /**
54
+ * Unmasks a buffer using the given mask.
55
+ *
56
+ * @param {Buffer} buffer The buffer to unmask
57
+ * @param {Buffer} mask The mask to use
58
+ * @public
59
+ */
60
+ function _unmask(buffer, mask) {
61
+ for (let i = 0; i < buffer.length; i++) {
62
+ buffer[i] ^= mask[i & 3];
63
+ }
64
+ }
65
+
66
+ /**
67
+ * Converts a buffer to an `ArrayBuffer`.
68
+ *
69
+ * @param {Buffer} buf The buffer to convert
70
+ * @return {ArrayBuffer} Converted buffer
71
+ * @public
72
+ */
73
+ function toArrayBuffer(buf) {
74
+ if (buf.length === buf.buffer.byteLength) {
75
+ return buf.buffer;
76
+ }
77
+
78
+ return buf.buffer.slice(buf.byteOffset, buf.byteOffset + buf.length);
79
+ }
80
+
81
+ /**
82
+ * Converts `data` to a `Buffer`.
83
+ *
84
+ * @param {*} data The data to convert
85
+ * @return {Buffer} The buffer
86
+ * @throws {TypeError}
87
+ * @public
88
+ */
89
+ function toBuffer(data) {
90
+ toBuffer.readOnly = true;
91
+
92
+ if (Buffer.isBuffer(data)) return data;
93
+
94
+ let buf;
95
+
96
+ if (data instanceof ArrayBuffer) {
97
+ buf = new FastBuffer(data);
98
+ } else if (ArrayBuffer.isView(data)) {
99
+ buf = new FastBuffer(data.buffer, data.byteOffset, data.byteLength);
100
+ } else {
101
+ buf = Buffer.from(data);
102
+ toBuffer.readOnly = false;
103
+ }
104
+
105
+ return buf;
106
+ }
107
+
108
+ module.exports = {
109
+ concat,
110
+ mask: _mask,
111
+ toArrayBuffer,
112
+ toBuffer,
113
+ unmask: _unmask
114
+ };
115
+
116
+ if (websocketMask) {
117
+ module.exports.mask = function (source, mask, output, offset, length) {
118
+ if (length < 24) _mask(source, mask, output, offset, length);
119
+ else websocketMask(source, mask, output, offset, length);
120
+ };
121
+
122
+ module.exports.unmask = function (buffer, mask) {
123
+ if (buffer.length < 24) _unmask(buffer, mask);
124
+ else websocketUnmask(buffer, mask);
125
+ };
126
+ } /* istanbul ignore else */ else if (!process.env.WS_NO_BUFFER_UTIL) {
127
+ try {
128
+ const bufferUtil = require('bufferutil');
129
+
130
+ module.exports.mask = function (source, mask, output, offset, length) {
131
+ if (length < 48) _mask(source, mask, output, offset, length);
132
+ else bufferUtil.mask(source, mask, output, offset, length);
133
+ };
134
+
135
+ module.exports.unmask = function (buffer, mask) {
136
+ if (buffer.length < 32) _unmask(buffer, mask);
137
+ else bufferUtil.unmask(buffer, mask);
138
+ };
139
+ } catch (e) {
140
+ // Continue regardless of the error.
141
+ }
142
+ }