@depup/ws 8.19.0-depup.0 → 8.21.1-depup.0
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/README.md +16 -539
- package/changes.json +5 -0
- package/index.js +15 -6
- package/lib/permessage-deflate.js +6 -6
- package/lib/receiver.js +37 -0
- package/lib/sender.js +6 -1
- package/lib/websocket-server.js +13 -5
- package/lib/websocket.js +20 -6
- package/package.json +26 -5
- package/wrapper.mjs +14 -1
package/README.md
CHANGED
|
@@ -1,548 +1,25 @@
|
|
|
1
|
-
# ws
|
|
1
|
+
# @depup/ws
|
|
2
2
|
|
|
3
|
-
[
|
|
4
|
-
[](https://github.com/websockets/ws/actions?query=workflow%3ACI+branch%3Amaster)
|
|
5
|
-
[](https://coveralls.io/github/websockets/ws)
|
|
3
|
+
> Dependency-bumped version of [ws](https://www.npmjs.com/package/ws)
|
|
6
4
|
|
|
7
|
-
|
|
8
|
-
|
|
5
|
+
Generated by [DepUp](https://github.com/depup/npm) -- all production
|
|
6
|
+
dependencies bumped to latest versions.
|
|
9
7
|
|
|
10
|
-
|
|
11
|
-
[client][client-report].
|
|
8
|
+
## Installation
|
|
12
9
|
|
|
13
|
-
|
|
14
|
-
|
|
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
|
-
- [Opt-in for performance](#opt-in-for-performance)
|
|
26
|
-
- [Legacy opt-in for performance](#legacy-opt-in-for-performance)
|
|
27
|
-
- [API docs](#api-docs)
|
|
28
|
-
- [WebSocket compression](#websocket-compression)
|
|
29
|
-
- [Usage examples](#usage-examples)
|
|
30
|
-
- [Sending and receiving text data](#sending-and-receiving-text-data)
|
|
31
|
-
- [Sending binary data](#sending-binary-data)
|
|
32
|
-
- [Simple server](#simple-server)
|
|
33
|
-
- [External HTTP/S server](#external-https-server)
|
|
34
|
-
- [Multiple servers sharing a single HTTP/S server](#multiple-servers-sharing-a-single-https-server)
|
|
35
|
-
- [Client authentication](#client-authentication)
|
|
36
|
-
- [Server broadcast](#server-broadcast)
|
|
37
|
-
- [Round-trip time](#round-trip-time)
|
|
38
|
-
- [Use the Node.js streams API](#use-the-nodejs-streams-api)
|
|
39
|
-
- [Other examples](#other-examples)
|
|
40
|
-
- [FAQ](#faq)
|
|
41
|
-
- [How to get the IP address of the client?](#how-to-get-the-ip-address-of-the-client)
|
|
42
|
-
- [How to detect and close broken connections?](#how-to-detect-and-close-broken-connections)
|
|
43
|
-
- [How to connect via a proxy?](#how-to-connect-via-a-proxy)
|
|
44
|
-
- [Changelog](#changelog)
|
|
45
|
-
- [License](#license)
|
|
46
|
-
|
|
47
|
-
## Protocol support
|
|
48
|
-
|
|
49
|
-
- **HyBi drafts 07-12** (Use the option `protocolVersion: 8`)
|
|
50
|
-
- **HyBi drafts 13-17** (Current default, alternatively option
|
|
51
|
-
`protocolVersion: 13`)
|
|
52
|
-
|
|
53
|
-
## Installing
|
|
54
|
-
|
|
55
|
-
```
|
|
56
|
-
npm install ws
|
|
57
|
-
```
|
|
58
|
-
|
|
59
|
-
### Opt-in for performance
|
|
60
|
-
|
|
61
|
-
[bufferutil][] is an optional module that can be installed alongside the ws
|
|
62
|
-
module:
|
|
63
|
-
|
|
64
|
-
```
|
|
65
|
-
npm install --save-optional bufferutil
|
|
66
|
-
```
|
|
67
|
-
|
|
68
|
-
This is a binary addon that improves the performance of certain operations such
|
|
69
|
-
as masking and unmasking the data payload of the WebSocket frames. Prebuilt
|
|
70
|
-
binaries are available for the most popular platforms, so you don't necessarily
|
|
71
|
-
need to have a C++ compiler installed on your machine.
|
|
72
|
-
|
|
73
|
-
To force ws to not use bufferutil, use the
|
|
74
|
-
[`WS_NO_BUFFER_UTIL`](./doc/ws.md#ws_no_buffer_util) environment variable. This
|
|
75
|
-
can be useful to enhance security in systems where a user can put a package in
|
|
76
|
-
the package search path of an application of another user, due to how the
|
|
77
|
-
Node.js resolver algorithm works.
|
|
78
|
-
|
|
79
|
-
#### Legacy opt-in for performance
|
|
80
|
-
|
|
81
|
-
If you are running on an old version of Node.js (prior to v18.14.0), ws also
|
|
82
|
-
supports the [utf-8-validate][] module:
|
|
83
|
-
|
|
84
|
-
```
|
|
85
|
-
npm install --save-optional utf-8-validate
|
|
86
|
-
```
|
|
87
|
-
|
|
88
|
-
This contains a binary polyfill for [`buffer.isUtf8()`][].
|
|
89
|
-
|
|
90
|
-
To force ws not to use utf-8-validate, use the
|
|
91
|
-
[`WS_NO_UTF_8_VALIDATE`](./doc/ws.md#ws_no_utf_8_validate) environment variable.
|
|
92
|
-
|
|
93
|
-
## API docs
|
|
94
|
-
|
|
95
|
-
See [`/doc/ws.md`](./doc/ws.md) for Node.js-like documentation of ws classes and
|
|
96
|
-
utility functions.
|
|
97
|
-
|
|
98
|
-
## WebSocket compression
|
|
99
|
-
|
|
100
|
-
ws supports the [permessage-deflate extension][permessage-deflate] which enables
|
|
101
|
-
the client and server to negotiate a compression algorithm and its parameters,
|
|
102
|
-
and then selectively apply it to the data payloads of each WebSocket message.
|
|
103
|
-
|
|
104
|
-
The extension is disabled by default on the server and enabled by default on the
|
|
105
|
-
client. It adds a significant overhead in terms of performance and memory
|
|
106
|
-
consumption so we suggest to enable it only if it is really needed.
|
|
107
|
-
|
|
108
|
-
Note that Node.js has a variety of issues with high-performance compression,
|
|
109
|
-
where increased concurrency, especially on Linux, can lead to [catastrophic
|
|
110
|
-
memory fragmentation][node-zlib-bug] and slow performance. If you intend to use
|
|
111
|
-
permessage-deflate in production, it is worthwhile to set up a test
|
|
112
|
-
representative of your workload and ensure Node.js/zlib will handle it with
|
|
113
|
-
acceptable performance and memory usage.
|
|
114
|
-
|
|
115
|
-
Tuning of permessage-deflate can be done via the options defined below. You can
|
|
116
|
-
also use `zlibDeflateOptions` and `zlibInflateOptions`, which is passed directly
|
|
117
|
-
into the creation of [raw deflate/inflate streams][node-zlib-deflaterawdocs].
|
|
118
|
-
|
|
119
|
-
See [the docs][ws-server-options] for more options.
|
|
120
|
-
|
|
121
|
-
```js
|
|
122
|
-
import WebSocket, { WebSocketServer } from 'ws';
|
|
123
|
-
|
|
124
|
-
const wss = new WebSocketServer({
|
|
125
|
-
port: 8080,
|
|
126
|
-
perMessageDeflate: {
|
|
127
|
-
zlibDeflateOptions: {
|
|
128
|
-
// See zlib defaults.
|
|
129
|
-
chunkSize: 1024,
|
|
130
|
-
memLevel: 7,
|
|
131
|
-
level: 3
|
|
132
|
-
},
|
|
133
|
-
zlibInflateOptions: {
|
|
134
|
-
chunkSize: 10 * 1024
|
|
135
|
-
},
|
|
136
|
-
// Other options settable:
|
|
137
|
-
clientNoContextTakeover: true, // Defaults to negotiated value.
|
|
138
|
-
serverNoContextTakeover: true, // Defaults to negotiated value.
|
|
139
|
-
serverMaxWindowBits: 10, // Defaults to negotiated value.
|
|
140
|
-
// Below options specified as default values.
|
|
141
|
-
concurrencyLimit: 10, // Limits zlib concurrency for perf.
|
|
142
|
-
threshold: 1024 // Size (in bytes) below which messages
|
|
143
|
-
// should not be compressed if context takeover is disabled.
|
|
144
|
-
}
|
|
145
|
-
});
|
|
146
|
-
```
|
|
147
|
-
|
|
148
|
-
The client will only use the extension if it is supported and enabled on the
|
|
149
|
-
server. To always disable the extension on the client, set the
|
|
150
|
-
`perMessageDeflate` option to `false`.
|
|
151
|
-
|
|
152
|
-
```js
|
|
153
|
-
import WebSocket from 'ws';
|
|
154
|
-
|
|
155
|
-
const ws = new WebSocket('ws://www.host.com/path', {
|
|
156
|
-
perMessageDeflate: false
|
|
157
|
-
});
|
|
10
|
+
```bash
|
|
11
|
+
npm install @depup/ws
|
|
158
12
|
```
|
|
159
13
|
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
const ws = new WebSocket('ws://www.host.com/path');
|
|
168
|
-
|
|
169
|
-
ws.on('error', console.error);
|
|
170
|
-
|
|
171
|
-
ws.on('open', function open() {
|
|
172
|
-
ws.send('something');
|
|
173
|
-
});
|
|
174
|
-
|
|
175
|
-
ws.on('message', function message(data) {
|
|
176
|
-
console.log('received: %s', data);
|
|
177
|
-
});
|
|
178
|
-
```
|
|
179
|
-
|
|
180
|
-
### Sending binary data
|
|
181
|
-
|
|
182
|
-
```js
|
|
183
|
-
import WebSocket from 'ws';
|
|
184
|
-
|
|
185
|
-
const ws = new WebSocket('ws://www.host.com/path');
|
|
186
|
-
|
|
187
|
-
ws.on('error', console.error);
|
|
188
|
-
|
|
189
|
-
ws.on('open', function open() {
|
|
190
|
-
const array = new Float32Array(5);
|
|
191
|
-
|
|
192
|
-
for (var i = 0; i < array.length; ++i) {
|
|
193
|
-
array[i] = i / 2;
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
ws.send(array);
|
|
197
|
-
});
|
|
198
|
-
```
|
|
199
|
-
|
|
200
|
-
### Simple server
|
|
201
|
-
|
|
202
|
-
```js
|
|
203
|
-
import { WebSocketServer } from 'ws';
|
|
204
|
-
|
|
205
|
-
const wss = new WebSocketServer({ port: 8080 });
|
|
206
|
-
|
|
207
|
-
wss.on('connection', function connection(ws) {
|
|
208
|
-
ws.on('error', console.error);
|
|
209
|
-
|
|
210
|
-
ws.on('message', function message(data) {
|
|
211
|
-
console.log('received: %s', data);
|
|
212
|
-
});
|
|
213
|
-
|
|
214
|
-
ws.send('something');
|
|
215
|
-
});
|
|
216
|
-
```
|
|
217
|
-
|
|
218
|
-
### External HTTP/S server
|
|
219
|
-
|
|
220
|
-
```js
|
|
221
|
-
import { createServer } from 'https';
|
|
222
|
-
import { readFileSync } from 'fs';
|
|
223
|
-
import { WebSocketServer } from 'ws';
|
|
224
|
-
|
|
225
|
-
const server = createServer({
|
|
226
|
-
cert: readFileSync('/path/to/cert.pem'),
|
|
227
|
-
key: readFileSync('/path/to/key.pem')
|
|
228
|
-
});
|
|
229
|
-
const wss = new WebSocketServer({ server });
|
|
230
|
-
|
|
231
|
-
wss.on('connection', function connection(ws) {
|
|
232
|
-
ws.on('error', console.error);
|
|
233
|
-
|
|
234
|
-
ws.on('message', function message(data) {
|
|
235
|
-
console.log('received: %s', data);
|
|
236
|
-
});
|
|
237
|
-
|
|
238
|
-
ws.send('something');
|
|
239
|
-
});
|
|
240
|
-
|
|
241
|
-
server.listen(8080);
|
|
242
|
-
```
|
|
243
|
-
|
|
244
|
-
### Multiple servers sharing a single HTTP/S server
|
|
245
|
-
|
|
246
|
-
```js
|
|
247
|
-
import { createServer } from 'http';
|
|
248
|
-
import { WebSocketServer } from 'ws';
|
|
249
|
-
|
|
250
|
-
const server = createServer();
|
|
251
|
-
const wss1 = new WebSocketServer({ noServer: true });
|
|
252
|
-
const wss2 = new WebSocketServer({ noServer: true });
|
|
253
|
-
|
|
254
|
-
wss1.on('connection', function connection(ws) {
|
|
255
|
-
ws.on('error', console.error);
|
|
256
|
-
|
|
257
|
-
// ...
|
|
258
|
-
});
|
|
259
|
-
|
|
260
|
-
wss2.on('connection', function connection(ws) {
|
|
261
|
-
ws.on('error', console.error);
|
|
262
|
-
|
|
263
|
-
// ...
|
|
264
|
-
});
|
|
265
|
-
|
|
266
|
-
server.on('upgrade', function upgrade(request, socket, head) {
|
|
267
|
-
const { pathname } = new URL(request.url, 'wss://base.url');
|
|
268
|
-
|
|
269
|
-
if (pathname === '/foo') {
|
|
270
|
-
wss1.handleUpgrade(request, socket, head, function done(ws) {
|
|
271
|
-
wss1.emit('connection', ws, request);
|
|
272
|
-
});
|
|
273
|
-
} else if (pathname === '/bar') {
|
|
274
|
-
wss2.handleUpgrade(request, socket, head, function done(ws) {
|
|
275
|
-
wss2.emit('connection', ws, request);
|
|
276
|
-
});
|
|
277
|
-
} else {
|
|
278
|
-
socket.destroy();
|
|
279
|
-
}
|
|
280
|
-
});
|
|
281
|
-
|
|
282
|
-
server.listen(8080);
|
|
283
|
-
```
|
|
284
|
-
|
|
285
|
-
### Client authentication
|
|
286
|
-
|
|
287
|
-
```js
|
|
288
|
-
import { createServer } from 'http';
|
|
289
|
-
import { WebSocketServer } from 'ws';
|
|
290
|
-
|
|
291
|
-
function onSocketError(err) {
|
|
292
|
-
console.error(err);
|
|
293
|
-
}
|
|
294
|
-
|
|
295
|
-
const server = createServer();
|
|
296
|
-
const wss = new WebSocketServer({ noServer: true });
|
|
297
|
-
|
|
298
|
-
wss.on('connection', function connection(ws, request, client) {
|
|
299
|
-
ws.on('error', console.error);
|
|
300
|
-
|
|
301
|
-
ws.on('message', function message(data) {
|
|
302
|
-
console.log(`Received message ${data} from user ${client}`);
|
|
303
|
-
});
|
|
304
|
-
});
|
|
305
|
-
|
|
306
|
-
server.on('upgrade', function upgrade(request, socket, head) {
|
|
307
|
-
socket.on('error', onSocketError);
|
|
308
|
-
|
|
309
|
-
// This function is not defined on purpose. Implement it with your own logic.
|
|
310
|
-
authenticate(request, function next(err, client) {
|
|
311
|
-
if (err || !client) {
|
|
312
|
-
socket.write('HTTP/1.1 401 Unauthorized\r\n\r\n');
|
|
313
|
-
socket.destroy();
|
|
314
|
-
return;
|
|
315
|
-
}
|
|
316
|
-
|
|
317
|
-
socket.removeListener('error', onSocketError);
|
|
318
|
-
|
|
319
|
-
wss.handleUpgrade(request, socket, head, function done(ws) {
|
|
320
|
-
wss.emit('connection', ws, request, client);
|
|
321
|
-
});
|
|
322
|
-
});
|
|
323
|
-
});
|
|
324
|
-
|
|
325
|
-
server.listen(8080);
|
|
326
|
-
```
|
|
327
|
-
|
|
328
|
-
Also see the provided [example][session-parse-example] using `express-session`.
|
|
329
|
-
|
|
330
|
-
### Server broadcast
|
|
331
|
-
|
|
332
|
-
A client WebSocket broadcasting to all connected WebSocket clients, including
|
|
333
|
-
itself.
|
|
334
|
-
|
|
335
|
-
```js
|
|
336
|
-
import WebSocket, { WebSocketServer } from 'ws';
|
|
337
|
-
|
|
338
|
-
const wss = new WebSocketServer({ port: 8080 });
|
|
339
|
-
|
|
340
|
-
wss.on('connection', function connection(ws) {
|
|
341
|
-
ws.on('error', console.error);
|
|
342
|
-
|
|
343
|
-
ws.on('message', function message(data, isBinary) {
|
|
344
|
-
wss.clients.forEach(function each(client) {
|
|
345
|
-
if (client.readyState === WebSocket.OPEN) {
|
|
346
|
-
client.send(data, { binary: isBinary });
|
|
347
|
-
}
|
|
348
|
-
});
|
|
349
|
-
});
|
|
350
|
-
});
|
|
351
|
-
```
|
|
352
|
-
|
|
353
|
-
A client WebSocket broadcasting to every other connected WebSocket clients,
|
|
354
|
-
excluding itself.
|
|
355
|
-
|
|
356
|
-
```js
|
|
357
|
-
import WebSocket, { WebSocketServer } from 'ws';
|
|
358
|
-
|
|
359
|
-
const wss = new WebSocketServer({ port: 8080 });
|
|
360
|
-
|
|
361
|
-
wss.on('connection', function connection(ws) {
|
|
362
|
-
ws.on('error', console.error);
|
|
363
|
-
|
|
364
|
-
ws.on('message', function message(data, isBinary) {
|
|
365
|
-
wss.clients.forEach(function each(client) {
|
|
366
|
-
if (client !== ws && client.readyState === WebSocket.OPEN) {
|
|
367
|
-
client.send(data, { binary: isBinary });
|
|
368
|
-
}
|
|
369
|
-
});
|
|
370
|
-
});
|
|
371
|
-
});
|
|
372
|
-
```
|
|
373
|
-
|
|
374
|
-
### Round-trip time
|
|
375
|
-
|
|
376
|
-
```js
|
|
377
|
-
import WebSocket from 'ws';
|
|
378
|
-
|
|
379
|
-
const ws = new WebSocket('wss://websocket-echo.com/');
|
|
380
|
-
|
|
381
|
-
ws.on('error', console.error);
|
|
382
|
-
|
|
383
|
-
ws.on('open', function open() {
|
|
384
|
-
console.log('connected');
|
|
385
|
-
ws.send(Date.now());
|
|
386
|
-
});
|
|
387
|
-
|
|
388
|
-
ws.on('close', function close() {
|
|
389
|
-
console.log('disconnected');
|
|
390
|
-
});
|
|
391
|
-
|
|
392
|
-
ws.on('message', function message(data) {
|
|
393
|
-
console.log(`Round-trip time: ${Date.now() - data} ms`);
|
|
394
|
-
|
|
395
|
-
setTimeout(function timeout() {
|
|
396
|
-
ws.send(Date.now());
|
|
397
|
-
}, 500);
|
|
398
|
-
});
|
|
399
|
-
```
|
|
400
|
-
|
|
401
|
-
### Use the Node.js streams API
|
|
402
|
-
|
|
403
|
-
```js
|
|
404
|
-
import WebSocket, { createWebSocketStream } from 'ws';
|
|
405
|
-
|
|
406
|
-
const ws = new WebSocket('wss://websocket-echo.com/');
|
|
407
|
-
|
|
408
|
-
const duplex = createWebSocketStream(ws, { encoding: 'utf8' });
|
|
409
|
-
|
|
410
|
-
duplex.on('error', console.error);
|
|
411
|
-
|
|
412
|
-
duplex.pipe(process.stdout);
|
|
413
|
-
process.stdin.pipe(duplex);
|
|
414
|
-
```
|
|
415
|
-
|
|
416
|
-
### Other examples
|
|
417
|
-
|
|
418
|
-
For a full example with a browser client communicating with a ws server, see the
|
|
419
|
-
examples folder.
|
|
420
|
-
|
|
421
|
-
Otherwise, see the test cases.
|
|
422
|
-
|
|
423
|
-
## FAQ
|
|
424
|
-
|
|
425
|
-
### How to get the IP address of the client?
|
|
426
|
-
|
|
427
|
-
The remote IP address can be obtained from the raw socket.
|
|
428
|
-
|
|
429
|
-
```js
|
|
430
|
-
import { WebSocketServer } from 'ws';
|
|
431
|
-
|
|
432
|
-
const wss = new WebSocketServer({ port: 8080 });
|
|
433
|
-
|
|
434
|
-
wss.on('connection', function connection(ws, req) {
|
|
435
|
-
const ip = req.socket.remoteAddress;
|
|
436
|
-
|
|
437
|
-
ws.on('error', console.error);
|
|
438
|
-
});
|
|
439
|
-
```
|
|
440
|
-
|
|
441
|
-
When the server runs behind a proxy like NGINX, the de-facto standard is to use
|
|
442
|
-
the `X-Forwarded-For` header.
|
|
443
|
-
|
|
444
|
-
```js
|
|
445
|
-
wss.on('connection', function connection(ws, req) {
|
|
446
|
-
const ip = req.headers['x-forwarded-for'].split(',')[0].trim();
|
|
447
|
-
|
|
448
|
-
ws.on('error', console.error);
|
|
449
|
-
});
|
|
450
|
-
```
|
|
451
|
-
|
|
452
|
-
### How to detect and close broken connections?
|
|
453
|
-
|
|
454
|
-
Sometimes, the link between the server and the client can be interrupted in a
|
|
455
|
-
way that keeps both the server and the client unaware of the broken state of the
|
|
456
|
-
connection (e.g. when pulling the cord).
|
|
457
|
-
|
|
458
|
-
In these cases, ping messages can be used as a means to verify that the remote
|
|
459
|
-
endpoint is still responsive.
|
|
460
|
-
|
|
461
|
-
```js
|
|
462
|
-
import { WebSocketServer } from 'ws';
|
|
463
|
-
|
|
464
|
-
function heartbeat() {
|
|
465
|
-
this.isAlive = true;
|
|
466
|
-
}
|
|
467
|
-
|
|
468
|
-
const wss = new WebSocketServer({ port: 8080 });
|
|
469
|
-
|
|
470
|
-
wss.on('connection', function connection(ws) {
|
|
471
|
-
ws.isAlive = true;
|
|
472
|
-
ws.on('error', console.error);
|
|
473
|
-
ws.on('pong', heartbeat);
|
|
474
|
-
});
|
|
475
|
-
|
|
476
|
-
const interval = setInterval(function ping() {
|
|
477
|
-
wss.clients.forEach(function each(ws) {
|
|
478
|
-
if (ws.isAlive === false) return ws.terminate();
|
|
479
|
-
|
|
480
|
-
ws.isAlive = false;
|
|
481
|
-
ws.ping();
|
|
482
|
-
});
|
|
483
|
-
}, 30000);
|
|
484
|
-
|
|
485
|
-
wss.on('close', function close() {
|
|
486
|
-
clearInterval(interval);
|
|
487
|
-
});
|
|
488
|
-
```
|
|
489
|
-
|
|
490
|
-
Pong messages are automatically sent in response to ping messages as required by
|
|
491
|
-
the spec.
|
|
492
|
-
|
|
493
|
-
Just like the server example above, your clients might as well lose connection
|
|
494
|
-
without knowing it. You might want to add a ping listener on your clients to
|
|
495
|
-
prevent that. A simple implementation would be:
|
|
496
|
-
|
|
497
|
-
```js
|
|
498
|
-
import WebSocket from 'ws';
|
|
499
|
-
|
|
500
|
-
function heartbeat() {
|
|
501
|
-
clearTimeout(this.pingTimeout);
|
|
502
|
-
|
|
503
|
-
// Use `WebSocket#terminate()`, which immediately destroys the connection,
|
|
504
|
-
// instead of `WebSocket#close()`, which waits for the close timer.
|
|
505
|
-
// Delay should be equal to the interval at which your server
|
|
506
|
-
// sends out pings plus a conservative assumption of the latency.
|
|
507
|
-
this.pingTimeout = setTimeout(() => {
|
|
508
|
-
this.terminate();
|
|
509
|
-
}, 30000 + 1000);
|
|
510
|
-
}
|
|
511
|
-
|
|
512
|
-
const client = new WebSocket('wss://websocket-echo.com/');
|
|
513
|
-
|
|
514
|
-
client.on('error', console.error);
|
|
515
|
-
client.on('open', heartbeat);
|
|
516
|
-
client.on('ping', heartbeat);
|
|
517
|
-
client.on('close', function clear() {
|
|
518
|
-
clearTimeout(this.pingTimeout);
|
|
519
|
-
});
|
|
520
|
-
```
|
|
521
|
-
|
|
522
|
-
### How to connect via a proxy?
|
|
523
|
-
|
|
524
|
-
Use a custom `http.Agent` implementation like [https-proxy-agent][] or
|
|
525
|
-
[socks-proxy-agent][].
|
|
526
|
-
|
|
527
|
-
## Changelog
|
|
528
|
-
|
|
529
|
-
We're using the GitHub [releases][changelog] for changelog entries.
|
|
14
|
+
| Field | Value |
|
|
15
|
+
|-------|-------|
|
|
16
|
+
| Original | [ws](https://www.npmjs.com/package/ws) @ 8.21.1 |
|
|
17
|
+
| Processed | 2026-07-21 |
|
|
18
|
+
| Smoke test | passed |
|
|
19
|
+
| Deps updated | 0 |
|
|
530
20
|
|
|
531
|
-
|
|
21
|
+
---
|
|
532
22
|
|
|
533
|
-
|
|
23
|
+
Source: https://github.com/depup/npm | Original: https://www.npmjs.com/package/ws
|
|
534
24
|
|
|
535
|
-
|
|
536
|
-
[bufferutil]: https://github.com/websockets/bufferutil
|
|
537
|
-
[changelog]: https://github.com/websockets/ws/releases
|
|
538
|
-
[client-report]: http://websockets.github.io/ws/autobahn/clients/
|
|
539
|
-
[https-proxy-agent]: https://github.com/TooTallNate/node-https-proxy-agent
|
|
540
|
-
[node-zlib-bug]: https://github.com/nodejs/node/issues/8871
|
|
541
|
-
[node-zlib-deflaterawdocs]:
|
|
542
|
-
https://nodejs.org/api/zlib.html#zlib_zlib_createdeflateraw_options
|
|
543
|
-
[permessage-deflate]: https://tools.ietf.org/html/rfc7692
|
|
544
|
-
[server-report]: http://websockets.github.io/ws/autobahn/servers/
|
|
545
|
-
[session-parse-example]: ./examples/express-session-parse
|
|
546
|
-
[socks-proxy-agent]: https://github.com/TooTallNate/node-socks-proxy-agent
|
|
547
|
-
[utf-8-validate]: https://github.com/websockets/utf-8-validate
|
|
548
|
-
[ws-server-options]: ./doc/ws.md#new-websocketserveroptions-callback
|
|
25
|
+
License inherited from the original package.
|
package/changes.json
ADDED
package/index.js
CHANGED
|
@@ -1,13 +1,22 @@
|
|
|
1
1
|
'use strict';
|
|
2
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');
|
|
3
9
|
const WebSocket = require('./lib/websocket');
|
|
10
|
+
const WebSocketServer = require('./lib/websocket-server');
|
|
4
11
|
|
|
5
|
-
WebSocket.createWebSocketStream =
|
|
6
|
-
WebSocket.
|
|
7
|
-
WebSocket.
|
|
8
|
-
WebSocket.
|
|
9
|
-
|
|
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;
|
|
10
19
|
WebSocket.WebSocket = WebSocket;
|
|
11
|
-
WebSocket.WebSocketServer =
|
|
20
|
+
WebSocket.WebSocketServer = WebSocketServer;
|
|
12
21
|
|
|
13
22
|
module.exports = WebSocket;
|
|
@@ -37,6 +37,9 @@ class PerMessageDeflate {
|
|
|
37
37
|
* acknowledge disabling of client context takeover
|
|
38
38
|
* @param {Number} [options.concurrencyLimit=10] The number of concurrent
|
|
39
39
|
* calls to zlib
|
|
40
|
+
* @param {Boolean} [options.isServer=false] Create the instance in either
|
|
41
|
+
* server or client mode
|
|
42
|
+
* @param {Number} [options.maxPayload=0] The maximum allowed message length
|
|
40
43
|
* @param {(Boolean|Number)} [options.serverMaxWindowBits] Request/confirm the
|
|
41
44
|
* use of a custom server window size
|
|
42
45
|
* @param {Boolean} [options.serverNoContextTakeover=false] Request/accept
|
|
@@ -47,16 +50,13 @@ class PerMessageDeflate {
|
|
|
47
50
|
* deflate
|
|
48
51
|
* @param {Object} [options.zlibInflateOptions] Options to pass to zlib on
|
|
49
52
|
* inflate
|
|
50
|
-
* @param {Boolean} [isServer=false] Create the instance in either server or
|
|
51
|
-
* client mode
|
|
52
|
-
* @param {Number} [maxPayload=0] The maximum allowed message length
|
|
53
53
|
*/
|
|
54
|
-
constructor(options
|
|
55
|
-
this._maxPayload = maxPayload | 0;
|
|
54
|
+
constructor(options) {
|
|
56
55
|
this._options = options || {};
|
|
57
56
|
this._threshold =
|
|
58
57
|
this._options.threshold !== undefined ? this._options.threshold : 1024;
|
|
59
|
-
this.
|
|
58
|
+
this._maxPayload = this._options.maxPayload | 0;
|
|
59
|
+
this._isServer = !!this._options.isServer;
|
|
60
60
|
this._deflate = null;
|
|
61
61
|
this._inflate = null;
|
|
62
62
|
|
package/lib/receiver.js
CHANGED
|
@@ -40,6 +40,10 @@ class Receiver extends Writable {
|
|
|
40
40
|
* extensions
|
|
41
41
|
* @param {Boolean} [options.isServer=false] Specifies whether to operate in
|
|
42
42
|
* client or server mode
|
|
43
|
+
* @param {Number} [options.maxBufferedChunks=0] The maximum number of
|
|
44
|
+
* buffered data chunks
|
|
45
|
+
* @param {Number} [options.maxFragments=0] The maximum number of message
|
|
46
|
+
* fragments
|
|
43
47
|
* @param {Number} [options.maxPayload=0] The maximum allowed message length
|
|
44
48
|
* @param {Boolean} [options.skipUTF8Validation=false] Specifies whether or
|
|
45
49
|
* not to skip UTF-8 validation for text and close messages
|
|
@@ -54,6 +58,8 @@ class Receiver extends Writable {
|
|
|
54
58
|
this._binaryType = options.binaryType || BINARY_TYPES[0];
|
|
55
59
|
this._extensions = options.extensions || {};
|
|
56
60
|
this._isServer = !!options.isServer;
|
|
61
|
+
this._maxBufferedChunks = options.maxBufferedChunks | 0;
|
|
62
|
+
this._maxFragments = options.maxFragments | 0;
|
|
57
63
|
this._maxPayload = options.maxPayload | 0;
|
|
58
64
|
this._skipUTF8Validation = !!options.skipUTF8Validation;
|
|
59
65
|
this[kWebSocket] = undefined;
|
|
@@ -71,6 +77,7 @@ class Receiver extends Writable {
|
|
|
71
77
|
|
|
72
78
|
this._totalPayloadLength = 0;
|
|
73
79
|
this._messageLength = 0;
|
|
80
|
+
this._numFragments = 0;
|
|
74
81
|
this._fragments = [];
|
|
75
82
|
|
|
76
83
|
this._errored = false;
|
|
@@ -89,6 +96,22 @@ class Receiver extends Writable {
|
|
|
89
96
|
_write(chunk, encoding, cb) {
|
|
90
97
|
if (this._opcode === 0x08 && this._state == GET_INFO) return cb();
|
|
91
98
|
|
|
99
|
+
if (
|
|
100
|
+
this._maxBufferedChunks > 0 &&
|
|
101
|
+
this._buffers.length >= this._maxBufferedChunks
|
|
102
|
+
) {
|
|
103
|
+
cb(
|
|
104
|
+
this.createError(
|
|
105
|
+
RangeError,
|
|
106
|
+
'Too many buffered chunks',
|
|
107
|
+
false,
|
|
108
|
+
1008,
|
|
109
|
+
'WS_ERR_TOO_MANY_BUFFERED_PARTS'
|
|
110
|
+
)
|
|
111
|
+
);
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
|
|
92
115
|
this._bufferedBytes += chunk.length;
|
|
93
116
|
this._buffers.push(chunk);
|
|
94
117
|
this.startLoop(cb);
|
|
@@ -478,6 +501,19 @@ class Receiver extends Writable {
|
|
|
478
501
|
return;
|
|
479
502
|
}
|
|
480
503
|
|
|
504
|
+
if (this._maxFragments > 0 && ++this._numFragments > this._maxFragments) {
|
|
505
|
+
const error = this.createError(
|
|
506
|
+
RangeError,
|
|
507
|
+
'Too many message fragments',
|
|
508
|
+
false,
|
|
509
|
+
1008,
|
|
510
|
+
'WS_ERR_TOO_MANY_BUFFERED_PARTS'
|
|
511
|
+
);
|
|
512
|
+
|
|
513
|
+
cb(error);
|
|
514
|
+
return;
|
|
515
|
+
}
|
|
516
|
+
|
|
481
517
|
if (this._compressed) {
|
|
482
518
|
this._state = INFLATING;
|
|
483
519
|
this.decompress(data, cb);
|
|
@@ -550,6 +586,7 @@ class Receiver extends Writable {
|
|
|
550
586
|
this._totalPayloadLength = 0;
|
|
551
587
|
this._messageLength = 0;
|
|
552
588
|
this._fragmented = 0;
|
|
589
|
+
this._numFragments = 0;
|
|
553
590
|
this._fragments = [];
|
|
554
591
|
|
|
555
592
|
if (this._opcode === 2) {
|
package/lib/sender.js
CHANGED
|
@@ -4,6 +4,9 @@
|
|
|
4
4
|
|
|
5
5
|
const { Duplex } = require('stream');
|
|
6
6
|
const { randomFillSync } = require('crypto');
|
|
7
|
+
const {
|
|
8
|
+
types: { isUint8Array }
|
|
9
|
+
} = require('util');
|
|
7
10
|
|
|
8
11
|
const PerMessageDeflate = require('./permessage-deflate');
|
|
9
12
|
const { EMPTY_BUFFER, kWebSocket, NOOP } = require('./constants');
|
|
@@ -200,8 +203,10 @@ class Sender {
|
|
|
200
203
|
|
|
201
204
|
if (typeof data === 'string') {
|
|
202
205
|
buf.write(data, 2);
|
|
203
|
-
} else {
|
|
206
|
+
} else if (isUint8Array(data)) {
|
|
204
207
|
buf.set(data, 2);
|
|
208
|
+
} else {
|
|
209
|
+
throw new TypeError('Second argument must be a string or a Uint8Array');
|
|
205
210
|
}
|
|
206
211
|
}
|
|
207
212
|
|
package/lib/websocket-server.js
CHANGED
|
@@ -43,6 +43,10 @@ class WebSocketServer extends EventEmitter {
|
|
|
43
43
|
* called
|
|
44
44
|
* @param {Function} [options.handleProtocols] A hook to handle protocols
|
|
45
45
|
* @param {String} [options.host] The hostname where to bind the server
|
|
46
|
+
* @param {Number} [options.maxBufferedChunks=262144] The maximum number of
|
|
47
|
+
* buffered data chunks
|
|
48
|
+
* @param {Number} [options.maxFragments=16384] The maximum number of message
|
|
49
|
+
* fragments
|
|
46
50
|
* @param {Number} [options.maxPayload=104857600] The maximum allowed message
|
|
47
51
|
* size
|
|
48
52
|
* @param {Boolean} [options.noServer=false] Enable no server mode
|
|
@@ -65,6 +69,8 @@ class WebSocketServer extends EventEmitter {
|
|
|
65
69
|
options = {
|
|
66
70
|
allowSynchronousEvents: true,
|
|
67
71
|
autoPong: true,
|
|
72
|
+
maxBufferedChunks: 256 * 1024,
|
|
73
|
+
maxFragments: 16 * 1024,
|
|
68
74
|
maxPayload: 100 * 1024 * 1024,
|
|
69
75
|
skipUTF8Validation: false,
|
|
70
76
|
perMessageDeflate: false,
|
|
@@ -293,11 +299,11 @@ class WebSocketServer extends EventEmitter {
|
|
|
293
299
|
this.options.perMessageDeflate &&
|
|
294
300
|
secWebSocketExtensions !== undefined
|
|
295
301
|
) {
|
|
296
|
-
const perMessageDeflate = new PerMessageDeflate(
|
|
297
|
-
this.options.perMessageDeflate,
|
|
298
|
-
true,
|
|
299
|
-
this.options.maxPayload
|
|
300
|
-
);
|
|
302
|
+
const perMessageDeflate = new PerMessageDeflate({
|
|
303
|
+
...this.options.perMessageDeflate,
|
|
304
|
+
isServer: true,
|
|
305
|
+
maxPayload: this.options.maxPayload
|
|
306
|
+
});
|
|
301
307
|
|
|
302
308
|
try {
|
|
303
309
|
const offers = extension.parse(secWebSocketExtensions);
|
|
@@ -424,6 +430,8 @@ class WebSocketServer extends EventEmitter {
|
|
|
424
430
|
|
|
425
431
|
ws.setSocket(socket, head, {
|
|
426
432
|
allowSynchronousEvents: this.options.allowSynchronousEvents,
|
|
433
|
+
maxBufferedChunks: this.options.maxBufferedChunks,
|
|
434
|
+
maxFragments: this.options.maxFragments,
|
|
427
435
|
maxPayload: this.options.maxPayload,
|
|
428
436
|
skipUTF8Validation: this.options.skipUTF8Validation
|
|
429
437
|
});
|
package/lib/websocket.js
CHANGED
|
@@ -201,6 +201,10 @@ class WebSocket extends EventEmitter {
|
|
|
201
201
|
* multiple times in the same tick
|
|
202
202
|
* @param {Function} [options.generateMask] The function used to generate the
|
|
203
203
|
* masking key
|
|
204
|
+
* @param {Number} [options.maxBufferedChunks=0] The maximum number of
|
|
205
|
+
* buffered data chunks
|
|
206
|
+
* @param {Number} [options.maxFragments=0] The maximum number of message
|
|
207
|
+
* fragments
|
|
204
208
|
* @param {Number} [options.maxPayload=0] The maximum allowed message size
|
|
205
209
|
* @param {Boolean} [options.skipUTF8Validation=false] Specifies whether or
|
|
206
210
|
* not to skip UTF-8 validation for text and close messages
|
|
@@ -212,6 +216,8 @@ class WebSocket extends EventEmitter {
|
|
|
212
216
|
binaryType: this.binaryType,
|
|
213
217
|
extensions: this._extensions,
|
|
214
218
|
isServer: this._isServer,
|
|
219
|
+
maxBufferedChunks: options.maxBufferedChunks,
|
|
220
|
+
maxFragments: options.maxFragments,
|
|
215
221
|
maxPayload: options.maxPayload,
|
|
216
222
|
skipUTF8Validation: options.skipUTF8Validation
|
|
217
223
|
});
|
|
@@ -640,6 +646,10 @@ module.exports = WebSocket;
|
|
|
640
646
|
* masking key
|
|
641
647
|
* @param {Number} [options.handshakeTimeout] Timeout in milliseconds for the
|
|
642
648
|
* handshake request
|
|
649
|
+
* @param {Number} [options.maxBufferedChunks=262144] The maximum number of
|
|
650
|
+
* buffered data chunks
|
|
651
|
+
* @param {Number} [options.maxFragments=16384] The maximum number of message
|
|
652
|
+
* fragments
|
|
643
653
|
* @param {Number} [options.maxPayload=104857600] The maximum allowed message
|
|
644
654
|
* size
|
|
645
655
|
* @param {Number} [options.maxRedirects=10] The maximum number of redirects
|
|
@@ -660,6 +670,8 @@ function initAsClient(websocket, address, protocols, options) {
|
|
|
660
670
|
autoPong: true,
|
|
661
671
|
closeTimeout: CLOSE_TIMEOUT,
|
|
662
672
|
protocolVersion: protocolVersions[1],
|
|
673
|
+
maxBufferedChunks: 256 * 1024,
|
|
674
|
+
maxFragments: 16 * 1024,
|
|
663
675
|
maxPayload: 100 * 1024 * 1024,
|
|
664
676
|
skipUTF8Validation: false,
|
|
665
677
|
perMessageDeflate: true,
|
|
@@ -693,7 +705,7 @@ function initAsClient(websocket, address, protocols, options) {
|
|
|
693
705
|
} else {
|
|
694
706
|
try {
|
|
695
707
|
parsedUrl = new URL(address);
|
|
696
|
-
} catch
|
|
708
|
+
} catch {
|
|
697
709
|
throw new SyntaxError(`Invalid URL: ${address}`);
|
|
698
710
|
}
|
|
699
711
|
}
|
|
@@ -755,11 +767,11 @@ function initAsClient(websocket, address, protocols, options) {
|
|
|
755
767
|
opts.timeout = opts.handshakeTimeout;
|
|
756
768
|
|
|
757
769
|
if (opts.perMessageDeflate) {
|
|
758
|
-
perMessageDeflate = new PerMessageDeflate(
|
|
759
|
-
opts.perMessageDeflate
|
|
760
|
-
false,
|
|
761
|
-
opts.maxPayload
|
|
762
|
-
);
|
|
770
|
+
perMessageDeflate = new PerMessageDeflate({
|
|
771
|
+
...opts.perMessageDeflate,
|
|
772
|
+
isServer: false,
|
|
773
|
+
maxPayload: opts.maxPayload
|
|
774
|
+
});
|
|
763
775
|
opts.headers['Sec-WebSocket-Extensions'] = format({
|
|
764
776
|
[PerMessageDeflate.extensionName]: perMessageDeflate.offer()
|
|
765
777
|
});
|
|
@@ -1017,6 +1029,8 @@ function initAsClient(websocket, address, protocols, options) {
|
|
|
1017
1029
|
websocket.setSocket(socket, head, {
|
|
1018
1030
|
allowSynchronousEvents: opts.allowSynchronousEvents,
|
|
1019
1031
|
generateMask: opts.generateMask,
|
|
1032
|
+
maxBufferedChunks: opts.maxBufferedChunks,
|
|
1033
|
+
maxFragments: opts.maxFragments,
|
|
1020
1034
|
maxPayload: opts.maxPayload,
|
|
1021
1035
|
skipUTF8Validation: opts.skipUTF8Validation
|
|
1022
1036
|
});
|
package/package.json
CHANGED
|
@@ -1,8 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@depup/ws",
|
|
3
|
-
"version": "8.
|
|
4
|
-
"description": "Simple to use, blazing fast and thoroughly tested websocket client and server for Node.js",
|
|
3
|
+
"version": "8.21.1-depup.0",
|
|
4
|
+
"description": "Simple to use, blazing fast and thoroughly tested websocket client and server for Node.js (with updated dependencies)",
|
|
5
5
|
"keywords": [
|
|
6
|
+
"ws",
|
|
7
|
+
"depup",
|
|
8
|
+
"updated-dependencies",
|
|
9
|
+
"security",
|
|
10
|
+
"latest",
|
|
11
|
+
"patched",
|
|
6
12
|
"HyBi",
|
|
7
13
|
"Push",
|
|
8
14
|
"RFC-6455",
|
|
@@ -35,7 +41,9 @@
|
|
|
35
41
|
"browser.js",
|
|
36
42
|
"index.js",
|
|
37
43
|
"lib/*.js",
|
|
38
|
-
"wrapper.mjs"
|
|
44
|
+
"wrapper.mjs",
|
|
45
|
+
"changes.json",
|
|
46
|
+
"README.md"
|
|
39
47
|
],
|
|
40
48
|
"scripts": {
|
|
41
49
|
"test": "nyc --reporter=lcov --reporter=text mocha --throw-deprecation test/*.test.js",
|
|
@@ -55,15 +63,28 @@
|
|
|
55
63
|
}
|
|
56
64
|
},
|
|
57
65
|
"devDependencies": {
|
|
66
|
+
"@eslint/js": "^10.0.1",
|
|
58
67
|
"benchmark": "^2.1.4",
|
|
59
68
|
"bufferutil": "^4.0.1",
|
|
60
|
-
"eslint": "^
|
|
69
|
+
"eslint": "^10.0.1",
|
|
61
70
|
"eslint-config-prettier": "^10.0.1",
|
|
62
71
|
"eslint-plugin-prettier": "^5.0.0",
|
|
63
|
-
"globals": "^
|
|
72
|
+
"globals": "^17.0.0",
|
|
64
73
|
"mocha": "^8.4.0",
|
|
65
74
|
"nyc": "^15.0.0",
|
|
66
75
|
"prettier": "^3.0.0",
|
|
67
76
|
"utf-8-validate": "^6.0.0"
|
|
77
|
+
},
|
|
78
|
+
"allowScripts": {
|
|
79
|
+
"bufferutil": true,
|
|
80
|
+
"utf-8-validate": true
|
|
81
|
+
},
|
|
82
|
+
"depup": {
|
|
83
|
+
"changes": {},
|
|
84
|
+
"depsUpdated": 0,
|
|
85
|
+
"originalPackage": "ws",
|
|
86
|
+
"originalVersion": "8.21.1",
|
|
87
|
+
"processedAt": "2026-07-21T16:27:05.729Z",
|
|
88
|
+
"smokeTest": "passed"
|
|
68
89
|
}
|
|
69
90
|
}
|
package/wrapper.mjs
CHANGED
|
@@ -1,8 +1,21 @@
|
|
|
1
1
|
import createWebSocketStream from './lib/stream.js';
|
|
2
|
+
import extension from './lib/extension.js';
|
|
3
|
+
import PerMessageDeflate from './lib/permessage-deflate.js';
|
|
2
4
|
import Receiver from './lib/receiver.js';
|
|
3
5
|
import Sender from './lib/sender.js';
|
|
6
|
+
import subprotocol from './lib/subprotocol.js';
|
|
4
7
|
import WebSocket from './lib/websocket.js';
|
|
5
8
|
import WebSocketServer from './lib/websocket-server.js';
|
|
6
9
|
|
|
7
|
-
export {
|
|
10
|
+
export {
|
|
11
|
+
createWebSocketStream,
|
|
12
|
+
extension,
|
|
13
|
+
PerMessageDeflate,
|
|
14
|
+
Receiver,
|
|
15
|
+
Sender,
|
|
16
|
+
subprotocol,
|
|
17
|
+
WebSocket,
|
|
18
|
+
WebSocketServer
|
|
19
|
+
};
|
|
20
|
+
|
|
8
21
|
export default WebSocket;
|