From b7b6494936e3b19a2ae876f06b5245d53bbca3d0 Mon Sep 17 00:00:00 2001 From: Brett Zamir Date: Mon, 6 Jul 2020 21:46:29 +0800 Subject: [PATCH 1/2] - Linting: Switch to add a recommended extension (json) - Linting: Lint `/examples` files also (using `overrides` to allow `console` therein) - Refactoring: Switch to ES6 in examples and docs - npm: Update airbnb to version advertised as compatible with exiting eslint version --- .eslintrc | 7 ------- .eslintrc.json | 15 +++++++++++++++ README.md | 26 +++++++++++++------------- examples/broadcast.js | 20 ++++++++++---------- examples/https.js | 26 +++++++++++++------------- examples/params.js | 18 +++++++++--------- examples/simple.js | 18 +++++++++--------- package.json | 4 ++-- 8 files changed, 71 insertions(+), 63 deletions(-) delete mode 100644 .eslintrc create mode 100644 .eslintrc.json diff --git a/.eslintrc b/.eslintrc deleted file mode 100644 index f644a4b..0000000 --- a/.eslintrc +++ /dev/null @@ -1,7 +0,0 @@ -{ - "extends": "airbnb/base", - "rules": { - "comma-dangle": 0, - "no-param-reassign": 0 - } -} diff --git a/.eslintrc.json b/.eslintrc.json new file mode 100644 index 0000000..123b099 --- /dev/null +++ b/.eslintrc.json @@ -0,0 +1,15 @@ +{ + "extends": "airbnb/base", + "overrides": [ + { + "files": "examples/**", + "rules": { + "no-console": 0 + } + } + ], + "rules": { + "comma-dangle": 0, + "no-param-reassign": 0 + } +} diff --git a/README.md b/README.md index 309651e..f654de9 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ __Full documentation can be found in the API section below. This section only sh Add this line to your Express application: ```javascript -var expressWs = require('express-ws')(app); +const expressWs = require('express-ws')(app); ``` __Important: Make sure to set up the `express-ws` module like above *before* loading or defining your routers!__ Otherwise, `express-ws` won't get a chance to set up support for Express routers, and you might run into an error along the lines of `router.ws is not a function`. @@ -21,8 +21,8 @@ __Important: Make sure to set up the `express-ws` module like above *before* loa After setting up `express-ws`, you will be able to add WebSocket routes (almost) the same way you add other routes. The following snippet sets up a simple echo server at `/echo`. The `ws` parameter is an instance of the WebSocket class described [here](https://github.com/websockets/ws/blob/master/doc/ws.md#class-websocket). ```javascript -app.ws('/echo', function(ws, req) { - ws.on('message', function(msg) { +app.ws('/echo', (ws, req) => { + ws.on('message', (msg) => { ws.send(msg); }); }); @@ -31,10 +31,10 @@ app.ws('/echo', function(ws, req) { It works with routers, too, this time at `/ws-stuff/echo`: ```javascript -var router = express.Router(); +const router = express.Router(); -router.ws('/echo', function(ws, req) { - ws.on('message', function(msg) { +router.ws('/echo', (ws, req) => { + ws.on('message', (msg) => { ws.send(msg); }); }); @@ -45,23 +45,23 @@ app.use("/ws-stuff", router); ## Full example ```javascript -var express = require('express'); -var app = express(); -var expressWs = require('express-ws')(app); +const express = require('express'); +const app = express(); +const expressWs = require('express-ws')(app); -app.use(function (req, res, next) { +app.use((req, res, next) => { console.log('middleware'); req.testing = 'testing'; return next(); }); -app.get('/', function(req, res, next){ +app.get('/', (req, res, next) => { console.log('get route', req.testing); res.end(); }); -app.ws('/', function(ws, req) { - ws.on('message', function(msg) { +app.ws('/', (ws, req) => { + ws.on('message', (msg) => { console.log(msg); }); console.log('socket', req.testing); diff --git a/examples/broadcast.js b/examples/broadcast.js index 3d8c606..eb5bd78 100755 --- a/examples/broadcast.js +++ b/examples/broadcast.js @@ -1,20 +1,20 @@ -var express = require('express'); -var expressWs = require('..') +const express = require('express'); +let expressWs = require('..'); -var expressWs = expressWs(express()); -var app = expressWs.app; +expressWs = expressWs(express()); +const app = expressWs.app; -app.ws('/a', function(ws, req) { +app.ws('/a', (/* ws, req */) => { }); -var aWss = expressWs.getWss('/a'); +const aWss = expressWs.getWss('/a'); -app.ws('/b', function(ws, req) { +app.ws('/b', (/* ws, req */) => { }); -setInterval(function () { - aWss.clients.forEach(function (client) { +setInterval(() => { + aWss.clients.forEach((client) => { client.send('hello'); }); }, 5000); -app.listen(3000) +app.listen(3000); diff --git a/examples/https.js b/examples/https.js index f881fda..0db5d73 100644 --- a/examples/https.js +++ b/examples/https.js @@ -1,33 +1,33 @@ -var https = require('https'); -var fs = require('fs'); +const https = require('https'); +const fs = require('fs'); -var express = require('express'); -var expressWs = require('..'); +const express = require('express'); +const expressWs = require('..'); -var options = { +const options = { key: fs.readFileSync('key.pem'), cert: fs.readFileSync('cert.pem') }; -var app = express(); -var server = https.createServer(options, app); -var expressWs = expressWs(app, server); +const app = express(); +const server = https.createServer(options, app); +expressWs(app, server); -app.use(function (req, res, next) { +app.use((req, res, next) => { console.log('middleware'); req.testing = 'testing'; return next(); }); -app.get('/', function(req, res, next){ +app.get('/', (req, res /* , next */) => { console.log('get route', req.testing); res.end(); }); -app.ws('/', function(ws, req) { - ws.on('message', function(msg) { +app.ws('/', (ws, req) => { + ws.on('message', (msg) => { console.log(msg); }); console.log('socket', req.testing); }); -server.listen(3000) +server.listen(3000); diff --git a/examples/params.js b/examples/params.js index b982a59..d9829dc 100644 --- a/examples/params.js +++ b/examples/params.js @@ -1,26 +1,26 @@ -var express = require('express'); -var expressWs = require('..'); +const express = require('express'); +let expressWs = require('..'); -var expressWs = expressWs(express()); -var app = expressWs.app; +expressWs = expressWs(express()); +const app = expressWs.app; -app.param('world', function (req, res, next, world) { +app.param('world', (req, res, next, world) => { req.world = world || 'world'; return next(); }); -app.get('/hello/:world', function(req, res, next){ +app.get('/hello/:world', (req, res, next) => { console.log('hello', req.world); res.end(); next(); }); -app.ws('/hello/:world', function(ws, req, next) { - ws.on('message', function(msg) { +app.ws('/hello/:world', (ws, req, next) => { + ws.on('message', (msg) => { console.log(msg); }); console.log('socket hello', req.world); next(); }); -app.listen(3000) +app.listen(3000); diff --git a/examples/simple.js b/examples/simple.js index d4b1abd..c3324c0 100755 --- a/examples/simple.js +++ b/examples/simple.js @@ -1,25 +1,25 @@ -var express = require('express'); -var expressWs = require('..'); +const express = require('express'); +let expressWs = require('..'); -var expressWs = expressWs(express()); -var app = expressWs.app; +expressWs = expressWs(express()); +const app = expressWs.app; -app.use(function (req, res, next) { +app.use((req, res, next) => { console.log('middleware'); req.testing = 'testing'; return next(); }); -app.get('/', function(req, res, next){ +app.get('/', (req, res /* , next */) => { console.log('get route', req.testing); res.end(); }); -app.ws('/', function(ws, req) { - ws.on('message', function(msg) { +app.ws('/', (ws, req) => { + ws.on('message', (msg) => { console.log(msg); }); console.log('socket', req.testing); }); -app.listen(3000) +app.listen(3000); diff --git a/package.json b/package.json index 791dd7c..10f95f5 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "main": "index", "module": "src/index", "scripts": { - "lint": "eslint src/" + "lint": "eslint ." }, "author": "Henning Morud ", "contributors": [ @@ -46,7 +46,7 @@ "homepage": "https://github.com/HenningM/express-ws", "devDependencies": { "eslint": "^4.19.0", - "eslint-config-airbnb": "^14.1.0", + "eslint-config-airbnb": "^15.1.0", "eslint-plugin-import": "^2.12.0", "express": "^5.0.0-alpha.6" } From 025b70f1bab8466f8e2461854792e5cb6fb64dfc Mon Sep 17 00:00:00 2001 From: Brett Zamir Date: Tue, 7 Jul 2020 15:59:07 +0800 Subject: [PATCH 2/2] - Docs: README clarifications --- README.md | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index f654de9..55ec9fc 100644 --- a/README.md +++ b/README.md @@ -39,7 +39,7 @@ router.ws('/echo', (ws, req) => { }); }); -app.use("/ws-stuff", router); +app.use('/ws-stuff', router); ``` ## Full example @@ -80,26 +80,26 @@ Sets up `express-ws` on the specified `app`. This will modify the global Router * __server__: *Optional.* When using a custom `http.Server`, you should pass it in here, so that `express-ws` can use it to set up the WebSocket upgrade handlers. If you don't specify a `server`, you will only be able to use it with the server that is created automatically when you call `app.listen`. * __options__: *Optional.* An object containing further options. * __leaveRouterUntouched:__ Set this to `true` to keep `express-ws` from modifying the Router prototype. You will have to manually `applyTo` every Router that you wish to make `.ws` available on, when this is enabled. - * __wsOptions:__ Options object passed to WebSocketServer constructor. Necessary for any ws specific features. + * __wsOptions:__ Options object passed to `ws.Server` constructor. Necessary for any ws specific features. -This function will return a new `express-ws` API object, which will be referred to as `wsInstance` in the rest of the documentation. +This function will return a new `express-ws` API object, which will be referred to as `expressWsInstance` in the rest of the documentation. -### wsInstance.app +### expressWsInstance.app This property contains the `app` that `express-ws` was set up on. -### wsInstance.getWss() +### expressWsInstance.getWss() -Returns the underlying WebSocket server/handler. You can use `wsInstance.getWss().clients` to obtain a list of all the connected WebSocket clients for this server. +Returns the underlying WebSocket server/handler. You can use `expressWsInstance.getWss().clients` to obtain a list of all the connected WebSocket clients for this server. Note that this list will include *all* clients, not just those for a specific route - this means that it's often *not* a good idea to use this for broadcasts, for example. -### wsInstance.applyTo(router) +### expressWsInstance.applyTo(router) Sets up `express-ws` on the given `router` (or other Router-like object). You will only need this in two scenarios: 1. You have enabled `options.leaveRouterUntouched`, or -2. You are using a custom router that is not based on the express.Router prototype. +2. You are using a custom router that is not based on the `express.Router` prototype. In most cases, you won't need this at all.