Skip to content

Commit 3a770f8

Browse files
committed
feat: pass request object to message interceptors
1 parent 66ffb89 commit 3a770f8

File tree

3 files changed

+28
-26
lines changed

3 files changed

+28
-26
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -395,21 +395,21 @@ proxyServer.listen(8015);
395395
396396
};
397397
```
398-
* **wsInterceptClientMsg**: Is a handler which is called when a websocket message is intercepted on its way to the server from the client. It takes two arguments: `data` - is a websocket message and flags (fin, mask, compress, binary). If falsy value is returned then nothing will be sended to the client.
398+
* **wsInterceptClientMsg**: Is a handler which is called when a websocket message is intercepted on its way to the server from the client. It takes two arguments: `data` - is a websocket message and `options` in which exists field `req` - websocket request object and `flags` (fin, mask, compress, binary). If falsy value is returned then nothing will be sended to the client.
399399
```
400400
const proxy = new HttpProxy({
401401
...
402-
wsInterceptClientMsg: (data, flags) {
402+
wsInterceptClientMsg: (data, options) {
403403
return typeof data === 'string ? data.toUpperCase() : data;
404404
}
405405
...
406406
})
407407
```
408-
* **wsInterceptServerMsg**: Is a handler which is called when a websocket message is intercepted on its way to the client from the server. It takes two arguments: `data` - is a websocket message and flags (fin, mask, compress, binary). If falsy value is returned then nothing will be sended to the target server.
408+
* **wsInterceptServerMsg**: Is a handler which is called when a websocket message is intercepted on its way to the client from the server. It takes two arguments: `data` - is a websocket message and `options` in which exist fields `req` - websocket request object and `flags` (fin, mask, compress, binary). If falsy value is returned then nothing will be sended to the target server.
409409
```
410410
const proxy = new HttpProxy({
411411
...
412-
wsInterceptServerMsg: (data, flags) {
412+
wsInterceptServerMsg: (data, options) {
413413
return typeof data === 'string ? data.toUpperCase() : data;
414414
}
415415
...

lib/http-proxy/passes/ws-incoming.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,8 @@ module.exports = {
145145
//
146146
socket.write(createHttpHeader('HTTP/1.1 101 Switching Protocols', proxyRes.headers));
147147

148-
const wsInterceptor = WsInterceptor.create({socket, options, proxyReq, proxyRes, proxySocket});
148+
const wsInterceptor = WsInterceptor.create({socket, options, req, proxyReq, proxyRes, proxySocket});
149+
149150
wsInterceptor.startDataTransfer();
150151

151152
server.emit('open', proxySocket);

lib/http-proxy/ws/interceptor.js

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,30 +19,15 @@ const acceptExtensions = ({extensions, isServer}) => {
1919
return {[extensionName]: perMessageDeflate};
2020
};
2121

22-
const getMsgHandler = ({interceptor, dataSender, binary}) => {
23-
return (data, flags) => {
24-
if (typeof interceptor !== 'function') {
25-
dataSender({data});
26-
return;
27-
}
28-
29-
const modifiedData = interceptor(data, flags);
30-
31-
// if interceptor does not return data then nothing will be sended to the server
32-
if (modifiedData) {
33-
dataSender({data: modifiedData, binary});
34-
}
35-
}
36-
};
37-
3822
module.exports = class Interceptor {
3923
static create(opts = {}) {
4024
return new this(opts);
4125
}
4226

43-
constructor({socket, options, proxyReq, proxyRes, proxySocket}) {
27+
constructor({socket, options, req, proxyReq, proxyRes, proxySocket}) {
4428
this._socket = socket;
4529
this._options = options;
30+
this._req = req;
4631
this._proxyReq = proxyReq;
4732
this._proxyRes = proxyRes;
4833
this._proxySocket = proxySocket;
@@ -74,6 +59,22 @@ module.exports = class Interceptor {
7459
};
7560
}
7661

62+
_getMsgHandler({interceptor, dataSender, binary}) {
63+
return (data, flags) => {
64+
if (typeof interceptor !== 'function') {
65+
dataSender({data});
66+
return;
67+
}
68+
69+
const modifiedData = interceptor(data, {req: this._req, flags});
70+
71+
// if interceptor does not return data then nothing will be sended to the server
72+
if (modifiedData) {
73+
dataSender({data: modifiedData, binary});
74+
}
75+
}
76+
}
77+
7778
_interceptClientMessages() {
7879
const receiver = new Receiver(this._clientExtensions);
7980
const sender = new Sender(this._proxySocket, this._serverExtensions);
@@ -83,8 +84,8 @@ module.exports = class Interceptor {
8384
const options = {mask: true};
8485
const dataSender = this._getDataSender({sender, event: 'wsClientMsg', options});
8586

86-
receiver.ontext = getMsgHandler({interceptor: this._options.wsInterceptClientMsg, dataSender, binary: false});
87-
receiver.onbinary = getMsgHandler({interceptor: this._options.wsInterceptClientMsg, dataSender, binary: true});
87+
receiver.ontext = this._getMsgHandler({interceptor: this._options.wsInterceptClientMsg, dataSender, binary: false});
88+
receiver.onbinary = this._getMsgHandler({interceptor: this._options.wsInterceptClientMsg, dataSender, binary: true});
8889
receiver.onclose = (code, msg, {masked: mask}) => this._isSocketOpened && sender.close(code, msg, mask);
8990

9091
this._socket.on('data', (data) => receiver.add(data));
@@ -98,8 +99,8 @@ module.exports = class Interceptor {
9899
const options = {mask: false};
99100
const dataSender = this._getDataSender({sender, event: 'wsServerMsg', options});
100101

101-
receiver.ontext = getMsgHandler({interceptor: this._options.wsInterceptServerMsg, dataSender, binary: false});
102-
receiver.onbinary = getMsgHandler({interceptor: this._options.wsInterceptServerMsg, dataSender, binary: true});
102+
receiver.ontext = this._getMsgHandler({interceptor: this._options.wsInterceptServerMsg, dataSender, binary: false});
103+
receiver.onbinary = this._getMsgHandler({interceptor: this._options.wsInterceptServerMsg, dataSender, binary: true});
103104
receiver.onclose = (code, msg, {masked: mask}) => this._isSocketOpened && sender.close(code, msg, mask);
104105

105106
this._proxySocket.on('data', (data) => receiver.add(data));

0 commit comments

Comments
 (0)