Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(client): event stream response check #89

Merged
merged 1 commit into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@

/dist
/coverage
/docs
package-lock.json
2 changes: 1 addition & 1 deletion libs/client/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@fal-ai/serverless-client",
"description": "The fal serverless JS/TS client",
"version": "0.14.2",
"version": "0.14.3",
"license": "MIT",
"repository": {
"type": "git",
Expand Down
11 changes: 8 additions & 3 deletions libs/client/src/streaming.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

export type StreamingConnectionMode = "client" | "server";

const CONTENT_TYPE_EVENT_STREAM = "text/event-stream";

/**
* The stream API options. It requires the API input and also
* offers configuration options.
Expand Down Expand Up @@ -67,7 +69,7 @@

type FalStreamEventType = "data" | "error" | "done";

type EventHandler<T = any> = (event: T) => void;

Check warning on line 72 in libs/client/src/streaming.ts

View workflow job for this annotation

GitHub Actions / build

Unexpected any. Specify a different type

/**
* The class representing a streaming response. With t
Expand Down Expand Up @@ -135,7 +137,7 @@
const response = await fetch(parsedUrl.toString(), {
method: method.toUpperCase(),
headers: {
accept: options.accept ?? "text/event-stream",
accept: options.accept ?? CONTENT_TYPE_EVENT_STREAM,
"content-type": "application/json",
},
body: input && method !== "get" ? JSON.stringify(input) : undefined,
Expand All @@ -145,7 +147,7 @@
}
return await dispatchRequest(method.toUpperCase(), this.url, input, {
headers: {
accept: options.accept ?? "text/event-stream",
accept: options.accept ?? CONTENT_TYPE_EVENT_STREAM,
},
responseHandler: this.handleResponse,
signal: this.abortController.signal,
Expand Down Expand Up @@ -180,8 +182,11 @@
return;
}

const isEventStream = response.headers
.get("content-type")
.startsWith(CONTENT_TYPE_EVENT_STREAM);
// any response that is not a text/event-stream will be handled as a binary stream
if (response.headers.get("content-type") !== "text/event-stream") {
if (!isEventStream) {
const reader = body.getReader();
const emitRawChunk = () => {
reader.read().then(({ done, value }) => {
Expand Down Expand Up @@ -212,7 +217,7 @@
this.emit("data", parsedData);

// also emit 'message'for backwards compatibility
this.emit("message" as any, parsedData);

Check warning on line 220 in libs/client/src/streaming.ts

View workflow job for this annotation

GitHub Actions / build

Unexpected any. Specify a different type
} catch (e) {
this.emit("error", e);
}
Expand Down Expand Up @@ -248,7 +253,7 @@
return;
};

private handleError = (error: any) => {

Check warning on line 256 in libs/client/src/streaming.ts

View workflow job for this annotation

GitHub Actions / build

Unexpected any. Specify a different type
const apiError =
error instanceof ApiError
? error
Expand All @@ -267,7 +272,7 @@
this.listeners.get(type)?.push(listener);
};

private emit = (type: FalStreamEventType, event: any) => {

Check warning on line 275 in libs/client/src/streaming.ts

View workflow job for this annotation

GitHub Actions / build

Unexpected any. Specify a different type
const listeners = this.listeners.get(type) || [];
for (const listener of listeners) {
listener(event);
Expand Down
6 changes: 5 additions & 1 deletion typedoc.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@
"excludeInternal": false,
"includeVersion": true,
"githubPages": true,
"plugin": ["typedoc-plugin-mdn-links", "typedoc-plugin-extras", "typedoc-github-theme"],
"plugin": [
"typedoc-plugin-mdn-links",
"typedoc-plugin-extras",
"typedoc-github-theme"
],
"readme": "none",
"hideGenerator": true
}
Loading