isomorfeus-transport 2.5.5 → 22.9.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (44) hide show
  1. checksums.yaml +4 -4
  2. data/lib/isomorfeus/core_ext/hash/deep_merge.rb +34 -0
  3. data/lib/isomorfeus/core_ext/kernel.rb +56 -0
  4. data/lib/isomorfeus/core_ext/object/deep_dup.rb +53 -0
  5. data/lib/isomorfeus/core_ext/object/duplicable.rb +60 -0
  6. data/lib/isomorfeus/transport/compressor_rack.rb +1 -1
  7. data/lib/isomorfeus/transport/config.rb +37 -0
  8. data/lib/isomorfeus/transport/rack_middleware.rb +16 -9
  9. data/lib/isomorfeus/transport/request_agent.rb +1 -0
  10. data/lib/isomorfeus/transport/server_socket_processor.rb +13 -1
  11. data/lib/isomorfeus/transport/version.rb +1 -1
  12. data/lib/isomorfeus/transport.rb +45 -38
  13. data/lib/isomorfeus-transport.rb +25 -16
  14. data/lib/lucid_channel.rb +103 -0
  15. data/lib/lucid_handler.rb +25 -0
  16. metadata +40 -90
  17. data/lib/isomorfeus/transport/handler/authentication_handler.rb +0 -55
  18. data/lib/isomorfeus/transport/imports.rb +0 -10
  19. data/lib/isomorfeus/transport/ssr_login.rb +0 -30
  20. data/lib/lucid_channel/base.rb +0 -8
  21. data/lib/lucid_channel/mixin.rb +0 -105
  22. data/lib/lucid_handler/base.rb +0 -8
  23. data/lib/lucid_handler/mixin.rb +0 -27
  24. data/node_modules/.package-lock.json +0 -27
  25. data/node_modules/ws/LICENSE +0 -19
  26. data/node_modules/ws/README.md +0 -489
  27. data/node_modules/ws/browser.js +0 -8
  28. data/node_modules/ws/index.js +0 -13
  29. data/node_modules/ws/lib/buffer-util.js +0 -126
  30. data/node_modules/ws/lib/constants.js +0 -12
  31. data/node_modules/ws/lib/event-target.js +0 -266
  32. data/node_modules/ws/lib/extension.js +0 -203
  33. data/node_modules/ws/lib/limiter.js +0 -55
  34. data/node_modules/ws/lib/permessage-deflate.js +0 -511
  35. data/node_modules/ws/lib/receiver.js +0 -618
  36. data/node_modules/ws/lib/sender.js +0 -478
  37. data/node_modules/ws/lib/stream.js +0 -159
  38. data/node_modules/ws/lib/subprotocol.js +0 -62
  39. data/node_modules/ws/lib/validation.js +0 -124
  40. data/node_modules/ws/lib/websocket-server.js +0 -488
  41. data/node_modules/ws/lib/websocket.js +0 -1264
  42. data/node_modules/ws/package.json +0 -61
  43. data/node_modules/ws/wrapper.mjs +0 -8
  44. data/package.json +0 -6
@@ -1,27 +0,0 @@
1
- {
2
- "name": "isomorfeus-transport",
3
- "lockfileVersion": 2,
4
- "requires": true,
5
- "packages": {
6
- "node_modules/ws": {
7
- "version": "8.5.0",
8
- "resolved": "https://registry.npmjs.org/ws/-/ws-8.5.0.tgz",
9
- "integrity": "sha512-BWX0SWVgLPzYwF8lTzEy1egjhS4S4OEAHfsO8o65WOVsrnSRGaSiUaa9e0ggGlkMTtBlmOpEXiie9RUcBO86qg==",
10
- "engines": {
11
- "node": ">=10.0.0"
12
- },
13
- "peerDependencies": {
14
- "bufferutil": "^4.0.1",
15
- "utf-8-validate": "^5.0.2"
16
- },
17
- "peerDependenciesMeta": {
18
- "bufferutil": {
19
- "optional": true
20
- },
21
- "utf-8-validate": {
22
- "optional": true
23
- }
24
- }
25
- }
26
- }
27
- }
@@ -1,19 +0,0 @@
1
- Copyright (c) 2011 Einar Otto Stangvik <einaros@gmail.com>
2
-
3
- Permission is hereby granted, free of charge, to any person obtaining a copy
4
- of this software and associated documentation files (the "Software"), to deal
5
- in the Software without restriction, including without limitation the rights
6
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
- copies of the Software, and to permit persons to whom the Software is
8
- furnished to do so, subject to the following conditions:
9
-
10
- The above copyright notice and this permission notice shall be included in all
11
- copies or substantial portions of the Software.
12
-
13
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
- SOFTWARE.
@@ -1,489 +0,0 @@
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/workflow/status/websockets/ws/CI/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 back end with the role of a client in the WebSocket
15
- communication. 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
- - [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
- ### Opt-in for performance
59
-
60
- There are 2 optional modules that can be installed along side with the ws
61
- module. These modules are binary addons which improve certain operations.
62
- Prebuilt binaries are available for the most popular platforms so you don't
63
- necessarily need to have a C++ compiler installed on your machine.
64
-
65
- - `npm install --save-optional bufferutil`: Allows to efficiently perform
66
- operations such as masking and unmasking the data payload of the WebSocket
67
- frames.
68
- - `npm install --save-optional utf-8-validate`: Allows to efficiently check if a
69
- message contains valid UTF-8.
70
-
71
- ## API docs
72
-
73
- See [`/doc/ws.md`](./doc/ws.md) for Node.js-like documentation of ws classes and
74
- utility functions.
75
-
76
- ## WebSocket compression
77
-
78
- ws supports the [permessage-deflate extension][permessage-deflate] which enables
79
- the client and server to negotiate a compression algorithm and its parameters,
80
- and then selectively apply it to the data payloads of each WebSocket message.
81
-
82
- The extension is disabled by default on the server and enabled by default on the
83
- client. It adds a significant overhead in terms of performance and memory
84
- consumption so we suggest to enable it only if it is really needed.
85
-
86
- Note that Node.js has a variety of issues with high-performance compression,
87
- where increased concurrency, especially on Linux, can lead to [catastrophic
88
- memory fragmentation][node-zlib-bug] and slow performance. If you intend to use
89
- permessage-deflate in production, it is worthwhile to set up a test
90
- representative of your workload and ensure Node.js/zlib will handle it with
91
- acceptable performance and memory usage.
92
-
93
- Tuning of permessage-deflate can be done via the options defined below. You can
94
- also use `zlibDeflateOptions` and `zlibInflateOptions`, which is passed directly
95
- into the creation of [raw deflate/inflate streams][node-zlib-deflaterawdocs].
96
-
97
- See [the docs][ws-server-options] for more options.
98
-
99
- ```js
100
- import WebSocket, { WebSocketServer } from 'ws';
101
-
102
- const wss = new WebSocketServer({
103
- port: 8080,
104
- perMessageDeflate: {
105
- zlibDeflateOptions: {
106
- // See zlib defaults.
107
- chunkSize: 1024,
108
- memLevel: 7,
109
- level: 3
110
- },
111
- zlibInflateOptions: {
112
- chunkSize: 10 * 1024
113
- },
114
- // Other options settable:
115
- clientNoContextTakeover: true, // Defaults to negotiated value.
116
- serverNoContextTakeover: true, // Defaults to negotiated value.
117
- serverMaxWindowBits: 10, // Defaults to negotiated value.
118
- // Below options specified as default values.
119
- concurrencyLimit: 10, // Limits zlib concurrency for perf.
120
- threshold: 1024 // Size (in bytes) below which messages
121
- // should not be compressed if context takeover is disabled.
122
- }
123
- });
124
- ```
125
-
126
- The client will only use the extension if it is supported and enabled on the
127
- server. To always disable the extension on the client set the
128
- `perMessageDeflate` option to `false`.
129
-
130
- ```js
131
- import WebSocket from 'ws';
132
-
133
- const ws = new WebSocket('ws://www.host.com/path', {
134
- perMessageDeflate: false
135
- });
136
- ```
137
-
138
- ## Usage examples
139
-
140
- ### Sending and receiving text data
141
-
142
- ```js
143
- import WebSocket from 'ws';
144
-
145
- const ws = new WebSocket('ws://www.host.com/path');
146
-
147
- ws.on('open', function open() {
148
- ws.send('something');
149
- });
150
-
151
- ws.on('message', function message(data) {
152
- console.log('received: %s', data);
153
- });
154
- ```
155
-
156
- ### Sending binary data
157
-
158
- ```js
159
- import WebSocket from 'ws';
160
-
161
- const ws = new WebSocket('ws://www.host.com/path');
162
-
163
- ws.on('open', function open() {
164
- const array = new Float32Array(5);
165
-
166
- for (var i = 0; i < array.length; ++i) {
167
- array[i] = i / 2;
168
- }
169
-
170
- ws.send(array);
171
- });
172
- ```
173
-
174
- ### Simple server
175
-
176
- ```js
177
- import { WebSocketServer } from 'ws';
178
-
179
- const wss = new WebSocketServer({ port: 8080 });
180
-
181
- wss.on('connection', function connection(ws) {
182
- ws.on('message', function message(data) {
183
- console.log('received: %s', data);
184
- });
185
-
186
- ws.send('something');
187
- });
188
- ```
189
-
190
- ### External HTTP/S server
191
-
192
- ```js
193
- import { createServer } from 'https';
194
- import { readFileSync } from 'fs';
195
- import { WebSocketServer } from 'ws';
196
-
197
- const server = createServer({
198
- cert: readFileSync('/path/to/cert.pem'),
199
- key: readFileSync('/path/to/key.pem')
200
- });
201
- const wss = new WebSocketServer({ server });
202
-
203
- wss.on('connection', function connection(ws) {
204
- ws.on('message', function message(data) {
205
- console.log('received: %s', data);
206
- });
207
-
208
- ws.send('something');
209
- });
210
-
211
- server.listen(8080);
212
- ```
213
-
214
- ### Multiple servers sharing a single HTTP/S server
215
-
216
- ```js
217
- import { createServer } from 'http';
218
- import { parse } from 'url';
219
- import { WebSocketServer } from 'ws';
220
-
221
- const server = createServer();
222
- const wss1 = new WebSocketServer({ noServer: true });
223
- const wss2 = new WebSocketServer({ noServer: true });
224
-
225
- wss1.on('connection', function connection(ws) {
226
- // ...
227
- });
228
-
229
- wss2.on('connection', function connection(ws) {
230
- // ...
231
- });
232
-
233
- server.on('upgrade', function upgrade(request, socket, head) {
234
- const { pathname } = parse(request.url);
235
-
236
- if (pathname === '/foo') {
237
- wss1.handleUpgrade(request, socket, head, function done(ws) {
238
- wss1.emit('connection', ws, request);
239
- });
240
- } else if (pathname === '/bar') {
241
- wss2.handleUpgrade(request, socket, head, function done(ws) {
242
- wss2.emit('connection', ws, request);
243
- });
244
- } else {
245
- socket.destroy();
246
- }
247
- });
248
-
249
- server.listen(8080);
250
- ```
251
-
252
- ### Client authentication
253
-
254
- ```js
255
- import WebSocket from 'ws';
256
- import { createServer } from 'http';
257
-
258
- const server = createServer();
259
- const wss = new WebSocketServer({ noServer: true });
260
-
261
- wss.on('connection', function connection(ws, request, client) {
262
- ws.on('message', function message(data) {
263
- console.log(`Received message ${data} from user ${client}`);
264
- });
265
- });
266
-
267
- server.on('upgrade', function upgrade(request, socket, head) {
268
- // This function is not defined on purpose. Implement it with your own logic.
269
- authenticate(request, function next(err, client) {
270
- if (err || !client) {
271
- socket.write('HTTP/1.1 401 Unauthorized\r\n\r\n');
272
- socket.destroy();
273
- return;
274
- }
275
-
276
- wss.handleUpgrade(request, socket, head, function done(ws) {
277
- wss.emit('connection', ws, request, client);
278
- });
279
- });
280
- });
281
-
282
- server.listen(8080);
283
- ```
284
-
285
- Also see the provided [example][session-parse-example] using `express-session`.
286
-
287
- ### Server broadcast
288
-
289
- A client WebSocket broadcasting to all connected WebSocket clients, including
290
- itself.
291
-
292
- ```js
293
- import WebSocket, { WebSocketServer } from 'ws';
294
-
295
- const wss = new WebSocketServer({ port: 8080 });
296
-
297
- wss.on('connection', function connection(ws) {
298
- ws.on('message', function message(data, isBinary) {
299
- wss.clients.forEach(function each(client) {
300
- if (client.readyState === WebSocket.OPEN) {
301
- client.send(data, { binary: isBinary });
302
- }
303
- });
304
- });
305
- });
306
- ```
307
-
308
- A client WebSocket broadcasting to every other connected WebSocket clients,
309
- excluding itself.
310
-
311
- ```js
312
- import WebSocket, { WebSocketServer } from 'ws';
313
-
314
- const wss = new WebSocketServer({ port: 8080 });
315
-
316
- wss.on('connection', function connection(ws) {
317
- ws.on('message', function message(data, isBinary) {
318
- wss.clients.forEach(function each(client) {
319
- if (client !== ws && client.readyState === WebSocket.OPEN) {
320
- client.send(data, { binary: isBinary });
321
- }
322
- });
323
- });
324
- });
325
- ```
326
-
327
- ### Round-trip time
328
-
329
- ```js
330
- import WebSocket from 'ws';
331
-
332
- const ws = new WebSocket('wss://websocket-echo.com/');
333
-
334
- ws.on('open', function open() {
335
- console.log('connected');
336
- ws.send(Date.now());
337
- });
338
-
339
- ws.on('close', function close() {
340
- console.log('disconnected');
341
- });
342
-
343
- ws.on('message', function message(data) {
344
- console.log(`Round-trip time: ${Date.now() - data} ms`);
345
-
346
- setTimeout(function timeout() {
347
- ws.send(Date.now());
348
- }, 500);
349
- });
350
- ```
351
-
352
- ### Use the Node.js streams API
353
-
354
- ```js
355
- import WebSocket, { createWebSocketStream } from 'ws';
356
-
357
- const ws = new WebSocket('wss://websocket-echo.com/');
358
-
359
- const duplex = createWebSocketStream(ws, { encoding: 'utf8' });
360
-
361
- duplex.pipe(process.stdout);
362
- process.stdin.pipe(duplex);
363
- ```
364
-
365
- ### Other examples
366
-
367
- For a full example with a browser client communicating with a ws server, see the
368
- examples folder.
369
-
370
- Otherwise, see the test cases.
371
-
372
- ## FAQ
373
-
374
- ### How to get the IP address of the client?
375
-
376
- The remote IP address can be obtained from the raw socket.
377
-
378
- ```js
379
- import { WebSocketServer } from 'ws';
380
-
381
- const wss = new WebSocketServer({ port: 8080 });
382
-
383
- wss.on('connection', function connection(ws, req) {
384
- const ip = req.socket.remoteAddress;
385
- });
386
- ```
387
-
388
- When the server runs behind a proxy like NGINX, the de-facto standard is to use
389
- the `X-Forwarded-For` header.
390
-
391
- ```js
392
- wss.on('connection', function connection(ws, req) {
393
- const ip = req.headers['x-forwarded-for'].split(',')[0].trim();
394
- });
395
- ```
396
-
397
- ### How to detect and close broken connections?
398
-
399
- Sometimes the link between the server and the client can be interrupted in a way
400
- that keeps both the server and the client unaware of the broken state of the
401
- connection (e.g. when pulling the cord).
402
-
403
- In these cases ping messages can be used as a means to verify that the remote
404
- endpoint is still responsive.
405
-
406
- ```js
407
- import { WebSocketServer } from 'ws';
408
-
409
- function heartbeat() {
410
- this.isAlive = true;
411
- }
412
-
413
- const wss = new WebSocketServer({ port: 8080 });
414
-
415
- wss.on('connection', function connection(ws) {
416
- ws.isAlive = true;
417
- ws.on('pong', heartbeat);
418
- });
419
-
420
- const interval = setInterval(function ping() {
421
- wss.clients.forEach(function each(ws) {
422
- if (ws.isAlive === false) return ws.terminate();
423
-
424
- ws.isAlive = false;
425
- ws.ping();
426
- });
427
- }, 30000);
428
-
429
- wss.on('close', function close() {
430
- clearInterval(interval);
431
- });
432
- ```
433
-
434
- Pong messages are automatically sent in response to ping messages as required by
435
- the spec.
436
-
437
- Just like the server example above your clients might as well lose connection
438
- without knowing it. You might want to add a ping listener on your clients to
439
- prevent that. A simple implementation would be:
440
-
441
- ```js
442
- import WebSocket from 'ws';
443
-
444
- function heartbeat() {
445
- clearTimeout(this.pingTimeout);
446
-
447
- // Use `WebSocket#terminate()`, which immediately destroys the connection,
448
- // instead of `WebSocket#close()`, which waits for the close timer.
449
- // Delay should be equal to the interval at which your server
450
- // sends out pings plus a conservative assumption of the latency.
451
- this.pingTimeout = setTimeout(() => {
452
- this.terminate();
453
- }, 30000 + 1000);
454
- }
455
-
456
- const client = new WebSocket('wss://websocket-echo.com/');
457
-
458
- client.on('open', heartbeat);
459
- client.on('ping', heartbeat);
460
- client.on('close', function clear() {
461
- clearTimeout(this.pingTimeout);
462
- });
463
- ```
464
-
465
- ### How to connect via a proxy?
466
-
467
- Use a custom `http.Agent` implementation like [https-proxy-agent][] or
468
- [socks-proxy-agent][].
469
-
470
- ## Changelog
471
-
472
- We're using the GitHub [releases][changelog] for changelog entries.
473
-
474
- ## License
475
-
476
- [MIT](LICENSE)
477
-
478
- [changelog]: https://github.com/websockets/ws/releases
479
- [client-report]: http://websockets.github.io/ws/autobahn/clients/
480
- [https-proxy-agent]: https://github.com/TooTallNate/node-https-proxy-agent
481
- [node-zlib-bug]: https://github.com/nodejs/node/issues/8871
482
- [node-zlib-deflaterawdocs]:
483
- https://nodejs.org/api/zlib.html#zlib_zlib_createdeflateraw_options
484
- [permessage-deflate]: https://tools.ietf.org/html/rfc7692
485
- [server-report]: http://websockets.github.io/ws/autobahn/servers/
486
- [session-parse-example]: ./examples/express-session-parse
487
- [socks-proxy-agent]: https://github.com/TooTallNate/node-socks-proxy-agent
488
- [ws-server-options]:
489
- https://github.com/websockets/ws/blob/master/doc/ws.md#new-websocketserveroptions-callback
@@ -1,8 +0,0 @@
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
- };
@@ -1,13 +0,0 @@
1
- 'use strict';
2
-
3
- const WebSocket = require('./lib/websocket');
4
-
5
- WebSocket.createWebSocketStream = require('./lib/stream');
6
- WebSocket.Server = require('./lib/websocket-server');
7
- WebSocket.Receiver = require('./lib/receiver');
8
- WebSocket.Sender = require('./lib/sender');
9
-
10
- WebSocket.WebSocket = WebSocket;
11
- WebSocket.WebSocketServer = WebSocket.Server;
12
-
13
- module.exports = WebSocket;
@@ -1,126 +0,0 @@
1
- 'use strict';
2
-
3
- const { EMPTY_BUFFER } = require('./constants');
4
-
5
- /**
6
- * Merges an array of buffers into a new buffer.
7
- *
8
- * @param {Buffer[]} list The array of buffers to concat
9
- * @param {Number} totalLength The total length of buffers in the list
10
- * @return {Buffer} The resulting buffer
11
- * @public
12
- */
13
- function concat(list, totalLength) {
14
- if (list.length === 0) return EMPTY_BUFFER;
15
- if (list.length === 1) return list[0];
16
-
17
- const target = Buffer.allocUnsafe(totalLength);
18
- let offset = 0;
19
-
20
- for (let i = 0; i < list.length; i++) {
21
- const buf = list[i];
22
- target.set(buf, offset);
23
- offset += buf.length;
24
- }
25
-
26
- if (offset < totalLength) return target.slice(0, offset);
27
-
28
- return target;
29
- }
30
-
31
- /**
32
- * Masks a buffer using the given mask.
33
- *
34
- * @param {Buffer} source The buffer to mask
35
- * @param {Buffer} mask The mask to use
36
- * @param {Buffer} output The buffer where to store the result
37
- * @param {Number} offset The offset at which to start writing
38
- * @param {Number} length The number of bytes to mask.
39
- * @public
40
- */
41
- function _mask(source, mask, output, offset, length) {
42
- for (let i = 0; i < length; i++) {
43
- output[offset + i] = source[i] ^ mask[i & 3];
44
- }
45
- }
46
-
47
- /**
48
- * Unmasks a buffer using the given mask.
49
- *
50
- * @param {Buffer} buffer The buffer to unmask
51
- * @param {Buffer} mask The mask to use
52
- * @public
53
- */
54
- function _unmask(buffer, mask) {
55
- for (let i = 0; i < buffer.length; i++) {
56
- buffer[i] ^= mask[i & 3];
57
- }
58
- }
59
-
60
- /**
61
- * Converts a buffer to an `ArrayBuffer`.
62
- *
63
- * @param {Buffer} buf The buffer to convert
64
- * @return {ArrayBuffer} Converted buffer
65
- * @public
66
- */
67
- function toArrayBuffer(buf) {
68
- if (buf.byteLength === buf.buffer.byteLength) {
69
- return buf.buffer;
70
- }
71
-
72
- return buf.buffer.slice(buf.byteOffset, buf.byteOffset + buf.byteLength);
73
- }
74
-
75
- /**
76
- * Converts `data` to a `Buffer`.
77
- *
78
- * @param {*} data The data to convert
79
- * @return {Buffer} The buffer
80
- * @throws {TypeError}
81
- * @public
82
- */
83
- function toBuffer(data) {
84
- toBuffer.readOnly = true;
85
-
86
- if (Buffer.isBuffer(data)) return data;
87
-
88
- let buf;
89
-
90
- if (data instanceof ArrayBuffer) {
91
- buf = Buffer.from(data);
92
- } else if (ArrayBuffer.isView(data)) {
93
- buf = Buffer.from(data.buffer, data.byteOffset, data.byteLength);
94
- } else {
95
- buf = Buffer.from(data);
96
- toBuffer.readOnly = false;
97
- }
98
-
99
- return buf;
100
- }
101
-
102
- try {
103
- const bufferUtil = require('bufferutil');
104
-
105
- module.exports = {
106
- concat,
107
- mask(source, mask, output, offset, length) {
108
- if (length < 48) _mask(source, mask, output, offset, length);
109
- else bufferUtil.mask(source, mask, output, offset, length);
110
- },
111
- toArrayBuffer,
112
- toBuffer,
113
- unmask(buffer, mask) {
114
- if (buffer.length < 32) _unmask(buffer, mask);
115
- else bufferUtil.unmask(buffer, mask);
116
- }
117
- };
118
- } catch (e) /* istanbul ignore next */ {
119
- module.exports = {
120
- concat,
121
- mask: _mask,
122
- toArrayBuffer,
123
- toBuffer,
124
- unmask: _unmask
125
- };
126
- }
@@ -1,12 +0,0 @@
1
- 'use strict';
2
-
3
- module.exports = {
4
- BINARY_TYPES: ['nodebuffer', 'arraybuffer', 'fragments'],
5
- EMPTY_BUFFER: Buffer.alloc(0),
6
- GUID: '258EAFA5-E914-47DA-95CA-C5AB0DC85B11',
7
- kForOnEventAttribute: Symbol('kIsForOnEventAttribute'),
8
- kListener: Symbol('kListener'),
9
- kStatusCode: Symbol('status-code'),
10
- kWebSocket: Symbol('websocket'),
11
- NOOP: () => {}
12
- };