Skip to content

Commit 83d4ec3

Browse files
authored
feat: switch default transportMode to ws (#2531)
BREAKING CHANGE: switch default transportMode to ws
1 parent 308ee21 commit 83d4ec3

22 files changed

+252
-181
lines changed

client-src/default/socket.js

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,13 @@
55
camelcase
66
*/
77

8-
// this SockJSClient is here as a default fallback, in case inline mode
9-
// is off or the client is not injected. This will be switched to
10-
// WebsocketClient when it becomes the default
11-
12-
// important: the path to SockJSClient here is made to work in the 'client'
13-
// directory, but is updated via the webpack compilation when compiled from
14-
// the 'client-src' directory
8+
// this WebsocketClient is here as a default fallback,
9+
// in case the client is not injected
1510
const Client =
1611
typeof __webpack_dev_server_client__ !== 'undefined'
1712
? __webpack_dev_server_client__
1813
: // eslint-disable-next-line import/no-unresolved
19-
require('./clients/SockJSClient');
14+
require('./clients/WebsocketClient');
2015

2116
let retries = 0;
2217
let client = null;

client-src/default/utils/createSocketUrl.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ function getSocketUrl(urlParts, loc) {
7575
// resourceQuery, so we need to fall back to the default if
7676
// they are not provided
7777
const sockHost = query.sockHost || hostname;
78-
const sockPath = query.sockPath || '/sockjs-node';
78+
const sockPath = query.sockPath || '/ws';
7979
let sockPort = query.sockPort || port;
8080

8181
if (sockPort === 'location') {

client-src/default/webpack.config.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ module.exports = {
1919
},
2020
plugins: [
2121
new webpack.NormalModuleReplacementPlugin(
22-
/^\.\/clients\/SockJSClient$/,
22+
/^\.\/clients\/WebsocketClient$/,
2323
(resource) => {
2424
if (resource.context.startsWith(process.cwd())) {
2525
resource.request = resource.request.replace(
26-
/^\.\/clients\/SockJSClient$/,
27-
'../clients/SockJSClient'
26+
/^\.\/clients\/WebsocketClient$/,
27+
'../clients/WebsocketClient'
2828
);
2929
}
3030
}

examples/cli/public-protocol/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@ You're now able to explicitly define the protocol used with the `public` option
1313

1414
The script should open `http://localhost:8080/` in your default browser.
1515

16-
You should see a failed attempt to establish a connection to `/sockjs-node`
16+
You should see a failed attempt to establish a connection to `/ws`
1717
via the explicitly defined `https://localhost:8080`. This fails of course since
1818
we're not hosting https.

lib/Server.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ class Server {
9595
this.sockPath = `/${
9696
this.options.sockPath
9797
? this.options.sockPath.replace(/^\/|\/$/g, '')
98-
: 'sockjs-node'
98+
: 'ws'
9999
}`;
100100

101101
if (this.progress) {

lib/utils/getSocketServerImplementation.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ function getSocketServerImplementation(options) {
3030

3131
if (!serverImplFound) {
3232
throw new Error(
33-
"transportMode.server must be a string denoting a default implementation (e.g. 'sockjs', 'ws'), a full path to " +
33+
"transportMode.server must be a string denoting a default implementation (e.g. 'ws', 'sockjs'), a full path to " +
3434
'a JS file which exports a class extending BaseServer (webpack-dev-server/lib/servers/BaseServer) ' +
3535
'via require.resolve(...), or the class itself which extends BaseServer'
3636
);

lib/utils/normalizeOptions.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ function normalizeOptions(compiler, options) {
1515
// normalize transportMode option
1616
if (options.transportMode === undefined) {
1717
options.transportMode = {
18-
server: 'sockjs',
19-
client: 'sockjs',
18+
server: 'ws',
19+
client: 'ws',
2020
};
2121
} else {
2222
switch (typeof options.transportMode) {
@@ -28,8 +28,8 @@ function normalizeOptions(compiler, options) {
2828
break;
2929
// if not a string, it is an object
3030
default:
31-
options.transportMode.server = options.transportMode.server || 'sockjs';
32-
options.transportMode.client = options.transportMode.client || 'sockjs';
31+
options.transportMode.server = options.transportMode.server || 'ws';
32+
options.transportMode.client = options.transportMode.client || 'ws';
3333
}
3434
}
3535

test/client/__snapshots__/socket-helper.test.js.snap

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,36 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`socket should default to SockJSClient when no __webpack_dev_server_client__ set 1`] = `
3+
exports[`socket should default to WebsocketClient when no __webpack_dev_server_client__ set 1`] = `
44
Array [
55
"my.url",
66
]
77
`;
88

9-
exports[`socket should default to SockJSClient when no __webpack_dev_server_client__ set 2`] = `
9+
exports[`socket should default to WebsocketClient when no __webpack_dev_server_client__ set 2`] = `
1010
Array [
1111
Array [
1212
[Function],
1313
],
1414
]
1515
`;
1616

17-
exports[`socket should default to SockJSClient when no __webpack_dev_server_client__ set 3`] = `
17+
exports[`socket should default to WebsocketClient when no __webpack_dev_server_client__ set 3`] = `
1818
Array [
1919
Array [
2020
[Function],
2121
],
2222
]
2323
`;
2424

25-
exports[`socket should default to SockJSClient when no __webpack_dev_server_client__ set 4`] = `
25+
exports[`socket should default to WebsocketClient when no __webpack_dev_server_client__ set 4`] = `
2626
Array [
2727
Array [
2828
[Function],
2929
],
3030
]
3131
`;
3232

33-
exports[`socket should default to SockJSClient when no __webpack_dev_server_client__ set 5`] = `
33+
exports[`socket should default to WebsocketClient when no __webpack_dev_server_client__ set 5`] = `
3434
Array [
3535
Array [
3636
"hello world",

test/client/clients/SockJSClient.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ describe('SockJSClient', () => {
2121
listeningApp.listen(port, 'localhost', () => {
2222
socketServer = sockjs.createServer();
2323
socketServer.installHandlers(listeningApp, {
24-
prefix: '/sockjs-node',
24+
prefix: '/ws',
2525
});
2626
done();
2727
});
@@ -41,7 +41,7 @@ describe('SockJSClient', () => {
4141
}, 1000);
4242
});
4343

44-
const client = new SockJSClient(`http://localhost:${port}/sockjs-node`);
44+
const client = new SockJSClient(`http://localhost:${port}/ws`);
4545
const data = [];
4646

4747
client.onOpen(() => {

test/client/socket-helper.test.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,17 @@ describe('socket', () => {
66
jest.resetModules();
77
});
88

9-
it('should default to SockJSClient when no __webpack_dev_server_client__ set', () => {
10-
jest.mock('../../client/clients/SockJSClient');
9+
it('should default to WebsocketClient when no __webpack_dev_server_client__ set', () => {
10+
jest.mock('../../client/clients/WebsocketClient');
1111
const socket = require('../../client/socket');
12-
const SockJSClient = require('../../client/clients/SockJSClient');
12+
const WebsocketClient = require('../../client/clients/WebsocketClient');
1313

1414
const mockHandler = jest.fn();
1515
socket('my.url', {
1616
example: mockHandler,
1717
});
1818

19-
const mockClientInstance = SockJSClient.mock.instances[0];
19+
const mockClientInstance = WebsocketClient.mock.instances[0];
2020

2121
// this simulates receiving a message from the server and passing it
2222
// along to the callback of onMessage
@@ -27,17 +27,17 @@ describe('socket', () => {
2727
})
2828
);
2929

30-
expect(SockJSClient.mock.calls[0]).toMatchSnapshot();
30+
expect(WebsocketClient.mock.calls[0]).toMatchSnapshot();
3131
expect(mockClientInstance.onOpen.mock.calls).toMatchSnapshot();
3232
expect(mockClientInstance.onClose.mock.calls).toMatchSnapshot();
3333
expect(mockClientInstance.onMessage.mock.calls).toMatchSnapshot();
3434
expect(mockHandler.mock.calls).toMatchSnapshot();
3535
});
3636

3737
it('should use __webpack_dev_server_client__ when set', () => {
38-
jest.mock('../../client/clients/SockJSClient');
38+
jest.mock('../../client/clients/WebsocketClient');
3939
const socket = require('../../client/socket');
40-
global.__webpack_dev_server_client__ = require('../../client/clients/SockJSClient');
40+
global.__webpack_dev_server_client__ = require('../../client/clients/WebsocketClient');
4141

4242
const mockHandler = jest.fn();
4343
socket('my.url', {
Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,41 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`createSocketUrl should return the url when __resourceQuery is ?test 1`] = `"/sockjs-node"`;
3+
exports[`createSocketUrl should return the url when __resourceQuery is ?test 1`] = `"/ws"`;
44

5-
exports[`createSocketUrl should return the url when __resourceQuery is http://0.0.0.0 1`] = `"http://localhost/sockjs-node"`;
5+
exports[`createSocketUrl should return the url when __resourceQuery is http://0.0.0.0 1`] = `"http://localhost/ws"`;
66

7-
exports[`createSocketUrl should return the url when __resourceQuery is http://127.0.0.1 1`] = `"http://127.0.0.1/sockjs-node"`;
7+
exports[`createSocketUrl should return the url when __resourceQuery is http://127.0.0.1 1`] = `"http://127.0.0.1/ws"`;
88

9-
exports[`createSocketUrl should return the url when __resourceQuery is http://user:pass@[::]:8080 1`] = `"http://user:pass@localhost:8080/sockjs-node"`;
9+
exports[`createSocketUrl should return the url when __resourceQuery is http://user:pass@[::]:8080 1`] = `"http://user:pass@localhost:8080/ws"`;
1010

11-
exports[`createSocketUrl should return the url when __resourceQuery is http://user:password@localhost/ 1`] = `"http://user:password@localhost/sockjs-node"`;
11+
exports[`createSocketUrl should return the url when __resourceQuery is http://user:password@localhost/ 1`] = `"http://user:password@localhost/ws"`;
1212

13-
exports[`createSocketUrl should return the url when __resourceQuery is https://example.com 1`] = `"https://example.com/sockjs-node"`;
13+
exports[`createSocketUrl should return the url when __resourceQuery is https://example.com 1`] = `"https://example.com/ws"`;
1414

15-
exports[`createSocketUrl should return the url when __resourceQuery is https://example.com/path 1`] = `"https://example.com/sockjs-node"`;
15+
exports[`createSocketUrl should return the url when __resourceQuery is https://example.com/path 1`] = `"https://example.com/ws"`;
1616

17-
exports[`createSocketUrl should return the url when __resourceQuery is https://example.com/path/foo.js 1`] = `"https://example.com/sockjs-node"`;
17+
exports[`createSocketUrl should return the url when __resourceQuery is https://example.com/path/foo.js 1`] = `"https://example.com/ws"`;
1818

19-
exports[`createSocketUrl should return the url when __resourceQuery is https://localhost:123 1`] = `"https://localhost:123/sockjs-node"`;
19+
exports[`createSocketUrl should return the url when __resourceQuery is https://localhost:123 1`] = `"https://localhost:123/ws"`;
2020

21-
exports[`createSocketUrl should return the url when __resourceQuery is undefined 1`] = `"/sockjs-node"`;
21+
exports[`createSocketUrl should return the url when __resourceQuery is undefined 1`] = `"/ws"`;
2222

23-
exports[`createSocketUrl should return the url when the current script source is ?test 1`] = `"/sockjs-node"`;
23+
exports[`createSocketUrl should return the url when the current script source is ?test 1`] = `"/ws"`;
2424

25-
exports[`createSocketUrl should return the url when the current script source is http://0.0.0.0 1`] = `"http://localhost/sockjs-node"`;
25+
exports[`createSocketUrl should return the url when the current script source is http://0.0.0.0 1`] = `"http://localhost/ws"`;
2626

27-
exports[`createSocketUrl should return the url when the current script source is http://127.0.0.1 1`] = `"http://127.0.0.1/sockjs-node"`;
27+
exports[`createSocketUrl should return the url when the current script source is http://127.0.0.1 1`] = `"http://127.0.0.1/ws"`;
2828

29-
exports[`createSocketUrl should return the url when the current script source is http://user:pass@[::]:8080 1`] = `"http://user:pass@localhost:8080/sockjs-node"`;
29+
exports[`createSocketUrl should return the url when the current script source is http://user:pass@[::]:8080 1`] = `"http://user:pass@localhost:8080/ws"`;
3030

31-
exports[`createSocketUrl should return the url when the current script source is http://user:password@localhost/ 1`] = `"http://user:password@localhost/sockjs-node"`;
31+
exports[`createSocketUrl should return the url when the current script source is http://user:password@localhost/ 1`] = `"http://user:password@localhost/ws"`;
3232

33-
exports[`createSocketUrl should return the url when the current script source is https://example.com 1`] = `"https://example.com/sockjs-node"`;
33+
exports[`createSocketUrl should return the url when the current script source is https://example.com 1`] = `"https://example.com/ws"`;
3434

35-
exports[`createSocketUrl should return the url when the current script source is https://example.com/path 1`] = `"https://example.com/sockjs-node"`;
35+
exports[`createSocketUrl should return the url when the current script source is https://example.com/path 1`] = `"https://example.com/ws"`;
3636

37-
exports[`createSocketUrl should return the url when the current script source is https://example.com/path/foo.js 1`] = `"https://example.com/sockjs-node"`;
37+
exports[`createSocketUrl should return the url when the current script source is https://example.com/path/foo.js 1`] = `"https://example.com/ws"`;
3838

39-
exports[`createSocketUrl should return the url when the current script source is https://localhost:123 1`] = `"https://localhost:123/sockjs-node"`;
39+
exports[`createSocketUrl should return the url when the current script source is https://localhost:123 1`] = `"https://localhost:123/ws"`;
4040

41-
exports[`createSocketUrl should return the url when the current script source is undefined 1`] = `"/sockjs-node"`;
41+
exports[`createSocketUrl should return the url when the current script source is undefined 1`] = `"/ws"`;

test/client/utils/createSocketUrl.test.js

Lines changed: 17 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -41,36 +41,12 @@ describe('createSocketUrl', () => {
4141

4242
const samples2 = [
4343
// script source, location, output socket URL
44-
[
45-
'http://example.com',
46-
'https://something.com',
47-
'https://example.com/sockjs-node',
48-
],
49-
[
50-
'http://127.0.0.1',
51-
'https://something.com',
52-
'http://127.0.0.1/sockjs-node',
53-
],
54-
[
55-
'http://0.0.0.0',
56-
'https://something.com',
57-
'https://something.com/sockjs-node',
58-
],
59-
[
60-
'http://0.0.0.0',
61-
'http://something.com',
62-
'http://something.com/sockjs-node',
63-
],
64-
[
65-
'http://example.com',
66-
'http://something.com',
67-
'http://example.com/sockjs-node',
68-
],
69-
[
70-
'https://example.com',
71-
'http://something.com',
72-
'https://example.com/sockjs-node',
73-
],
44+
['http://example.com', 'https://something.com', 'https://example.com/ws'],
45+
['http://127.0.0.1', 'https://something.com', 'http://127.0.0.1/ws'],
46+
['http://0.0.0.0', 'https://something.com', 'https://something.com/ws'],
47+
['http://0.0.0.0', 'http://something.com', 'http://something.com/ws'],
48+
['http://example.com', 'http://something.com', 'http://example.com/ws'],
49+
['https://example.com', 'http://something.com', 'https://example.com/ws'],
7450
];
7551

7652
samples2.forEach(([scriptSrc, loc, expected]) => {
@@ -91,45 +67,21 @@ describe('createSocketUrl', () => {
9167

9268
const samples3 = [
9369
// script source, location, output socket URL
94-
[
95-
'?http://example.com',
96-
'https://something.com',
97-
'https://example.com/sockjs-node',
98-
],
99-
[
100-
'?http://127.0.0.1',
101-
'https://something.com',
102-
'http://127.0.0.1/sockjs-node',
103-
],
104-
[
105-
'?http://0.0.0.0',
106-
'https://something.com',
107-
'https://something.com/sockjs-node',
108-
],
109-
[
110-
'?http://0.0.0.0',
111-
'http://something.com',
112-
'http://something.com/sockjs-node',
113-
],
114-
[
115-
'?http://example.com',
116-
'http://something.com',
117-
'http://example.com/sockjs-node',
118-
],
119-
[
120-
'?https://example.com',
121-
'http://something.com',
122-
'https://example.com/sockjs-node',
123-
],
70+
['?http://example.com', 'https://something.com', 'https://example.com/ws'],
71+
['?http://127.0.0.1', 'https://something.com', 'http://127.0.0.1/ws'],
72+
['?http://0.0.0.0', 'https://something.com', 'https://something.com/ws'],
73+
['?http://0.0.0.0', 'http://something.com', 'http://something.com/ws'],
74+
['?http://example.com', 'http://something.com', 'http://example.com/ws'],
75+
['?https://example.com', 'http://something.com', 'https://example.com/ws'],
12476
[
12577
'?https://example.com?sockHost=asdf',
12678
'http://something.com',
127-
'https://asdf/sockjs-node',
79+
'https://asdf/ws',
12880
],
12981
[
13082
'?https://example.com?sockPort=34',
13183
'http://something.com',
132-
'https://example.com:34/sockjs-node',
84+
'https://example.com:34/ws',
13385
],
13486
[
13587
'?https://example.com?sockPath=xxx',
@@ -139,17 +91,17 @@ describe('createSocketUrl', () => {
13991
[
14092
'?http://0.0.0.0:8096&sockPort=8097',
14193
'http://localhost',
142-
'http://localhost:8097/sockjs-node',
94+
'http://localhost:8097/ws',
14395
],
14496
[
14597
'?http://example.com:8096&sockPort=location',
14698
'http://something.com',
147-
'http://example.com/sockjs-node',
99+
'http://example.com/ws',
148100
],
149101
[
150102
'?http://0.0.0.0:8096&sockPort=location',
151103
'http://localhost:3000',
152-
'http://localhost:3000/sockjs-node',
104+
'http://localhost:3000/ws',
153105
],
154106
];
155107
samples3.forEach(([scriptSrc, loc, expected]) => {

test/e2e/Client.test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@ const cssFilePath = resolve(__dirname, '../fixtures/reload-config/main.css');
2323
describe('reload', () => {
2424
const modes = [
2525
{
26-
title: 'hot with default transportMode.client (sockjs)',
26+
title: 'hot with default transportMode.client (ws)',
2727
shouldRefresh: false,
2828
},
2929
{
30-
title: 'hot with transportMode.client ws',
30+
title: 'hot with transportMode.client sockjs',
3131
options: {
32-
transportMode: 'ws',
32+
transportMode: 'sockjs',
3333
},
3434
shouldRefresh: false,
3535
},

0 commit comments

Comments
 (0)