Skip to content

Commit fa7efb8

Browse files
committed
Detect file streams by looking at URL path prefix
The file streaming API is hosted at https://stream.replicate.com/v1/files so we can use this prefix to determine that the response will contain file entities. This is far cleaner than relying on the event contents.
1 parent c2e5811 commit fa7efb8

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

lib/stream.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ class ServerSentEvent {
5353
*/
5454
function createReadableStream({ url, fetch, options = {} }) {
5555
const { useFileOutput = true, headers = {}, ...initOptions } = options;
56+
const shouldProcessFileOutput = useFileOutput && isFileStream(url);
5657

5758
return new ReadableStream({
5859
async start(controller) {
@@ -89,9 +90,9 @@ function createReadableStream({ url, fetch, options = {} }) {
8990

9091
let data = event.data;
9192
if (
92-
useFileOutput &&
93-
typeof data === "string" &&
94-
(data.startsWith("https:") || data.startsWith("data:"))
93+
event.event === "output" &&
94+
shouldProcessFileOutput &&
95+
typeof data === "string"
9596
) {
9697
data = createFileOutput({ url: data, fetch });
9798
}
@@ -169,6 +170,13 @@ function createFileOutput({ url, fetch }) {
169170
});
170171
}
171172

173+
function isFileStream(url) {
174+
try {
175+
return new URL(url).pathname.startsWith("/v1/files/");
176+
} catch {}
177+
return false;
178+
}
179+
172180
module.exports = {
173181
createFileOutput,
174182
createReadableStream,

0 commit comments

Comments
 (0)