Skip to content

Commit

Permalink
refactor: refactor the handling of the options
Browse files Browse the repository at this point in the history
  • Loading branch information
darrachequesne committed Feb 10, 2020
1 parent 61e639b commit bafe684
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 49 deletions.
70 changes: 31 additions & 39 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,30 @@ class Server extends EventEmitter {
* @param {Object} options
* @api public
*/
constructor(opts) {
constructor(opts = {}) {
super();

this.clients = {};
this.clientsCount = 0;

opts = opts || {};

this.wsEngine = opts.wsEngine || process.env.EIO_WS_ENGINE || "ws";
this.pingTimeout = opts.pingTimeout || 5000;
this.pingInterval = opts.pingInterval || 25000;
this.upgradeTimeout = opts.upgradeTimeout || 10000;
this.maxHttpBufferSize = opts.maxHttpBufferSize || 10e7;
this.transports = opts.transports || Object.keys(transports);
this.allowUpgrades = false !== opts.allowUpgrades;
this.allowRequest = opts.allowRequest;
this.perMessageDeflate =
false !== opts.perMessageDeflate ? opts.perMessageDeflate || true : false;
this.httpCompression =
false !== opts.httpCompression ? opts.httpCompression || {} : false;
this.initialPacket = opts.initialPacket;

this.opts = Object.assign({}, opts);
this.opts = Object.assign(
{
wsEngine: process.env.EIO_WS_ENGINE || "ws",
pingTimeout: 5000,
pingInterval: 25000,
upgradeTimeout: 10000,
maxHttpBufferSize: 10e7,
transports: Object.keys(transports),
allowUpgrades: true,
perMessageDeflate: {
threshold: 1024
},
httpCompression: {
threshold: 1024
}
},
opts
);

if (opts.cookie) {
this.opts.cookie = Object.assign(
Expand All @@ -50,15 +51,6 @@ class Server extends EventEmitter {
);
}

// initialize compression options
["perMessageDeflate", "httpCompression"].forEach(type => {
let compression = this[type];
if (true === compression) this[type] = compression = {};
if (compression && null == compression.threshold) {
compression.threshold = 1024;
}
});

this.init();
}

Expand All @@ -68,12 +60,12 @@ class Server extends EventEmitter {
* @api private
*/
init() {
if (!~this.transports.indexOf("websocket")) return;
if (!~this.opts.transports.indexOf("websocket")) return;

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

let wsModule;
switch (this.wsEngine) {
switch (this.opts.wsEngine) {
case "uws":
wsModule = require("uws");
break;
Expand All @@ -86,8 +78,8 @@ class Server extends EventEmitter {
this.ws = new wsModule.Server({
noServer: true,
clientTracking: false,
perMessageDeflate: this.perMessageDeflate,
maxPayload: this.maxHttpBufferSize
perMessageDeflate: this.opts.perMessageDeflate,
maxPayload: this.opts.maxHttpBufferSize
});
}

Expand All @@ -98,7 +90,7 @@ class Server extends EventEmitter {
* @api public
*/
upgrades(transport) {
if (!this.allowUpgrades) return [];
if (!this.opts.allowUpgrades) return [];
return transports[transport].upgradesTo || [];
}

Expand All @@ -112,7 +104,7 @@ class Server extends EventEmitter {
verify(req, upgrade, fn) {
// transport check
const transport = req._query.transport;
if (!~this.transports.indexOf(transport)) {
if (!~this.opts.transports.indexOf(transport)) {
debug('unknown transport "%s"', transport);
return fn(Server.errors.UNKNOWN_TRANSPORT, false);
}
Expand Down Expand Up @@ -140,8 +132,8 @@ class Server extends EventEmitter {
// handshake is GET only
if ("GET" !== req.method)
return fn(Server.errors.BAD_HANDSHAKE_METHOD, false);
if (!this.allowRequest) return fn(null, true);
return this.allowRequest(req, fn);
if (!this.opts.allowRequest) return fn(null, true);
return this.opts.allowRequest(req, fn);
}

fn(null, true);
Expand Down Expand Up @@ -240,10 +232,10 @@ class Server extends EventEmitter {
try {
var transport = new transports[transportName](req);
if ("polling" === transportName) {
transport.maxHttpBufferSize = this.maxHttpBufferSize;
transport.httpCompression = this.httpCompression;
transport.maxHttpBufferSize = this.opts.maxHttpBufferSize;
transport.httpCompression = this.opts.httpCompression;
} else if ("websocket" === transportName) {
transport.perMessageDeflate = this.perMessageDeflate;
transport.perMessageDeflate = this.opts.perMessageDeflate;
}

if (req._query && req._query.b64) {
Expand Down Expand Up @@ -424,7 +416,7 @@ class Server extends EventEmitter {
}
});

if (~self.transports.indexOf("websocket")) {
if (~self.opts.transports.indexOf("websocket")) {
server.on("upgrade", function(req, socket, head) {
if (check(req)) {
self.handleUpgrade(req, socket, head);
Expand Down
22 changes: 12 additions & 10 deletions lib/socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,13 @@ class Socket extends EventEmitter {
JSON.stringify({
sid: this.id,
upgrades: this.getAvailableUpgrades(),
pingInterval: this.server.pingInterval,
pingTimeout: this.server.pingTimeout
pingInterval: this.server.opts.pingInterval,
pingTimeout: this.server.opts.pingTimeout
})
);

if (this.server.initialPacket) {
this.sendPacket("message", this.server.initialPacket);
if (this.server.opts.initialPacket) {
this.sendPacket("message", this.server.opts.initialPacket);
}

this.emit("open");
Expand All @@ -78,7 +78,9 @@ class Socket extends EventEmitter {

// Reset ping timeout on any packet, incoming data is a good sign of
// other side's liveness
this.resetPingTimeout(this.server.pingInterval + this.server.pingTimeout);
this.resetPingTimeout(
this.server.opts.pingInterval + this.server.opts.pingTimeout
);

switch (packet.type) {
case "pong":
Expand Down Expand Up @@ -123,11 +125,11 @@ class Socket extends EventEmitter {
this.pingIntervalTimer = setTimeout(() => {
debug(
"writing ping packet - expecting pong within %sms",
this.server.pingTimeout
this.server.opts.pingTimeout
);
this.sendPacket("ping");
this.resetPingTimeout(this.server.pingTimeout);
}, this.server.pingInterval);
this.resetPingTimeout(this.server.opts.pingTimeout);
}, this.server.opts.pingInterval);
}

/**
Expand Down Expand Up @@ -195,7 +197,7 @@ class Socket extends EventEmitter {
if ("open" === transport.readyState) {
transport.close();
}
}, this.server.upgradeTimeout);
}, this.server.opts.upgradeTimeout);

function onPacket(packet) {
if ("ping" === packet.type && "probe" === packet.data) {
Expand Down Expand Up @@ -455,7 +457,7 @@ class Socket extends EventEmitter {
const l = allUpgrades.length;
for (; i < l; ++i) {
const upg = allUpgrades[i];
if (this.server.transports.indexOf(upg) !== -1) {
if (this.server.opts.transports.indexOf(upg) !== -1) {
availableUpgrades.push(upg);
}
}
Expand Down

0 comments on commit bafe684

Please sign in to comment.