New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@smarterservices/smarterclock

Package Overview
Dependencies
Maintainers
4
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@smarterservices/smarterclock - npm Package Compare versions

Comparing version 1.0.1 to 1.0.2

readme.md

81

index.js

@@ -1,39 +0,86 @@

var smarterClock = function (tickRate, arrayLimit) {
var smarterClock = function (config) {
//set client to be passed between methods
this.client = require('ntp-client');
//set your servers array that you will use only shifting to a new server when the first one quits working
this.ntpServers = config.ntpServers || [{server: this.client.defaultNtpServer, port: this.client.defaultNtpPort}];
//current server index
this.currentIndex = 0;
//set current server using current server index
this.currentServer = this.ntpServers[this.currentIndex];
console.log(this.currentServer)
//tickrate for getting delta from ntp server
this.tickRate = config.tickRate;
//array containing delta values
this.delta = [];
//array upper limit. Once reached the oldest value will be discarded for the new value
this.limit = config.arrayLimit || 1000;
//do an initial sync
this.syncTime();
this.limit = arrayLimit || 1000;
this.startTick(tickRate, arrayLimit);
//start your interval to get deltas between your local time and ntp time
this.startTick();
};
smarterClock.prototype.startTick = function (tickRate, arrayLimit) {
//this function is called when the current ntp server times out
smarterClock.prototype.shiftServer = function () {
//if another server in ntpservers array shift current server to that server
if (this.ntpServers[this.currentIndex + 1]) {
this.currentIndex++;
this.currentServer = this.ntpServers[this.currentIndex];
}
};
//this function is called to start pulling your delta values from ntp using the tickRate set in the constructor
smarterClock.prototype.startTick = function () {
//start interval
setInterval(function () {
this.client.getNetworkTime(null, null, function (err, date) {
var tempServerTime = date.getTime();
var tempLocalTime = (new Date()).getTime();
if (this.delta.length === this.limit) {
this.delta.shift();
//get ntp time
this.client.getNetworkTime(this.currentServer.server, this.currentServer.port, function (err, date) {
if (err) {
//if error shift server for next time around
console.log('Shifting to backup server');
this.shiftServer();
} else {
//get delta by comparing server time and your client time
var tempServerTime = date.getTime();
var tempLocalTime = (new Date()).getTime();
if (this.delta.length === this.limit) {
this.delta.shift();
}
//add delta value to delta array
this.delta.push(tempServerTime - tempLocalTime);
}
this.delta.push(tempServerTime - tempLocalTime);
}.bind(this))
}.bind(this), tickRate);
}.bind(this), this.tickRate);
};
//this function is used to get your sync time based on average of delta times
smarterClock.prototype.getSyncTime = function () {
//get sum of this.delta array
var sum = this.delta.reduce(function (a, b) {
return a + b;
});
//get avg delta of your local time compared to ntp time
var avg = Math.round(sum / this.delta.length);
//return your time +/- the avg delta
return ((new Date()).getTime() + avg);
};
//this function is used for a one off sync(adds one delta value to the this.delta array)
smarterClock.prototype.syncTime = function () {
this.client.getNetworkTime(null, null, function (err, date) {
var tempServerTime = date.getTime();
var tempLocalTime = (new Date()).getTime();
if (this.delta.length === this.limit) {
this.delta.shift();
//get the ntp time
this.client.getNetworkTime(this.currentServer.server, this.currentServer.port, function (err, date) {
if (err) {
//shift server if an error happens
console.log('Shifting to backup server');
this.shiftServer();
} else {
//get delta value and push into this.delta array
var tempServerTime = date.getTime();
var tempLocalTime = (new Date()).getTime();
if (this.delta.length === this.limit) {
this.delta.shift();
}
this.delta.push(tempServerTime - tempLocalTime);
}
this.delta.push(tempServerTime - tempLocalTime);
}.bind(this))
};
//return smarterClock
module.exports = smarterClock;
{
"name": "@smarterservices/smarterclock",
"version": "1.0.1",
"version": "1.0.2",
"description": "store of delta values between ntp time and local time to get an accurate sync time",

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

var smarterclock = require('./index.js');
var clock = new smarterclock(60000);
var config = {
tickRate:60000
};
var clock = new smarterclock(config);
setInterval(function(){
console.log('SyncTime:',clock.getSyncTime());
},15000);
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