Skip to content

Commit c8f8715

Browse files
committed
Fix logic
1 parent b3e7e1b commit c8f8715

File tree

2 files changed

+39
-12
lines changed

2 files changed

+39
-12
lines changed

packages/app/src/heartbeatSize.test.ts

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
* limitations under the License.
1616
*/
1717

18+
import { base64Encode } from '@firebase/util';
1819
import { expect } from 'chai';
1920
import '../test/setup';
2021
import {
@@ -41,7 +42,7 @@ function generateDates(count: number): string[] {
4142
return dates;
4243
}
4344

44-
describe.only('splitHeartbeatsCache()', () => {
45+
describe('splitHeartbeatsCache()', () => {
4546
it('returns empty heartbeatsToKeep if it cannot get under maxSize', () => {
4647
const heartbeats = [
4748
{ userAgent: generateUserAgentString(1), dates: generateDates(1) }
@@ -86,3 +87,28 @@ describe.only('splitHeartbeatsCache()', () => {
8687
);
8788
});
8889
});
90+
91+
describe('countBytes()', () => {
92+
it('counts how many bytes there will be in a stringified, encoded header', () => {
93+
const heartbeats = [
94+
{ userAgent: generateUserAgentString(1), dates: generateDates(1) },
95+
{ userAgent: generateUserAgentString(3), dates: generateDates(2) }
96+
];
97+
let size: number = 0;
98+
const headerString = base64Encode(
99+
JSON.stringify({ version: 2, heartbeats })
100+
);
101+
console.log(JSON.stringify({ version: 2, heartbeats }));
102+
// We don't use this measurement method in the app because user
103+
// environments are much more unpredictable while we know the
104+
// tests will run in either a standard headless browser or Node.
105+
if (typeof Blob !== 'undefined') {
106+
const blob = new Blob([headerString]);
107+
size = blob.size;
108+
} else if (typeof Buffer !== 'undefined') {
109+
const buffer = Buffer.from(headerString);
110+
size = buffer.byteLength;
111+
}
112+
expect(countBytes(heartbeats)).to.equal(size);
113+
});
114+
});

packages/app/src/heartbeatSize.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@
1515
* limitations under the License.
1616
*/
1717

18+
import { base64Encode } from '@firebase/util';
1819
import { HeartbeatsByUserAgent } from './types';
1920

20-
const BASE64_SIZE_MULTIPLIER = 4 / 3;
21-
const BYTES_PER_DATE = 12 * BASE64_SIZE_MULTIPLIER;
22-
21+
/**
22+
* Calculate byte length of a string. From:
23+
* https://codereview.stackexchange.com/questions/37512/count-byte-length-of-string
24+
*/
2325
function getByteLength(str: string): number {
2426
let byteLength = 0;
2527
for (let i = 0; i < str.length; i++) {
@@ -41,7 +43,7 @@ function getByteLength(str: string): number {
4143
* being stringified and converted to base64.
4244
*/
4345
export function countHeartbeatBytes(heartbeat: HeartbeatsByUserAgent): number {
44-
return getByteLength(JSON.stringify(heartbeat)) * BASE64_SIZE_MULTIPLIER;
46+
return getByteLength(base64Encode(JSON.stringify(heartbeat)));
4547
}
4648

4749
/**
@@ -51,13 +53,9 @@ export function countHeartbeatBytes(heartbeat: HeartbeatsByUserAgent): number {
5153
*/
5254
export function countBytes(heartbeatsCache: HeartbeatsByUserAgent[]): number {
5355
// heartbeatsCache wrapper properties
54-
let count =
55-
getByteLength(JSON.stringify({ version: 2, heartbeats: [] })) *
56-
BASE64_SIZE_MULTIPLIER;
57-
for (const heartbeat of heartbeatsCache) {
58-
count += countHeartbeatBytes(heartbeat);
59-
}
60-
return count;
56+
return getByteLength(
57+
base64Encode(JSON.stringify({ version: 2, heartbeats: heartbeatsCache }))
58+
);
6159
}
6260

6361
/**
@@ -73,6 +71,9 @@ export function splitHeartbeatsCache(
7371
heartbeatsToSend: HeartbeatsByUserAgent[];
7472
heartbeatsToKeep: HeartbeatsByUserAgent[];
7573
} {
74+
const BYTES_PER_DATE = getByteLength(
75+
base64Encode(JSON.stringify('2022-12-12'))
76+
);
7677
let totalBytes = 0;
7778
const heartbeatsToSend = [];
7879
const heartbeatsToKeep = [...heartbeatsCache];

0 commit comments

Comments
 (0)