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

Support streaming streaming responses for callable functions. #8609

Draft
wants to merge 22 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 10 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
22 changes: 19 additions & 3 deletions common/api-review/functions.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,16 @@ export type FunctionsErrorCodeCore = 'ok' | 'cancelled' | 'unknown' | 'invalid-a
export function getFunctions(app?: FirebaseApp, regionOrCustomDomain?: string): Functions;

// @public
export type HttpsCallable<RequestData = unknown, ResponseData = unknown> = (data?: RequestData | null) => Promise<HttpsCallableResult<ResponseData>>;
export type HttpsCallable<RequestData = unknown, ResponseData = unknown, StreamData = unknown> = {
(data?: RequestData | null): Promise<HttpsCallableResult<ResponseData>>;
stream: (data?: RequestData | null, options?: HttpsCallableStreamOptions) => Promise<HttpsCallableStreamResult<ResponseData, StreamData>>;
};

// @public
export function httpsCallable<RequestData = unknown, ResponseData = unknown>(functionsInstance: Functions, name: string, options?: HttpsCallableOptions): HttpsCallable<RequestData, ResponseData>;
export function httpsCallable<RequestData = unknown, ResponseData = unknown, StreamData = unknown>(functionsInstance: Functions, name: string, options?: HttpsCallableOptions): HttpsCallable<RequestData, ResponseData, StreamData>;

// @public
export function httpsCallableFromURL<RequestData = unknown, ResponseData = unknown>(functionsInstance: Functions, url: string, options?: HttpsCallableOptions): HttpsCallable<RequestData, ResponseData>;
export function httpsCallableFromURL<RequestData = unknown, ResponseData = unknown, StreamData = unknown>(functionsInstance: Functions, url: string, options?: HttpsCallableOptions): HttpsCallable<RequestData, ResponseData, StreamData>;

// @public
export interface HttpsCallableOptions {
Expand All @@ -54,5 +57,18 @@ export interface HttpsCallableResult<ResponseData = unknown> {
readonly data: ResponseData;
}

// @public
export interface HttpsCallableStreamOptions {
signal?: AbortSignal;
}

// @public
export interface HttpsCallableStreamResult<ResponseData = unknown, StreamData = unknown> {
// (undocumented)
readonly data: Promise<ResponseData>;
// (undocumented)
readonly stream: AsyncIterable<StreamData>;
}


```
9 changes: 9 additions & 0 deletions packages/functions-types/index.d.ts
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we actually need to touch functions-types - these -types packages are normally used only for compat. Sometimes the modular packages do import from the types packages but it doesn't look like functions does.

Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,21 @@ export interface HttpsCallableResult {
readonly data: any;
}

/**
* An HttpsCallableStreamResult wraps a single streaming result from a function call.
*/
export interface HttpsCallableStreamResult {
readonly data: Promise<any>;
readonly stream: AsyncIterable<any>;
}

/**
* An HttpsCallable is a reference to a "callable" http trigger in
* Google Cloud Functions.
*/
export interface HttpsCallable {
(data?: {} | null): Promise<HttpsCallableResult>;
stream(data?: {} | null): Promise<HttpsCallableStreamResult>;
}

/**
Expand Down
3 changes: 2 additions & 1 deletion packages/functions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
"test:browser": "karma start",
"test:browser:debug": "karma start --browsers=Chrome --auto-watch",
"test:node": "TS_NODE_COMPILER_OPTIONS='{\"module\":\"commonjs\"}' nyc --reporter lcovonly -- mocha 'src/{,!(browser)/**/}*.test.ts' --file src/index.ts --config ../../config/mocharc.node.js",
"test:emulator": "env FIREBASE_FUNCTIONS_EMULATOR_ORIGIN=http://localhost:5005 run-p --npm-path npm test:node",
"test:emulator": "env FIREBASE_FUNCTIONS_EMULATOR_ORIGIN=http://127.0.0.1:5005 run-p test:node",
"test:emulator": "env FIREBASE_FUNCTIONS_EMULATOR_ORIGIN=http://127.0.0.1:5005 run-p --npm-path npm test:node",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is my github diff wonky or are these two lines with the same script name?

"trusted-type-check": "tsec -p tsconfig.json --noEmit",
"api-report": "api-extractor run --local --verbose",
"doc": "api-documenter markdown --input temp --output docs",
Expand Down
13 changes: 7 additions & 6 deletions packages/functions/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,12 @@ export function connectFunctionsEmulator(
* @param name - The name of the trigger.
* @public
*/
export function httpsCallable<RequestData = unknown, ResponseData = unknown>(
export function httpsCallable<RequestData = unknown, ResponseData = unknown, StreamData = unknown>(
functionsInstance: Functions,
name: string,
options?: HttpsCallableOptions
): HttpsCallable<RequestData, ResponseData> {
return _httpsCallable<RequestData, ResponseData>(
): HttpsCallable<RequestData, ResponseData, StreamData> {
return _httpsCallable<RequestData, ResponseData, StreamData>(
getModularInstance<FunctionsService>(functionsInstance as FunctionsService),
name,
options
Expand All @@ -107,13 +107,14 @@ export function httpsCallable<RequestData = unknown, ResponseData = unknown>(
*/
export function httpsCallableFromURL<
RequestData = unknown,
ResponseData = unknown
ResponseData = unknown,
StreamData = unknown,
>(
functionsInstance: Functions,
url: string,
options?: HttpsCallableOptions
): HttpsCallable<RequestData, ResponseData> {
return _httpsCallableFromURL<RequestData, ResponseData>(
): HttpsCallable<RequestData, ResponseData, StreamData> {
return _httpsCallableFromURL<RequestData, ResponseData, StreamData>(
getModularInstance<FunctionsService>(functionsInstance as FunctionsService),
url,
options
Expand Down
Loading
Loading