Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ws] add options to transform client and server streams #1301

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -373,6 +373,8 @@ proxyServer.listen(8015);
}
```
* **headers**: object with extra headers to be added to target requests.
* **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.
* **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.
* **proxyTimeout**: timeout (in millis) for outgoing proxy requests
* **timeout**: timeout (in millis) for incoming requests
* **followRedirects**: true/false, Default: false - specify whether you want to follow redirects
Expand Down
28 changes: 27 additions & 1 deletion lib/http-proxy/passes/ws-incoming.js
Expand Up @@ -142,7 +142,33 @@ module.exports = {
//
socket.write(createHttpHeader('HTTP/1.1 101 Switching Protocols', proxyRes.headers));

proxySocket.pipe(socket).pipe(proxySocket);
var proxyStream = proxySocket;

if (options.createWsServerTransformStream) {
const wsServerTransformStream = options.createWsServerTransformStream(
req,
proxyReq,
proxyRes,
);

wsServerTransformStream.on('error', onOutgoingError);
proxyStream = proxyStream.pipe(wsServerTransformStream);
}

proxyStream = proxyStream.pipe(socket);

if (options.createWsClientTransformStream) {
const wsClientTransformStream = options.createWsClientTransformStream(
req,
proxyReq,
proxyRes,
);

wsClientTransformStream.on('error', onOutgoingError);
proxyStream = proxyStream.pipe(wsClientTransformStream);
}

proxyStream.pipe(proxySocket);

server.emit('open', proxySocket);
server.emit('proxySocket', proxySocket); //DEPRECATED.
Expand Down