Skip to content

Commit

Permalink
- Fix: Remove unused parameter passed to getWss in broadcast.js e…
Browse files Browse the repository at this point in the history
…xample (also renaming variable to reflect lack of specificity to route of web socket server)

- Fix: Allow middleware error handler (arity 4) to be triggered
- Refactoring: Remove `upgradeReq` check (dropped back in ws 3)
- Testing: Add mocha/chai/nyc

Re: middleware error handler, `Layer.prototype.handle_error` of `router` (used by `express.Router`) only handles arity 4
  • Loading branch information
brettz9 committed Jul 7, 2020
1 parent 025b70f commit a54f4f3
Show file tree
Hide file tree
Showing 7 changed files with 582 additions and 15 deletions.
14 changes: 13 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,19 @@
"extends": "airbnb/base",
"overrides": [
{
"files": "examples/**",
"files": ["examples/**"],
"rules": {
"no-console": 0
}
},
{
"files": ["test/**"],
"env": {
"mocha": true
},
"globals": {
"expect": true
},
"rules": {
"no-console": 0
}
Expand Down
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
src/
test/
4 changes: 2 additions & 2 deletions examples/broadcast.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ const app = expressWs.app;

app.ws('/a', (/* ws, req */) => {
});
const aWss = expressWs.getWss('/a');
const wss = expressWs.getWss();

app.ws('/b', (/* ws, req */) => {
});

setInterval(() => {
aWss.clients.forEach((client) => {
wss.clients.forEach((client) => {
client.send('hello');
});
}, 5000);
Expand Down
22 changes: 14 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
"main": "index",
"module": "src/index",
"scripts": {
"mocha": "mocha --require esm --require chai/register-expect --timeout 20000 test",
"test": "echo 'Blanking out cache for ESM/nyc that can get corrupted by terminated processes...' && rm -Rf ./node_modules/.cache && nyc npm run mocha",
"lint": "eslint ."
},
"author": "Henning Morud <[email protected]>",
Expand All @@ -18,13 +20,6 @@
"Alexis Tyler <[email protected]>"
],
"license": "BSD-2-Clause",
"dependencies": {
"esm": "^3.0.84",
"ws": "^6.0.0"
},
"peerDependencies": {
"express": "^4.0.0 || ^5.0.0-alpha.1"
},
"engines": {
"node": ">=4.5.0"
},
Expand All @@ -44,10 +39,21 @@
"url": "https://github.com/HenningM/express-ws/issues"
},
"homepage": "https://github.com/HenningM/express-ws",
"dependencies": {
"esm": "^3.0.84",
"ws": "^6.0.0"
},
"peerDependencies": {
"express": "^4.0.0 || ^5.0.0-alpha.1"
},
"devDependencies": {
"chai": "^4.2.0",
"eslint": "^4.19.0",
"eslint-config-airbnb": "^15.1.0",
"eslint-plugin-import": "^2.12.0",
"express": "^5.0.0-alpha.6"
"express": "^5.0.0-alpha.6",
"got": "^11.4.0",
"mocha": "^8.0.1",
"nyc": "^15.1.0"
}
}
4 changes: 0 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,6 @@ export default function expressWs(app, httpServer, options = {}) {
const wsServer = new ws.Server(wsOptions);

wsServer.on('connection', (socket, request) => {
if ('upgradeReq' in socket) {
request = socket.upgradeReq;
}

request.ws = socket;
request.wsHandled = false;

Expand Down
15 changes: 15 additions & 0 deletions src/wrap-middleware.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,19 @@
export default function wrapMiddleware(middleware) {
if (middleware.length === 4) {
return (error, req, res, next) => {
// Not checking for `req.ws` here as error shouldn't be reached if
// no middleware is available to run first (and that would be
// handled below)
req.wsHandled = true;
try {
/* Unpack the `.ws` property and call the actual handler. */
middleware(error, req.ws, req, next);
} catch (err) {
/* If an error is thrown, let's send that on to any error handling */
next(err);
}
};
}
return (req, res, next) => {
if (req.ws !== null && req.ws !== undefined) {
req.wsHandled = true;
Expand Down
Loading

0 comments on commit a54f4f3

Please sign in to comment.