Skip to content

Commit

Permalink
feat: remove dynamic require() with wsEngine
Browse files Browse the repository at this point in the history
This change is necessary to get rid of:

> Critical dependency: the request of a dependency is an expression

when bundling the server with webpack.

BREAKING CHANGE: the syntax of the "wsEngine" option is updated

Before:

```js
const eioServer = require("engine.io")(httpServer, {
  wsEngine: "eiows"
});
```

After:

```js
const eioServer = require("engine.io")(httpServer, {
  wsEngine: require("eiows").Server
});
```

Related: #609
  • Loading branch information
darrachequesne committed Mar 9, 2021
1 parent 868d891 commit edb7343
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 8 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ server.on('connection', /* ... */);
const engine = require('engine.io');
const httpServer = require('http').createServer().listen(3000);
const server = engine.attach(httpServer, {
wsEngine: 'uws' // requires having uws as dependency
wsEngine: require('eiows').Server // requires having eiows as dependency
});

server.on('connection', /* ... */);
Expand Down Expand Up @@ -249,7 +249,7 @@ to a single process.
contains the client sid to send as part of handshake response
headers. This cookie might be used for sticky-session. Defaults to not sending any cookie (`false`).
See [here](https://github.com/jshttp/cookie#options-1) for all supported options.
- `wsEngine` (`String`): what WebSocket server implementation to use. Specified module must conform to the `ws` interface (see [ws module api docs](https://github.com/websockets/ws/blob/master/doc/ws.md)). Default value is `ws`. An alternative c++ addon is also available by installing `uws` module.
- `wsEngine` (`Function`): what WebSocket server implementation to use. Specified module must conform to the `ws` interface (see [ws module api docs](https://github.com/websockets/ws/blob/master/doc/ws.md)). Default value is `ws`. An alternative c++ addon is also available by installing `eiows` module.
- `cors` (`Object`): the options that will be forwarded to the cors module. See [there](https://github.com/expressjs/cors#configuration-options) for all available options. Defaults to no CORS allowed.
- `initialPacket` (`Object`): an optional packet which will be concatenated to the handshake packet emitted by Engine.IO.
- `allowEIO3` (`Boolean`): whether to support v3 Engine.IO clients (defaults to `false`)
Expand Down
9 changes: 4 additions & 5 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ const Socket = require("./socket");
const debug = require("debug")("engine");
const cookieMod = require("cookie");

const DEFAULT_WS_ENGINE = require("ws").Server;

class Server extends EventEmitter {
/**
* Server constructor.
Expand All @@ -22,7 +24,7 @@ class Server extends EventEmitter {

this.opts = Object.assign(
{
wsEngine: process.env.EIO_WS_ENGINE || "ws",
wsEngine: DEFAULT_WS_ENGINE,
pingTimeout: 20000,
pingInterval: 25000,
upgradeTimeout: 10000,
Expand Down Expand Up @@ -76,10 +78,7 @@ class Server extends EventEmitter {

if (this.ws) this.ws.close();

// add explicit require for bundlers like webpack
const wsModule =
this.opts.wsEngine === "ws" ? require("ws") : require(this.opts.wsEngine);
this.ws = new wsModule.Server({
this.ws = new this.opts.wsEngine({
noServer: true,
clientTracking: false,
perMessageDeflate: this.opts.perMessageDeflate,
Expand Down
4 changes: 4 additions & 0 deletions test/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ exports.listen = (opts, fn) => {

opts.allowEIO3 = true;

if (process.env.EIO_WS_ENGINE) {
opts.wsEngine = require(process.env.EIO_WS_ENGINE).Server;
}

const e = eio.listen(0, opts, () => {
fn(e.httpServer.address().port);
});
Expand Down
2 changes: 1 addition & 1 deletion test/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -3121,7 +3121,7 @@ describe("server", () => {
describe("wsEngine option", () => {
it("should allow loading of other websocket server implementation like eiows", done => {
const engine = listen(
{ allowUpgrades: false, wsEngine: "eiows" },
{ allowUpgrades: false, wsEngine: require("eiows").Server },
port => {
expect(engine.ws instanceof require("eiows").Server).to.be.ok();
const socket = new eioc.Socket("ws://localhost:%d".s(port));
Expand Down

0 comments on commit edb7343

Please sign in to comment.