-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathstreaming.ts
More file actions
461 lines (413 loc) · 13.5 KB
/
streaming.ts
File metadata and controls
461 lines (413 loc) · 13.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
import { createParser } from "eventsource-parser";
import { type TokenProvider, getTemporaryAuthToken } from "./auth";
import { RequiredConfig } from "./config";
import { buildUrl, dispatchRequest } from "./request";
import { ApiError, defaultResponseHandler } from "./response";
import { type StorageClient } from "./storage";
import { EndpointType, InputType, OutputType } from "./types/client";
import { ensureEndpointIdFormat, resolveEndpointPath } from "./utils";
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.
*/
export type StreamOptions<Input> = {
/**
* The endpoint URL. If not provided, it will be generated from the
* `endpointId` and the `queryParams`.
*/
readonly url?: string;
/**
* The API input payload.
*/
readonly input?: Input;
/**
* The query parameters to be sent with the request.
*/
readonly queryParams?: Record<string, string>;
/**
* The maximum time interval in milliseconds between stream chunks. Defaults to 15s.
*/
readonly timeout?: number;
/**
* Whether it should auto-upload File-like types to fal's storage
* or not.
*/
readonly autoUpload?: boolean;
/**
* The HTTP method, defaults to `post`;
*/
readonly method?: "get" | "post" | "put" | "delete" | string;
/**
* The content type the client accepts as response.
* By default this is set to `text/event-stream`.
*/
readonly accept?: string;
/**
* The streaming connection mode. This is used to determine
* whether the streaming will be done from the browser itself (client)
* or through your own server, either when running on NodeJS or when
* using a proxy that supports streaming.
*
* It defaults to `server`. Set to `client` if your server proxy doesn't
* support streaming.
*/
readonly connectionMode?: StreamingConnectionMode;
/**
* The signal to abort the request.
*/
readonly signal?: AbortSignal;
/**
* A custom token provider function. Only used when `connectionMode` is `"client"`.
* When provided, this function will be used to fetch authentication tokens
* instead of the default internal token fetching mechanism.
*/
readonly tokenProvider?: TokenProvider;
};
const EVENT_STREAM_TIMEOUT = 15 * 1000;
type FalStreamEventType = "data" | "error" | "done";
type EventHandler<T = any> = (event: T) => void;
/**
* The class representing a streaming response. With t
*/
export class FalStream<Input, Output> {
// properties
config: RequiredConfig;
endpointId: string;
url: string;
options: StreamOptions<Input>;
// support for event listeners
private listeners: Map<FalStreamEventType, EventHandler[]> = new Map();
private buffer: Output[] = [];
// local state
private currentData: Output | undefined = undefined;
private lastEventTimestamp = 0;
private streamClosed = false;
private _requestId: string | null = null;
private donePromise: Promise<Output>;
private abortController = new AbortController();
constructor(
endpointId: string,
config: RequiredConfig,
options: StreamOptions<Input>,
) {
this.endpointId = endpointId;
this.config = config;
this.url =
options.url ??
buildUrl(endpointId, {
path: resolveEndpointPath(endpointId, undefined, "/stream"),
query: options.queryParams,
});
this.options = options;
this.donePromise = new Promise<Output>((resolve, reject) => {
if (this.streamClosed) {
reject(
new ApiError({
message: "Streaming connection is already closed.",
status: 400,
body: undefined,
requestId: this._requestId || undefined,
}),
);
}
this.signal.addEventListener("abort", () => {
resolve(this.currentData ?? ({} as Output));
});
this.on("done", (data) => {
this.streamClosed = true;
resolve(data);
});
this.on("error", (error) => {
this.streamClosed = true;
reject(error);
});
});
// if a abort signal was passed, sync it with the internal one
if (options.signal) {
options.signal.addEventListener("abort", () => {
this.abortController.abort();
});
}
// start the streaming request
this.start().catch(this.handleError);
}
private start = async () => {
const { endpointId, options } = this;
const {
input,
method = "post",
connectionMode = "server",
tokenProvider,
} = options;
try {
if (connectionMode === "client") {
// if we are in the browser, we need to get a temporary token
// to authenticate the request
const appId = ensureEndpointIdFormat(endpointId);
const resolvedPath =
resolveEndpointPath(endpointId, undefined, "/stream") ?? "";
const fetchToken = tokenProvider
? () => tokenProvider(`${appId}${resolvedPath}`)
: () => {
console.warn(
"[fal.stream] Using the default token provider is deprecated. " +
'Please provide a `tokenProvider` function when using `connectionMode: "client"`. ' +
"See https://docs.fal.ai/fal-client/authentication for more information.",
);
return getTemporaryAuthToken(endpointId, this.config);
};
const token = await fetchToken();
const { fetch } = this.config;
const parsedUrl = new URL(this.url);
parsedUrl.searchParams.set("fal_jwt_token", token);
const response = await fetch(parsedUrl.toString(), {
method: method.toUpperCase(),
headers: {
accept: options.accept ?? CONTENT_TYPE_EVENT_STREAM,
"content-type": "application/json",
},
body: input && method !== "get" ? JSON.stringify(input) : undefined,
signal: this.abortController.signal,
});
this._requestId = response.headers.get("x-fal-request-id");
return await this.handleResponse(response);
}
return await dispatchRequest({
method: method.toUpperCase(),
targetUrl: this.url,
input,
config: this.config,
options: {
headers: {
accept: options.accept ?? CONTENT_TYPE_EVENT_STREAM,
},
responseHandler: async (response) => {
this._requestId = response.headers.get("x-fal-request-id");
return await this.handleResponse(response);
},
signal: this.abortController.signal,
},
});
} catch (error) {
this.handleError(error);
}
};
private handleResponse = async (response: Response) => {
if (!response.ok) {
try {
// we know the response failed, call the response handler
// so the exception gets converted to ApiError correctly
await defaultResponseHandler(response);
} catch (error) {
this.emit("error", error);
}
return;
}
const body = response.body;
if (!body) {
this.emit(
"error",
new ApiError({
message: "Response body is empty.",
status: 400,
body: undefined,
requestId: this._requestId || undefined,
}),
);
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 (!isEventStream) {
const reader = body.getReader();
const emitRawChunk = () => {
reader.read().then(({ done, value }) => {
if (done) {
this.emit("done", this.currentData);
return;
}
this.buffer.push(value as Output);
this.currentData = value as Output;
this.emit("data", value);
emitRawChunk();
});
};
emitRawChunk();
return;
}
const decoder = new TextDecoder("utf-8");
const reader = response.body.getReader();
const parser = createParser((event) => {
if (event.type === "event") {
const data = event.data;
try {
const parsedData = JSON.parse(data);
this.buffer.push(parsedData);
this.currentData = parsedData;
this.emit("data", parsedData);
// also emit 'message'for backwards compatibility
this.emit("message" as any, parsedData);
} catch (e) {
this.emit("error", e);
}
}
});
const timeout = this.options.timeout ?? EVENT_STREAM_TIMEOUT;
const readPartialResponse = async () => {
const { value, done } = await reader.read();
this.lastEventTimestamp = Date.now();
parser.feed(decoder.decode(value));
if (Date.now() - this.lastEventTimestamp > timeout) {
this.emit(
"error",
new ApiError({
message: `Event stream timed out after ${(timeout / 1000).toFixed(0)} seconds with no messages.`,
status: 408,
requestId: this._requestId || undefined,
}),
);
}
if (!done) {
readPartialResponse().catch(this.handleError);
} else {
this.emit("done", this.currentData);
}
};
readPartialResponse().catch(this.handleError);
return;
};
private handleError = (error: any) => {
// In case AbortError is thrown but the signal is marked as aborted
// it means the user called abort() and we should not emit an error
// as it's expected behavior
// See note on: https://developer.mozilla.org/en-US/docs/Web/API/AbortController/abort
if (error.name === "AbortError" || this.signal.aborted) {
return;
}
const apiError =
error instanceof ApiError
? error
: new ApiError({
message: error.message ?? "An unknown error occurred",
status: 500,
requestId: this._requestId || undefined,
});
this.emit("error", apiError);
return;
};
public on = (type: FalStreamEventType, listener: EventHandler) => {
if (!this.listeners.has(type)) {
this.listeners.set(type, []);
}
this.listeners.get(type)?.push(listener);
};
private emit = (type: FalStreamEventType, event: any) => {
const listeners = this.listeners.get(type) || [];
for (const listener of listeners) {
listener(event);
}
};
async *[Symbol.asyncIterator]() {
let running = true;
const stopAsyncIterator = () => (running = false);
this.on("error", stopAsyncIterator);
this.on("done", stopAsyncIterator);
while (running || this.buffer.length > 0) {
const data = this.buffer.shift();
if (data) {
yield data;
}
// the short timeout ensures the while loop doesn't block other
// frames getting executed concurrently
await new Promise((resolve) => setTimeout(resolve, 16));
}
}
/**
* Gets a reference to the `Promise` that indicates whether the streaming
* is done or not. Developers should always call this in their apps to ensure
* the request is over.
*
* An alternative to this, is to use `on('done')` in case your application
* architecture works best with event listeners.
*
* @returns the promise that resolves when the request is done.
*/
public done = async () => this.donePromise;
/**
* Aborts the streaming request.
*
* **Note:** This method is noop in case the request is already done.
*
* @param reason optional cause for aborting the request.
*/
public abort = (reason?: string | Error) => {
if (!this.streamClosed) {
this.abortController.abort(reason);
}
};
/**
* Gets the `AbortSignal` instance that can be used to listen for abort events.
*
* **Note:** this signal is internal to the `FalStream` instance. If you pass your
* own abort signal, the `FalStream` will listen to it and abort it appropriately.
*
* @returns the `AbortSignal` instance.
* @see https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal
*/
public get signal() {
return this.abortController.signal;
}
/**
* Gets the request id of the streaming request.
*
* @returns the request id.
*/
public get requestId() {
return this._requestId;
}
}
/**
* The streaming client interface.
*/
export interface StreamingClient {
/**
* Calls a fal app that supports streaming and provides a streaming-capable
* object as a result, that can be used to get partial results through either
* `AsyncIterator` or through an event listener.
*
* @param endpointId the endpoint id, e.g. `fal-ai/llavav15-13b`.
* @param options the request options, including the input payload.
* @returns the `FalStream` instance.
*/
stream<Id extends EndpointType>(
endpointId: Id,
options: StreamOptions<InputType<Id>>,
): Promise<FalStream<InputType<Id>, OutputType<Id>>>;
}
type StreamingClientDependencies = {
config: RequiredConfig;
storage: StorageClient;
};
export function createStreamingClient({
config,
storage,
}: StreamingClientDependencies): StreamingClient {
return {
async stream<Id extends EndpointType>(
endpointId: Id,
options: StreamOptions<InputType<Id>>,
) {
const input = options.input
? await storage.transformInput(options.input)
: undefined;
return new FalStream<InputType<Id>, OutputType<Id>>(endpointId, config, {
...options,
input: input as InputType<Id>,
});
},
};
}