Skip to content

Commit 311e55b

Browse files
committed
Address PR comments
1 parent a3bd347 commit 311e55b

File tree

1 file changed

+8
-26
lines changed

1 file changed

+8
-26
lines changed

packages/app/src/heartbeatService.ts

Lines changed: 8 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ import {
3434
SingleDateHeartbeat
3535
} from './types';
3636

37-
const MAX_HEADER_BYTES = 1000;
37+
const MAX_HEADER_BYTES = 1024;
3838
// 30 days
3939
const STORED_HEARTBEAT_RETENTION_MAX_MILLIS = 30 * 24 * 60 * 60 * 1000;
4040

@@ -100,7 +100,7 @@ export class HeartbeatServiceImpl implements HeartbeatService {
100100
return;
101101
} else {
102102
// There is no entry for this date. Create one.
103-
this._heartbeatsCache!.push({ date, userAgent });
103+
this._heartbeatsCache.push({ date, userAgent });
104104
}
105105
// Remove entries older than 30 days.
106106
this._heartbeatsCache = this._heartbeatsCache.filter(
@@ -152,6 +152,7 @@ export class HeartbeatServiceImpl implements HeartbeatService {
152152

153153
function getUTCDateString(): string {
154154
const today = new Date();
155+
// Returns date format 'YYYY-MM-DD'
155156
return today.toISOString().substring(0, 10);
156157
}
157158

@@ -275,34 +276,15 @@ export class HeartbeatStorageImpl implements HeartbeatStorage {
275276
}
276277
}
277278

278-
/**
279-
* Calculate byte length of a string. From:
280-
* https://codereview.stackexchange.com/questions/37512/count-byte-length-of-string
281-
*/
282-
export function getByteLength(str: string): number {
283-
let byteLength = 0;
284-
for (let i = 0; i < str.length; i++) {
285-
const c = str.charCodeAt(i);
286-
byteLength +=
287-
(c & 0xf800) === 0xd800
288-
? 2 // Code point is half of a surrogate pair
289-
: c < 1 << 7
290-
? 1
291-
: c < 1 << 11
292-
? 2
293-
: 3;
294-
}
295-
return byteLength;
296-
}
297-
298279
/**
299280
* Calculate bytes of a HeartbeatsByUserAgent array after being wrapped
300281
* in a platform logging header JSON object, stringified, and converted
301282
* to base 64.
302283
*/
303284
export function countBytes(heartbeatsCache: HeartbeatsByUserAgent[]): number {
304-
// heartbeatsCache wrapper properties
305-
return getByteLength(
306-
base64Encode(JSON.stringify({ version: 2, heartbeats: heartbeatsCache }))
307-
);
285+
// base64 has a restricted set of characters, all of which should be 1 byte.
286+
return base64Encode(
287+
// heartbeatsCache wrapper properties
288+
JSON.stringify({ version: 2, heartbeats: heartbeatsCache })
289+
).length;
308290
}

0 commit comments

Comments
 (0)