Skip to content
This repository was archived by the owner on May 17, 2021. It is now read-only.
Merged
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
37 changes: 33 additions & 4 deletions lib/native-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,6 @@ class NativeClient extends EventEmitter {
* @param {Function} callback - The callback.
*/
command(databaseName, comm, callback) {
debug('running command', { databaseName, comm });
var db = this._database(databaseName);
db.command(comm, (error, result) => {
if (error) {
Expand Down Expand Up @@ -497,7 +496,6 @@ class NativeClient extends EventEmitter {
* Disconnect the client.
*/
disconnect(callback) {
debug('disconnect()');
this.client.close(true, callback);
}

Expand Down Expand Up @@ -759,7 +757,6 @@ class NativeClient extends EventEmitter {
* @return {Stream} The sample stream.
*/
sample(ns, options) {
debug('getting sample', { ns, options });
var db = this._database(this._databaseName(ns));
return createSampleStream(db, this._collectionName(ns), options);
}
Expand Down Expand Up @@ -1174,4 +1171,36 @@ class NativeClient extends EventEmitter {
}
}

module.exports = NativeClient;
function addDebugToClass(cls) {
if (!debug.enabled) {
return cls;
}

const proto = cls.prototype;
for (const prop of Object.getOwnPropertyNames(proto)) {
if (prop.startsWith('_')) {
continue;
}
const descriptor = Object.getOwnPropertyDescriptor(proto, prop);
if (typeof descriptor.value !== 'function') {
continue;
}

const orig = descriptor.value;
descriptor.value = function(...args) {
debug(`${prop}()`, args);
if (args.length > 0 & typeof args[args.length - 1] === 'function') {
const origCallback = args[args.length - 1];
args[args.length - 1] = function(...callbackArgs) {
debug(`${prop}()`, args, 'finished ->', callbackArgs);
return origCallback.call(this, ...callbackArgs);
};
}
return orig.call(this, ...args);
};
Object.defineProperty(proto, prop, descriptor);
}
return cls;
}

module.exports = addDebugToClass(NativeClient);