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

Commit e4240d4

Browse files
committed
fix: fixes #86, client port bungle
1 parent 538a90a commit e4240d4

File tree

7 files changed

+231
-5
lines changed

7 files changed

+231
-5
lines changed

lib/options.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,11 @@ module.exports = (opts = {}) => {
6464
client: options.port,
6565
server: options.port,
6666
};
67-
} else if (!options.port.server) {
67+
} else if (isNaN(parseInt(options.port.server, 10))) {
6868
throw new HotClientError(
6969
'`port.server` must be defined when setting host to an Object'
7070
);
71-
} else if (!options.port.client) {
71+
} else if (isNaN(parseInt(options.port.client, 10))) {
7272
throw new HotClientError(
7373
'`port.client` must be defined when setting host to an Object'
7474
);
@@ -87,7 +87,8 @@ module.exports = (opts = {}) => {
8787
if (server && server.listening) {
8888
options.webSocket = {
8989
host: server.address().address,
90-
port: server.address().port,
90+
// a port.client value of 0 will be falsy, so it should pull the server port
91+
port: options.port.client || server.address().port,
9192
};
9293
} else {
9394
options.webSocket = {

lib/socket-server.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,8 @@ function onListening(server, options) {
104104
const { address, port } = server._server.address();
105105
server.host = address;
106106
server.port = port;
107-
options.webSocket.port = port;
107+
// a port.client value of 0 will be falsy, so it should pull the server port
108+
options.webSocket.port = options.port.client || port;
108109

109110
log.info(`WebSocket Server Listening on ${host.server}:${port}`);
110111
});

test/__snapshots__/index.test.js.snap

+165
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`api mismatched client/server options sanity check 1`] = `
4+
Object {
5+
"host": "0.0.0.0",
6+
"port": 7000,
7+
}
8+
`;
9+
10+
exports[`api mismatched client/server options sanity check 2`] = `
11+
Object {
12+
"allEntries": false,
13+
"autoConfigure": true,
14+
"hmr": true,
15+
"host": Object {
16+
"client": "localhost",
17+
"server": "0.0.0.0",
18+
},
19+
"https": undefined,
20+
"log": LogLevel {
21+
"currentLevel": 5,
22+
"debug": [Function],
23+
"error": [Function],
24+
"info": [Function],
25+
"log": [Function],
26+
"methodFactory": PrefixFactory {
27+
"options": Object {
28+
"level": [Function],
29+
"name": [Function],
30+
"template": "{{level}} 「{{name}}」: ",
31+
"time": [Function],
32+
},
33+
Symbol(a log instance): [Circular],
34+
Symbol(valid log levels): Object {
35+
"DEBUG": 1,
36+
"ERROR": 4,
37+
"INFO": 2,
38+
"SILENT": 5,
39+
"TRACE": 0,
40+
"WARN": 3,
41+
},
42+
},
43+
"name": "hot",
44+
"options": Object {
45+
"factory": null,
46+
"level": "silent",
47+
"name": "hot",
48+
"prefix": Object {
49+
"level": [Function],
50+
"template": "{{level}} 「{{name}}」: ",
51+
},
52+
"timestamp": false,
53+
"unique": true,
54+
},
55+
"trace": [Function],
56+
"type": "LogLevel",
57+
"warn": [Function],
58+
},
59+
"logLevel": "silent",
60+
"logTime": false,
61+
"port": Object {
62+
"client": 6000,
63+
"server": 7000,
64+
},
65+
"reload": true,
66+
"send": Object {
67+
"errors": true,
68+
"warnings": true,
69+
},
70+
"server": null,
71+
"stats": Object {
72+
"context": "<PROJECT_ROOT>",
73+
},
74+
"test": false,
75+
"validTargets": Array [
76+
"web",
77+
],
78+
"webSocket": Object {
79+
"host": "localhost",
80+
"port": 6000,
81+
},
82+
}
83+
`;
84+
85+
exports[`api options sanity check 1`] = `
86+
Object {
87+
"host": "127.0.0.1",
88+
"port": 56130,
89+
}
90+
`;
91+
92+
exports[`api options sanity check 2`] = `
93+
Object {
94+
"allEntries": false,
95+
"autoConfigure": true,
96+
"hmr": true,
97+
"host": Object {
98+
"client": "localhost",
99+
"server": "localhost",
100+
},
101+
"https": undefined,
102+
"log": LogLevel {
103+
"currentLevel": 5,
104+
"debug": [Function],
105+
"error": [Function],
106+
"info": [Function],
107+
"log": [Function],
108+
"methodFactory": PrefixFactory {
109+
"options": Object {
110+
"level": [Function],
111+
"name": [Function],
112+
"template": "{{level}} 「{{name}}」: ",
113+
"time": [Function],
114+
},
115+
Symbol(a log instance): [Circular],
116+
Symbol(valid log levels): Object {
117+
"DEBUG": 1,
118+
"ERROR": 4,
119+
"INFO": 2,
120+
"SILENT": 5,
121+
"TRACE": 0,
122+
"WARN": 3,
123+
},
124+
},
125+
"name": "hot",
126+
"options": Object {
127+
"factory": null,
128+
"level": "silent",
129+
"name": "hot",
130+
"prefix": Object {
131+
"level": [Function],
132+
"template": "{{level}} 「{{name}}」: ",
133+
},
134+
"timestamp": false,
135+
"unique": true,
136+
},
137+
"trace": [Function],
138+
"type": "LogLevel",
139+
"warn": [Function],
140+
},
141+
"logLevel": "silent",
142+
"logTime": false,
143+
"port": Object {
144+
"client": 0,
145+
"server": 0,
146+
},
147+
"reload": true,
148+
"send": Object {
149+
"errors": true,
150+
"warnings": true,
151+
},
152+
"server": null,
153+
"stats": Object {
154+
"context": "<PROJECT_ROOT>",
155+
},
156+
"test": false,
157+
"validTargets": Array [
158+
"web",
159+
],
160+
"webSocket": Object {
161+
"host": "localhost",
162+
"port": 56130,
163+
},
164+
}
165+
`;

test/__snapshots__/options.test.js.snap

+4
Original file line numberDiff line numberDiff line change
@@ -300,3 +300,7 @@ Object {
300300
},
301301
}
302302
`;
303+
304+
exports[`options throws if port.client is missing 1`] = `"webpack-hot-client: \`port.client\` must be defined when setting host to an Object"`;
305+
306+
exports[`options throws if port.server is missing 1`] = `"webpack-hot-client: \`port.server\` must be defined when setting host to an Object"`;

test/fixtures/app.js

+7
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,10 @@ console.log('dirty');
2121
console.log('dirty');
2222
console.log('dirty');
2323
console.log('dirty');
24+
25+
console.log('dirty');
26+
console.log('dirty');
27+
console.log('dirty');
28+
console.log('dirty');
29+
console.log('dirty');
30+
console.log('dirty');

test/index.test.js

+38
Original file line numberDiff line numberDiff line change
@@ -113,4 +113,42 @@ describe('api', () => {
113113
});
114114
});
115115
});
116+
117+
test('options sanity check', (done) => {
118+
const config = require('./fixtures/webpack.config-object.js');
119+
const compiler = webpack(config);
120+
const { options: opts, server } = client(compiler, options);
121+
122+
server.on('listening', () => {
123+
const { host, port } = server;
124+
expect({ host, port }).toMatchSnapshot();
125+
expect(opts).toMatchSnapshot();
126+
127+
setTimeout(() => server.close(done), 500);
128+
});
129+
});
130+
131+
test('mismatched client/server options sanity check', (done) => {
132+
const config = require('./fixtures/webpack.config-object.js');
133+
const compiler = webpack(config);
134+
const clientOptions = Object.assign({}, options, {
135+
host: {
136+
client: 'localhost',
137+
server: '0.0.0.0',
138+
},
139+
port: {
140+
client: 6000,
141+
server: 7000,
142+
},
143+
});
144+
const { options: opts, server } = client(compiler, clientOptions);
145+
146+
server.on('listening', () => {
147+
const { host, port } = server;
148+
expect({ host, port }).toMatchSnapshot();
149+
expect(opts).toMatchSnapshot();
150+
151+
setTimeout(() => server.close(done), 500);
152+
});
153+
});
116154
});

test/options.test.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ describe('options', () => {
8787
});
8888
});
8989

90-
test('throws on invalid options', () => {
90+
test('throws on invalid host option', () => {
9191
const t = () => getOptions({ host: true });
9292
expect(t).toThrow();
9393
});
@@ -101,4 +101,14 @@ describe('options', () => {
101101
const t = () => getOptions({ host: { client: 'localhost' } });
102102
expect(t).toThrow();
103103
});
104+
105+
test('throws if port.client is missing', () => {
106+
const t = () => getOptions({ port: { server: 0 } });
107+
expect(t).toThrowErrorMatchingSnapshot();
108+
});
109+
110+
test('throws if port.server is missing', () => {
111+
const t = () => getOptions({ port: { client: 9000 } });
112+
expect(t).toThrowErrorMatchingSnapshot();
113+
});
104114
});

0 commit comments

Comments
 (0)