Skip to content
This repository was archived by the owner on Jul 13, 2021. It is now read-only.

Commit 48f0bc2

Browse files
authored
beta: https better server support, client-server port config (#85)
* feat: client-server port config, better server support * chore: beta * docs: add https note about firefox * test: fix tests
1 parent bc5fd38 commit 48f0bc2

8 files changed

Lines changed: 95 additions & 35 deletions

File tree

README.md

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,8 @@ Default: `'localhost'`
147147

148148
Sets the host that the `WebSocket` server will listen on. If this doesn't match
149149
the host of the server the module is used with, the module may not function
150-
properly. If the `server` option is defined, this option is ignored.
150+
properly. If the `server` option is defined, and the server has been instructed
151+
to listen, this option is ignored.
151152

152153
If using the module in a specialized environment, you may choose to specify an
153154
`object` to define `client` and `server` host separately. The `object` value
@@ -181,6 +182,10 @@ module won't function properly. The module will examine the `server` instance
181182
passed and if `server` _is an instance of `https.Server`, and `https` is not
182183
already set_, will set `https` to `true`.
183184

185+
_Note: When using a self-signed certificate on Firefox, you must add a "Server
186+
Exception" for `localhost:{port}` where `{port}` is either the `port` or the
187+
`port.server` option for secure `WebSocket` to work correctly._
188+
184189
##### logLevel
185190

186191
Type: `String`
@@ -198,12 +203,21 @@ If true, instructs the internal logger to prepend log output with a timestamp.
198203

199204
##### port
200205

201-
Type: `Number`
206+
Type: `Number|Object`
202207
Default: `0`
203208

204209
The port the `WebSocket` server should listen on. By default, the socket server
205210
will allocate a port. If a different port is chosen, the consumer of the module
206-
must ensure that the port is free before hand.
211+
must ensure that the port is free before hand. If the `server` option is defined,
212+
and the server has been instructed to listen, this option is ignored.
213+
214+
If using the module in a specialized environment, you may choose to specify an
215+
`object` to define `client` and `server` port separately. The `object` value
216+
should match `{ client: <Number>, server: <Number> }`. Be aware that the `client`
217+
port will be used _in the browser_ by `WebSockets`. You should not use this
218+
option in this way unless _you know what you're doing._ Using a mismatched
219+
`client` and `server` port will be **unsupported by the project** as the behavior
220+
in the browser can be unpredictable and is specific to a particular environment.
207221

208222
##### reload
209223

lib/options.js

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ module.exports = (opts = {}) => {
4646

4747
if (typeof options.host === 'string') {
4848
options.host = {
49-
server: options.host,
5049
client: options.host,
50+
server: options.host,
5151
};
5252
} else if (!options.host.server) {
5353
throw new HotClientError(
@@ -59,23 +59,41 @@ module.exports = (opts = {}) => {
5959
);
6060
}
6161

62-
const { server } = options;
63-
64-
if (server && !server.listening) {
62+
if (typeof options.port === 'number') {
63+
options.port = {
64+
client: options.port,
65+
server: options.port,
66+
};
67+
} else if (!options.port.server) {
68+
throw new HotClientError(
69+
'`port.server` must be defined when setting host to an Object'
70+
);
71+
} else if (!options.port.client) {
6572
throw new HotClientError(
66-
'`options.server` must be a running/listening net.Server instance'
73+
'`port.client` must be defined when setting host to an Object'
6774
);
68-
} else if (server) {
75+
}
76+
77+
const { server } = options;
78+
79+
if (
80+
server &&
81+
server instanceof HttpsServer &&
82+
typeof options.https === 'undefined'
83+
) {
84+
options.https = true;
85+
}
86+
87+
if (server && server.listening) {
6988
options.webSocket = {
7089
host: server.address().address,
7190
port: server.address().port,
7291
};
73-
74-
if (server instanceof HttpsServer && typeof options.https === 'undefined') {
75-
options.https = true;
76-
}
7792
} else {
78-
options.webSocket = { host: options.host.client, port: options.port };
93+
options.webSocket = {
94+
host: options.host.client,
95+
port: options.port.client,
96+
};
7997
}
8098

8199
return options;

lib/socket-server.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,14 @@ const WebSocket = require('ws');
44

55
function getServer(options) {
66
const { host, log, port, server } = options;
7-
const wssOptions = server ? { server } : { host: host.server, port };
7+
const wssOptions = server
8+
? { server }
9+
: { host: host.server, port: port.server };
10+
11+
if (server && !server.listening) {
12+
server.listen(port.server, host.server);
13+
}
14+
815
const wss = new WebSocket.Server(wssOptions);
916

1017
onConnection(wss, options);
@@ -87,7 +94,7 @@ function onListening(server, options) {
8794
/* eslint-disable no-underscore-dangle, no-param-reassign */
8895
const { host, log } = options;
8996

90-
if (options.server) {
97+
if (options.server && options.server.listening) {
9198
const addr = options.server.address();
9299
server.host = addr.address;
93100
server.port = addr.port;

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
{
22
"name": "webpack-hot-client",
3-
"version": "4.0.4",
3+
"version": "4.1.0-beta.1",
4+
"publishConfig": {
5+
"tag": "beta"
6+
},
47
"description": "A client for enabling, and interacting with, webpack Hot Module Replacement",
58
"license": "MIT",
69
"repository": "webpack-contrib/webpack-hot-client",

schemas/options.json

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,22 @@
4141
"type": "boolean"
4242
},
4343
"port": {
44-
"type": "integer"
44+
"anyOf": [
45+
{
46+
"type": "integer"
47+
},
48+
{
49+
"properties": {
50+
"client": {
51+
"type": "integer"
52+
},
53+
"server": {
54+
"type": "integer"
55+
}
56+
},
57+
"type": "object"
58+
}
59+
]
4560
},
4661
"reload": {
4762
"type": "boolean"

test/__snapshots__/options.test.js.snap

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,10 @@ Object {
5151
},
5252
"logLevel": "trace",
5353
"logTime": true,
54-
"port": 0,
54+
"port": Object {
55+
"client": 0,
56+
"server": 0,
57+
},
5558
"reload": false,
5659
"send": Object {
5760
"errors": false,
@@ -124,7 +127,10 @@ Object {
124127
},
125128
"logLevel": "info",
126129
"logTime": false,
127-
"port": 0,
130+
"port": Object {
131+
"client": 0,
132+
"server": 0,
133+
},
128134
"reload": true,
129135
"send": Object {
130136
"errors": true,
@@ -196,7 +202,10 @@ Object {
196202
},
197203
"logLevel": "info",
198204
"logTime": false,
199-
"port": 0,
205+
"port": Object {
206+
"client": 0,
207+
"server": 0,
208+
},
200209
"reload": true,
201210
"send": Object {
202211
"errors": true,
@@ -268,7 +277,10 @@ Object {
268277
},
269278
"logLevel": "info",
270279
"logTime": false,
271-
"port": 0,
280+
"port": Object {
281+
"client": 0,
282+
"server": 0,
283+
},
272284
"reload": true,
273285
"send": Object {
274286
"errors": true,

test/fixtures/app.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,7 @@ console.log('dirty');
1717
console.log('dirty');
1818

1919
console.log('dirty');
20+
21+
console.log('dirty');
22+
console.log('dirty');
23+
console.log('dirty');

test/options.test.js

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
const { readFileSync: read } = require('fs');
22
const { resolve } = require('path');
3-
const { createServer } = require('http');
43
const https = require('https');
54

65
const killable = require('killable');
@@ -102,16 +101,4 @@ describe('options', () => {
102101
const t = () => getOptions({ host: { client: 'localhost' } });
103102
expect(t).toThrow();
104103
});
105-
106-
test('reject non-http.Server server', () => {
107-
const server = {};
108-
const t = () => getOptions({ server });
109-
expect(t).toThrow();
110-
});
111-
112-
test('reject non-running server', () => {
113-
const server = createServer();
114-
const t = () => getOptions({ server });
115-
expect(t).toThrow();
116-
});
117104
});

0 commit comments

Comments
 (0)