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
Add support for websockets in proxy build script #373
Conversation
I think that adding I have a couple ideas that could improve this PR:
I think we should have @FredKSchott weigh in here before you act on any of my suggestions. I can also help out with implementation and testing for this feature. It is one of the major things holding me back from being able to port apps over. Also, wondering if there would be a benefit in offering the basic string configuration for a simple to use for general use-cases? |
For me, this is a must have to keep the basic string config : both in terms of backward compat (even if 2.0 is fresh), simplicity and documentation.
💯 with that, something like Currently, my issue regarding
👍 |
Agreed, the backwards compat and simplicity I think warrant this feature.
That's definitely an option that I hadn't thought about. I was thinking something like: module.exports = {
scripts: {
"proxy:api": {
target: ENVIRONMENTS.dev,
changeOrigin: true,
ws: true,
}
}
} This would allow multiple proxies to be able to be configured in a similar setup to the existing proxying config just with some advanced features/capabilities. In my work setup, we have multiple proxies. I think we'll also need to create a proxy for each config, unless we move towards a middleware approach like CRA implemented. That I feel is probably out-of-scope though. |
…rConfig exists or not
@stramel @fcamblor I think that the idea behind snowpack is to have a simple directive that uses sensible defaults for the majority of use cases. But, as you point out, there are a lot of edge cases that cannot be expressed in that simple way. We could add // snowpack.config.js
module.exports = {
scripts: {
'proxy:api': 'proxy http://localhost:5000/api --to /api',
'proxy:weather': 'proxy http://weather.dev/weather--to /weather'
},
proxyOptions: {
'proxy:weather': (proxy, options) => {
Object.assign(options, {
changeOrigin: true
});
proxy.on('proxyReq', function (proxyReq, req, res, options) {
proxyReq.setHeader('X-Special-Proxy-Header', 'foobar');
});
},
},
}; Dont' know if that would be overkill. |
FYI, created a PR for this PR on @germanftorres repo : germanftorres#1 regarding Regarding config, I would have done something like this : // snowpack.config.js
module.exports = {
scripts: {
'proxy:api': 'proxy http://localhost:5000/api --to /api',
'proxy:weather': 'proxy http://weather.dev/weather--to /weather'
},
devOptions: {
// General purpose options that are going to be applied on proxy:* configs
httpProxyOptions: {
changeOrigin: false
},
// Allows finer (maybe overkill) tuning
configureHttpProxy: (snowpackProxyConfig, httpProxyOptions, proxy) => {
if(snowpackProxyConfig.id === 'weather') {
proxy.on('proxyReq', function (proxyReq, req, res, options) {
proxyReq.setHeader('X-Special-Proxy-Header', 'foobar');
});
return Object.assign({}, httpProxyOptions, { changeOrigin: true });
}
// For other configs than the `weather` one, keep config as it is
return httpProxyOptions;
}
}
}; My feeling is we're not going to have a lot of different proxy configs, that's why we may have different config levels here :
What I like in this is :
However, one important thing is : stuff passed to the |
Snowpack PR FredKSchott#373 improvement
@stramel @germanftorres @fcamblor just left some feedback here and here |
@germanftorres wondering if you shouldn't rename your PR as the |
Thanks for the contribution @germanftorres! There's a little bit of cleanup needed to pass linting but instead of going back and forth I'm just going to tackle as a part of merging on the command line to unblock everyone in #371 . |
Hi,
I have found several issues with the proxy implementation based on
got
: websockets support was missing and the request body was no being proxied correctly (in POST, PUT...)This commit adds a dependency on http-proxy which does all the heavy work for proxying requests. I have manually tested POST, GET, and websockets upgrade scenario, but really don't know how to write a proper test.
Please review very carefully.
Thanks!!