internet-time
Advanced tools
Comparing version 0.0.13 to 0.0.14
@@ -1,1 +0,1 @@ | ||
const{CircularQueue}=require("@ganorberg/data-structures-javascript");function avg(b){return b.reduce((a,b)=>a+b)/b.length}module.exports=class{constructor({sendRequest:a,createRequest:b}={}){this.tDelta=null,this.rtt=null,this.timer=null,this.alpha=.01,this.updatePeriod=125,this.sendRequest=a,this.createRequest=b,this.deltaBuf=new CircularQueue(512),this.fHiddenTab=!1;try{this.fHiddenTab=document.hidden,document.addEventListener("visibilitychange",()=>{this.fHiddenTab=document.hidden})}catch(a){}}processResponse(a){const b=this.createRequest(),{tTx:c,tServer:d}=a,e=b-c;return this.tDelta=this.calcNewDelta(d-e/2-c),this.rtt=e,this.tDelta}calcNewDelta(a){return this.deltaBuf.enqueue(a),avg(this.deltaBuf.ring.filter(Boolean))}getDelta(){return this.tDelta}getRtt(){return this.rtt}start(){null!=this.timer&&this.stop(),this.sendAutoRequest(!0),this.timer=setInterval(this.sendAutoRequest.bind(this),this.updatePeriod)}stop(){null==this.timer||(clearInterval(this.timer),this.timer=null)}sendAutoRequest(a){if(!this.fHiddenTab||a){const a=this.createRequest();this.sendRequest(a)}}}; | ||
const{CircularQueue}=require("@ganorberg/data-structures-javascript");function avg(b){return b.reduce((a,b)=>a+b)/b.length}module.exports=class{constructor({sendRequest:a,createRequest:b}={}){this.tDelta=null,this.rtt=null,this.timer=null,this.alpha=.01,this.updatePeriod=125,this.sendRequest=a,this.createRequest=b,this.deltaBuf=new CircularQueue(512),this.rttBuf=new CircularQueue(512),this.running=!1,this.fHiddenTab=!1;try{this.fHiddenTab=document.hidden,document.addEventListener("visibilitychange",()=>{this.fHiddenTab=document.hidden})}catch(a){}}processResponse(a){const b=this.createRequest(),{tTx:c,tServer:d}=a,e=b-c;return this.tDelta=this.calcNewDelta(d-e/2-c),this.rtt=e,this.tDelta}calcNewDelta(a){return this.deltaBuf.enqueue(a),avg(this.deltaBuf.ring.filter(Boolean))}getDelta(){return this.tDelta}getRtt(){return avg(this.rttBuf.ring.filter(Boolean))}async start(){if(!this.running)for(this.running=!0,await this.sendAutoRequest(!0);this.running;){const a=this.createRequest();await this.sendAutoRequest();const b=this.createRequest();this.rttBuf.enqueue(b-a)}}stop(){this.running=!1}async sendAutoRequest(a){if(!this.fHiddenTab||a){const a=this.createRequest();await this.sendRequest(a)}}}; |
@@ -1,1 +0,1 @@ | ||
const EventEmitter=require("events"),WebSocket=require("isomorphic-ws"),perf=require("./performance"),TimeClient=require("./client"),e=new EventEmitter;let s;const timeClient=new TimeClient({sendRequest:a=>{try{s.send(a)}catch(a){}},createRequest:()=>perf.now()+perf.timeOrigin}),socket=()=>(s||(s=new WebSocket("wss://time-server.fly.dev/"),s.onclose=()=>{s=void 0,socket()},s.onmessage=({data:a})=>{const b=a.split(" "),c=timeClient.processResponse({tTx:+b[0],tServer:+(b[1]/1e3)});e.emit("delta",c)},s.onerror=()=>{},s.onopen=()=>timeClient.start()),s);e.delta=()=>(socket(),timeClient.getDelta()||0),e.now=()=>perf.timeOrigin+perf.now()+e.delta(),e.nextDelta=()=>new Promise(a=>{e.now(),e.once("delta",a)}),module.exports=e; | ||
const EventEmitter=require("events");require("isomorphic-fetch");const perf=require("./performance"),TimeClient=require("./client"),e=new EventEmitter,timeClient=new TimeClient({sendRequest:async a=>{try{const b=await fetch("https://cloudflare.com/cdn-cgi/trace"),c=await b.text(),d=c.split("\n"),f=+d.find(a=>a.startsWith("ts=")).slice(3),g=timeClient.processResponse({tTx:a,tServer:1e3*f});e.emit("delta",g)}catch(a){}},createRequest:()=>perf.now()+perf.timeOrigin});e.delta=()=>(timeClient.start(),timeClient.getDelta()||0),e.now=()=>perf.timeOrigin+perf.now()+e.delta(),e.nextDelta=()=>new Promise(a=>{e.now(),e.once("delta",a)}),e.latency=()=>timeClient.getRtt(),module.exports=e; |
{ | ||
"name": "internet-time", | ||
"version": "0.0.13", | ||
"version": "0.0.14", | ||
"main": "dist/index.js", | ||
@@ -13,2 +13,3 @@ "license": "MIT", | ||
"clocksy": "^1.1.0", | ||
"isomorphic-fetch": "^2.2.1", | ||
"isomorphic-ws": "^4.0.1", | ||
@@ -15,0 +16,0 @@ "socket.io-client": "^2.2.0", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
2853
6
2
+ Addedisomorphic-fetch@^2.2.1
+ Addedencoding@0.1.13(transitive)
+ Addediconv-lite@0.6.3(transitive)
+ Addedis-stream@1.1.0(transitive)
+ Addedisomorphic-fetch@2.2.1(transitive)
+ Addednode-fetch@1.7.3(transitive)
+ Addedsafer-buffer@2.1.2(transitive)
+ Addedwhatwg-fetch@3.6.20(transitive)