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', {

0 commit comments

Comments
 (0)