Skip to content

Commit 0b04f18

Browse files
committed
[ws] add options to transform client and server streams
1 parent a3fe02d commit 0b04f18

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,8 @@ proxyServer.listen(8015);
373373
}
374374
```
375375
* **headers**: object with extra headers to be added to target requests.
376+
* **createWsClientTransformStream**: if set, this function will be called with three arguments `req`, `proxyReq` and `proxyRes` and should return a Duplex stream, data from the client websocket will be piped through this stream before being piped to the server, allowing you to influence the request data.
377+
* **createWsServerTransformStream**: if set, this function will be called with three arguments `req`, `proxyReq` and `proxyRes` and should return a Duplex stream, data from the server websocket will be piped through this stream before being piped to the client, allowing you to influence the response data.
376378
* **proxyTimeout**: timeout (in millis) for outgoing proxy requests
377379
* **timeout**: timeout (in millis) for incoming requests
378380
* **followRedirects**: true/false, Default: false - specify whether you want to follow redirects

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

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,33 @@ module.exports = {
142142
//
143143
socket.write(createHttpHeader('HTTP/1.1 101 Switching Protocols', proxyRes.headers));
144144

145-
proxySocket.pipe(socket).pipe(proxySocket);
145+
var proxyStream = proxySocket;
146+
147+
if (options.createWsServerTransformStream) {
148+
const wsServerTransformStream = options.createWsServerTransformStream(
149+
req,
150+
proxyReq,
151+
proxyRes,
152+
);
153+
154+
wsServerTransformStream.on('error', onOutgoingError);
155+
proxyStream = proxyStream.pipe(wsServerTransformStream);
156+
}
157+
158+
proxyStream = proxyStream.pipe(socket);
159+
160+
if (options.createWsClientTransformStream) {
161+
const wsClientTransformStream = options.createWsClientTransformStream(
162+
req,
163+
proxyReq,
164+
proxyRes,
165+
);
166+
167+
wsClientTransformStream.on('error', onOutgoingError);
168+
proxyStream = proxyStream.pipe(wsClientTransformStream);
169+
}
170+
171+
proxyStream.pipe(proxySocket);
146172

147173
server.emit('open', proxySocket);
148174
server.emit('proxySocket', proxySocket); //DEPRECATED.

0 commit comments

Comments
 (0)