-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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 3 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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
'use strict'; | ||
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. The file name should be changed from |
||
|
||
// 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,64 @@ | ||
'use strict'; | ||
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. The file name should be changed from |
||
|
||
/* eslint-disable | ||
class-methods-use-this, | ||
func-names | ||
*/ | ||
const sockjs = require('sockjs'); | ||
const BaseServer = require('./baseServer'); | ||
|
||
// Workaround for sockjs@~0.3.19 | ||
// sockjs will remove Origin header, however Origin header is required for checking host. | ||
// See https://github.com/webpack/webpack-dev-server/issues/1604 for more information | ||
{ | ||
// eslint-disable-next-line global-require | ||
const SockjsSession = require('sockjs/lib/transport').Session; | ||
const decorateConnection = SockjsSession.prototype.decorateConnection; | ||
SockjsSession.prototype.decorateConnection = function(req) { | ||
decorateConnection.call(this, req); | ||
const connection = this.connection; | ||
if ( | ||
connection.headers && | ||
!('origin' in connection.headers) && | ||
'origin' in req.headers | ||
) { | ||
connection.headers.origin = req.headers.origin; | ||
} | ||
}; | ||
} | ||
|
||
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); | ||
} | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
'use strict'; | ||
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. The file name should be changed from |
||
|
||
// 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.
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.
Better always pass all options (this.options)
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.
@evilebottnawi Are you suggesting, in addition to what is already there,
options: this.options
? Becausethis.listeningApp
is not part of the options, along with theerror
,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?
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.
@Loonride
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.