From ac6d677bf485af21da079687d99999bd0c440614 Mon Sep 17 00:00:00 2001 From: wolfy1339 <4595477+wolfy1339@users.noreply.github.com> Date: Thu, 17 Oct 2024 11:06:44 -0400 Subject: [PATCH] fix: add proxy support for `EventSource` (#317) This PR adds proxy support for the `EventSource` which was missing. Fixes #194 --- README.md | 29 +++++++++++++++++++++++++++++ index.ts | 4 +++- 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index d1f8e281..bc65a646 100644 --- a/README.md +++ b/README.md @@ -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(); +``` diff --git a/index.ts b/index.ts index b4442a2f..22cf1eb0 100644 --- a/index.ts +++ b/index.ts @@ -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