diff --git a/packages/telemetry/index.node.ts b/packages/telemetry/index.node.ts index 726f98a1289..409c7d50a07 100644 --- a/packages/telemetry/index.node.ts +++ b/packages/telemetry/index.node.ts @@ -22,7 +22,10 @@ * just use index.ts */ -import { testFxn } from './src'; +import { registerTelemetry } from './src/index.node'; console.log('Hi Node.js Users!'); -testFxn(); +registerTelemetry(); + +export * from './src/api'; +export * from './src/public-types'; diff --git a/packages/telemetry/index.ts b/packages/telemetry/index.ts index 218b9afbb10..28fb9e7762a 100644 --- a/packages/telemetry/index.ts +++ b/packages/telemetry/index.ts @@ -15,6 +15,9 @@ * limitations under the License. */ -import { testFxn } from './src'; +import { registerTelemetry } from './src'; -testFxn(); +registerTelemetry(); + +export * from './src/api'; +export * from './src/public-types'; diff --git a/packages/telemetry/package.json b/packages/telemetry/package.json index 110c6ef7cc9..d9858a9e2f9 100644 --- a/packages/telemetry/package.json +++ b/packages/telemetry/package.json @@ -43,12 +43,15 @@ "@firebase/app-types": "0.x" }, "dependencies": { - "tslib": "^2.1.0" + "tslib": "^2.1.0", + "@firebase/component": "0.7.0" }, "license": "Apache-2.0", "devDependencies": { "@firebase/app": "0.14.1", + "@rollup/plugin-json": "6.1.0", "rollup": "2.79.2", + "rollup-plugin-replace": "2.2.0", "rollup-plugin-typescript2": "0.36.0", "typescript": "5.5.4" }, diff --git a/packages/telemetry/rollup.config.js b/packages/telemetry/rollup.config.js index 1450a975ce6..6ce188cb72d 100644 --- a/packages/telemetry/rollup.config.js +++ b/packages/telemetry/rollup.config.js @@ -15,6 +15,7 @@ * limitations under the License. */ +import json from '@rollup/plugin-json'; import typescriptPlugin from 'rollup-plugin-typescript2'; import typescript from 'typescript'; import pkg from './package.json'; @@ -24,7 +25,7 @@ const deps = Object.keys( Object.assign({}, pkg.peerDependencies, pkg.dependencies) ); -const buildPlugins = [typescriptPlugin({ typescript })]; +const buildPlugins = [typescriptPlugin({ typescript }), json()]; const browserBuilds = [ { diff --git a/packages/telemetry/src/api.ts b/packages/telemetry/src/api.ts new file mode 100644 index 00000000000..950d47ada33 --- /dev/null +++ b/packages/telemetry/src/api.ts @@ -0,0 +1,46 @@ +/** + * @license + * Copyright 2025 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. + */ + +import { _getProvider, FirebaseApp, getApp } from '@firebase/app'; +import { TELEMETRY_TYPE } from './constants'; +import { Telemetry } from './public-types'; +import { Provider } from '@firebase/component'; + +/** + * Returns the default {@link Telemetry} instance that is associated with the provided + * {@link @firebase/app#FirebaseApp}. If no instance exists, initializes a new instance with the + * default settings. + * + * @example + * ```javascript + * const telemetry = getTelemetry(app); + * ``` + * + * @param app - The {@link @firebase/app#FirebaseApp} to use. + * @returns The default {@link Telemetry} instance for the given {@link @firebase/app#FirebaseApp}. + * + * @public + */ +export function getTelemetry(app: FirebaseApp = getApp()): Telemetry { + // Dependencies + const telemetryProvider: Provider<'telemetry'> = _getProvider( + app, + TELEMETRY_TYPE + ); + + return telemetryProvider.getImmediate(); +} diff --git a/packages/telemetry/src/constants.ts b/packages/telemetry/src/constants.ts new file mode 100644 index 00000000000..5928220b811 --- /dev/null +++ b/packages/telemetry/src/constants.ts @@ -0,0 +1,19 @@ +/** + * @license + * Copyright 2025 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. + */ + +/** Type constant for Firebase Telemetry. */ +export const TELEMETRY_TYPE = 'telemetry'; diff --git a/packages/telemetry/src/index.node.ts b/packages/telemetry/src/index.node.ts new file mode 100644 index 00000000000..8c14e0f3359 --- /dev/null +++ b/packages/telemetry/src/index.node.ts @@ -0,0 +1,53 @@ +/** + * @license + * Copyright 2025 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. + */ + +import { _registerComponent, registerVersion } from '@firebase/app'; +import { TestType } from './types/index'; +import { Component, ComponentType } from '@firebase/component'; +import { TELEMETRY_TYPE } from './constants'; +import { name, version } from '../package.json'; +import { TelemetryService } from './service'; + +export function testFxn(): number { + const _thing: TestType = {}; + console.log('hi'); + return 42; +} + +declare module '@firebase/component' { + interface NameServiceMapping { + [TELEMETRY_TYPE]: TelemetryService; + } +} + +export function registerTelemetry(): void { + _registerComponent( + new Component( + TELEMETRY_TYPE, + container => { + // getImmediate for FirebaseApp will always succeed + const app = container.getProvider('app').getImmediate(); + return new TelemetryService(app); + }, + ComponentType.PUBLIC + ) + ); + + registerVersion(name, version, 'node'); + // BUILD_TARGET will be replaced by values like esm, cjs, etc during the compilation + registerVersion(name, version, '__BUILD_TARGET__'); +} diff --git a/packages/telemetry/src/index.ts b/packages/telemetry/src/index.ts index 8dca8834e11..352b0853aef 100644 --- a/packages/telemetry/src/index.ts +++ b/packages/telemetry/src/index.ts @@ -15,10 +15,39 @@ * limitations under the License. */ +import { _registerComponent, registerVersion } from '@firebase/app'; import { TestType } from './types/index'; +import { Component, ComponentType } from '@firebase/component'; +import { TELEMETRY_TYPE } from './constants'; +import { name, version } from '../package.json'; +import { TelemetryService } from './service'; export function testFxn(): number { const _thing: TestType = {}; console.log('hi'); return 42; } + +declare module '@firebase/component' { + interface NameServiceMapping { + [TELEMETRY_TYPE]: TelemetryService; + } +} + +export function registerTelemetry(): void { + _registerComponent( + new Component( + TELEMETRY_TYPE, + (container, {}) => { + // getImmediate for FirebaseApp will always succeed + const app = container.getProvider('app').getImmediate(); + return new TelemetryService(app); + }, + ComponentType.PUBLIC + ) + ); + + registerVersion(name, version); + // BUILD_TARGET will be replaced by values like esm, cjs, etc during the compilation + registerVersion(name, version, '__BUILD_TARGET__'); +} diff --git a/packages/telemetry/src/public-types.ts b/packages/telemetry/src/public-types.ts new file mode 100644 index 00000000000..5bcc1830929 --- /dev/null +++ b/packages/telemetry/src/public-types.ts @@ -0,0 +1,32 @@ +/** + * @license + * Copyright 2025 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. + */ + +import { FirebaseApp } from '@firebase/app'; + +/** + * An instance of the Firebase Telemetry SDK. + * + * Do not create this instance directly. Instead, use {@link getTelemetry | getTelemetry()}. + * + * @public + */ +export interface Telemetry { + /** + * The {@link @firebase/app#FirebaseApp} this {@link Telemetry} instance is associated with. + */ + app: FirebaseApp; +} diff --git a/packages/telemetry/src/service.ts b/packages/telemetry/src/service.ts new file mode 100644 index 00000000000..1c314b2af9d --- /dev/null +++ b/packages/telemetry/src/service.ts @@ -0,0 +1,27 @@ +/** + * @license + * Copyright 2025 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. + */ + +import { _FirebaseService, FirebaseApp } from '@firebase/app'; +import { Telemetry } from './public-types'; + +export class TelemetryService implements Telemetry, _FirebaseService { + constructor(public app: FirebaseApp) {} + + _delete(): Promise { + return Promise.resolve(); + } +}