Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

clockskew

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

clockskew - npm Package Compare versions

Comparing version 0.2.1 to 0.2.2

client.getfirebasetime.js

124

client.js

@@ -25,71 +25,2 @@ (function(){

function getHttpTime(url,getTime,callback) {
var start,end;
xhr = new XMLHttpRequest();
xhr.open('GET', url);
xhr.onreadystatechange = function() {
if(xhr.readyState === 2){
end = getTime();
} else if (xhr.readyState == 4) {
if(xhr.status === 200){
callback(null,[start,xhr.responseText * 1.0,end]);
} else {
callback(xhr.status,xhr.responseText);
}
}
};
xhr.send();
start = getTime();
}
function getFirebaseTime(url,getTime,callback) {
var ref = new Firebase(url);
var start = null;
ref.set(Firebase.ServerValue.TIMESTAMP);
ref.on('value',function(snap){
if(start === null){
start = getTime();
} else {
end = getTime();
callback(null,[start,snap.val(),end]);
ref.off();
}
});
}
function getSocketTime(url){
var websocket;
var queue = [];
function create(){
queue.splice(1);
if(websocket) websocket.close();
websocket = new WebSocket(url);
websocket.onopen = function(){
websocket.send('?');
};
websocket.onmessage = function(e){
var stuff = queue.shift();
if(stuff){
stuff[0](null,[stuff[1],e.data,stuff[2]()]);
}
};
websocket.onerror = function(){
websocket.close();
websocket = null;
};
}
return function get(getTime,callback){
var start = getTime();
queue.push([callback,start,getTime]);
if(!websocket || websocket.readyState > 1) {
create();
} else {
websocket.send('?');
}
};
}
/**

@@ -129,3 +60,3 @@ * CLIENT SERVER

this.options.interval = options.interval || 1000;
this.options.waitInterval = options.waitInterval || this.options.interval * 30; // 30 sec
this.options.waitInterval = options.waitInterval || this.options.interval * 15; // 15 sec
this.options.minRttValues = options.minRttValues || 5;

@@ -138,4 +69,5 @@ this.options.minSkewValues = options.minSkewValues || 5;

this.getTime = options.getTime || getTime;
if(options.getServerTime){
this.getServerTime = options.getServerTime.bind(null,options.getTime || getTime);
this.getServerTime = options.getServerTime.bind(null,this.getTime);
} else {

@@ -152,8 +84,6 @@ throw new Error('getServerTime is a required option');

this.onServerTime = this.onServerTime.bind(this);
this.promises = [];
}
ClockSkew.getTime = getTime;
ClockSkew.getFirebaseTime = getFirebaseTime;
ClockSkew.getHttpTime = getHttpTime;
ClockSkew.getSocketTime = getSocketTime;

@@ -164,2 +94,6 @@ ClockSkew.prototype.start = function(){

this.timerId = this.setTimeout(this.calculate,this.options.interval || 1000);
var self = this;
return new Promise(function(resolve){
self.promises.push(resolve);
});
};

@@ -234,2 +168,5 @@

//this.reset();
this.promises.forEach(function(resolve){
resolve(avg);
});
}

@@ -260,7 +197,44 @@ }

/**
* Utils
*/
ClockSkew.prototype.getTimeUntilNextCycle = function(interval,offset){
offset = offset || 0;
var serverTime = this.getTime() - this.skew;
var timeInCycle = serverTime % interval;
if(timeInCycle < 0) timeInCycle += interval;
var timeUntilNextCycle = interval - timeInCycle;
return timeUntilNextCycle;
};
ClockSkew.prototype.getTimeOfNextCycle = function(interval,offset){
offset = offset || 0;
var now = this.getTime();
var serverTime = now - this.skew + offset;
var timeInCycle = serverTime % interval;
if(timeInCycle < 0) timeInCycle += interval;
var timeUntilNextCycle = interval - timeInCycle;
return now + timeUntilNextCycle;
};
ClockSkew.prototype.onNextCycle = function(callback,interval){
setTimeout(callback,this.getTimeUntilNextCycle(interval));
};
ClockSkew.prototype.toServerTime = function toServerTime(time,skew){
time = time || this.getTime();
skew = skew || this.skew;
return time - skew;
};
if(typeof module !== 'undefined'){
module.exports = ClockSkew;
} else {
}
if(typeof window !== 'undefined'){
window.ClockSkew = ClockSkew;
}
})();
{
"name": "clockskew",
"version": "0.2.1",
"version": "0.2.2",
"description": "Websocket TimeServer and client library to calculate clock skew",

@@ -5,0 +5,0 @@ "main": "server.js",

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc