Skip to content

Commit

Permalink
Merge branch 'master' into fei-v9-main
Browse files Browse the repository at this point in the history
  • Loading branch information
Feiyang1 committed Aug 21, 2021
2 parents 568459d + 7818176 commit 5c0662e
Show file tree
Hide file tree
Showing 13 changed files with 151 additions and 37 deletions.
2 changes: 1 addition & 1 deletion packages-exp/functions-exp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
"typings": "dist/functions-exp-public.d.ts",
"dependencies": {
"@firebase/component": "0.5.6",
"@firebase/messaging-types": "0.5.0",
"@firebase/messaging-interop-types": "0.0.1",
"@firebase/auth-interop-types": "0.1.6",
"@firebase/app-check-interop-types": "0.1.0",
"@firebase/util": "1.3.0",
Expand Down
24 changes: 14 additions & 10 deletions packages-exp/functions-exp/src/callable.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ import {
ComponentType
} from '@firebase/component';
import {
FirebaseMessaging,
FirebaseMessagingName
} from '@firebase/messaging-types';
MessagingInternal,
MessagingInternalComponentName
} from '@firebase/messaging-interop-types';
import {
FirebaseAuthInternal,
FirebaseAuthInternalName
Expand Down Expand Up @@ -108,9 +108,9 @@ describe('Firebase Functions > Call', () => {

it('token', async () => {
// mock auth-internal service
const authMock: FirebaseAuthInternal = ({
const authMock: FirebaseAuthInternal = {
getToken: async () => ({ accessToken: 'token' })
} as unknown) as FirebaseAuthInternal;
} as unknown as FirebaseAuthInternal;
const authProvider = new Provider<FirebaseAuthInternalName>(
'auth-internal',
new ComponentContainer('test')
Expand Down Expand Up @@ -139,15 +139,19 @@ describe('Firebase Functions > Call', () => {
return;
}
// mock firebase messaging
const messagingMock: FirebaseMessaging = ({
const messagingMock: MessagingInternal = {
getToken: async () => 'iid'
} as unknown) as FirebaseMessaging;
const messagingProvider = new Provider<FirebaseMessagingName>(
'messaging',
} as unknown as MessagingInternal;
const messagingProvider = new Provider<MessagingInternalComponentName>(
'messaging-internal',
new ComponentContainer('test')
);
messagingProvider.setComponent(
new Component('messaging', () => messagingMock, ComponentType.PRIVATE)
new Component(
'messaging-internal',
() => messagingMock,
ComponentType.PRIVATE
)
);

const functions = createTestService(
Expand Down
10 changes: 8 additions & 2 deletions packages-exp/functions-exp/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,25 @@ import {
InstanceFactory
} from '@firebase/component';
import { FUNCTIONS_TYPE } from './constants';
import { FirebaseAuthInternalName } from '@firebase/auth-interop-types';
import { AppCheckInternalComponentName } from '@firebase/app-check-interop-types';
import { MessagingInternalComponentName } from '@firebase/messaging-interop-types';

const AUTH_INTERNAL_NAME: FirebaseAuthInternalName = 'auth-internal';
const APP_CHECK_INTERNAL_NAME: AppCheckInternalComponentName =
'app-check-internal';
const MESSAGING_INTERNAL_NAME: MessagingInternalComponentName =
'messaging-internal';

export function registerFunctions(fetchImpl: typeof fetch): void {
const factory: InstanceFactory<'functions-exp'> = (
container: ComponentContainer,
{ instanceIdentifier: regionOrCustomDomain }
) => {
// Dependencies
const app = container.getProvider('app-exp').getImmediate();
const authProvider = container.getProvider('auth-internal');
const messagingProvider = container.getProvider('messaging');
const authProvider = container.getProvider(AUTH_INTERNAL_NAME);
const messagingProvider = container.getProvider(MESSAGING_INTERNAL_NAME);
const appCheckProvider = container.getProvider(APP_CHECK_INTERNAL_NAME);

// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand Down
12 changes: 6 additions & 6 deletions packages-exp/functions-exp/src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@ import {
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import {
FirebaseMessaging,
FirebaseMessagingName
} from '@firebase/messaging-types';

import { Provider } from '@firebase/component';
import {
AppCheckInternalComponentName,
FirebaseAppCheckInternal
} from '@firebase/app-check-interop-types';
import {
MessagingInternal,
MessagingInternalComponentName
} from '@firebase/messaging-interop-types';

/**
* The metadata that should be supplied with function calls.
Expand All @@ -45,11 +45,11 @@ export interface Context {
*/
export class ContextProvider {
private auth: FirebaseAuthInternal | null = null;
private messaging: FirebaseMessaging | null = null;
private messaging: MessagingInternal | null = null;
private appCheck: FirebaseAppCheckInternal | null = null;
constructor(
authProvider: Provider<FirebaseAuthInternalName>,
messagingProvider: Provider<FirebaseMessagingName>,
messagingProvider: Provider<MessagingInternalComponentName>,
appCheckProvider: Provider<AppCheckInternalComponentName>
) {
this.auth = authProvider.getImmediate({ optional: true });
Expand Down
4 changes: 2 additions & 2 deletions packages-exp/functions-exp/src/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import { ContextProvider } from './context';
import { encode, decode } from './serializer';
import { Provider } from '@firebase/component';
import { FirebaseAuthInternalName } from '@firebase/auth-interop-types';
import { FirebaseMessagingName } from '@firebase/messaging-types';
import { MessagingInternalComponentName } from '@firebase/messaging-interop-types';
import { AppCheckInternalComponentName } from '@firebase/app-check-interop-types';

export const DEFAULT_REGION = 'us-central1';
Expand Down Expand Up @@ -86,7 +86,7 @@ export class FunctionsService implements _FirebaseService {
constructor(
readonly app: FirebaseApp,
authProvider: Provider<FirebaseAuthInternalName>,
messagingProvider: Provider<FirebaseMessagingName>,
messagingProvider: Provider<MessagingInternalComponentName>,
appCheckProvider: Provider<AppCheckInternalComponentName>,
regionOrCustomDomain: string = DEFAULT_REGION,
readonly fetchImpl: typeof fetch
Expand Down
6 changes: 3 additions & 3 deletions packages-exp/functions-exp/test/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@
import { FirebaseOptions, FirebaseApp } from '@firebase/app-exp';
import { Provider, ComponentContainer } from '@firebase/component';
import { FirebaseAuthInternalName } from '@firebase/auth-interop-types';
import { FirebaseMessagingName } from '@firebase/messaging-types';
import { AppCheckInternalComponentName } from '@firebase/app-check-interop-types';
import { FunctionsService } from '../src/service';
import { connectFunctionsEmulator } from '../src/api';
import nodeFetch from 'node-fetch';
import { MessagingInternalComponentName } from '../../../packages/messaging-interop-types';

export function makeFakeApp(options: FirebaseOptions = {}): FirebaseApp {
options = {
Expand All @@ -49,8 +49,8 @@ export function createTestService(
'auth-internal',
new ComponentContainer('test')
),
messagingProvider = new Provider<FirebaseMessagingName>(
'messaging',
messagingProvider = new Provider<MessagingInternalComponentName>(
'messaging-internal',
new ComponentContainer('test')
),
appCheckProvider = new Provider<AppCheckInternalComponentName>(
Expand Down
1 change: 1 addition & 0 deletions packages-exp/messaging-exp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"dependencies": {
"@firebase/component": "0.5.6",
"@firebase/installations-exp": "0.0.900",
"@firebase/messaging-interop-types": "0.0.1",
"@firebase/util": "1.3.0",
"idb": "3.0.2",
"tslib": "^2.1.0"
Expand Down
57 changes: 44 additions & 13 deletions packages-exp/messaging-exp/src/helpers/register.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,27 +29,18 @@ import {
onSubChange
} from '../listeners/sw-listeners';

import { GetTokenOptions } from '../interfaces/public-types';
import { MessagingInternal } from '@firebase/messaging-interop-types';
import { MessagingService } from '../messaging-service';
import { ServiceWorkerGlobalScope } from '../util/sw-types';
import { _registerComponent } from '@firebase/app-exp';
import { getToken } from '../api/getToken';
import { messageEventListener } from '../listeners/window-listener';

const WindowMessagingFactory: InstanceFactory<'messaging-exp'> = (
container: ComponentContainer
) => {
// Conscious decision to make this async check non-blocking during the messaging instance
// initialization phase for performance consideration. An error would be thrown latter for
// developer's information. Developers can then choose to import and call `isSupported` for
// special handling.
isWindowSupported()
.then(isSupported => {
if (!isSupported) {
throw ERROR_FACTORY.create(ErrorCode.UNSUPPORTED_BROWSER);
}
})
.catch(_ => {
throw ERROR_FACTORY.create(ErrorCode.INDEXED_DB_UNSUPPORTED);
});
maybeThrowWindowError();

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

const WindowMessagingInternalFactory: InstanceFactory<'messaging-internal'> = (
container: ComponentContainer
) => {
maybeThrowWindowError();

const messaging = container
.getProvider('messaging-exp')
.getImmediate() as MessagingService;

const messagingInternal: MessagingInternal = {
getToken: (options?: GetTokenOptions) => getToken(messaging, options)
};

return messagingInternal;
};

function maybeThrowWindowError(): void {
// Conscious decision to make this async check non-blocking during the messaging instance
// initialization phase for performance consideration. An error would be thrown latter for
// developer's information. Developers can then choose to import and call `isSupported` for
// special handling.
isWindowSupported()
.then(isSupported => {
if (!isSupported) {
throw ERROR_FACTORY.create(ErrorCode.UNSUPPORTED_BROWSER);
}
})
.catch(_ => {
throw ERROR_FACTORY.create(ErrorCode.INDEXED_DB_UNSUPPORTED);
});
}

declare const self: ServiceWorkerGlobalScope;
const SwMessagingFactory: InstanceFactory<'messaging-exp'> = (
container: ComponentContainer
Expand Down Expand Up @@ -105,6 +128,14 @@ export function registerMessagingInWindow(): void {
_registerComponent(
new Component('messaging-exp', WindowMessagingFactory, ComponentType.PUBLIC)
);

_registerComponent(
new Component(
'messaging-internal',
WindowMessagingInternalFactory,
ComponentType.PRIVATE
)
);
}

/**
Expand Down
1 change: 1 addition & 0 deletions packages/messaging-interop-types/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# @firebase/messaging-interop-types
4 changes: 4 additions & 0 deletions packages/messaging-interop-types/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# @firebase/messaging-interop-types

**This package is not intended for direct usage, and should only be used via the officially
supported [firebase](https://www.npmjs.com/package/firebase) package.**
33 changes: 33 additions & 0 deletions packages/messaging-interop-types/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* @license
* Copyright 2021 Google LLC
*
* 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.
*/

export interface MessagingInternal {
getToken(options?: GetTokenOptions): Promise<string>;
}

interface GetTokenOptions {
vapidKey?: string;
serviceWorkerRegistration?: ServiceWorkerRegistration;
}

export type MessagingInternalComponentName = 'messaging-internal';

declare module '@firebase/component' {
interface NameServiceMapping {
'messaging-internal': MessagingInternal;
}
}
25 changes: 25 additions & 0 deletions packages/messaging-interop-types/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "@firebase/messaging-interop-types",
"version": "0.0.1",
"description": "@firebase/messaging-interop-types Types",
"author": "Firebase <[email protected]> (https://firebase.google.com/)",
"license": "Apache-2.0",
"scripts": {
"test": "tsc",
"test:ci": "node ../../scripts/run_tests_in_ci.js"
},
"files": [
"index.d.ts"
],
"repository": {
"directory": "packages/messaging-interop-types",
"type": "git",
"url": "https://github.com/firebase/firebase-js-sdk.git"
},
"bugs": {
"url": "https://github.com/firebase/firebase-js-sdk/issues"
},
"devDependencies": {
"typescript": "4.2.2"
}
}
9 changes: 9 additions & 0 deletions packages/messaging-interop-types/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "../../config/tsconfig.base.json",
"compilerOptions": {
"noEmit": true
},
"exclude": [
"dist/**/*"
]
}

0 comments on commit 5c0662e

Please sign in to comment.