Skip to content

Commit

Permalink
[lint] Use prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
lpinca committed Nov 17, 2018
1 parent 029de0c commit b9fad73
Show file tree
Hide file tree
Showing 35 changed files with 1,560 additions and 1,265 deletions.
2 changes: 0 additions & 2 deletions .eslintignore

This file was deleted.

18 changes: 13 additions & 5 deletions .eslintrc.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
extends: standard
env:
browser: true
es6: true
mocha: true
node: true
extends:
- eslint:recommended
- prettier
parserOptions:
ecmaVersion: 9
plugins:
- prettier
rules:
no-extra-semi: error
semi:
- error
- always
no-console: off
prefer-const: error
prettier/prettier: error
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,3 @@ node_modules/
.nyc_output/
coverage/
.vscode/
npm-debug.log
4 changes: 4 additions & 0 deletions .prettierrc.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
arrowParens: always
endOfLine: lf
proseWrap: always
singleQuote: true
11 changes: 4 additions & 7 deletions ISSUE_TEMPLATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,20 @@ General support questions should be raised on a channel like Stack Overflow.
Please fill in as much of the template below as you're able.
-->

- [ ] I've searched for any related issues and avoided creating a duplicate issue.
- [ ] I've searched for any related issues and avoided creating a duplicate
issue.

#### Description

<!-- e.g. Description of the bug or feature -->

#### Reproducible in:

version:
Node.js version(s):
OS version(s):
version: Node.js version(s): OS version(s):

#### Steps to reproduce:

1.
2.
3.
1. 2. 3.

### Expected result:

Expand Down
116 changes: 61 additions & 55 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,48 +5,49 @@
[![Windows Build](https://ci.appveyor.com/api/projects/status/github/websockets/ws?branch=master&svg=true)](https://ci.appveyor.com/project/lpinca/ws)
[![Coverage Status](https://img.shields.io/coveralls/websockets/ws/master.svg)](https://coveralls.io/r/websockets/ws?branch=master)

ws is a simple to use, blazing fast, and thoroughly tested WebSocket client
and server implementation.
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
[`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)
+ [Server broadcast](#server-broadcast)
+ [echo.websocket.org demo](#echowebsocketorg-demo)
+ [Other examples](#other-examples)
* [Error handling best practices](#error-handling-best-practices)
* [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](#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)
- [Server broadcast](#server-broadcast)
- [echo.websocket.org demo](#echowebsocketorg-demo)
- [Other examples](#other-examples)
- [Error handling best practices](#error-handling-best-practices)
- [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`)
- **HyBi drafts 07-12** (Use the option `protocolVersion: 8`)
- **HyBi drafts 13-17** (Current default, alternatively option
`protocolVersion: 13`)

## Installing

Expand All @@ -64,30 +65,29 @@ 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.
- `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 docs for the ws classes.

## 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.
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
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.
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
Expand All @@ -101,22 +101,23 @@ const WebSocket = require('ws');
const wss = new WebSocket.Server({
port: 8080,
perMessageDeflate: {
zlibDeflateOptions: { // See zlib defaults.
zlibDeflateOptions: {
// See zlib defaults.
chunkSize: 1024,
memLevel: 7,
level: 3,
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.
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.
concurrencyLimit: 10, // Limits zlib concurrency for perf.
threshold: 1024 // Size (in bytes) below which messages
// should not be compressed.
}
});
```
Expand Down Expand Up @@ -325,8 +326,11 @@ ws.send('something', function ack(error) {
// Immediate errors can also be handled with `try...catch`, but **note** that
// since sends are inherently asynchronous, socket write failures will *not* be
// captured when this technique is used.
try { ws.send('something'); }
catch (e) { /* handle error */ }
try {
ws.send('something');
} catch (e) {
/* handle error */
}
```

## FAQ
Expand Down Expand Up @@ -356,8 +360,8 @@ wss.on('connection', function connection(ws, req) {

### 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
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
Expand Down Expand Up @@ -389,8 +393,8 @@ const interval = setInterval(function ping() {
}, 30000);
```

Pong messages are automatically sent in response to ping messages as required
by the spec.
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
Expand Down Expand Up @@ -439,5 +443,7 @@ We're using the GitHub [releases][changelog] for changelog entries.
[permessage-deflate]: https://tools.ietf.org/html/rfc7692
[changelog]: https://github.com/websockets/ws/releases
[node-zlib-bug]: https://github.com/nodejs/node/issues/8871
[node-zlib-deflaterawdocs]: https://nodejs.org/api/zlib.html#zlib_zlib_createdeflateraw_options
[ws-server-options]: https://github.com/websockets/ws/blob/master/doc/ws.md#new-websocketserveroptions-callback
[node-zlib-deflaterawdocs]:
https://nodejs.org/api/zlib.html#zlib_zlib_createdeflateraw_options
[ws-server-options]:
https://github.com/websockets/ws/blob/master/doc/ws.md#new-websocketserveroptions-callback
18 changes: 10 additions & 8 deletions SECURITY.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,20 @@ us the benefit of the doubt as it's possible that we haven't seen it yet. In
this case please send us a message **without details** using one of the
following methods:

- Contact the lead developers of this project on their personal e-mails. You
can find the e-mails in the git logs, for example using the following command:
- Contact the lead developers of this project on their personal e-mails. You can
find the e-mails in the git logs, for example using the following command:
`git --no-pager show -s --format='%an <%ae>' <gitsha>` where `<gitsha>` is the
SHA1 of their latest commit in the project.
- Create a GitHub issue stating contact details and the severity of the issue.

Once we have acknowledged receipt of your report and confirmed the bug
ourselves we will work with you to fix the vulnerability and publicly acknowledge
your responsible disclosure, if you wish. In addition to that we will report
all vulnerabilities to the [Node Security Project](https://nodesecurity.io/).
Once we have acknowledged receipt of your report and confirmed the bug ourselves
we will work with you to fix the vulnerability and publicly acknowledge your
responsible disclosure, if you wish. In addition to that we will report all
vulnerabilities to the [Node Security Project](https://nodesecurity.io/).

## History

- 04 Jan 2016: [Buffer vulnerability](https://github.com/websockets/ws/releases/tag/1.0.1)
- 08 Nov 2017: [DoS vulnerability](https://github.com/websockets/ws/releases/tag/3.3.1)
- 04 Jan 2016:
[Buffer vulnerability](https://github.com/websockets/ws/releases/tag/1.0.1)
- 08 Nov 2017:
[DoS vulnerability](https://github.com/websockets/ws/releases/tag/3.3.1)
9 changes: 4 additions & 5 deletions bench/parser.benchmark.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const options = {
readOnly: false
};

function createBinaryFrame (length) {
function createBinaryFrame(length) {
const list = Sender.frame(
crypto.randomBytes(length),
Object.assign({ opcode: 0x02 }, options)
Expand All @@ -24,10 +24,9 @@ function createBinaryFrame (length) {
return Buffer.concat(list);
}

const pingFrame1 = Buffer.concat(Sender.frame(
crypto.randomBytes(5),
Object.assign({ opcode: 0x09 }, options)
));
const pingFrame1 = Buffer.concat(
Sender.frame(crypto.randomBytes(5), Object.assign({ opcode: 0x09 }, options))
);

const textFrame = Buffer.from('819461616161' + '61'.repeat(20), 'hex');
const pingFrame2 = Buffer.from('8900', 'hex');
Expand Down
14 changes: 9 additions & 5 deletions bench/speed.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@ if (cluster.isMaster) {
return roundPrec(bytes, 2) + ' B';
};

const largest = configs.reduce((prev, curr) => curr[2] > prev ? curr[2] : prev, 0);
const largest = configs.reduce(
(prev, curr) => (curr[2] > prev ? curr[2] : prev),
0
);
console.log('Generating %s of test data...', humanSize(largest));
const randomBytes = Buffer.allocUnsafe(largest);

Expand Down Expand Up @@ -81,26 +84,27 @@ if (cluster.isMaster) {
ws.send(data, { binary: useBinary });
});
ws.on('message', () => {
if (++roundtrip !== roundtrips) return ws.send(data, { binary: useBinary });
if (++roundtrip !== roundtrips)
return ws.send(data, { binary: useBinary });

var elapsed = process.hrtime(time);
elapsed = (elapsed[0] * 1e9) + elapsed[1];
elapsed = elapsed[0] * 1e9 + elapsed[1];

console.log(
'%d roundtrips of %s %s data:\t%ss\t%s',
roundtrips,
humanSize(size),
useBinary ? 'binary' : 'text',
roundPrec(elapsed / 1e9, 1),
humanSize(size * 2 * roundtrips / elapsed * 1e9) + '/s'
humanSize(((size * 2 * roundtrips) / elapsed) * 1e9) + '/s'
);

ws.close();
cb();
});
};

(function run () {
(function run() {
if (configs.length === 0) return cluster.worker.disconnect();
var config = configs.shift();
config.push(run);
Expand Down
2 changes: 1 addition & 1 deletion browser.js
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
Loading

0 comments on commit b9fad73

Please sign in to comment.