Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Installations heartbeat implementation #5966

Merged
merged 5 commits into from
Mar 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/proud-otters-tap.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@firebase/installations': patch
---

Update platform logging code to send to new endpoint.
7 changes: 2 additions & 5 deletions packages/installations/src/api/get-id.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,14 @@ import {
import { getFakeInstallations } from '../testing/fake-generators';
import '../testing/setup';
import { getId } from './get-id';
import {
FirebaseInstallationsImpl,
AppConfig
} from '../interfaces/installation-impl';
import { FirebaseInstallationsImpl } from '../interfaces/installation-impl';

const FID = 'disciples-of-the-watch';

describe('getId', () => {
let installations: FirebaseInstallationsImpl;
let getInstallationEntrySpy: SinonStub<
[AppConfig],
[FirebaseInstallationsImpl],
Promise<getInstallationEntryModule.InstallationEntryWithRegistrationPromise>
>;

Expand Down
2 changes: 1 addition & 1 deletion packages/installations/src/api/get-id.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import { Installations } from '../interfaces/public-types';
export async function getId(installations: Installations): Promise<string> {
const installationsImpl = installations as FirebaseInstallationsImpl;
const { installationEntry, registrationPromise } = await getInstallationEntry(
installationsImpl.appConfig
installationsImpl
);

if (registrationPromise) {
Expand Down
2 changes: 1 addition & 1 deletion packages/installations/src/api/get-token.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ const setupInstallationEntryMap: Map<
describe('getToken', () => {
let installations: FirebaseInstallationsImpl;
let createInstallationRequestSpy: SinonStub<
[AppConfig, InProgressInstallationEntry],
[FirebaseInstallationsImpl, InProgressInstallationEntry],
Promise<RegisteredInstallationEntry>
>;
let generateAuthTokenRequestSpy: SinonStub<
Expand Down
11 changes: 4 additions & 7 deletions packages/installations/src/api/get-token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@

import { getInstallationEntry } from '../helpers/get-installation-entry';
import { refreshAuthToken } from '../helpers/refresh-auth-token';
import {
FirebaseInstallationsImpl,
AppConfig
} from '../interfaces/installation-impl';
import { FirebaseInstallationsImpl } from '../interfaces/installation-impl';
import { Installations } from '../interfaces/public-types';

/**
Expand All @@ -36,7 +33,7 @@ export async function getToken(
forceRefresh = false
): Promise<string> {
const installationsImpl = installations as FirebaseInstallationsImpl;
await completeInstallationRegistration(installationsImpl.appConfig);
await completeInstallationRegistration(installationsImpl);

// At this point we either have a Registered Installation in the DB, or we've
// already thrown an error.
Expand All @@ -45,9 +42,9 @@ export async function getToken(
}

async function completeInstallationRegistration(
appConfig: AppConfig
installations: FirebaseInstallationsImpl
): Promise<void> {
const { registrationPromise } = await getInstallationEntry(appConfig);
const { registrationPromise } = await getInstallationEntry(installations);

if (registrationPromise) {
// A createInstallation request is in progress. Wait until it finishes.
Expand Down
2 changes: 1 addition & 1 deletion packages/installations/src/api/on-id-change.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import { Installations } from '../interfaces/public-types';
*/
export type IdChangeCallbackFn = (installationId: string) => void;
/**
* Unsubscribe a callback function previously added via {@link #IdChangeCallbackFn}.
* Unsubscribe a callback function previously added via {@link IdChangeCallbackFn}.
*
* @public
*/
Expand Down
4 changes: 2 additions & 2 deletions packages/installations/src/functions/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ const publicFactory: InstanceFactory<'installations'> = (
const app = container.getProvider('app').getImmediate();
// Throws if app isn't configured properly.
const appConfig = extractAppConfig(app);
const platformLoggerProvider = _getProvider(app, 'platform-logger');
const heartbeatServiceProvider = _getProvider(app, 'heartbeat');

const installationsImpl: FirebaseInstallationsImpl = {
app,
appConfig,
platformLoggerProvider,
heartbeatServiceProvider,
_delete: () => Promise.resolve()
};
return installationsImpl;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ import { FirebaseError } from '@firebase/util';
import { expect } from 'chai';
import { SinonStub, stub } from 'sinon';
import { CreateInstallationResponse } from '../interfaces/api-response';
import { AppConfig } from '../interfaces/installation-impl';
import { FirebaseInstallationsImpl } from '../interfaces/installation-impl';
import {
InProgressInstallationEntry,
RequestStatus
} from '../interfaces/installation-entry';
import { compareHeaders } from '../testing/compare-headers';
import { getFakeAppConfig } from '../testing/fake-generators';
import { getFakeInstallations } from '../testing/fake-generators';
import '../testing/setup';
import {
INSTALLATIONS_API_URL,
Expand All @@ -38,13 +38,13 @@ import { createInstallationRequest } from './create-installation-request';
const FID = 'defenders-of-the-faith';

describe('createInstallationRequest', () => {
let appConfig: AppConfig;
let fakeInstallations: FirebaseInstallationsImpl;
let fetchSpy: SinonStub<[RequestInfo, RequestInit?], Promise<Response>>;
let inProgressInstallationEntry: InProgressInstallationEntry;
let response: CreateInstallationResponse;

beforeEach(() => {
appConfig = getFakeAppConfig();
fakeInstallations = getFakeInstallations();

inProgressInstallationEntry = {
fid: FID,
Expand All @@ -71,7 +71,7 @@ describe('createInstallationRequest', () => {

it('registers a pending InstallationEntry', async () => {
const registeredInstallationEntry = await createInstallationRequest(
appConfig,
fakeInstallations,
inProgressInstallationEntry
);
expect(registeredInstallationEntry.registrationStatus).to.equal(
Expand All @@ -83,12 +83,13 @@ describe('createInstallationRequest', () => {
const expectedHeaders = new Headers({
'Content-Type': 'application/json',
Accept: 'application/json',
'x-goog-api-key': 'apiKey'
'x-goog-api-key': 'apiKey',
'x-firebase-client': 'a/1.2.3 b/2.3.4'
});
const expectedBody = {
fid: FID,
authVersion: INTERNAL_AUTH_VERSION,
appId: appConfig.appId,
appId: fakeInstallations.appConfig.appId,
sdkVersion: PACKAGE_VERSION
};
const expectedRequest: RequestInit = {
Expand All @@ -98,7 +99,10 @@ describe('createInstallationRequest', () => {
};
const expectedEndpoint = `${INSTALLATIONS_API_URL}/projects/projectId/installations`;

await createInstallationRequest(appConfig, inProgressInstallationEntry);
await createInstallationRequest(
fakeInstallations,
inProgressInstallationEntry
);
expect(fetchSpy).to.be.calledOnceWith(expectedEndpoint, expectedRequest);
const actualHeaders = fetchSpy.lastCall.lastArg.headers;
compareHeaders(expectedHeaders, actualHeaders);
Expand All @@ -117,7 +121,7 @@ describe('createInstallationRequest', () => {
fetchSpy.resolves(new Response(JSON.stringify(response)));

const registeredInstallationEntry = await createInstallationRequest(
appConfig,
fakeInstallations,
inProgressInstallationEntry
);
expect(registeredInstallationEntry.fid).to.equal(FID);
Expand All @@ -138,7 +142,10 @@ describe('createInstallationRequest', () => {
);

await expect(
createInstallationRequest(appConfig, inProgressInstallationEntry)
createInstallationRequest(
fakeInstallations,
inProgressInstallationEntry
)
).to.be.rejectedWith(FirebaseError);
});

Expand All @@ -157,7 +164,10 @@ describe('createInstallationRequest', () => {
fetchSpy.onCall(1).resolves(new Response(JSON.stringify(response)));

await expect(
createInstallationRequest(appConfig, inProgressInstallationEntry)
createInstallationRequest(
fakeInstallations,
inProgressInstallationEntry
)
).to.be.fulfilled;
expect(fetchSpy).to.be.calledTwice;
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,27 @@ import {
getInstallationsEndpoint,
retryIfServerError
} from './common';
import { AppConfig } from '../interfaces/installation-impl';
import { FirebaseInstallationsImpl } from '../interfaces/installation-impl';

export async function createInstallationRequest(
appConfig: AppConfig,
{ appConfig, heartbeatServiceProvider }: FirebaseInstallationsImpl,
{ fid }: InProgressInstallationEntry
): Promise<RegisteredInstallationEntry> {
const endpoint = getInstallationsEndpoint(appConfig);

const headers = getHeaders(appConfig);

// If heartbeat service exists, add the heartbeat string to the header.
const heartbeatService = heartbeatServiceProvider.getImmediate({
optional: true
});
if (heartbeatService) {
const heartbeatsHeader = await heartbeatService.getHeartbeatsHeader();
if (heartbeatsHeader) {
headers.append('x-firebase-client', heartbeatsHeader);
}
}

const body = {
fid,
authVersion: INTERNAL_AUTH_VERSION,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ describe('generateAuthTokenRequest', () => {
});
const expectedBody = {
installation: {
sdkVersion: PACKAGE_VERSION
sdkVersion: PACKAGE_VERSION,
appId: installations.appConfig.appId
}
};
const expectedRequest: RequestInit = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,24 +34,28 @@ import {
} from '../interfaces/installation-impl';

export async function generateAuthTokenRequest(
{ appConfig, platformLoggerProvider }: FirebaseInstallationsImpl,
{ appConfig, heartbeatServiceProvider }: FirebaseInstallationsImpl,
installationEntry: RegisteredInstallationEntry
): Promise<CompletedAuthToken> {
const endpoint = getGenerateAuthTokenEndpoint(appConfig, installationEntry);

const headers = getHeadersWithAuth(appConfig, installationEntry);

// If platform logger exists, add the platform info string to the header.
const platformLogger = platformLoggerProvider.getImmediate({
// If heartbeat service exists, add the heartbeat string to the header.
const heartbeatService = heartbeatServiceProvider.getImmediate({
optional: true
});
if (platformLogger) {
headers.append('x-firebase-client', platformLogger.getPlatformInfoString());
if (heartbeatService) {
const heartbeatsHeader = await heartbeatService.getHeartbeatsHeader();
if (heartbeatsHeader) {
headers.append('x-firebase-client', heartbeatsHeader);
}
}

const body = {
installation: {
sdkVersion: PACKAGE_VERSION
sdkVersion: PACKAGE_VERSION,
appId: appConfig.appId
}
};

Expand Down
Loading