Skip to content

Commit

Permalink
Change userAgent field to agent
Browse files Browse the repository at this point in the history
  • Loading branch information
hsubox76 committed Mar 10, 2022
1 parent 46a3a0a commit 303d844
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 33 deletions.
47 changes: 23 additions & 24 deletions packages/app/src/heartbeatService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import { FirebaseApp } from './public-types';
import * as firebaseUtil from '@firebase/util';
import { SinonStub, stub, useFakeTimers } from 'sinon';
import * as indexedDb from './indexeddb';
import { base64Encode, isIndexedDBAvailable } from '@firebase/util';

declare module '@firebase/component' {
interface NameServiceMapping {
Expand Down Expand Up @@ -101,7 +100,7 @@ describe('HeartbeatServiceImpl', () => {
await heartbeatService.triggerHeartbeat();
expect(heartbeatService._heartbeatsCache?.heartbeats.length).to.equal(1);
const heartbeat1 = heartbeatService._heartbeatsCache?.heartbeats[0];
expect(heartbeat1?.userAgent).to.equal(USER_AGENT_STRING_1);
expect(heartbeat1?.agent).to.equal(USER_AGENT_STRING_1);
expect(heartbeat1?.date).to.equal('1970-01-01');
expect(writeStub).to.be.calledWith({ heartbeats: [heartbeat1] });
});
Expand Down Expand Up @@ -156,11 +155,11 @@ describe('HeartbeatServiceImpl', () => {
const mockIndexedDBHeartbeats = [
// Chosen so one will exceed 30 day limit and one will not.
{
userAgent: 'old-user-agent',
agent: 'old-user-agent',
date: '1969-12-01'
},
{
userAgent: 'old-user-agent',
agent: 'old-user-agent',
date: '1969-12-31'
}
];
Expand Down Expand Up @@ -199,7 +198,7 @@ describe('HeartbeatServiceImpl', () => {
*/
it(`new heartbeat service reads from indexedDB cache`, async () => {
const promiseResult = await heartbeatService._heartbeatsCachePromise;
if (isIndexedDBAvailable()) {
if (firebaseUtil.isIndexedDBAvailable()) {
expect(promiseResult).to.deep.equal({
heartbeats: mockIndexedDBHeartbeats
});
Expand All @@ -221,25 +220,25 @@ describe('HeartbeatServiceImpl', () => {
userAgentString = USER_AGENT_STRING_2;
clock.tick(3 * 24 * 60 * 60 * 1000);
await heartbeatService.triggerHeartbeat();
if (isIndexedDBAvailable()) {
if (firebaseUtil.isIndexedDBAvailable()) {
expect(writeStub).to.be.calledWith({
heartbeats: [
// The first entry exceeds the 30 day retention limit.
mockIndexedDBHeartbeats[1],
{ userAgent: USER_AGENT_STRING_2, date: '1970-01-04' }
{ agent: USER_AGENT_STRING_2, date: '1970-01-04' }
]
});
} else {
expect(writeStub).to.be.calledWith({
heartbeats: [{ userAgent: USER_AGENT_STRING_2, date: '1970-01-04' }]
heartbeats: [{ agent: USER_AGENT_STRING_2, date: '1970-01-04' }]
});
}
});
it('getHeartbeatHeaders() gets stored heartbeats and clears heartbeats', async () => {
const heartbeatHeaders = firebaseUtil.base64Decode(
await heartbeatService.getHeartbeatsHeader()
);
if (isIndexedDBAvailable()) {
if (firebaseUtil.isIndexedDBAvailable()) {
expect(heartbeatHeaders).to.include('old-user-agent');
expect(heartbeatHeaders).to.include('1969-12-31');
}
Expand All @@ -263,11 +262,11 @@ describe('HeartbeatServiceImpl', () => {
const mockIndexedDBHeartbeats = [
// Chosen so one will exceed 30 day limit and one will not.
{
userAgent: 'old-user-agent',
agent: 'old-user-agent',
date: '1969-12-01'
},
{
userAgent: 'old-user-agent',
agent: 'old-user-agent',
date: '1969-12-31'
}
];
Expand Down Expand Up @@ -303,7 +302,7 @@ describe('HeartbeatServiceImpl', () => {
});
it(`new heartbeat service reads from indexedDB cache`, async () => {
const promiseResult = await heartbeatService._heartbeatsCachePromise;
if (isIndexedDBAvailable()) {
if (firebaseUtil.isIndexedDBAvailable()) {
expect(promiseResult).to.deep.equal({
lastSentHeartbeatDate: '1970-01-01',
heartbeats: mockIndexedDBHeartbeats
Expand All @@ -326,7 +325,7 @@ describe('HeartbeatServiceImpl', () => {
it(`triggerHeartbeat() will skip storing new data`, async () => {
await heartbeatService.triggerHeartbeat();
expect(writeStub).to.not.be.called;
if (isIndexedDBAvailable()) {
if (firebaseUtil.isIndexedDBAvailable()) {
expect(heartbeatService._heartbeatsCache?.heartbeats).to.deep.equal(
mockIndexedDBHeartbeats
);
Expand All @@ -337,11 +336,11 @@ describe('HeartbeatServiceImpl', () => {
describe('countBytes()', () => {
it('counts how many bytes there will be in a stringified, encoded header', () => {
const heartbeats = [
{ userAgent: generateUserAgentString(1), dates: generateDates(1) },
{ userAgent: generateUserAgentString(3), dates: generateDates(2) }
{ agent: generateUserAgentString(1), dates: generateDates(1) },
{ agent: generateUserAgentString(3), dates: generateDates(2) }
];
let size: number = 0;
const headerString = base64Encode(
const headerString = firebaseUtil.base64urlEncodeWithoutPadding(
JSON.stringify({ version: 2, heartbeats })
);
// Use independent methods to validate our byte count method matches.
Expand All @@ -362,7 +361,7 @@ describe('HeartbeatServiceImpl', () => {
describe('_extractHeartbeatsForHeader()', () => {
it('returns empty heartbeatsToKeep if it cannot get under maxSize', () => {
const heartbeats = [
{ userAgent: generateUserAgentString(1), date: '2022-01-01' }
{ agent: generateUserAgentString(1), date: '2022-01-01' }
];
const { unsentEntries, heartbeatsToSend } = extractHeartbeatsForHeader(
heartbeats,
Expand All @@ -373,11 +372,11 @@ describe('HeartbeatServiceImpl', () => {
});
it('splits heartbeats array', () => {
const heartbeats = [
{ userAgent: generateUserAgentString(20), date: '2022-01-01' },
{ userAgent: generateUserAgentString(4), date: '2022-01-02' }
{ agent: generateUserAgentString(20), date: '2022-01-01' },
{ agent: generateUserAgentString(4), date: '2022-01-02' }
];
const sizeWithHeartbeat0Only = countBytes([
{ userAgent: heartbeats[0].userAgent, dates: [heartbeats[0].date] }
{ agent: heartbeats[0].agent, dates: [heartbeats[0].date] }
]);
const { unsentEntries, heartbeatsToSend } = extractHeartbeatsForHeader(
heartbeats,
Expand All @@ -389,12 +388,12 @@ describe('HeartbeatServiceImpl', () => {
it('splits the first heartbeat if needed', () => {
const uaString = generateUserAgentString(20);
const heartbeats = [
{ userAgent: uaString, date: '2022-01-01' },
{ userAgent: uaString, date: '2022-01-02' },
{ userAgent: uaString, date: '2022-01-03' }
{ agent: uaString, date: '2022-01-01' },
{ agent: uaString, date: '2022-01-02' },
{ agent: uaString, date: '2022-01-03' }
];
const sizeWithHeartbeat0Only = countBytes([
{ userAgent: heartbeats[0].userAgent, dates: [heartbeats[0].date] }
{ agent: heartbeats[0].agent, dates: [heartbeats[0].date] }
]);
const { unsentEntries, heartbeatsToSend } = extractHeartbeatsForHeader(
heartbeats,
Expand Down
14 changes: 7 additions & 7 deletions packages/app/src/heartbeatService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

import { ComponentContainer } from '@firebase/component';
import {
base64Encode,
base64urlEncodeWithoutPadding,
isIndexedDBAvailable,
validateIndexedDBOpenable
} from '@firebase/util';
Expand Down Expand Up @@ -86,7 +86,7 @@ export class HeartbeatServiceImpl implements HeartbeatService {

// This is the "Firebase user agent" string from the platform logger
// service, not the browser user agent.
const userAgent = platformLogger.getPlatformInfoString();
const agent = platformLogger.getPlatformInfoString();
const date = getUTCDateString();
if (this._heartbeatsCache === null) {
this._heartbeatsCache = await this._heartbeatsCachePromise;
Expand All @@ -102,7 +102,7 @@ export class HeartbeatServiceImpl implements HeartbeatService {
return;
} else {
// There is no entry for this date. Create one.
this._heartbeatsCache.heartbeats.push({ date, userAgent });
this._heartbeatsCache.heartbeats.push({ date, agent });
}
// Remove entries older than 30 days.
this._heartbeatsCache.heartbeats = this._heartbeatsCache.heartbeats.filter(
Expand Down Expand Up @@ -138,7 +138,7 @@ export class HeartbeatServiceImpl implements HeartbeatService {
const { heartbeatsToSend, unsentEntries } = extractHeartbeatsForHeader(
this._heartbeatsCache.heartbeats
);
const headerString = base64Encode(
const headerString = base64urlEncodeWithoutPadding(
JSON.stringify({ version: 2, heartbeats: heartbeatsToSend })
);
// Store last sent date to prevent another being logged/sent for the same day.
Expand Down Expand Up @@ -180,12 +180,12 @@ export function extractHeartbeatsForHeader(
for (const singleDateHeartbeat of heartbeatsCache) {
// Look for an existing entry with the same user agent.
const heartbeatEntry = heartbeatsToSend.find(
hb => hb.userAgent === singleDateHeartbeat.userAgent
hb => hb.agent === singleDateHeartbeat.agent
);
if (!heartbeatEntry) {
// If no entry for this user agent exists, create one.
heartbeatsToSend.push({
userAgent: singleDateHeartbeat.userAgent,
agent: singleDateHeartbeat.agent,
dates: [singleDateHeartbeat.date]
});
if (countBytes(heartbeatsToSend) > maxSize) {
Expand Down Expand Up @@ -281,7 +281,7 @@ export class HeartbeatStorageImpl implements HeartbeatStorage {
*/
export function countBytes(heartbeatsCache: HeartbeatsByUserAgent[]): number {
// base64 has a restricted set of characters, all of which should be 1 byte.
return base64Encode(
return base64urlEncodeWithoutPadding(
// heartbeatsCache wrapper properties
JSON.stringify({ version: 2, heartbeats: heartbeatsCache })
).length;
Expand Down
4 changes: 2 additions & 2 deletions packages/app/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ export interface HeartbeatService {

// Heartbeats grouped by the same user agent string
export interface HeartbeatsByUserAgent {
userAgent: string;
agent: string;
dates: string[];
}

export interface SingleDateHeartbeat {
userAgent: string;
agent: string;
date: string;
}

Expand Down

0 comments on commit 303d844

Please sign in to comment.