Skip to content

Commit

Permalink
refactor: listen method (#1884)
Browse files Browse the repository at this point in the history
  • Loading branch information
evilebottnawi committed May 16, 2019
1 parent c535bb2 commit 294e155
Showing 1 changed file with 104 additions and 101 deletions.
205 changes: 104 additions & 101 deletions lib/Server.js
Expand Up @@ -675,6 +675,110 @@ 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);
}
},
});

socket.on('connection', (connection) => {
if (!connection) {
return;
}

if (
!this.checkHost(connection.headers) ||
!this.checkOrigin(connection.headers)
) {
this.sockWrite([connection], 'error', 'Invalid Host/Origin header');

connection.close();

return;
}

this.sockets.push(connection);

connection.on('close', () => {
const idx = this.sockets.indexOf(connection);

if (idx >= 0) {
this.sockets.splice(idx, 1);
}
});

if (this.hot) {
this.sockWrite([connection], 'hot');
}

if (this.progress) {
this.sockWrite([connection], 'progress', this.progress);
}

if (this.clientOverlay) {
this.sockWrite([connection], 'overlay', this.clientOverlay);
}

if (this.clientLogLevel) {
this.sockWrite([connection], 'log-level', this.clientLogLevel);
}

if (!this._stats) {
return;
}

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

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

listen(port, hostname, fn) {
this.hostname = hostname;

return this.listeningApp.listen(port, hostname, (err) => {
this.createSocketServer();

if (this.options.bonjour) {
runBonjour(this.options);
}

this.showStatus();

if (fn) {
fn.call(this.listeningApp, err);
}
});
}

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

this.sockets = [];

this.contentBaseWatchers.forEach((watcher) => {
watcher.close();
});

this.contentBaseWatchers = [];

this.listeningApp.kill(() => {
this.middleware.close(cb);
});
}

static get DEFAULT_STATS() {
return {
all: false,
Expand Down Expand Up @@ -817,107 +921,6 @@ class Server {
);
}

// delegate listen call and init sockjs
listen(port, hostname, fn) {
this.hostname = hostname;

return this.listeningApp.listen(port, hostname, (err) => {
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);
}
},
});

socket.on('connection', (connection) => {
if (!connection) {
return;
}

if (
!this.checkHost(connection.headers) ||
!this.checkOrigin(connection.headers)
) {
this.sockWrite([connection], 'error', 'Invalid Host/Origin header');

connection.close();

return;
}

this.sockets.push(connection);

connection.on('close', () => {
const idx = this.sockets.indexOf(connection);

if (idx >= 0) {
this.sockets.splice(idx, 1);
}
});

if (this.hot) {
this.sockWrite([connection], 'hot');
}

if (this.progress) {
this.sockWrite([connection], 'progress', this.progress);
}

if (this.clientOverlay) {
this.sockWrite([connection], 'overlay', this.clientOverlay);
}

if (this.clientLogLevel) {
this.sockWrite([connection], 'log-level', this.clientLogLevel);
}

if (!this._stats) {
return;
}

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

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

if (this.options.bonjour) {
runBonjour(this.options);
}

this.showStatus();

if (fn) {
fn.call(this.listeningApp, err);
}
});
}

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

this.sockets = [];

this.contentBaseWatchers.forEach((watcher) => {
watcher.close();
});

this.contentBaseWatchers = [];

this.listeningApp.kill(() => {
this.middleware.close(cb);
});
}

// eslint-disable-next-line
sockWrite(sockets, type, data) {
sockets.forEach((socket) => {
Expand Down

0 comments on commit 294e155

Please sign in to comment.