Skip to content

Commit

Permalink
feat: add sockPath option (options.sockPath) (#1553)
Browse files Browse the repository at this point in the history
  • Loading branch information
trescenzi authored and evilebottnawi committed Jan 30, 2019
1 parent 11c9896 commit 4bf1f76
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
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
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
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
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
@@ -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);
});
});
});

2 comments on commit 4bf1f76

@ebrentnelson
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉 please release this soon it will save the day!

@alexander-akait
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ebrentnelson near future (tomorrow i think)

Please sign in to comment.