Skip to content

Commit

Permalink
refactor(server): fixed capitalization
Browse files Browse the repository at this point in the history
  • Loading branch information
knagaitsev committed May 24, 2019
1 parent f915ce5 commit 8f6bd69
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 0 deletions.
9 changes: 9 additions & 0 deletions lib/servers/BaseServer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict';

// base class that users should extend if they are making their own
// server implementation
module.exports = class BaseServer {
constructor(server) {
this.server = server;
}
};
64 changes: 64 additions & 0 deletions lib/servers/SockJSServer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
'use strict';

/* 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(server) {
super(server);
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') {
this.server.log.error(line);
} else {
this.server.log.debug(line);
}
},
});

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

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

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

// f should return the resulting connection
onConnection(f) {
this.socket.on('connection', f);
}
};
8 changes: 8 additions & 0 deletions lib/servers/WsServer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
'use strict';

// 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 {};

0 comments on commit 8f6bd69

Please sign in to comment.