MCPcopy Create free account
hub / github.com/Authenticator-Extension/Authenticator / syncTimeWithGoogle

Function syncTimeWithGoogle

src/syncTime.ts:3–45  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

1import { UserSettings } from "./models/settings";
2
3export async function syncTimeWithGoogle() {
4 await UserSettings.updateItems();
5
6 return new Promise(
7 (resolve: (value: string) => void, reject: (reason: Error) => void) => {
8 try {
9 // @ts-expect-error - these typings are wrong
10 const xhr = new XMLHttpRequest({ mozAnon: true });
11 xhr.open("HEAD", "https://www.google.com/generate_204");
12 const xhrAbort = setTimeout(() => {
13 xhr.abort();
14 return resolve("updateFailure");
15 }, 5000);
16 xhr.onreadystatechange = () => {
17 if (xhr.readyState === 4) {
18 clearTimeout(xhrAbort);
19 const date = xhr.getResponseHeader("date");
20 if (!date) {
21 return resolve("updateFailure");
22 }
23 const serverTime = new Date(date).getTime();
24 const clientTime = new Date().getTime();
25 const offset = Math.round((serverTime - clientTime) / 1000);
26
27 if (Math.abs(offset) <= 300) {
28 // within 5 minutes
29 UserSettings.items.offset = Math.round(
30 (serverTime - clientTime) / 1000
31 );
32 UserSettings.commitItems();
33 return resolve("updateSuccess");
34 } else {
35 return resolve("clock_too_far_off");
36 }
37 }
38 };
39 xhr.send();
40 } catch (error) {
41 return reject(error as Error);
42 }
43 }
44 );
45}

Callers 1

initFunction · 0.90

Calls 2

updateItemsMethod · 0.80
commitItemsMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…