Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
cbidx = i;
break;
} else if (typeof arguments[i] !== "undefined") {
break;
}
}
const cb = arguments[cbidx];
const resultContainer = {result: null, startTime: null};
if (typeof cb === "function") {
// Preserve context on the callback.
// If this is one of the functions that we want to track,
// then wrap the callback with the tracking wrapper
if (cbWrapper) {
resultContainer.startTime = process.hrtime();
arguments[cbidx] = channel.bindToContext(cbWrapper(resultContainer, cb));
} else {
arguments[cbidx] = channel.bindToContext(cb);
}
}
const result = originalFunc.apply(this, arguments);
resultContainer.result = result;
return result;
};
}
listener.on("started", function(event) {
if (eventMap[event.requestId]) {
// Note: Mongo can generate 2 completely separate requests
// which share the same requestId, if a certain race condition is triggered.
// For now, we accept that this can happen and potentially miss or mislabel some events.
return;
}
contextMap[event.requestId] = channel.bindToContext((cb) => cb());
eventMap[event.requestId] = event;
});
originalRedis.RedisClient.prototype.internal_send_command = function(commandObj) {
if (commandObj) {
const cb = commandObj.callback;
if (!cb || !cb.pubsubBound) {
const address = this.address;
const startTime = process.hrtime();
// Note: augmenting the callback on internal_send_command is correct for context
// tracking, but may be too low-level for dependency tracking. There are some 'errors'
// which higher levels expect in some cases
// However, the only other option is to intercept every individual command.
commandObj.callback = channel.bindToContext(function(err, result) {
const hrDuration = process.hrtime(startTime);
/* tslint:disable-next-line:no-bitwise */
const duration = (hrDuration[0] * 1e3 + hrDuration[1] / 1e6) | 0;
channel.publish("redis", {duration, address, commandObj, err, result});
if (typeof cb === "function") {
cb.apply(this, arguments);
}
});
commandObj.callback.pubsubBound = true;
}
}
return originalSend.call(this, commandObj);
};
function patchCallback(cb?: PostgresCallback): PostgresCallback {
if (cb && cb[diagnosticOriginalFunc]) {
cb = cb[diagnosticOriginalFunc];
}
const trackingCallback = channel.bindToContext(function(err: Error, res: IPostgresResult): any {
const end = process.hrtime(start);
data.result = res && { rowCount: res.rowCount, command: res.command };
data.error = err;
data.duration = Math.ceil((end[0] * 1e3) + (end[1] / 1e6));
channel.publish("postgres", data);
// emulate weird internal behavior in pg@6
// on success, the callback is called *before* query events are emitted
// on failure, the callback is called *instead of* the query emitting events
// with no events, that means no promises (since the promise is resolved/rejected in an event handler)
// since we are always inserting ourselves as a callback, we have to restore the original
// behavior if the user didn't provide one themselves
if (err) {
if (cb) {
return cb.apply(this, arguments);
} else if (queryResult && queryResult instanceof EventEmitter) {
function getPatchedCallback(origCallback: CompletionCallback) {
const start = process.hrtime();
let data: ITediousData = {
query: {},
database: {
host: null,
port: null,
},
result: null,
error: null,
duration: 0,
};
return channel.bindToContext(function(err: Error | null, rowCount?: number | null, rows?: any) {
const end = process.hrtime(start);
data = { ...data,
database: {
host: this.connection.config.server,
port: this.connection.config.options.port,
},
result: !err && { rowCount, rows },
query: {
text: this.parametersByName.statement.value,
},
error: err,
duration: Math.ceil((end[0] * 1e3) + (end[1] / 1e6)),
};
channel.publish("tedious", data);
origCallback.call(this, err, rowCount, rows);
});
this.s.pool.logout = function contextPreservingLogout() {
if (typeof arguments[1] === "function") {
arguments[1] = channel.bindToContext(arguments[1]);
}
return originalLogout.apply(this, arguments);
};
return ret;
originalPgPool.prototype.connect = function connect(callback?: Function): void | Promise {
if (callback) {
arguments[0] = channel.bindToContext(callback);
}
return originalConnect.apply(this, arguments);
};
this.s.coreTopology.s.pool.logout = function contextPreservingLogout() {
if (typeof arguments[1] === "function") {
arguments[1] = channel.bindToContext(arguments[1]);
}
return originalLogout.apply(this, arguments);
};
return ret;