@firebase/data-connect 0.5.0 → 0.6.0-20260409172004
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/index.cjs.js +1173 -143
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.esm.js +1172 -144
- package/dist/index.esm.js.map +1 -1
- package/dist/index.node.cjs.js +1239 -190
- package/dist/index.node.cjs.js.map +1 -1
- package/dist/internal.d.ts +133 -12
- package/dist/node-esm/index.node.esm.js +1238 -191
- package/dist/node-esm/index.node.esm.js.map +1 -1
- package/dist/node-esm/src/api/Mutation.d.ts +2 -2
- package/dist/node-esm/src/api/query.d.ts +1 -1
- package/dist/node-esm/src/core/query/QueryManager.d.ts +22 -3
- package/dist/node-esm/src/network/index.d.ts +1 -1
- package/dist/node-esm/src/network/manager.d.ts +61 -0
- package/dist/node-esm/src/network/{fetch.d.ts → rest/fetch.d.ts} +9 -4
- package/dist/node-esm/src/network/rest/index.d.ts +18 -0
- package/dist/node-esm/src/network/rest/restTransport.d.ts +33 -0
- package/dist/node-esm/src/network/stream/streamTransport.d.ts +243 -0
- package/dist/node-esm/src/network/stream/websocket.d.ts +90 -0
- package/dist/node-esm/src/network/stream/wire.d.ts +138 -0
- package/dist/node-esm/src/network/transport.d.ts +179 -0
- package/dist/node-esm/src/util/url.d.ts +3 -1
- package/dist/private.d.ts +29 -7
- package/dist/public.d.ts +3 -1
- package/dist/src/api/Mutation.d.ts +2 -2
- package/dist/src/api/query.d.ts +1 -1
- package/dist/src/core/query/QueryManager.d.ts +22 -3
- package/dist/src/network/index.d.ts +1 -1
- package/dist/src/network/manager.d.ts +61 -0
- package/dist/src/network/{fetch.d.ts → rest/fetch.d.ts} +9 -4
- package/dist/src/network/rest/index.d.ts +18 -0
- package/dist/src/network/rest/restTransport.d.ts +33 -0
- package/dist/src/network/stream/streamTransport.d.ts +243 -0
- package/dist/src/network/stream/websocket.d.ts +90 -0
- package/dist/src/network/stream/wire.d.ts +138 -0
- package/dist/src/network/transport.d.ts +179 -0
- package/dist/src/util/url.d.ts +3 -1
- package/package.json +1 -1
- package/dist/node-esm/src/network/transport/index.d.ts +0 -81
- package/dist/node-esm/src/network/transport/rest.d.ts +0 -49
- package/dist/src/network/transport/index.d.ts +0 -81
- package/dist/src/network/transport/rest.d.ts +0 -49
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
* See the License for the specific language governing permissions and
|
|
15
15
|
* limitations under the License.
|
|
16
16
|
*/
|
|
17
|
-
import {
|
|
17
|
+
import { DataConnectTransportInterface } from '../network';
|
|
18
18
|
import { DataConnect } from './DataConnect';
|
|
19
19
|
import { DataConnectResult, MUTATION_STR, OperationRef } from './Reference';
|
|
20
20
|
export interface MutationRef<Data, Variables> extends OperationRef<Data, Variables> {
|
|
@@ -39,7 +39,7 @@ export declare function mutationRef<Data, Variables>(dcInstance: DataConnect, mu
|
|
|
39
39
|
export declare class MutationManager {
|
|
40
40
|
private _transport;
|
|
41
41
|
private _inflight;
|
|
42
|
-
constructor(_transport:
|
|
42
|
+
constructor(_transport: DataConnectTransportInterface);
|
|
43
43
|
executeMutation<Data, Variables>(mutationRef: MutationRef<Data, Variables>): MutationPromise<Data, Variables>;
|
|
44
44
|
}
|
|
45
45
|
/**
|
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
* limitations under the License.
|
|
16
16
|
*/
|
|
17
17
|
import { ExecuteQueryOptions } from '../core/query/queryOptions';
|
|
18
|
-
import { DataConnectExtensionWithMaxAge } from '../network
|
|
18
|
+
import { DataConnectExtensionWithMaxAge } from '../network';
|
|
19
19
|
import { DataConnect } from './DataConnect';
|
|
20
20
|
import { OperationRef, QUERY_STR, DataConnectResult, SerializedRef } from './Reference';
|
|
21
21
|
/**
|
|
@@ -18,8 +18,7 @@ import { type DataConnect } from '../../api/DataConnect';
|
|
|
18
18
|
import { QueryRef, QueryResult } from '../../api/query';
|
|
19
19
|
import { SerializedRef, DataSource } from '../../api/Reference';
|
|
20
20
|
import { DataConnectCache } from '../../cache/Cache';
|
|
21
|
-
import {
|
|
22
|
-
import { DataConnectExtensionWithMaxAge } from '../../network/transport';
|
|
21
|
+
import { DataConnectTransportInterface, DataConnectExtensionWithMaxAge } from '../../network';
|
|
23
22
|
import { OnCompleteSubscription, OnErrorSubscription, OnResultSubscription } from './subscribe';
|
|
24
23
|
export declare function getRefSerializer<Data, Variables>(queryRef: QueryRef<Data, Variables>, data: Data, source: DataSource, fetchTime: string): () => SerializedRef<Data, Variables>;
|
|
25
24
|
export declare class QueryManager {
|
|
@@ -28,8 +27,12 @@ export declare class QueryManager {
|
|
|
28
27
|
private cache?;
|
|
29
28
|
preferCacheResults<Data, Variables>(queryRef: QueryRef<Data, Variables>, allowStale?: boolean): Promise<QueryResult<Data, Variables>>;
|
|
30
29
|
private callbacks;
|
|
30
|
+
/**
|
|
31
|
+
* Map of serialized query keys to most recent Query Result. Used as a simple fallback cache
|
|
32
|
+
* for subsciptions if caching is not enabled.
|
|
33
|
+
*/
|
|
31
34
|
private subscriptionCache;
|
|
32
|
-
constructor(transport:
|
|
35
|
+
constructor(transport: DataConnectTransportInterface, dc: DataConnect, cache?: DataConnectCache | undefined);
|
|
33
36
|
private queue;
|
|
34
37
|
waitForQueuedWrites(): Promise<void>;
|
|
35
38
|
updateSSR<Data, Variables>(updatedData: QueryResult<Data, Variables>): void;
|
|
@@ -40,8 +43,24 @@ export declare class QueryManager {
|
|
|
40
43
|
publishErrorToSubscribers(key: string, err: unknown): void;
|
|
41
44
|
getFromResultTreeCache<Data, Variables>(queryRef: QueryRef<Data, Variables>, allowStale?: boolean): Promise<QueryResult<Data, Variables> | null>;
|
|
42
45
|
getFromSubscriberCache<Data, Variables>(queryRef: QueryRef<Data, Variables>): Promise<QueryResult<Data, Variables> | undefined>;
|
|
46
|
+
/** Call the registered onNext callbacks for the given key */
|
|
43
47
|
publishDataToSubscribers(key: string, queryResult: QueryResult<unknown, unknown>): void;
|
|
44
48
|
publishCacheResultsToSubscribers(impactedQueries: string[], fetchTime: string): Promise<void>;
|
|
45
49
|
enableEmulator(host: string, port: number): void;
|
|
50
|
+
/**
|
|
51
|
+
* Create a new {@link SubscribeObserver} for the given QueryRef. This will be passed to
|
|
52
|
+
* {@link DataConnectTransportInterface.invokeSubscribe | invokeSubscribe()} to notify the query
|
|
53
|
+
* layer of data update notifications or if the stream disconnected.
|
|
54
|
+
*/
|
|
55
|
+
private makeSubscribeObserver;
|
|
56
|
+
/**
|
|
57
|
+
* Handle a data update notification from the stream. Notify subscribers of results/errors, and
|
|
58
|
+
* update the cache.
|
|
59
|
+
*/
|
|
60
|
+
private handleStreamNotification;
|
|
61
|
+
/**
|
|
62
|
+
* Handle a disconnect from the stream. Unsubscribe all callbacks for the given key.
|
|
63
|
+
*/
|
|
64
|
+
private handleStreamDisconnect;
|
|
46
65
|
}
|
|
47
66
|
export declare function getMaxAgeFromExtensions(extensions: DataConnectExtensionWithMaxAge[] | undefined): number | undefined;
|
|
@@ -14,4 +14,4 @@
|
|
|
14
14
|
* See the License for the specific language governing permissions and
|
|
15
15
|
* limitations under the License.
|
|
16
16
|
*/
|
|
17
|
-
export
|
|
17
|
+
export * from './transport';
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2026 Google LLC
|
|
4
|
+
*
|
|
5
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
* you may not use this file except in compliance with the License.
|
|
7
|
+
* You may obtain a copy of the License at
|
|
8
|
+
*
|
|
9
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
*
|
|
11
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
* See the License for the specific language governing permissions and
|
|
15
|
+
* limitations under the License.
|
|
16
|
+
*/
|
|
17
|
+
import { DataConnectOptions, TransportOptions } from '../api/DataConnect';
|
|
18
|
+
import { AppCheckTokenProvider } from '../core/AppCheckTokenProvider';
|
|
19
|
+
import { AuthTokenProvider } from '../core/FirebaseAuthProvider';
|
|
20
|
+
import { CallerSdkType, DataConnectResponse, DataConnectResponseWithMaxAge, DataConnectTransportInterface, SubscribeObserver } from './transport';
|
|
21
|
+
/**
|
|
22
|
+
* Entry point for the transport layer. Manages routing between transport implementations.
|
|
23
|
+
* @internal
|
|
24
|
+
*/
|
|
25
|
+
export declare class DataConnectTransportManager implements DataConnectTransportInterface {
|
|
26
|
+
private options;
|
|
27
|
+
private apiKey?;
|
|
28
|
+
private appId?;
|
|
29
|
+
private authProvider?;
|
|
30
|
+
private appCheckProvider?;
|
|
31
|
+
private transportOptions?;
|
|
32
|
+
private _isUsingGen;
|
|
33
|
+
private _callerSdkType?;
|
|
34
|
+
private restTransport;
|
|
35
|
+
private streamTransport?;
|
|
36
|
+
private isUsingEmulator;
|
|
37
|
+
constructor(options: DataConnectOptions, apiKey?: string | undefined, appId?: (string | null) | undefined, authProvider?: AuthTokenProvider | undefined, appCheckProvider?: AppCheckTokenProvider | undefined, transportOptions?: TransportOptions | undefined, _isUsingGen?: boolean, _callerSdkType?: CallerSdkType | undefined);
|
|
38
|
+
/**
|
|
39
|
+
* Initializes the stream transport if it hasn't been already.
|
|
40
|
+
*/
|
|
41
|
+
private initStreamTransport;
|
|
42
|
+
/**
|
|
43
|
+
* Returns true if the stream is in a healthy, ready connection state and has active subscriptions.
|
|
44
|
+
*/
|
|
45
|
+
private executeShouldUseStream;
|
|
46
|
+
/**
|
|
47
|
+
* Prefer to use Streaming Transport connection when one is available.
|
|
48
|
+
* @inheritdoc
|
|
49
|
+
*/
|
|
50
|
+
invokeQuery<Data, Variables>(queryName: string, body?: Variables): Promise<DataConnectResponseWithMaxAge<Data>>;
|
|
51
|
+
/**
|
|
52
|
+
* Prefer to use Streaming Transport connection when one is available.
|
|
53
|
+
* @inheritdoc
|
|
54
|
+
*/
|
|
55
|
+
invokeMutation<Data, Variables>(queryName: string, body?: Variables): Promise<DataConnectResponse<Data>>;
|
|
56
|
+
invokeSubscribe<Data, Variables>(observer: SubscribeObserver<Data>, queryName: string, body?: Variables): void;
|
|
57
|
+
invokeUnsubscribe<Variables>(queryName: string, body?: Variables): void;
|
|
58
|
+
useEmulator(host: string, port?: number, sslEnabled?: boolean): void;
|
|
59
|
+
onAuthTokenChanged(token: string | null): void;
|
|
60
|
+
_setCallerSdkType(callerSdkType: CallerSdkType): void;
|
|
61
|
+
}
|
|
@@ -14,11 +14,16 @@
|
|
|
14
14
|
* See the License for the specific language governing permissions and
|
|
15
15
|
* limitations under the License.
|
|
16
16
|
*/
|
|
17
|
-
import { CallerSdkType, DataConnectResponse } from '
|
|
17
|
+
import { CallerSdkType, DataConnectResponse } from '..';
|
|
18
|
+
/**
|
|
19
|
+
* This function is ONLY used for testing and for ensuring compatability in environments which may
|
|
20
|
+
* be using a poyfill and/or bundlers. It should not be called by users of the Firebase JS SDK.
|
|
21
|
+
* @internal
|
|
22
|
+
*/
|
|
18
23
|
export declare function initializeFetch(fetchImpl: typeof fetch): void;
|
|
19
|
-
export interface DataConnectFetchBody<
|
|
24
|
+
export interface DataConnectFetchBody<Variables> {
|
|
20
25
|
name: string;
|
|
21
26
|
operationName: string;
|
|
22
|
-
variables:
|
|
27
|
+
variables: Variables;
|
|
23
28
|
}
|
|
24
|
-
export declare function dcFetch<
|
|
29
|
+
export declare function dcFetch<Data, Variables>(url: string, body: DataConnectFetchBody<Variables>, { signal }: AbortController, appId: string | null | undefined, accessToken: string | null, appCheckToken: string | null | undefined, _isUsingGen: boolean, _callerSdkType: CallerSdkType, _isUsingEmulator: boolean): Promise<DataConnectResponse<Data>>;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2026 Google LLC
|
|
4
|
+
*
|
|
5
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
* you may not use this file except in compliance with the License.
|
|
7
|
+
* You may obtain a copy of the License at
|
|
8
|
+
*
|
|
9
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
*
|
|
11
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
* See the License for the specific language governing permissions and
|
|
15
|
+
* limitations under the License.
|
|
16
|
+
*/
|
|
17
|
+
export * from './fetch';
|
|
18
|
+
export * from './restTransport';
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2024 Google LLC
|
|
4
|
+
*
|
|
5
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
* you may not use this file except in compliance with the License.
|
|
7
|
+
* You may obtain a copy of the License at
|
|
8
|
+
*
|
|
9
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
*
|
|
11
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
* See the License for the specific language governing permissions and
|
|
15
|
+
* limitations under the License.
|
|
16
|
+
*/
|
|
17
|
+
import { CallerSdkType, DataConnectResponse, DataConnectResponseWithMaxAge, AbstractDataConnectTransport, SubscribeObserver } from '..';
|
|
18
|
+
import { DataConnectOptions, TransportOptions } from '../../api/DataConnect';
|
|
19
|
+
import { AppCheckTokenProvider } from '../../core/AppCheckTokenProvider';
|
|
20
|
+
import { AuthTokenProvider } from '../../core/FirebaseAuthProvider';
|
|
21
|
+
/**
|
|
22
|
+
* Fetch-based REST implementation of {@link AbstractDataConnectTransport}.
|
|
23
|
+
* @internal
|
|
24
|
+
*/
|
|
25
|
+
export declare class RESTTransport extends AbstractDataConnectTransport {
|
|
26
|
+
constructor(options: DataConnectOptions, apiKey?: string | undefined, appId?: string | null, authProvider?: AuthTokenProvider | undefined, appCheckProvider?: AppCheckTokenProvider | undefined, transportOptions?: TransportOptions | undefined, _isUsingGen?: boolean, _callerSdkType?: CallerSdkType);
|
|
27
|
+
get endpointUrl(): string;
|
|
28
|
+
invokeQuery: <Data, Variables>(queryName: string, body?: Variables) => Promise<DataConnectResponseWithMaxAge<Data>>;
|
|
29
|
+
invokeMutation: <Data, Variables>(queryName: string, body?: Variables) => Promise<DataConnectResponse<Data>>;
|
|
30
|
+
invokeSubscribe<Data, Variables>(observer: SubscribeObserver<Data>, queryName: string, body?: Variables): void;
|
|
31
|
+
invokeUnsubscribe<Variables>(queryName: string, body?: Variables): void;
|
|
32
|
+
onAuthTokenChanged(newToken: string | null): void;
|
|
33
|
+
}
|
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2026 Google LLC
|
|
4
|
+
*
|
|
5
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
* you may not use this file except in compliance with the License.
|
|
7
|
+
* You may obtain a copy of the License at
|
|
8
|
+
*
|
|
9
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
*
|
|
11
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
* See the License for the specific language governing permissions and
|
|
15
|
+
* limitations under the License.
|
|
16
|
+
*/
|
|
17
|
+
import { AbstractDataConnectTransport, DataConnectResponse, SubscribeObserver } from '../transport';
|
|
18
|
+
import { DataConnectStreamRequest } from './wire';
|
|
19
|
+
/**
|
|
20
|
+
* The base class for all {@link DataConnectStreamTransport | Stream Transport} implementations.
|
|
21
|
+
* Handles management of logical streams (requests), authentication, data routing to query layer, etc.
|
|
22
|
+
* @internal
|
|
23
|
+
*/
|
|
24
|
+
export declare abstract class AbstractDataConnectStreamTransport extends AbstractDataConnectTransport {
|
|
25
|
+
/** Optional callback invoked when the stream closes gracefully. */
|
|
26
|
+
onGracefulStreamClose?: () => void;
|
|
27
|
+
/** True if the physical stream connection is fully open and ready to transmit data. */
|
|
28
|
+
abstract get streamIsReady(): boolean;
|
|
29
|
+
/** Is the stream currently waiting to close connection? */
|
|
30
|
+
get isPendingClose(): boolean;
|
|
31
|
+
private pendingClose;
|
|
32
|
+
/** True if the transport is unable to connect to the server */
|
|
33
|
+
isUnableToConnect: boolean;
|
|
34
|
+
/** True if there are active subscriptions on the stream */
|
|
35
|
+
get hasActiveSubscriptions(): boolean;
|
|
36
|
+
/** True if there are active execute or mutation requests on the stream */
|
|
37
|
+
get hasActiveExecuteRequests(): boolean;
|
|
38
|
+
/**
|
|
39
|
+
* Open a physical connection to the server.
|
|
40
|
+
* @returns a promise which resolves when the connection is ready, or rejects if it fails to open.
|
|
41
|
+
*/
|
|
42
|
+
protected abstract openConnection(): Promise<void>;
|
|
43
|
+
/**
|
|
44
|
+
* Close the physical connection with the server. Handles no cleanup - simply closes the
|
|
45
|
+
* implementation-specific connection.
|
|
46
|
+
* @returns a promise which resolves when the connection is closed, or rejects if it fails to close.
|
|
47
|
+
* On failure to close, the connection is still considered closed.
|
|
48
|
+
*/
|
|
49
|
+
protected abstract closeConnection(): Promise<void>;
|
|
50
|
+
/**
|
|
51
|
+
* Queue a message to be sent over the stream.
|
|
52
|
+
* @param requestBody The body of the message to be sent.
|
|
53
|
+
* @throws DataConnectError if sending fails.
|
|
54
|
+
*/
|
|
55
|
+
protected abstract sendMessage<Variables>(requestBody: DataConnectStreamRequest<Variables>): Promise<void>;
|
|
56
|
+
/**
|
|
57
|
+
* Ensures that that there is an open connection. If there is none, it initiates a new one.
|
|
58
|
+
* If a connection attempt is already in progress, it returns the existing connection promise.
|
|
59
|
+
* @returns A promise that resolves when the stream is open and ready.
|
|
60
|
+
*/
|
|
61
|
+
protected abstract ensureConnection(): Promise<void>;
|
|
62
|
+
/** The request ID of the next message to be sent. Monotonically increasing sequence number. */
|
|
63
|
+
private requestNumber;
|
|
64
|
+
/**
|
|
65
|
+
* Generates and returns the next request ID.
|
|
66
|
+
*/
|
|
67
|
+
private nextRequestId;
|
|
68
|
+
/**
|
|
69
|
+
* Map of query/variables to their active execute/resume request bodies.
|
|
70
|
+
*/
|
|
71
|
+
private activeQueryExecuteRequests;
|
|
72
|
+
/**
|
|
73
|
+
* Map of mutation/variables to their active execute request bodies.
|
|
74
|
+
*/
|
|
75
|
+
private activeMutationExecuteRequests;
|
|
76
|
+
/**
|
|
77
|
+
* Map of query/variables to their active subscribe request bodies.
|
|
78
|
+
*/
|
|
79
|
+
private activeSubscribeRequests;
|
|
80
|
+
/**
|
|
81
|
+
* Map of active execution RequestIds and their corresponding Promises and resolvers.
|
|
82
|
+
*/
|
|
83
|
+
private executeRequestPromises;
|
|
84
|
+
/**
|
|
85
|
+
* Map of active subscription RequestIds and their corresponding observers.
|
|
86
|
+
*/
|
|
87
|
+
private subscribeObservers;
|
|
88
|
+
/** current close timeout from setTimeout(), if any */
|
|
89
|
+
private closeTimeout;
|
|
90
|
+
/** has the close timeout finished? */
|
|
91
|
+
private closeTimeoutFinished;
|
|
92
|
+
/** current auth uid. used to detect if a different user logs in */
|
|
93
|
+
private authUid;
|
|
94
|
+
/** Flag to ensure we wait for the initial auth state once per connection attempt. */
|
|
95
|
+
private hasWaitedForInitialAuth;
|
|
96
|
+
/**
|
|
97
|
+
* Tracks a query execution request, storing the request body and creating and storing a promise that
|
|
98
|
+
* will be resolved when the response is received.
|
|
99
|
+
* @returns The reject function and the response promise.
|
|
100
|
+
*
|
|
101
|
+
* @remarks
|
|
102
|
+
* This method returns a promise, but is synchronous.
|
|
103
|
+
*/
|
|
104
|
+
private trackQueryExecuteRequest;
|
|
105
|
+
/**
|
|
106
|
+
* Tracks a mutation execution request, storing the request body and creating and storing a promise
|
|
107
|
+
* that will be resolved when the response is received.
|
|
108
|
+
* @returns The reject function and the response promise.
|
|
109
|
+
*
|
|
110
|
+
* @remarks
|
|
111
|
+
* This method returns a promise, but is synchronous.
|
|
112
|
+
*/
|
|
113
|
+
private trackMutationExecuteRequest;
|
|
114
|
+
/**
|
|
115
|
+
* Tracks a subscribe request, storing the request body and the notification observer.
|
|
116
|
+
* @remarks
|
|
117
|
+
* This method is synchronous.
|
|
118
|
+
*/
|
|
119
|
+
private trackSubscribeRequest;
|
|
120
|
+
/**
|
|
121
|
+
* Cleans up the query execute request tracking data structures, deleting the tracked request and
|
|
122
|
+
* it's associated promise.
|
|
123
|
+
*/
|
|
124
|
+
private cleanupQueryExecuteRequest;
|
|
125
|
+
/**
|
|
126
|
+
* Cleans up the mutation execute request tracking data structures, deleting the tracked request and
|
|
127
|
+
* it's associated promise.
|
|
128
|
+
*/
|
|
129
|
+
private cleanupMutationExecuteRequest;
|
|
130
|
+
/**
|
|
131
|
+
* Cleans up the subscribe request tracking data structures, deleting the tracked request and
|
|
132
|
+
* it's associated promise.
|
|
133
|
+
*/
|
|
134
|
+
private cleanupSubscribeRequest;
|
|
135
|
+
/**
|
|
136
|
+
* Tracks if the next message to be sent is the first message of the stream.
|
|
137
|
+
*/
|
|
138
|
+
private isFirstStreamMessage;
|
|
139
|
+
/**
|
|
140
|
+
* Tracks the last auth token sent to the server.
|
|
141
|
+
* Used to detect if the token has changed and needs to be resent.
|
|
142
|
+
*/
|
|
143
|
+
private lastSentAuthToken;
|
|
144
|
+
/**
|
|
145
|
+
* Indicates whether we should include the auth token in the next message.
|
|
146
|
+
* Only true if there is an auth token and it is different from the last sent auth token, or this
|
|
147
|
+
* is the first message.
|
|
148
|
+
*/
|
|
149
|
+
private get shouldIncludeAuth();
|
|
150
|
+
/**
|
|
151
|
+
* Called by the concrete transport implementation when the physical connection is ready.
|
|
152
|
+
*/
|
|
153
|
+
protected onConnectionReady(): void;
|
|
154
|
+
/**
|
|
155
|
+
* Attempt to close the connection. Will only close if there are no active requests preventing it
|
|
156
|
+
* from doing so.
|
|
157
|
+
*/
|
|
158
|
+
private attemptClose;
|
|
159
|
+
/**
|
|
160
|
+
* Begin closing the connection. Waits for and cleans up all active requests, and waits for
|
|
161
|
+
* {@link IDLE_CONNECTION_TIMEOUT_MS}. This is a graceful close - it will be called when there are
|
|
162
|
+
* no more active subscriptions, so there's no need to cleanup.
|
|
163
|
+
*/
|
|
164
|
+
private prepareToCloseGracefully;
|
|
165
|
+
/**
|
|
166
|
+
* Cancel closing the connection.
|
|
167
|
+
*/
|
|
168
|
+
private cancelClose;
|
|
169
|
+
/**
|
|
170
|
+
* Reject all active execute promises and notify all subscribe observers with the given error.
|
|
171
|
+
* Clear active request tracking maps without cancelling or re-invoking any requests.
|
|
172
|
+
*/
|
|
173
|
+
private rejectAllActiveRequests;
|
|
174
|
+
/**
|
|
175
|
+
* Called by concrete implementations when the stream is successfully closed, gracefully or otherwise.
|
|
176
|
+
*/
|
|
177
|
+
protected onStreamClose(code: number, reason: string): void;
|
|
178
|
+
/**
|
|
179
|
+
* Prepares a stream request message by adding necessary headers and metadata.
|
|
180
|
+
* If this is the first message on the stream, it includes the resource name, auth token, and App Check token.
|
|
181
|
+
* If the auth token has refreshed since the last message, it includes the new auth token.
|
|
182
|
+
*
|
|
183
|
+
* This method is called by the concrete transport implementation before sending a message.
|
|
184
|
+
*
|
|
185
|
+
* @returns the requestBody, with attached headers and initial request fields
|
|
186
|
+
*/
|
|
187
|
+
protected prepareMessage<Variables, StreamBody extends DataConnectStreamRequest<Variables>>(requestBody: StreamBody): StreamBody;
|
|
188
|
+
/**
|
|
189
|
+
* Sends a request message to the server via the concrete implementation.
|
|
190
|
+
* Ensures the connection is ready and prepares the message before sending.
|
|
191
|
+
* @returns A promise that resolves when the request message has been sent.
|
|
192
|
+
*/
|
|
193
|
+
private sendRequestMessage;
|
|
194
|
+
/**
|
|
195
|
+
* Helper to generate a consistent string key for the tracking maps.
|
|
196
|
+
*/
|
|
197
|
+
private getMapKey;
|
|
198
|
+
/**
|
|
199
|
+
* Recursively sorts the keys of an object.
|
|
200
|
+
*/
|
|
201
|
+
private sortObjectKeys;
|
|
202
|
+
/**
|
|
203
|
+
* @inheritdoc
|
|
204
|
+
* @remarks
|
|
205
|
+
* This method synchronously updates the request tracking data structures before sending any message.
|
|
206
|
+
* If any asynchronous functionality is added to this function, it MUST be done in a way that
|
|
207
|
+
* preserves the synchronous update of the tracking data structures before the method returns.
|
|
208
|
+
*/
|
|
209
|
+
invokeQuery<Data, Variables>(queryName: string, variables?: Variables): Promise<DataConnectResponse<Data>>;
|
|
210
|
+
/**
|
|
211
|
+
* @inheritdoc
|
|
212
|
+
* @remarks
|
|
213
|
+
* This method synchronously updates the request tracking data structures before sending any message.
|
|
214
|
+
* If any asynchronous functionality is added to this function, it MUST be done in a way that
|
|
215
|
+
* preserves the synchronous update of the tracking data structures before the method returns.
|
|
216
|
+
*/
|
|
217
|
+
invokeMutation<Data, Variables>(mutationName: string, variables?: Variables): Promise<DataConnectResponse<Data>>;
|
|
218
|
+
/**
|
|
219
|
+
* @inheritdoc
|
|
220
|
+
* @remarks
|
|
221
|
+
* This method synchronously updates the request tracking data structures before sending any message
|
|
222
|
+
* or cancelling the closing of the stream. If any asynchronous functionality is added to this function,
|
|
223
|
+
* it MUST be done in a way that preserves the synchronous update of the tracking data structures
|
|
224
|
+
* before the method returns.
|
|
225
|
+
*/
|
|
226
|
+
invokeSubscribe<Data, Variables>(observer: SubscribeObserver<Data>, queryName: string, variables: Variables): void;
|
|
227
|
+
/**
|
|
228
|
+
* @inheritdoc
|
|
229
|
+
* @remarks
|
|
230
|
+
* This method synchronously updates the request tracking data structures before sending any message.
|
|
231
|
+
* If any asynchronous functionality is added to this function, it MUST be done in a way that
|
|
232
|
+
* preserves the synchronous update of the tracking data structures before the method returns.
|
|
233
|
+
*/
|
|
234
|
+
invokeUnsubscribe<Variables>(queryName: string, variables: Variables): void;
|
|
235
|
+
onAuthTokenChanged(newToken: string | null): void;
|
|
236
|
+
/**
|
|
237
|
+
* Handle a response message from the server. Called by the connection-specific implementation after
|
|
238
|
+
* it's transformed a message from the server into a {@link DataConnectResponse}.
|
|
239
|
+
* @param requestId the requestId associated with this response.
|
|
240
|
+
* @param response the response from the server.
|
|
241
|
+
*/
|
|
242
|
+
protected handleResponse<Data>(requestId: string, response: DataConnectResponse<Data>): Promise<void>;
|
|
243
|
+
}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2026 Google LLC
|
|
4
|
+
*
|
|
5
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
* you may not use this file except in compliance with the License.
|
|
7
|
+
* You may obtain a copy of the License at
|
|
8
|
+
*
|
|
9
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
*
|
|
11
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
* See the License for the specific language governing permissions and
|
|
15
|
+
* limitations under the License.
|
|
16
|
+
*/
|
|
17
|
+
import { DataConnectOptions, TransportOptions } from '../../api/DataConnect';
|
|
18
|
+
import { AppCheckTokenProvider } from '../../core/AppCheckTokenProvider';
|
|
19
|
+
import { AuthTokenProvider } from '../../core/FirebaseAuthProvider';
|
|
20
|
+
import { CallerSdkType } from '../transport';
|
|
21
|
+
import { AbstractDataConnectStreamTransport } from './streamTransport';
|
|
22
|
+
import { DataConnectStreamRequest } from './wire';
|
|
23
|
+
/**
|
|
24
|
+
* This function is ONLY used for testing and for ensuring compatability in environments which may
|
|
25
|
+
* be using a poyfill and/or bundlers. It should not be called by users of the Firebase JS SDK.
|
|
26
|
+
* @internal
|
|
27
|
+
*/
|
|
28
|
+
export declare function initializeWebSocket(webSocketImpl: typeof WebSocket): void;
|
|
29
|
+
/**
|
|
30
|
+
* The code used to close the WebSocket connection.
|
|
31
|
+
* This is a protocol-level code, and is not the same as the {@link Code | DataConnect error code}.
|
|
32
|
+
* @internal
|
|
33
|
+
*/
|
|
34
|
+
export declare const WEBSOCKET_CLOSE_CODE = 1000;
|
|
35
|
+
/**
|
|
36
|
+
* An {@link AbstractDataConnectStreamTransport | Stream Transport} implementation that uses {@link WebSocket | WebSockets} to stream requests and responses.
|
|
37
|
+
* This class handles the lifecycle of the WebSocket connection, including automatic
|
|
38
|
+
* reconnection and request correlation.
|
|
39
|
+
* @internal
|
|
40
|
+
*/
|
|
41
|
+
export declare class WebSocketTransport extends AbstractDataConnectStreamTransport {
|
|
42
|
+
protected apiKey?: string | undefined;
|
|
43
|
+
protected appId?: (string | null) | undefined;
|
|
44
|
+
protected authProvider?: AuthTokenProvider | undefined;
|
|
45
|
+
protected appCheckProvider?: AppCheckTokenProvider | undefined;
|
|
46
|
+
protected _isUsingGen: boolean;
|
|
47
|
+
protected _callerSdkType: CallerSdkType;
|
|
48
|
+
get endpointUrl(): string;
|
|
49
|
+
/** Decodes binary WebSocket responses to strings */
|
|
50
|
+
private decoder;
|
|
51
|
+
/**
|
|
52
|
+
* Decodes a WebSocket response from a Uint8Array to a JSON object.
|
|
53
|
+
* Emulator does not send messages as Uint8Arrays, but prod does.
|
|
54
|
+
*/
|
|
55
|
+
private decodeBinaryResponse;
|
|
56
|
+
/** The current connection to the server. Undefined if disconnected. */
|
|
57
|
+
private connection;
|
|
58
|
+
get streamIsReady(): boolean;
|
|
59
|
+
/**
|
|
60
|
+
* Current connection attempt. If null, we are not currently attemping to connect (not connected,
|
|
61
|
+
* or already connected). Will be resolved or rejected when the connection is opened or fails to open.
|
|
62
|
+
*/
|
|
63
|
+
private connectionAttempt;
|
|
64
|
+
constructor(options: DataConnectOptions, apiKey?: string | undefined, appId?: (string | null) | undefined, authProvider?: AuthTokenProvider | undefined, appCheckProvider?: AppCheckTokenProvider | undefined, transportOptions?: TransportOptions | undefined, _isUsingGen?: boolean, _callerSdkType?: CallerSdkType);
|
|
65
|
+
protected ensureConnection(): Promise<void>;
|
|
66
|
+
protected openConnection(): Promise<void>;
|
|
67
|
+
protected closeConnection(code?: number, reason?: string): Promise<void>;
|
|
68
|
+
/**
|
|
69
|
+
* Handle a disconnection from the server. Initiates graceful clean up and reconnection attempts.
|
|
70
|
+
* @param ev the {@link CloseEvent} that closed the WebSocket.
|
|
71
|
+
*/
|
|
72
|
+
private handleWebsocketDisconnect;
|
|
73
|
+
/**
|
|
74
|
+
* Handle an error that occurred on the WebSocket. Close the connection and reject all active requests.
|
|
75
|
+
*/
|
|
76
|
+
private handleError;
|
|
77
|
+
protected sendMessage<Variables>(requestBody: DataConnectStreamRequest<Variables>): Promise<void>;
|
|
78
|
+
/**
|
|
79
|
+
* Handles incoming WebSocket messages.
|
|
80
|
+
* @param ev The {@link MessageEvent} from the WebSocket.
|
|
81
|
+
*/
|
|
82
|
+
private handleWebSocketMessage;
|
|
83
|
+
/**
|
|
84
|
+
* Parse a response from the server. Assert that it has a {@link DataConnectStreamResponse.requestId | requestId}.
|
|
85
|
+
* @param data the message from the server to be parsed
|
|
86
|
+
* @returns the parsed message as a {@link DataConnectStreamResponse}
|
|
87
|
+
* @throws {DataConnectError} if parsing fails or message is malformed.
|
|
88
|
+
*/
|
|
89
|
+
private parseWebSocketData;
|
|
90
|
+
}
|