Comparing version 2.1.0 to 3.0.0
135
lib/index.js
@@ -9,2 +9,3 @@ 'use strict'; | ||
const Hoek = require('hoek'); | ||
const Teamwork = require('teamwork'); | ||
@@ -17,9 +18,4 @@ | ||
exports.time = function (options, callback) { | ||
exports.time = async function (options = {}) { | ||
if (arguments.length !== 2) { | ||
callback = arguments[0]; | ||
options = {}; | ||
} | ||
const settings = Hoek.clone(options); | ||
@@ -30,33 +26,8 @@ settings.host = settings.host || 'time.google.com'; | ||
// Declare variables used by callback | ||
const team = new Teamwork(); | ||
let timeoutId = null; | ||
let sent = 0; | ||
// Ensure callback is only called once | ||
const finish = Hoek.once((err, result) => { | ||
clearTimeout(timeoutId); | ||
socket.removeAllListeners(); | ||
socket.once('error', Hoek.ignore); | ||
try { | ||
socket.close(); | ||
} | ||
catch (ignoreErr) { } // Ignore errors if the socket is already closed | ||
return callback(err, result); | ||
}); | ||
// Set timeout | ||
if (settings.timeout) { | ||
timeoutId = setTimeout(() => { | ||
const timeoutId = (settings.timeout ? setTimeout(() => team.attend(new Error('Timeout')), settings.timeout) : null); | ||
return finish(new Error('Timeout')); | ||
}, settings.timeout); | ||
} | ||
// Create UDP socket | ||
@@ -66,3 +37,3 @@ | ||
socket.once('error', (err) => finish(err)); | ||
socket.once('error', (err) => team.attend(err)); | ||
@@ -77,7 +48,11 @@ // Listen to incoming messages | ||
if (!message.isValid) { | ||
return finish(new Error('Invalid server response'), message); | ||
const error = new Error('Invalid server response'); | ||
error.time = message; | ||
return team.attend(error); | ||
} | ||
if (message.originateTimestamp !== sent) { | ||
return finish(new Error('Wrong originate timestamp'), message); | ||
const error = new Error('Wrong originate timestamp'); | ||
error.time = message; | ||
return team.attend(error); | ||
} | ||
@@ -108,3 +83,3 @@ | ||
return finish(null, message); | ||
return team.attend(message); | ||
} | ||
@@ -120,3 +95,3 @@ | ||
return finish(null, message); | ||
return team.attend(message); | ||
}); | ||
@@ -133,3 +108,3 @@ }); | ||
message[0] = (0 << 6) + (4 << 3) + (3 << 0); // Set version number to 4 and Mode to 3 (client) | ||
sent = Date.now(); | ||
const sent = Date.now(); | ||
internals.fromMsecs(sent, message, 40); // Set transmit timestamp (returns as originate) | ||
@@ -144,5 +119,20 @@ | ||
return finish(err || new Error('Could not send entire message')); | ||
return team.attend(err || new Error('Could not send entire message')); | ||
} | ||
}); | ||
try { | ||
return await team.work; | ||
} | ||
finally { | ||
clearTimeout(timeoutId); | ||
socket.removeAllListeners(); | ||
socket.once('error', Hoek.ignore); | ||
try { | ||
socket.close(); | ||
} | ||
catch (ignoreErr) { } // Ignore errors if the socket is already closed | ||
} | ||
}; | ||
@@ -312,9 +302,4 @@ | ||
exports.offset = function (options, callback) { | ||
exports.offset = async function (options = {}) { | ||
if (arguments.length !== 2) { | ||
callback = arguments[0]; | ||
options = {}; | ||
} | ||
const now = Date.now(); | ||
@@ -328,21 +313,15 @@ const clockSyncRefresh = options.clockSyncRefresh || 24 * 60 * 60 * 1000; // Daily | ||
process.nextTick(() => callback(null, internals.last.offset)); | ||
return; | ||
return internals.last.offset; | ||
} | ||
exports.time(options, (err, time) => { | ||
const time = await exports.time(options); | ||
if (err) { | ||
return callback(err, 0); | ||
} | ||
internals.last = { | ||
offset: Math.round(time.t), | ||
expires: now + clockSyncRefresh, | ||
host: options.host, | ||
port: options.port | ||
}; | ||
internals.last = { | ||
offset: Math.round(time.t), | ||
expires: now + clockSyncRefresh, | ||
host: options.host, | ||
port: options.port | ||
}; | ||
return callback(null, internals.last.offset); | ||
}); | ||
return internals.last.offset; | ||
}; | ||
@@ -359,35 +338,23 @@ | ||
exports.start = function (options, callback) { | ||
exports.start = async function (options = {}) { | ||
if (arguments.length !== 2) { | ||
callback = arguments[0]; | ||
options = {}; | ||
} | ||
if (internals.now.started) { | ||
process.nextTick(() => callback()); | ||
return; | ||
} | ||
const report = (err) => { | ||
const tick = async () => { | ||
if (err && | ||
options.onError) { | ||
options.onError(err); | ||
try { | ||
await exports.offset(options); | ||
} | ||
catch (err) { | ||
if (options.onError) { | ||
options.onError(err); | ||
} | ||
} | ||
}; | ||
internals.now.started = true; | ||
exports.offset(options, (err, offset) => { | ||
report(err); | ||
internals.now.intervalId = setInterval(() => { | ||
exports.offset(options, report); | ||
}, options.clockSyncRefresh || 24 * 60 * 60 * 1000); // Daily | ||
return callback(); | ||
}); | ||
internals.now.intervalId = setInterval(tick, options.clockSyncRefresh || 24 * 60 * 60 * 1000); // Daily | ||
await exports.offset(options); | ||
}; | ||
@@ -394,0 +361,0 @@ |
{ | ||
"name": "sntp", | ||
"description": "SNTP Client", | ||
"version": "2.1.0", | ||
"version": "3.0.0", | ||
"author": "Eran Hammer <eran@hammer.io> (http://hueniverse.com)", | ||
@@ -17,10 +17,11 @@ "repository": { | ||
"engines": { | ||
"node": ">=4.0.0" | ||
"node": ">=8.8.0" | ||
}, | ||
"dependencies": { | ||
"hoek": "4.x.x" | ||
"hoek": "5.x.x", | ||
"teamwork": "3.x.x" | ||
}, | ||
"devDependencies": { | ||
"code": "4.x.x", | ||
"lab": "14.x.x" | ||
"code": "5.x.x", | ||
"lab": "15.x.x" | ||
}, | ||
@@ -27,0 +28,0 @@ "scripts": { |
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
13571
2
262
+ Addedteamwork@3.x.x
+ Addedhoek@5.0.4(transitive)
+ Addedteamwork@3.2.0(transitive)
- Removedhoek@4.3.1(transitive)
Updatedhoek@5.x.x