Skip to content

Commit 7af0f8d

Browse files
refactor: simplify client (#3430)
1 parent eaed4a8 commit 7af0f8d

12 files changed

+19
-44
lines changed

client-src/clients/BaseClient.js

Lines changed: 0 additions & 8 deletions
This file was deleted.

client-src/clients/SockJSClient.js

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,9 @@
22

33
const SockJS = require('../modules/sockjs-client');
44
const { log } = require('../utils/log');
5-
const BaseClient = require('./BaseClient');
65

7-
module.exports = class SockJSClient extends BaseClient {
6+
module.exports = class SockJSClient {
87
constructor(url) {
9-
super();
10-
118
// SockJS requires `http` and `https` protocols
129
this.sock = new SockJS(
1310
url.replace(/^ws:/i, 'http:').replace(/^wss:/i, 'https:')
@@ -17,11 +14,6 @@ module.exports = class SockJSClient extends BaseClient {
1714
};
1815
}
1916

20-
// eslint-disable-next-line no-unused-vars
21-
static getClientPath(options) {
22-
return require.resolve('./SockJSClient');
23-
}
24-
2517
onOpen(f) {
2618
this.sock.onopen = f;
2719
}

client-src/clients/WebsocketClient.js

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,15 @@
11
'use strict';
22

33
const { log } = require('../utils/log');
4-
const BaseClient = require('./BaseClient');
54

6-
module.exports = class WebsocketClient extends BaseClient {
5+
module.exports = class WebsocketClient {
76
constructor(url) {
8-
super();
9-
107
this.client = new WebSocket(url);
118
this.client.onerror = (error) => {
129
log.error(error);
1310
};
1411
}
1512

16-
// eslint-disable-next-line no-unused-vars
17-
static getClientPath(options) {
18-
return require.resolve('./WebsocketClient');
19-
}
20-
2113
onOpen(f) {
2214
this.client.onopen = f;
2315
}

lib/utils/getSocketClientPath.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,17 @@ function getSocketClientPath(options) {
2121
case 'string':
2222
// could be 'sockjs', 'ws', or a path that should be required
2323
if (clientTransport === 'sockjs') {
24-
ClientImplementation = require('../../client/clients/SockJSClient');
24+
ClientImplementation = require.resolve(
25+
'../../client/clients/SockJSClient'
26+
);
2527
} else if (clientTransport === 'ws') {
26-
ClientImplementation = require('../../client/clients/WebsocketClient');
28+
ClientImplementation = require.resolve(
29+
'../../client/clients/WebsocketClient'
30+
);
2731
} else {
2832
try {
2933
// eslint-disable-next-line import/no-dynamic-require
30-
ClientImplementation = require(clientTransport);
34+
ClientImplementation = require.resolve(clientTransport);
3135
} catch (e) {
3236
clientImplementationFound = false;
3337
}
@@ -43,11 +47,11 @@ function getSocketClientPath(options) {
4347
!isKnownWebSocketServerImplementation
4448
? 'When you use custom web socket implementation you must explicitly specify client.transport. '
4549
: ''
46-
}client.transport must be a string denoting a default implementation (e.g. 'sockjs', 'ws') or a full path to a JS file which exports a class extending BaseClient (webpack-dev-server/client-src/clients/BaseClient.js) via require.resolve(...)`
50+
}client.transport must be a string denoting a default implementation (e.g. 'sockjs', 'ws') or a full path to a JS file via require.resolve(...) which exports a class `
4751
);
4852
}
4953

50-
return ClientImplementation.getClientPath(options);
54+
return ClientImplementation;
5155
}
5256

5357
module.exports = getSocketClientPath;

test/__snapshots__/validate-options.test.js.snap.webpack4

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -613,7 +613,7 @@ exports[`options validate should throw an error on the "webSocketServer" option
613613
object { type?, options? }"
614614
`;
615615

616-
exports[`options validate should throw an error on the "webSocketServer" option with 'nonexistent-implementation' value 1`] = `"Error: When you use custom web socket implementation you must explicitly specify client.transport. client.transport must be a string denoting a default implementation (e.g. 'sockjs', 'ws') or a full path to a JS file which exports a class extending BaseClient (webpack-dev-server/client-src/clients/BaseClient.js) via require.resolve(...)"`;
616+
exports[`options validate should throw an error on the "webSocketServer" option with 'nonexistent-implementation' value 1`] = `"Error: When you use custom web socket implementation you must explicitly specify client.transport. client.transport must be a string denoting a default implementation (e.g. 'sockjs', 'ws') or a full path to a JS file via require.resolve(...) which exports a class "`;
617617

618618
exports[`options validate should throw an error on the "webSocketServer" option with 'null' value 1`] = `
619619
"ValidationError: Invalid options object. Dev Server has been initialized using an options object that does not match the API schema.

test/__snapshots__/validate-options.test.js.snap.webpack5

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -613,7 +613,7 @@ exports[`options validate should throw an error on the "webSocketServer" option
613613
object { type?, options? }"
614614
`;
615615

616-
exports[`options validate should throw an error on the "webSocketServer" option with 'nonexistent-implementation' value 1`] = `"Error: When you use custom web socket implementation you must explicitly specify client.transport. client.transport must be a string denoting a default implementation (e.g. 'sockjs', 'ws') or a full path to a JS file which exports a class extending BaseClient (webpack-dev-server/client-src/clients/BaseClient.js) via require.resolve(...)"`;
616+
exports[`options validate should throw an error on the "webSocketServer" option with 'nonexistent-implementation' value 1`] = `"Error: When you use custom web socket implementation you must explicitly specify client.transport. client.transport must be a string denoting a default implementation (e.g. 'sockjs', 'ws') or a full path to a JS file via require.resolve(...) which exports a class "`;
617617

618618
exports[`options validate should throw an error on the "webSocketServer" option with 'null' value 1`] = `
619619
"ValidationError: Invalid options object. Dev Server has been initialized using an options object that does not match the API schema.

test/client/socket-helper.test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ describe('socket', () => {
1212
const WebsocketClient = require('../../client/clients/WebsocketClient');
1313

1414
const mockHandler = jest.fn();
15+
1516
socket('my.url', {
1617
example: mockHandler,
1718
});
File renamed without changes.

test/fixtures/custom-client/CustomSockJSClient.js

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,14 @@
44
no-unused-vars
55
*/
66
const SockJS = require('sockjs-client/dist/sockjs');
7-
const BaseClient = require('../../../client/clients/BaseClient');
87

9-
module.exports = class SockJSClient extends BaseClient {
8+
module.exports = class SockJSClient {
109
constructor(url) {
11-
super();
12-
1310
this.sock = new SockJS(
1411
url.replace(/^ws:/i, 'http://').replace(/^wss:/i, 'https://')
1512
);
1613
}
1714

18-
static getClientPath(options) {
19-
return require.resolve('./CustomSockJSClient');
20-
}
21-
2215
onOpen(f) {
2316
this.sock.onopen = () => {
2417
console.log('open');

test/server/utils/getSocketClientPath.test.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ const getSocketClientPath = require('../../../lib/utils/getSocketClientPath');
55
const sockjsClientPath = require.resolve(
66
'../../../client/clients/SockJSClient'
77
);
8-
const baseClientPath = require.resolve('../../../client/clients/BaseClient');
98

109
describe('getSocketClientPath', () => {
1110
it("should work with client.transport: 'sockjs'", () => {
@@ -64,10 +63,12 @@ describe('getSocketClientPath', () => {
6463
expect(() => {
6564
getSocketClientPath({
6665
client: {
67-
transport: baseClientPath,
66+
transport: 'foo',
6867
},
6968
webSocketServer: 'sockjs',
7069
});
71-
}).toThrow('Client needs implementation');
70+
}).toThrow(
71+
'When you use custom web socket implementation you must explicitly specify client.transport'
72+
);
7273
});
7374
});

0 commit comments

Comments
 (0)