const mongodbcorePatchFunction = function()

in src/diagnostic-channel-publishers/src/mongodb.pub.ts [115:145]


const mongodbcorePatchFunction = function(originalMongo) {
    const originalConnect = originalMongo.Server.prototype.connect;
    originalMongo.Server.prototype.connect = function contextPreservingConnect() {
        const ret = originalConnect.apply(this, arguments);

        // Messages sent to mongo progress through a pool
        // This can result in context getting mixed between different responses
        // so we wrap the callbacks to restore appropriate state
        const originalWrite = this.s.coreTopology.s.pool.write;
        this.s.coreTopology.s.pool.write = function contextPreservingWrite() {
            const cbidx = typeof arguments[1] === "function" ? 1 : 2;
            if (typeof arguments[cbidx] === "function" ) {
                arguments[cbidx] = channel.bindToContext(arguments[cbidx]);
            }
            return originalWrite.apply(this, arguments);
        };

        // Logout is a special case, it doesn't call the write function but instead
        // directly calls into connection.write
        const originalLogout = this.s.coreTopology.s.pool.logout;
        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;
    };

    return originalMongo;
};