Skip to content
This repository has been archived by the owner on Apr 5, 2024. It is now read-only.

Do not require @hapi/sntp anymore #277

Merged
merged 1 commit into from
Nov 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,13 +158,19 @@ Request(requestOptions, function (error, response, body) {
});
```

**Hawk** utilized the [**SNTP**](https://github.com/hueniverse/sntp) module for time sync management. By default, the local
machine time is used. To automatically retrieve and synchronize the clock within the application, use the SNTP 'start()' method.
## Time Sync

**Hawk** can utilize the [`@hapi/sntp`](https://github.com/outmoded/sntp) module for time sync management.
By default, the local machine time is used.
To automatically retrieve and synchronize the clock within the application, add `@hapi/sntp` to your application and initialize it at the top level:

```js
Hawk.sntp.start();
```
const Sntp = require('@hapi/sntp');
const Hawk = require('hawk');

Sntp.start();
Hawk.utils.setTimeFunction(Sntp.now)
```

## Protocol Example

Expand Down
2 changes: 0 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
'use strict';


exports.sntp = require('@hapi/sntp');

exports.server = require('./server');

exports.client = require('./client');
Expand Down
14 changes: 12 additions & 2 deletions lib/utils.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';

const Boom = require('@hapi/boom');
const Sntp = require('@hapi/sntp');


const internals = {};
Expand Down Expand Up @@ -93,9 +92,20 @@ exports.parseRequest = function (req, options) {
};


let _now = Date.now;


// override the `now` function, e.g., to use sntp

exports.setTimeFunction = function (fn) {

_now = fn;
};


exports.now = function (localtimeOffsetMsec) {

return Sntp.now() + (localtimeOffsetMsec || 0);
return _now() + (localtimeOffsetMsec || 0);
};


Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@
"@hapi/b64": "5.x.x",
"@hapi/boom": "9.x.x",
"@hapi/cryptiles": "5.x.x",
"@hapi/hoek": "9.x.x",
"@hapi/sntp": "4.x.x"
"@hapi/hoek": "9.x.x"
},
"devDependencies": {
"@hapi/code": "8.x.x",
Expand Down
16 changes: 15 additions & 1 deletion test/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const Package = require('../package.json');
const internals = {};


const { describe, it } = exports.lab = Lab.script();
const { describe, it, after } = exports.lab = Lab.script();
const expect = Code.expect;


Expand Down Expand Up @@ -119,6 +119,20 @@ describe('Utils', () => {
});
});

describe('setTimeFunction()', () => {

after(() => {

Hawk.utils.setTimeFunction(Date.now);
});

it('creates the value that now() will return', () => {

Hawk.utils.setTimeFunction(() => 269323200000);
expect(Hawk.utils.now()).to.equal(269323200000);
});
});

describe('unauthorized()', () => {

it('returns a hawk 401', () => {
Expand Down