Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Encapsulate sockjs, introduce ws and build base for users to make own server implementation #1904

Merged
merged 7 commits into from May 28, 2019
Merged
29 changes: 10 additions & 19 deletions lib/Server.js
Expand Up @@ -12,7 +12,6 @@ const url = require('url');
const http = require('http');
const https = require('https');
const ip = require('ip');
const sockjs = require('sockjs');
const semver = require('semver');
const killable = require('killable');
const chokidar = require('chokidar');
Expand All @@ -32,6 +31,7 @@ const createDomain = require('./utils/createDomain');
const runBonjour = require('./utils/runBonjour');
const routes = require('./utils/routes');
const schema = require('./options.json');
const SockjsServer = require('./utils/server/sockjsServer');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rename sockjsServer to SockJSServer because it is class


// Workaround for sockjs@~0.3.19
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's move workaround to sockjsServer

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes

// sockjs will remove Origin header, however Origin header is required for checking host.
Expand Down Expand Up @@ -674,20 +674,15 @@ class Server {
}

createSocketServer() {
const socket = sockjs.createServer({
// Use provided up-to-date sockjs-client
sockjs_url: '/__webpack_dev_server__/sockjs.bundle.js',
// Limit useless logs
log: (severity, line) => {
if (severity === 'error') {
this.log.error(line);
} else {
this.log.debug(line);
}
},
const socket = new SockjsServer({
error: this.log.error.bind(this),
debug: this.log.debug.bind(this),
server: this.listeningApp,
path: this.sockPath,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better always pass all options (this.options)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@evilebottnawi Are you suggesting, in addition to what is already there, options: this.options? Because this.listeningApp is not part of the options, along with the error, debug callbacks.

Users may be creating server implementations of their own, so they would be interacting with the options passed in. Do you think it's a good idea to expose all the options to the user?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Loonride

Do you think it's a good idea to expose all the options to the user?

Yes, we because behavior can be changed based on options (not only) in theory.

Let's do it like:
new SockJSServer(this);

Custom implementation should have access to compiler, application, server, options and devServer.

Also it is not available for developers right now, so we can change this in future.

});
this.socketServer = socket;

socket.on('connection', (connection) => {
socket.onConnection((connection) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better use this.sockerServer everywhere

if (!connection) {
return;
}
Expand Down Expand Up @@ -739,10 +734,6 @@ class Server {

this._sendStats([connection], this.getStats(this._stats), true);
});

socket.installHandlers(this.listeningApp, {
prefix: this.sockPath,
});
}

listen(port, hostname, fn) {
Expand All @@ -765,7 +756,7 @@ class Server {

close(cb) {
this.sockets.forEach((socket) => {
socket.close();
this.socketServer.close(socket);
});

this.sockets = [];
Expand Down Expand Up @@ -926,7 +917,7 @@ class Server {
// eslint-disable-next-line
sockWrite(sockets, type, data) {
sockets.forEach((socket) => {
socket.write(JSON.stringify({ type, data }));
this.socketServer.send(socket, JSON.stringify({ type, data }));
});
}

Expand Down
5 changes: 5 additions & 0 deletions lib/utils/server/baseServer.js
@@ -0,0 +1,5 @@
'use strict';

// base class that users should extend if they are making their own
// server implementation
module.exports = class BaseServer {};
43 changes: 43 additions & 0 deletions lib/utils/server/sockjsServer.js
@@ -0,0 +1,43 @@
'use strict';

/* eslint-disable
class-methods-use-this
*/
const sockjs = require('sockjs');
const BaseServer = require('./baseServer');

module.exports = class SockjsServer extends BaseServer {
// options has: error (function), debug (function), server (http/s server), path (string)
constructor(options) {
super();
this.socket = sockjs.createServer({
// Use provided up-to-date sockjs-client
sockjs_url: '/__webpack_dev_server__/sockjs.bundle.js',
// Limit useless logs
log: (severity, line) => {
if (severity === 'error') {
options.error(line);
} else {
options.debug(line);
}
},
});

this.socket.installHandlers(options.server, {
prefix: options.path,
});
}

send(connection, message) {
connection.write(message);
}

close(connection) {
connection.close();
}

// f should return the resulting connection
onConnection(f) {
this.socket.on('connection', f);
}
};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's move this to lib/servers/sockjsServer.js, it is not util

11 changes: 11 additions & 0 deletions lib/utils/server/wsServer.js
@@ -0,0 +1,11 @@
'use strict';

/* eslint-disable
no-unused-vars
*/
const ws = require('ws');
const BaseServer = require('./baseServer');

// ws server implementation under construction
// will need changes in the client as well to function
module.exports = class WSServer extends BaseServer {};
29 changes: 8 additions & 21 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Expand Up @@ -62,6 +62,7 @@
"url": "^0.11.0",
"webpack-dev-middleware": "^3.7.0",
"webpack-log": "^2.0.0",
"ws": "^6.2.1",
"yargs": "12.0.5"
},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need this right now, it is increase package size

"devDependencies": {
Expand Down Expand Up @@ -101,8 +102,7 @@
"supertest": "^4.0.2",
"url-loader": "^1.1.2",
"webpack": "^4.31.0",
"webpack-cli": "^3.3.2",
"ws": "^6.2.1"
"webpack-cli": "^3.3.2"
},
"peerDependencies": {
"webpack": "^4.0.0"
Expand Down