Skip to content

Commit 6de744f

Browse files
authored
Refactor Stream to use eventstream-parser library (#214)
* Use the fetch() function provided in the Stream implementation * Allow js files to be imported into tests * Fix types for validateWebhook * Handle processing chunked event streams * Await validateWebhook in tests
1 parent 742c664 commit 6de744f

11 files changed

+588
-119
lines changed

README.md

+8
Original file line numberDiff line numberDiff line change
@@ -811,3 +811,11 @@ You can call this method directly to make other requests to the API.
811811
## TypeScript
812812

813813
The `Replicate` constructor and all `replicate.*` methods are fully typed.
814+
815+
## Vendored Dependencies
816+
817+
We have a few dependencies that have been bundled into the vendor directory rather than adding external npm dependencies.
818+
819+
These have been generated using bundlejs.com and copied into the appropriate directory along with the license and repository information.
820+
821+
* [eventsource-parser/stream](https://bundlejs.com/?bundle&q=eventsource-parser%40latest%2Fstream&config=%7B%22esbuild%22%3A%7B%22format%22%3A%22cjs%22%2C%22minify%22%3Afalse%2C%22platform%22%3A%22neutral%22%7D%7D)

biome.json

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
{
22
"$schema": "https://biomejs.dev/schemas/1.0.0/schema.json",
3+
"files": {
4+
"ignore": [".wrangler", "vendor/*"]
5+
},
36
"formatter": {
47
"indentStyle": "space",
58
"indentWidth": 2

index.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ declare module "replicate" {
279279
signature?: string;
280280
},
281281
secret: string
282-
): boolean;
282+
): Promise<boolean>;
283283

284284
export function parseProgressFromLogs(logs: Prediction | string): {
285285
percentage: number;

index.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const ApiError = require("./lib/error");
22
const ModelVersionIdentifier = require("./lib/identifier");
3-
const { Stream } = require("./lib/stream");
3+
const { createReadableStream } = require("./lib/stream");
44
const {
55
withAutomaticRetries,
66
validateWebhook,
@@ -291,7 +291,11 @@ class Replicate {
291291

292292
if (prediction.urls && prediction.urls.stream) {
293293
const { signal } = options;
294-
const stream = new Stream(prediction.urls.stream, { signal });
294+
const stream = createReadableStream({
295+
url: prediction.urls.stream,
296+
fetch: this.fetch,
297+
options: { signal },
298+
});
295299
yield* stream;
296300
} else {
297301
throw new Error("Prediction does not support streaming");

0 commit comments

Comments
 (0)