|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +/* eslint-disable |
| 4 | + class-methods-use-this, |
| 5 | + func-names |
| 6 | +*/ |
| 7 | +const sockjs = require('sockjs'); |
| 8 | +const BaseServer = require('./BaseServer'); |
| 9 | + |
| 10 | +// Workaround for sockjs@~0.3.19 |
| 11 | +// sockjs will remove Origin header, however Origin header is required for checking host. |
| 12 | +// See https://github.com/webpack/webpack-dev-server/issues/1604 for more information |
| 13 | +{ |
| 14 | + // eslint-disable-next-line global-require |
| 15 | + const SockjsSession = require('sockjs/lib/transport').Session; |
| 16 | + const decorateConnection = SockjsSession.prototype.decorateConnection; |
| 17 | + SockjsSession.prototype.decorateConnection = function(req) { |
| 18 | + decorateConnection.call(this, req); |
| 19 | + const connection = this.connection; |
| 20 | + if ( |
| 21 | + connection.headers && |
| 22 | + !('origin' in connection.headers) && |
| 23 | + 'origin' in req.headers |
| 24 | + ) { |
| 25 | + connection.headers.origin = req.headers.origin; |
| 26 | + } |
| 27 | + }; |
| 28 | +} |
| 29 | + |
| 30 | +module.exports = class SockJSServer extends BaseServer { |
| 31 | + // options has: error (function), debug (function), server (http/s server), path (string) |
| 32 | + constructor(server) { |
| 33 | + super(server); |
| 34 | + this.socket = sockjs.createServer({ |
| 35 | + // Use provided up-to-date sockjs-client |
| 36 | + sockjs_url: '/__webpack_dev_server__/sockjs.bundle.js', |
| 37 | + // Limit useless logs |
| 38 | + log: (severity, line) => { |
| 39 | + if (severity === 'error') { |
| 40 | + this.server.log.error(line); |
| 41 | + } else { |
| 42 | + this.server.log.debug(line); |
| 43 | + } |
| 44 | + }, |
| 45 | + }); |
| 46 | + |
| 47 | + this.socket.installHandlers(this.server.listeningApp, { |
| 48 | + prefix: this.server.sockPath, |
| 49 | + }); |
| 50 | + } |
| 51 | + |
| 52 | + send(connection, message) { |
| 53 | + connection.write(message); |
| 54 | + } |
| 55 | + |
| 56 | + close(connection) { |
| 57 | + connection.close(); |
| 58 | + } |
| 59 | + |
| 60 | + // f should return the resulting connection |
| 61 | + onConnection(f) { |
| 62 | + this.socket.on('connection', f); |
| 63 | + } |
| 64 | +}; |
0 commit comments