Skip to content
Draft
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
3 changes: 2 additions & 1 deletion eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ module.exports = [
'no-sequences': 'error',
'no-shadow': 'error',
'no-shadow-restricted-names': 'error',
'func-call-spacing': 'error',
'func-call-spacing': ['error', 'never'],
'no-sparse-arrays': 'warn',
'no-sync': 'warn',
'no-ternary': 'off',
Expand All @@ -129,6 +129,7 @@ module.exports = [
'operator-assignment': ['error', 'always'],
'operator-linebreak': ['error', 'before'],
'padded-blocks': 'off',
'prefer-template': 'error',
'quote-props': ['error', 'consistent'],
'quotes': ['error', 'single', 'avoid-escape'],
'radix': 'error',
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
},
"main": "vnstat-dumpdb.js",
"files": [
"vnstat-dumpdb.js",
"preinstall.js"
],
"dependencies": {},
Expand Down
26 changes: 15 additions & 11 deletions preinstall.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
var exec = require ('child_process') .exec;
var exec = require( 'child_process' ).exec;

var bin = process.env.NODE_APP_BIN || 'vnstat';

exec (bin + ' --version', function (err, res) {
if (err) {
throw err;
exec( `${bin} --version`, function ( err, res ) {
if ( err ) {
console.warn( 'Warning: vnstat not found. This package requires vnStat >= v1.13 to be installed.' );
console.warn( 'Install vnStat from: https://github.com/vergoh/vnstat' );
return;
}

res.replace (/^vnStat (\d+)\.(\d+) /, function (s, major, minor) {
if (major >= 1 && minor >= 13) {
res.replace( /^vnStat (\d+)\.(\d+) /, function ( s, major, minor ) {
major = parseInt( major, 10 );
minor = parseInt( minor, 10 );

if ( major > 1 || ( major === 1 && minor >= 13 ) ) {
return;
}

console.log ('Wrong vnStat version: requires >= v1.13, but v' + major + '.' + minor + ' installed');
console.log ('Command run: ' + bin);
process.exit (1);
});
});
console.warn( `Warning: Wrong vnStat version: requires >= v1.13, but v${major}.${minor} installed` );
console.warn( `Command run: ${bin}` );
} );
} );
156 changes: 78 additions & 78 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,102 +1,102 @@
var dotest = require ('dotest');
var app = require ('./');
var dotest = require( 'dotest' );
var app = require( './' );

// Setup
// $ NODE_APP_IFACE=eth0 npm test
var config = {
bin: process.env.NODE_APP_BIN || null
bin: process.env.NODE_APP_BIN || null,
};

var iface = process.env.NODE_APP_IFACE || 'eth0';

var vnstat = app (config);
var vnstat = app( config );


dotest.add ('Module', function (test) {
test ()
.isFunction ('fail', 'exports', app)
.isObject ('fail', 'interface', vnstat)
.isFunction ('fail', '.getConfig', vnstat && vnstat.getConfig)
.isFunction ('fail', '.getStats', vnstat && vnstat.getStats)
.done ();
});
dotest.add( 'Module', function ( test ) {
test()
.isFunction( 'fail', 'exports', app )
.isObject( 'fail', 'interface', vnstat )
.isFunction( 'fail', '.getConfig', vnstat && vnstat.getConfig )
.isFunction( 'fail', '.getStats', vnstat && vnstat.getStats )
.done();
} );


dotest.add ('Method .getConfig', function (test) {
vnstat.getConfig (function (err, data) {
test (err)
.isObject ('fail', 'data', data)
.isNotEmpty ('fail', 'data.DatabaseDir', data && data.DatabaseDir)
.isNotEmpty ('fail', 'data.Interface', data && data.Interface)
.done ();
});
});
dotest.add( 'Method .getConfig', function ( test ) {
vnstat.getConfig( function ( err, data ) {
test( err )
.isObject( 'fail', 'data', data )
.isNotEmpty( 'fail', 'data.DatabaseDir', data && data.DatabaseDir )
.isNotEmpty( 'fail', 'data.Interface', data && data.Interface )
.done();
} );
} );


dotest.add ('Method .getStats - iface', function (test) {
vnstat.getStats (iface, function (err, data) {
dotest.add( 'Method .getStats - iface', function ( test ) {
vnstat.getStats( iface, function ( err, data ) {
var days = data && data.traffic && data.traffic.days;
var rx = days && days [0] && days [0] .rx;

test (err)
.isObject ('fail', 'data', data)
.isString ('fail', 'data.id', data && data.id)
.isObject ('fail', 'data.traffic', data && data.traffic)
.isArray ('fail', 'data.traffic.days', days)
.isObject ('fail', 'data.traffic.days[0]', days && days [0])
.isNumber ('fail', 'data.traffic.days[0].rx', rx)
.done ();
});
});


dotest.add ('Method .getStats - all', function (test) {
vnstat.getStats (function (err, data) {
test (err)
.isArray ('fail', 'data', data)
.done ();
});
});


dotest.add ('Error: invalid interface', function (test) {
vnstat.getStats ('unreal-iface', function (err, data) {
test ()
.isError ('fail', 'err', err)
.isExactly ('fail', 'err.message', err && err.message, 'invalid interface')
.isUndefined ('fail', 'data', data)
.done ();
});
});


dotest.add ('Error: no config', function (test) {
var rx = days && days[0] && days[0].rx;

test( err )
.isObject( 'fail', 'data', data )
.isString( 'fail', 'data.id', data && data.id )
.isObject( 'fail', 'data.traffic', data && data.traffic )
.isArray( 'fail', 'data.traffic.days', days )
.isObject( 'fail', 'data.traffic.days[0]', days && days[0] )
.isNumber( 'fail', 'data.traffic.days[0].rx', rx )
.done();
} );
} );


dotest.add( 'Method .getStats - all', function ( test ) {
vnstat.getStats( function ( err, data ) {
test( err )
.isArray( 'fail', 'data', data )
.done();
} );
} );


dotest.add( 'Error: invalid interface', function ( test ) {
vnstat.getStats( 'unreal-iface', function ( err, data ) {
test()
.isError( 'fail', 'err', err )
.isExactly( 'fail', 'err.message', err && err.message, 'invalid interface' )
.isUndefined( 'fail', 'data', data )
.done();
} );
} );


dotest.add( 'Error: no config', function ( test ) {
config.bin = '-';
vnstat = app (config);
vnstat = app( config );

vnstat.getConfig (function (err, data) {
test ()
.isError ('fail', 'err', err)
.isExactly ('fail', 'err.message', err && err.message, 'no config')
.isUndefined ('fail', 'data', data)
.done ();
});
});
vnstat.getConfig( function ( err, data ) {
test()
.isError( 'fail', 'err', err )
.isExactly( 'fail', 'err.message', err && err.message, 'no config' )
.isUndefined( 'fail', 'data', data )
.done();
} );
} );


dotest.add ('Error: command failed', function (test) {
dotest.add( 'Error: command failed', function ( test ) {
config.bin = '-';
vnstat = app (config);
vnstat = app( config );

vnstat.getStats (function (err, data) {
test ()
.isError ('fail', 'err', err)
.isExactly ('fail', 'err.message', err && err.message, 'command failed')
.isUndefined ('fail', 'data', data)
.done ();
});
});
vnstat.getStats( function ( err, data ) {
test()
.isError( 'fail', 'err', err )
.isExactly( 'fail', 'err.message', err && err.message, 'command failed' )
.isUndefined( 'fail', 'data', data )
.done();
} );
} );


// Start the tests
dotest.run ();
dotest.run();
73 changes: 37 additions & 36 deletions vnstat-dumpdb.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ Feedback: https://github.com/fvdm/nodejs-vnstat-dumpdb/issues
License: Unlicense (see LICENSE file)
*/

var exec = require ('child_process') .exec;
var exec = require( 'child_process' ).exec;

var set = {
bin: 'vnstat',
iface: null,
config: {}
config: {},
};


Expand All @@ -27,12 +27,12 @@ var set = {
* @returns {void}
*/

function doError (message, err, details, callback) {
var error = new Error (message);
function doError ( message, err, details, callback ) {
var error = new Error( message );

error.error = err;
error.details = details;
callback (error);
callback( error );
}


Expand All @@ -44,31 +44,31 @@ function doError (message, err, details, callback) {
* @returns {void}
*/

function getConfig (callback) {
exec (set.bin + ' --showconfig', function (err, text) {
function getConfig ( callback ) {
exec( `${set.bin} --showconfig`, function ( err, text ) {
var config = {};
var line;
var i;

if (err) {
doError ('no config', err, text, callback);
if ( err ) {
doError( 'no config', err, text, callback );
return;
}

text = text.split ('\n');
text = text.split( '\n' );

for (i = 0; i < text.length; i++) {
line = text [i] .trim ();
for ( i = 0; i < text.length; i++ ) {
line = text[i].trim();

if (line.substr (0, 1) !== '#') {
line.replace (/(\w+)\s+(.+)/, function (s, key, val) {
config [key] = val.slice (0, 1) === '"' ? val.slice (1, -1) : val;
});
if ( line.substr( 0, 1 ) !== '#' ) {
line.replace( /(\w+)\s+(.+)/, function ( s, key, val ) {
config[key] = val.slice( 0, 1 ) === '"' ? val.slice( 1, -1 ) : val;
} );
}
}

callback (null, config);
});
callback( null, config );
} );
}


Expand All @@ -81,42 +81,43 @@ function getConfig (callback) {
* @returns {void}
*/

function getStats (iface, callback) {
function getStats ( iface, callback ) {
var i;

if (typeof iface === 'function') {
if ( typeof iface === 'function' ) {
callback = iface;
iface = set.iface;
}

exec (set.bin + ' --json', function (err, json, stderr) {
if (err) {
exec( `${set.bin} --json`, function ( err, json, stderr ) {
if ( err ) {
err.stderr = stderr;
doError ('command failed', err, json, callback);
doError( 'command failed', err, json, callback );
return;
}

try {
json = JSON.parse (json);
} catch (e) {
doError ('invalid data', e, json, callback);
json = JSON.parse( json );
}
catch ( e ) {
doError( 'invalid data', e, json, callback );
return;
}

if (iface) {
for (i = 0; i < json.interfaces.length; i++) {
if (json.interfaces [i] .id === iface) {
callback (null, json.interfaces [i]);
if ( iface ) {
for ( i = 0; i < json.interfaces.length; i++ ) {
if ( json.interfaces[i].id === iface ) {
callback( null, json.interfaces[i] );
return;
}
}

doError ('invalid interface', { iface }, json, callback);
doError( 'invalid interface', { iface }, json, callback );
return;
}

callback (null, json.interfaces);
});
callback( null, json.interfaces );
} );
}


Expand All @@ -129,15 +130,15 @@ function getStats (iface, callback) {
* @returns {object} - Module interface methods
*/

module.exports = function (setup) {
if (setup instanceof Object) {
module.exports = function ( setup ) {
if ( setup instanceof Object ) {
set.bin = setup.bin || set.bin;
set.iface = setup.iface || set.iface;
}

return {
getStats: getStats,
getConfig: getConfig,
set: set
set: set,
};
};