Skip to content

Commit 88cb6be

Browse files
committed
Merge and squash platform updates
1 parent 6caf8ef commit 88cb6be

File tree

15 files changed

+269
-3
lines changed

15 files changed

+269
-3
lines changed

.vscode/settings.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@
99
// Exclude installed dependencies from searches too
1010
"**/node_modules": true
1111
},
12-
"typescript.tsdk": "node_modules/typescript/lib"
12+
"typescript.tsdk": "node_modules/typescript/lib",
13+
"eslint.enable": true
1314
}

config/.eslintrc.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,18 @@
2424
"no-unused-expressions": "off",
2525
"@typescript-eslint/no-explicit-any": "off"
2626
}
27+
},
28+
{
29+
"files": "rollup.config.js",
30+
"rules": {
31+
"import/no-default-export": "off",
32+
"import/no-extraneous-dependencies": [
33+
"error",
34+
{
35+
"devDependencies": true
36+
}
37+
]
38+
}
2739
}
2840
],
2941
"rules": {

config/tsconfig.base.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
],
1818
"module": "ES2015",
1919
"moduleResolution": "node",
20+
"resolveJsonModule": true,
2021
"sourceMap": true,
2122
"target": "es5",
2223
"typeRoots": [

packages/analytics/index.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ export function registerAnalytics(instance: _FirebaseNamespace): void {
5757
new Component('analytics-internal', internalFactory, ComponentType.PRIVATE)
5858
);
5959

60+
instance.INTERNAL.registerVersionComponent(ANALYTICS_TYPE, version);
61+
6062
function internalFactory(
6163
container: ComponentContainer
6264
): FirebaseAnalyticsInternal {
@@ -88,3 +90,11 @@ declare module '@firebase/app-types' {
8890
analytics(): FirebaseAnalytics;
8991
}
9092
}
93+
94+
declare module '@firebase/component' {
95+
interface ComponentContainer {
96+
getProvider(name: typeof ANALYTICS_TYPE): Provider<FirebaseAnalytics>;
97+
}
98+
99+
interface Provider<T> {}
100+
}

packages/analytics/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
"rollup-plugin-commonjs": "10.1.0",
4646
"rollup-plugin-json": "4.0.0",
4747
"rollup-plugin-node-resolve": "5.2.0",
48+
"rollup-plugin-typescript2": "0.24.3",
4849
"rollup-plugin-uglify": "6.0.3",
4950
"sinon": "7.5.0",
5051
"sinon-chai": "3.3.0",

packages/app-types/private.d.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ export interface FirebaseServiceFactory {
5656
): FirebaseService;
5757
}
5858

59+
export interface PlatformLoggerService {
60+
getPlatformInfoString(): string;
61+
}
62+
5963
/**
6064
* All ServiceNamespaces extend from FirebaseServiceNamespace
6165
*/
@@ -107,6 +111,11 @@ export interface _FirebaseNamespace extends FirebaseNamespace {
107111
component: Component
108112
): FirebaseServiceNamespace<FirebaseService> | null;
109113

114+
registerVersionComponent(
115+
library: string,
116+
version: string
117+
): FirebaseServiceNamespace<FirebaseService> | null;
118+
110119
/**
111120
* Just used for testing to start from a fresh namespace.
112121
*/

packages/app/src/constants.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,17 @@
1616
*/
1717

1818
export const DEFAULT_ENTRY_NAME = '[DEFAULT]';
19+
20+
export const platformLogString: { [key: string]: string } = {
21+
'firebase': 'fire-js',
22+
'app': 'fire-core',
23+
'analytics': 'fire-analytics',
24+
'auth': 'fire-auth',
25+
'database': 'fire-rtdb',
26+
'functions': 'fire-fn',
27+
'messaging': 'fire-fcm',
28+
'performance': 'fire-perf',
29+
'remote-config': 'fire-rc',
30+
'storage': 'fire-gcs',
31+
'firestore': 'fire-fst'
32+
};

packages/app/src/firebaseApp.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ import {
3535
import { AppError, ERROR_FACTORY } from './errors';
3636
import { DEFAULT_ENTRY_NAME } from './constants';
3737
import { logger } from './logger';
38+
import { PlatformLoggerService } from './platformLoggerService';
39+
import { VersionService } from './version-service';
40+
41+
import { version } from '../package.json';
3842

3943
/**
4044
* Global context object for a collection of services using
@@ -60,6 +64,20 @@ export class FirebaseAppImpl implements FirebaseApp {
6064

6165
// add itself to container
6266
this._addComponent(new Component('app', () => this, ComponentType.PUBLIC));
67+
this._addComponent(
68+
new Component(
69+
'platform-logger',
70+
container => new PlatformLoggerService(container),
71+
ComponentType.PRIVATE
72+
)
73+
);
74+
this._addComponent(
75+
new Component(
76+
'app-version',
77+
() => new VersionService('app', version),
78+
ComponentType.VERSION
79+
)
80+
);
6381
// populate ComponentContainer with existing components
6482
for (const component of this.firebase_.INTERNAL.components.values()) {
6583
this._addComponent(component);

packages/app/src/firebaseNamespaceCore.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import { DEFAULT_ENTRY_NAME } from './constants';
3535
import { version } from '../../firebase/package.json';
3636
import { logger } from './logger';
3737
import { Component, ComponentType } from '@firebase/component';
38+
import { VersionService } from './version-service';
3839

3940
/**
4041
* Because auth can't share code with other components, we attach the utility functions
@@ -64,6 +65,7 @@ export function createFirebaseNamespaceCore(
6465
SDK_VERSION: version,
6566
INTERNAL: {
6667
registerComponent,
68+
registerVersionComponent,
6769
removeApp,
6870
components,
6971
useAsService
@@ -234,6 +236,22 @@ export function createFirebaseNamespaceCore(
234236
: null;
235237
}
236238

239+
240+
function registerVersionComponent(
241+
library: string,
242+
version: string
243+
): FirebaseServiceNamespace<FirebaseService> | null {
244+
return registerComponent(
245+
new Component(
246+
`${library}-version`,
247+
() => {
248+
return new VersionService(library, version);
249+
},
250+
ComponentType.VERSION
251+
)
252+
);
253+
}
254+
237255
// Map the requested service to a registered service name
238256
// (used to map auth to serverAuth service when needed).
239257
function useAsService(app: FirebaseApp, name: string): string | null {
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* @license
3+
* Copyright 2019 Google Inc.
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+
import { ComponentContainer, ComponentType } from '@firebase/component';
19+
import { VersionService } from './version-service';
20+
import { platformLogString } from './constants';
21+
22+
export class PlatformLoggerService {
23+
constructor(private container: ComponentContainer) {}
24+
// In initial implementation, this will be called by installations on
25+
// auth token refresh, and installations will send this string.
26+
getPlatformInfoString(): string {
27+
const providers = this.container.getProviders();
28+
// Loop through providers and get library/version pairs from any that are
29+
// version components.
30+
return providers
31+
.map(provider => {
32+
const service = provider.getImmediate();
33+
const component = provider.getComponent();
34+
if (service && component && component.type === ComponentType.VERSION) {
35+
const platformString =
36+
platformLogString[(service as VersionService).library] ||
37+
(service as VersionService).library;
38+
return `${platformString}/${(service as VersionService).version}`;
39+
} else {
40+
return null;
41+
}
42+
})
43+
.filter((logString: string | null) => logString)
44+
.join(' ');
45+
}
46+
}

packages/app/src/version-service.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* @license
3+
* Copyright 2019 Google Inc.
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 class VersionService {
19+
constructor(readonly library: string, readonly version: string) {}
20+
}
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/**
2+
* @license
3+
* Copyright 2017 Google Inc.
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+
import { FirebaseNamespace } from '@firebase/app-types';
19+
import { _FirebaseApp, _FirebaseNamespace } from '@firebase/app-types/private';
20+
import { createFirebaseNamespace } from '../src/firebaseNamespace';
21+
import { expect } from 'chai';
22+
import './setup';
23+
import { PlatformLoggerService } from '../src/platformLoggerService';
24+
import {
25+
Component,
26+
ComponentType,
27+
ComponentContainer
28+
} from '@firebase/component';
29+
import { VersionService } from '../src/version-service';
30+
31+
describe('Platform Logger Service Unit Tests', () => {
32+
it(`logs core version`, () => {
33+
const container = new ComponentContainer('testContainer');
34+
container.addComponent(
35+
new Component(
36+
'comp1',
37+
() => new VersionService('comp1', '1.2.3'),
38+
ComponentType.VERSION
39+
)
40+
);
41+
container.addComponent(
42+
new Component(
43+
'comp2',
44+
() => new VersionService('comp2', '3.02.01'),
45+
ComponentType.VERSION
46+
)
47+
);
48+
const platformLoggerService = new PlatformLoggerService(container);
49+
const platformInfoString = platformLoggerService.getPlatformInfoString();
50+
expect(platformInfoString).to.include('comp1/1.2.3');
51+
expect(platformInfoString).to.include('comp2/3.02.01');
52+
});
53+
});
54+
55+
describe('Platform Logger Service Integration Tests', () => {
56+
let firebase: FirebaseNamespace;
57+
58+
beforeEach(() => {
59+
firebase = createFirebaseNamespace();
60+
});
61+
62+
it(`logs core version`, () => {
63+
firebase.initializeApp({});
64+
(firebase as _FirebaseNamespace).INTERNAL.registerComponent(
65+
new Component(
66+
'test',
67+
async (container: ComponentContainer) => {
68+
const platformLoggerProvider = container.getProvider(
69+
'platform-logger'
70+
);
71+
const platformLogger = (await platformLoggerProvider.get()) as PlatformLoggerService;
72+
const platformInfoString = platformLogger.getPlatformInfoString();
73+
expect(platformInfoString).to.include('fire-core');
74+
return {};
75+
},
76+
ComponentType.PUBLIC
77+
)
78+
);
79+
(firebase as any).test();
80+
});
81+
82+
it(`logs other components' versions`, () => {
83+
firebase.initializeApp({});
84+
(firebase as _FirebaseNamespace).INTERNAL.registerVersionComponent(
85+
'analytics',
86+
'1.2.3'
87+
);
88+
(firebase as _FirebaseNamespace).INTERNAL.registerComponent(
89+
new Component(
90+
'test',
91+
async (container: ComponentContainer) => {
92+
const platformLoggerProvider = container.getProvider(
93+
'platform-logger'
94+
);
95+
const platformLogger = (await platformLoggerProvider.get()) as PlatformLoggerService;
96+
const platformInfoString = platformLogger.getPlatformInfoString();
97+
expect(platformInfoString).to.include('fire-analytics/1.2.3');
98+
return {};
99+
},
100+
ComponentType.PUBLIC
101+
)
102+
);
103+
(firebase as any).test();
104+
});
105+
});

packages/component/src/provider.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,6 @@ export class Provider<T extends Name> {
100100
}
101101
throw Error(`Service ${this.name} is not available`);
102102
}
103-
104103
return instance;
105104
} catch (e) {
106105
if (optional) {
@@ -111,6 +110,10 @@ export class Provider<T extends Name> {
111110
}
112111
}
113112

113+
getComponent(): Component<T> | null {
114+
return this.component;
115+
}
116+
114117
setComponent(component: Component<T>): void {
115118
if (component.name !== this.name) {
116119
throw Error(

packages/component/src/types.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ export const enum InstantiationMode {
3232
*/
3333
export const enum ComponentType {
3434
PUBLIC = 'PUBLIC',
35-
PRIVATE = 'PRIVATE'
35+
PRIVATE = 'PRIVATE',
36+
VERSION = 'VERSION'
3637
}
3738

3839
/**

packages/firebase/app/index.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,11 @@
1616
*/
1717

1818
import firebase from '@firebase/app';
19+
import { _FirebaseNamespace } from '@firebase/app-types/private';
20+
21+
(firebase as _FirebaseNamespace).INTERNAL.registerVersionComponent(
22+
'firebase',
23+
firebase.SDK_VERSION
24+
);
25+
1926
export default firebase;

0 commit comments

Comments
 (0)