-
Notifications
You must be signed in to change notification settings - Fork 923
/
Copy pathDataConnect.ts
309 lines (289 loc) · 8.38 KB
/
DataConnect.ts
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
/**
* @license
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import {
FirebaseApp,
_getProvider,
_removeServiceInstance,
getApp
} from '@firebase/app';
import { AppCheckInternalComponentName } from '@firebase/app-check-interop-types';
import { FirebaseAuthInternalName } from '@firebase/auth-interop-types';
import { Provider } from '@firebase/component';
import { AppCheckTokenProvider } from '../core/AppCheckTokenProvider';
import { Code, DataConnectError } from '../core/error';
import {
AuthTokenProvider,
FirebaseAuthProvider
} from '../core/FirebaseAuthProvider';
import { QueryManager } from '../core/QueryManager';
import { logDebug, logError } from '../logger';
import { DataConnectTransport, TransportClass } from '../network';
import { RESTTransport } from '../network/transport/rest';
import { MutationManager } from './Mutation';
/**
* Connector Config for calling Data Connect backend.
*/
export interface ConnectorConfig {
location: string;
connector: string;
service: string;
}
/**
* Options to connect to emulator
*/
export interface TransportOptions {
host: string;
sslEnabled?: boolean;
port?: number;
}
const FIREBASE_DATA_CONNECT_EMULATOR_HOST_VAR =
'FIREBASE_DATA_CONNECT_EMULATOR_HOST';
/**
*
* @param fullHost
* @returns TransportOptions
* @internal
*/
export function parseOptions(fullHost: string): TransportOptions {
const [protocol, hostName] = fullHost.split('://');
const isSecure = protocol === 'https';
const [host, portAsString] = hostName.split(':');
const port = Number(portAsString);
return { host, port, sslEnabled: isSecure };
}
/**
* DataConnectOptions including project id
*/
export interface DataConnectOptions extends ConnectorConfig {
projectId: string;
}
/**
* Class representing Firebase Data Connect
*/
export class DataConnect {
_queryManager!: QueryManager;
_mutationManager!: MutationManager;
isEmulator = false;
_initialized = false;
private _transport!: DataConnectTransport;
private _transportClass: TransportClass | undefined;
private _transportOptions?: TransportOptions;
private _authTokenProvider?: AuthTokenProvider;
_isUsingGeneratedSdk: boolean = false;
private _appCheckTokenProvider?: AppCheckTokenProvider;
// @internal
constructor(
public readonly app: FirebaseApp,
// TODO(mtewani): Replace with _dataConnectOptions in the future
private readonly dataConnectOptions: DataConnectOptions,
private readonly _authProvider: Provider<FirebaseAuthInternalName>,
private readonly _appCheckProvider: Provider<AppCheckInternalComponentName>
) {
if (typeof process !== 'undefined' && process.env) {
const host = process.env[FIREBASE_DATA_CONNECT_EMULATOR_HOST_VAR];
if (host) {
logDebug('Found custom host. Using emulator');
this.isEmulator = true;
this._transportOptions = parseOptions(host);
}
}
}
// @internal
_useGeneratedSdk(): void {
if (!this._isUsingGeneratedSdk) {
this._isUsingGeneratedSdk = true;
}
}
_delete(): Promise<void> {
_removeServiceInstance(
this.app,
'data-connect',
JSON.stringify(this.getSettings())
);
return Promise.resolve();
}
// @internal
getSettings(): ConnectorConfig {
const copy = JSON.parse(JSON.stringify(this.dataConnectOptions));
delete copy.projectId;
return copy;
}
// @internal
setInitialized(): void {
if (this._initialized) {
return;
}
if (this._transportClass === undefined) {
logDebug('transportClass not provided. Defaulting to RESTTransport.');
this._transportClass = RESTTransport;
}
if (this._authProvider) {
this._authTokenProvider = new FirebaseAuthProvider(
this.app.name,
this.app.options,
this._authProvider
);
}
if (this._appCheckProvider) {
this._appCheckTokenProvider = new AppCheckTokenProvider(
this.app,
this._appCheckProvider
);
}
this._initialized = true;
this._transport = new this._transportClass(
this.dataConnectOptions,
this.app.options.apiKey,
this.app.options.appId,
this._authTokenProvider,
this._appCheckTokenProvider,
undefined,
this._isUsingGeneratedSdk
);
if (this._transportOptions) {
this._transport.useEmulator(
this._transportOptions.host,
this._transportOptions.port,
this._transportOptions.sslEnabled
);
}
this._queryManager = new QueryManager(this._transport);
this._mutationManager = new MutationManager(this._transport);
}
// @internal
enableEmulator(transportOptions: TransportOptions): void {
if (
this._initialized &&
!areTransportOptionsEqual(this._transportOptions, transportOptions)
) {
logError('enableEmulator called after initialization');
throw new DataConnectError(
Code.ALREADY_INITIALIZED,
'DataConnect instance already initialized!'
);
}
this._transportOptions = transportOptions;
this.isEmulator = true;
}
}
/**
* @internal
* @param transportOptions1
* @param transportOptions2
* @returns
*/
export function areTransportOptionsEqual(
transportOptions1: TransportOptions,
transportOptions2: TransportOptions
): boolean {
return (
transportOptions1.host === transportOptions2.host &&
transportOptions1.port === transportOptions2.port &&
transportOptions1.sslEnabled === transportOptions2.sslEnabled
);
}
/**
* Connect to the DataConnect Emulator
* @param dc Data Connect instance
* @param host host of emulator server
* @param port port of emulator server
* @param sslEnabled use https
*/
export function connectDataConnectEmulator(
dc: DataConnect,
host: string,
port?: number,
sslEnabled = false
): void {
dc.enableEmulator({ host, port, sslEnabled });
}
/**
* Initialize DataConnect instance
* @param options ConnectorConfig
*/
export function getDataConnect(options: ConnectorConfig): DataConnect;
/**
* Initialize DataConnect instance
* @param app FirebaseApp to initialize to.
* @param options ConnectorConfig
*/
export function getDataConnect(
app: FirebaseApp,
options: ConnectorConfig
): DataConnect;
export function getDataConnect(
appOrOptions: FirebaseApp | ConnectorConfig,
optionalOptions?: ConnectorConfig
): DataConnect {
let app: FirebaseApp;
let dcOptions: ConnectorConfig;
if ('location' in appOrOptions) {
dcOptions = appOrOptions;
app = getApp();
} else {
dcOptions = optionalOptions!;
app = appOrOptions;
}
if (!app || Object.keys(app).length === 0) {
app = getApp();
}
const provider = _getProvider(app, 'data-connect');
const identifier = JSON.stringify(dcOptions);
if (provider.isInitialized(identifier)) {
const dcInstance = provider.getImmediate({ identifier });
const options = provider.getOptions(identifier);
const optionsValid = Object.keys(options).length > 0;
if (optionsValid) {
logDebug('Re-using cached instance');
return dcInstance;
}
}
validateDCOptions(dcOptions);
logDebug('Creating new DataConnect instance');
// Initialize with options.
return provider.initialize({
instanceIdentifier: identifier,
options: dcOptions
});
}
/**
*
* @param dcOptions
* @returns {void}
* @internal
*/
export function validateDCOptions(dcOptions: ConnectorConfig): boolean {
const fields = ['connector', 'location', 'service'];
if (!dcOptions) {
throw new DataConnectError(Code.INVALID_ARGUMENT, 'DC Option Required');
}
fields.forEach(field => {
if (dcOptions[field] === null || dcOptions[field] === undefined) {
throw new DataConnectError(Code.INVALID_ARGUMENT, `${field} Required`);
}
});
return true;
}
/**
* Delete DataConnect instance
* @param dataConnect DataConnect instance
* @returns
*/
export function terminate(dataConnect: DataConnect): Promise<void> {
return dataConnect._delete();
// TODO(mtewani): Stop pending tasks
}