agentfeed 0.1.1 → 0.1.2
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 +68 -47
- package/package.json +1 -1
package/dist/sse-client.js
CHANGED
|
@@ -1,60 +1,81 @@
|
|
|
1
|
+
import http from "node:http";
|
|
2
|
+
import https from "node:https";
|
|
1
3
|
export function connectSSE(url, apiKey, onEvent, onError) {
|
|
2
4
|
let aborted = false;
|
|
3
|
-
|
|
4
|
-
const
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
5
|
+
let currentReq = null;
|
|
6
|
+
const connect = () => {
|
|
7
|
+
if (aborted)
|
|
8
|
+
return;
|
|
9
|
+
const parsed = new URL(url);
|
|
10
|
+
const mod = parsed.protocol === "https:" ? https : http;
|
|
11
|
+
const req = mod.request(url, {
|
|
12
|
+
headers: {
|
|
13
|
+
Authorization: `Bearer ${apiKey}`,
|
|
14
|
+
Accept: "text/event-stream",
|
|
15
|
+
"Cache-Control": "no-cache",
|
|
16
|
+
},
|
|
17
|
+
}, (res) => {
|
|
18
|
+
if (res.statusCode !== 200) {
|
|
19
|
+
res.resume();
|
|
20
|
+
onError(new Error(`SSE connect failed: ${res.statusCode}`));
|
|
21
|
+
scheduleReconnect();
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
res.setEncoding("utf-8");
|
|
25
|
+
let buffer = "";
|
|
26
|
+
let currentEvent = { type: "message", data: "" };
|
|
27
|
+
res.on("data", (chunk) => {
|
|
28
|
+
buffer += chunk;
|
|
29
|
+
const lines = buffer.split("\n");
|
|
30
|
+
buffer = lines.pop() ?? "";
|
|
31
|
+
for (const line of lines) {
|
|
32
|
+
if (line.startsWith("event: ")) {
|
|
33
|
+
currentEvent.type = line.slice(7);
|
|
34
|
+
}
|
|
35
|
+
else if (line.startsWith("data: ")) {
|
|
36
|
+
currentEvent.data = line.slice(6);
|
|
37
|
+
}
|
|
38
|
+
else if (line.startsWith("id: ")) {
|
|
39
|
+
currentEvent.id = line.slice(4);
|
|
40
|
+
}
|
|
41
|
+
else if (line === "") {
|
|
42
|
+
if (currentEvent.data || currentEvent.type === "heartbeat") {
|
|
43
|
+
onEvent({ ...currentEvent });
|
|
40
44
|
}
|
|
45
|
+
currentEvent = { type: "message", data: "" };
|
|
41
46
|
}
|
|
42
47
|
}
|
|
48
|
+
});
|
|
49
|
+
res.on("end", () => {
|
|
50
|
+
if (!aborted)
|
|
51
|
+
scheduleReconnect();
|
|
52
|
+
});
|
|
53
|
+
res.on("error", (err) => {
|
|
54
|
+
if (!aborted) {
|
|
55
|
+
onError(err);
|
|
56
|
+
scheduleReconnect();
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
});
|
|
60
|
+
req.on("error", (err) => {
|
|
61
|
+
if (!aborted) {
|
|
62
|
+
onError(err);
|
|
63
|
+
scheduleReconnect();
|
|
43
64
|
}
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
65
|
+
});
|
|
66
|
+
req.end();
|
|
67
|
+
currentReq = req;
|
|
68
|
+
};
|
|
69
|
+
const scheduleReconnect = () => {
|
|
70
|
+
if (aborted)
|
|
71
|
+
return;
|
|
72
|
+
setTimeout(connect, 5000);
|
|
52
73
|
};
|
|
53
|
-
|
|
74
|
+
connect();
|
|
54
75
|
return {
|
|
55
76
|
close: () => {
|
|
56
77
|
aborted = true;
|
|
57
|
-
|
|
78
|
+
currentReq?.destroy();
|
|
58
79
|
},
|
|
59
80
|
};
|
|
60
81
|
}
|