@firebase/remote-config 0.5.0-canary.554c7bdc1 → 0.5.0-canary.69c33266d

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.
@@ -1,11 +1,11 @@
1
1
  import { _getProvider, getApp, _registerComponent, registerVersion, SDK_VERSION } from '@firebase/app';
2
- import { ErrorFactory, FirebaseError, getModularInstance, deepEqual, calculateBackoffMillis, isIndexedDBAvailable, validateIndexedDBOpenable } from '@firebase/util';
2
+ import { ErrorFactory, FirebaseError, getModularInstance, calculateBackoffMillis, isIndexedDBAvailable, validateIndexedDBOpenable } from '@firebase/util';
3
3
  import { Component } from '@firebase/component';
4
4
  import { LogLevel, Logger } from '@firebase/logger';
5
5
  import '@firebase/installations';
6
6
 
7
7
  const name = "@firebase/remote-config";
8
- const version = "0.5.0-canary.554c7bdc1";
8
+ const version = "0.5.0-canary.69c33266d";
9
9
 
10
10
  /**
11
11
  * @license
@@ -81,7 +81,6 @@ const RC_CUSTOM_SIGNAL_VALUE_MAX_LENGTH = 500;
81
81
  * limitations under the License.
82
82
  */
83
83
  const ERROR_DESCRIPTION_MAP = {
84
- ["already-initialized" /* ErrorCode.ALREADY_INITIALIZED */]: 'Remote Config already initialized',
85
84
  ["registration-window" /* ErrorCode.REGISTRATION_WINDOW */]: 'Undefined window object. This SDK only supports usage in a browser environment.',
86
85
  ["registration-project-id" /* ErrorCode.REGISTRATION_PROJECT_ID */]: 'Undefined project identifier. Check Firebase app initialization.',
87
86
  ["registration-api-key" /* ErrorCode.REGISTRATION_API_KEY */]: 'Undefined API key. Check Firebase app initialization.',
@@ -177,40 +176,14 @@ class Value {
177
176
  /**
178
177
  *
179
178
  * @param app - The {@link @firebase/app#FirebaseApp} instance.
180
- * @param options - Optional. The {@link RemoteConfigOptions} with which to instantiate the
181
- * Remote Config instance.
182
179
  * @returns A {@link RemoteConfig} instance.
183
180
  *
184
181
  * @public
185
182
  */
186
- function getRemoteConfig(app = getApp(), options = {}) {
187
- var _a, _b;
183
+ function getRemoteConfig(app = getApp()) {
188
184
  app = getModularInstance(app);
189
185
  const rcProvider = _getProvider(app, RC_COMPONENT_NAME);
190
- if (rcProvider.isInitialized()) {
191
- const initialOptions = rcProvider.getOptions();
192
- if (deepEqual(initialOptions, options)) {
193
- return rcProvider.getImmediate();
194
- }
195
- throw ERROR_FACTORY.create("already-initialized" /* ErrorCode.ALREADY_INITIALIZED */);
196
- }
197
- rcProvider.initialize({ options });
198
- const rc = rcProvider.getImmediate();
199
- if (options.initialFetchResponse) {
200
- // We use these initial writes as the initialization promise since they will hydrate the same
201
- // fields that `storageCache.loadFromStorage` would set.
202
- rc._initializePromise = Promise.all([
203
- rc._storage.setLastSuccessfulFetchResponse(options.initialFetchResponse),
204
- rc._storage.setActiveConfigEtag(((_a = options.initialFetchResponse) === null || _a === void 0 ? void 0 : _a.eTag) || ''),
205
- rc._storageCache.setLastSuccessfulFetchTimestampMillis(Date.now()),
206
- rc._storageCache.setLastFetchStatus('success'),
207
- rc._storageCache.setActiveConfig(((_b = options.initialFetchResponse) === null || _b === void 0 ? void 0 : _b.config) || {})
208
- ]).then();
209
- // The `storageCache` methods above set their in-memory fields synchronously, so it's
210
- // safe to declare our initialization complete at this point.
211
- rc._isInitializationComplete = true;
212
- }
213
- return rc;
186
+ return rcProvider.getImmediate();
214
187
  }
215
188
  /**
216
189
  * Makes the last fetched config available to the getters.
@@ -956,6 +929,17 @@ function openDatabase() {
956
929
  * Abstracts data persistence.
957
930
  */
958
931
  class Storage {
932
+ /**
933
+ * @param appId enables storage segmentation by app (ID + name).
934
+ * @param appName enables storage segmentation by app (ID + name).
935
+ * @param namespace enables storage segmentation by namespace.
936
+ */
937
+ constructor(appId, appName, namespace, openDbPromise = openDatabase()) {
938
+ this.appId = appId;
939
+ this.appName = appName;
940
+ this.namespace = namespace;
941
+ this.openDbPromise = openDbPromise;
942
+ }
959
943
  getLastFetchStatus() {
960
944
  return this.get('last_fetch_status');
961
945
  }
@@ -1000,25 +984,28 @@ class Storage {
1000
984
  getCustomSignals() {
1001
985
  return this.get('custom_signals');
1002
986
  }
1003
- }
1004
- class IndexedDbStorage extends Storage {
1005
- /**
1006
- * @param appId enables storage segmentation by app (ID + name).
1007
- * @param appName enables storage segmentation by app (ID + name).
1008
- * @param namespace enables storage segmentation by namespace.
1009
- */
1010
- constructor(appId, appName, namespace, openDbPromise = openDatabase()) {
1011
- super();
1012
- this.appId = appId;
1013
- this.appName = appName;
1014
- this.namespace = namespace;
1015
- this.openDbPromise = openDbPromise;
1016
- }
1017
987
  async setCustomSignals(customSignals) {
1018
988
  const db = await this.openDbPromise;
1019
989
  const transaction = db.transaction([APP_NAMESPACE_STORE], 'readwrite');
1020
990
  const storedSignals = await this.getWithTransaction('custom_signals', transaction);
1021
- const updatedSignals = mergeCustomSignals(customSignals, storedSignals || {});
991
+ const combinedSignals = Object.assign(Object.assign({}, storedSignals), customSignals);
992
+ // Filter out key-value assignments with null values since they are signals being unset
993
+ const updatedSignals = Object.fromEntries(Object.entries(combinedSignals)
994
+ .filter(([_, v]) => v !== null)
995
+ .map(([k, v]) => {
996
+ // Stringify numbers to store a map of string keys and values which can be sent
997
+ // as-is in a fetch call.
998
+ if (typeof v === 'number') {
999
+ return [k, v.toString()];
1000
+ }
1001
+ return [k, v];
1002
+ }));
1003
+ // Throw an error if the number of custom signals to be stored exceeds the limit
1004
+ if (Object.keys(updatedSignals).length > RC_CUSTOM_SIGNAL_MAX_ALLOWED_SIGNALS) {
1005
+ throw ERROR_FACTORY.create("custom-signal-max-allowed-signals" /* ErrorCode.CUSTOM_SIGNAL_MAX_ALLOWED_SIGNALS */, {
1006
+ maxSignals: RC_CUSTOM_SIGNAL_MAX_ALLOWED_SIGNALS
1007
+ });
1008
+ }
1022
1009
  await this.setWithTransaction('custom_signals', updatedSignals, transaction);
1023
1010
  return updatedSignals;
1024
1011
  }
@@ -1123,50 +1110,6 @@ class IndexedDbStorage extends Storage {
1123
1110
  return [this.appId, this.appName, this.namespace, key].join();
1124
1111
  }
1125
1112
  }
1126
- class InMemoryStorage extends Storage {
1127
- constructor() {
1128
- super(...arguments);
1129
- this.storage = {};
1130
- }
1131
- async get(key) {
1132
- return Promise.resolve(this.storage[key]);
1133
- }
1134
- async set(key, value) {
1135
- this.storage[key] = value;
1136
- return Promise.resolve(undefined);
1137
- }
1138
- async delete(key) {
1139
- this.storage[key] = undefined;
1140
- return Promise.resolve();
1141
- }
1142
- async setCustomSignals(customSignals) {
1143
- const storedSignals = (this.storage['custom_signals'] ||
1144
- {});
1145
- this.storage['custom_signals'] = mergeCustomSignals(customSignals, storedSignals);
1146
- return Promise.resolve(this.storage['custom_signals']);
1147
- }
1148
- }
1149
- function mergeCustomSignals(customSignals, storedSignals) {
1150
- const combinedSignals = Object.assign(Object.assign({}, storedSignals), customSignals);
1151
- // Filter out key-value assignments with null values since they are signals being unset
1152
- const updatedSignals = Object.fromEntries(Object.entries(combinedSignals)
1153
- .filter(([_, v]) => v !== null)
1154
- .map(([k, v]) => {
1155
- // Stringify numbers to store a map of string keys and values which can be sent
1156
- // as-is in a fetch call.
1157
- if (typeof v === 'number') {
1158
- return [k, v.toString()];
1159
- }
1160
- return [k, v];
1161
- }));
1162
- // Throw an error if the number of custom signals to be stored exceeds the limit
1163
- if (Object.keys(updatedSignals).length > RC_CUSTOM_SIGNAL_MAX_ALLOWED_SIGNALS) {
1164
- throw ERROR_FACTORY.create("custom-signal-max-allowed-signals" /* ErrorCode.CUSTOM_SIGNAL_MAX_ALLOWED_SIGNALS */, {
1165
- maxSignals: RC_CUSTOM_SIGNAL_MAX_ALLOWED_SIGNALS
1166
- });
1167
- }
1168
- return updatedSignals;
1169
- }
1170
1113
 
1171
1114
  /**
1172
1115
  * @license
@@ -1278,7 +1221,7 @@ function registerRemoteConfig() {
1278
1221
  registerVersion(name, version);
1279
1222
  // BUILD_TARGET will be replaced by values like esm2017, cjs2017, etc during the compilation
1280
1223
  registerVersion(name, version, 'esm2017');
1281
- function remoteConfigFactory(container, { options }) {
1224
+ function remoteConfigFactory(container, { instanceIdentifier: namespace }) {
1282
1225
  /* Dependencies */
1283
1226
  // getImmediate for FirebaseApp will always succeed
1284
1227
  const app = container.getProvider('app').getImmediate();
@@ -1286,6 +1229,14 @@ function registerRemoteConfig() {
1286
1229
  const installations = container
1287
1230
  .getProvider('installations-internal')
1288
1231
  .getImmediate();
1232
+ // Guards against the SDK being used in non-browser environments.
1233
+ if (typeof window === 'undefined') {
1234
+ throw ERROR_FACTORY.create("registration-window" /* ErrorCode.REGISTRATION_WINDOW */);
1235
+ }
1236
+ // Guards against the SDK being used when indexedDB is not available.
1237
+ if (!isIndexedDBAvailable()) {
1238
+ throw ERROR_FACTORY.create("indexed-db-unavailable" /* ErrorCode.INDEXED_DB_UNAVAILABLE */);
1239
+ }
1289
1240
  // Normalizes optional inputs.
1290
1241
  const { projectId, apiKey, appId } = app.options;
1291
1242
  if (!projectId) {
@@ -1297,10 +1248,8 @@ function registerRemoteConfig() {
1297
1248
  if (!appId) {
1298
1249
  throw ERROR_FACTORY.create("registration-app-id" /* ErrorCode.REGISTRATION_APP_ID */);
1299
1250
  }
1300
- const namespace = (options === null || options === void 0 ? void 0 : options.templateId) || 'firebase';
1301
- const storage = isIndexedDBAvailable()
1302
- ? new IndexedDbStorage(appId, app.name, namespace)
1303
- : new InMemoryStorage();
1251
+ namespace = namespace || 'firebase';
1252
+ const storage = new Storage(appId, app.name, namespace);
1304
1253
  const storageCache = new StorageCache(storage);
1305
1254
  const logger = new Logger(name);
1306
1255
  // Sets ERROR as the default log level.