Event Server Client done
This commit is contained in:
1 parent
e0bc1cf3b3
commit
7515fd910e
25 files changed
+786
-196
No files matched your search
@@ -427,6 +427,14 @@
|
||||
"fileName": "MiscellaneousFunctions.js"
|
||||
}
|
||||
],
|
||||
"taskModules": [
|
||||
{
|
||||
"name": "Event Server Client",
|
||||
"propertyName": "eventServerClient",
|
||||
"functionName": "newSuperalgosTaskModulesEventServerClient",
|
||||
"fileName": "EventServerClient.js"
|
||||
}
|
||||
],
|
||||
"processModules": [
|
||||
{
|
||||
"name": "Status Report",
|
||||
|
||||
@@ -18,7 +18,6 @@
|
||||
let processThisDependsOn; // This is the process this process needs to wait for it to finishes in order to start.
|
||||
let currentProcessKey
|
||||
|
||||
let EVENT_SERVER_CLIENT = require('./EventServerClient.js');
|
||||
let eventServerClient
|
||||
|
||||
return thisObject;
|
||||
@@ -127,7 +126,7 @@
|
||||
' -> port = ' + networkNode.config.webSocketsPort + '.');
|
||||
|
||||
|
||||
eventServerClient = EVENT_SERVER_CLIENT.newEventsServerClient(networkNode.config.host, networkNode.config.webSocketsPort)
|
||||
eventServerClient = TS.projects.superalgos.taskModules.eventServerClient.newSuperalgosTaskModulesEventServerClient(networkNode.config.host, networkNode.config.webSocketsPort)
|
||||
eventServerClient.initialize(onConnected)
|
||||
|
||||
function onConnected() {
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
exports.newEventsServerClient = function newEventsServerClient(host, port) {
|
||||
exports.newSuperalgosTaskModulesEventServerClient = function (host, port) {
|
||||
|
||||
const MODULE = "Task Server "
|
||||
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2011 Einar Otto Stangvik <einaros@gmail.com>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
+496
@@ -0,0 +1,496 @@
|
||||
# ws: a Node.js WebSocket library
|
||||
|
||||
[](https://www.npmjs.com/package/ws)
|
||||
[](https://travis-ci.com/websockets/ws)
|
||||
[](https://ci.appveyor.com/project/lpinca/ws)
|
||||
[](https://coveralls.io/github/websockets/ws)
|
||||
|
||||
ws is a simple to use, blazing fast, and thoroughly tested WebSocket client and
|
||||
server implementation.
|
||||
|
||||
Passes the quite extensive Autobahn test suite: [server][server-report],
|
||||
[client][client-report].
|
||||
|
||||
**Note**: This module does not work in the browser. The client in the docs is a
|
||||
reference to a back end with the role of a client in the WebSocket
|
||||
communication. Browser clients must use the native
|
||||
[`WebSocket`](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket)
|
||||
object. To make the same code work seamlessly on Node.js and the browser, you
|
||||
can use one of the many wrappers available on npm, like
|
||||
[isomorphic-ws](https://github.com/heineiuo/isomorphic-ws).
|
||||
|
||||
## Table of Contents
|
||||
|
||||
- [Protocol support](#protocol-support)
|
||||
- [Installing](#installing)
|
||||
- [Opt-in for performance and spec compliance](#opt-in-for-performance-and-spec-compliance)
|
||||
- [API docs](#api-docs)
|
||||
- [WebSocket compression](#websocket-compression)
|
||||
- [Usage examples](#usage-examples)
|
||||
- [Sending and receiving text data](#sending-and-receiving-text-data)
|
||||
- [Sending binary data](#sending-binary-data)
|
||||
- [Simple server](#simple-server)
|
||||
- [External HTTP/S server](#external-https-server)
|
||||
- [Multiple servers sharing a single HTTP/S server](#multiple-servers-sharing-a-single-https-server)
|
||||
- [Client authentication](#client-authentication)
|
||||
- [Server broadcast](#server-broadcast)
|
||||
- [echo.websocket.org demo](#echowebsocketorg-demo)
|
||||
- [Use the Node.js streams API](#use-the-nodejs-streams-api)
|
||||
- [Other examples](#other-examples)
|
||||
- [FAQ](#faq)
|
||||
- [How to get the IP address of the client?](#how-to-get-the-ip-address-of-the-client)
|
||||
- [How to detect and close broken connections?](#how-to-detect-and-close-broken-connections)
|
||||
- [How to connect via a proxy?](#how-to-connect-via-a-proxy)
|
||||
- [Changelog](#changelog)
|
||||
- [License](#license)
|
||||
|
||||
## Protocol support
|
||||
|
||||
- **HyBi drafts 07-12** (Use the option `protocolVersion: 8`)
|
||||
- **HyBi drafts 13-17** (Current default, alternatively option
|
||||
`protocolVersion: 13`)
|
||||
|
||||
## Installing
|
||||
|
||||
```
|
||||
npm install ws
|
||||
```
|
||||
|
||||
### Opt-in for performance and spec compliance
|
||||
|
||||
There are 2 optional modules that can be installed along side with the ws
|
||||
module. These modules are binary addons which improve certain operations.
|
||||
Prebuilt binaries are available for the most popular platforms so you don't
|
||||
necessarily need to have a C++ compiler installed on your machine.
|
||||
|
||||
- `npm install --save-optional bufferutil`: Allows to efficiently perform
|
||||
operations such as masking and unmasking the data payload of the WebSocket
|
||||
frames.
|
||||
- `npm install --save-optional utf-8-validate`: Allows to efficiently check if a
|
||||
message contains valid UTF-8 as required by the spec.
|
||||
|
||||
## API docs
|
||||
|
||||
See [`/doc/ws.md`](./doc/ws.md) for Node.js-like documentation of ws classes and
|
||||
utility functions.
|
||||
|
||||
## WebSocket compression
|
||||
|
||||
ws supports the [permessage-deflate extension][permessage-deflate] which enables
|
||||
the client and server to negotiate a compression algorithm and its parameters,
|
||||
and then selectively apply it to the data payloads of each WebSocket message.
|
||||
|
||||
The extension is disabled by default on the server and enabled by default on the
|
||||
client. It adds a significant overhead in terms of performance and memory
|
||||
consumption so we suggest to enable it only if it is really needed.
|
||||
|
||||
Note that Node.js has a variety of issues with high-performance compression,
|
||||
where increased concurrency, especially on Linux, can lead to [catastrophic
|
||||
memory fragmentation][node-zlib-bug] and slow performance. If you intend to use
|
||||
permessage-deflate in production, it is worthwhile to set up a test
|
||||
representative of your workload and ensure Node.js/zlib will handle it with
|
||||
acceptable performance and memory usage.
|
||||
|
||||
Tuning of permessage-deflate can be done via the options defined below. You can
|
||||
also use `zlibDeflateOptions` and `zlibInflateOptions`, which is passed directly
|
||||
into the creation of [raw deflate/inflate streams][node-zlib-deflaterawdocs].
|
||||
|
||||
See [the docs][ws-server-options] for more options.
|
||||
|
||||
```js
|
||||
const WebSocket = require('ws');
|
||||
|
||||
const wss = new WebSocket.Server({
|
||||
port: 8080,
|
||||
perMessageDeflate: {
|
||||
zlibDeflateOptions: {
|
||||
// See zlib defaults.
|
||||
chunkSize: 1024,
|
||||
memLevel: 7,
|
||||
level: 3
|
||||
},
|
||||
zlibInflateOptions: {
|
||||
chunkSize: 10 * 1024
|
||||
},
|
||||
// Other options settable:
|
||||
clientNoContextTakeover: true, // Defaults to negotiated value.
|
||||
serverNoContextTakeover: true, // Defaults to negotiated value.
|
||||
serverMaxWindowBits: 10, // Defaults to negotiated value.
|
||||
// Below options specified as default values.
|
||||
concurrencyLimit: 10, // Limits zlib concurrency for perf.
|
||||
threshold: 1024 // Size (in bytes) below which messages
|
||||
// should not be compressed.
|
||||
}
|
||||
});
|
||||
```
|
||||
|
||||
The client will only use the extension if it is supported and enabled on the
|
||||
server. To always disable the extension on the client set the
|
||||
`perMessageDeflate` option to `false`.
|
||||
|
||||
```js
|
||||
const WebSocket = require('ws');
|
||||
|
||||
const ws = new WebSocket('ws://www.host.com/path', {
|
||||
perMessageDeflate: false
|
||||
});
|
||||
```
|
||||
|
||||
## Usage examples
|
||||
|
||||
### Sending and receiving text data
|
||||
|
||||
```js
|
||||
const WebSocket = require('ws');
|
||||
|
||||
const ws = new WebSocket('ws://www.host.com/path');
|
||||
|
||||
ws.on('open', function open() {
|
||||
ws.send('something');
|
||||
});
|
||||
|
||||
ws.on('message', function incoming(data) {
|
||||
console.log(data);
|
||||
});
|
||||
```
|
||||
|
||||
### Sending binary data
|
||||
|
||||
```js
|
||||
const WebSocket = require('ws');
|
||||
|
||||
const ws = new WebSocket('ws://www.host.com/path');
|
||||
|
||||
ws.on('open', function open() {
|
||||
const array = new Float32Array(5);
|
||||
|
||||
for (var i = 0; i < array.length; ++i) {
|
||||
array[i] = i / 2;
|
||||
}
|
||||
|
||||
ws.send(array);
|
||||
});
|
||||
```
|
||||
|
||||
### Simple server
|
||||
|
||||
```js
|
||||
const WebSocket = require('ws');
|
||||
|
||||
const wss = new WebSocket.Server({ port: 8080 });
|
||||
|
||||
wss.on('connection', function connection(ws) {
|
||||
ws.on('message', function incoming(message) {
|
||||
console.log('received: %s', message);
|
||||
});
|
||||
|
||||
ws.send('something');
|
||||
});
|
||||
```
|
||||
|
||||
### External HTTP/S server
|
||||
|
||||
```js
|
||||
const fs = require('fs');
|
||||
const https = require('https');
|
||||
const WebSocket = require('ws');
|
||||
|
||||
const server = https.createServer({
|
||||
cert: fs.readFileSync('/path/to/cert.pem'),
|
||||
key: fs.readFileSync('/path/to/key.pem')
|
||||
});
|
||||
const wss = new WebSocket.Server({ server });
|
||||
|
||||
wss.on('connection', function connection(ws) {
|
||||
ws.on('message', function incoming(message) {
|
||||
console.log('received: %s', message);
|
||||
});
|
||||
|
||||
ws.send('something');
|
||||
});
|
||||
|
||||
server.listen(8080);
|
||||
```
|
||||
|
||||
### Multiple servers sharing a single HTTP/S server
|
||||
|
||||
```js
|
||||
const http = require('http');
|
||||
const WebSocket = require('ws');
|
||||
const url = require('url');
|
||||
|
||||
const server = http.createServer();
|
||||
const wss1 = new WebSocket.Server({ noServer: true });
|
||||
const wss2 = new WebSocket.Server({ noServer: true });
|
||||
|
||||
wss1.on('connection', function connection(ws) {
|
||||
// ...
|
||||
});
|
||||
|
||||
wss2.on('connection', function connection(ws) {
|
||||
// ...
|
||||
});
|
||||
|
||||
server.on('upgrade', function upgrade(request, socket, head) {
|
||||
const pathname = url.parse(request.url).pathname;
|
||||
|
||||
if (pathname === '/foo') {
|
||||
wss1.handleUpgrade(request, socket, head, function done(ws) {
|
||||
wss1.emit('connection', ws, request);
|
||||
});
|
||||
} else if (pathname === '/bar') {
|
||||
wss2.handleUpgrade(request, socket, head, function done(ws) {
|
||||
wss2.emit('connection', ws, request);
|
||||
});
|
||||
} else {
|
||||
socket.destroy();
|
||||
}
|
||||
});
|
||||
|
||||
server.listen(8080);
|
||||
```
|
||||
|
||||
### Client authentication
|
||||
|
||||
```js
|
||||
const http = require('http');
|
||||
const WebSocket = require('ws');
|
||||
|
||||
const server = http.createServer();
|
||||
const wss = new WebSocket.Server({ noServer: true });
|
||||
|
||||
wss.on('connection', function connection(ws, request, client) {
|
||||
ws.on('message', function message(msg) {
|
||||
console.log(`Received message ${msg} from user ${client}`);
|
||||
});
|
||||
});
|
||||
|
||||
server.on('upgrade', function upgrade(request, socket, head) {
|
||||
// This function is not defined on purpose. Implement it with your own logic.
|
||||
authenticate(request, (err, client) => {
|
||||
if (err || !client) {
|
||||
socket.write('HTTP/1.1 401 Unauthorized\r\n\r\n');
|
||||
socket.destroy();
|
||||
return;
|
||||
}
|
||||
|
||||
wss.handleUpgrade(request, socket, head, function done(ws) {
|
||||
wss.emit('connection', ws, request, client);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
server.listen(8080);
|
||||
```
|
||||
|
||||
Also see the provided [example][session-parse-example] using `express-session`.
|
||||
|
||||
### Server broadcast
|
||||
|
||||
A client WebSocket broadcasting to all connected WebSocket clients, including
|
||||
itself.
|
||||
|
||||
```js
|
||||
const WebSocket = require('ws');
|
||||
|
||||
const wss = new WebSocket.Server({ port: 8080 });
|
||||
|
||||
wss.on('connection', function connection(ws) {
|
||||
ws.on('message', function incoming(data) {
|
||||
wss.clients.forEach(function each(client) {
|
||||
if (client.readyState === WebSocket.OPEN) {
|
||||
client.send(data);
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
```
|
||||
|
||||
A client WebSocket broadcasting to every other connected WebSocket clients,
|
||||
excluding itself.
|
||||
|
||||
```js
|
||||
const WebSocket = require('ws');
|
||||
|
||||
const wss = new WebSocket.Server({ port: 8080 });
|
||||
|
||||
wss.on('connection', function connection(ws) {
|
||||
ws.on('message', function incoming(data) {
|
||||
wss.clients.forEach(function each(client) {
|
||||
if (client !== ws && client.readyState === WebSocket.OPEN) {
|
||||
client.send(data);
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
```
|
||||
|
||||
### echo.websocket.org demo
|
||||
|
||||
```js
|
||||
const WebSocket = require('ws');
|
||||
|
||||
const ws = new WebSocket('wss://echo.websocket.org/', {
|
||||
origin: 'https://websocket.org'
|
||||
});
|
||||
|
||||
ws.on('open', function open() {
|
||||
console.log('connected');
|
||||
ws.send(Date.now());
|
||||
});
|
||||
|
||||
ws.on('close', function close() {
|
||||
console.log('disconnected');
|
||||
});
|
||||
|
||||
ws.on('message', function incoming(data) {
|
||||
console.log(`Roundtrip time: ${Date.now() - data} ms`);
|
||||
|
||||
setTimeout(function timeout() {
|
||||
ws.send(Date.now());
|
||||
}, 500);
|
||||
});
|
||||
```
|
||||
|
||||
### Use the Node.js streams API
|
||||
|
||||
```js
|
||||
const WebSocket = require('ws');
|
||||
|
||||
const ws = new WebSocket('wss://echo.websocket.org/', {
|
||||
origin: 'https://websocket.org'
|
||||
});
|
||||
|
||||
const duplex = WebSocket.createWebSocketStream(ws, { encoding: 'utf8' });
|
||||
|
||||
duplex.pipe(process.stdout);
|
||||
process.stdin.pipe(duplex);
|
||||
```
|
||||
|
||||
### Other examples
|
||||
|
||||
For a full example with a browser client communicating with a ws server, see the
|
||||
examples folder.
|
||||
|
||||
Otherwise, see the test cases.
|
||||
|
||||
## FAQ
|
||||
|
||||
### How to get the IP address of the client?
|
||||
|
||||
The remote IP address can be obtained from the raw socket.
|
||||
|
||||
```js
|
||||
const WebSocket = require('ws');
|
||||
|
||||
const wss = new WebSocket.Server({ port: 8080 });
|
||||
|
||||
wss.on('connection', function connection(ws, req) {
|
||||
const ip = req.socket.remoteAddress;
|
||||
});
|
||||
```
|
||||
|
||||
When the server runs behind a proxy like NGINX, the de-facto standard is to use
|
||||
the `X-Forwarded-For` header.
|
||||
|
||||
```js
|
||||
wss.on('connection', function connection(ws, req) {
|
||||
const ip = req.headers['x-forwarded-for'].split(/\s*,\s*/)[0];
|
||||
});
|
||||
```
|
||||
|
||||
### How to detect and close broken connections?
|
||||
|
||||
Sometimes the link between the server and the client can be interrupted in a way
|
||||
that keeps both the server and the client unaware of the broken state of the
|
||||
connection (e.g. when pulling the cord).
|
||||
|
||||
In these cases ping messages can be used as a means to verify that the remote
|
||||
endpoint is still responsive.
|
||||
|
||||
```js
|
||||
const WebSocket = require('ws');
|
||||
|
||||
function noop() {}
|
||||
|
||||
function heartbeat() {
|
||||
this.isAlive = true;
|
||||
}
|
||||
|
||||
const wss = new WebSocket.Server({ port: 8080 });
|
||||
|
||||
wss.on('connection', function connection(ws) {
|
||||
ws.isAlive = true;
|
||||
ws.on('pong', heartbeat);
|
||||
});
|
||||
|
||||
const interval = setInterval(function ping() {
|
||||
wss.clients.forEach(function each(ws) {
|
||||
if (ws.isAlive === false) return ws.terminate();
|
||||
|
||||
ws.isAlive = false;
|
||||
ws.ping(noop);
|
||||
});
|
||||
}, 30000);
|
||||
|
||||
wss.on('close', function close() {
|
||||
clearInterval(interval);
|
||||
});
|
||||
```
|
||||
|
||||
Pong messages are automatically sent in response to ping messages as required by
|
||||
the spec.
|
||||
|
||||
Just like the server example above your clients might as well lose connection
|
||||
without knowing it. You might want to add a ping listener on your clients to
|
||||
prevent that. A simple implementation would be:
|
||||
|
||||
```js
|
||||
const WebSocket = require('ws');
|
||||
|
||||
function heartbeat() {
|
||||
clearTimeout(this.pingTimeout);
|
||||
|
||||
// Use `WebSocket#terminate()`, which immediately destroys the connection,
|
||||
// instead of `WebSocket#close()`, which waits for the close timer.
|
||||
// Delay should be equal to the interval at which your server
|
||||
// sends out pings plus a conservative assumption of the latency.
|
||||
this.pingTimeout = setTimeout(() => {
|
||||
this.terminate();
|
||||
}, 30000 + 1000);
|
||||
}
|
||||
|
||||
const client = new WebSocket('wss://echo.websocket.org/');
|
||||
|
||||
client.on('open', heartbeat);
|
||||
client.on('ping', heartbeat);
|
||||
client.on('close', function clear() {
|
||||
clearTimeout(this.pingTimeout);
|
||||
});
|
||||
```
|
||||
|
||||
### How to connect via a proxy?
|
||||
|
||||
Use a custom `http.Agent` implementation like [https-proxy-agent][] or
|
||||
[socks-proxy-agent][].
|
||||
|
||||
## Changelog
|
||||
|
||||
We're using the GitHub [releases][changelog] for changelog entries.
|
||||
|
||||
## License
|
||||
|
||||
[MIT](LICENSE)
|
||||
|
||||
[changelog]: https://github.com/websockets/ws/releases
|
||||
[client-report]: http://websockets.github.io/ws/autobahn/clients/
|
||||
[https-proxy-agent]: https://github.com/TooTallNate/node-https-proxy-agent
|
||||
[node-zlib-bug]: https://github.com/nodejs/node/issues/8871
|
||||
[node-zlib-deflaterawdocs]:
|
||||
https://nodejs.org/api/zlib.html#zlib_zlib_createdeflateraw_options
|
||||
[permessage-deflate]: https://tools.ietf.org/html/rfc7692
|
||||
[server-report]: http://websockets.github.io/ws/autobahn/servers/
|
||||
[session-parse-example]: ./examples/express-session-parse
|
||||
[socks-proxy-agent]: https://github.com/TooTallNate/node-socks-proxy-agent
|
||||
[ws-server-options]:
|
||||
https://github.com/websockets/ws/blob/master/doc/ws.md#new-websocketserveroptions-callback
|
||||
Generated
Vendored
+1
-1
@@ -1,6 +1,6 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = function() {
|
||||
module.exports = function () {
|
||||
throw new Error(
|
||||
'ws does not work in the browser. Browser clients must use the native ' +
|
||||
'WebSocket object'
|
||||
Generated
Vendored
File renamed without changes.
Generated
Vendored
File renamed without changes.
Generated
Vendored
File renamed without changes.
Generated
Vendored
+18
-11
@@ -10,7 +10,8 @@ class Event {
|
||||
* Create a new `Event`.
|
||||
*
|
||||
* @param {String} type The name of the event
|
||||
* @param {Object} target A reference to the target to which the event was dispatched
|
||||
* @param {Object} target A reference to the target to which the event was
|
||||
* dispatched
|
||||
*/
|
||||
constructor(type, target) {
|
||||
this.target = target;
|
||||
@@ -29,7 +30,8 @@ class MessageEvent extends Event {
|
||||
* Create a new `MessageEvent`.
|
||||
*
|
||||
* @param {(String|Buffer|ArrayBuffer|Buffer[])} data The received data
|
||||
* @param {WebSocket} target A reference to the target to which the event was dispatched
|
||||
* @param {WebSocket} target A reference to the target to which the event was
|
||||
* dispatched
|
||||
*/
|
||||
constructor(data, target) {
|
||||
super('message', target);
|
||||
@@ -48,9 +50,12 @@ class CloseEvent extends Event {
|
||||
/**
|
||||
* Create a new `CloseEvent`.
|
||||
*
|
||||
* @param {Number} code The status code explaining why the connection is being closed
|
||||
* @param {String} reason A human-readable string explaining why the connection is closing
|
||||
* @param {WebSocket} target A reference to the target to which the event was dispatched
|
||||
* @param {Number} code The status code explaining why the connection is being
|
||||
* closed
|
||||
* @param {String} reason A human-readable string explaining why the
|
||||
* connection is closing
|
||||
* @param {WebSocket} target A reference to the target to which the event was
|
||||
* dispatched
|
||||
*/
|
||||
constructor(code, reason, target) {
|
||||
super('close', target);
|
||||
@@ -71,7 +76,8 @@ class OpenEvent extends Event {
|
||||
/**
|
||||
* Create a new `OpenEvent`.
|
||||
*
|
||||
* @param {WebSocket} target A reference to the target to which the event was dispatched
|
||||
* @param {WebSocket} target A reference to the target to which the event was
|
||||
* dispatched
|
||||
*/
|
||||
constructor(target) {
|
||||
super('open', target);
|
||||
@@ -89,7 +95,8 @@ class ErrorEvent extends Event {
|
||||
* Create a new `ErrorEvent`.
|
||||
*
|
||||
* @param {Object} error The error that generated this event
|
||||
* @param {WebSocket} target A reference to the target to which the event was dispatched
|
||||
* @param {WebSocket} target A reference to the target to which the event was
|
||||
* dispatched
|
||||
*/
|
||||
constructor(error, target) {
|
||||
super('error', target);
|
||||
@@ -111,11 +118,11 @@ const EventTarget = {
|
||||
*
|
||||
* @param {String} type A string representing the event type to listen for
|
||||
* @param {Function} listener The listener to add
|
||||
* @param {Object} options An options object specifies characteristics about
|
||||
* @param {Object} [options] An options object specifies characteristics about
|
||||
* the event listener
|
||||
* @param {Boolean} options.once A `Boolean`` indicating that the listener
|
||||
* should be invoked at most once after being added. If `true`, the
|
||||
* listener would be automatically removed when invoked.
|
||||
* @param {Boolean} [options.once=false] A `Boolean`` indicating that the
|
||||
* listener should be invoked at most once after being added. If `true`,
|
||||
* the listener would be automatically removed when invoked.
|
||||
* @public
|
||||
*/
|
||||
addEventListener(type, listener, options) {
|
||||
Generated
Vendored
File renamed without changes.
Generated
Vendored
+3
-2
@@ -11,8 +11,8 @@ class Limiter {
|
||||
/**
|
||||
* Creates a new `Limiter`.
|
||||
*
|
||||
* @param {Number} concurrency The maximum number of jobs allowed to run
|
||||
* concurrently
|
||||
* @param {Number} [concurrency=Infinity] The maximum number of jobs allowed
|
||||
* to run concurrently
|
||||
*/
|
||||
constructor(concurrency) {
|
||||
this[kDone] = () => {
|
||||
@@ -27,6 +27,7 @@ class Limiter {
|
||||
/**
|
||||
* Adds a job to the queue.
|
||||
*
|
||||
* @param {Function} job The job to run
|
||||
* @public
|
||||
*/
|
||||
add(job) {
|
||||
Generated
Vendored
+18
-16
@@ -29,24 +29,26 @@ class PerMessageDeflate {
|
||||
/**
|
||||
* Creates a PerMessageDeflate instance.
|
||||
*
|
||||
* @param {Object} options Configuration options
|
||||
* @param {Boolean} options.serverNoContextTakeover Request/accept disabling
|
||||
* of server context takeover
|
||||
* @param {Boolean} options.clientNoContextTakeover Advertise/acknowledge
|
||||
* disabling of client context takeover
|
||||
* @param {(Boolean|Number)} options.serverMaxWindowBits Request/confirm the
|
||||
* @param {Object} [options] Configuration options
|
||||
* @param {Boolean} [options.serverNoContextTakeover=false] Request/accept
|
||||
* disabling of server context takeover
|
||||
* @param {Boolean} [options.clientNoContextTakeover=false] Advertise/
|
||||
* acknowledge disabling of client context takeover
|
||||
* @param {(Boolean|Number)} [options.serverMaxWindowBits] Request/confirm the
|
||||
* use of a custom server window size
|
||||
* @param {(Boolean|Number)} options.clientMaxWindowBits Advertise support
|
||||
* @param {(Boolean|Number)} [options.clientMaxWindowBits] Advertise support
|
||||
* for, or request, a custom client window size
|
||||
* @param {Object} options.zlibDeflateOptions Options to pass to zlib on deflate
|
||||
* @param {Object} options.zlibInflateOptions Options to pass to zlib on inflate
|
||||
* @param {Number} options.threshold Size (in bytes) below which messages
|
||||
* should not be compressed
|
||||
* @param {Number} options.concurrencyLimit The number of concurrent calls to
|
||||
* zlib
|
||||
* @param {Boolean} isServer Create the instance in either server or client
|
||||
* mode
|
||||
* @param {Number} maxPayload The maximum allowed message length
|
||||
* @param {Object} [options.zlibDeflateOptions] Options to pass to zlib on
|
||||
* deflate
|
||||
* @param {Object} [options.zlibInflateOptions] Options to pass to zlib on
|
||||
* inflate
|
||||
* @param {Number} [options.threshold=1024] Size (in bytes) below which
|
||||
* messages should not be compressed
|
||||
* @param {Number} [options.concurrencyLimit=10] The number of concurrent
|
||||
* calls to zlib
|
||||
* @param {Boolean} [isServer=false] Create the instance in either server or
|
||||
* client mode
|
||||
* @param {Number} [maxPayload=0] The maximum allowed message length
|
||||
*/
|
||||
constructor(options, isServer, maxPayload) {
|
||||
this._maxPayload = maxPayload | 0;
|
||||
Generated
Vendored
+6
-5
@@ -28,11 +28,11 @@ class Receiver extends Writable {
|
||||
/**
|
||||
* Creates a Receiver instance.
|
||||
*
|
||||
* @param {String} binaryType The type for binary data
|
||||
* @param {Object} extensions An object containing the negotiated extensions
|
||||
* @param {Boolean} isServer Specifies whether to operate in client or server
|
||||
* mode
|
||||
* @param {Number} maxPayload The maximum allowed message length
|
||||
* @param {String} [binaryType=nodebuffer] The type for binary data
|
||||
* @param {Object} [extensions] An object containing the negotiated extensions
|
||||
* @param {Boolean} [isServer=false] Specifies whether to operate in client or
|
||||
* server mode
|
||||
* @param {Number} [maxPayload=0] The maximum allowed message length
|
||||
*/
|
||||
constructor(binaryType, extensions, isServer, maxPayload) {
|
||||
super();
|
||||
@@ -68,6 +68,7 @@ class Receiver extends Writable {
|
||||
* @param {Buffer} chunk The chunk of data to write
|
||||
* @param {String} encoding The character encoding of `chunk`
|
||||
* @param {Function} cb Callback
|
||||
* @private
|
||||
*/
|
||||
_write(chunk, encoding, cb) {
|
||||
if (this._opcode === 0x08 && this._state == GET_INFO) return cb();
|
||||
Generated
Vendored
+50
-35
@@ -17,7 +17,7 @@ class Sender {
|
||||
* Creates a Sender instance.
|
||||
*
|
||||
* @param {net.Socket} socket The connection socket
|
||||
* @param {Object} extensions An object containing the negotiated extensions
|
||||
* @param {Object} [extensions] An object containing the negotiated extensions
|
||||
*/
|
||||
constructor(socket, extensions) {
|
||||
this._extensions = extensions || {};
|
||||
@@ -37,10 +37,14 @@ class Sender {
|
||||
* @param {Buffer} data The data to frame
|
||||
* @param {Object} options Options object
|
||||
* @param {Number} options.opcode The opcode
|
||||
* @param {Boolean} options.readOnly Specifies whether `data` can be modified
|
||||
* @param {Boolean} options.fin Specifies whether or not to set the FIN bit
|
||||
* @param {Boolean} options.mask Specifies whether or not to mask `data`
|
||||
* @param {Boolean} options.rsv1 Specifies whether or not to set the RSV1 bit
|
||||
* @param {Boolean} [options.readOnly=false] Specifies whether `data` can be
|
||||
* modified
|
||||
* @param {Boolean} [options.fin=false] Specifies whether or not to set the
|
||||
* FIN bit
|
||||
* @param {Boolean} [options.mask=false] Specifies whether or not to mask
|
||||
* `data`
|
||||
* @param {Boolean} [options.rsv1=false] Specifies whether or not to set the
|
||||
* RSV1 bit
|
||||
* @return {Buffer[]} The framed data as a list of `Buffer` instances
|
||||
* @public
|
||||
*/
|
||||
@@ -93,10 +97,10 @@ class Sender {
|
||||
/**
|
||||
* Sends a close message to the other peer.
|
||||
*
|
||||
* @param {(Number|undefined)} code The status code component of the body
|
||||
* @param {String} data The message component of the body
|
||||
* @param {Boolean} mask Specifies whether or not to mask the message
|
||||
* @param {Function} cb Callback
|
||||
* @param {Number} [code] The status code component of the body
|
||||
* @param {String} [data] The message component of the body
|
||||
* @param {Boolean} [mask=false] Specifies whether or not to mask the message
|
||||
* @param {Function} [cb] Callback
|
||||
* @public
|
||||
*/
|
||||
close(code, data, mask, cb) {
|
||||
@@ -132,8 +136,8 @@ class Sender {
|
||||
* Frames and sends a close message.
|
||||
*
|
||||
* @param {Buffer} data The message to send
|
||||
* @param {Boolean} mask Specifies whether or not to mask `data`
|
||||
* @param {Function} cb Callback
|
||||
* @param {Boolean} [mask=false] Specifies whether or not to mask `data`
|
||||
* @param {Function} [cb] Callback
|
||||
* @private
|
||||
*/
|
||||
doClose(data, mask, cb) {
|
||||
@@ -153,8 +157,8 @@ class Sender {
|
||||
* Sends a ping message to the other peer.
|
||||
*
|
||||
* @param {*} data The message to send
|
||||
* @param {Boolean} mask Specifies whether or not to mask `data`
|
||||
* @param {Function} cb Callback
|
||||
* @param {Boolean} [mask=false] Specifies whether or not to mask `data`
|
||||
* @param {Function} [cb] Callback
|
||||
* @public
|
||||
*/
|
||||
ping(data, mask, cb) {
|
||||
@@ -174,10 +178,10 @@ class Sender {
|
||||
/**
|
||||
* Frames and sends a ping message.
|
||||
*
|
||||
* @param {*} data The message to send
|
||||
* @param {Boolean} mask Specifies whether or not to mask `data`
|
||||
* @param {Boolean} readOnly Specifies whether `data` can be modified
|
||||
* @param {Function} cb Callback
|
||||
* @param {Buffer} data The message to send
|
||||
* @param {Boolean} [mask=false] Specifies whether or not to mask `data`
|
||||
* @param {Boolean} [readOnly=false] Specifies whether `data` can be modified
|
||||
* @param {Function} [cb] Callback
|
||||
* @private
|
||||
*/
|
||||
doPing(data, mask, readOnly, cb) {
|
||||
@@ -197,8 +201,8 @@ class Sender {
|
||||
* Sends a pong message to the other peer.
|
||||
*
|
||||
* @param {*} data The message to send
|
||||
* @param {Boolean} mask Specifies whether or not to mask `data`
|
||||
* @param {Function} cb Callback
|
||||
* @param {Boolean} [mask=false] Specifies whether or not to mask `data`
|
||||
* @param {Function} [cb] Callback
|
||||
* @public
|
||||
*/
|
||||
pong(data, mask, cb) {
|
||||
@@ -218,10 +222,10 @@ class Sender {
|
||||
/**
|
||||
* Frames and sends a pong message.
|
||||
*
|
||||
* @param {*} data The message to send
|
||||
* @param {Boolean} mask Specifies whether or not to mask `data`
|
||||
* @param {Boolean} readOnly Specifies whether `data` can be modified
|
||||
* @param {Function} cb Callback
|
||||
* @param {Buffer} data The message to send
|
||||
* @param {Boolean} [mask=false] Specifies whether or not to mask `data`
|
||||
* @param {Boolean} [readOnly=false] Specifies whether `data` can be modified
|
||||
* @param {Function} [cb] Callback
|
||||
* @private
|
||||
*/
|
||||
doPong(data, mask, readOnly, cb) {
|
||||
@@ -242,11 +246,15 @@ class Sender {
|
||||
*
|
||||
* @param {*} data The message to send
|
||||
* @param {Object} options Options object
|
||||
* @param {Boolean} options.compress Specifies whether or not to compress `data`
|
||||
* @param {Boolean} options.binary Specifies whether `data` is binary or text
|
||||
* @param {Boolean} options.fin Specifies whether the fragment is the last one
|
||||
* @param {Boolean} options.mask Specifies whether or not to mask `data`
|
||||
* @param {Function} cb Callback
|
||||
* @param {Boolean} [options.compress=false] Specifies whether or not to
|
||||
* compress `data`
|
||||
* @param {Boolean} [options.binary=false] Specifies whether `data` is binary
|
||||
* or text
|
||||
* @param {Boolean} [options.fin=false] Specifies whether the fragment is the
|
||||
* last one
|
||||
* @param {Boolean} [options.mask=false] Specifies whether or not to mask
|
||||
* `data`
|
||||
* @param {Function} [cb] Callback
|
||||
* @public
|
||||
*/
|
||||
send(data, options, cb) {
|
||||
@@ -300,14 +308,19 @@ class Sender {
|
||||
* Dispatches a data message.
|
||||
*
|
||||
* @param {Buffer} data The message to send
|
||||
* @param {Boolean} compress Specifies whether or not to compress `data`
|
||||
* @param {Boolean} [compress=false] Specifies whether or not to compress
|
||||
* `data`
|
||||
* @param {Object} options Options object
|
||||
* @param {Number} options.opcode The opcode
|
||||
* @param {Boolean} options.readOnly Specifies whether `data` can be modified
|
||||
* @param {Boolean} options.fin Specifies whether or not to set the FIN bit
|
||||
* @param {Boolean} options.mask Specifies whether or not to mask `data`
|
||||
* @param {Boolean} options.rsv1 Specifies whether or not to set the RSV1 bit
|
||||
* @param {Function} cb Callback
|
||||
* @param {Boolean} [options.readOnly=false] Specifies whether `data` can be
|
||||
* modified
|
||||
* @param {Boolean} [options.fin=false] Specifies whether or not to set the
|
||||
* FIN bit
|
||||
* @param {Boolean} [options.mask=false] Specifies whether or not to mask
|
||||
* `data`
|
||||
* @param {Boolean} [options.rsv1=false] Specifies whether or not to set the
|
||||
* RSV1 bit
|
||||
* @param {Function} [cb] Callback
|
||||
* @private
|
||||
*/
|
||||
dispatch(data, compress, options, cb) {
|
||||
@@ -318,6 +331,7 @@ class Sender {
|
||||
|
||||
const perMessageDeflate = this._extensions[PerMessageDeflate.extensionName];
|
||||
|
||||
this._bufferedBytes += data.length;
|
||||
this._deflating = true;
|
||||
perMessageDeflate.compress(data, options.fin, (_, buf) => {
|
||||
if (this._socket.destroyed) {
|
||||
@@ -336,6 +350,7 @@ class Sender {
|
||||
return;
|
||||
}
|
||||
|
||||
this._bufferedBytes -= data.length;
|
||||
this._deflating = false;
|
||||
options.readOnly = false;
|
||||
this.sendFrame(Sender.frame(buf, options), cb);
|
||||
@@ -372,7 +387,7 @@ class Sender {
|
||||
* Sends a frame.
|
||||
*
|
||||
* @param {Buffer[]} list The frame to send
|
||||
* @param {Function} cb Callback
|
||||
* @param {Function} [cb] Callback
|
||||
* @private
|
||||
*/
|
||||
sendFrame(list, cb) {
|
||||
Generated
Vendored
+6
-5
@@ -26,6 +26,7 @@ function duplexOnEnd() {
|
||||
/**
|
||||
* The listener of the `'error'` event.
|
||||
*
|
||||
* @param {Error} err The error
|
||||
* @private
|
||||
*/
|
||||
function duplexOnError(err) {
|
||||
@@ -41,7 +42,7 @@ function duplexOnError(err) {
|
||||
* Wraps a `WebSocket` in a duplex stream.
|
||||
*
|
||||
* @param {WebSocket} ws The `WebSocket` to wrap
|
||||
* @param {Object} options The options for the `Duplex` constructor
|
||||
* @param {Object} [options] The options for the `Duplex` constructor
|
||||
* @return {stream.Duplex} The duplex stream
|
||||
* @public
|
||||
*/
|
||||
@@ -89,7 +90,7 @@ function createWebSocketStream(ws, options) {
|
||||
duplex.push(null);
|
||||
});
|
||||
|
||||
duplex._destroy = function(err, callback) {
|
||||
duplex._destroy = function (err, callback) {
|
||||
if (ws.readyState === ws.CLOSED) {
|
||||
callback(err);
|
||||
process.nextTick(emitClose, duplex);
|
||||
@@ -110,7 +111,7 @@ function createWebSocketStream(ws, options) {
|
||||
ws.terminate();
|
||||
};
|
||||
|
||||
duplex._final = function(callback) {
|
||||
duplex._final = function (callback) {
|
||||
if (ws.readyState === ws.CONNECTING) {
|
||||
ws.once('open', function open() {
|
||||
duplex._final(callback);
|
||||
@@ -138,14 +139,14 @@ function createWebSocketStream(ws, options) {
|
||||
}
|
||||
};
|
||||
|
||||
duplex._read = function() {
|
||||
duplex._read = function () {
|
||||
if (ws.readyState === ws.OPEN && !resumeOnReceiverDrain) {
|
||||
resumeOnReceiverDrain = true;
|
||||
if (!ws._receiver._writableState.needDrain) ws._socket.resume();
|
||||
}
|
||||
};
|
||||
|
||||
duplex._write = function(chunk, encoding, callback) {
|
||||
duplex._write = function (chunk, encoding, callback) {
|
||||
if (ws.readyState === ws.CONNECTING) {
|
||||
ws.once('open', function open() {
|
||||
duplex._write(chunk, encoding, callback);
|
||||
Generated
Vendored
File renamed without changes.
Generated
Vendored
+23
-21
@@ -21,21 +21,22 @@ class WebSocketServer extends EventEmitter {
|
||||
* Create a `WebSocketServer` instance.
|
||||
*
|
||||
* @param {Object} options Configuration options
|
||||
* @param {Number} options.backlog The maximum length of the queue of pending
|
||||
* connections
|
||||
* @param {Boolean} options.clientTracking Specifies whether or not to track
|
||||
* clients
|
||||
* @param {Function} options.handleProtocols A hook to handle protocols
|
||||
* @param {String} options.host The hostname where to bind the server
|
||||
* @param {Number} options.maxPayload The maximum allowed message size
|
||||
* @param {Boolean} options.noServer Enable no server mode
|
||||
* @param {String} options.path Accept only connections matching this path
|
||||
* @param {(Boolean|Object)} options.perMessageDeflate Enable/disable
|
||||
* @param {Number} [options.backlog=511] The maximum length of the queue of
|
||||
* pending connections
|
||||
* @param {Boolean} [options.clientTracking=true] Specifies whether or not to
|
||||
* track clients
|
||||
* @param {Function} [options.handleProtocols] A hook to handle protocols
|
||||
* @param {String} [options.host] The hostname where to bind the server
|
||||
* @param {Number} [options.maxPayload=104857600] The maximum allowed message
|
||||
* size
|
||||
* @param {Boolean} [options.noServer=false] Enable no server mode
|
||||
* @param {String} [options.path] Accept only connections matching this path
|
||||
* @param {(Boolean|Object)} [options.perMessageDeflate=false] Enable/disable
|
||||
* permessage-deflate
|
||||
* @param {Number} options.port The port where to bind the server
|
||||
* @param {http.Server} options.server A pre-created HTTP/S server to use
|
||||
* @param {Function} options.verifyClient A hook to reject connections
|
||||
* @param {Function} callback A listener for the `listening` event
|
||||
* @param {Number} [options.port] The port where to bind the server
|
||||
* @param {http.Server} [options.server] A pre-created HTTP/S server to use
|
||||
* @param {Function} [options.verifyClient] A hook to reject connections
|
||||
* @param {Function} [callback] A listener for the `listening` event
|
||||
*/
|
||||
constructor(options, callback) {
|
||||
super();
|
||||
@@ -82,13 +83,13 @@ class WebSocketServer extends EventEmitter {
|
||||
}
|
||||
|
||||
if (this._server) {
|
||||
const emitConnection = this.emit.bind(this, 'connection');
|
||||
|
||||
this._removeListeners = addListeners(this._server, {
|
||||
listening: this.emit.bind(this, 'listening'),
|
||||
error: this.emit.bind(this, 'error'),
|
||||
upgrade: (req, socket, head) => {
|
||||
this.handleUpgrade(req, socket, head, (ws) => {
|
||||
this.emit('connection', ws, req);
|
||||
});
|
||||
this.handleUpgrade(req, socket, head, emitConnection);
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -119,7 +120,7 @@ class WebSocketServer extends EventEmitter {
|
||||
/**
|
||||
* Close the server.
|
||||
*
|
||||
* @param {Function} cb Callback
|
||||
* @param {Function} [cb] Callback
|
||||
* @public
|
||||
*/
|
||||
close(cb) {
|
||||
@@ -298,7 +299,7 @@ class WebSocketServer extends EventEmitter {
|
||||
|
||||
if (protocol) {
|
||||
headers.push(`Sec-WebSocket-Protocol: ${protocol}`);
|
||||
ws.protocol = protocol;
|
||||
ws._protocol = protocol;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -326,7 +327,7 @@ class WebSocketServer extends EventEmitter {
|
||||
ws.on('close', () => this.clients.delete(ws));
|
||||
}
|
||||
|
||||
cb(ws);
|
||||
cb(ws, req);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -338,7 +339,8 @@ module.exports = WebSocketServer;
|
||||
*
|
||||
* @param {EventEmitter} server The event emitter
|
||||
* @param {Object.<String, Function>} map The listeners to add
|
||||
* @return {Function} A function that will remove the added listeners when called
|
||||
* @return {Function} A function that will remove the added listeners when
|
||||
* called
|
||||
* @private
|
||||
*/
|
||||
function addListeners(server, map) {
|
||||
Generated
Vendored
+91
-66
@@ -37,22 +37,21 @@ class WebSocket extends EventEmitter {
|
||||
* Create a new `WebSocket`.
|
||||
*
|
||||
* @param {(String|url.URL)} address The URL to which to connect
|
||||
* @param {(String|String[])} protocols The subprotocols
|
||||
* @param {Object} options Connection options
|
||||
* @param {(String|String[])} [protocols] The subprotocols
|
||||
* @param {Object} [options] Connection options
|
||||
*/
|
||||
constructor(address, protocols, options) {
|
||||
super();
|
||||
|
||||
this.readyState = WebSocket.CONNECTING;
|
||||
this.protocol = '';
|
||||
|
||||
this._binaryType = BINARY_TYPES[0];
|
||||
this._closeCode = 1006;
|
||||
this._closeFrameReceived = false;
|
||||
this._closeFrameSent = false;
|
||||
this._closeMessage = '';
|
||||
this._closeTimer = null;
|
||||
this._closeCode = 1006;
|
||||
this._extensions = {};
|
||||
this._protocol = '';
|
||||
this._readyState = WebSocket.CONNECTING;
|
||||
this._receiver = null;
|
||||
this._sender = null;
|
||||
this._socket = null;
|
||||
@@ -75,19 +74,6 @@ class WebSocket extends EventEmitter {
|
||||
}
|
||||
}
|
||||
|
||||
get CONNECTING() {
|
||||
return WebSocket.CONNECTING;
|
||||
}
|
||||
get CLOSING() {
|
||||
return WebSocket.CLOSING;
|
||||
}
|
||||
get CLOSED() {
|
||||
return WebSocket.CLOSED;
|
||||
}
|
||||
get OPEN() {
|
||||
return WebSocket.OPEN;
|
||||
}
|
||||
|
||||
/**
|
||||
* This deviates from the WHATWG interface since ws doesn't support the
|
||||
* required default "blob" type (instead we define a custom "nodebuffer"
|
||||
@@ -116,10 +102,7 @@ class WebSocket extends EventEmitter {
|
||||
get bufferedAmount() {
|
||||
if (!this._socket) return this._bufferedAmount;
|
||||
|
||||
//
|
||||
// `socket.bufferSize` is `undefined` if the socket is closed.
|
||||
//
|
||||
return (this._socket.bufferSize || 0) + this._sender._bufferedBytes;
|
||||
return this._socket._writableState.length + this._sender._bufferedBytes;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -129,17 +112,38 @@ class WebSocket extends EventEmitter {
|
||||
return Object.keys(this._extensions).join();
|
||||
}
|
||||
|
||||
/**
|
||||
* @type {String}
|
||||
*/
|
||||
get protocol() {
|
||||
return this._protocol;
|
||||
}
|
||||
|
||||
/**
|
||||
* @type {Number}
|
||||
*/
|
||||
get readyState() {
|
||||
return this._readyState;
|
||||
}
|
||||
|
||||
/**
|
||||
* @type {String}
|
||||
*/
|
||||
get url() {
|
||||
return this._url;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set up the socket and the internal resources.
|
||||
*
|
||||
* @param {net.Socket} socket The network socket between the server and client
|
||||
* @param {Buffer} head The first packet of the upgraded stream
|
||||
* @param {Number} maxPayload The maximum allowed message size
|
||||
* @param {Number} [maxPayload=0] The maximum allowed message size
|
||||
* @private
|
||||
*/
|
||||
setSocket(socket, head, maxPayload) {
|
||||
const receiver = new Receiver(
|
||||
this._binaryType,
|
||||
this.binaryType,
|
||||
this._extensions,
|
||||
this._isServer,
|
||||
maxPayload
|
||||
@@ -169,7 +173,7 @@ class WebSocket extends EventEmitter {
|
||||
socket.on('end', socketOnEnd);
|
||||
socket.on('error', socketOnError);
|
||||
|
||||
this.readyState = WebSocket.OPEN;
|
||||
this._readyState = WebSocket.OPEN;
|
||||
this.emit('open');
|
||||
}
|
||||
|
||||
@@ -180,7 +184,7 @@ class WebSocket extends EventEmitter {
|
||||
*/
|
||||
emitClose() {
|
||||
if (!this._socket) {
|
||||
this.readyState = WebSocket.CLOSED;
|
||||
this._readyState = WebSocket.CLOSED;
|
||||
this.emit('close', this._closeCode, this._closeMessage);
|
||||
return;
|
||||
}
|
||||
@@ -190,7 +194,7 @@ class WebSocket extends EventEmitter {
|
||||
}
|
||||
|
||||
this._receiver.removeAllListeners();
|
||||
this.readyState = WebSocket.CLOSED;
|
||||
this._readyState = WebSocket.CLOSED;
|
||||
this.emit('close', this._closeCode, this._closeMessage);
|
||||
}
|
||||
|
||||
@@ -209,8 +213,8 @@ class WebSocket extends EventEmitter {
|
||||
* - - - - -|fin|<---------------------+
|
||||
* +---+
|
||||
*
|
||||
* @param {Number} code Status code explaining why the connection is closing
|
||||
* @param {String} data A string explaining why the connection is closing
|
||||
* @param {Number} [code] Status code explaining why the connection is closing
|
||||
* @param {String} [data] A string explaining why the connection is closing
|
||||
* @public
|
||||
*/
|
||||
close(code, data) {
|
||||
@@ -225,7 +229,7 @@ class WebSocket extends EventEmitter {
|
||||
return;
|
||||
}
|
||||
|
||||
this.readyState = WebSocket.CLOSING;
|
||||
this._readyState = WebSocket.CLOSING;
|
||||
this._sender.close(code, data, !this._isServer, (err) => {
|
||||
//
|
||||
// This error is handled by the `'error'` listener on the socket. We only
|
||||
@@ -249,9 +253,9 @@ class WebSocket extends EventEmitter {
|
||||
/**
|
||||
* Send a ping.
|
||||
*
|
||||
* @param {*} data The data to send
|
||||
* @param {Boolean} mask Indicates whether or not to mask `data`
|
||||
* @param {Function} cb Callback which is executed when the ping is sent
|
||||
* @param {*} [data] The data to send
|
||||
* @param {Boolean} [mask] Indicates whether or not to mask `data`
|
||||
* @param {Function} [cb] Callback which is executed when the ping is sent
|
||||
* @public
|
||||
*/
|
||||
ping(data, mask, cb) {
|
||||
@@ -281,9 +285,9 @@ class WebSocket extends EventEmitter {
|
||||
/**
|
||||
* Send a pong.
|
||||
*
|
||||
* @param {*} data The data to send
|
||||
* @param {Boolean} mask Indicates whether or not to mask `data`
|
||||
* @param {Function} cb Callback which is executed when the pong is sent
|
||||
* @param {*} [data] The data to send
|
||||
* @param {Boolean} [mask] Indicates whether or not to mask `data`
|
||||
* @param {Function} [cb] Callback which is executed when the pong is sent
|
||||
* @public
|
||||
*/
|
||||
pong(data, mask, cb) {
|
||||
@@ -314,13 +318,15 @@ class WebSocket extends EventEmitter {
|
||||
* Send a data message.
|
||||
*
|
||||
* @param {*} data The message to send
|
||||
* @param {Object} options Options object
|
||||
* @param {Boolean} options.compress Specifies whether or not to compress
|
||||
* @param {Object} [options] Options object
|
||||
* @param {Boolean} [options.compress] Specifies whether or not to compress
|
||||
* `data`
|
||||
* @param {Boolean} options.binary Specifies whether `data` is binary or text
|
||||
* @param {Boolean} options.fin Specifies whether the fragment is the last one
|
||||
* @param {Boolean} options.mask Specifies whether or not to mask `data`
|
||||
* @param {Function} cb Callback which is executed when data is written out
|
||||
* @param {Boolean} [options.binary] Specifies whether `data` is binary or
|
||||
* text
|
||||
* @param {Boolean} [options.fin=true] Specifies whether the fragment is the
|
||||
* last one
|
||||
* @param {Boolean} [options.mask] Specifies whether or not to mask `data`
|
||||
* @param {Function} [cb] Callback which is executed when data is written out
|
||||
* @public
|
||||
*/
|
||||
send(data, options, cb) {
|
||||
@@ -368,14 +374,28 @@ class WebSocket extends EventEmitter {
|
||||
}
|
||||
|
||||
if (this._socket) {
|
||||
this.readyState = WebSocket.CLOSING;
|
||||
this._readyState = WebSocket.CLOSING;
|
||||
this._socket.destroy();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
readyStates.forEach((readyState, i) => {
|
||||
WebSocket[readyState] = i;
|
||||
const descriptor = { enumerable: true, value: i };
|
||||
|
||||
Object.defineProperty(WebSocket.prototype, readyState, descriptor);
|
||||
Object.defineProperty(WebSocket, readyState, descriptor);
|
||||
});
|
||||
|
||||
[
|
||||
'binaryType',
|
||||
'bufferedAmount',
|
||||
'extensions',
|
||||
'protocol',
|
||||
'readyState',
|
||||
'url'
|
||||
].forEach((property) => {
|
||||
Object.defineProperty(WebSocket.prototype, property, { enumerable: true });
|
||||
});
|
||||
|
||||
//
|
||||
@@ -384,6 +404,8 @@ readyStates.forEach((readyState, i) => {
|
||||
//
|
||||
['open', 'error', 'close', 'message'].forEach((method) => {
|
||||
Object.defineProperty(WebSocket.prototype, `on${method}`, {
|
||||
configurable: true,
|
||||
enumerable: true,
|
||||
/**
|
||||
* Return the listener of the event.
|
||||
*
|
||||
@@ -427,19 +449,22 @@ module.exports = WebSocket;
|
||||
*
|
||||
* @param {WebSocket} websocket The client to initialize
|
||||
* @param {(String|url.URL)} address The URL to which to connect
|
||||
* @param {String} protocols The subprotocols
|
||||
* @param {Object} options Connection options
|
||||
* @param {(Boolean|Object)} options.perMessageDeflate Enable/disable
|
||||
* @param {String} [protocols] The subprotocols
|
||||
* @param {Object} [options] Connection options
|
||||
* @param {(Boolean|Object)} [options.perMessageDeflate=true] Enable/disable
|
||||
* permessage-deflate
|
||||
* @param {Number} options.handshakeTimeout Timeout in milliseconds for the
|
||||
* @param {Number} [options.handshakeTimeout] Timeout in milliseconds for the
|
||||
* handshake request
|
||||
* @param {Number} options.protocolVersion Value of the `Sec-WebSocket-Version`
|
||||
* header
|
||||
* @param {String} options.origin Value of the `Origin` or
|
||||
* @param {Number} [options.protocolVersion=13] Value of the
|
||||
* `Sec-WebSocket-Version` header
|
||||
* @param {String} [options.origin] Value of the `Origin` or
|
||||
* `Sec-WebSocket-Origin` header
|
||||
* @param {Number} options.maxPayload The maximum allowed message size
|
||||
* @param {Boolean} options.followRedirects Whether or not to follow redirects
|
||||
* @param {Number} options.maxRedirects The maximum number of redirects allowed
|
||||
* @param {Number} [options.maxPayload=104857600] The maximum allowed message
|
||||
* size
|
||||
* @param {Boolean} [options.followRedirects=false] Whether or not to follow
|
||||
* redirects
|
||||
* @param {Number} [options.maxRedirects=10] The maximum number of redirects
|
||||
* allowed
|
||||
* @private
|
||||
*/
|
||||
function initAsClient(websocket, address, protocols, options) {
|
||||
@@ -472,10 +497,10 @@ function initAsClient(websocket, address, protocols, options) {
|
||||
|
||||
if (address instanceof URL) {
|
||||
parsedUrl = address;
|
||||
websocket.url = address.href;
|
||||
websocket._url = address.href;
|
||||
} else {
|
||||
parsedUrl = new URL(address);
|
||||
websocket.url = address;
|
||||
websocket._url = address;
|
||||
}
|
||||
|
||||
const isUnixSocket = parsedUrl.protocol === 'ws+unix:';
|
||||
@@ -550,7 +575,7 @@ function initAsClient(websocket, address, protocols, options) {
|
||||
if (websocket._req.aborted) return;
|
||||
|
||||
req = websocket._req = null;
|
||||
websocket.readyState = WebSocket.CLOSING;
|
||||
websocket._readyState = WebSocket.CLOSING;
|
||||
websocket.emit('error', err);
|
||||
websocket.emitClose();
|
||||
});
|
||||
@@ -621,7 +646,7 @@ function initAsClient(websocket, address, protocols, options) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (serverProt) websocket.protocol = serverProt;
|
||||
if (serverProt) websocket._protocol = serverProt;
|
||||
|
||||
if (perMessageDeflate) {
|
||||
try {
|
||||
@@ -686,7 +711,7 @@ function tlsConnect(options) {
|
||||
* @private
|
||||
*/
|
||||
function abortHandshake(websocket, stream, message) {
|
||||
websocket.readyState = WebSocket.CLOSING;
|
||||
websocket._readyState = WebSocket.CLOSING;
|
||||
|
||||
const err = new Error(message);
|
||||
Error.captureStackTrace(err, abortHandshake);
|
||||
@@ -707,8 +732,8 @@ function abortHandshake(websocket, stream, message) {
|
||||
* when the `readyState` attribute is `CLOSING` or `CLOSED`.
|
||||
*
|
||||
* @param {WebSocket} websocket The WebSocket instance
|
||||
* @param {*} data The data to send
|
||||
* @param {Function} cb Callback
|
||||
* @param {*} [data] The data to send
|
||||
* @param {Function} [cb] Callback
|
||||
* @private
|
||||
*/
|
||||
function sendAfterClose(websocket, data, cb) {
|
||||
@@ -775,7 +800,7 @@ function receiverOnError(err) {
|
||||
|
||||
websocket._socket.removeListener('data', socketOnData);
|
||||
|
||||
websocket.readyState = WebSocket.CLOSING;
|
||||
websocket._readyState = WebSocket.CLOSING;
|
||||
websocket._closeCode = err[kStatusCode];
|
||||
websocket.emit('error', err);
|
||||
websocket._socket.destroy();
|
||||
@@ -834,7 +859,7 @@ function socketOnClose() {
|
||||
this.removeListener('close', socketOnClose);
|
||||
this.removeListener('end', socketOnEnd);
|
||||
|
||||
websocket.readyState = WebSocket.CLOSING;
|
||||
websocket._readyState = WebSocket.CLOSING;
|
||||
|
||||
//
|
||||
// The close frame might not have been received or the `'end'` event emitted,
|
||||
@@ -885,7 +910,7 @@ function socketOnData(chunk) {
|
||||
function socketOnEnd() {
|
||||
const websocket = this[kWebSocket];
|
||||
|
||||
websocket.readyState = WebSocket.CLOSING;
|
||||
websocket._readyState = WebSocket.CLOSING;
|
||||
websocket._receiver.end();
|
||||
this.end();
|
||||
}
|
||||
@@ -902,7 +927,7 @@ function socketOnError() {
|
||||
this.on('error', NOOP);
|
||||
|
||||
if (websocket) {
|
||||
websocket.readyState = WebSocket.CLOSING;
|
||||
websocket._readyState = WebSocket.CLOSING;
|
||||
this.destroy();
|
||||
}
|
||||
}
|
||||
Generated
Vendored
+17
-22
@@ -1,27 +1,28 @@
|
||||
{
|
||||
"_from": "ws@^7.1.2",
|
||||
"_id": "ws@7.3.0",
|
||||
"_from": "ws",
|
||||
"_id": "ws@7.4.0",
|
||||
"_inBundle": false,
|
||||
"_integrity": "sha512-iFtXzngZVXPGgpTlP1rBqsUK82p9tKqsWRPg5L56egiljujJT3vGAYnHANvFxBieXrTFavhzhxW52jnaWV+w2w==",
|
||||
"_integrity": "sha512-kyFwXuV/5ymf+IXhS6f0+eAFvydbaBW3zjpT6hUdAh/hbVjTIB5EHBGi0bPoCLSK2wcuz3BrEkB9LrYv1Nm4NQ==",
|
||||
"_location": "/ws",
|
||||
"_phantomChildren": {},
|
||||
"_requested": {
|
||||
"type": "range",
|
||||
"type": "tag",
|
||||
"registry": true,
|
||||
"raw": "ws@^7.1.2",
|
||||
"raw": "ws",
|
||||
"name": "ws",
|
||||
"escapedName": "ws",
|
||||
"rawSpec": "^7.1.2",
|
||||
"rawSpec": "",
|
||||
"saveSpec": null,
|
||||
"fetchSpec": "^7.1.2"
|
||||
"fetchSpec": "latest"
|
||||
},
|
||||
"_requiredBy": [
|
||||
"#USER",
|
||||
"/"
|
||||
],
|
||||
"_resolved": "https://registry.npmjs.org/ws/-/ws-7.3.0.tgz",
|
||||
"_shasum": "4b2f7f219b3d3737bc1a2fbf145d825b94d38ffd",
|
||||
"_spec": "ws@^7.1.2",
|
||||
"_where": "C:\\Users\\Luis\\source\\repos\\AAMasters\\TaskServer",
|
||||
"_resolved": "https://registry.npmjs.org/ws/-/ws-7.4.0.tgz",
|
||||
"_shasum": "a5dd76a24197940d4a8bb9e0e152bb4503764da7",
|
||||
"_spec": "ws",
|
||||
"_where": "c:\\Superalgos\\Projects\\Superalgos\\TS\\Task-Modules",
|
||||
"author": {
|
||||
"name": "Einar Otto Stangvik",
|
||||
"email": "einaros@gmail.com",
|
||||
@@ -38,12 +39,12 @@
|
||||
"benchmark": "^2.1.4",
|
||||
"bufferutil": "^4.0.1",
|
||||
"coveralls": "^3.0.3",
|
||||
"eslint": "^6.0.0",
|
||||
"eslint": "^7.2.0",
|
||||
"eslint-config-prettier": "^6.0.0",
|
||||
"eslint-plugin-prettier": "^3.0.1",
|
||||
"mocha": "^7.0.0",
|
||||
"nyc": "^15.0.0",
|
||||
"prettier": "^1.17.0",
|
||||
"prettier": "^2.0.5",
|
||||
"utf-8-validate": "^5.0.2"
|
||||
},
|
||||
"engines": {
|
||||
@@ -54,12 +55,6 @@
|
||||
"index.js",
|
||||
"lib/*.js"
|
||||
],
|
||||
"greenkeeper": {
|
||||
"commitMessages": {
|
||||
"dependencyUpdate": "[pkg] Update ${dependency} to version ${version}",
|
||||
"devDependencyUpdate": "[pkg] Update ${dependency} to version ${version}"
|
||||
}
|
||||
},
|
||||
"homepage": "https://github.com/websockets/ws",
|
||||
"keywords": [
|
||||
"HyBi",
|
||||
@@ -89,9 +84,9 @@
|
||||
"url": "git+https://github.com/websockets/ws.git"
|
||||
},
|
||||
"scripts": {
|
||||
"integration": "npm run lint && mocha --throw-deprecation test/*.integration.js",
|
||||
"integration": "mocha --throw-deprecation test/*.integration.js",
|
||||
"lint": "eslint --ignore-path .gitignore . && prettier --check --ignore-path .gitignore \"**/*.{json,md,yaml,yml}\"",
|
||||
"test": "npm run lint && nyc --reporter=html --reporter=text mocha --throw-deprecation test/*.test.js"
|
||||
"test": "nyc --reporter=html --reporter=text mocha --throw-deprecation test/*.test.js"
|
||||
},
|
||||
"version": "7.3.0"
|
||||
"version": "7.4.0"
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"requires": true,
|
||||
"lockfileVersion": 1,
|
||||
"dependencies": {
|
||||
"ws": {
|
||||
"version": "7.4.0",
|
||||
"resolved": "https://registry.npmjs.org/ws/-/ws-7.4.0.tgz",
|
||||
"integrity": "sha512-kyFwXuV/5ymf+IXhS6f0+eAFvydbaBW3zjpT6hUdAh/hbVjTIB5EHBGi0bPoCLSK2wcuz3BrEkB9LrYv1Nm4NQ=="
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -22,6 +22,7 @@ exports.newMultiProject = function () {
|
||||
projectInstance.utilities = {}
|
||||
projectInstance.globals = {}
|
||||
projectInstance.functionLibraries = {}
|
||||
projectInstance.taskModules = {}
|
||||
projectInstance.processModules = {}
|
||||
|
||||
/* Set up Utilities of this Project */
|
||||
@@ -72,6 +73,17 @@ exports.newMultiProject = function () {
|
||||
}
|
||||
}
|
||||
|
||||
/* Set up Process Modules for this Project */
|
||||
if (projectDefinition.TS.taskModules !== undefined) {
|
||||
for (let j = 0; j < projectDefinition.TS.taskModules.length; j++) {
|
||||
let taskModuleDefinition = projectDefinition.TS.taskModules[j]
|
||||
let path = global.env.PATH_TO_PROJECTS_REQUIRED + '/' + projectDefinition.name + '/' + 'TS' + '/' + 'Task-Modules' + '/' + taskModuleDefinition.fileName
|
||||
|
||||
let requiredObject = require(path)
|
||||
projectInstance.taskModules[taskModuleDefinition.propertyName] = requiredObject
|
||||
}
|
||||
}
|
||||
|
||||
/* Set up Process Modules for this Project */
|
||||
if (projectDefinition.TS.processModules !== undefined) {
|
||||
for (let j = 0; j < projectDefinition.TS.processModules.length; j++) {
|
||||
|
||||
+2
-2
@@ -9,8 +9,8 @@ let NODE_JS_PROCESS_MODULE = NODE_JS_PROCESS.newNodeJsProcess()
|
||||
NODE_JS_PROCESS_MODULE.initialize()
|
||||
|
||||
/* Setting up the global Event Handler */
|
||||
let EVENT_SERVER_CLIENT = require('./EventServerClient.js');
|
||||
global.EVENT_SERVER_CLIENT_MODULE = EVENT_SERVER_CLIENT.newEventsServerClient()
|
||||
let EVENT_SERVER_CLIENT = require('../Projects/Superalgos/TS/Task-Modules/EventServerClient.js');
|
||||
global.EVENT_SERVER_CLIENT_MODULE = EVENT_SERVER_CLIENT.newSuperalgosTaskModulesEventServerClient()
|
||||
global.EVENT_SERVER_CLIENT_MODULE.initialize(preLoader)
|
||||
|
||||
function preLoader() {
|
||||
|
||||
Generated
-5
@@ -59,11 +59,6 @@
|
||||
"version": "3.6.1",
|
||||
"resolved": "https://registry.npmjs.org/telegram-typings/-/telegram-typings-3.6.1.tgz",
|
||||
"integrity": "sha512-njVv1EAhIZnmQVLocZEADYUyqA1WIXuVcDYlsp+mXua/XB0pxx+PKtMSPeZ/EE4wPWTw9h/hA9ASTT6yQelkiw=="
|
||||
},
|
||||
"ws": {
|
||||
"version": "7.3.0",
|
||||
"resolved": "https://registry.npmjs.org/ws/-/ws-7.3.0.tgz",
|
||||
"integrity": "sha512-iFtXzngZVXPGgpTlP1rBqsUK82p9tKqsWRPg5L56egiljujJT3vGAYnHANvFxBieXrTFavhzhxW52jnaWV+w2w=="
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -14,7 +14,6 @@
|
||||
},
|
||||
"dependencies": {
|
||||
"ccxt": "^1.25.80",
|
||||
"telegraf": "^3.37.0",
|
||||
"ws": "^7.1.2"
|
||||
"telegraf": "^3.37.0"
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user