Skip to content

Commit 5c0662e

Browse files
committed
Merge branch 'master' into fei-v9-main
2 parents 568459d + 7818176 commit 5c0662e

File tree

13 files changed

+151
-37
lines changed

13 files changed

+151
-37
lines changed

packages-exp/functions-exp/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
"typings": "dist/functions-exp-public.d.ts",
5252
"dependencies": {
5353
"@firebase/component": "0.5.6",
54-
"@firebase/messaging-types": "0.5.0",
54+
"@firebase/messaging-interop-types": "0.0.1",
5555
"@firebase/auth-interop-types": "0.1.6",
5656
"@firebase/app-check-interop-types": "0.1.0",
5757
"@firebase/util": "1.3.0",

packages-exp/functions-exp/src/callable.test.ts

+14-10
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ import {
2525
ComponentType
2626
} from '@firebase/component';
2727
import {
28-
FirebaseMessaging,
29-
FirebaseMessagingName
30-
} from '@firebase/messaging-types';
28+
MessagingInternal,
29+
MessagingInternalComponentName
30+
} from '@firebase/messaging-interop-types';
3131
import {
3232
FirebaseAuthInternal,
3333
FirebaseAuthInternalName
@@ -108,9 +108,9 @@ describe('Firebase Functions > Call', () => {
108108

109109
it('token', async () => {
110110
// mock auth-internal service
111-
const authMock: FirebaseAuthInternal = ({
111+
const authMock: FirebaseAuthInternal = {
112112
getToken: async () => ({ accessToken: 'token' })
113-
} as unknown) as FirebaseAuthInternal;
113+
} as unknown as FirebaseAuthInternal;
114114
const authProvider = new Provider<FirebaseAuthInternalName>(
115115
'auth-internal',
116116
new ComponentContainer('test')
@@ -139,15 +139,19 @@ describe('Firebase Functions > Call', () => {
139139
return;
140140
}
141141
// mock firebase messaging
142-
const messagingMock: FirebaseMessaging = ({
142+
const messagingMock: MessagingInternal = {
143143
getToken: async () => 'iid'
144-
} as unknown) as FirebaseMessaging;
145-
const messagingProvider = new Provider<FirebaseMessagingName>(
146-
'messaging',
144+
} as unknown as MessagingInternal;
145+
const messagingProvider = new Provider<MessagingInternalComponentName>(
146+
'messaging-internal',
147147
new ComponentContainer('test')
148148
);
149149
messagingProvider.setComponent(
150-
new Component('messaging', () => messagingMock, ComponentType.PRIVATE)
150+
new Component(
151+
'messaging-internal',
152+
() => messagingMock,
153+
ComponentType.PRIVATE
154+
)
151155
);
152156

153157
const functions = createTestService(

packages-exp/functions-exp/src/config.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,25 @@ import {
2424
InstanceFactory
2525
} from '@firebase/component';
2626
import { FUNCTIONS_TYPE } from './constants';
27+
import { FirebaseAuthInternalName } from '@firebase/auth-interop-types';
2728
import { AppCheckInternalComponentName } from '@firebase/app-check-interop-types';
29+
import { MessagingInternalComponentName } from '@firebase/messaging-interop-types';
2830

31+
const AUTH_INTERNAL_NAME: FirebaseAuthInternalName = 'auth-internal';
2932
const APP_CHECK_INTERNAL_NAME: AppCheckInternalComponentName =
3033
'app-check-internal';
34+
const MESSAGING_INTERNAL_NAME: MessagingInternalComponentName =
35+
'messaging-internal';
36+
3137
export function registerFunctions(fetchImpl: typeof fetch): void {
3238
const factory: InstanceFactory<'functions-exp'> = (
3339
container: ComponentContainer,
3440
{ instanceIdentifier: regionOrCustomDomain }
3541
) => {
3642
// Dependencies
3743
const app = container.getProvider('app-exp').getImmediate();
38-
const authProvider = container.getProvider('auth-internal');
39-
const messagingProvider = container.getProvider('messaging');
44+
const authProvider = container.getProvider(AUTH_INTERNAL_NAME);
45+
const messagingProvider = container.getProvider(MESSAGING_INTERNAL_NAME);
4046
const appCheckProvider = container.getProvider(APP_CHECK_INTERNAL_NAME);
4147

4248
// eslint-disable-next-line @typescript-eslint/no-explicit-any

packages-exp/functions-exp/src/context.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,16 @@ import {
1818
* See the License for the specific language governing permissions and
1919
* limitations under the License.
2020
*/
21-
import {
22-
FirebaseMessaging,
23-
FirebaseMessagingName
24-
} from '@firebase/messaging-types';
2521

2622
import { Provider } from '@firebase/component';
2723
import {
2824
AppCheckInternalComponentName,
2925
FirebaseAppCheckInternal
3026
} from '@firebase/app-check-interop-types';
27+
import {
28+
MessagingInternal,
29+
MessagingInternalComponentName
30+
} from '@firebase/messaging-interop-types';
3131

3232
/**
3333
* The metadata that should be supplied with function calls.
@@ -45,11 +45,11 @@ export interface Context {
4545
*/
4646
export class ContextProvider {
4747
private auth: FirebaseAuthInternal | null = null;
48-
private messaging: FirebaseMessaging | null = null;
48+
private messaging: MessagingInternal | null = null;
4949
private appCheck: FirebaseAppCheckInternal | null = null;
5050
constructor(
5151
authProvider: Provider<FirebaseAuthInternalName>,
52-
messagingProvider: Provider<FirebaseMessagingName>,
52+
messagingProvider: Provider<MessagingInternalComponentName>,
5353
appCheckProvider: Provider<AppCheckInternalComponentName>
5454
) {
5555
this.auth = authProvider.getImmediate({ optional: true });

packages-exp/functions-exp/src/service.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import { ContextProvider } from './context';
2626
import { encode, decode } from './serializer';
2727
import { Provider } from '@firebase/component';
2828
import { FirebaseAuthInternalName } from '@firebase/auth-interop-types';
29-
import { FirebaseMessagingName } from '@firebase/messaging-types';
29+
import { MessagingInternalComponentName } from '@firebase/messaging-interop-types';
3030
import { AppCheckInternalComponentName } from '@firebase/app-check-interop-types';
3131

3232
export const DEFAULT_REGION = 'us-central1';
@@ -86,7 +86,7 @@ export class FunctionsService implements _FirebaseService {
8686
constructor(
8787
readonly app: FirebaseApp,
8888
authProvider: Provider<FirebaseAuthInternalName>,
89-
messagingProvider: Provider<FirebaseMessagingName>,
89+
messagingProvider: Provider<MessagingInternalComponentName>,
9090
appCheckProvider: Provider<AppCheckInternalComponentName>,
9191
regionOrCustomDomain: string = DEFAULT_REGION,
9292
readonly fetchImpl: typeof fetch

packages-exp/functions-exp/test/utils.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@
1818
import { FirebaseOptions, FirebaseApp } from '@firebase/app-exp';
1919
import { Provider, ComponentContainer } from '@firebase/component';
2020
import { FirebaseAuthInternalName } from '@firebase/auth-interop-types';
21-
import { FirebaseMessagingName } from '@firebase/messaging-types';
2221
import { AppCheckInternalComponentName } from '@firebase/app-check-interop-types';
2322
import { FunctionsService } from '../src/service';
2423
import { connectFunctionsEmulator } from '../src/api';
2524
import nodeFetch from 'node-fetch';
25+
import { MessagingInternalComponentName } from '../../../packages/messaging-interop-types';
2626

2727
export function makeFakeApp(options: FirebaseOptions = {}): FirebaseApp {
2828
options = {
@@ -49,8 +49,8 @@ export function createTestService(
4949
'auth-internal',
5050
new ComponentContainer('test')
5151
),
52-
messagingProvider = new Provider<FirebaseMessagingName>(
53-
'messaging',
52+
messagingProvider = new Provider<MessagingInternalComponentName>(
53+
'messaging-internal',
5454
new ComponentContainer('test')
5555
),
5656
appCheckProvider = new Provider<AppCheckInternalComponentName>(

packages-exp/messaging-exp/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
"dependencies": {
3939
"@firebase/component": "0.5.6",
4040
"@firebase/installations-exp": "0.0.900",
41+
"@firebase/messaging-interop-types": "0.0.1",
4142
"@firebase/util": "1.3.0",
4243
"idb": "3.0.2",
4344
"tslib": "^2.1.0"

packages-exp/messaging-exp/src/helpers/register.ts

+44-13
Original file line numberDiff line numberDiff line change
@@ -29,27 +29,18 @@ import {
2929
onSubChange
3030
} from '../listeners/sw-listeners';
3131

32+
import { GetTokenOptions } from '../interfaces/public-types';
33+
import { MessagingInternal } from '@firebase/messaging-interop-types';
3234
import { MessagingService } from '../messaging-service';
3335
import { ServiceWorkerGlobalScope } from '../util/sw-types';
3436
import { _registerComponent } from '@firebase/app-exp';
37+
import { getToken } from '../api/getToken';
3538
import { messageEventListener } from '../listeners/window-listener';
3639

3740
const WindowMessagingFactory: InstanceFactory<'messaging-exp'> = (
3841
container: ComponentContainer
3942
) => {
40-
// Conscious decision to make this async check non-blocking during the messaging instance
41-
// initialization phase for performance consideration. An error would be thrown latter for
42-
// developer's information. Developers can then choose to import and call `isSupported` for
43-
// special handling.
44-
isWindowSupported()
45-
.then(isSupported => {
46-
if (!isSupported) {
47-
throw ERROR_FACTORY.create(ErrorCode.UNSUPPORTED_BROWSER);
48-
}
49-
})
50-
.catch(_ => {
51-
throw ERROR_FACTORY.create(ErrorCode.INDEXED_DB_UNSUPPORTED);
52-
});
43+
maybeThrowWindowError();
5344

5445
const messaging = new MessagingService(
5546
container.getProvider('app-exp').getImmediate(),
@@ -64,6 +55,38 @@ const WindowMessagingFactory: InstanceFactory<'messaging-exp'> = (
6455
return messaging;
6556
};
6657

58+
const WindowMessagingInternalFactory: InstanceFactory<'messaging-internal'> = (
59+
container: ComponentContainer
60+
) => {
61+
maybeThrowWindowError();
62+
63+
const messaging = container
64+
.getProvider('messaging-exp')
65+
.getImmediate() as MessagingService;
66+
67+
const messagingInternal: MessagingInternal = {
68+
getToken: (options?: GetTokenOptions) => getToken(messaging, options)
69+
};
70+
71+
return messagingInternal;
72+
};
73+
74+
function maybeThrowWindowError(): void {
75+
// Conscious decision to make this async check non-blocking during the messaging instance
76+
// initialization phase for performance consideration. An error would be thrown latter for
77+
// developer's information. Developers can then choose to import and call `isSupported` for
78+
// special handling.
79+
isWindowSupported()
80+
.then(isSupported => {
81+
if (!isSupported) {
82+
throw ERROR_FACTORY.create(ErrorCode.UNSUPPORTED_BROWSER);
83+
}
84+
})
85+
.catch(_ => {
86+
throw ERROR_FACTORY.create(ErrorCode.INDEXED_DB_UNSUPPORTED);
87+
});
88+
}
89+
6790
declare const self: ServiceWorkerGlobalScope;
6891
const SwMessagingFactory: InstanceFactory<'messaging-exp'> = (
6992
container: ComponentContainer
@@ -105,6 +128,14 @@ export function registerMessagingInWindow(): void {
105128
_registerComponent(
106129
new Component('messaging-exp', WindowMessagingFactory, ComponentType.PUBLIC)
107130
);
131+
132+
_registerComponent(
133+
new Component(
134+
'messaging-internal',
135+
WindowMessagingInternalFactory,
136+
ComponentType.PRIVATE
137+
)
138+
);
108139
}
109140

110141
/**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# @firebase/messaging-interop-types
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# @firebase/messaging-interop-types
2+
3+
**This package is not intended for direct usage, and should only be used via the officially
4+
supported [firebase](https://www.npmjs.com/package/firebase) package.**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* @license
3+
* Copyright 2021 Google LLC
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
export interface MessagingInternal {
19+
getToken(options?: GetTokenOptions): Promise<string>;
20+
}
21+
22+
interface GetTokenOptions {
23+
vapidKey?: string;
24+
serviceWorkerRegistration?: ServiceWorkerRegistration;
25+
}
26+
27+
export type MessagingInternalComponentName = 'messaging-internal';
28+
29+
declare module '@firebase/component' {
30+
interface NameServiceMapping {
31+
'messaging-internal': MessagingInternal;
32+
}
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"name": "@firebase/messaging-interop-types",
3+
"version": "0.0.1",
4+
"description": "@firebase/messaging-interop-types Types",
5+
"author": "Firebase <[email protected]> (https://firebase.google.com/)",
6+
"license": "Apache-2.0",
7+
"scripts": {
8+
"test": "tsc",
9+
"test:ci": "node ../../scripts/run_tests_in_ci.js"
10+
},
11+
"files": [
12+
"index.d.ts"
13+
],
14+
"repository": {
15+
"directory": "packages/messaging-interop-types",
16+
"type": "git",
17+
"url": "https://github.com/firebase/firebase-js-sdk.git"
18+
},
19+
"bugs": {
20+
"url": "https://github.com/firebase/firebase-js-sdk/issues"
21+
},
22+
"devDependencies": {
23+
"typescript": "4.2.2"
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"extends": "../../config/tsconfig.base.json",
3+
"compilerOptions": {
4+
"noEmit": true
5+
},
6+
"exclude": [
7+
"dist/**/*"
8+
]
9+
}

0 commit comments

Comments
 (0)