fix(client): event stream response check (#89)
This commit is contained in:
parent
39089b2462
commit
762f28918d
@ -2,4 +2,5 @@
|
|||||||
|
|
||||||
/dist
|
/dist
|
||||||
/coverage
|
/coverage
|
||||||
|
/docs
|
||||||
package-lock.json
|
package-lock.json
|
||||||
|
|||||||
@ -8,6 +8,8 @@ import { storageImpl } from "./storage";
|
|||||||
|
|
||||||
export type StreamingConnectionMode = "client" | "server";
|
export type StreamingConnectionMode = "client" | "server";
|
||||||
|
|
||||||
|
const CONTENT_TYPE_EVENT_STREAM = "text/event-stream";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The stream API options. It requires the API input and also
|
* The stream API options. It requires the API input and also
|
||||||
* offers configuration options.
|
* offers configuration options.
|
||||||
@ -135,7 +137,7 @@ export class FalStream<Input, Output> {
|
|||||||
const response = await fetch(parsedUrl.toString(), {
|
const response = await fetch(parsedUrl.toString(), {
|
||||||
method: method.toUpperCase(),
|
method: method.toUpperCase(),
|
||||||
headers: {
|
headers: {
|
||||||
accept: options.accept ?? "text/event-stream",
|
accept: options.accept ?? CONTENT_TYPE_EVENT_STREAM,
|
||||||
"content-type": "application/json",
|
"content-type": "application/json",
|
||||||
},
|
},
|
||||||
body: input && method !== "get" ? JSON.stringify(input) : undefined,
|
body: input && method !== "get" ? JSON.stringify(input) : undefined,
|
||||||
@ -145,7 +147,7 @@ export class FalStream<Input, Output> {
|
|||||||
}
|
}
|
||||||
return await dispatchRequest(method.toUpperCase(), this.url, input, {
|
return await dispatchRequest(method.toUpperCase(), this.url, input, {
|
||||||
headers: {
|
headers: {
|
||||||
accept: options.accept ?? "text/event-stream",
|
accept: options.accept ?? CONTENT_TYPE_EVENT_STREAM,
|
||||||
},
|
},
|
||||||
responseHandler: this.handleResponse,
|
responseHandler: this.handleResponse,
|
||||||
signal: this.abortController.signal,
|
signal: this.abortController.signal,
|
||||||
@ -180,8 +182,11 @@ export class FalStream<Input, Output> {
|
|||||||
return;
|
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
|
// 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 reader = body.getReader();
|
||||||
const emitRawChunk = () => {
|
const emitRawChunk = () => {
|
||||||
reader.read().then(({ done, value }) => {
|
reader.read().then(({ done, value }) => {
|
||||||
|
|||||||
@ -7,7 +7,11 @@
|
|||||||
"excludeInternal": false,
|
"excludeInternal": false,
|
||||||
"includeVersion": true,
|
"includeVersion": true,
|
||||||
"githubPages": 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",
|
"readme": "none",
|
||||||
"hideGenerator": true
|
"hideGenerator": true
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user