Skip to content

Commit 4593a88

Browse files
authored
Merge aa00008 into 8bece48
2 parents 8bece48 + aa00008 commit 4593a88

File tree

5 files changed

+54
-8
lines changed

5 files changed

+54
-8
lines changed

.changeset/fair-garlics-smoke.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@firebase/app': patch
3+
'@firebase/app-check': patch
4+
---
5+
6+
Make App Check initialization explicit, to prevent unexpected errors for users who do not intend to use App Check.

packages/app-check/src/index.ts

+26-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@
1616
*/
1717
import firebase from '@firebase/app';
1818
import { _FirebaseNamespace } from '@firebase/app-types/private';
19-
import { Component, ComponentType } from '@firebase/component';
19+
import {
20+
Component,
21+
ComponentType,
22+
InstantiationMode
23+
} from '@firebase/component';
2024
import {
2125
FirebaseAppCheck,
2226
AppCheckComponentName
@@ -41,6 +45,26 @@ function registerAppCheck(firebase: _FirebaseNamespace): void {
4145
},
4246
ComponentType.PUBLIC
4347
)
48+
/**
49+
* AppCheck can only be initialized by explicitly calling firebase.appCheck()
50+
* We don't want firebase products that consume AppCheck to gate on AppCheck
51+
* if the user doesn't intend them to, just because the AppCheck component
52+
* is registered.
53+
*/
54+
.setInstantiationMode(InstantiationMode.EXPLICIT)
55+
/**
56+
* Because all firebase products that depend on app-check depend on app-check-internal directly,
57+
* we need to initialize app-check-internal after app-check is initialized to make it
58+
* available to other firebase products.
59+
*/
60+
.setInstanceCreatedCallback(
61+
(container, _instanceIdentifier, _instance) => {
62+
const appCheckInternalProvider = container.getProvider(
63+
APP_CHECK_NAME_INTERNAL
64+
);
65+
appCheckInternalProvider.initialize();
66+
}
67+
)
4468
);
4569

4670
// The internal interface used by other Firebase products
@@ -54,7 +78,7 @@ function registerAppCheck(firebase: _FirebaseNamespace): void {
5478
return internalFactory(app, platformLoggerProvider);
5579
},
5680
ComponentType.PUBLIC
57-
)
81+
).setInstantiationMode(InstantiationMode.EXPLICIT)
5882
);
5983

6084
firebase.registerVersion(name, version);

packages/app/src/firebaseApp.ts

+12-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ import {
2929
ComponentContainer,
3030
Component,
3131
ComponentType,
32-
Name
32+
Name,
33+
InstantiationMode
3334
} from '@firebase/component';
3435
import { AppError, ERROR_FACTORY } from './errors';
3536
import { DEFAULT_ENTRY_NAME } from './constants';
@@ -122,8 +123,17 @@ export class FirebaseAppImpl implements FirebaseApp {
122123
): FirebaseService {
123124
this.checkDestroyed_();
124125

126+
// Initialize instance if InstatiationMode is `EXPLICIT`.
127+
const provider = this.container.getProvider(name as Name);
128+
if (
129+
!provider.isInitialized() &&
130+
provider.getComponent()?.instantiationMode === InstantiationMode.EXPLICIT
131+
) {
132+
provider.initialize();
133+
}
134+
125135
// getImmediate will always succeed because _getService is only called for registered components.
126-
return (this.container.getProvider(name as Name).getImmediate({
136+
return (provider.getImmediate({
127137
identifier: instanceIdentifier
128138
}) as unknown) as FirebaseService;
129139
}

packages/app/test/clientLogger.test.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@
1616
*/
1717

1818
import { FirebaseNamespace, VersionService } from '@firebase/app-types';
19-
import { _FirebaseNamespace } from '@firebase/app-types/private';
19+
import {
20+
_FirebaseNamespace,
21+
FirebaseService
22+
} from '@firebase/app-types/private';
2023
import { createFirebaseNamespace } from '../src/firebaseNamespace';
2124
import { expect } from 'chai';
2225
import { spy as Spy } from 'sinon';
@@ -29,7 +32,7 @@ declare module '@firebase/component' {
2932
interface NameServiceMapping {
3033
'vs1': VersionService;
3134
'vs2': VersionService;
32-
'test-shell': Promise<void>;
35+
'test-shell': FirebaseService;
3336
}
3437
}
3538

packages/app/test/platformLogger.test.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@
1616
*/
1717

1818
import { FirebaseNamespace, VersionService } from '@firebase/app-types';
19-
import { _FirebaseNamespace } from '@firebase/app-types/private';
19+
import {
20+
FirebaseService,
21+
_FirebaseNamespace
22+
} from '@firebase/app-types/private';
2023
import { createFirebaseNamespace } from '../src/firebaseNamespace';
2124
import { expect } from 'chai';
2225
import './setup';
@@ -33,7 +36,7 @@ declare module '@firebase/component' {
3336
interface NameServiceMapping {
3437
'vs1': VersionService;
3538
'vs2': VersionService;
36-
'test-shell': Promise<void>;
39+
'test-shell': FirebaseService;
3740
}
3841
}
3942

0 commit comments

Comments
 (0)