Comparing version 2.0.1 to 2.0.2
{ | ||
"name": "ntp-time", | ||
"version": "2.0.1", | ||
"version": "2.0.2", | ||
"description": "A ntp library for nodejs", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -5,9 +5,21 @@ const udp = require('dgram'); | ||
function defaultOnInvalidPacket(message, error) { | ||
console.error('[node-ntp-sync] Invalid packet received', message, error.message) | ||
} | ||
class Server extends EventEmitter { | ||
constructor() { | ||
constructor(options) { | ||
super(); | ||
options = options || {} | ||
this.socket = udp.createSocket('udp4'); | ||
this.socket.on('message', this.parse.bind(this)); | ||
if (options.onInvalidPacket && typeof options.onInvalidPacket == 'function') { | ||
this._onInvalidPacket = options.onInvalidPacket | ||
} else { | ||
this._onInvalidPacket = defaultOnInvalidPacket | ||
} | ||
return this; | ||
@@ -24,3 +36,3 @@ } | ||
this.socket.send(sendPackage, rinfo.port, rinfo.server, callback); | ||
this.socket.send(sendPackage, rinfo.port, rinfo.address, callback); | ||
@@ -44,10 +56,14 @@ return this; | ||
parse(message, rinfo) { | ||
const packet = NTPPacket.parse(message); | ||
const rxTimestamp = Math.floor(Date.now() / 1000); | ||
try { | ||
const packet = NTPPacket.parse(message); | ||
const rxTimestamp = Math.floor(Date.now() / 1000); | ||
packet.originateTimestamp = Math.floor(packet.txTimestamp); | ||
packet.referenceTimestamp = rxTimestamp - 5; | ||
packet.rxTimestamp = rxTimestamp; | ||
packet.originateTimestamp = Math.floor(packet.txTimestamp); | ||
packet.referenceTimestamp = rxTimestamp - 5; | ||
packet.rxTimestamp = rxTimestamp; | ||
this.emit('request', packet, this.send.bind(this, rinfo)); | ||
this.emit('request', packet, this.send.bind(this, rinfo)); | ||
} catch (error) { | ||
this._onInvalidPacket(message, error) | ||
} | ||
@@ -54,0 +70,0 @@ return; |
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
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
11496
268
2