Skip to content

Commit ff0b156

Browse files
committed
Use the fetch() function provided in the Stream implementation
1 parent 60a8e18 commit ff0b156

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,11 @@ class Replicate {
289289

290290
if (prediction.urls && prediction.urls.stream) {
291291
const { signal } = options;
292-
const stream = new Stream(prediction.urls.stream, { signal });
292+
const stream = new Stream({
293+
url: prediction.urls.stream,
294+
fetch: this.fetch,
295+
options: { signal },
296+
});
293297
yield* stream;
294298
} else {
295299
throw new Error("Prediction does not support streaming");

lib/stream.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,12 @@ class Stream extends Readable {
4848
/**
4949
* Create a new stream of server-sent events.
5050
*
51-
* @param {string} url The URL to connect to.
52-
* @param {object} options The fetch options.
51+
* @param {object} config
52+
* @param {string} config.url The URL to connect to.
53+
* @param {Function} [config.fetch] The fetch implemention to use.
54+
* @param {object} [config.options] The fetch options.
5355
*/
54-
constructor(url, options) {
56+
constructor({ url, fetch = globalThis.fetch, options = {} }) {
5557
if (!Readable) {
5658
throw new Error(
5759
"Readable streams are not supported. Please use Node.js 18 or later, or install the readable-stream package."
@@ -60,6 +62,7 @@ class Stream extends Readable {
6062

6163
super();
6264
this.url = url;
65+
this.fetch = fetch;
6366
this.options = options;
6467

6568
this.event = null;
@@ -104,7 +107,7 @@ class Stream extends Readable {
104107
}
105108

106109
async *[Symbol.asyncIterator]() {
107-
const response = await fetch(this.url, {
110+
const response = await this.fetch(this.url, {
108111
...this.options,
109112
headers: {
110113
Accept: "text/event-stream",

0 commit comments

Comments
 (0)