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

fix: add proxy support for EventSource #317

Merged
merged 2 commits into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,32 @@ const events = smee.start()
// Stop forwarding events
events.close()
```

#### Proxy

By default, the Smee client does not make use of the standard proxy server environment variables. To add support for proxy servers you will need to provide an https client that supports them such as [`undici.EnvHttpProxyAgent()`](https://undici.nodejs.org/#/docs/api/EnvHttpProxyAgent).

Afterwards, you will be able to use the standard proxy server environment variables.

For example, this would use a `EnvHttpProxyAgent` to make requests through a proxy server:

```js
const { EnvHttpProxyAgent, fetch: undiciFetch } = require("undici");
const SmeeClient = require('smee-client');

const myFetch = (url, options) => {
return undiciFetch(url, {
...options,
dispatcher: new EnvHTTPProxyAgent()
})
};

const smee = new SmeeClient({
source: 'https://smee.io/abc123',
target: 'http://localhost:3000/events',
logger: console,
fetch: myFetch
});

const events = smee.start();
```
4 changes: 3 additions & 1 deletion index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,9 @@ class Client {
}

start() {
const events = new EventSource(this.source);
const events = new EventSource(this.source, {
proxy: process.env.HTTP_PROXY || process.env.HTTPS_PROXY,
});

// Reconnect immediately
(events as any).reconnectInterval = 0; // This isn't a valid property of EventSource
Expand Down