Skip to content

Commit 6bcddc9

Browse files
fix(server): respect sockPath on transportMode: 'ws' (webpack#2310)
1 parent a391b18 commit 6bcddc9

File tree

2 files changed

+65
-1
lines changed

2 files changed

+65
-1
lines changed

lib/servers/WebsocketServer.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,20 @@ module.exports = class WebsocketServer extends BaseServer {
1010
constructor(server) {
1111
super(server);
1212
this.wsServer = new ws.Server({
13-
server: this.server.listeningApp,
13+
noServer: true,
1414
path: this.server.sockPath,
1515
});
1616

17+
this.server.listeningApp.on('upgrade', (req, sock, head) => {
18+
if (!this.wsServer.shouldHandle(req)) {
19+
return;
20+
}
21+
22+
this.wsServer.handleUpgrade(req, sock, head, (connection) => {
23+
this.wsServer.emit('connection', connection, req);
24+
});
25+
});
26+
1727
this.wsServer.on('error', (err) => {
1828
this.server.log.error(err.message);
1929
});

test/server/servers/WebsocketServer.test.js

+54
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,60 @@ describe('WebsocketServer', () => {
8585
done();
8686
}, 3000);
8787
});
88+
89+
it('should match sockPath', (done) => {
90+
let receivedConnection = false;
91+
socketServer.onConnection((connection) => {
92+
receivedConnection = true;
93+
connection.close(4000);
94+
});
95+
96+
// eslint-disable-next-line new-cap
97+
const client = new ws(`http://localhost:${port}/ws-server?YEP`);
98+
99+
let receivedError = false;
100+
client.onerror = (e) => {
101+
receivedError = e.error;
102+
};
103+
104+
client.onclose = (e) => {
105+
expect(e.code).toEqual(4000);
106+
};
107+
108+
setTimeout(() => {
109+
expect(receivedConnection).toBeTruthy();
110+
expect(receivedError).toBeFalsy();
111+
done();
112+
}, 3000);
113+
});
114+
115+
it('should ignore other paths', (done) => {
116+
let receivedConnection = false;
117+
socketServer.onConnection((connection) => {
118+
receivedConnection = true;
119+
connection.close(4000);
120+
});
121+
122+
// eslint-disable-next-line new-cap
123+
const client = new ws(`http://localhost:${port}/ws-server-NOT`, {
124+
handshakeTimeout: 1000,
125+
});
126+
127+
let receivedError = false;
128+
client.onerror = (e) => {
129+
receivedError = e.error;
130+
};
131+
132+
client.onclose = (e) => {
133+
expect(e.code).not.toEqual(4000);
134+
};
135+
136+
setTimeout(() => {
137+
expect(receivedConnection).toBeFalsy();
138+
expect(receivedError).toBeTruthy();
139+
done();
140+
}, 3000);
141+
});
88142
});
89143

90144
afterAll((done) => {

0 commit comments

Comments
 (0)