Skip to content

feat: add callback support to updater function #127

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
62 changes: 39 additions & 23 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -360,40 +360,56 @@ Device.prototype.toJSON = function toJSON() {
* Int3rNetz when we want to. We will be using the compiled version by default
* but users can opt-in for updates.
*
* @param {Boolean} refresh Refresh the dataset from the remote
* @param {updateRequestCallback} [cb] - callback to be invoked after request completes
* @api public
*/
module.exports = function updater() {
try {
require('./lib/update').update(function updating(err, results) {
if (err) {
console.log('[useragent] Failed to update the parsed due to an error:');
console.log('[useragent] '+ (err.message ? err.message : err));
return;
}

regexps = results;

// OperatingSystem parsers:
osparsers = regexps.os;
osparserslength = osparsers.length;
module.exports = function updater(cb) {
var update
cb = typeof cb === 'function' ? cb : function noop () {}

// UserAgent parsers:
agentparsers = regexps.browser;
agentparserslength = agentparsers.length;

// Device parsers:
deviceparsers = regexps.device;
deviceparserslength = deviceparsers.length;
});
try {
update = require('./lib/update').update
} catch (e) {
console.error('[useragent] If you want to use automatic updating, please add:');
console.error('[useragent] - request (npm install request --save)');
console.error('[useragent] - yamlparser (npm install yamlparser --save)');
console.error('[useragent] To your own package.json');
cb(e)
return
}

update(function updating(err, results) {
if (err) {
console.log('[useragent] Failed to update the parsed due to an error:');
console.log('[useragent] '+ (err.message ? err.message : err));
cb(err)
return;
}

regexps = results;

// OperatingSystem parsers:
osparsers = regexps.os;
osparserslength = osparsers.length;

// UserAgent parsers:
agentparsers = regexps.browser;
agentparserslength = agentparsers.length;

// Device parsers:
deviceparsers = regexps.device;
deviceparserslength = deviceparsers.length;

cb(undefined, results)
});
};

/**
* @callback updateRequestCallback
* @param {Error=}
* @param {object=}
*/

// Override the exports with our newly set module.exports
exports = module.exports;

Expand Down
4 changes: 4 additions & 0 deletions test/parser.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ describe('useragent', function () {
assume(useragent.is).is.a('function')
});

it('should expose the updater function', function () {
assume(useragent).is.a('function');
});

describe('#parse', function () {
it('correctly transforms everything to the correct instances', function () {
var agent = useragent.parse(ua);
Expand Down