Skip to content

Commit 61b9492

Browse files
feat: use the cors module to handle cross-origin requests
We'll now rely on the standard cors module (https://github.com/expressjs/cors), instead of the custom implementation that is error-prone and not really user-friendly. Breaking change: the handlePreflightRequest option is removed by the change. Before: ``` new Server({ handlePreflightRequest: (req, res) => { res.writeHead(200, { "Access-Control-Allow-Origin": 'https://example.com', "Access-Control-Allow-Methods": 'GET', "Access-Control-Allow-Headers": 'Authorization', "Access-Control-Allow-Credentials": true }); res.end(); } }) ``` After: ``` new Server({ cors: { origin: "https://example.com", methods: ["GET"], allowedHeaders: ["Authorization"], credentials: true } }) ```
1 parent bafe684 commit 61b9492

File tree

7 files changed

+164
-143
lines changed

7 files changed

+164
-143
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,7 @@ to a single process.
249249
headers. This cookie might be used for sticky-session. Defaults to not sending any cookie (`false`).
250250
See [here](https://github.com/jshttp/cookie#options-1) for all supported options.
251251
- `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+
- `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.
252253
- `initialPacket` (`Object`): an optional packet which will be concatenated to the handshake packet emitted by Engine.IO.
253254
- `close`
254255
- Closes all clients
@@ -277,7 +278,6 @@ to a single process.
277278
- `path` (`String`): name of the path to capture (`/engine.io`).
278279
- `destroyUpgrade` (`Boolean`): destroy unhandled upgrade requests (`true`)
279280
- `destroyUpgradeTimeout` (`Number`): milliseconds after which unhandled requests are ended (`1000`)
280-
- `handlePreflightRequest` (`Boolean|Function`): whether to let engine.io handle the OPTIONS requests. You can also pass a custom function to handle the requests (`true`)
281281
- `generateId`
282282
- Generate a socket id.
283283
- Overwrite this method to generate your custom socket id.

lib/server.js

+19-20
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ class Server extends EventEmitter {
3434
},
3535
httpCompression: {
3636
threshold: 1024
37-
}
37+
},
38+
cors: false
3839
},
3940
opts
4041
);
@@ -51,6 +52,10 @@ class Server extends EventEmitter {
5152
);
5253
}
5354

55+
if (this.opts.cors) {
56+
this.corsMiddleware = require("cors")(this.opts.cors);
57+
}
58+
5459
this.init();
5560
}
5661

@@ -183,20 +188,27 @@ class Server extends EventEmitter {
183188
this.prepare(req);
184189
req.res = res;
185190

186-
const self = this;
187-
this.verify(req, false, function(err, success) {
191+
const callback = (err, success) => {
188192
if (!success) {
189193
sendErrorMessage(req, res, err);
190194
return;
191195
}
192196

193197
if (req._query.sid) {
194198
debug("setting new request for existing client");
195-
self.clients[req._query.sid].transport.onRequest(req);
199+
this.clients[req._query.sid].transport.onRequest(req);
196200
} else {
197-
self.handshake(req._query.transport, req);
201+
this.handshake(req._query.transport, req);
198202
}
199-
});
203+
};
204+
205+
if (this.corsMiddleware) {
206+
this.corsMiddleware.call(null, req, res, () => {
207+
this.verify(req, false, callback);
208+
});
209+
} else {
210+
this.verify(req, false, callback);
211+
}
200212
}
201213

202214
/**
@@ -380,12 +392,6 @@ class Server extends EventEmitter {
380392
path += "/";
381393

382394
function check(req) {
383-
if (
384-
"OPTIONS" === req.method &&
385-
false === options.handlePreflightRequest
386-
) {
387-
return false;
388-
}
389395
return path === req.url.substr(0, path.length);
390396
}
391397

@@ -399,14 +405,7 @@ class Server extends EventEmitter {
399405
server.on("request", function(req, res) {
400406
if (check(req)) {
401407
debug('intercepting request for path "%s"', path);
402-
if (
403-
"OPTIONS" === req.method &&
404-
"function" === typeof options.handlePreflightRequest
405-
) {
406-
options.handlePreflightRequest.call(server, req, res);
407-
} else {
408-
self.handleRequest(req, res);
409-
}
408+
self.handleRequest(req, res);
410409
} else {
411410
let i = 0;
412411
const l = listeners.length;

lib/transports/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const XHR = require("./polling-xhr");
1+
const XHR = require("./polling");
22
const JSONP = require("./polling-jsonp");
33

44
/**

lib/transports/polling-xhr.js

-43
This file was deleted.

package-lock.json

+15-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"accepts": "~1.3.4",
2929
"base64id": "2.0.0",
3030
"cookie": "0.3.1",
31+
"cors": "~2.8.5",
3132
"debug": "~4.1.0",
3233
"engine.io-parser": "git+https://github.com/socketio/engine.io-parser.git#v4",
3334
"ws": "^7.1.2"
@@ -57,7 +58,7 @@
5758
"files": [
5859
"lib/"
5960
],
60-
"engines" : {
61-
"node" : ">=8.0.0"
61+
"engines": {
62+
"node": ">=8.0.0"
6263
}
6364
}

0 commit comments

Comments
 (0)