@smarterservices/smarterclock
Advanced tools
Comparing version 1.0.3 to 1.0.5
12
index.js
var smarterClock = function (config) { | ||
//set client to be passed between methods | ||
this.client = require('ntp-client'); | ||
// if config is not passed, create an empty config object. | ||
if(!config){ | ||
config = {}; | ||
} | ||
//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}]; | ||
this.ntpServers = config.servers || [{server: this.client.defaultNtpServer, port: this.client.defaultNtpPort}]; | ||
//current server index | ||
@@ -11,7 +15,9 @@ this.currentIndex = 0; | ||
//tickrate for getting delta from ntp server | ||
this.tickRate = config.tickRate; | ||
this.tickRate = config.syncDelay || 300; | ||
// set the tickRate to milliseconds. | ||
this.tickRate = this.tickRate * 1000; | ||
//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; | ||
this.limit = config.history || 10; | ||
//do an initial sync | ||
@@ -18,0 +24,0 @@ this.syncTime(); |
{ | ||
"name": "@smarterservices/smarterclock", | ||
"version": "1.0.3", | ||
"version": "1.0.5", | ||
"description": "store of delta values between ntp time and local time to get an accurate sync time", | ||
@@ -15,3 +15,2 @@ "main": "index.js", | ||
"author": "jordan l piepkow", | ||
"license": "ISC", | ||
"dependencies": { | ||
@@ -18,0 +17,0 @@ "ntp-client": "^0.5.3" |
@@ -1,36 +0,77 @@ | ||
smarter-clock | ||
== | ||
Description: Run to get a synced up time to passed in ntp servers. Runs an average of delta values from ntp server and your local clock to get more accurate over time | ||
# SmarterClock | ||
Used to ensure the time used is in sync across distributed systems. The sync is achived by the following process: | ||
* Fetches the time from an NTP server. | ||
* Adjusts for network latency and transfer time | ||
* Computes the delta between the NTP server and the system clock and stores the delta for later use. | ||
* Uses all the stored deltas to get the average time drift from UTC. | ||
##Getting Started | ||
Install the module. | ||
npm install @smarterservices/smarterclock | ||
```javascript | ||
npm install @smarterservices/smarterclock | ||
``` | ||
Import the module into your codebase | ||
```javascript | ||
var smarterclock = require('@smarterservices/smarterclock') | ||
``` | ||
Create an instance of the clock object passing in the required params. See the options section below for options that can be used. | ||
```javascript | ||
var options = {}; | ||
// create a new instance | ||
var clock = new smarterclock(options); | ||
// get the current unix timestamp | ||
var currentTime = clock.getTime(); | ||
console.log(currentTime); | ||
``` | ||
## Options | ||
The clock constructor can accept the following options. | ||
* ```syncDelay``` (number) : The time (in seconds) between each call to an NTP server to get the latest UTC timestamp. Defaults to 300 (which is 5 minutes). | ||
* ```history``` (number): The nubmer of delta values that should be maintained and used for calculating your local time drift. Defaults to 10. | ||
* ```servers``` (array) : An array of NTP servers to use when looking up time. Each value in the array should be an object with the keys ```server``` and ```port```. | ||
### Example | ||
```javascript | ||
{ | ||
"syncDelay" : 60, | ||
"history": 10, | ||
"servers" : [{"server": "pool.ntp.org", "port": 123}] | ||
} | ||
``` | ||
var smarterclock = require('@smarterservices/smarterclock') | ||
## Methods | ||
###getTime() | ||
Returns unix timestamp based on delta values between server and your local time. This is the time that can be used instead of ```new Date().getTime()``` | ||
var config = { | ||
tickRate: 'numarical value for requesting ntp time i.e 60000=ever minute', //required | ||
arrayLimit: 'set the max size your array of delta values can be' | ||
ntpServers:[{server:'server address',port:'port for server'}]//array of server address and ports | ||
} | ||
#### Example | ||
```javascript | ||
clock.getTime(); | ||
``` | ||
var clock = new smarterclock(config) | ||
####Methods: | ||
clock.getTime() | ||
###syncTime() | ||
returns synced time based on delta values between server and your local time (unix mill) | ||
<hr> | ||
clock.syncTime() | ||
An on-demand method that will force a sync with an NTP server. | ||
will pull ntp time once and add a delta value to delta array | ||
<hr> | ||
clock.shiftServer() | ||
will shift the server you use to upll your ntp times if more servers in array | ||
<hr> | ||
clock.getDelta(callback) | ||
will add delta to this.delta array and return current delta to callback if passed | ||
```javascript | ||
clock.syncTime(); | ||
``` | ||
var smarterclock = require('./index.js'); | ||
var clock = new smarterclock(); | ||
var config = { | ||
tickRate:60000 | ||
}; | ||
setInterval(function(){ | ||
var localTime = new Date().getTime(); | ||
var syncTime = clock.getTime(); | ||
var drift = parseInt(localTime) - parseInt(syncTime); | ||
var clock = new smarterclock(config); | ||
setInterval(function(){ | ||
console.log('SyncTime:',clock.getTime()); | ||
},15000); | ||
console.log('SyncTime:' + syncTime + ' vs LocalTime: ' + localTime + ' Difference: ' + drift + 'ms'); | ||
},5000); |
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
7058
88
77
1