Skip to content

Commit 84daa27

Browse files
committed
refactor(server): move transport mode default setting into helper
1 parent d5f1dfc commit 84daa27

File tree

4 files changed

+153
-37
lines changed

4 files changed

+153
-37
lines changed

lib/Server.js

+1-21
Original file line numberDiff line numberDiff line change
@@ -61,27 +61,7 @@ class Server {
6161

6262
this.log = _log || createLogger(options);
6363

64-
if (this.options.transportMode === undefined) {
65-
this.options.transportMode = {
66-
server: 'sockjs',
67-
client: 'sockjs',
68-
};
69-
} else {
70-
switch (typeof this.options.transportMode) {
71-
case 'string':
72-
this.options.transportMode = {
73-
server: this.options.transportMode,
74-
client: this.options.transportMode,
75-
};
76-
break;
77-
// if not a string, it is an object
78-
default:
79-
this.options.transportMode.server =
80-
this.options.transportMode.server || 'sockjs';
81-
this.options.transportMode.client =
82-
this.options.transportMode.client || 'sockjs';
83-
}
84-
64+
if (this.options.transportMode !== undefined) {
8565
this.log.warn(
8666
'transportMode is an experimental option, meaning its usage could potentially change without warning'
8767
);

lib/utils/normalizeOptions.js

+19-8
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,25 @@ function normalizeOptions(compiler, options) {
99
options.contentBase =
1010
options.contentBase !== undefined ? options.contentBase : process.cwd();
1111

12-
// set serverMode default
13-
if (options.serverMode === undefined) {
14-
options.serverMode = 'sockjs';
15-
}
16-
17-
// set clientMode default
18-
if (options.clientMode === undefined) {
19-
options.clientMode = 'sockjs';
12+
// normalize transportMode option
13+
if (options.transportMode === undefined) {
14+
options.transportMode = {
15+
server: 'sockjs',
16+
client: 'sockjs',
17+
};
18+
} else {
19+
switch (typeof options.transportMode) {
20+
case 'string':
21+
options.transportMode = {
22+
server: options.transportMode,
23+
client: options.transportMode,
24+
};
25+
break;
26+
// if not a string, it is an object
27+
default:
28+
options.transportMode.server = options.transportMode.server || 'sockjs';
29+
options.transportMode.client = options.transportMode.client || 'sockjs';
30+
}
2031
}
2132

2233
if (!options.watchOptions) {

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

+76-8
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,105 @@
22

33
exports[`normalizeOptions contentBase array should set correct options 1`] = `
44
Object {
5-
"clientMode": "sockjs",
65
"contentBase": Array [
76
"/path/to/dist1",
87
"/path/to/dist2",
98
],
10-
"serverMode": "sockjs",
9+
"transportMode": Object {
10+
"client": "sockjs",
11+
"server": "sockjs",
12+
},
1113
"watchOptions": Object {},
1214
}
1315
`;
1416

1517
exports[`normalizeOptions contentBase string should set correct options 1`] = `
1618
Object {
17-
"clientMode": "sockjs",
1819
"contentBase": "/path/to/dist",
19-
"serverMode": "sockjs",
20+
"transportMode": Object {
21+
"client": "sockjs",
22+
"server": "sockjs",
23+
},
2024
"watchOptions": Object {},
2125
}
2226
`;
2327

2428
exports[`normalizeOptions no options should set correct options 1`] = `
2529
Object {
26-
"clientMode": "sockjs",
27-
"serverMode": "sockjs",
30+
"transportMode": Object {
31+
"client": "sockjs",
32+
"server": "sockjs",
33+
},
34+
"watchOptions": Object {},
35+
}
36+
`;
37+
38+
exports[`normalizeOptions transportMode custom client path should set correct options 1`] = `
39+
Object {
40+
"transportMode": Object {
41+
"client": "/path/to/custom/client/",
42+
"server": "sockjs",
43+
},
44+
"watchOptions": Object {},
45+
}
46+
`;
47+
48+
exports[`normalizeOptions transportMode custom server class should set correct options 1`] = `
49+
Object {
50+
"transportMode": Object {
51+
"client": "sockjs",
52+
"server": [Function],
53+
},
54+
"watchOptions": Object {},
55+
}
56+
`;
57+
58+
exports[`normalizeOptions transportMode custom server path should set correct options 1`] = `
59+
Object {
60+
"transportMode": Object {
61+
"client": "sockjs",
62+
"server": "/path/to/custom/server/",
63+
},
64+
"watchOptions": Object {},
65+
}
66+
`;
67+
68+
exports[`normalizeOptions transportMode sockjs string should set correct options 1`] = `
69+
Object {
70+
"transportMode": Object {
71+
"client": "sockjs",
72+
"server": "sockjs",
73+
},
74+
"watchOptions": Object {},
75+
}
76+
`;
77+
78+
exports[`normalizeOptions transportMode ws object should set correct options 1`] = `
79+
Object {
80+
"transportMode": Object {
81+
"client": "ws",
82+
"server": "ws",
83+
},
84+
"watchOptions": Object {},
85+
}
86+
`;
87+
88+
exports[`normalizeOptions transportMode ws string should set correct options 1`] = `
89+
Object {
90+
"transportMode": Object {
91+
"client": "ws",
92+
"server": "ws",
93+
},
2894
"watchOptions": Object {},
2995
}
3096
`;
3197

3298
exports[`normalizeOptions watchOptions should set correct options 1`] = `
3399
Object {
34-
"clientMode": "sockjs",
35-
"serverMode": "sockjs",
100+
"transportMode": Object {
101+
"client": "sockjs",
102+
"server": "sockjs",
103+
},
36104
"watchOptions": Object {
37105
"poll": true,
38106
},

test/server/utils/normalizeOptions.test.js

+57
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,63 @@ describe('normalizeOptions', () => {
3737
},
3838
optionsResults: null,
3939
},
40+
{
41+
title: 'transportMode sockjs string',
42+
multiCompiler: false,
43+
options: {
44+
transportMode: 'sockjs',
45+
},
46+
optionsResults: null,
47+
},
48+
{
49+
title: 'transportMode ws string',
50+
multiCompiler: false,
51+
options: {
52+
transportMode: 'ws',
53+
},
54+
optionsResults: null,
55+
},
56+
{
57+
title: 'transportMode ws object',
58+
multiCompiler: false,
59+
options: {
60+
transportMode: {
61+
server: 'ws',
62+
client: 'ws',
63+
},
64+
},
65+
optionsResults: null,
66+
},
67+
{
68+
title: 'transportMode custom server path',
69+
multiCompiler: false,
70+
options: {
71+
transportMode: {
72+
server: '/path/to/custom/server/',
73+
},
74+
},
75+
optionsResults: null,
76+
},
77+
{
78+
title: 'transportMode custom server class',
79+
multiCompiler: false,
80+
options: {
81+
transportMode: {
82+
server: class CustomServerImplementation {},
83+
},
84+
},
85+
optionsResults: null,
86+
},
87+
{
88+
title: 'transportMode custom client path',
89+
multiCompiler: false,
90+
options: {
91+
transportMode: {
92+
client: '/path/to/custom/client/',
93+
},
94+
},
95+
optionsResults: null,
96+
},
4097
];
4198

4299
cases.forEach((data) => {

0 commit comments

Comments
 (0)