Skip to content

Commit

Permalink
feat: add sockPath option (options.sockPath) (webpack#1553)
Browse files Browse the repository at this point in the history
  • Loading branch information
trescenzi authored and Carlos Requena López committed Feb 26, 2019
1 parent acfaaf8 commit 9fc1476
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 11 deletions.
7 changes: 5 additions & 2 deletions client-src/default/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

/* global __resourceQuery WorkerGlobalScope self */
/* eslint prefer-destructuring: off */

const querystring = require('querystring');
const url = require('url');
const stripAnsi = require('strip-ansi');
const log = require('loglevel').getLogger('webpack-dev-server');
Expand Down Expand Up @@ -196,7 +196,10 @@ const socketUrl = url.format({
auth: urlParts.auth,
hostname,
port: urlParts.port,
pathname: urlParts.path == null || urlParts.path === '/' ? '/sockjs-node' : urlParts.path
// If sockPath is provided it'll be passed in via the __resourceQuery as a
// query param so it has to be parsed out of the querystring in order for the
// client to open the socket to the correct location.
pathname: urlParts.path == null || urlParts.path === '/' ? '/sockjs-node' : (querystring.parse(urlParts.path).sockPath || urlParts.path)
});

socket(socketUrl, onSocketMsg);
Expand Down
4 changes: 3 additions & 1 deletion lib/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ function Server (compiler, options = {}, _log) {

this.watchOptions = options.watchOptions || {};
this.contentBaseWatchers = [];
// Replace leading and trailing slashes to normalize path
this.sockPath = `/${options.sockPath ? options.sockPath.replace(/^\/|\/$/g, '') : 'sockjs-node'}`;

// Listening for events
const invalidPlugin = () => {
Expand Down Expand Up @@ -798,7 +800,7 @@ Server.prototype.listen = function (port, hostname, fn) {
});

socket.installHandlers(this.listeningApp, {
prefix: '/sockjs-node'
prefix: this.sockPath
});

if (fn) {
Expand Down
4 changes: 4 additions & 0 deletions lib/options.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@
"socket": {
"type": "string"
},
"sockPath": {
"type": "string"
},
"watchOptions": {
"type": "object"
},
Expand Down Expand Up @@ -321,6 +324,7 @@
"filename": "should be {String|RegExp|Function} (https://webpack.js.org/configuration/dev-server/#devserver-filename-)",
"port": "should be {String|Number} (https://webpack.js.org/configuration/dev-server/#devserver-port)",
"socket": "should be {String} (https://webpack.js.org/configuration/dev-server/#devserver-socket)",
"sockPath": "should be {String} (https://webpack.js.org/configuration/dev-server/#devserver-sockPath)",
"watchOptions": "should be {Object} (https://webpack.js.org/configuration/dev-server/#devserver-watchoptions)",
"writeToDisk": "should be {Boolean|Function} (https://github.com/webpack/webpack-dev-middleware#writetodisk)",
"headers": "should be {Object} (https://webpack.js.org/configuration/dev-server/#devserver-headers-)",
Expand Down
3 changes: 2 additions & 1 deletion lib/utils/addEntries.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ function addEntries (config, options, server) {
};

const domain = createDomain(options, app);
const entries = [ `${require.resolve('../../client/')}?${domain}` ];
const sockPath = options.sockPath ? `&sockPath=${options.sockPath}` : '';
const entries = [ `${require.resolve('../../client/')}?${domain}${sockPath}` ];

if (options.hotOnly) {
entries.push(require.resolve('webpack/hot/only-dev-server'));
Expand Down
12 changes: 5 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

49 changes: 49 additions & 0 deletions test/Socket.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
'use strict';

const assert = require('assert');
const request = require('supertest');
const config = require('./fixtures/simple-config/webpack.config');
const helper = require('./helper');

describe('socket options', () => {
let server;
let req;

afterEach((done) => {
helper.close(done);
req = null;
server = null;
});
describe('default behavior', () => {
beforeEach((done) => {
server = helper.start(config, {}, done);
req = request('http://localhost:8080');
});

it('defaults to a path', () => {
assert.ok(server.sockPath.match(/\/[a-z0-9\-/]+[^/]$/));
});

it('responds with a 200', (done) => {
req.get('/sockjs-node').expect(200, done);
});
});

describe('socksPath option', () => {
const path = '/foo/test/bar';
beforeEach((done) => {
server = helper.start(config, {
sockPath: '/foo/test/bar/'
}, done);
req = request('http://localhost:8080');
});

it('sets the sock path correctly and strips leading and trailing /s', () => {
assert.equal(server.sockPath, path);
});

it('responds with a 200 second', (done) => {
req.get(path).expect(200, done);
});
});
});

0 comments on commit 9fc1476

Please sign in to comment.