Security News
Node.js EOL Versions CVE Dubbed the "Worst CVE of the Year" by Security Experts
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
Gander at my hooks, ye Mighty, and despair!
Gander, in its most basic form, wraps itself around functions to provide execution times. Times are provided in sub-milliseconds when the precision is available (process.hrtime, performance.now) and milliseconds when not.
Generally, though, gander exposes callbacks that are invoked before and after a function is called so that you can perform thorough investigations and/or mad science.
Gander supports CommonJS/node.js and AMD/require.js imports. You can also use it in your web app via a normal script
tag and it will add itself to the window
object.
npm install gander
var gander = require('gander');
var fs = require('fs');
gander(fs, {
name: 'fs',
async: true,
ignore: ['write', 'writeFile']
});
fs.readFile(__filename, function(err, data) {
/* log messages:
* fs.open: 0.543ms
* fs.fstat: 0.301ms
* fs.read: 0.154ms
* fs.close: 0.086ms
* fs.readFile: 13.760ms
*/
fs.writeFile(__filename + '.bak', data, function(err) {
// Even though 'write' and 'writeFile' are ignored, underlying
// method calls are logged since they're not ignored
/* log messages
* fs.open: 0.364ms
* fs.close: 0.555ms
*/
});
});
define(['gander', 'backbone'], function(gander, Backbone) {
var MyView = Backbone.View.extend({
events: {
'click': 'logBoop'
},
constructor: function() {
Backbone.View.apply(this, arguments);
gander(this, {name: 'MyView', unique: true});
},
render: function() {
this.$el.html('<div>This is a view</div>');
return this;
},
logBoop: function() {
console.log('boop');
}
});
return MyView;
});
define(['views/myview'], function(MyView) {
var view = new MyView();
$('body').append(view.render().el);
// MyView1.render: 0.109ms
/*** click the view ***/
// MyView1.logBoop: 0.287ms
});
Gander takes an object and wraps all keys and prototype methods so that before
and after
callbacks are available. By default, the before
callback stores a start time. By default, the after
callback retrieves the start time and logs the diff in time. These defaults make gander a nice way of doing timing tests.
String
- The name of the object. This is for your convenience so you can keep track of what's going on.Boolean
- If set to true
, gander will add an integer to the end of the name
in the cases where the name is reused by multiple instantiated objects.Boolean
- If set to true
, gander will wrap callbacks where a callback is a function in the last argument position.Boolean
- If set to true
, gander will also wrap promises returned from methods on an object.Array.<String>
- Gander will ignore any function/method with a name in the ignore
array.Function(name, method, arg1, arg2, ..., argn)
- Gander will call before
immediately prior to calling the underlying function. After the name
and method
parameters is the complete set of parameters passed to the underlying function.Function(name, method, arg1, arg2, ..., argn, returnValue)
- Gander will call after
immediately after calling the underlying function. The function parameters are the same as before
but now include the return value from the underlying function as the last argument.Function(name, method, arg1, arg2, ..., argn, callbackArg1, ..., callbackArgn)
- Gander will call after
immediately prior to calling the underlying callback function. The function parameters to after
are the same as before
but now include the arguments to be passed to the underlying callback.Function(name, method, timeInMs)
- If you wish to hook into a custom logger, pass in a function that will be called with the name
of the object, the method
being called, and the timeInMs
it took to execute the function.var gander = require('gander');
var TestObject = function() {
this.num = 1;
};
TestObject.prototype.increment = function(val) {
this.num += val;
return this.num;
};
var obj = new TestObject();
/**
* A horribly contrived example where in `before` we decrement `this.num` by the amount
* to be passed in by `val`. `increment` will then return `this.num` to its
* original value. The `after` function will then set things right by incrementing
* `this.num` by `val`.
*/
gander(obj, {
name: 'testObject',
before: function(name, method, val) {
this.num -= val;
console.log('before: this.num:', this.num);
},
after: function(name, method, val, returnValue) {
this.num += val;
console.log('after: this.num:', this.num);
}
});
obj.increment(2);
console.log('obj.num:', obj.num);
// before: this.num: -1
// after: this.num: 3
// obj.num: 3
var winston = require('winston');
var fs = require('fs');
var gander = require('gander');
gander(fs, {
async: true,
logger: function(name, method, timeInMs) {
winston.info(name + '.' + method, timeInMs);
}
});
fs.readFile(__filename, function(err, data) {
/* log messages:
* info: fs.open 0.491
* info: fs.fstat 0.782
* info: fs.read 0.147
* info: fs.close 0.132
* info: fs.readFile 16.468
*/
});
Copyright (c) 2014 Michael Maelzer
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
FAQs
A function timing library and so much more
We found that gander demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
Security News
cURL and Go security teams are publicly rejecting CVSS as flawed for assessing vulnerabilities and are calling for more accurate, context-aware approaches.
Security News
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.