-
Notifications
You must be signed in to change notification settings - Fork 898
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support streaming streaming responses for callable functions. (#8609)
The new .stream() API allows the client to consume streaming responses from the WIP streaming callable functions in Firebase Functions Node.js SDK. When client makes a request to the callable function w/ header Accept: text/event-stream, the callable function responds with response chunks in Server-Sent Event format. The sdk changes here abstracts over the wire-protocol by parsing the response chunks and returning an instance of a AsyncIterable to consume to data: import { getFunctions, httpsCallable } from "firebase/functions"; const functions = getFunctions(); const generateText = httpsCallable(functions, 'generateText'); const resp = await generateText.stream( { text: 'What is your favorite Firebase service and why?' }, { signal: AbortSignal.timeout(60_000) }, ); try { for await (const message of resp.stream) { console.log(message); // prints "foo", "bar" } console.log(await resp.data) // prints "foo bar" } catch (e) { // FirebaseError(code='cancelled', message='Request was cancelled.'); console.error(e) }
- Loading branch information
Showing
14 changed files
with
918 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
'@firebase/functions': minor | ||
'firebase': minor | ||
--- | ||
|
||
Add `.stream()` api for callable functions for consuming streaming responses. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# Uncomment this if you'd like others to create their own Firebase project. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
Project: /docs/reference/js/_project.yaml | ||
Book: /docs/reference/_book.yaml | ||
page_type: reference | ||
|
||
{% comment %} | ||
DO NOT EDIT THIS FILE! | ||
This is generated by the JS SDK team, and any local changes will be | ||
overwritten. Changes should be made in the source code at | ||
https://github.com/firebase/firebase-js-sdk | ||
{% endcomment %} | ||
|
||
# HttpsCallable interface | ||
A reference to a "callable" HTTP trigger in Cloud Functions. | ||
|
||
<b>Signature:</b> | ||
|
||
```typescript | ||
export interface HttpsCallable<RequestData = unknown, ResponseData = unknown, StreamData = unknown> | ||
``` | ||
|
||
## Properties | ||
|
||
| Property | Type | Description | | ||
| --- | --- | --- | | ||
| [stream](./functions.httpscallable.md#httpscallablestream) | (data?: RequestData \| null, options?: [HttpsCallableStreamOptions](./functions.httpscallablestreamoptions.md#httpscallablestreamoptions_interface)<!-- -->) => Promise<[HttpsCallableStreamResult](./functions.httpscallablestreamresult.md#httpscallablestreamresult_interface)<!-- --><ResponseData, StreamData>> | | | ||
|
||
## HttpsCallable.stream | ||
|
||
<b>Signature:</b> | ||
|
||
```typescript | ||
stream: (data?: RequestData | null, options?: HttpsCallableStreamOptions) => Promise<HttpsCallableStreamResult<ResponseData, StreamData>>; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
Project: /docs/reference/js/_project.yaml | ||
Book: /docs/reference/_book.yaml | ||
page_type: reference | ||
|
||
{% comment %} | ||
DO NOT EDIT THIS FILE! | ||
This is generated by the JS SDK team, and any local changes will be | ||
overwritten. Changes should be made in the source code at | ||
https://github.com/firebase/firebase-js-sdk | ||
{% endcomment %} | ||
|
||
# HttpsCallableStreamOptions interface | ||
An interface for metadata about how a stream call should be executed. | ||
|
||
<b>Signature:</b> | ||
|
||
```typescript | ||
export interface HttpsCallableStreamOptions | ||
``` | ||
|
||
## Properties | ||
|
||
| Property | Type | Description | | ||
| --- | --- | --- | | ||
| [limitedUseAppCheckTokens](./functions.httpscallablestreamoptions.md#httpscallablestreamoptionslimiteduseappchecktokens) | boolean | If set to true, uses a limited-use App Check token for callable function requests from this instance of [Functions](./functions.functions.md#functions_interface)<!-- -->. You must use limited-use tokens to call functions with replay protection enabled. By default, this is false. | | ||
| [signal](./functions.httpscallablestreamoptions.md#httpscallablestreamoptionssignal) | AbortSignal | An <code>AbortSignal</code> that can be used to cancel the streaming response. When the signal is aborted, the underlying HTTP connection will be terminated. | | ||
|
||
## HttpsCallableStreamOptions.limitedUseAppCheckTokens | ||
|
||
If set to true, uses a limited-use App Check token for callable function requests from this instance of [Functions](./functions.functions.md#functions_interface)<!-- -->. You must use limited-use tokens to call functions with replay protection enabled. By default, this is false. | ||
|
||
<b>Signature:</b> | ||
|
||
```typescript | ||
limitedUseAppCheckTokens?: boolean; | ||
``` | ||
|
||
## HttpsCallableStreamOptions.signal | ||
|
||
An `AbortSignal` that can be used to cancel the streaming response. When the signal is aborted, the underlying HTTP connection will be terminated. | ||
|
||
<b>Signature:</b> | ||
|
||
```typescript | ||
signal?: AbortSignal; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
Project: /docs/reference/js/_project.yaml | ||
Book: /docs/reference/_book.yaml | ||
page_type: reference | ||
|
||
{% comment %} | ||
DO NOT EDIT THIS FILE! | ||
This is generated by the JS SDK team, and any local changes will be | ||
overwritten. Changes should be made in the source code at | ||
https://github.com/firebase/firebase-js-sdk | ||
{% endcomment %} | ||
|
||
# HttpsCallableStreamResult interface | ||
An `HttpsCallableStreamResult` wraps a single streaming result from a function call. | ||
|
||
<b>Signature:</b> | ||
|
||
```typescript | ||
export interface HttpsCallableStreamResult<ResponseData = unknown, StreamData = unknown> | ||
``` | ||
|
||
## Properties | ||
|
||
| Property | Type | Description | | ||
| --- | --- | --- | | ||
| [data](./functions.httpscallablestreamresult.md#httpscallablestreamresultdata) | Promise<ResponseData> | | | ||
| [stream](./functions.httpscallablestreamresult.md#httpscallablestreamresultstream) | AsyncIterable<StreamData> | | | ||
|
||
## HttpsCallableStreamResult.data | ||
|
||
<b>Signature:</b> | ||
|
||
```typescript | ||
readonly data: Promise<ResponseData>; | ||
``` | ||
|
||
## HttpsCallableStreamResult.stream | ||
|
||
<b>Signature:</b> | ||
|
||
```typescript | ||
readonly stream: AsyncIterable<StreamData>; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.