Skip to content

Commit 354ba8d

Browse files
committed
Handle microtime() collisions
1 parent 8012ce0 commit 354ba8d

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

timeflux_ui/www/common/assets/js/timeflux.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ class IO {
200200
* @returns {boolean}
201201
*/
202202
send(command, payload) {
203-
try {
203+
try{
204204
let message = JSON.stringify({command: command, payload: payload});
205205
this.socket.send(message);
206206
} catch {
@@ -419,6 +419,7 @@ Object.assign(IO.prototype, Dispatcher);
419419

420420
/**
421421
* RFC-compliant UUID
422+
*
422423
* @see {@link https://www.ietf.org/rfc/rfc4122.txt}
423424
*/
424425
function uuidv4() {
@@ -434,6 +435,25 @@ function microtime() {
434435
return performance.now() + performance.timing.navigationStart;
435436
}
436437

438+
/**
439+
* Same as Date.now(), but with microsecond precision
440+
*
441+
* To offer protection against timing attacks and fingerprinting, the precision of performance.now()
442+
* might get rounded depending on browser settings. Currently, the resolution is 5μs in Chrome.
443+
* To avoid collisions, we add 1μs if two consecutive timestamps are identical.
444+
*
445+
* @see {@link https://developer.mozilla.org/en-US/docs/Web/API/Performance/now}
446+
* @see {@link https://hal.inria.fr/hal-03215569/document}
447+
* @see {@link https://gruss.cc/files/fantastictimers.pdf}
448+
*/
449+
function microtime() {
450+
let ts = performance.now();
451+
if (ts <= globalThis._last_microtime) ts = globalThis._last_microtime + 1e-3;
452+
globalThis._last_microtime = ts;
453+
return ts + performance.timing.navigationStart;
454+
}
455+
456+
437457
/**
438458
* Performs a deep merge of objects and returns new object. Does not modify
439459
* objects (immutable) and merges arrays via concatenation.

0 commit comments

Comments
 (0)