Skip to content

Commit edb7343

Browse files
feat: remove dynamic require() with wsEngine
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
1 parent 868d891 commit edb7343

File tree

4 files changed

+11
-8
lines changed

4 files changed

+11
-8
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ server.on('connection', /* ... */);
188188
const engine = require('engine.io');
189189
const httpServer = require('http').createServer().listen(3000);
190190
const server = engine.attach(httpServer, {
191-
wsEngine: 'uws' // requires having uws as dependency
191+
wsEngine: require('eiows').Server // requires having eiows as dependency
192192
});
193193

194194
server.on('connection', /* ... */);
@@ -249,7 +249,7 @@ to a single process.
249249
contains the client sid to send as part of handshake response
250250
headers. This cookie might be used for sticky-session. Defaults to not sending any cookie (`false`).
251251
See [here](https://github.com/jshttp/cookie#options-1) for all supported options.
252-
- `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.
252+
- `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.
253253
- `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.
254254
- `initialPacket` (`Object`): an optional packet which will be concatenated to the handshake packet emitted by Engine.IO.
255255
- `allowEIO3` (`Boolean`): whether to support v3 Engine.IO clients (defaults to `false`)

lib/server.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ const Socket = require("./socket");
77
const debug = require("debug")("engine");
88
const cookieMod = require("cookie");
99

10+
const DEFAULT_WS_ENGINE = require("ws").Server;
11+
1012
class Server extends EventEmitter {
1113
/**
1214
* Server constructor.
@@ -22,7 +24,7 @@ class Server extends EventEmitter {
2224

2325
this.opts = Object.assign(
2426
{
25-
wsEngine: process.env.EIO_WS_ENGINE || "ws",
27+
wsEngine: DEFAULT_WS_ENGINE,
2628
pingTimeout: 20000,
2729
pingInterval: 25000,
2830
upgradeTimeout: 10000,
@@ -76,10 +78,7 @@ class Server extends EventEmitter {
7678

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

79-
// add explicit require for bundlers like webpack
80-
const wsModule =
81-
this.opts.wsEngine === "ws" ? require("ws") : require(this.opts.wsEngine);
82-
this.ws = new wsModule.Server({
81+
this.ws = new this.opts.wsEngine({
8382
noServer: true,
8483
clientTracking: false,
8584
perMessageDeflate: this.opts.perMessageDeflate,

test/common.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ exports.listen = (opts, fn) => {
1616

1717
opts.allowEIO3 = true;
1818

19+
if (process.env.EIO_WS_ENGINE) {
20+
opts.wsEngine = require(process.env.EIO_WS_ENGINE).Server;
21+
}
22+
1923
const e = eio.listen(0, opts, () => {
2024
fn(e.httpServer.address().port);
2125
});

test/server.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3121,7 +3121,7 @@ describe("server", () => {
31213121
describe("wsEngine option", () => {
31223122
it("should allow loading of other websocket server implementation like eiows", done => {
31233123
const engine = listen(
3124-
{ allowUpgrades: false, wsEngine: "eiows" },
3124+
{ allowUpgrades: false, wsEngine: require("eiows").Server },
31253125
port => {
31263126
expect(engine.ws instanceof require("eiows").Server).to.be.ok();
31273127
const socket = new eioc.Socket("ws://localhost:%d".s(port));

0 commit comments

Comments
 (0)