-
Notifications
You must be signed in to change notification settings - Fork 371
/
remote-config.ts
615 lines (550 loc) · 20.6 KB
/
remote-config.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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
/*!
* Copyright 2020 Google Inc.
*
* 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 { App } from '../app';
import * as validator from '../utils/validator';
import { FirebaseRemoteConfigError, RemoteConfigApiClient } from './remote-config-api-client-internal';
import { ConditionEvaluator } from './condition-evaluator-internal';
import { ValueImpl } from './internal/value-impl';
import {
ListVersionsOptions,
ListVersionsResult,
RemoteConfigCondition,
RemoteConfigParameter,
RemoteConfigParameterGroup,
ServerTemplate,
RemoteConfigTemplate,
RemoteConfigUser,
Version,
ExplicitParameterValue,
InAppDefaultValue,
ServerConfig,
RemoteConfigParameterValue,
EvaluationContext,
ServerTemplateData,
NamedCondition,
Value,
DefaultConfig,
GetServerTemplateOptions,
InitServerTemplateOptions,
ServerTemplateDataType,
} from './remote-config-api';
/**
* The Firebase `RemoteConfig` service interface.
*/
export class RemoteConfig {
private readonly client: RemoteConfigApiClient;
/**
* @param app - The app for this RemoteConfig service.
* @constructor
* @internal
*/
constructor(readonly app: App) {
this.client = new RemoteConfigApiClient(app);
}
/**
* Gets the current active version of the {@link RemoteConfigTemplate} of the project.
*
* @returns A promise that fulfills with a `RemoteConfigTemplate`.
*/
public getTemplate(): Promise<RemoteConfigTemplate> {
return this.client.getTemplate()
.then((templateResponse) => {
return new RemoteConfigTemplateImpl(templateResponse);
});
}
/**
* Gets the requested version of the {@link RemoteConfigTemplate} of the project.
*
* @param versionNumber - Version number of the Remote Config template to look up.
*
* @returns A promise that fulfills with a `RemoteConfigTemplate`.
*/
public getTemplateAtVersion(versionNumber: number | string): Promise<RemoteConfigTemplate> {
return this.client.getTemplateAtVersion(versionNumber)
.then((templateResponse) => {
return new RemoteConfigTemplateImpl(templateResponse);
});
}
/**
* Validates a {@link RemoteConfigTemplate}.
*
* @param template - The Remote Config template to be validated.
* @returns A promise that fulfills with the validated `RemoteConfigTemplate`.
*/
public validateTemplate(template: RemoteConfigTemplate): Promise<RemoteConfigTemplate> {
return this.client.validateTemplate(template)
.then((templateResponse) => {
return new RemoteConfigTemplateImpl(templateResponse);
});
}
/**
* Publishes a Remote Config template.
*
* @param template - The Remote Config template to be published.
* @param options - Optional options object when publishing a Remote Config template:
* - `force`: Setting this to `true` forces the Remote Config template to
* be updated and circumvent the ETag. This approach is not recommended
* because it risks causing the loss of updates to your Remote Config
* template if multiple clients are updating the Remote Config template.
* See {@link https://firebase.google.com/docs/remote-config/use-config-rest#etag_usage_and_forced_updates |
* ETag usage and forced updates}.
*
* @returns A Promise that fulfills with the published `RemoteConfigTemplate`.
*/
public publishTemplate(template: RemoteConfigTemplate, options?: { force: boolean }): Promise<RemoteConfigTemplate> {
return this.client.publishTemplate(template, options)
.then((templateResponse) => {
return new RemoteConfigTemplateImpl(templateResponse);
});
}
/**
* Rolls back a project's published Remote Config template to the specified version.
* A rollback is equivalent to getting a previously published Remote Config
* template and re-publishing it using a force update.
*
* @param versionNumber - The version number of the Remote Config template to roll back to.
* The specified version number must be lower than the current version number, and not have
* been deleted due to staleness. Only the last 300 versions are stored.
* All versions that correspond to non-active Remote Config templates (that is, all except the
* template that is being fetched by clients) are also deleted if they are more than 90 days old.
* @returns A promise that fulfills with the published `RemoteConfigTemplate`.
*/
public rollback(versionNumber: number | string): Promise<RemoteConfigTemplate> {
return this.client.rollback(versionNumber)
.then((templateResponse) => {
return new RemoteConfigTemplateImpl(templateResponse);
});
}
/**
* Gets a list of Remote Config template versions that have been published, sorted in reverse
* chronological order. Only the last 300 versions are stored.
* All versions that correspond to non-active Remote Config templates (i.e., all except the
* template that is being fetched by clients) are also deleted if they are older than 90 days.
*
* @param options - Optional options object for getting a list of versions.
* @returns A promise that fulfills with a `ListVersionsResult`.
*/
public listVersions(options?: ListVersionsOptions): Promise<ListVersionsResult> {
return this.client.listVersions(options)
.then((listVersionsResponse) => {
return {
versions: listVersionsResponse.versions?.map(version => new VersionImpl(version)) ?? [],
nextPageToken: listVersionsResponse.nextPageToken,
}
});
}
/**
* Creates and returns a new Remote Config template from a JSON string.
*
* @param json - The JSON string to populate a Remote Config template.
*
* @returns A new template instance.
*/
public createTemplateFromJSON(json: string): RemoteConfigTemplate {
if (!validator.isNonEmptyString(json)) {
throw new FirebaseRemoteConfigError(
'invalid-argument',
'JSON string must be a valid non-empty string');
}
let template: RemoteConfigTemplate;
try {
template = JSON.parse(json);
} catch (e) {
throw new FirebaseRemoteConfigError(
'invalid-argument',
`Failed to parse the JSON string: ${json}. ` + e
);
}
return new RemoteConfigTemplateImpl(template);
}
/**
* Instantiates {@link ServerTemplate} and then fetches and caches the latest
* template version of the project.
*/
public async getServerTemplate(options?: GetServerTemplateOptions): Promise<ServerTemplate> {
const template = this.initServerTemplate(options);
await template.load();
return template;
}
/**
* Synchronously instantiates {@link ServerTemplate}.
*/
public initServerTemplate(options?: InitServerTemplateOptions): ServerTemplate {
const template = new ServerTemplateImpl(
this.client, new ConditionEvaluator(), options?.defaultConfig);
if (options?.template) {
template.set(options?.template);
}
return template;
}
}
/**
* Remote Config template internal implementation.
*/
class RemoteConfigTemplateImpl implements RemoteConfigTemplate {
public parameters: { [key: string]: RemoteConfigParameter };
public parameterGroups: { [key: string]: RemoteConfigParameterGroup };
public conditions: RemoteConfigCondition[];
private readonly etagInternal: string;
public version?: Version;
constructor(config: RemoteConfigTemplate) {
if (!validator.isNonNullObject(config) ||
!validator.isNonEmptyString(config.etag)) {
throw new FirebaseRemoteConfigError(
'invalid-argument',
`Invalid Remote Config template: ${JSON.stringify(config)}`);
}
this.etagInternal = config.etag;
if (typeof config.parameters !== 'undefined') {
if (!validator.isNonNullObject(config.parameters)) {
throw new FirebaseRemoteConfigError(
'invalid-argument',
'Remote Config parameters must be a non-null object');
}
this.parameters = config.parameters;
} else {
this.parameters = {};
}
if (typeof config.parameterGroups !== 'undefined') {
if (!validator.isNonNullObject(config.parameterGroups)) {
throw new FirebaseRemoteConfigError(
'invalid-argument',
'Remote Config parameter groups must be a non-null object');
}
this.parameterGroups = config.parameterGroups;
} else {
this.parameterGroups = {};
}
if (typeof config.conditions !== 'undefined') {
if (!validator.isArray(config.conditions)) {
throw new FirebaseRemoteConfigError(
'invalid-argument',
'Remote Config conditions must be an array');
}
this.conditions = config.conditions;
} else {
this.conditions = [];
}
if (typeof config.version !== 'undefined') {
this.version = new VersionImpl(config.version);
}
}
/**
* Gets the ETag of the template.
*
* @returns The ETag of the Remote Config template.
*/
get etag(): string {
return this.etagInternal;
}
/**
* Returns a JSON-serializable representation of this object.
*
* @returns A JSON-serializable representation of this object.
*/
public toJSON(): object {
return {
conditions: this.conditions,
parameters: this.parameters,
parameterGroups: this.parameterGroups,
etag: this.etag,
version: this.version,
}
}
}
/**
* Remote Config dataplane template data implementation.
*/
class ServerTemplateImpl implements ServerTemplate {
private cache: ServerTemplateData;
private stringifiedDefaultConfig: {[key: string]: string} = {};
constructor(
private readonly apiClient: RemoteConfigApiClient,
private readonly conditionEvaluator: ConditionEvaluator,
public readonly defaultConfig: DefaultConfig = {}
) {
// RC stores all remote values as string, but it's more intuitive
// to declare default values with specific types, so this converts
// the external declaration to an internal string representation.
for (const key in defaultConfig) {
this.stringifiedDefaultConfig[key] = String(defaultConfig[key]);
}
}
/**
* Fetches and caches the current active version of the project's {@link ServerTemplate}.
*/
public load(): Promise<void> {
return this.apiClient.getServerTemplate()
.then((template) => {
this.cache = new ServerTemplateDataImpl(template);
});
}
/**
* Parses a {@link ServerTemplateDataType} and caches it.
*/
public set(template: ServerTemplateDataType): void {
let parsed;
if (validator.isString(template)) {
try {
parsed = JSON.parse(template);
} catch (e) {
// Transforms JSON parse errors to Firebase error.
throw new FirebaseRemoteConfigError(
'invalid-argument',
`Failed to parse the JSON string: ${template}. ` + e);
}
} else {
parsed = template;
}
// Throws template parse errors.
this.cache = new ServerTemplateDataImpl(parsed);
}
/**
* Evaluates the current template in cache to produce a {@link ServerConfig}.
*/
public evaluate(context: EvaluationContext = {}): ServerConfig {
if (!this.cache) {
// This is the only place we should throw during evaluation, since it's under the
// control of application logic. To preserve forward-compatibility, we should only
// return false in cases where the SDK is unsure how to evaluate the fetched template.
throw new FirebaseRemoteConfigError(
'failed-precondition',
'No Remote Config Server template in cache. Call load() before calling evaluate().');
}
const evaluatedConditions = this.conditionEvaluator.evaluateConditions(
this.cache.conditions, context);
const configValues: { [key: string]: Value } = {};
// Initializes config Value objects with default values.
for (const key in this.stringifiedDefaultConfig) {
configValues[key] = new ValueImpl('default', this.stringifiedDefaultConfig[key]);
}
// Overlays config Value objects derived by evaluating the template.
for (const [key, parameter] of Object.entries(this.cache.parameters)) {
const { conditionalValues, defaultValue } = parameter;
// Supports parameters with no conditional values.
const normalizedConditionalValues = conditionalValues || {};
let parameterValueWrapper: RemoteConfigParameterValue | undefined = undefined;
// Iterates in order over condition list. If there is a value associated
// with a condition, this checks if the condition is true.
for (const [conditionName, conditionEvaluation] of evaluatedConditions) {
if (normalizedConditionalValues[conditionName] && conditionEvaluation) {
parameterValueWrapper = normalizedConditionalValues[conditionName];
break;
}
}
if (parameterValueWrapper && (parameterValueWrapper as InAppDefaultValue).useInAppDefault) {
// TODO: add logging once we have a wrapped logger.
continue;
}
if (parameterValueWrapper) {
const parameterValue = (parameterValueWrapper as ExplicitParameterValue).value;
configValues[key] = new ValueImpl('remote', parameterValue);
continue;
}
if (!defaultValue) {
// TODO: add logging once we have a wrapped logger.
continue;
}
if ((defaultValue as InAppDefaultValue).useInAppDefault) {
// TODO: add logging once we have a wrapped logger.
continue;
}
const parameterDefaultValue = (defaultValue as ExplicitParameterValue).value;
configValues[key] = new ValueImpl('remote', parameterDefaultValue);
}
return new ServerConfigImpl(configValues);
}
/**
* @returns JSON representation of the server template
*/
public toJSON(): ServerTemplateData {
return this.cache;
}
}
class ServerConfigImpl implements ServerConfig {
constructor(
private readonly configValues: { [key: string]: Value },
){}
getBoolean(key: string): boolean {
return this.getValue(key).asBoolean();
}
getNumber(key: string): number {
return this.getValue(key).asNumber();
}
getString(key: string): string {
return this.getValue(key).asString();
}
getValue(key: string): Value {
return this.configValues[key] || new ValueImpl('static');
}
}
/**
* Remote Config dataplane template data implementation.
*/
class ServerTemplateDataImpl implements ServerTemplateData {
public parameters: { [key: string]: RemoteConfigParameter };
public parameterGroups: { [key: string]: RemoteConfigParameterGroup };
public conditions: NamedCondition[];
public readonly etag: string;
public version?: Version;
constructor(template: ServerTemplateData) {
if (!validator.isNonNullObject(template) ||
!validator.isNonEmptyString(template.etag)) {
throw new FirebaseRemoteConfigError(
'invalid-argument',
`Invalid Remote Config template: ${JSON.stringify(template)}`);
}
this.etag = template.etag;
if (typeof template.parameters !== 'undefined') {
if (!validator.isNonNullObject(template.parameters)) {
throw new FirebaseRemoteConfigError(
'invalid-argument',
'Remote Config parameters must be a non-null object');
}
this.parameters = template.parameters;
} else {
this.parameters = {};
}
if (typeof template.conditions !== 'undefined') {
if (!validator.isArray(template.conditions)) {
throw new FirebaseRemoteConfigError(
'invalid-argument',
'Remote Config conditions must be an array');
}
this.conditions = template.conditions;
} else {
this.conditions = [];
}
if (typeof template.version !== 'undefined') {
this.version = new VersionImpl(template.version);
}
}
}
/**
* Remote Config Version internal implementation.
*/
class VersionImpl implements Version {
public readonly versionNumber?: string; // int64 format
public readonly updateTime?: string; // in UTC
public readonly updateOrigin?: ('REMOTE_CONFIG_UPDATE_ORIGIN_UNSPECIFIED' | 'CONSOLE' |
'REST_API' | 'ADMIN_SDK_NODE');
public readonly updateType?: ('REMOTE_CONFIG_UPDATE_TYPE_UNSPECIFIED' |
'INCREMENTAL_UPDATE' | 'FORCED_UPDATE' | 'ROLLBACK');
public readonly updateUser?: RemoteConfigUser;
public readonly description?: string;
public readonly rollbackSource?: string;
public readonly isLegacy?: boolean;
constructor(version: Version) {
if (!validator.isNonNullObject(version)) {
throw new FirebaseRemoteConfigError(
'invalid-argument',
`Invalid Remote Config version instance: ${JSON.stringify(version)}`);
}
if (typeof version.versionNumber !== 'undefined') {
if (!validator.isNonEmptyString(version.versionNumber) &&
!validator.isNumber(version.versionNumber)) {
throw new FirebaseRemoteConfigError(
'invalid-argument',
'Version number must be a non-empty string in int64 format or a number');
}
if (!Number.isInteger(Number(version.versionNumber))) {
throw new FirebaseRemoteConfigError(
'invalid-argument',
'Version number must be an integer or a string in int64 format');
}
this.versionNumber = version.versionNumber;
}
if (typeof version.updateOrigin !== 'undefined') {
if (!validator.isNonEmptyString(version.updateOrigin)) {
throw new FirebaseRemoteConfigError(
'invalid-argument',
'Version update origin must be a non-empty string');
}
this.updateOrigin = version.updateOrigin;
}
if (typeof version.updateType !== 'undefined') {
if (!validator.isNonEmptyString(version.updateType)) {
throw new FirebaseRemoteConfigError(
'invalid-argument',
'Version update type must be a non-empty string');
}
this.updateType = version.updateType;
}
if (typeof version.updateUser !== 'undefined') {
if (!validator.isNonNullObject(version.updateUser)) {
throw new FirebaseRemoteConfigError(
'invalid-argument',
'Version update user must be a non-null object');
}
this.updateUser = version.updateUser;
}
if (typeof version.description !== 'undefined') {
if (!validator.isNonEmptyString(version.description)) {
throw new FirebaseRemoteConfigError(
'invalid-argument',
'Version description must be a non-empty string');
}
this.description = version.description;
}
if (typeof version.rollbackSource !== 'undefined') {
if (!validator.isNonEmptyString(version.rollbackSource)) {
throw new FirebaseRemoteConfigError(
'invalid-argument',
'Version rollback source must be a non-empty string');
}
this.rollbackSource = version.rollbackSource;
}
if (typeof version.isLegacy !== 'undefined') {
if (!validator.isBoolean(version.isLegacy)) {
throw new FirebaseRemoteConfigError(
'invalid-argument',
'Version.isLegacy must be a boolean');
}
this.isLegacy = version.isLegacy;
}
// The backend API provides timestamps in ISO date strings. The Admin SDK exposes timestamps
// in UTC date strings. If a developer uses a previously obtained template with UTC timestamps
// we could still validate it below.
if (typeof version.updateTime !== 'undefined') {
if (!this.isValidTimestamp(version.updateTime)) {
throw new FirebaseRemoteConfigError(
'invalid-argument',
'Version update time must be a valid date string');
}
this.updateTime = new Date(version.updateTime).toUTCString();
}
}
/**
* @returns A JSON-serializable representation of this object.
*/
public toJSON(): object {
return {
versionNumber: this.versionNumber,
updateOrigin: this.updateOrigin,
updateType: this.updateType,
updateUser: this.updateUser,
description: this.description,
rollbackSource: this.rollbackSource,
isLegacy: this.isLegacy,
updateTime: this.updateTime,
}
}
private isValidTimestamp(timestamp: string): boolean {
// This validation fails for timestamps earlier than January 1, 1970 and considers strings
// such as "1.2" as valid timestamps.
return validator.isNonEmptyString(timestamp) && (new Date(timestamp)).getTime() > 0;
}
}