Skip to content

Commit 8aa5d9f

Browse files
committed
Make installation provider optional
1 parent 91f8bbb commit 8aa5d9f

File tree

3 files changed

+32
-7
lines changed

3 files changed

+32
-7
lines changed

packages/telemetry/src/logging/installation-id-provider.test.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,12 @@ describe('InstallationIdProvider', () => {
2929
}
3030
} as unknown as _FirebaseInstallationsInternal;
3131

32-
const provider = new InstallationIdProvider(mockInstallations);
32+
const mockProvider = {
33+
getImmediate: () => mockInstallations,
34+
get: async () => mockInstallations
35+
} as any;
36+
37+
const provider = new InstallationIdProvider(mockProvider);
3338

3439
const attr1 = await provider.getAttribute();
3540
expect(attr1).to.deep.equal(['user.id', 'iid-123']);
@@ -50,7 +55,12 @@ describe('InstallationIdProvider', () => {
5055
}
5156
} as unknown as _FirebaseInstallationsInternal;
5257

53-
const provider = new InstallationIdProvider(mockInstallations);
58+
const mockProvider = {
59+
getImmediate: () => mockInstallations,
60+
get: async () => mockInstallations
61+
} as any;
62+
63+
const provider = new InstallationIdProvider(mockProvider);
5464

5565
const attr1 = await provider.getAttribute();
5666
expect(attr1).to.be.null;

packages/telemetry/src/logging/installation-id-provider.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
* limitations under the License.
1616
*/
1717

18+
import { Provider } from '@firebase/component';
1819
import { DynamicLogAttributeProvider, LogEntryAttribute } from '../types';
1920
import { _FirebaseInstallationsInternal } from '@firebase/installations';
2021

@@ -24,16 +25,30 @@ import { _FirebaseInstallationsInternal } from '@firebase/installations';
2425
* @internal
2526
*/
2627
export class InstallationIdProvider implements DynamicLogAttributeProvider {
28+
private installations: _FirebaseInstallationsInternal | null;
2729
private _iid: string | undefined;
2830

29-
constructor(private installationsProvider: _FirebaseInstallationsInternal) {}
31+
constructor(installationsProvider: Provider<'installations-internal'>) {
32+
this.installations = installationsProvider?.getImmediate({
33+
optional: true
34+
});
35+
if (!this.installations) {
36+
void installationsProvider
37+
?.get()
38+
.then(installations => (this.installations = installations))
39+
.catch();
40+
}
41+
}
3042

3143
async getAttribute(): Promise<LogEntryAttribute | null> {
44+
if (!this.installations) {
45+
return null;
46+
}
3247
if (this._iid) {
3348
return ['user.id', this._iid];
3449
}
3550

36-
const iid = await this.installationsProvider.getId();
51+
const iid = await this.installations.getId();
3752
if (!iid) {
3853
return null;
3954
}

packages/telemetry/src/register.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ export function registerTelemetry(): void {
4343
// getImmediate for FirebaseApp will always succeed
4444
const app = container.getProvider('app').getImmediate();
4545
const appCheckProvider = container.getProvider('app-check-internal');
46-
const installationsProvider = container
47-
.getProvider('installations-internal')
48-
.getImmediate();
46+
const installationsProvider = container.getProvider(
47+
'installations-internal'
48+
);
4949
const dynamicHeaderProviders = [new AppCheckProvider(appCheckProvider)];
5050
const dynamicLogAttributeProviders = [
5151
new InstallationIdProvider(installationsProvider)

0 commit comments

Comments
 (0)