Skip to content

Commit 4bf1f76

Browse files
trescenzievilebottnawi
authored andcommitted
feat: add sockPath option (options.sockPath) (#1553)
1 parent 11c9896 commit 4bf1f76

File tree

6 files changed

+68
-11
lines changed

6 files changed

+68
-11
lines changed

client-src/default/index.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
/* global __resourceQuery WorkerGlobalScope self */
44
/* eslint prefer-destructuring: off */
5-
5+
const querystring = require('querystring');
66
const url = require('url');
77
const stripAnsi = require('strip-ansi');
88
const log = require('loglevel').getLogger('webpack-dev-server');
@@ -196,7 +196,10 @@ const socketUrl = url.format({
196196
auth: urlParts.auth,
197197
hostname,
198198
port: urlParts.port,
199-
pathname: urlParts.path == null || urlParts.path === '/' ? '/sockjs-node' : urlParts.path
199+
// If sockPath is provided it'll be passed in via the __resourceQuery as a
200+
// query param so it has to be parsed out of the querystring in order for the
201+
// client to open the socket to the correct location.
202+
pathname: urlParts.path == null || urlParts.path === '/' ? '/sockjs-node' : (querystring.parse(urlParts.path).sockPath || urlParts.path)
200203
});
201204

202205
socket(socketUrl, onSocketMsg);

lib/Server.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ function Server (compiler, options = {}, _log) {
101101

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

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

800802
socket.installHandlers(this.listeningApp, {
801-
prefix: '/sockjs-node'
803+
prefix: this.sockPath
802804
});
803805

804806
if (fn) {

lib/options.json

+4
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@
5151
"socket": {
5252
"type": "string"
5353
},
54+
"sockPath": {
55+
"type": "string"
56+
},
5457
"watchOptions": {
5558
"type": "object"
5659
},
@@ -321,6 +324,7 @@
321324
"filename": "should be {String|RegExp|Function} (https://webpack.js.org/configuration/dev-server/#devserver-filename-)",
322325
"port": "should be {String|Number} (https://webpack.js.org/configuration/dev-server/#devserver-port)",
323326
"socket": "should be {String} (https://webpack.js.org/configuration/dev-server/#devserver-socket)",
327+
"sockPath": "should be {String} (https://webpack.js.org/configuration/dev-server/#devserver-sockPath)",
324328
"watchOptions": "should be {Object} (https://webpack.js.org/configuration/dev-server/#devserver-watchoptions)",
325329
"writeToDisk": "should be {Boolean|Function} (https://github.com/webpack/webpack-dev-middleware#writetodisk)",
326330
"headers": "should be {Object} (https://webpack.js.org/configuration/dev-server/#devserver-headers-)",

lib/utils/addEntries.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ function addEntries (config, options, server) {
2121
};
2222

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

2627
if (options.hotOnly) {
2728
entries.push(require.resolve('webpack/hot/only-dev-server'));

package-lock.json

+5-7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/Socket.test.js

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
'use strict';
2+
3+
const assert = require('assert');
4+
const request = require('supertest');
5+
const config = require('./fixtures/simple-config/webpack.config');
6+
const helper = require('./helper');
7+
8+
describe('socket options', () => {
9+
let server;
10+
let req;
11+
12+
afterEach((done) => {
13+
helper.close(done);
14+
req = null;
15+
server = null;
16+
});
17+
describe('default behavior', () => {
18+
beforeEach((done) => {
19+
server = helper.start(config, {}, done);
20+
req = request('http://localhost:8080');
21+
});
22+
23+
it('defaults to a path', () => {
24+
assert.ok(server.sockPath.match(/\/[a-z0-9\-/]+[^/]$/));
25+
});
26+
27+
it('responds with a 200', (done) => {
28+
req.get('/sockjs-node').expect(200, done);
29+
});
30+
});
31+
32+
describe('socksPath option', () => {
33+
const path = '/foo/test/bar';
34+
beforeEach((done) => {
35+
server = helper.start(config, {
36+
sockPath: '/foo/test/bar/'
37+
}, done);
38+
req = request('http://localhost:8080');
39+
});
40+
41+
it('sets the sock path correctly and strips leading and trailing /s', () => {
42+
assert.equal(server.sockPath, path);
43+
});
44+
45+
it('responds with a 200 second', (done) => {
46+
req.get(path).expect(200, done);
47+
});
48+
});
49+
});

0 commit comments

Comments
 (0)