UNPKG

@wiz-develop/server-clock

Version:

Server-synchronized clock implementation for browsers with WebWorker support

2 lines (1 loc) 2.97 kB
!(function(){"use strict";function e(e,t,n,r){return new(n||(n=Promise))((function(o,i){function s(e){try{a(r.next(e))}catch(e){i(e)}}function u(e){try{a(r.throw(e))}catch(e){i(e)}}function a(e){var t;e.done?o(e.value):(t=e.value,t instanceof n?t:new n((function(e){e(t)}))).then(s,u)}a((r=r.apply(e,t||[])).next())}))}"function"==typeof SuppressedError&&SuppressedError;const t=e=>`${e.getUTCFullYear()}/${(""+(e.getUTCMonth()+1)).padStart(2,"0")}/${(""+e.getUTCDate()).padStart(2,"0")} ${(""+e.getUTCHours()).padStart(2,"0")}:${(""+e.getUTCMinutes()).padStart(2,"0")}:${(""+e.getUTCSeconds()).padStart(2,"0")}.${(""+e.getUTCMilliseconds()).padStart(3,"0")}`,n=(t,n,r)=>e(void 0,void 0,void 0,(function*(){return"undefined"!=typeof navigator&&!navigator.onLine&&r?r:((e,t)=>{if(0===e.length)return{status:"client_only",offset:0};const{maxLb:n,minUb:r}=e.reduce(((e,t)=>({maxLb:Math.max(e.maxLb,t.lb),minUb:Math.min(e.minUb,t.ub)})),{maxLb:-1/0,minUb:1/0}),o=-(n+r)/2;return e.length>=t.length&&r>n&&500>r-n?{status:"accurate",offset:o}:{status:"server_only",offset:o}})((yield Promise.allSettled(t.map((t=>e(void 0,void 0,void 0,(function*(){const r=Date.now(),o=yield((t,...n)=>e(void 0,[t,...n],void 0,(function*(e,t=3e3){return Promise.race([fetch(e,{method:"GET"}),new Promise(((e,n)=>{setTimeout((()=>{n(Error("\u274c fetch timeout"))}),t)}))])})))(t,n).catch((e=>{throw e})),i=Date.now(),s=yield o.json();if("object"!=typeof(u=s)||null===u||!("requestReceivedAt"in u)||!("responseSentAt"in u)||null==u.requestReceivedAt||null==u.responseSentAt||"number"!=typeof u.requestReceivedAt||"number"!=typeof u.responseSentAt)throw Error(`Invalid server response from '${t}'`);var u;return((e,t,n)=>{const r=n.requestReceivedAt,o=n.responseSentAt;return Object.assign(Object.assign({},n),{it:e,rt:t,rtt:t-e,diff:(r-e-(t-o))/2,lb:e-16-r,ub:t+16-o})})(r,i,s)})))))).filter((e=>"fulfilled"===e.status)).map((e=>e.value)),t)}));if(void 0===self)throw new TypeError("This script must be executed in a WebWorker environment");let r={status:"pending",offset:0},o=null,i=null;self.addEventListener("message",(s=>e(void 0,void 0,void 0,(function*(){switch(s.data.type){case"start":if(null==s.data.serverUrls)break;yield((s,...u)=>e(void 0,[s,...u],void 0,(function*(s,u=18e4){o&&clearInterval(o),i&&clearInterval(i),r=yield n(s,3e3),i=setInterval((()=>e(void 0,void 0,void 0,(function*(){r=yield n(s,3e3,r)}))),u),o=setInterval((()=>{const e=(e=>{const n=new Date,r=6e4*n.getTimezoneOffset(),o=n.getTime()+e.offset+50,i={status:e.status,offset:Math.round(e.offset/1e3*10)/10,LOCAL:new Date(n.getTime()-r),JST:new Date(o+324e5),UTC:new Date(o),LOC:new Date(o-r)};return Object.assign(Object.assign({},i),{LOCAL_STR:t(i.LOCAL),JST_STR:t(i.JST),UTC_STR:t(i.UTC),LOC_STR:t(i.LOC)})})(r);self.postMessage(e)}),100)})))(s.data.serverUrls,s.data.fetchInterval);break;case"stop":o&&(clearInterval(o),o=null),i&&(clearInterval(i),i=null)}})))),self.addEventListener("error",(e=>{}))})();