Skip to content

Commit fe80fa4

Browse files
authored
Union sock options (#2047)
* refactor: update snapshot & change clientSocketOptions type to Object * refactor: union sockHost, sockPath and sockPort in clientSocketOptions * refactor: remove console.error
1 parent 0e875f8 commit fe80fa4

File tree

9 files changed

+76
-51
lines changed

9 files changed

+76
-51
lines changed

lib/Server.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,14 @@ class Server {
9191
this.hot = this.options.hot || this.options.hotOnly;
9292
this.watchOptions = options.watchOptions || {};
9393

94+
if (!this.options.clientSocketOptions) {
95+
this.options.clientSocketOptions = {};
96+
}
97+
9498
// Replace leading and trailing slashes to normalize path
9599
this.sockPath = `/${
96-
this.options.sockPath
97-
? this.options.sockPath.replace(/^\/|\/$/g, '')
100+
this.options.clientSocketOptions.path
101+
? this.options.clientSocketOptions.path.replace(/^\/|\/$/g, '')
98102
: 'sockjs-node'
99103
}`;
100104

lib/options.json

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,27 @@
3939
"clientLogLevel": {
4040
"enum": ["info", "warn", "error", "debug", "trace", "silent"]
4141
},
42+
"clientSocketOptions": {
43+
"type": "object",
44+
"properties": {
45+
"host": {
46+
"type": "string"
47+
},
48+
"path": {
49+
"type": "string"
50+
},
51+
"port": {
52+
"anyOf": [
53+
{
54+
"type": "string"
55+
},
56+
{
57+
"type": "number"
58+
}
59+
]
60+
}
61+
}
62+
},
4263
"compress": {
4364
"type": "boolean"
4465
},
@@ -297,25 +318,6 @@
297318
"setup": {
298319
"instanceof": "Function"
299320
},
300-
"sockHost": {
301-
"type": "string"
302-
},
303-
"sockPath": {
304-
"type": "string"
305-
},
306-
"sockPort": {
307-
"anyOf": [
308-
{
309-
"type": "number"
310-
},
311-
{
312-
"type": "string"
313-
},
314-
{
315-
"type": "null"
316-
}
317-
]
318-
},
319321
"socket": {
320322
"type": "string"
321323
},
@@ -403,6 +405,7 @@
403405
"ca": "should be {String|Buffer}",
404406
"cert": "should be {String|Buffer}",
405407
"clientLogLevel": "should be {String} and equal to one of the allowed values\n\n [ 'silent', 'info', 'debug', 'trace', 'error', 'warn' ]\n\n (https://webpack.js.org/configuration/dev-server/#devserverclientloglevel)",
408+
"clientSocketOptions": "should be {Object} (https://webpack.js.org/configuration/dev-server/#devserverclientsocketoptions)",
406409
"compress": "should be {Boolean} (https://webpack.js.org/configuration/dev-server/#devservercompress)",
407410
"contentBase": "should be {Number|String|Array} (https://webpack.js.org/configuration/dev-server/#devservercontentbase)",
408411
"disableHostCheck": "should be {Boolean} (https://webpack.js.org/configuration/dev-server/#devserverdisablehostcheck)",
@@ -446,9 +449,6 @@
446449
"serveIndex": "should be {Boolean} (https://webpack.js.org/configuration/dev-server/#devserverserveindex)",
447450
"serverSideRender": "should be {Boolean} (https://github.com/webpack/webpack-dev-middleware#serversiderender)",
448451
"setup": "should be {Function} (https://webpack.js.org/configuration/dev-server/#devserversetup)",
449-
"sockHost": "should be {String|Null} (https://webpack.js.org/configuration/dev-server/#devserversockhost)",
450-
"sockPath": "should be {String} (https://webpack.js.org/configuration/dev-server/#devserversockpath)",
451-
"sockPort": "should be {Number|String|Null} (https://webpack.js.org/configuration/dev-server/#devserversockport)",
452452
"socket": "should be {String} (https://webpack.js.org/configuration/dev-server/#devserversocket)",
453453
"staticOptions": "should be {Object} (https://webpack.js.org/configuration/dev-server/#devserverstaticoptions)",
454454
"stats": "should be {Object|Boolean} (https://webpack.js.org/configuration/dev-server/#devserverstats-)",

lib/utils/addEntries.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,18 @@ function addEntries(config, options, server) {
2727
},
2828
};
2929

30+
if (!options.clientSocketOptions) {
31+
options.clientSocketOptions = {};
32+
}
33+
3034
/** @type {string} */
3135
const domain = createDomain(options, app);
3236
/** @type {string} */
33-
const sockHost = options.sockHost ? `&sockHost=${options.sockHost}` : '';
37+
const sockHost = options.clientSocketOptions.host ? `&sockHost=${options.clientSocketOptions.host}` : '';
3438
/** @type {string} */
35-
const sockPath = options.sockPath ? `&sockPath=${options.sockPath}` : '';
39+
const sockPath = options.clientSocketOptions.path ? `&sockPath=${options.clientSocketOptions.path}` : '';
3640
/** @type {string} */
37-
const sockPort = options.sockPort ? `&sockPort=${options.sockPort}` : '';
41+
const sockPort = options.clientSocketOptions.port ? `&sockPort=${options.clientSocketOptions.port}` : '';
3842
/** @type {string} */
3943
const clientEntry = `${require.resolve(
4044
'../../client/'

lib/utils/createConfig.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,23 @@ function createConfig(config, argv, { port }) {
3131
options.socket = argv.socket;
3232
}
3333

34+
if (
35+
(argv.sockHost || argv.sockPath || argv.sockPort) &&
36+
!options.clientSocketOptions
37+
) {
38+
options.clientSocketOptions = {};
39+
}
40+
3441
if (argv.sockHost) {
35-
options.sockHost = argv.sockHost;
42+
options.clientSocketOptions.host = argv.sockHost;
3643
}
3744

3845
if (argv.sockPath) {
39-
options.sockPath = argv.sockPath;
46+
options.clientSocketOptions.path = argv.sockPath;
4047
}
4148

4249
if (argv.sockPort) {
43-
options.sockPort = argv.sockPort;
50+
options.clientSocketOptions.port = argv.sockPort;
4451
}
4552

4653
if (argv.liveReload === false) {

test/e2e/ClientOptions.test.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,9 @@ describe('Client complex inline script path', () => {
9696
poll: true,
9797
},
9898
public: 'myhost.test',
99-
sockPath: '/foo/test/bar/',
99+
clientSocketOptions: {
100+
path: '/foo/test/bar/',
101+
},
100102
quiet: true,
101103
};
102104
testServer.startAwaitingCompilation(config, options, done);
@@ -138,8 +140,10 @@ describe('Client complex inline script path with sockPort', () => {
138140
watchOptions: {
139141
poll: true,
140142
},
141-
sockPath: '/foo/test/bar/',
142-
sockPort: port3,
143+
clientSocketOptions: {
144+
path: '/foo/test/bar/',
145+
port: port3,
146+
},
143147
quiet: true,
144148
};
145149
testServer.startAwaitingCompilation(config, options, done);
@@ -185,7 +189,9 @@ describe('Client complex inline script path with sockPort, no sockPath', () => {
185189
watchOptions: {
186190
poll: true,
187191
},
188-
sockPort: port3,
192+
clientSocketOptions: {
193+
port: port3,
194+
},
189195
quiet: true,
190196
};
191197
testServer.startAwaitingCompilation(config, options, done);
@@ -225,7 +231,9 @@ describe('Client complex inline script path with sockHost', () => {
225231
watchOptions: {
226232
poll: true,
227233
},
228-
sockHost: 'myhost.test',
234+
clientSocketOptions: {
235+
host: 'myhost.test',
236+
},
229237
quiet: true,
230238
};
231239
testServer.startAwaitingCompilation(config, options, done);

test/options.test.js

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -290,16 +290,8 @@ describe('options', () => {
290290
success: [''],
291291
failure: [false],
292292
},
293-
sockHost: {
294-
success: [''],
295-
failure: [false],
296-
},
297-
sockPath: {
298-
success: [''],
299-
failure: [false],
300-
},
301-
sockPort: {
302-
success: ['', 0, null],
293+
clientSocketOptions: {
294+
success: [{}],
303295
failure: [false],
304296
},
305297
staticOptions: {

test/server/sockPath-option.test.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ describe('sockPath options', () => {
3737
server = testServer.start(
3838
config,
3939
{
40-
sockPath: '/foo/test/bar/',
40+
clientSocketOptions: {
41+
path: '/foo/test/bar/',
42+
},
4143
port,
4244
},
4345
done

test/server/transportMode-option.test.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,9 @@ describe('transportMode', () => {
195195
config,
196196
{
197197
port,
198-
sockPath: '/foo/test/bar/',
198+
clientSocketOptions: {
199+
path: '/foo/test/bar/',
200+
},
199201
transportMode: {
200202
server: class MySockJSServer extends BaseServer {
201203
constructor(serv) {
@@ -217,7 +219,7 @@ describe('transportMode', () => {
217219
prefix: this.server.sockPath,
218220
});
219221

220-
sockPath = server.options.sockPath;
222+
sockPath = server.options.clientSocketOptions.path;
221223
}
222224

223225
send(connection, message) {

test/server/utils/__snapshots__/createConfig.test.js.snap

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1202,12 +1202,14 @@ Object {
12021202

12031203
exports[`createConfig sockHost option 1`] = `
12041204
Object {
1205+
"clientSocketOptions": Object {
1206+
"host": true,
1207+
},
12051208
"hot": true,
12061209
"hotOnly": false,
12071210
"noInfo": true,
12081211
"port": 8080,
12091212
"publicPath": "/",
1210-
"sockHost": true,
12111213
"stats": Object {
12121214
"cached": false,
12131215
"cachedAssets": false,
@@ -1217,12 +1219,14 @@ Object {
12171219

12181220
exports[`createConfig sockPath option 1`] = `
12191221
Object {
1222+
"clientSocketOptions": Object {
1223+
"path": "path",
1224+
},
12201225
"hot": true,
12211226
"hotOnly": false,
12221227
"noInfo": true,
12231228
"port": 8080,
12241229
"publicPath": "/",
1225-
"sockPath": "path",
12261230
"stats": Object {
12271231
"cached": false,
12281232
"cachedAssets": false,
@@ -1232,12 +1236,14 @@ Object {
12321236

12331237
exports[`createConfig sockPort option 1`] = `
12341238
Object {
1239+
"clientSocketOptions": Object {
1240+
"port": "port",
1241+
},
12351242
"hot": true,
12361243
"hotOnly": false,
12371244
"noInfo": true,
12381245
"port": 8080,
12391246
"publicPath": "/",
1240-
"sockPort": "port",
12411247
"stats": Object {
12421248
"cached": false,
12431249
"cachedAssets": false,

0 commit comments

Comments
 (0)