agentfeed 0.1.2 → 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/sse-client.js +24 -5
- package/package.json +1 -1
package/dist/sse-client.js
CHANGED
|
@@ -2,25 +2,39 @@ import http from "node:http";
|
|
|
2
2
|
import https from "node:https";
|
|
3
3
|
export function connectSSE(url, apiKey, onEvent, onError) {
|
|
4
4
|
let aborted = false;
|
|
5
|
+
let reconnecting = false;
|
|
5
6
|
let currentReq = null;
|
|
6
7
|
const connect = () => {
|
|
7
8
|
if (aborted)
|
|
8
9
|
return;
|
|
10
|
+
reconnecting = false;
|
|
9
11
|
const parsed = new URL(url);
|
|
10
|
-
const
|
|
11
|
-
const
|
|
12
|
+
const isHttps = parsed.protocol === "https:";
|
|
13
|
+
const options = {
|
|
14
|
+
hostname: parsed.hostname,
|
|
15
|
+
port: parsed.port || (isHttps ? 443 : 80),
|
|
16
|
+
path: parsed.pathname + parsed.search,
|
|
17
|
+
method: "GET",
|
|
12
18
|
headers: {
|
|
13
19
|
Authorization: `Bearer ${apiKey}`,
|
|
14
20
|
Accept: "text/event-stream",
|
|
15
21
|
"Cache-Control": "no-cache",
|
|
22
|
+
Connection: "keep-alive",
|
|
16
23
|
},
|
|
17
|
-
|
|
24
|
+
// Disable connection pooling to avoid agent timeouts
|
|
25
|
+
agent: false,
|
|
26
|
+
};
|
|
27
|
+
const mod = isHttps ? https : http;
|
|
28
|
+
const req = mod.request(options, (res) => {
|
|
18
29
|
if (res.statusCode !== 200) {
|
|
19
30
|
res.resume();
|
|
20
31
|
onError(new Error(`SSE connect failed: ${res.statusCode}`));
|
|
21
32
|
scheduleReconnect();
|
|
22
33
|
return;
|
|
23
34
|
}
|
|
35
|
+
// Keep TCP connection alive
|
|
36
|
+
res.socket?.setKeepAlive(true, 10000);
|
|
37
|
+
res.socket?.setTimeout(0);
|
|
24
38
|
res.setEncoding("utf-8");
|
|
25
39
|
let buffer = "";
|
|
26
40
|
let currentEvent = { type: "message", data: "" };
|
|
@@ -47,8 +61,10 @@ export function connectSSE(url, apiKey, onEvent, onError) {
|
|
|
47
61
|
}
|
|
48
62
|
});
|
|
49
63
|
res.on("end", () => {
|
|
50
|
-
if (!aborted)
|
|
64
|
+
if (!aborted) {
|
|
65
|
+
console.log("SSE stream ended, reconnecting...");
|
|
51
66
|
scheduleReconnect();
|
|
67
|
+
}
|
|
52
68
|
});
|
|
53
69
|
res.on("error", (err) => {
|
|
54
70
|
if (!aborted) {
|
|
@@ -63,12 +79,15 @@ export function connectSSE(url, apiKey, onEvent, onError) {
|
|
|
63
79
|
scheduleReconnect();
|
|
64
80
|
}
|
|
65
81
|
});
|
|
82
|
+
// Disable request timeout
|
|
83
|
+
req.setTimeout(0);
|
|
66
84
|
req.end();
|
|
67
85
|
currentReq = req;
|
|
68
86
|
};
|
|
69
87
|
const scheduleReconnect = () => {
|
|
70
|
-
if (aborted)
|
|
88
|
+
if (aborted || reconnecting)
|
|
71
89
|
return;
|
|
90
|
+
reconnecting = true;
|
|
72
91
|
setTimeout(connect, 5000);
|
|
73
92
|
};
|
|
74
93
|
connect();
|