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
Changes from 2 commits
5a6b2a4
2f22843
62cc34c
f915ce5
8f6bd69
11b6522
bee6f83
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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'); | ||
|
@@ -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'); | ||
|
||
// Workaround for sockjs@~0.3.19 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's move workaround to sockjsServer There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You mean this workaround? https://github.com/webpack/webpack-dev-server/blob/master/lib/Server.js#L41 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||
|
@@ -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, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Better always pass all options (this.options) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @evilebottnawi Are you suggesting, in addition to what is already there, 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Yes, we because behavior can be changed based on options (not only) in theory. Let's do it like: 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) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Better use |
||
if (!connection) { | ||
return; | ||
} | ||
|
@@ -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) { | ||
|
@@ -765,7 +756,7 @@ class Server { | |
|
||
close(cb) { | ||
this.sockets.forEach((socket) => { | ||
socket.close(); | ||
this.socketServer.close(socket); | ||
}); | ||
|
||
this.sockets = []; | ||
|
@@ -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 })); | ||
}); | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
'use strict'; | ||
|
||
// base class that users should extend if they are making their own | ||
// server implementation | ||
module.exports = class BaseServer {}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's move this to |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 {}; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" | ||
}, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No need this right now, it is increase package size |
||
"devDependencies": { | ||
|
@@ -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" | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
rename
sockjsServer
toSockJSServer
because it is class