@@ -34,7 +34,7 @@ import {
34
34
SingleDateHeartbeat
35
35
} from './types' ;
36
36
37
- const MAX_HEADER_BYTES = 1000 ;
37
+ const MAX_HEADER_BYTES = 1024 ;
38
38
// 30 days
39
39
const STORED_HEARTBEAT_RETENTION_MAX_MILLIS = 30 * 24 * 60 * 60 * 1000 ;
40
40
@@ -100,7 +100,7 @@ export class HeartbeatServiceImpl implements HeartbeatService {
100
100
return ;
101
101
} else {
102
102
// There is no entry for this date. Create one.
103
- this . _heartbeatsCache ! . push ( { date, userAgent } ) ;
103
+ this . _heartbeatsCache . push ( { date, userAgent } ) ;
104
104
}
105
105
// Remove entries older than 30 days.
106
106
this . _heartbeatsCache = this . _heartbeatsCache . filter (
@@ -152,6 +152,7 @@ export class HeartbeatServiceImpl implements HeartbeatService {
152
152
153
153
function getUTCDateString ( ) : string {
154
154
const today = new Date ( ) ;
155
+ // Returns date format 'YYYY-MM-DD'
155
156
return today . toISOString ( ) . substring ( 0 , 10 ) ;
156
157
}
157
158
@@ -275,34 +276,15 @@ export class HeartbeatStorageImpl implements HeartbeatStorage {
275
276
}
276
277
}
277
278
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
-
298
279
/**
299
280
* Calculate bytes of a HeartbeatsByUserAgent array after being wrapped
300
281
* in a platform logging header JSON object, stringified, and converted
301
282
* to base 64.
302
283
*/
303
284
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 ;
308
290
}
0 commit comments