From fe847cbb502fbf095b6e3d6e5c0c5baa03d1c961 Mon Sep 17 00:00:00 2001 From: wolfy1339 Date: Tue, 24 Sep 2024 10:51:13 -0400 Subject: [PATCH 1/2] fix: add proxy support for `EventSource` --- README.md | 28 ++++++++++++++++++++++++++++ index.ts | 4 +++- 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index d1f8e28..ac0d82d 100644 --- a/README.md +++ b/README.md @@ -40,3 +40,31 @@ 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 b4442a2..22cf1eb 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 From 10efdf7098eac6f713eb1bb330fd0e61aa08667d Mon Sep 17 00:00:00 2001 From: wolfy1339 Date: Tue, 24 Sep 2024 10:53:56 -0400 Subject: [PATCH 2/2] docs(README): add linebreak --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index ac0d82d..bc65a64 100644 --- a/README.md +++ b/README.md @@ -44,6 +44,7 @@ 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: