-
Notifications
You must be signed in to change notification settings - Fork 220
/
Copy pathstream.js
147 lines (128 loc) · 3.21 KB
/
stream.js
1
2
3
4
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/**
* A server-sent event.
*/
class ServerSentEvent {
/**
* Create a new server-sent event.
*
* @param {string} event The event name.
* @param {string} data The event data.
* @param {string} id The event ID.
* @param {number} retry The retry time.
*/
constructor(event, data, id, retry) {
this.event = event;
this.data = data;
this.id = id;
this.retry = retry;
}
/**
* Convert the event to a string.
*/
toString() {
if (this.event === "output") {
return this.data;
}
return "";
}
}
async function createStream(url, options) {
// Attempt to use readable-stream if available, attempt to use the built-in stream module.
let Readable;
try {
Readable = await import("readable-stream").then(
(module) => module.Readable
);
} catch (e) {
try {
Readable = await import("node:stream").then((module) => module.Readable);
} catch (e) {
throw new Error(
"Readable streams are not supported. Please use Node.js 18 or later, or install the readable-stream package."
);
}
}
/**
* A stream of server-sent events.
*/
class Stream extends Readable {
/**
* Create a new stream of server-sent events.
*
* @param {string} url The URL to connect to.
* @param {object} options The fetch options.
*/
constructor(url, options) {
if (!Readable) {
throw new Error(
"Readable streams are not supported. Please use Node.js 18 or later, or install the readable-stream package."
);
}
super();
this.url = url;
this.options = options;
this.event = null;
this.data = [];
this.lastEventId = null;
this.retry = null;
}
decode(line) {
if (!line) {
if (!this.event && !this.data.length && !this.lastEventId) {
return null;
}
const sse = new ServerSentEvent(
this.event,
this.data.join("\n"),
this.lastEventId
);
this.event = null;
this.data = [];
this.retry = null;
return sse;
}
if (line.startsWith(":")) {
return null;
}
const [field, value] = line.split(": ");
if (field === "event") {
this.event = value;
} else if (field === "data") {
this.data.push(value);
} else if (field === "id") {
this.lastEventId = value;
}
return null;
}
async *[Symbol.asyncIterator]() {
const response = await fetch(this.url, {
...this.options,
headers: {
Accept: "text/event-stream",
},
});
for await (const chunk of response.body) {
const decoder = new TextDecoder("utf-8");
const text = decoder.decode(chunk);
const lines = text.split("\n");
for (const line of lines) {
const sse = this.decode(line);
if (sse) {
if (sse.event === "error") {
throw new Error(sse.data);
}
yield sse;
if (sse.event === "done") {
return;
}
}
}
}
}
}
return new Stream(url, options);
}
module.exports = {
ServerSentEvent,
createStream,
};