Skip to content

Commit

Permalink
fix: add proxy support for EventSource (#317)
Browse files Browse the repository at this point in the history
This PR adds proxy support for the `EventSource` which was missing.
Fixes #194
  • Loading branch information
wolfy1339 authored Oct 17, 2024
1 parent 85c8747 commit ac6d677
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
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

0 comments on commit ac6d677

Please sign in to comment.