@firebase/functions 0.11.10 → 0.12.0-20241210191049
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/esm/index.esm2017.js +244 -22
- package/dist/esm/index.esm2017.js.map +1 -1
- package/dist/esm/src/api.d.ts +2 -2
- package/dist/esm/src/public-types.d.ts +31 -3
- package/dist/esm/src/service.d.ts +4 -3
- package/dist/functions-public.d.ts +35 -5
- package/dist/functions.d.ts +35 -5
- package/dist/index.cjs.js +244 -22
- package/dist/index.cjs.js.map +1 -1
- package/dist/src/api.d.ts +2 -2
- package/dist/src/public-types.d.ts +31 -3
- package/dist/src/service.d.ts +4 -3
- package/package.json +2 -2
|
@@ -26,11 +26,22 @@ export interface HttpsCallableResult<ResponseData = unknown> {
|
|
|
26
26
|
readonly data: ResponseData;
|
|
27
27
|
}
|
|
28
28
|
/**
|
|
29
|
-
*
|
|
29
|
+
* An `HttpsCallableStreamResult` wraps a single streaming result from a function call.
|
|
30
|
+
* @public
|
|
31
|
+
*/
|
|
32
|
+
export interface HttpsCallableStreamResult<ResponseData = unknown, StreamData = unknown> {
|
|
33
|
+
readonly data: Promise<ResponseData>;
|
|
34
|
+
readonly stream: AsyncIterable<StreamData>;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* A reference to a "callable" HTTP trigger in Cloud Functions.
|
|
30
38
|
* @param data - Data to be passed to callable function.
|
|
31
39
|
* @public
|
|
32
40
|
*/
|
|
33
|
-
export
|
|
41
|
+
export interface HttpsCallable<RequestData = unknown, ResponseData = unknown, StreamData = unknown> {
|
|
42
|
+
(data?: RequestData | null): Promise<HttpsCallableResult<ResponseData>>;
|
|
43
|
+
stream: (data?: RequestData | null, options?: HttpsCallableStreamOptions) => Promise<HttpsCallableStreamResult<ResponseData, StreamData>>;
|
|
44
|
+
}
|
|
34
45
|
/**
|
|
35
46
|
* An interface for metadata about how calls should be executed.
|
|
36
47
|
* @public
|
|
@@ -42,7 +53,24 @@ export interface HttpsCallableOptions {
|
|
|
42
53
|
*/
|
|
43
54
|
timeout?: number;
|
|
44
55
|
/**
|
|
45
|
-
* If set to true, uses limited-use App Check token for callable function requests from this
|
|
56
|
+
* If set to true, uses a limited-use App Check token for callable function requests from this
|
|
57
|
+
* instance of {@link Functions}. You must use limited-use tokens to call functions with
|
|
58
|
+
* replay protection enabled. By default, this is false.
|
|
59
|
+
*/
|
|
60
|
+
limitedUseAppCheckTokens?: boolean;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* An interface for metadata about how a stream call should be executed.
|
|
64
|
+
* @public
|
|
65
|
+
*/
|
|
66
|
+
export interface HttpsCallableStreamOptions {
|
|
67
|
+
/**
|
|
68
|
+
* An `AbortSignal` that can be used to cancel the streaming response. When the signal is aborted,
|
|
69
|
+
* the underlying HTTP connection will be terminated.
|
|
70
|
+
*/
|
|
71
|
+
signal?: AbortSignal;
|
|
72
|
+
/**
|
|
73
|
+
* If set to true, uses a limited-use App Check token for callable function requests from this
|
|
46
74
|
* instance of {@link Functions}. You must use limited-use tokens to call functions with
|
|
47
75
|
* replay protection enabled. By default, this is false.
|
|
48
76
|
*/
|
|
@@ -42,6 +42,7 @@ export interface HttpResponseBody {
|
|
|
42
42
|
*/
|
|
43
43
|
export declare class FunctionsService implements _FirebaseService {
|
|
44
44
|
readonly app: FirebaseApp;
|
|
45
|
+
readonly fetchImpl: typeof fetch;
|
|
45
46
|
readonly contextProvider: ContextProvider;
|
|
46
47
|
emulatorOrigin: string | null;
|
|
47
48
|
cancelAllRequests: Promise<void>;
|
|
@@ -52,7 +53,7 @@ export declare class FunctionsService implements _FirebaseService {
|
|
|
52
53
|
* Creates a new Functions service for the given app.
|
|
53
54
|
* @param app - The FirebaseApp to use.
|
|
54
55
|
*/
|
|
55
|
-
constructor(app: FirebaseApp, authProvider: Provider<FirebaseAuthInternalName>, messagingProvider: Provider<MessagingInternalComponentName>, appCheckProvider: Provider<AppCheckInternalComponentName>, regionOrCustomDomain?: string);
|
|
56
|
+
constructor(app: FirebaseApp, authProvider: Provider<FirebaseAuthInternalName>, messagingProvider: Provider<MessagingInternalComponentName>, appCheckProvider: Provider<AppCheckInternalComponentName>, regionOrCustomDomain?: string, fetchImpl?: typeof fetch);
|
|
56
57
|
_delete(): Promise<void>;
|
|
57
58
|
/**
|
|
58
59
|
* Returns the URL for a callable with the given name.
|
|
@@ -76,10 +77,10 @@ export declare function connectFunctionsEmulator(functionsInstance: FunctionsSer
|
|
|
76
77
|
* @param name - The name of the trigger.
|
|
77
78
|
* @public
|
|
78
79
|
*/
|
|
79
|
-
export declare function httpsCallable<RequestData, ResponseData>(functionsInstance: FunctionsService, name: string, options?: HttpsCallableOptions): HttpsCallable<RequestData, ResponseData>;
|
|
80
|
+
export declare function httpsCallable<RequestData, ResponseData, StreamData = unknown>(functionsInstance: FunctionsService, name: string, options?: HttpsCallableOptions): HttpsCallable<RequestData, ResponseData, StreamData>;
|
|
80
81
|
/**
|
|
81
82
|
* Returns a reference to the callable https trigger with the given url.
|
|
82
83
|
* @param url - The url of the trigger.
|
|
83
84
|
* @public
|
|
84
85
|
*/
|
|
85
|
-
export declare function httpsCallableFromURL<RequestData, ResponseData>(functionsInstance: FunctionsService, url: string, options?: HttpsCallableOptions): HttpsCallable<RequestData, ResponseData>;
|
|
86
|
+
export declare function httpsCallableFromURL<RequestData, ResponseData, StreamData = unknown>(functionsInstance: FunctionsService, url: string, options?: HttpsCallableOptions): HttpsCallable<RequestData, ResponseData, StreamData>;
|
|
@@ -126,25 +126,28 @@ export declare type FunctionsErrorCodeCore = 'ok' | 'cancelled' | 'unknown' | 'i
|
|
|
126
126
|
export declare function getFunctions(app?: FirebaseApp, regionOrCustomDomain?: string): Functions;
|
|
127
127
|
|
|
128
128
|
/**
|
|
129
|
-
* A reference to a "callable" HTTP trigger in
|
|
129
|
+
* A reference to a "callable" HTTP trigger in Cloud Functions.
|
|
130
130
|
* @param data - Data to be passed to callable function.
|
|
131
131
|
* @public
|
|
132
132
|
*/
|
|
133
|
-
export declare
|
|
133
|
+
export declare interface HttpsCallable<RequestData = unknown, ResponseData = unknown, StreamData = unknown> {
|
|
134
|
+
(data?: RequestData | null): Promise<HttpsCallableResult<ResponseData>>;
|
|
135
|
+
stream: (data?: RequestData | null, options?: HttpsCallableStreamOptions) => Promise<HttpsCallableStreamResult<ResponseData, StreamData>>;
|
|
136
|
+
}
|
|
134
137
|
|
|
135
138
|
/**
|
|
136
139
|
* Returns a reference to the callable HTTPS trigger with the given name.
|
|
137
140
|
* @param name - The name of the trigger.
|
|
138
141
|
* @public
|
|
139
142
|
*/
|
|
140
|
-
export declare function httpsCallable<RequestData = unknown, ResponseData = unknown>(functionsInstance: Functions, name: string, options?: HttpsCallableOptions): HttpsCallable<RequestData, ResponseData>;
|
|
143
|
+
export declare function httpsCallable<RequestData = unknown, ResponseData = unknown, StreamData = unknown>(functionsInstance: Functions, name: string, options?: HttpsCallableOptions): HttpsCallable<RequestData, ResponseData, StreamData>;
|
|
141
144
|
|
|
142
145
|
/**
|
|
143
146
|
* Returns a reference to the callable HTTPS trigger with the specified url.
|
|
144
147
|
* @param url - The url of the trigger.
|
|
145
148
|
* @public
|
|
146
149
|
*/
|
|
147
|
-
export declare function httpsCallableFromURL<RequestData = unknown, ResponseData = unknown>(functionsInstance: Functions, url: string, options?: HttpsCallableOptions): HttpsCallable<RequestData, ResponseData>;
|
|
150
|
+
export declare function httpsCallableFromURL<RequestData = unknown, ResponseData = unknown, StreamData = unknown>(functionsInstance: Functions, url: string, options?: HttpsCallableOptions): HttpsCallable<RequestData, ResponseData, StreamData>;
|
|
148
151
|
|
|
149
152
|
/**
|
|
150
153
|
* An interface for metadata about how calls should be executed.
|
|
@@ -157,7 +160,7 @@ export declare interface HttpsCallableOptions {
|
|
|
157
160
|
*/
|
|
158
161
|
timeout?: number;
|
|
159
162
|
/**
|
|
160
|
-
* If set to true, uses limited-use App Check token for callable function requests from this
|
|
163
|
+
* If set to true, uses a limited-use App Check token for callable function requests from this
|
|
161
164
|
* instance of {@link Functions}. You must use limited-use tokens to call functions with
|
|
162
165
|
* replay protection enabled. By default, this is false.
|
|
163
166
|
*/
|
|
@@ -175,4 +178,31 @@ export declare interface HttpsCallableResult<ResponseData = unknown> {
|
|
|
175
178
|
readonly data: ResponseData;
|
|
176
179
|
}
|
|
177
180
|
|
|
181
|
+
/**
|
|
182
|
+
* An interface for metadata about how a stream call should be executed.
|
|
183
|
+
* @public
|
|
184
|
+
*/
|
|
185
|
+
export declare interface HttpsCallableStreamOptions {
|
|
186
|
+
/**
|
|
187
|
+
* An `AbortSignal` that can be used to cancel the streaming response. When the signal is aborted,
|
|
188
|
+
* the underlying HTTP connection will be terminated.
|
|
189
|
+
*/
|
|
190
|
+
signal?: AbortSignal;
|
|
191
|
+
/**
|
|
192
|
+
* If set to true, uses a limited-use App Check token for callable function requests from this
|
|
193
|
+
* instance of {@link Functions}. You must use limited-use tokens to call functions with
|
|
194
|
+
* replay protection enabled. By default, this is false.
|
|
195
|
+
*/
|
|
196
|
+
limitedUseAppCheckTokens?: boolean;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* An `HttpsCallableStreamResult` wraps a single streaming result from a function call.
|
|
201
|
+
* @public
|
|
202
|
+
*/
|
|
203
|
+
export declare interface HttpsCallableStreamResult<ResponseData = unknown, StreamData = unknown> {
|
|
204
|
+
readonly data: Promise<ResponseData>;
|
|
205
|
+
readonly stream: AsyncIterable<StreamData>;
|
|
206
|
+
}
|
|
207
|
+
|
|
178
208
|
export { }
|
package/dist/functions.d.ts
CHANGED
|
@@ -126,25 +126,28 @@ export declare type FunctionsErrorCodeCore = 'ok' | 'cancelled' | 'unknown' | 'i
|
|
|
126
126
|
export declare function getFunctions(app?: FirebaseApp, regionOrCustomDomain?: string): Functions;
|
|
127
127
|
|
|
128
128
|
/**
|
|
129
|
-
* A reference to a "callable" HTTP trigger in
|
|
129
|
+
* A reference to a "callable" HTTP trigger in Cloud Functions.
|
|
130
130
|
* @param data - Data to be passed to callable function.
|
|
131
131
|
* @public
|
|
132
132
|
*/
|
|
133
|
-
export declare
|
|
133
|
+
export declare interface HttpsCallable<RequestData = unknown, ResponseData = unknown, StreamData = unknown> {
|
|
134
|
+
(data?: RequestData | null): Promise<HttpsCallableResult<ResponseData>>;
|
|
135
|
+
stream: (data?: RequestData | null, options?: HttpsCallableStreamOptions) => Promise<HttpsCallableStreamResult<ResponseData, StreamData>>;
|
|
136
|
+
}
|
|
134
137
|
|
|
135
138
|
/**
|
|
136
139
|
* Returns a reference to the callable HTTPS trigger with the given name.
|
|
137
140
|
* @param name - The name of the trigger.
|
|
138
141
|
* @public
|
|
139
142
|
*/
|
|
140
|
-
export declare function httpsCallable<RequestData = unknown, ResponseData = unknown>(functionsInstance: Functions, name: string, options?: HttpsCallableOptions): HttpsCallable<RequestData, ResponseData>;
|
|
143
|
+
export declare function httpsCallable<RequestData = unknown, ResponseData = unknown, StreamData = unknown>(functionsInstance: Functions, name: string, options?: HttpsCallableOptions): HttpsCallable<RequestData, ResponseData, StreamData>;
|
|
141
144
|
|
|
142
145
|
/**
|
|
143
146
|
* Returns a reference to the callable HTTPS trigger with the specified url.
|
|
144
147
|
* @param url - The url of the trigger.
|
|
145
148
|
* @public
|
|
146
149
|
*/
|
|
147
|
-
export declare function httpsCallableFromURL<RequestData = unknown, ResponseData = unknown>(functionsInstance: Functions, url: string, options?: HttpsCallableOptions): HttpsCallable<RequestData, ResponseData>;
|
|
150
|
+
export declare function httpsCallableFromURL<RequestData = unknown, ResponseData = unknown, StreamData = unknown>(functionsInstance: Functions, url: string, options?: HttpsCallableOptions): HttpsCallable<RequestData, ResponseData, StreamData>;
|
|
148
151
|
|
|
149
152
|
/**
|
|
150
153
|
* An interface for metadata about how calls should be executed.
|
|
@@ -157,7 +160,7 @@ export declare interface HttpsCallableOptions {
|
|
|
157
160
|
*/
|
|
158
161
|
timeout?: number;
|
|
159
162
|
/**
|
|
160
|
-
* If set to true, uses limited-use App Check token for callable function requests from this
|
|
163
|
+
* If set to true, uses a limited-use App Check token for callable function requests from this
|
|
161
164
|
* instance of {@link Functions}. You must use limited-use tokens to call functions with
|
|
162
165
|
* replay protection enabled. By default, this is false.
|
|
163
166
|
*/
|
|
@@ -175,4 +178,31 @@ export declare interface HttpsCallableResult<ResponseData = unknown> {
|
|
|
175
178
|
readonly data: ResponseData;
|
|
176
179
|
}
|
|
177
180
|
|
|
181
|
+
/**
|
|
182
|
+
* An interface for metadata about how a stream call should be executed.
|
|
183
|
+
* @public
|
|
184
|
+
*/
|
|
185
|
+
export declare interface HttpsCallableStreamOptions {
|
|
186
|
+
/**
|
|
187
|
+
* An `AbortSignal` that can be used to cancel the streaming response. When the signal is aborted,
|
|
188
|
+
* the underlying HTTP connection will be terminated.
|
|
189
|
+
*/
|
|
190
|
+
signal?: AbortSignal;
|
|
191
|
+
/**
|
|
192
|
+
* If set to true, uses a limited-use App Check token for callable function requests from this
|
|
193
|
+
* instance of {@link Functions}. You must use limited-use tokens to call functions with
|
|
194
|
+
* replay protection enabled. By default, this is false.
|
|
195
|
+
*/
|
|
196
|
+
limitedUseAppCheckTokens?: boolean;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* An `HttpsCallableStreamResult` wraps a single streaming result from a function call.
|
|
201
|
+
* @public
|
|
202
|
+
*/
|
|
203
|
+
export declare interface HttpsCallableStreamResult<ResponseData = unknown, StreamData = unknown> {
|
|
204
|
+
readonly data: Promise<ResponseData>;
|
|
205
|
+
readonly stream: AsyncIterable<StreamData>;
|
|
206
|
+
}
|
|
207
|
+
|
|
178
208
|
export { }
|
package/dist/index.cjs.js
CHANGED
|
@@ -403,6 +403,7 @@ class ContextProvider {
|
|
|
403
403
|
* limitations under the License.
|
|
404
404
|
*/
|
|
405
405
|
const DEFAULT_REGION = 'us-central1';
|
|
406
|
+
const responseLineRE = /^data: (.*?)(?:\n|$)/;
|
|
406
407
|
/**
|
|
407
408
|
* Returns a Promise that will be rejected after the given duration.
|
|
408
409
|
* The error will be of type FunctionsError.
|
|
@@ -436,8 +437,9 @@ class FunctionsService {
|
|
|
436
437
|
* Creates a new Functions service for the given app.
|
|
437
438
|
* @param app - The FirebaseApp to use.
|
|
438
439
|
*/
|
|
439
|
-
constructor(app, authProvider, messagingProvider, appCheckProvider, regionOrCustomDomain = DEFAULT_REGION) {
|
|
440
|
+
constructor(app, authProvider, messagingProvider, appCheckProvider, regionOrCustomDomain = DEFAULT_REGION, fetchImpl = (...args) => fetch(...args)) {
|
|
440
441
|
this.app = app;
|
|
442
|
+
this.fetchImpl = fetchImpl;
|
|
441
443
|
this.emulatorOrigin = null;
|
|
442
444
|
this.contextProvider = new ContextProvider(authProvider, messagingProvider, appCheckProvider);
|
|
443
445
|
// Cancels all ongoing requests when resolved.
|
|
@@ -496,9 +498,13 @@ function connectFunctionsEmulator$1(functionsInstance, host, port) {
|
|
|
496
498
|
* @public
|
|
497
499
|
*/
|
|
498
500
|
function httpsCallable$1(functionsInstance, name, options) {
|
|
499
|
-
|
|
501
|
+
const callable = (data) => {
|
|
500
502
|
return call(functionsInstance, name, data, options || {});
|
|
501
|
-
}
|
|
503
|
+
};
|
|
504
|
+
callable.stream = (data, options) => {
|
|
505
|
+
return stream(functionsInstance, name, data, options);
|
|
506
|
+
};
|
|
507
|
+
return callable;
|
|
502
508
|
}
|
|
503
509
|
/**
|
|
504
510
|
* Returns a reference to the callable https trigger with the given url.
|
|
@@ -506,9 +512,13 @@ function httpsCallable$1(functionsInstance, name, options) {
|
|
|
506
512
|
* @public
|
|
507
513
|
*/
|
|
508
514
|
function httpsCallableFromURL$1(functionsInstance, url, options) {
|
|
509
|
-
|
|
515
|
+
const callable = (data) => {
|
|
510
516
|
return callAtURL(functionsInstance, url, data, options || {});
|
|
511
|
-
}
|
|
517
|
+
};
|
|
518
|
+
callable.stream = (data, options) => {
|
|
519
|
+
return streamAtURL(functionsInstance, url, data, options || {});
|
|
520
|
+
};
|
|
521
|
+
return callable;
|
|
512
522
|
}
|
|
513
523
|
/**
|
|
514
524
|
* Does an HTTP POST and returns the completed response.
|
|
@@ -517,11 +527,11 @@ function httpsCallableFromURL$1(functionsInstance, url, options) {
|
|
|
517
527
|
* @param headers The HTTP headers to include in the request.
|
|
518
528
|
* @return A Promise that will succeed when the request finishes.
|
|
519
529
|
*/
|
|
520
|
-
async function postJSON(url, body, headers) {
|
|
530
|
+
async function postJSON(url, body, headers, fetchImpl) {
|
|
521
531
|
headers['Content-Type'] = 'application/json';
|
|
522
532
|
let response;
|
|
523
533
|
try {
|
|
524
|
-
response = await
|
|
534
|
+
response = await fetchImpl(url, {
|
|
525
535
|
method: 'POST',
|
|
526
536
|
body: JSON.stringify(body),
|
|
527
537
|
headers
|
|
@@ -549,10 +559,30 @@ async function postJSON(url, body, headers) {
|
|
|
549
559
|
json
|
|
550
560
|
};
|
|
551
561
|
}
|
|
562
|
+
/**
|
|
563
|
+
* Creates authorization headers for Firebase Functions requests.
|
|
564
|
+
* @param functionsInstance The Firebase Functions service instance.
|
|
565
|
+
* @param options Options for the callable function, including AppCheck token settings.
|
|
566
|
+
* @return A Promise that resolves a headers map to include in outgoing fetch request.
|
|
567
|
+
*/
|
|
568
|
+
async function makeAuthHeaders(functionsInstance, options) {
|
|
569
|
+
const headers = {};
|
|
570
|
+
const context = await functionsInstance.contextProvider.getContext(options.limitedUseAppCheckTokens);
|
|
571
|
+
if (context.authToken) {
|
|
572
|
+
headers['Authorization'] = 'Bearer ' + context.authToken;
|
|
573
|
+
}
|
|
574
|
+
if (context.messagingToken) {
|
|
575
|
+
headers['Firebase-Instance-ID-Token'] = context.messagingToken;
|
|
576
|
+
}
|
|
577
|
+
if (context.appCheckToken !== null) {
|
|
578
|
+
headers['X-Firebase-AppCheck'] = context.appCheckToken;
|
|
579
|
+
}
|
|
580
|
+
return headers;
|
|
581
|
+
}
|
|
552
582
|
/**
|
|
553
583
|
* Calls a callable function asynchronously and returns the result.
|
|
554
584
|
* @param name The name of the callable trigger.
|
|
555
|
-
* @param data The data to pass as params to the function.
|
|
585
|
+
* @param data The data to pass as params to the function.
|
|
556
586
|
*/
|
|
557
587
|
function call(functionsInstance, name, data, options) {
|
|
558
588
|
const url = functionsInstance._url(name);
|
|
@@ -561,29 +591,19 @@ function call(functionsInstance, name, data, options) {
|
|
|
561
591
|
/**
|
|
562
592
|
* Calls a callable function asynchronously and returns the result.
|
|
563
593
|
* @param url The url of the callable trigger.
|
|
564
|
-
* @param data The data to pass as params to the function.
|
|
594
|
+
* @param data The data to pass as params to the function.
|
|
565
595
|
*/
|
|
566
596
|
async function callAtURL(functionsInstance, url, data, options) {
|
|
567
597
|
// Encode any special types, such as dates, in the input data.
|
|
568
598
|
data = encode(data);
|
|
569
599
|
const body = { data };
|
|
570
600
|
// Add a header for the authToken.
|
|
571
|
-
const headers =
|
|
572
|
-
const context = await functionsInstance.contextProvider.getContext(options.limitedUseAppCheckTokens);
|
|
573
|
-
if (context.authToken) {
|
|
574
|
-
headers['Authorization'] = 'Bearer ' + context.authToken;
|
|
575
|
-
}
|
|
576
|
-
if (context.messagingToken) {
|
|
577
|
-
headers['Firebase-Instance-ID-Token'] = context.messagingToken;
|
|
578
|
-
}
|
|
579
|
-
if (context.appCheckToken !== null) {
|
|
580
|
-
headers['X-Firebase-AppCheck'] = context.appCheckToken;
|
|
581
|
-
}
|
|
601
|
+
const headers = await makeAuthHeaders(functionsInstance, options);
|
|
582
602
|
// Default timeout to 70s, but let the options override it.
|
|
583
603
|
const timeout = options.timeout || 70000;
|
|
584
604
|
const failAfterHandle = failAfter(timeout);
|
|
585
605
|
const response = await Promise.race([
|
|
586
|
-
postJSON(url, body, headers),
|
|
606
|
+
postJSON(url, body, headers, functionsInstance.fetchImpl),
|
|
587
607
|
failAfterHandle.promise,
|
|
588
608
|
functionsInstance.cancelAllRequests
|
|
589
609
|
]);
|
|
@@ -615,9 +635,211 @@ async function callAtURL(functionsInstance, url, data, options) {
|
|
|
615
635
|
const decodedData = decode(responseData);
|
|
616
636
|
return { data: decodedData };
|
|
617
637
|
}
|
|
638
|
+
/**
|
|
639
|
+
* Calls a callable function asynchronously and returns a streaming result.
|
|
640
|
+
* @param name The name of the callable trigger.
|
|
641
|
+
* @param data The data to pass as params to the function.
|
|
642
|
+
* @param options Streaming request options.
|
|
643
|
+
*/
|
|
644
|
+
function stream(functionsInstance, name, data, options) {
|
|
645
|
+
const url = functionsInstance._url(name);
|
|
646
|
+
return streamAtURL(functionsInstance, url, data, options || {});
|
|
647
|
+
}
|
|
648
|
+
/**
|
|
649
|
+
* Calls a callable function asynchronously and return a streaming result.
|
|
650
|
+
* @param url The url of the callable trigger.
|
|
651
|
+
* @param data The data to pass as params to the function.
|
|
652
|
+
* @param options Streaming request options.
|
|
653
|
+
*/
|
|
654
|
+
async function streamAtURL(functionsInstance, url, data, options) {
|
|
655
|
+
var _a;
|
|
656
|
+
// Encode any special types, such as dates, in the input data.
|
|
657
|
+
data = encode(data);
|
|
658
|
+
const body = { data };
|
|
659
|
+
//
|
|
660
|
+
// Add a header for the authToken.
|
|
661
|
+
const headers = await makeAuthHeaders(functionsInstance, options);
|
|
662
|
+
headers['Content-Type'] = 'application/json';
|
|
663
|
+
headers['Accept'] = 'text/event-stream';
|
|
664
|
+
let response;
|
|
665
|
+
try {
|
|
666
|
+
response = await functionsInstance.fetchImpl(url, {
|
|
667
|
+
method: 'POST',
|
|
668
|
+
body: JSON.stringify(body),
|
|
669
|
+
headers,
|
|
670
|
+
signal: options === null || options === void 0 ? void 0 : options.signal
|
|
671
|
+
});
|
|
672
|
+
}
|
|
673
|
+
catch (e) {
|
|
674
|
+
if (e instanceof Error && e.name === 'AbortError') {
|
|
675
|
+
const error = new FunctionsError('cancelled', 'Request was cancelled.');
|
|
676
|
+
return {
|
|
677
|
+
data: Promise.reject(error),
|
|
678
|
+
stream: {
|
|
679
|
+
[Symbol.asyncIterator]() {
|
|
680
|
+
return {
|
|
681
|
+
next() {
|
|
682
|
+
return Promise.reject(error);
|
|
683
|
+
}
|
|
684
|
+
};
|
|
685
|
+
}
|
|
686
|
+
}
|
|
687
|
+
};
|
|
688
|
+
}
|
|
689
|
+
// This could be an unhandled error on the backend, or it could be a
|
|
690
|
+
// network error. There's no way to know, since an unhandled error on the
|
|
691
|
+
// backend will fail to set the proper CORS header, and thus will be
|
|
692
|
+
// treated as a network error by fetch.
|
|
693
|
+
const error = _errorForResponse(0, null);
|
|
694
|
+
return {
|
|
695
|
+
data: Promise.reject(error),
|
|
696
|
+
// Return an empty async iterator
|
|
697
|
+
stream: {
|
|
698
|
+
[Symbol.asyncIterator]() {
|
|
699
|
+
return {
|
|
700
|
+
next() {
|
|
701
|
+
return Promise.reject(error);
|
|
702
|
+
}
|
|
703
|
+
};
|
|
704
|
+
}
|
|
705
|
+
}
|
|
706
|
+
};
|
|
707
|
+
}
|
|
708
|
+
let resultResolver;
|
|
709
|
+
let resultRejecter;
|
|
710
|
+
const resultPromise = new Promise((resolve, reject) => {
|
|
711
|
+
resultResolver = resolve;
|
|
712
|
+
resultRejecter = reject;
|
|
713
|
+
});
|
|
714
|
+
(_a = options === null || options === void 0 ? void 0 : options.signal) === null || _a === void 0 ? void 0 : _a.addEventListener('abort', () => {
|
|
715
|
+
const error = new FunctionsError('cancelled', 'Request was cancelled.');
|
|
716
|
+
resultRejecter(error);
|
|
717
|
+
});
|
|
718
|
+
const reader = response.body.getReader();
|
|
719
|
+
const rstream = createResponseStream(reader, resultResolver, resultRejecter, options === null || options === void 0 ? void 0 : options.signal);
|
|
720
|
+
return {
|
|
721
|
+
stream: {
|
|
722
|
+
[Symbol.asyncIterator]() {
|
|
723
|
+
const rreader = rstream.getReader();
|
|
724
|
+
return {
|
|
725
|
+
async next() {
|
|
726
|
+
const { value, done } = await rreader.read();
|
|
727
|
+
return { value: value, done };
|
|
728
|
+
},
|
|
729
|
+
async return() {
|
|
730
|
+
await rreader.cancel();
|
|
731
|
+
return { done: true, value: undefined };
|
|
732
|
+
}
|
|
733
|
+
};
|
|
734
|
+
}
|
|
735
|
+
},
|
|
736
|
+
data: resultPromise
|
|
737
|
+
};
|
|
738
|
+
}
|
|
739
|
+
/**
|
|
740
|
+
* Creates a ReadableStream that processes a streaming response from a streaming
|
|
741
|
+
* callable function that returns data in server-sent event format.
|
|
742
|
+
*
|
|
743
|
+
* @param reader The underlying reader providing raw response data
|
|
744
|
+
* @param resultResolver Callback to resolve the final result when received
|
|
745
|
+
* @param resultRejecter Callback to reject with an error if encountered
|
|
746
|
+
* @param signal Optional AbortSignal to cancel the stream processing
|
|
747
|
+
* @returns A ReadableStream that emits decoded messages from the response
|
|
748
|
+
*
|
|
749
|
+
* The returned ReadableStream:
|
|
750
|
+
* 1. Emits individual messages when "message" data is received
|
|
751
|
+
* 2. Resolves with the final result when a "result" message is received
|
|
752
|
+
* 3. Rejects with an error if an "error" message is received
|
|
753
|
+
*/
|
|
754
|
+
function createResponseStream(reader, resultResolver, resultRejecter, signal) {
|
|
755
|
+
const processLine = (line, controller) => {
|
|
756
|
+
const match = line.match(responseLineRE);
|
|
757
|
+
// ignore all other lines (newline, comments, etc.)
|
|
758
|
+
if (!match) {
|
|
759
|
+
return;
|
|
760
|
+
}
|
|
761
|
+
const data = match[1];
|
|
762
|
+
try {
|
|
763
|
+
const jsonData = JSON.parse(data);
|
|
764
|
+
if ('result' in jsonData) {
|
|
765
|
+
resultResolver(decode(jsonData.result));
|
|
766
|
+
return;
|
|
767
|
+
}
|
|
768
|
+
if ('message' in jsonData) {
|
|
769
|
+
controller.enqueue(decode(jsonData.message));
|
|
770
|
+
return;
|
|
771
|
+
}
|
|
772
|
+
if ('error' in jsonData) {
|
|
773
|
+
const error = _errorForResponse(0, jsonData);
|
|
774
|
+
controller.error(error);
|
|
775
|
+
resultRejecter(error);
|
|
776
|
+
return;
|
|
777
|
+
}
|
|
778
|
+
}
|
|
779
|
+
catch (error) {
|
|
780
|
+
if (error instanceof FunctionsError) {
|
|
781
|
+
controller.error(error);
|
|
782
|
+
resultRejecter(error);
|
|
783
|
+
return;
|
|
784
|
+
}
|
|
785
|
+
// ignore other parsing errors
|
|
786
|
+
}
|
|
787
|
+
};
|
|
788
|
+
const decoder = new TextDecoder();
|
|
789
|
+
return new ReadableStream({
|
|
790
|
+
start(controller) {
|
|
791
|
+
let currentText = '';
|
|
792
|
+
return pump();
|
|
793
|
+
async function pump() {
|
|
794
|
+
if (signal === null || signal === void 0 ? void 0 : signal.aborted) {
|
|
795
|
+
const error = new FunctionsError('cancelled', 'Request was cancelled');
|
|
796
|
+
controller.error(error);
|
|
797
|
+
resultRejecter(error);
|
|
798
|
+
return Promise.resolve();
|
|
799
|
+
}
|
|
800
|
+
try {
|
|
801
|
+
const { value, done } = await reader.read();
|
|
802
|
+
if (done) {
|
|
803
|
+
if (currentText.trim()) {
|
|
804
|
+
processLine(currentText.trim(), controller);
|
|
805
|
+
}
|
|
806
|
+
controller.close();
|
|
807
|
+
return;
|
|
808
|
+
}
|
|
809
|
+
if (signal === null || signal === void 0 ? void 0 : signal.aborted) {
|
|
810
|
+
const error = new FunctionsError('cancelled', 'Request was cancelled');
|
|
811
|
+
controller.error(error);
|
|
812
|
+
resultRejecter(error);
|
|
813
|
+
await reader.cancel();
|
|
814
|
+
return;
|
|
815
|
+
}
|
|
816
|
+
currentText += decoder.decode(value, { stream: true });
|
|
817
|
+
const lines = currentText.split('\n');
|
|
818
|
+
currentText = lines.pop() || '';
|
|
819
|
+
for (const line of lines) {
|
|
820
|
+
if (line.trim()) {
|
|
821
|
+
processLine(line.trim(), controller);
|
|
822
|
+
}
|
|
823
|
+
}
|
|
824
|
+
return pump();
|
|
825
|
+
}
|
|
826
|
+
catch (error) {
|
|
827
|
+
const functionsError = error instanceof FunctionsError
|
|
828
|
+
? error
|
|
829
|
+
: _errorForResponse(0, null);
|
|
830
|
+
controller.error(functionsError);
|
|
831
|
+
resultRejecter(functionsError);
|
|
832
|
+
}
|
|
833
|
+
}
|
|
834
|
+
},
|
|
835
|
+
cancel() {
|
|
836
|
+
return reader.cancel();
|
|
837
|
+
}
|
|
838
|
+
});
|
|
839
|
+
}
|
|
618
840
|
|
|
619
841
|
const name = "@firebase/functions";
|
|
620
|
-
const version = "0.
|
|
842
|
+
const version = "0.12.0-20241210191049";
|
|
621
843
|
|
|
622
844
|
/**
|
|
623
845
|
* @license
|