Skip to content

Commit c660cae

Browse files
authored
fix: don't use undici EventSource (#352)
Unfortunately it isn't suitable as it completely swallows errors with no way of getting them. No errors are thrown, and no details are included in the `error` event.
1 parent 6947311 commit c660cae

File tree

3 files changed

+39
-6
lines changed

3 files changed

+39
-6
lines changed

index.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import validator from "validator";
2+
import { fetch as undiciFetch, EnvHttpProxyAgent } from "undici";
23
import {
3-
fetch as undiciFetch,
44
EventSource,
5-
EnvHttpProxyAgent,
5+
type FetchLike,
6+
type FetchLikeInit,
67
type ErrorEvent,
7-
type MessageEvent,
8-
} from "undici";
8+
} from "eventsource";
99
import url from "node:url";
1010
import querystring from "node:querystring";
1111

@@ -56,7 +56,7 @@ class Client {
5656
return address;
5757
}
5858

59-
async onmessage(msg: MessageEvent<string>) {
59+
async onmessage(msg: MessageEvent) {
6060
const data = JSON.parse(msg.data);
6161

6262
const target = url.parse(this.#target, true);
@@ -104,8 +104,18 @@ class Client {
104104
}
105105

106106
start() {
107+
const customFetch: FetchLike = (
108+
url: string | URL,
109+
options?: FetchLikeInit,
110+
) => {
111+
return this.#fetch(url, {
112+
...options,
113+
dispatcher: proxyAgent,
114+
});
115+
};
116+
107117
const events = new EventSource(this.#source, {
108-
dispatcher: proxyAgent,
118+
fetch: customFetch,
109119
});
110120

111121
// Reconnect immediately

package-lock.json

Lines changed: 22 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
"license": "ISC",
2626
"dependencies": {
2727
"commander": "^12.0.0",
28+
"eventsource": "^3.0.5",
2829
"undici": "^6.19.8",
2930
"validator": "^13.11.0"
3031
},

0 commit comments

Comments
 (0)