Skip to content

Instantly share code, notes, and snippets.

@mjpearson
Created October 14, 2014 18:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mjpearson/5f2b8ded61dd9879779a to your computer and use it in GitHub Desktop.
Save mjpearson/5f2b8ded61dd9879779a to your computer and use it in GitHub Desktop.
ldapjs client example - heapdump + gc
#!/usr/bin/env node
var memwatch = require('memwatch'),
heapdump = require('heapdump'),
_ = require('underscore'),
ldap = require('ldapjs');
function dump() {
var f = '/tmp/node_' + process.pid + '_' + Date.now() + '.heapsnapshot';
console.log('Writing Heap Snapshot ' + f);
global.gc();
heapdump.writeSnapshot(f);
}
memwatch.on('leak', function(info) {
console.error(info);
dump();
});
var ldapConfig = {
"server": {
"url": "ldap://ldap.example.org"
},
"base": "dc=example,dc=org",
"search": {
"filter": "(uid={{username}})",
"scope": "sub"
}
};
var username = 'my_user';
var password = 'my_password';
var cb = function(message) {
console.log(message);
}
var ldapCompare = function(ldapClient, dn) {
ldapClient.compare(dn, 'userPassword', password, function(err, pass ) {
if (err) {
cb(err);
} else if (!pass) {
cb('Not Authorized')
} else {
cb('OK!');
}
});
}
var auth = function(i, next) {
if (i === 999 || i === 4999) {
dump();
}
var ldapClient = ldap.createClient(ldapConfig.server),
search = _.clone(ldapConfig.search);
if (search.filter) {
search.filter = search.filter.replace(/{{username}}/, username);
}
ldapClient.search(ldapConfig.base, search, function(err, res) {
if (err) {
next(err);
} else {
res.on('searchEntry', function(entry) {
ldapCompare(ldapClient, entry.dn);
});
}
});
}
dump();
var limit = 5000;
setTimeout(function() {
for (var i = 0; i < limit; i++) {
auth(i, cb);
if (i === limit - 1) {
setTimeout(function() {
dump();
process.exit();
}, 5000);
}
}
}, 1000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment