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

Proxy support for smee client #194

Closed
ojoggerst opened this issue May 16, 2022 · 13 comments · Fixed by #299 or #317
Closed

Proxy support for smee client #194

ojoggerst opened this issue May 16, 2022 · 13 comments · Fixed by #299 or #317

Comments

@ojoggerst
Copy link

ojoggerst commented May 16, 2022

Problem description

We would like to use the smee client behind a proxy. Currently, there is no possibility to add a proxy URL to the smee client, so that all outgoing connections would be proxied through a specific server.

Solution

  1. Use superagent proxy (https://www.npmjs.com/package/superagent-proxy)
  2. Use global agent (https://www.npmjs.com/package/global-agent)

Option one needs code changes and seems a bit more cumbersome.

Option two is fairly simple and only needs global agent dependencies and one line in index.js to import the package.

After that you can do:
GLOBAL_AGENT_HTTP_PROXY=http://<proxy-host>:<proxy-port> node ./bin/smee.js -u https://<smee-server>/<token>

Please check PR: #195

@jakobkolb
Copy link

I need this feature to be able to use smee in our environment. Would love to see it become reality ❤️

@wolfy1339
Copy link
Contributor

You can pass in your own custom fetch wrapper. NodeJS doesn't support Proxies out of the box with the Fetch API currently

import { fetch as undiciFetch, ProxyAgent } from 'undici';
import SmeeClient from "smee-client";

const myFetch = (url, options) => {
  return undiciFetch(url, {
    ...options,
    dispatcher: new ProxyAgent(<your_proxy_url>)
  })
}
const smee = new SmeeClient({
  source: 'https://smee.io/abc123',
  target: 'http://localhost:3000/events',
  logger: console,
  fetch: myFetch
})

This should be added to the README

@ftrahman
Copy link

ftrahman commented Aug 9, 2024

where are we meant to pass the custom fetch wrapper to? smee.js?

i tried the previous solution and ended up with a sea of errors.

@wolfy1339
Copy link
Contributor

where are we meant to pass the custom fetch wrapper to? smee.js?

Yes. That is what is indicated in the code example I shared

i tried the previous solution and ended up with a sea of errors.

Those packages are probably not compatible with the native Fetch API available in Node and instead use the Node HTTP API

@Uzlopak
Copy link
Collaborator

Uzlopak commented Aug 9, 2024

Actually the examply is not quite the recommended way by undici.

You would use setGlobalDispatcher to set the proxy agent globally. Also there is EnvProxyAgent, which can handle the HTTP_PROXY and HTTPS_PROXY env variables. I have to look up if it is enabled by default now on node.js, but that was the desired behavior.

@wolfy1339
Copy link
Contributor

Actually the examply is not quite the recommended way by undici.

You would use setGlobalDispatcher to set the proxy agent globally. Also there is EnvProxyAgent, which can handle the HTTP_PROXY and HTTPS_PROXY env variables. I have to look up if it is enabled by default now on node.js, but that was the desired behavior.

Undici probably updated since then, but my solution is backwards compatible to all NodeJS versions we support.

At the time of Node 18.0, there was no support for the HTTP_PROXY and HTTPS_PROXY env variables, nor was there an EnvProxyAgent

Besides, do we really want to include undici (which is a fairly large package) just to be able to have the proxy env variables?

@ftrahman
Copy link

ftrahman commented Aug 9, 2024

even having tried your solution @wolfy1339, i am still receiving connection timeouts. this has been hindering me for a few days now

@wolfy1339
Copy link
Contributor

What version are you running?

@ftrahman
Copy link

ftrahman commented Aug 9, 2024

node is 18.20.2, smee is 2.0.2

@wolfy1339
Copy link
Contributor

wolfy1339 commented Aug 11, 2024

There is also another place you need to specify the proxy:
https://www.npmjs.com/package/eventsource#httphttps-proxy

npm
W3C compliant EventSource client for Node.js and browser (polyfill). Latest version: 2.0.2, last published: 2 years ago. Start using eventsource in your project by running `npm i eventsource`. There are 805 other projects in the npm registry using eventsource.

wolfy1339 added a commit that referenced this issue Aug 22, 2024
wolfy1339 added a commit that referenced this issue Aug 23, 2024
Copy link

🎉 This issue has been resolved in version 3.0.0-beta.2 🎉

The release is available on:

Your semantic-release bot 📦🚀

wolfy1339 added a commit that referenced this issue Oct 17, 2024
This PR adds proxy support for the `EventSource` which was missing.
Fixes #194
Copy link

🎉 This issue has been resolved in version 2.0.4 🎉

The release is available on:

Your semantic-release bot 📦🚀

Copy link

🎉 This issue has been resolved in version 3.0.0-beta.5 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
5 participants